Parsee/src/XMPP/Stanza.c
LDA 8472ada953 [ADD/MOD] Help/stats and two-way modification
More on the Reverse Ideology!
2024-06-29 19:10:28 +02:00

270 lines
6.9 KiB
C

#include <XMPP.h>
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Str.h>
#include <Cytoplasm/Log.h>
#include <Parsee.h>
#include <XML.h>
void
XMPPSendPlain(XMPPComponent *comp, char *fr, char *to, char *msg, char *type, char *rst, char *rse, char *event_id, char *oob, char *edit)
{
XMLElement *message, *body, *data, *parsee;
char *from;
if (!comp || !fr || !to || !msg)
{
return;
}
pthread_mutex_lock(&comp->write_lock);
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(oob ? oob : msg);
/* TODO: Add Parsee specific fields here */
parsee = XMLCreateTag("x-parsee");
{
XMLElement *parsee_version, *ver_elem;
XMLElement *parsee_link, *link_elem;
XMLElement *parsee_text, *text_elem;
XMLElement *parsee_event, *event_elem;
parsee_version = XMLCreateTag("version");
ver_elem = XMLCreateText(VERSION);
XMLAddChild(parsee_version, ver_elem);
XMLAddChild(parsee, parsee_version);
parsee_link = XMLCreateTag("repository");
link_elem = XMLCreateText(REPOSITORY);
XMLAddChild(parsee_link, link_elem);
XMLAddChild(parsee, parsee_link);
parsee_text = XMLCreateTag("zayds-note");
text_elem = XMLCreateText("\"LDA HANG YOURSELF\" - Zayd");
XMLAddChild(parsee_text, text_elem);
XMLAddChild(parsee, parsee_text);
parsee_text = XMLCreateTag("mcnebs-note");
text_elem = XMLCreateText("LDA will never beat the allegations");
XMLAddChild(parsee_text, text_elem);
XMLAddChild(parsee, parsee_text);
if (event_id)
{
parsee_event = XMLCreateTag("event-id");
event_elem = XMLCreateText(event_id);
XMLAddChild(parsee_event, event_elem);
XMLAddChild(parsee, parsee_event);
}
/* TODO: Add custom fields depending on the caller's wishes */
}
if (edit)
{
XMLElement *xedit = XMLCreateTag("replace");
XMLAddAttr(xedit, "xmlns", "urn:xmpp:message-correct:0");
XMLAddAttr(xedit, "id", edit);
XMLAddChild(message, xedit);
}
if (oob)
{
XMLElement *xoob, *oob_data, *oob_url;
xoob = XMLCreateTag("x");
XMLAddAttr(xoob, "xmlns", "jabber:x:oob");
oob_url = XMLCreateTag("url");
oob_data = XMLCreateText(oob);
XMLAddChild(oob_url, oob_data);
XMLAddChild(xoob, oob_url);
XMLAddChild(message, xoob);
}
if (rst && rse)
{
int off = ParseeFindDatastart(msg);
char *ostr = StrInt(off);
XMLElement *reply = XMLCreateTag("reply");
XMLElement *fallback = XMLCreateTag("fallback");
XMLElement *fall_body = XMLCreateTag("body");
XMLAddAttr(reply, "to", rse);
XMLAddAttr(reply, "id", rst);
XMLAddAttr(reply, "xmlns", "urn:xmpp:reply:0");
XMLAddAttr(fallback, "xmlns", "urn:xmpp:fallback:0");
XMLAddAttr(fallback, "for", "urn:xmpp:reply:0");
XMLAddAttr(fall_body, "start", "0");
XMLAddAttr(fall_body, "end", ostr);
XMLAddChild(fallback, fall_body);
XMLAddChild(message, reply);
XMLAddChild(message, fallback);
Free(ostr);
}
XMLAddChild(message, body);
XMLAddChild(message, parsee);
XMLAddChild(body, data);
XMLEncode(comp->stream, message);
StreamFlush(comp->stream);
XMLFreeElement(message);
Free(from);
pthread_mutex_unlock(&comp->write_lock);
}
void
XMPPSendMUC(XMPPComponent *comp, char *fr, char *as, char *to, char *msg, char *type)
{
XMLElement *message, *body, *data;
char *from, *id;
if (!comp || !fr || !to || !as || !msg)
{
return;
}
pthread_mutex_lock(&comp->write_lock);
message = XMLCreateTag("message");
XMLAddAttr(message, "from",
(from = StrConcat(5, fr, "@", comp->host, "/", as)));
XMLAddAttr(message, "to", to);
XMLAddAttr(message, "type", type);
XMLAddAttr(message, "id", (id = StrRandom(8)));
body = XMLCreateTag("body");
data = XMLCreateText(msg);
XMLAddChild(message, body);
XMLAddChild(body, data);
XMLEncode(comp->stream, message);
StreamFlush(comp->stream);
XMLFreeElement(message);
Free(from);
Free(id);
pthread_mutex_unlock(&comp->write_lock);
}
void
XMPPJoinMUC(XMPPComponent *comp, char *fr, char *muc)
{
XMLElement *presence, *x;
char *from, *id;
if (!comp || !fr || !muc)
{
return;
}
pthread_mutex_lock(&comp->write_lock);
presence = XMLCreateTag("presence");
x = XMLCreateTag("x");
XMLAddAttr(presence, "from", (from = StrConcat(3, fr, "@", comp->host)));
XMLAddAttr(presence, "to", muc);
XMLAddAttr(presence, "id", (id = StrRandom(8)));
XMLAddAttr(x, "xmlns", "http://jabber.org/protocol/muc");
XMLAddChild(presence, x);
XMLEncode(comp->stream, presence);
StreamFlush(comp->stream);
XMLFreeElement(presence);
Free(from);
Free(id);
pthread_mutex_unlock(&comp->write_lock);
}
void
XMPPKillThread(XMPPComponent *jabber, char *killer)
{
XMLElement *message, *body, *data;
char *from;
if (!jabber || !killer)
{
return;
}
pthread_mutex_lock(&jabber->write_lock);
from = StrConcat(3, killer, "@", jabber->host);
message = XMLCreateTag("message");
XMLAddAttr(message, "from", from);
XMLAddAttr(message, "to", from);
XMLAddAttr(message, "type", "kill_parsee");
body = XMLCreateTag("body");
XMLAddChild(message, body);
XMLEncode(jabber->stream, message);
StreamFlush(jabber->stream);
XMLFreeElement(message);
Free(from);
pthread_mutex_unlock(&jabber->write_lock);
}
bool
XMPPIsKiller(XMLElement *stanza)
{
if (!stanza)
{
return false;
}
return StrEquals( HashMapGet(stanza->attrs, "from"),
HashMapGet(stanza->attrs, "to"));
}
bool
XMPPIsParseeStanza(XMLElement *stanza)
{
if (!stanza)
{
return false;
}
return !!XMLookForUnique(stanza, "x-parsee");
}
char *
XMPPGetStanzaID(XMLElement *stanza)
{
XMLElement *stanza_id;
if (!stanza)
{
return NULL;
}
stanza_id = XMLookForTKV(stanza, "stanza-id", "xmlns", "urn:xmpp:sid:0");
return stanza_id ? HashMapGet(stanza_id->attrs, "id") : NULL;
}
char *
XMPPGetOriginID(XMLElement *stanza)
{
XMLElement *origin_id;
if (!stanza)
{
return NULL;
}
origin_id = XMLookForTKV(stanza, "origin-id", "xmlns", "urn:xmpp:sid:0");
return origin_id ? HashMapGet(origin_id->attrs, "id") : NULL;
}
char *
XMPPGetReplacedID(XMLElement *stanza)
{
XMLElement *replace = XMLookForTKV(
stanza, "replace",
"xmlns", "urn:xmpp:message-correct:0"
);
return replace ? HashMapGet(replace->attrs, "id") : NULL;
}