Parsee/src/XMPP/Stanza.c
2024-06-23 23:46:46 +02:00

184 lines
4.5 KiB
C

#include <XMPP.h>
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Str.h>
#include <Cytoplasm/Log.h>
#include <XML.h>
void
XMPPSendPlain(XMPPComponent *comp, char *fr, char *to, char *msg, char *type, char *rst, char *rse)
{
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(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;
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);
/* TODO: Add custom fields depending on the caller's wishes */
}
if (rst && rse)
{
XMLElement *reply = XMLCreateTag("reply");
XMLAddAttr(reply, "to", rse);
XMLAddAttr(reply, "id", rst);
XMLAddAttr(reply, "xmlns", "urn:xmpp:reply:0");
XMLAddChild(message, reply);
}
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");
}