[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");
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
@ -63,6 +65,7 @@ Main(void)
);
}
Log(LOG_NOTICE, "Setting up local Matrix user...");
ASRegisterUser(parsee_conf, parsee_conf->sender_localpart);
memset(&conf, 0, sizeof(conf));
@ -72,6 +75,7 @@ Main(void)
conf.handlerArgs = ParseeInitData(jabber);
conf.handler = ParseeRequest;
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.");
@ -99,11 +103,8 @@ Main(void)
#undef SIGACTION
/* TODO: The rest of Parsee. */
server = HttpServerCreate(&conf);
HttpServerStart(server);
/* TODO: Manage signals(^C) with finesse */
HttpServerJoin(server);
HttpServerFree(server);

View file

@ -1,5 +1,7 @@
#include <XML.h>
#include <string.h>
XMLElement *
XMLDecode(Stream *stream, bool autofree)
{
@ -88,9 +90,90 @@ XMLDecode(Stream *stream, bool autofree)
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
XMLEncode(Stream *stream, XMLElement *element)
{
/* TODO: Write the entire XML element. This shouldn't be
* 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 <Cytoplasm/Memory.h>
#include <Cytoplasm/Str.h>
#include <XML.h>
void
XMPPSendPlain(XMPPComponent *comp, char *fr, char *to, char *msg, char *type)
{
XMLElement *message, *body, *data;
char *from;
if (!comp || !fr || !to || !msg)
{
return;
}
StreamPrintf(
comp->stream, "<message from='%s@%s' to='%s'",
fr, comp->host, to
);
if (type)
{
StreamPrintf(comp->stream, " type='%s'", type);
}
StreamPrintf(comp->stream, ">");
StreamPrintf(comp->stream,
"<body>"
"%s"
"</body>"
"</message>",
msg
);
message = XMLCreateTag("message");
XMLAddAttr(message, "from", (from = StrConcat(3, fr, "@", comp->host)));
XMLAddAttr(message, "to", to);
XMLAddAttr(message, "type", type);
body = XMLCreateTag("body");
data = XMLCreateText(msg);
XMLAddChild(message, body);
XMLAddChild(body, data);
XMLEncode(comp->stream, message);
StreamFlush(comp->stream);
XMLFreeElement(message);
Free(from);
}
void
XMPPSendPresence(XMPPComponent *comp, char *fr, char *to)

View file

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