about summary refs log tree commit diff stats
path: root/src/xmpp/message.c
diff options
context:
space:
mode:
authorMichael Vetter <jubalh@iodoru.org>2019-10-31 13:45:44 +0100
committerMichael Vetter <jubalh@iodoru.org>2019-10-31 13:45:44 +0100
commitf71de61b9dc297d111a4ab2ccc77238251659574 (patch)
tree10303d1cb8565fcf05ebf5daaab7dca2c0748c30 /src/xmpp/message.c
parent4ecd4dea6a0a4c216a57d039dc7dc01d5e41d68a (diff)
downloadprofani-tty-f71de61b9dc297d111a4ab2ccc77238251659574.tar.gz
Don't override ProfMessage Id with origin-id
Profanity sends the same value for both. Other clients might not.
Safe both since we could need them later.

Once we implement Last Message Correction we will need the regular id.
If we override it with origin-id and another client chooses to not use
the same value for id and origin-id then we can't interpret the id sent
with the LMC request correctly.
Diffstat (limited to 'src/xmpp/message.c')
-rw-r--r--src/xmpp/message.c58
1 files changed, 40 insertions, 18 deletions
diff --git a/src/xmpp/message.c b/src/xmpp/message.c
index b8a6d3a8..76124ee7 100644
--- a/src/xmpp/message.c
+++ b/src/xmpp/message.c
@@ -186,6 +186,7 @@ message_init(void)
 
     message->jid = NULL;
     message->id = NULL;
+    message->originid = NULL;
     message->body = NULL;
     message->encrypted = NULL;
     message->plain = NULL;
@@ -209,6 +210,10 @@ message_free(ProfMessage *message)
         xmpp_free(ctx, message->id);
     }
 
+    if (message->originid) {
+        xmpp_free(ctx, message->originid);
+    }
+
     if (message->body) {
         xmpp_free(ctx, message->body);
     }
@@ -748,10 +753,11 @@ _handle_groupchat(xmpp_stanza_t *const stanza)
     xmpp_ctx_t *ctx = connection_get_ctx();
 
     const char *id = xmpp_stanza_get_id(stanza);
+    char *originid = NULL;
 
     xmpp_stanza_t *origin = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_STABLE_ID);
     if (origin && g_strcmp0(xmpp_stanza_get_name(origin), STANZA_NAME_ORIGIN_ID) == 0) {
-        id = xmpp_stanza_get_attribute(origin, STANZA_ATTR_ID);
+        originid = (char*)xmpp_stanza_get_attribute(origin, STANZA_ATTR_ID);
     }
 
     const char *room_jid = xmpp_stanza_get_from(stanza);
@@ -803,6 +809,9 @@ _handle_groupchat(xmpp_stanza_t *const stanza)
     if (id) {
         message->id = strdup(id);
     }
+    if (originid) {
+        message->originid = strdup(originid);
+    }
 
     message->body = xmpp_message_get_body(stanza);
 
@@ -1167,33 +1176,46 @@ _send_message_stanza(xmpp_stanza_t *const stanza)
     xmpp_free(connection_get_ctx(), text);
 }
 
+/* ckeckOID = true: check origin-id
+ * checkOID = false: check regular id
+ */
 bool
-message_is_sent_by_us(ProfMessage *message) {
+message_is_sent_by_us(ProfMessage *message, bool checkOID) {
     bool ret = FALSE;
 
     // we check the </origin-id> for this we calculate a hash into it so we can detect
     // whether this client sent it. See connection_create_stanza_id()
-    if (message && message->id != NULL) {
-        gsize tmp_len;
-        char *tmp = (char*)g_base64_decode(message->id, &tmp_len);
+    if (message) {
+        char *tmp_id = NULL;
+
+        if (checkOID && message->originid != NULL) {
+            tmp_id = message->originid;
+        } else if (!checkOID && message->id != NULL) {
+            tmp_id = message->id;
+        }
 
-        // our client sents at least 36 (uuid) + identifier
-        if (tmp_len > 36) {
-            char *uuid = g_strndup(tmp, 36);
-            const char *prof_identifier = connection_get_profanity_identifier();
+        if (tmp_id != NULL) {
+            gsize tmp_len;
+            char *tmp = (char*)g_base64_decode(tmp_id, &tmp_len);
 
-            gchar *hmac = g_compute_hmac_for_string(G_CHECKSUM_SHA256,
-                    (guchar*)prof_identifier, strlen(prof_identifier),
-                    uuid, strlen(uuid));
+            // our client sents at least 36 (uuid) + identifier
+            if (tmp_len > 36) {
+                char *uuid = g_strndup(tmp, 36);
+                const char *prof_identifier = connection_get_profanity_identifier();
 
-            if (g_strcmp0(&tmp[36], hmac) == 0) {
-                ret = TRUE;
-            }
+                gchar *hmac = g_compute_hmac_for_string(G_CHECKSUM_SHA256,
+                        (guchar*)prof_identifier, strlen(prof_identifier),
+                        uuid, strlen(uuid));
 
-            g_free(uuid);
-            g_free(hmac);
+                if (g_strcmp0(&tmp[36], hmac) == 0) {
+                    ret = TRUE;
+                }
+
+                g_free(uuid);
+                g_free(hmac);
+            }
+            free(tmp);
         }
-        free(tmp);
     }
 
     return  ret;