[ADD] Shove XML stanza, use show field, rename OOB

Also, https://at.kappach.at/
This commit is contained in:
LDA 2024-07-10 15:49:15 +02:00
commit be15a67cba

View file

@ -183,7 +183,7 @@ static char *
ParseeGetBridgedRoom(ParseeData *data, XMLElement *stanza) ParseeGetBridgedRoom(ParseeData *data, XMLElement *stanza)
{ {
char *to = ParseeDecodeMXID(HashMapGet(stanza->attrs, "to")); char *to = ParseeDecodeMXID(HashMapGet(stanza->attrs, "to"));
char *from = (HashMapGet(stanza->attrs, "from")); char *from = HashMapGet(stanza->attrs, "from");
char *chat_id = ParseeGetFromMUCID(data, from); char *chat_id = ParseeGetFromMUCID(data, from);
char *mroom_id = ParseeGetRoomID(data, chat_id); char *mroom_id = ParseeGetRoomID(data, chat_id);
char *ret; char *ret;
@ -400,6 +400,25 @@ end:
DbUnlock(args->db, avatars); DbUnlock(args->db, avatars);
} }
static HashMap *
ShoveStanza(HashMap *content, XMLElement *stanza)
{
char *encoded_stanza = NULL;
Stream *str_writer = StrStreamWriter(&encoded_stanza);
XMLEncode(str_writer, stanza);
StreamFlush(str_writer);
StreamClose(str_writer);
JsonFree(HashMapSet(content,
"at.kappach.at.parsee.stanza",
JsonValueString(encoded_stanza)
));
Free(encoded_stanza);
return content;
}
static bool static bool
MessageStanza(ParseeData *args, XMLElement *stanza, XMPPThread *thr) MessageStanza(ParseeData *args, XMLElement *stanza, XMPPThread *thr)
{ {
@ -467,10 +486,8 @@ MessageStanza(ParseeData *args, XMLElement *stanza, XMPPThread *thr)
for (i = 0; i < ArraySize(items->children); i++) for (i = 0; i < ArraySize(items->children); i++)
{ {
ManageProfileItem( ManageProfileItem(
args, args, ArrayGet(items->children, i),
ArrayGet(items->children, i), stanza, thr
stanza,
thr
); );
} }
} }
@ -630,9 +647,11 @@ MessageStanza(ParseeData *args, XMLElement *stanza, XMPPThread *thr)
content = MatrixCreateMedia(mxc, data->data, mime); content = MatrixCreateMedia(mxc, data->data, mime);
/* Yeah, no, I'm not modifying the media creation code. */ /* Yeah, no, I'm not modifying the media creation code. */
HashMapSet(content, "external_url", HashMapSet(content,
"at.kappach.at.parsee.external",
JsonValueString(oob_data->data) JsonValueString(oob_data->data)
); );
ShoveStanza(content, stanza);
event_id = ASSend( event_id = ASSend(
args->config, mroom_id, encoded, args->config, mroom_id, encoded,
@ -657,7 +676,10 @@ MessageStanza(ParseeData *args, XMLElement *stanza, XMPPThread *thr)
Free(ASSend( Free(ASSend(
args->config, mroom_id, encoded, args->config, mroom_id, encoded,
"m.reaction", "m.reaction",
MatrixCreateReact(event_id, react_data->data) ShoveStanza(
MatrixCreateReact(event_id, react_data->data),
stanza
)
)); ));
} }
Free(event_id); Free(event_id);
@ -687,6 +709,7 @@ MessageStanza(ParseeData *args, XMLElement *stanza, XMPPThread *thr)
MatrixSetReply(ev, reply_id); MatrixSetReply(ev, reply_id);
Free(reply_id); Free(reply_id);
} }
ShoveStanza(ev, stanza);
event_id = ASSend( event_id = ASSend(
args->config, mroom_id, encoded, args->config, mroom_id, encoded,
"m.room.message", ev "m.room.message", ev
@ -702,7 +725,8 @@ MessageStanza(ParseeData *args, XMLElement *stanza, XMPPThread *thr)
event_id = ParseeGetEventFromID(args, stanza, replaced); event_id = ParseeGetEventFromID(args, stanza, replaced);
Free(ASSend( Free(ASSend(
args->config, mroom_id, encoded, args->config, mroom_id, encoded,
"m.room.message", MatrixCreateReplace(event_id, data->data) "m.room.message",
ShoveStanza(MatrixCreateReplace(event_id, data->data), stanza)
)); ));
ParseePushAllStanza(args, stanza, event_id); ParseePushAllStanza(args, stanza, event_id);
pthread_mutex_unlock(&thr->info->chk_lock); pthread_mutex_unlock(&thr->info->chk_lock);
@ -993,7 +1017,15 @@ static void
IQGet(ParseeData *args, XMLElement *stanza) IQGet(ParseeData *args, XMLElement *stanza)
{ {
XMPPComponent *jabber = args->jabber; XMPPComponent *jabber = args->jabber;
if (XMLookForTKV(stanza, "query", "xmlns", DISCO)) char *from = HashMapGet(stanza->attrs, "from");
char *to = HashMapGet(stanza->attrs, "to");
char *id = HashMapGet(stanza->attrs, "id");
if (XMLookForTKV(stanza, "vCard", "xmlns", "vcard-temp"))
{
Log(LOG_INFO, "vCard information GET for %s", to);
}
else if (XMLookForTKV(stanza, "query", "xmlns", DISCO))
{ {
IQDiscoGet(args, jabber, stanza); IQDiscoGet(args, jabber, stanza);
} }
@ -1001,9 +1033,6 @@ IQGet(ParseeData *args, XMLElement *stanza)
{ {
XMLElement *iq_reply, *query; XMLElement *iq_reply, *query;
XMLElement *name, *version; XMLElement *name, *version;
char *from = HashMapGet(stanza->attrs, "from");
char *to = HashMapGet(stanza->attrs, "to");
char *id = HashMapGet(stanza->attrs, "id");
iq_reply = XMLCreateTag("iq"); iq_reply = XMLCreateTag("iq");
XMLAddAttr(iq_reply, "to", from); XMLAddAttr(iq_reply, "to", from);
@ -1085,6 +1114,34 @@ CreateVCardRequest(char *from, char *to)
return vcard_request; return vcard_request;
} }
static UserStatus
GuessStatus(XMLElement *stanza)
{
/* C.F RFC3921: XMPP IM */
XMLElement *show = XMLookForUnique(stanza, "show");
XMLElement *data = show ? ArrayGet(show->children, 0) : NULL;
if (!show || !data)
{
return USER_STATUS_ONLINE;
}
if (StrEquals(data->data, "away") ||
StrEquals(data->data, "xa"))
{
return USER_STATUS_OFFLINE;
}
if (StrEquals(data->data, "chat"))
{
return USER_STATUS_ONLINE;
}
if (StrEquals(data->data, "dnd"))
{
return USER_STATUS_UNAVAILABLE;
}
return USER_STATUS_ONLINE;
}
static void static void
PresenceStanza(ParseeData *args, XMLElement *stanza) PresenceStanza(ParseeData *args, XMLElement *stanza)
{ {
@ -1112,7 +1169,7 @@ PresenceStanza(ParseeData *args, XMLElement *stanza)
* my own instance (kappach.at) with presence enabled. */ * my own instance (kappach.at) with presence enabled. */
ASSetStatus( ASSetStatus(
args->config, from_matrix, args->config, from_matrix,
USER_STATUS_ONLINE, status_str GuessStatus(stanza), status_str
); );
} }
@ -1230,7 +1287,7 @@ PresenceStanza(ParseeData *args, XMLElement *stanza)
Free(parsee); Free(parsee);
Free(room); Free(room);
} }
else if (vc) if (vc)
{ {
XMLElement *photo = XMLookForUnique(vc, "photo"); XMLElement *photo = XMLookForUnique(vc, "photo");
XMLElement *p_dat = photo ? ArrayGet(photo->children, 0) : NULL; XMLElement *p_dat = photo ? ArrayGet(photo->children, 0) : NULL;
@ -1330,9 +1387,10 @@ XMPPDispatcher(void *argp)
{ {
XMLElement *stanza = RetrieveStanza(thread); XMLElement *stanza = RetrieveStanza(thread);
/* TODO: I've seen some spikes in some threads. */
if (!stanza) if (!stanza)
{ {
UtilSleepMillis(1); UtilSleepMillis(10);
continue; continue;
} }