Parsee/src/Main.c

112 lines
2.8 KiB
C

#include <Cytoplasm/HttpServer.h>
#include <Cytoplasm/Cytoplasm.h>
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Util.h>
#include <Cytoplasm/Cron.h>
#include <Cytoplasm/Log.h>
#include <Cytoplasm/Str.h>
#include <pthread.h>
#include <string.h>
#include <signal.h>
#include <Parsee.h>
#include <XMPP.h>
#include <XML.h>
#include <AS.h>
static HttpServer *server = NULL;
static pthread_t xmpp_thr;
static XMPPComponent *jabber = NULL;
int
Main(void)
{
HttpServerConfig conf;
ParseeData *data = NULL;
const ParseeConfig *parsee_conf;
Stream *yaml;
Cron *cron = NULL;
Log(LOG_INFO,
"%s - v%s (Cytoplasm %s)",
NAME, VERSION, CytoplasmGetVersionStr()
);
Log(LOG_INFO, "=======================");
LogConfigIndent(LogConfigGlobal());
ParseeConfigLoad("parsee.json");
ParseeConfigInit();
/* Write out the config file to a YAML document */
yaml = StreamOpen("parsee.yaml", "w");
ParseeExportConfigYAML(yaml);
StreamClose(yaml);
parsee_conf = ParseeConfigGet();
{
Log(LOG_NOTICE, "Connecting to XMPP...");
jabber = XMPPInitialiseCompStream(
parsee_conf->component_host,
parsee_conf->component_port
);
if (!XMPPAuthenticateCompStream(
jabber,
parsee_conf->shared_comp_secret
))
{
Log(LOG_ERR, "Could not connect to XMPP...");
XMPPEndCompStream(jabber);
goto end;
}
}
Log(LOG_NOTICE, "Setting up local Matrix user...");
ASRegisterUser(parsee_conf, parsee_conf->sender_localpart);
memset(&conf, 0, sizeof(conf));
conf.port = parsee_conf->port;
conf.threads = 4;
conf.maxConnections = 32;
conf.handlerArgs = ParseeInitData(jabber);
conf.handler = ParseeRequest;
Log(LOG_NOTICE, "Starting up local cronjobs...");
cron = CronCreate( 10 SECONDS );
CronEvery(cron, 30 MINUTES, ParseeCleanup, conf.handlerArgs);
Log(LOG_NOTICE, "Creating XMPP listener thread...");
if (pthread_create(&xmpp_thr, NULL, ParseeXMPPThread, conf.handlerArgs))
{
Log(LOG_ERR, "Couldn't start XMPP listener thread.");
goto end;
}
Log(LOG_NOTICE, "Listening to MUCs...");
LogConfigIndent(LogConfigGlobal());
ParseeSendPresence(conf.handlerArgs);
LogConfigUnindent(LogConfigGlobal());
server = HttpServerCreate(&conf);
if (!ParseeInitialiseSignals(server, xmpp_thr, jabber))
{
goto end;
}
HttpServerStart(server);
LogConfigUnindent(LogConfigGlobal());
Log(LOG_INFO, "=======================");
HttpServerJoin(server);
end:
Log(LOG_INFO, "Exiting...");
HttpServerFree(server);
ParseeConfigFree();
CronStop(cron);
CronFree(cron);
ParseeFreeData(conf.handlerArgs);
return 0;
}