Parsee/src/Main.c
LDA bdb4fd2f68 [MOD] One-way Matrix->XMPP comms.
This is not sanitised. I need to make an XML writer.
2024-06-17 18:26:37 +02:00

105 lines
2.3 KiB
C

#include <Cytoplasm/HttpServer.h>
#include <Cytoplasm/Log.h>
#include <string.h>
#include <signal.h>
#include <Parsee.h>
#include <XMPP.h>
#include <XML.h>
#include <AS.h>
static HttpServer *server = NULL;
static void
SignalHandler(int signal)
{
size_t i;
switch (signal)
{
case SIGPIPE:
return;
case SIGTERM:
case SIGINT:
if (!server)
{
return;
}
HttpServerStop(server);
break;
}
}
int
Main(void)
{
HttpServerConfig conf;
ParseeData *data;
const ParseeConfig *parsee_conf;
Stream *yaml;
XMPPComponent *jabber;
struct sigaction sigAction;
Log(LOG_INFO, "%s - v%s", NAME, VERSION);
ParseeConfigLoad("parsee.json");
ParseeConfigInit();
yaml = StreamOpen("parsee.yaml", "w");
ParseeExportConfigYAML(yaml);
StreamClose(yaml);
parsee_conf = ParseeConfigGet();
{
jabber = XMPPInitialiseCompStream(
parsee_conf->component_host,
parsee_conf->component_port
);
XMPPAuthenticateCompStream(
jabber,
parsee_conf->shared_comp_secret
);
}
Log(LOG_INFO, "HS token: %s", parsee_conf->hs_token);
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;
sigAction.sa_handler = SignalHandler;
sigfillset(&sigAction.sa_mask);
sigAction.sa_flags = SA_RESTART;
#define SIGACTION(sig, act, oact) \
if (sigaction(sig, act, oact) < 0) \
{ \
Log(LOG_ERR, "Unable to install signal handler: %s", #sig); \
} \
else \
{ \
Log(LOG_DEBUG, "Installed signal handler: %s", #sig); \
}
SIGACTION(SIGINT, &sigAction, NULL);
SIGACTION(SIGTERM, &sigAction, NULL);
SIGACTION(SIGPIPE, &sigAction, NULL);
SIGACTION(SIGUSR1, &sigAction, NULL);
#undef SIGACTION
/* TODO: The rest of Parsee. */
server = HttpServerCreate(&conf);
HttpServerStart(server);
/* TODO: Manage signals(^C) with finesse */
HttpServerJoin(server);
HttpServerFree(server);
ParseeConfigFree();
ParseeFreeData(conf.handlerArgs);
return 0;
}