[ADD] Make and start using an XML encoder

Now, we have basic sanitation!!!!!!
This commit is contained in:
LDA 2024-06-19 02:33:53 +02:00
commit b408cbf224
4 changed files with 110 additions and 19 deletions

View file

@ -47,12 +47,14 @@ Main(void)
ParseeConfigLoad("parsee.json"); ParseeConfigLoad("parsee.json");
ParseeConfigInit(); ParseeConfigInit();
/* Write out the config file to a YAML document */
yaml = StreamOpen("parsee.yaml", "w"); yaml = StreamOpen("parsee.yaml", "w");
ParseeExportConfigYAML(yaml); ParseeExportConfigYAML(yaml);
StreamClose(yaml); StreamClose(yaml);
parsee_conf = ParseeConfigGet(); parsee_conf = ParseeConfigGet();
{ {
Log(LOG_NOTICE, "Connecting to XMPP...");
jabber = XMPPInitialiseCompStream( jabber = XMPPInitialiseCompStream(
parsee_conf->component_host, parsee_conf->component_host,
parsee_conf->component_port parsee_conf->component_port
@ -63,6 +65,7 @@ Main(void)
); );
} }
Log(LOG_NOTICE, "Setting up local Matrix user...");
ASRegisterUser(parsee_conf, parsee_conf->sender_localpart); ASRegisterUser(parsee_conf, parsee_conf->sender_localpart);
memset(&conf, 0, sizeof(conf)); memset(&conf, 0, sizeof(conf));
@ -72,6 +75,7 @@ Main(void)
conf.handlerArgs = ParseeInitData(jabber); conf.handlerArgs = ParseeInitData(jabber);
conf.handler = ParseeRequest; conf.handler = ParseeRequest;
Log(LOG_NOTICE, "Creating XMPP listener thread...");
if (pthread_create(&xmpp_thr, NULL, ParseeXMPPThread, conf.handlerArgs)) if (pthread_create(&xmpp_thr, NULL, ParseeXMPPThread, conf.handlerArgs))
{ {
Log(LOG_ERR, "Couldn't start XMPP listener thread."); Log(LOG_ERR, "Couldn't start XMPP listener thread.");
@ -99,11 +103,8 @@ Main(void)
#undef SIGACTION #undef SIGACTION
/* TODO: The rest of Parsee. */
server = HttpServerCreate(&conf); server = HttpServerCreate(&conf);
HttpServerStart(server); HttpServerStart(server);
/* TODO: Manage signals(^C) with finesse */
HttpServerJoin(server); HttpServerJoin(server);
HttpServerFree(server); HttpServerFree(server);

View file

@ -1,5 +1,7 @@
#include <XML.h> #include <XML.h>
#include <string.h>
XMLElement * XMLElement *
XMLDecode(Stream *stream, bool autofree) XMLDecode(Stream *stream, bool autofree)
{ {
@ -88,9 +90,90 @@ XMLDecode(Stream *stream, bool autofree)
return ret; return ret;
} }
void
XMLEncodeString(Stream *stream, char *data)
{
size_t i;
for (i = 0; i < strlen(data); i++)
{
char c = data[i];
if (c == '<')
{
StreamPrintf(stream, "&lt;");
continue;
}
else if (c == '>')
{
StreamPrintf(stream, "&gt;");
continue;
}
else if (c == '&')
{
StreamPrintf(stream, "&amp;");
continue;
}
else if (c == '\'')
{
StreamPrintf(stream, "&apos;");
continue;
}
else if (c == '"')
{
StreamPrintf(stream, "&quot;");
continue;
}
StreamPrintf(stream, "%c", c);
}
}
void
XMLEncodeTag(Stream *stream, XMLElement *element)
{
char *key, *val;
StreamPrintf(stream, "<%s", element->name);
while (HashMapIterate(element->attrs, &key, (void **) &val))
{
StreamPrintf(stream, " %s='", key);
XMLEncodeString(stream, val);
StreamPrintf(stream, "'");
}
if (ArraySize(element->children))
{
size_t i;
XMLElement *child;
StreamPrintf(stream, ">", key, val);
for (i = 0; i < ArraySize(element->children); i++)
{
child = ArrayGet(element->children, i);
XMLEncode(stream, child);
}
StreamPrintf(stream, "</%s>", element->name);
return;
}
StreamPrintf(stream, "/>");
}
void void
XMLEncode(Stream *stream, XMLElement *element) XMLEncode(Stream *stream, XMLElement *element)
{ {
/* TODO: Write the entire XML element. This shouldn't be /* TODO: Write the entire XML element. This shouldn't be
* too hard. */ * too hard. */
if (!stream || !element)
{
return;
}
switch (element->type)
{
case XML_ELEMENT_TAG:
XMLEncodeTag(stream, element);
return;
case XML_ELEMENT_DATA:
XMLEncodeString(stream, element->data);
return;
default:
/* TODO */
break;
}
} }

View file

@ -1,29 +1,35 @@
#include <XMPP.h> #include <XMPP.h>
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Str.h>
#include <XML.h>
void void
XMPPSendPlain(XMPPComponent *comp, char *fr, char *to, char *msg, char *type) XMPPSendPlain(XMPPComponent *comp, char *fr, char *to, char *msg, char *type)
{ {
XMLElement *message, *body, *data;
char *from;
if (!comp || !fr || !to || !msg) if (!comp || !fr || !to || !msg)
{ {
return; return;
} }
StreamPrintf(
comp->stream, "<message from='%s@%s' to='%s'", message = XMLCreateTag("message");
fr, comp->host, to XMLAddAttr(message, "from", (from = StrConcat(3, fr, "@", comp->host)));
); XMLAddAttr(message, "to", to);
if (type) XMLAddAttr(message, "type", type);
{
StreamPrintf(comp->stream, " type='%s'", type); body = XMLCreateTag("body");
} data = XMLCreateText(msg);
StreamPrintf(comp->stream, ">");
StreamPrintf(comp->stream, XMLAddChild(message, body);
"<body>" XMLAddChild(body, data);
"%s"
"</body>" XMLEncode(comp->stream, message);
"</message>",
msg
);
StreamFlush(comp->stream); StreamFlush(comp->stream);
XMLFreeElement(message);
Free(from);
} }
void void
XMPPSendPresence(XMPPComponent *comp, char *fr, char *to) XMPPSendPresence(XMPPComponent *comp, char *fr, char *to)

View file

@ -31,6 +31,7 @@ ParseeXMPPThread(void *argp)
{ {
continue; continue;
} }
if (StrEquals(stanza->name, "message")) if (StrEquals(stanza->name, "message"))
{ {
size_t i; size_t i;