Parsee/src/XMPPThread/PEPs/VCard.c
LDA ca87972b3a [ADD/WIP] Push all the Janet changes
This is still unstable(and I still need to design/document the exposed
API)! Do(n't) go and use it!
2024-11-16 14:11:32 +01:00

117 lines
3.6 KiB
C

#include "XMPPThread/internal.h"
#include <Parsee.h>
#include <XMPP.h>
#include <XML.h>
#include <AS.h>
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Str.h>
#include <Cytoplasm/Log.h>
#include <string.h>
#define AddFieldBase(type) \
XMLElement *field, *text, *text_data; \
field = XMLCreateTag(name); \
text = XMLCreateTag(type); \
text_data = XMLCreateText(value); \
\
XMLAddChild(vcard, field); \
XMLAddChild(field, text); \
XMLAddChild(text, text_data)
static void
AddTextField(XMLElement *vcard, char *name, char *value)
{
AddFieldBase("text");
}
static void
AddURIField(XMLElement *vcard, char *name, char *value)
{
AddFieldBase("uri");
}
#undef AddFieldBase
void
PEPVCardEvent(PEPManager *m, XMLElement *stanza, XMLElement *item)
{
XMPPThreadInfo *info = PEPManagerCookie(m);
XMPPComponent *jabber = info->jabber;
ParseeData *data = info->args;
char *from = HashMapGet(stanza->attrs, "from");
char *to = HashMapGet(stanza->attrs, "to");
char *id = HashMapGet(stanza->attrs, "id");
XMLElement *reply;
reply = XMLCreateTag("iq");
XMLAddAttr(reply, "type", "result");
XMLAddAttr(reply, "id", id);
XMLAddAttr(reply, "from", to);
XMLAddAttr(reply, "to", from);
{
XMLElement *pubsub, *items, *item;
pubsub = XMLCreateTag("pubsub");
items = XMLCreateTag("items");
item = XMLCreateTag("item");
XMLAddChild(reply, pubsub);
XMLAddChild(pubsub, items);
XMLAddChild(items, item);
XMLAddAttr(pubsub, "xmlns", "http://jabber.org/protocol/pubsub");
XMLAddAttr(items, "node", "urn:xmpp:vcard4");
{
XMLElement *vcard = XMLCreateTag("vcard");
XMLAddAttr(vcard, "xmlns", "urn:ietf:params:xml:ns:vcard-4.0");
if (!strncmp(to, "parsee", 6))
{
AddTextField(vcard, "title", "System user");
AddTextField(vcard, "fn", "Parsee Mizuhashi");
AddTextField(vcard, "nickname", "parsee");
AddTextField(vcard, "nickname", "that annoying bridge");
AddTextField(vcard, "nickname", "green-eyed monster");
AddURIField(vcard, "url", REPOSITORY);
AddURIField(vcard, "url", "https://kappach.at/parsee");
AddTextField(vcard,
"note",
"This is the Parsee system account. " "\n"
"Admins may execute ad-hoc commands through this "
"account."
);
}
else
{
/* TODO: Get the user's info(over the Matrix profile API if
* the server shows support for it.) */
char *mxid = ParseeDecodeMXID(to);
char *name = ASGetName(data->config, NULL, mxid);
char *m_to = ParseeGenerateMTO(mxid);
AddTextField(vcard, "title", "Matrix user");
AddTextField(vcard, "fn", name);
AddTextField(vcard, "nickname", mxid);
AddURIField(vcard, "url", "https://kappach.at/parsee");
AddURIField(vcard, "url", m_to);
AddTextField(
vcard,
"note",
"This is a bridged Matrix user, from " NAME "."
);
Free(mxid);
Free(name);
Free(m_to);
}
XMLAddChild(item, vcard);
}
}
XMPPSendStanza(jabber, reply, data->config->max_stanza_size);
XMLFreeElement(reply);
(void) item;
}