about summary refs log tree commit diff stats
path: root/src/xmpp
diff options
context:
space:
mode:
authorMichael Vetter <jubalh@iodoru.org>2020-04-11 17:11:53 +0200
committerMichael Vetter <jubalh@iodoru.org>2020-04-11 17:11:53 +0200
commit180ec2b474b39de0b22964adbfc5e13bc3d2c791 (patch)
treee85e0c219b81189a9fbeb39b2c58881e0939d3d9 /src/xmpp
parente23bc3808350a4fbf6086bde71108c56e2b1c69f (diff)
downloadprofani-tty-180ec2b474b39de0b22964adbfc5e13bc3d2c791.tar.gz
Add to_jid field to ProfMessage struct
Is usefult in many cases if we want cleaner code.
Hope this edit didn't break anything though ;-)
Diffstat (limited to 'src/xmpp')
-rw-r--r--src/xmpp/message.c56
-rw-r--r--src/xmpp/xmpp.h3
2 files changed, 34 insertions, 25 deletions
diff --git a/src/xmpp/message.c b/src/xmpp/message.c
index 73ae8549..59970012 100644
--- a/src/xmpp/message.c
+++ b/src/xmpp/message.c
@@ -200,7 +200,8 @@ message_init(void)
 {
     ProfMessage *message = malloc(sizeof(ProfMessage));
 
-    message->jid = NULL;
+    message->from_jid = NULL;
+    message->to_jid = NULL;
     message->id = NULL;
     message->originid = NULL;
     message->replace_id = NULL;
@@ -219,8 +220,12 @@ void
 message_free(ProfMessage *message)
 {
     xmpp_ctx_t *ctx = connection_get_ctx();
-    if (message->jid) {
-        jid_destroy(message->jid);
+    if (message->from_jid) {
+        jid_destroy(message->from_jid);
+    }
+
+    if (message->to_jid) {
+        jid_destroy(message->to_jid);
     }
 
     if (message->id) {
@@ -801,51 +806,51 @@ _handle_groupchat(xmpp_stanza_t *const stanza)
     }
 
     const char *room_jid = xmpp_stanza_get_from(stanza);
-    Jid *jid = jid_create(room_jid);
+    Jid *from_jid = jid_create(room_jid);
 
     // handle room subject
     xmpp_stanza_t *subject = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_SUBJECT);
     if (subject) {
         char *subject_text;
         subject_text = xmpp_stanza_get_text(subject);
-        sv_ev_room_subject(jid->barejid, jid->resourcepart, subject_text);
+        sv_ev_room_subject(from_jid->barejid, from_jid->resourcepart, subject_text);
         xmpp_free(ctx, subject_text);
 
-        jid_destroy(jid);
+        jid_destroy(from_jid);
         return;
     }
 
     // handle room broadcasts
-    if (!jid->resourcepart) {
+    if (!from_jid->resourcepart) {
         char *broadcast;
         broadcast = xmpp_message_get_body(stanza);
         if (!broadcast) {
-            jid_destroy(jid);
+            jid_destroy(from_jid);
             return;
         }
 
         sv_ev_room_broadcast(room_jid, broadcast);
         xmpp_free(ctx, broadcast);
 
-        jid_destroy(jid);
+        jid_destroy(from_jid);
         return;
     }
 
-    if (!jid_is_valid_room_form(jid)) {
-        log_error("Invalid room JID: %s", jid->str);
-        jid_destroy(jid);
+    if (!jid_is_valid_room_form(from_jid)) {
+        log_error("Invalid room JID: %s", from_jid->str);
+        jid_destroy(from_jid);
         return;
     }
 
     // room not active in profanity
-    if (!muc_active(jid->barejid)) {
-        log_error("Message received for inactive chat room: %s", jid->str);
-        jid_destroy(jid);
+    if (!muc_active(from_jid->barejid)) {
+        log_error("Message received for inactive chat room: %s", from_jid->str);
+        jid_destroy(from_jid);
         return;
     }
 
     ProfMessage *message = message_init();
-    message->jid = jid;
+    message->from_jid = from_jid;
     message->type = PROF_MSG_TYPE_MUC;
 
     if (id) {
@@ -875,17 +880,17 @@ _handle_groupchat(xmpp_stanza_t *const stanza)
 #endif
 
     if (!message->plain && !message->body) {
-        log_error("Message received without body for room: %s", jid->str);
+        log_error("Message received without body for room: %s", from_jid->str);
         goto out;
     } else if (!message->plain) {
         message->plain = strdup(message->body);
     }
 
     // determine if the notifications happened whilst offline (MUC history)
-    message->timestamp = stanza_get_delay_from(stanza, jid->barejid);
+    message->timestamp = stanza_get_delay_from(stanza, from_jid->barejid);
     if (message->timestamp == NULL) {
         // checking the domainpart is a workaround for some prosody versions (gh#1190)
-        message->timestamp = stanza_get_delay_from(stanza, jid->domainpart);
+        message->timestamp = stanza_get_delay_from(stanza, from_jid->domainpart);
     }
 
     bool is_muc_history = FALSE;
@@ -996,7 +1001,7 @@ _handle_muc_private_message(xmpp_stanza_t *const stanza)
     message->type = PROF_MSG_TYPE_MUCPM;
 
     const gchar *from = xmpp_stanza_get_from(stanza);
-    message->jid = jid_create(from);
+    message->from_jid = jid_create(from);
 
     // message stanza id
     const char *id = xmpp_stanza_get_id(stanza);
@@ -1016,7 +1021,7 @@ _handle_muc_private_message(xmpp_stanza_t *const stanza)
     message->body = xmpp_message_get_body(stanza);
 
     if (!message->plain && !message->body) {
-        log_error("Message received without body from: %s", message->jid->str);
+        log_error("Message received without body from: %s", message->from_jid->str);
         goto out;
     } else if (!message->plain) {
         message->plain = strdup(message->body);
@@ -1133,17 +1138,18 @@ _handle_carbons(xmpp_stanza_t *const stanza)
         message->encrypted = xmpp_stanza_get_text(x);
     }
 
+    //TODO: now that profmessage has from_jid AND to_jid we should save both. and maybe also add is_carbon so we can decide later on.
     if (message->plain || message->encrypted || message->body) {
         // if we are the recipient, treat as standard incoming message
         if (g_strcmp0(my_jid->barejid, jid_to->barejid) == 0) {
             jid_destroy(jid_to);
-            message->jid = jid_from;
+            message->from_jid = jid_from;
             sv_ev_incoming_carbon(message);
 
         // else treat as a sent message
         } else {
             jid_destroy(jid_from);
-            message->jid = jid_to;
+            message->from_jid = jid_to;
             sv_ev_outgoing_carbon(message);
         }
     }
@@ -1200,7 +1206,9 @@ _handle_chat(xmpp_stanza_t *const stanza)
 
     // standard chat message, use jid without resource
     ProfMessage *message = message_init();
-    message->jid = jid;
+    // TODO: safe to_jid too. also need to have the same check like in carbons. in case this is MAM.
+    // or have a is_mam and handle later. 
+    message->from_jid = jid;
     message->type = PROF_MSG_TYPE_CHAT;
 
     // message stanza id
diff --git a/src/xmpp/xmpp.h b/src/xmpp/xmpp.h
index 7544189b..3f384fb7 100644
--- a/src/xmpp/xmpp.h
+++ b/src/xmpp/xmpp.h
@@ -137,7 +137,8 @@ typedef enum {
 } prof_msg_type_t;
 
 typedef struct prof_message_t {
-   Jid *jid;
+   Jid *from_jid;
+   Jid *to_jid;
    /* regular <message id=""> */
    char *id;
    /* </origin-id> XEP-0359 */