about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2015-03-10 21:16:34 +0000
committerJames Booth <boothj5@gmail.com>2015-03-10 21:16:34 +0000
commit2ff6873cf1ad4ca3c78947f5e3372d1da5a0b7d3 (patch)
tree3689896b22e111b8ed21b489cc219d7b0f984f19
parent3c1e8c4e154600b8b7aeb60bbf316e39a97ece27 (diff)
downloadprofani-tty-2ff6873cf1ad4ca3c78947f5e3372d1da5a0b7d3.tar.gz
Send private carbons element with message when encrypted
-rw-r--r--src/command/command.c2
-rw-r--r--src/command/commands.c10
-rw-r--r--src/otr/otr.c2
-rw-r--r--src/server_events.c4
-rw-r--r--src/xmpp/message.c40
-rw-r--r--src/xmpp/stanza.c10
-rw-r--r--src/xmpp/stanza.h2
-rw-r--r--src/xmpp/xmpp.h1
-rw-r--r--tests/test_cmd_otr.c4
-rw-r--r--tests/xmpp/stub_xmpp.c6
10 files changed, 62 insertions, 19 deletions
diff --git a/src/command/command.c b/src/command/command.c
index 6502af75..6a5603ee 100644
--- a/src/command/command.c
+++ b/src/command/command.c
@@ -1928,7 +1928,7 @@ _cmd_execute_default(const char * inp)
                 if (otr_is_secure(chatwin->barejid)) {
                     char *encrypted = otr_encrypt_message(chatwin->barejid, inp);
                     if (encrypted != NULL) {
-                        message_send_chat(chatwin->barejid, encrypted);
+                        message_send_chat_encrypted(chatwin->barejid, encrypted);
                         otr_free_message(encrypted);
                         if (prefs_get_boolean(PREF_CHLOG)) {
                             const char *jid = jabber_get_fulljid();
diff --git a/src/command/commands.c b/src/command/commands.c
index 8c93b065..2ca749fb 100644
--- a/src/command/commands.c
+++ b/src/command/commands.c
@@ -1265,7 +1265,7 @@ cmd_msg(gchar **args, struct cmd_help_t help)
             if (otr_is_secure(barejid)) {
                 char *encrypted = otr_encrypt_message(barejid, msg);
                 if (encrypted != NULL) {
-                    message_send_chat(barejid, encrypted);
+                    message_send_chat_encrypted(barejid, encrypted);
                     otr_free_message(encrypted);
                     ui_outgoing_chat_msg("me", barejid, msg);
 
@@ -1294,7 +1294,7 @@ cmd_msg(gchar **args, struct cmd_help_t help)
                     GString *otr_message = g_string_new(msg);
                     g_string_append(otr_message, OTRL_MESSAGE_TAG_BASE);
                     g_string_append(otr_message, OTRL_MESSAGE_TAG_V2);
-                    message_send_chat(barejid, otr_message->str);
+                    message_send_chat_encrypted(barejid, otr_message->str);
 
                     g_string_free(otr_message, TRUE);
                 } else {
@@ -3073,7 +3073,7 @@ cmd_tiny(gchar **args, struct cmd_help_t help)
                 if (otr_is_secure(chatwin->barejid)) {
                     char *encrypted = otr_encrypt_message(chatwin->barejid, tiny);
                     if (encrypted != NULL) {
-                        message_send_chat(chatwin->barejid, encrypted);
+                        message_send_chat_encrypted(chatwin->barejid, encrypted);
                         otr_free_message(encrypted);
                         if (prefs_get_boolean(PREF_CHLOG)) {
                             const char *jid = jabber_get_fulljid();
@@ -4080,7 +4080,7 @@ cmd_otr(gchar **args, struct cmd_help_t help)
                     ui_current_print_formatted_line('!', 0, "You have not generated or loaded a private key, use '/otr gen'");
                 } else if (!otr_is_secure(barejid)) {
                     char *otr_query_message = otr_start_query();
-                    message_send_chat(barejid, otr_query_message);
+                    message_send_chat_encrypted(barejid, otr_query_message);
                 } else {
                     ui_gone_secure(barejid, otr_is_trusted(barejid));
                 }
@@ -4098,7 +4098,7 @@ cmd_otr(gchar **args, struct cmd_help_t help)
                 } else {
                     ProfChatWin *chatwin = ui_get_current_chat();
                     char *otr_query_message = otr_start_query();
-                    message_send_chat(chatwin->barejid, otr_query_message);
+                    message_send_chat_encrypted(chatwin->barejid, otr_query_message);
                 }
             }
         }
diff --git a/src/otr/otr.c b/src/otr/otr.c
index 7c500e71..d0515e97 100644
--- a/src/otr/otr.c
+++ b/src/otr/otr.c
@@ -110,7 +110,7 @@ static void
 cb_inject_message(void *opdata, const char *accountname,
     const char *protocol, const char *recipient, const char *message)
 {
-    message_send_chat(recipient, message);
+    message_send_chat_encrypted(recipient, message);
 }
 
 static void
diff --git a/src/server_events.c b/src/server_events.c
index e5602b87..b0a9b1e3 100644
--- a/src/server_events.c
+++ b/src/server_events.c
@@ -336,7 +336,7 @@ handle_incoming_message(char *barejid, char *resource, char *message)
                 memmove(whitespace_base, whitespace_base+tag_length, tag_length);
                 char *otr_query_message = otr_start_query();
                 cons_show("OTR Whitespace pattern detected. Attempting to start OTR session...");
-                message_send_chat(barejid, otr_query_message);
+                message_send_chat_encrypted(barejid, otr_query_message);
             }
         }
     }
@@ -350,7 +350,7 @@ handle_incoming_message(char *barejid, char *resource, char *message)
     if (policy == PROF_OTRPOLICY_ALWAYS && !was_decrypted && !whitespace_base) {
         char *otr_query_message = otr_start_query();
         cons_show("Attempting to start OTR session...");
-        message_send_chat(barejid, otr_query_message);
+        message_send_chat_encrypted(barejid, otr_query_message);
     }
 
     ui_incoming_msg(barejid, resource, newmessage, NULL);
diff --git a/src/xmpp/message.c b/src/xmpp/message.c
index df6afed6..ab7f52cd 100644
--- a/src/xmpp/message.c
+++ b/src/xmpp/message.c
@@ -93,14 +93,42 @@ message_send_chat(const char * const barejid, const char * const msg)
             state = STANZA_NAME_ACTIVE;
         }
         Jid *jidp = jid_create_from_bare_and_resource(session->barejid, session->resource);
-        message = stanza_create_message(ctx, jidp->fulljid, STANZA_TYPE_CHAT, msg, state);
+        message = stanza_create_message(ctx, jidp->fulljid, STANZA_TYPE_CHAT, msg, state, false);
         jid_destroy(jidp);
     } else {
         char *state = NULL;
         if (prefs_get_boolean(PREF_STATES)) {
             state = STANZA_NAME_ACTIVE;
         }
-        message = stanza_create_message(ctx, barejid, STANZA_TYPE_CHAT, msg, state);
+        message = stanza_create_message(ctx, barejid, STANZA_TYPE_CHAT, msg, state, false);
+    }
+
+    xmpp_send(conn, message);
+    xmpp_stanza_release(message);
+}
+
+void
+message_send_chat_encrypted(const char * const barejid, const char * const msg)
+{
+    xmpp_stanza_t *message;
+    xmpp_conn_t * const conn = connection_get_conn();
+    xmpp_ctx_t * const ctx = connection_get_ctx();
+
+    ChatSession *session = chat_session_get(barejid);
+    if (session) {
+        char *state = NULL;
+        if (prefs_get_boolean(PREF_STATES) && session->send_states) {
+            state = STANZA_NAME_ACTIVE;
+        }
+        Jid *jidp = jid_create_from_bare_and_resource(session->barejid, session->resource);
+        message = stanza_create_message(ctx, jidp->fulljid, STANZA_TYPE_CHAT, msg, state, true);
+        jid_destroy(jidp);
+    } else {
+        char *state = NULL;
+        if (prefs_get_boolean(PREF_STATES)) {
+            state = STANZA_NAME_ACTIVE;
+        }
+        message = stanza_create_message(ctx, barejid, STANZA_TYPE_CHAT, msg, state, true);
     }
 
     xmpp_send(conn, message);
@@ -112,7 +140,7 @@ message_send_private(const char * const fulljid, const char * const msg)
 {
     xmpp_conn_t * const conn = connection_get_conn();
     xmpp_ctx_t * const ctx = connection_get_ctx();
-    xmpp_stanza_t *message = stanza_create_message(ctx, fulljid, STANZA_TYPE_CHAT, msg, NULL);
+    xmpp_stanza_t *message = stanza_create_message(ctx, fulljid, STANZA_TYPE_CHAT, msg, NULL, false);
 
     xmpp_send(conn, message);
     xmpp_stanza_release(message);
@@ -123,7 +151,7 @@ message_send_groupchat(const char * const roomjid, const char * const msg)
 {
     xmpp_conn_t * const conn = connection_get_conn();
     xmpp_ctx_t * const ctx = connection_get_ctx();
-    xmpp_stanza_t *message = stanza_create_message(ctx, roomjid, STANZA_TYPE_GROUPCHAT, msg, NULL);
+    xmpp_stanza_t *message = stanza_create_message(ctx, roomjid, STANZA_TYPE_GROUPCHAT, msg, NULL, false);
 
     xmpp_send(conn, message);
     xmpp_stanza_release(message);
@@ -424,7 +452,7 @@ _chat_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
     }
 
     // check if carbon message
-    xmpp_stanza_t *received = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_CARBONS); 
+    xmpp_stanza_t *received = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_CARBONS);
     if(received != NULL){
         xmpp_stanza_t *forwarded = xmpp_stanza_get_child_by_ns(received, STANZA_NS_FORWARD);
         xmpp_stanza_t *message = xmpp_stanza_get_child_by_name(forwarded, STANZA_NAME_MESSAGE);
@@ -434,7 +462,7 @@ _chat_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
         gchar *to = xmpp_stanza_get_attribute(message, STANZA_ATTR_TO);
         gchar *from = xmpp_stanza_get_attribute(message, STANZA_ATTR_FROM);
 
-        // happens when receive a carbon of a self sent message 
+        // happens when receive a carbon of a self sent message
         if(to == NULL) {
             to = from;
         }
diff --git a/src/xmpp/stanza.c b/src/xmpp/stanza.c
index 9b1431a9..e849ac97 100644
--- a/src/xmpp/stanza.c
+++ b/src/xmpp/stanza.c
@@ -284,7 +284,7 @@ stanza_create_room_subject_message(xmpp_ctx_t *ctx, const char * const room, con
 xmpp_stanza_t *
 stanza_create_message(xmpp_ctx_t *ctx, const char * const recipient,
     const char * const type, const char * const message,
-    const char * const state)
+    const char * const state, gboolean encrypted)
 {
     xmpp_stanza_t *msg, *body, *text;
 
@@ -314,6 +314,14 @@ stanza_create_message(xmpp_ctx_t *ctx, const char * const recipient,
         xmpp_stanza_release(chat_state);
     }
 
+    if (encrypted) {
+        xmpp_stanza_t *private_carbon = xmpp_stanza_new(ctx);
+        xmpp_stanza_set_name(private_carbon, "private");
+        xmpp_stanza_set_ns(private_carbon, STANZA_NS_CARBONS);
+        xmpp_stanza_add_child(msg, private_carbon);
+        xmpp_stanza_release(private_carbon);
+    }
+
     return msg;
 }
 
diff --git a/src/xmpp/stanza.h b/src/xmpp/stanza.h
index 4291e1fd..365bed86 100644
--- a/src/xmpp/stanza.h
+++ b/src/xmpp/stanza.h
@@ -191,7 +191,7 @@ xmpp_stanza_t* stanza_create_chat_state(xmpp_ctx_t *ctx,
 
 xmpp_stanza_t* stanza_create_message(xmpp_ctx_t *ctx,
     const char * const recipient, const char * const type,
-    const char * const message, const char * const state);
+    const char * const message, const char * const state, gboolean encrypted);
 
 xmpp_stanza_t* stanza_create_room_join_presence(xmpp_ctx_t * const ctx,
     const char * const full_room_jid, const char * const passwd);
diff --git a/src/xmpp/xmpp.h b/src/xmpp/xmpp.h
index 724e11c4..e8e79ee9 100644
--- a/src/xmpp/xmpp.h
+++ b/src/xmpp/xmpp.h
@@ -146,6 +146,7 @@ GList * jabber_get_available_resources(void);
 
 // message functions
 void message_send_chat(const char * const barejid, const char * const msg);
+void message_send_chat_encrypted(const char * const barejid, const char * const msg);
 void message_send_private(const char * const fulljid, const char * const msg);
 void message_send_groupchat(const char * const roomjid, const char * const msg);
 void message_send_groupchat_subject(const char * const roomjid, const char * const subject);
diff --git a/tests/test_cmd_otr.c b/tests/test_cmd_otr.c
index c6c6f7cf..dae17947 100644
--- a/tests/test_cmd_otr.c
+++ b/tests/test_cmd_otr.c
@@ -551,8 +551,8 @@ cmd_otr_start_sends_otr_query_message_to_current_recipeint(void **state)
     will_return(otr_key_loaded, TRUE);
     will_return(otr_start_query, query_message);
 
-    expect_string(message_send_chat, barejid, chatwin->barejid);
-    expect_string(message_send_chat, msg, query_message);
+    expect_string(message_send_chat_encrypted, barejid, chatwin->barejid);
+    expect_string(message_send_chat_encrypted, msg, query_message);
 
     gboolean result = cmd_otr(args, *help);
     assert_true(result);
diff --git a/tests/xmpp/stub_xmpp.c b/tests/xmpp/stub_xmpp.c
index dc9a258e..9ca0c2d4 100644
--- a/tests/xmpp/stub_xmpp.c
+++ b/tests/xmpp/stub_xmpp.c
@@ -64,6 +64,12 @@ void message_send_chat(const char * const barejid, const char * const msg)
     check_expected(msg);
 }
 
+void message_send_chat_encrypted(const char * const barejid, const char * const msg)
+{
+    check_expected(barejid);
+    check_expected(msg);
+}
+
 void message_send_private(const char * const fulljid, const char * const msg) {}
 void message_send_groupchat(const char * const roomjid, const char * const msg) {}
 void message_send_groupchat_subject(const char * const roomjid, const char * const subject) {}