From 55ac682d26d2787cd2b291c1ebfd7b1df9aea3dd Mon Sep 17 00:00:00 2001 From: LDA Date: Mon, 28 Oct 2024 13:26:25 +0100 Subject: [PATCH] [MOD] Timestamp Matrix messages --- etc/media/unknown.png | Bin 0 -> 542 bytes src/AS/Send.c | 30 +++++++++++++++++++++++++++--- src/MatrixEventHandler.c | 4 ++-- src/XMPPThread/Stanzas/IQ.c | 2 +- src/XMPPThread/Stanzas/Message.c | 19 ++++++++++--------- src/XMPPThread/Stanzas/Presence.c | 3 ++- src/include/AS.h | 3 ++- src/include/Bot.h | 4 ++-- src/include/Parsee.h | 2 ++ 9 files changed, 48 insertions(+), 19 deletions(-) create mode 100644 etc/media/unknown.png diff --git a/etc/media/unknown.png b/etc/media/unknown.png new file mode 100644 index 0000000000000000000000000000000000000000..00ccf916cadd9c3b5995a00dade4d053fc87089f GIT binary patch literal 542 zcmeAS@N?(olHy`uVBq!ia0vp^4j|0I3?%1nZ+ru!7>k44ofy`glX(f`Bn0?`xB_V) zaMiF$vo!1vv%X)bc5^}6yTh}7e=?i-G$3oAf%%j??@aw5Zxx16M+O%YgM_B&yobrN z|Hq$wa{Bh4|NsBbUc0@eu01m=D>5vo%&BXQkrB{91tp&T>0Vv=2FIrb90l=9g8YIR zfU5CEaj?(fanP@1_<79+n4p8n{<{w|_QesoIjy%qeMx+?A_0zWBy` z)cyGM=6g-sAJ}u<68x8I^eA%SgPynNE$Y{B{xT`o;qdvhYK9oYzU&#o1ukZWW)@5{ z_gV5LAGED(7L@E!IML1~((>~}LzBgM-jxq#J$)`Yozr0b{NsPQS;|-irfc7m+TyS_ z;kZq8lB55<#Y}tI1UwEMd+Ni(n)RU1kMq)>SqDz8=bTgHyI`H@H;4GBIl z-_)wIH?S6nU74Z%f}QIaZ?x71md))_4NRZyPwr#+5bEl9_~o<97m64q-Yn;QF5PJM z!137cYNp(VJ#*i3NY(XE`fbfrT$13<)}(S)wlhIuzqZoBE0IU`F8KGaxbm+_#?OBV z3%vH_F9_O~{h(>PwA55zFA|>gTe~ HDWM4f=^pg& literal 0 HcmV?d00001 diff --git a/src/AS/Send.c b/src/AS/Send.c index 4032423..766f393 100644 --- a/src/AS/Send.c +++ b/src/AS/Send.c @@ -1,21 +1,38 @@ #include #include +#include #include #include #include +#include #include #include +#include #include +static char * +TSToStr(uint64_t ts) +{ + size_t len; + char *str; + + len = snprintf(NULL, 0, "%"PRIu64, ts); + str = Malloc(len+1); + snprintf(str, len+1, "%"PRIu64, ts); + + return str; +} + char * -ASSend(const ParseeConfig *conf, char *id, char *user, char *type, HashMap *c) +ASSend(const ParseeConfig *conf, char *id, char *user, char *type, HashMap *c, uint64_t ts) { HttpClientContext *ctx = NULL; char *path; char *txn, *ret; + char *ts_str; HashMap *reply; if (!conf || !id || !type || !user || !c) { @@ -23,13 +40,20 @@ ASSend(const ParseeConfig *conf, char *id, char *user, char *type, HashMap *c) return NULL; } + if (!ts) + { + ts = UtilTsMillis(); + } + ts_str = TSToStr(ts); + txn = StrRandom(16); - path = StrConcat(9, + path = StrConcat(11, "/_matrix/client/v3/rooms/", id, "/send/", type, "/", txn, "?", - "user_id=", user + "user_id=", user, "&ts=", ts_str ); Free(txn); + Free(ts_str); ctx = ParseeCreateRequest(conf, HTTP_PUT, path); Free(path); diff --git a/src/MatrixEventHandler.c b/src/MatrixEventHandler.c index 234001b..3d6a69e 100644 --- a/src/MatrixEventHandler.c +++ b/src/MatrixEventHandler.c @@ -263,7 +263,7 @@ ParseeBotHandler(ParseeData *data, HashMap *event) Free(ASSend( data->config, id, profile, "m.room.message", - MatrixCreateNotice("Please enter a valid command") + MatrixCreateNotice("Please enter a valid command"), 0 )); Free(profile); return; @@ -274,7 +274,7 @@ ParseeBotHandler(ParseeData *data, HashMap *event) Free(ASSend( data->config, id, profile, "m.room.message", - MatrixCreateNotice("You are not authorised to do this.") + MatrixCreateNotice("You are not authorised to do this."), 0 )); Free(profile); return; diff --git a/src/XMPPThread/Stanzas/IQ.c b/src/XMPPThread/Stanzas/IQ.c index 562fc22..32aa0c9 100644 --- a/src/XMPPThread/Stanzas/IQ.c +++ b/src/XMPPThread/Stanzas/IQ.c @@ -69,7 +69,7 @@ AvatarGrab(ParseeData *data, char *mxc, char **mime, char **b64, size_t *len) { Free(b64i); Free(mimei); - b64i = StrDuplicate(media_parsee_logo); /* TODO: Different assets! */ + b64i = StrDuplicate(media_unknown); /* TODO: Different assets! */ mimei = StrDuplicate("image/png"); } diff --git a/src/XMPPThread/Stanzas/Message.c b/src/XMPPThread/Stanzas/Message.c index d72d4d9..0962fc0 100644 --- a/src/XMPPThread/Stanzas/Message.c +++ b/src/XMPPThread/Stanzas/Message.c @@ -43,7 +43,7 @@ LazyRegister(ParseeData *data, char *mxid, char *name) Free(hash); } static char * -LazySend(ParseeData *args, char *mxid, char *mroom_id, char *type, HashMap *ev) +LazySend(ParseeData *args, char *mxid, char *mroom_id, char *type, HashMap *ev, uint64_t ts) { HashMap *duplicate; char *event; @@ -59,8 +59,7 @@ LazySend(ParseeData *args, char *mxid, char *mroom_id, char *type, HashMap *ev) duplicate = JsonDuplicate(ev); event = ASSend( args->config, mroom_id, mxid, - type, - ev + type, ev, ts ); if (event) { @@ -73,7 +72,7 @@ LazySend(ParseeData *args, char *mxid, char *mroom_id, char *type, HashMap *ev) return ASSend( args->config, mroom_id, mxid, - type, duplicate + type, duplicate, ts ); } @@ -254,7 +253,8 @@ MessageStanza(ParseeData *args, XMLElement *stanza, XMPPThread *thr) room = ParseeGetBridgedRoom(args, stanza); Free(LazySend( args, parsee, room, NULL, - MatrixCreateNotice(message) + MatrixCreateNotice(message), + time )); end_error: @@ -431,7 +431,7 @@ end_error: event_id = LazySend( args, encoded, mroom_id, NULL, - content + content, time ); Free(mxc); } @@ -477,7 +477,8 @@ end_error: ShoveStanza( MatrixCreateReact(event_id, react_data->data), stanza - ) + ), + time )); } Free(event_id); @@ -510,7 +511,7 @@ end_error: ShoveStanza(ev, stanza); event_id = LazySend( args, encoded, mroom_id, NULL, - ev + ev, time ); } pthread_mutex_lock(&thr->info->chk_lock); @@ -533,7 +534,7 @@ end_error: Log(LOG_DEBUG, "Replacing events in %s(%s)", mroom_id, event_id); Free(LazySend( args, encoded, mroom_id, NULL, - ev + ev, time )); ParseePushAllStanza(args, stanza, event_id); pthread_mutex_unlock(&thr->info->chk_lock); diff --git a/src/XMPPThread/Stanzas/Presence.c b/src/XMPPThread/Stanzas/Presence.c index 4a66f94..5c7c21d 100644 --- a/src/XMPPThread/Stanzas/Presence.c +++ b/src/XMPPThread/Stanzas/Presence.c @@ -278,7 +278,8 @@ PresenceStanza(ParseeData *args, XMLElement *stanza, XMPPThread *thr) Free(ASSend( args->config, room, parsee, "m.room.message", - MatrixCreateNotice("This room has been unlinked.") + MatrixCreateNotice("This room has been unlinked."), + 0 )); ASLeave(args->config, room, parsee); Free(chat_id); diff --git a/src/include/AS.h b/src/include/AS.h index ad91e60..8afef6f 100644 --- a/src/include/AS.h +++ b/src/include/AS.h @@ -67,7 +67,8 @@ extern HashMap * ASFind(const ParseeConfig *, char *, char *); /* Sends a message event with a specific type and body. * Said body is freed during the function's execution. */ -extern char * ASSend(const ParseeConfig *, char *, char *, char *, HashMap *); +extern char * +ASSend(const ParseeConfig *, char *, char *, char *, HashMap *, uint64_t ts); extern void ASType(const ParseeConfig *, char *, char *, bool); extern void ASPresence(const ParseeConfig *, char *, char *, char *); diff --git a/src/include/Bot.h b/src/include/Bot.h index e2c0340..3154a3a 100644 --- a/src/include/Bot.h +++ b/src/include/Bot.h @@ -26,7 +26,7 @@ Free(ASSend( \ data->config, id, profile, \ "m.room.message", \ - MatrixCreateNotice(rep) \ + MatrixCreateNotice(rep), 0 \ )); \ } \ while(0) @@ -45,7 +45,7 @@ Free(ASSend( \ data->config, id, profile, \ "m.room.message", \ - MatrixCreateNotice(formatted) \ + MatrixCreateNotice(formatted), 0 \ )); \ Free(formatted); \ } \ diff --git a/src/include/Parsee.h b/src/include/Parsee.h index 1cc93bf..6b2d9cc 100644 --- a/src/include/Parsee.h +++ b/src/include/Parsee.h @@ -107,6 +107,8 @@ typedef struct Argument { /* A base64-encoded Parsee logo */ extern const char media_parsee_logo[]; +/* "Unknown avatar" */ +extern const char media_unknown[]; /* An ASCII-art rendition of "小橋". * I'm sorry for its quality. If anyone wants to redraw it, feel free. */