diff --git a/CHANGELOG.md b/CHANGELOG.md index 3076eeb..0e038b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,8 @@ system of Parsee. - Allows MbedTLS through a specific Cytoplasm patch. - "Lone" XMPP messages no longer render weirdly on Element Android's weird rendering. +- Start fixing bug where Parsee takes several seconds to send a +message coming from XMPP #### Deprecated features - The old `build.c` and `Makefile`s used for building are removed, and replaced by the `configure.c` C file(/script via TCC). diff --git a/src/Parsee/Data.c b/src/Parsee/Data.c index d4bf6cb..de53627 100644 --- a/src/Parsee/Data.c +++ b/src/Parsee/Data.c @@ -181,6 +181,8 @@ ParseeCleanup(void *datp) CleanupField(stanza, 30 MINUTES, 500); CleanupField(event, 30 MINUTES, 500); CleanupField(id, 30 MINUTES, 500); + + /* TODO: Also cleanup user cache information */ #undef CleanupField } DbListFree(chats); diff --git a/src/XMPPThread/Bridged.c b/src/XMPPThread/Bridged.c index 58fb571..7e047a4 100644 --- a/src/XMPPThread/Bridged.c +++ b/src/XMPPThread/Bridged.c @@ -226,7 +226,6 @@ ParseeGetBridgedUserI(ParseeData *data, XMLElement *stanza, char *force) { ParseePushOIDTable(xmpp_from, occ_id); } - Log(LOG_DEBUG, "Trying Occ ID for %s{%s}", xmpp_from, occ_id); } if (!occ_id) diff --git a/src/XMPPThread/Stanzas/Message.c b/src/XMPPThread/Stanzas/Message.c index 1b79b77..32f9fa1 100644 --- a/src/XMPPThread/Stanzas/Message.c +++ b/src/XMPPThread/Stanzas/Message.c @@ -1,6 +1,7 @@ #include "XMPPThread/internal.h" #include +#include #include #include @@ -12,15 +13,58 @@ static void LazyRegister(ParseeData *data, char *mxid, char *name) { - if (!DbExists(data->db, 2, "users", mxid)) + DbRef *ref; + char *hash = ParseeHMACS(data->id, mxid); + char *dbname; + if (!(ref = DbLock(data->db, 2, "users", hash))) { ASRegisterUser(data->config, mxid); - DbUnlock(data->db, DbCreate(data->db, 2, "users", mxid)); + ref = DbCreate(data->db, 2, "users", hash); + HashMapSet(DbJson(ref), "mxid", JsonValueString(mxid)); + HashMapSet(DbJson(ref), "ts", JsonValueInteger(UtilTsMillis())); } - if (name) + dbname = GrabString(DbJson(ref), 1, "name"); + if (name && !StrEquals(dbname, name)) { ASSetName(data->config, mxid, name); + HashMapSet(DbJson(ref), "name", JsonValueString(name)); } + DbUnlock(data->db, ref); + Free(hash); +} +static char * +LazySend(ParseeData *args, char *mxid, char *mroom_id, char *type, HashMap *ev) +{ + HashMap *duplicate; + char *event; + if (!args || !mxid || !mroom_id || !ev) + { + return NULL; + } + if (!mroom_id) + { + mroom_id = "m.room.message"; + } + + duplicate = JsonDuplicate(ev); + event = ASSend( + args->config, mroom_id, mxid, + "m.room.message", + ev + ); + if (event) + { + JsonFree(duplicate); + return event; + } + + ASInvite(args->config, mroom_id, mxid); + Free(ASJoin(args->config, mroom_id, mxid)); + + return ASSend( + args->config, mroom_id, mxid, + "m.room.message", duplicate + ); } static void @@ -79,6 +123,15 @@ ProcessChatstates(ParseeData *args, XMLElement *stanza) } #undef CHAT_STATES } +static float +TimeElapsed(uint64_t *rectime, uint64_t v) +{ + uint64_t time = UtilTsMillis(); + float t = ((time-v)/1000.f); + *rectime = time; + + return t; +} bool MessageStanza(ParseeData *args, XMLElement *stanza, XMPPThread *thr) { @@ -97,30 +150,30 @@ MessageStanza(ParseeData *args, XMLElement *stanza, XMPPThread *thr) char *type = HashMapGet(stanza->attrs, "type"); bool chat = StrEquals(type, "chat"); size_t i; + uint64_t time, rectime; +#define Elapsed(v) (TimeElapsed(&rectime, v)) + to = NULL; from = NULL; decode_from = NULL; from_matrix = NULL; Log(LOG_DEBUG, " usage=%d", MemoryAllocated()); + time = UtilTsMillis(); + rectime = time; from = HashMapGet(stanza->attrs, "from"); if (ParseeManageBan(args, from, NULL)) { XMLFreeElement(stanza); - Log(LOG_DEBUG, " usage=%d (%s:%d)", MemoryAllocated(), __FILE__, __LINE__); + Log(LOG_DEBUG, + " time=%f " + "usage=%d (%s:%d)", + Elapsed(time), + MemoryAllocated(), __FILE__, __LINE__ + ); return false; } - /*{ - XMLElement *foo = XMLCreateTag("message"); - XMLElement *body = XMLCreateTag("body"); - XMLAddAttr(foo, "type", "chat"); - XMLAddChild(foo, body); - XMLAddChild(body, XMLCreateText("Storm on Mt. Ooe (sorry if you see this)")); - - BroadcastStanza(args, HashMapGet(stanza->attrs, "to"), foo); - XMLFreeElement(foo); - }*/ if (ServerHasXEP421(args, from)) @@ -132,13 +185,17 @@ MessageStanza(ParseeData *args, XMLElement *stanza, XMPPThread *thr) char *occ_id = occupant ? HashMapGet(occupant->attrs, "id") : NULL; if (occ_id) { - Log(LOG_DEBUG, - "'%s' has support for XEP-421, fetching OID=%s", - from, occ_id - ); + if (args->verbosity >= PARSEE_VERBOSE_COMICAL) + { + Log(LOG_DEBUG, + "'%s' has support for XEP-421, fetching OID=%s", + from, occ_id + ); + } ParseePushOIDTable(from, occ_id); } } + Log(LOG_DEBUG, "XEP-421: %fs", Elapsed(rectime)); if (StrEquals(type, "error")) { @@ -164,9 +221,8 @@ MessageStanza(ParseeData *args, XMLElement *stanza, XMPPThread *thr) message = StrConcat(3, type, ": ", text); room = ParseeGetBridgedRoom(args, stanza); - Free(ASSend( - args->config, room, parsee, - "m.room.message", + Free(LazySend( + args, parsee, room, NULL, MatrixCreateNotice(message) )); @@ -176,9 +232,15 @@ end_error: Free(parsee); Free(room); Free(user); - Log(LOG_DEBUG, " usage=%d (%s:%d)", MemoryAllocated(), __FILE__, __LINE__); + Log(LOG_DEBUG, + " time=%f " + "usage=%d (%s:%d)", + Elapsed(time), + MemoryAllocated(), __FILE__, __LINE__ + ); return false; } + Log(LOG_DEBUG, "Error management: %fs", Elapsed(rectime)); if (moderated) { @@ -206,18 +268,22 @@ end_error: body = XMLookForUnique(stanza, "body"); PEPManagerHandle(thr->info->pep_manager, stanza); + Log(LOG_DEBUG, "PEP management: %fs", Elapsed(rectime)); ProcessChatstates(args, stanza); + Log(LOG_DEBUG, "Chatstate management: %fs", Elapsed(rectime)); to = ParseeDecodeMXID(HashMapGet(stanza->attrs, "to")); decode_from = ParseeLookupJID(from); from_matrix = ParseeEncodeJID(args->config, decode_from, true); room = ParseeFindDMRoom(args, to, from); data = body ? ArrayGet(body->children, 0) : NULL; + Log(LOG_DEBUG, "Fetching user info: %fs", Elapsed(rectime)); /* TODO: CLEAN THAT UP INTO A CREATEDM FUNCTION */ mroom_id = ParseeGetBridgedRoom(args, stanza); Log(LOG_DEBUG, "Bridging event to '%s'...", mroom_id); + Log(LOG_DEBUG, "Fetching bridge: %fs", Elapsed(rectime)); if (!mroom_id && !room && !XMPPIsParseeStanza(stanza) && to && *to == '@') { @@ -228,7 +294,7 @@ end_error: LazyRegister(args, from_matrix, NULL); room = ASCreateDM(args->config, from_matrix, to); mroom_id = StrDuplicate(room); - Log(LOG_INFO, "Creating a DM to '%s'(%s)...", to, mroom_id); + Log(LOG_DEBUG, "Creating a DM to '%s'(%s)...", to, mroom_id); if (room) { room_ref = DbCreate(args->db, 3, "rooms", room, "data"); @@ -277,6 +343,7 @@ end_error: StreamFlush(jabber->stream); pthread_mutex_unlock(&jabber->write_lock); XMLFreeElement(ps); + Log(LOG_DEBUG, "Subscription to XEP-0084: %fs", Elapsed(rectime)); Free(parsee); Free(trim); @@ -289,10 +356,7 @@ end_error: pthread_mutex_unlock(&thr->info->chk_lock); LazyRegister(args, encoded, !chat ? res : NULL); - /* TODO: I don't think we can quite remove that. Maybe - * the user was kicked and the bridge is unaware? */ - ASInvite(args->config, mroom_id, encoded); - Free(ASJoin(args->config, mroom_id, encoded)); + Log(LOG_DEBUG, "Matrix registration: %fs", Elapsed(rectime)); reactions = XMLookForTKV(stanza, "reactions", "xmlns", "urn:xmpp:reactions:0" @@ -322,9 +386,9 @@ end_error: ); ShoveStanza(content, stanza); - event_id = ASSend( - args->config, mroom_id, encoded, - "m.room.message", content + event_id = LazySend( + args, encoded, mroom_id, NULL, + content ); Free(mxc); } @@ -364,14 +428,13 @@ end_error: reaction = ArrayGet(react_child, i); react_data = ArrayGet(reaction->children, 0); - Free(ASSend( - args->config, mroom_id, encoded, - "m.reaction", + event_id = LazySend( + args, encoded, mroom_id, "m.reaction", ShoveStanza( MatrixCreateReact(event_id, react_data->data), stanza ) - )); + ); } Free(event_id); event_id = NULL; @@ -401,9 +464,9 @@ end_error: Free(reply_id); } ShoveStanza(ev, stanza); - event_id = ASSend( - args->config, mroom_id, encoded, - "m.room.message", ev + event_id = LazySend( + args, encoded, mroom_id, NULL, + ev ); } pthread_mutex_lock(&thr->info->chk_lock); @@ -424,9 +487,9 @@ end_error: ); Log(LOG_DEBUG, "Replacing events in %s(%s)", mroom_id, event_id); - Free(ASSend( - args->config, mroom_id, encoded, - "m.room.message", ev + Free(LazySend( + args, encoded, mroom_id, NULL, + ev )); ParseePushAllStanza(args, stanza, event_id); pthread_mutex_unlock(&thr->info->chk_lock); @@ -459,7 +522,12 @@ end: Free(decode_from); Free(room); Free(to); - Log(LOG_DEBUG, " usage=%d (%s:%d)", MemoryAllocated(), __FILE__, __LINE__); + Log(LOG_DEBUG, + " time=%f " + "usage=%d (%s:%d)", + (UtilTsMillis()-time)/1000.f, + MemoryAllocated(), __FILE__, __LINE__ + ); return true; } diff --git a/src/XMPPThread/Stanzas/Presence.c b/src/XMPPThread/Stanzas/Presence.c index 8323f6a..174a916 100644 --- a/src/XMPPThread/Stanzas/Presence.c +++ b/src/XMPPThread/Stanzas/Presence.c @@ -89,10 +89,6 @@ PresenceStanza(ParseeData *args, XMLElement *stanza, XMPPThread *thr) char *occ_id = occupant ? HashMapGet(occupant->attrs, "id") : NULL; if (occ_id) { - Log(LOG_DEBUG, - "'%s' has support for XEP-421, fetching OID=%s", - oid, occ_id - ); ParseePushOIDTable(oid, occ_id); } }