From f219302f069e1db38ae656f1d8cca931756f388c Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 27 Apr 2015 22:34:06 +0100 Subject: [PATCH 1/3] Renamed incoming otr message --- src/event/server_events.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/event/server_events.c b/src/event/server_events.c index bd4634e5..cc76ed17 100644 --- a/src/event/server_events.c +++ b/src/event/server_events.c @@ -322,7 +322,7 @@ srv_incoming_message(char *barejid, char *resource, char *message) { #ifdef HAVE_LIBOTR gboolean was_decrypted = FALSE; - char *newmessage; + char *otr_message; prof_otrpolicy_t policy = otr_get_policy(barejid); char *whitespace_base = strstr(message,OTRL_MESSAGE_TAG_BASE); @@ -344,10 +344,10 @@ srv_incoming_message(char *barejid, char *resource, char *message) } } } - newmessage = otr_decrypt_message(barejid, message, &was_decrypted); + otr_message = otr_decrypt_message(barejid, message, &was_decrypted); // internal OTR message - if (newmessage == NULL) { + if (otr_message == NULL) { return; } @@ -357,9 +357,9 @@ srv_incoming_message(char *barejid, char *resource, char *message) message_send_chat_encrypted(barejid, otr_query_message); } - ui_incoming_msg(barejid, resource, newmessage, NULL); - chat_log_otr_msg_in(barejid, newmessage, was_decrypted); - otr_free_message(newmessage); + ui_incoming_msg(barejid, resource, otr_message, NULL); + chat_log_otr_msg_in(barejid, otr_message, was_decrypted); + otr_free_message(otr_message); #else ui_incoming_msg(barejid, resource, message, NULL); chat_log_msg_in(barejid, message); From eb177ccbefe24732dc1c0a127d1720f1809e53e5 Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 27 Apr 2015 23:17:38 +0100 Subject: [PATCH 2/3] Removed server_events dependency on libotr headers --- src/event/server_events.c | 41 +----------------------------------- src/otr/otr.c | 44 +++++++++++++++++++++++++++++++++++++++ src/otr/otr.h | 1 + tests/otr/stub_otr.c | 3 ++- 4 files changed, 48 insertions(+), 41 deletions(-) diff --git a/src/event/server_events.c b/src/event/server_events.c index cc76ed17..514835ba 100644 --- a/src/event/server_events.c +++ b/src/event/server_events.c @@ -46,7 +46,6 @@ #ifdef HAVE_LIBOTR #include "otr/otr.h" -#include #endif #include "ui/ui.h" @@ -321,45 +320,7 @@ void srv_incoming_message(char *barejid, char *resource, char *message) { #ifdef HAVE_LIBOTR - gboolean was_decrypted = FALSE; - char *otr_message; - - prof_otrpolicy_t policy = otr_get_policy(barejid); - char *whitespace_base = strstr(message,OTRL_MESSAGE_TAG_BASE); - - //check for OTR whitespace (opportunistic or always) - if (policy == PROF_OTRPOLICY_OPPORTUNISTIC || policy == PROF_OTRPOLICY_ALWAYS) { - if (whitespace_base) { - if (strstr(message, OTRL_MESSAGE_TAG_V2) || strstr(message, OTRL_MESSAGE_TAG_V1)) { - // Remove whitespace pattern for proper display in UI - // Handle both BASE+TAGV1/2(16+8) and BASE+TAGV1+TAGV2(16+8+8) - int tag_length = 24; - if (strstr(message, OTRL_MESSAGE_TAG_V2) && strstr(message, OTRL_MESSAGE_TAG_V1)) { - tag_length = 32; - } - 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_encrypted(barejid, otr_query_message); - } - } - } - otr_message = otr_decrypt_message(barejid, message, &was_decrypted); - - // internal OTR message - if (otr_message == NULL) { - return; - } - - 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_encrypted(barejid, otr_query_message); - } - - ui_incoming_msg(barejid, resource, otr_message, NULL); - chat_log_otr_msg_in(barejid, otr_message, was_decrypted); - otr_free_message(otr_message); + otr_on_message_recv(barejid, resource, message); #else ui_incoming_msg(barejid, resource, message, NULL); chat_log_msg_in(barejid, message); diff --git a/src/otr/otr.c b/src/otr/otr.c index 9dcff1f9..70bf9761 100644 --- a/src/otr/otr.c +++ b/src/otr/otr.c @@ -269,6 +269,50 @@ otr_on_connect(ProfAccount *account) return; } +void +otr_on_message_recv(const char * const barejid, const char * const resource, const char * const message) +{ + gboolean was_decrypted = FALSE; + char *decrypted; + + prof_otrpolicy_t policy = otr_get_policy(barejid); + char *whitespace_base = strstr(message, OTRL_MESSAGE_TAG_BASE); + + //check for OTR whitespace (opportunistic or always) + if (policy == PROF_OTRPOLICY_OPPORTUNISTIC || policy == PROF_OTRPOLICY_ALWAYS) { + if (whitespace_base) { + if (strstr(message, OTRL_MESSAGE_TAG_V2) || strstr(message, OTRL_MESSAGE_TAG_V1)) { + // Remove whitespace pattern for proper display in UI + // Handle both BASE+TAGV1/2(16+8) and BASE+TAGV1+TAGV2(16+8+8) + int tag_length = 24; + if (strstr(message, OTRL_MESSAGE_TAG_V2) && strstr(message, OTRL_MESSAGE_TAG_V1)) { + tag_length = 32; + } + 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_encrypted(barejid, otr_query_message); + } + } + } + decrypted = otr_decrypt_message(barejid, message, &was_decrypted); + + // internal OTR message + if (decrypted == NULL) { + return; + } + + 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_encrypted(barejid, otr_query_message); + } + + ui_incoming_msg(barejid, resource, decrypted, NULL); + chat_log_otr_msg_in(barejid, decrypted, was_decrypted); + otr_free_message(decrypted); +} + void otr_keygen(ProfAccount *account) { diff --git a/src/otr/otr.h b/src/otr/otr.h index 91445a5c..8eb322ed 100644 --- a/src/otr/otr.h +++ b/src/otr/otr.h @@ -56,6 +56,7 @@ char* otr_libotr_version(void); char* otr_start_query(void); void otr_poll(void); void otr_on_connect(ProfAccount *account); +void otr_on_message_recv(const char * const barejid, const char * const resource, const char * const message); void otr_keygen(ProfAccount *account); char* otr_tag_message(const char * const msg); diff --git a/tests/otr/stub_otr.c b/tests/otr/stub_otr.c index 7a0122b6..8e0a5d75 100644 --- a/tests/otr/stub_otr.c +++ b/tests/otr/stub_otr.c @@ -41,6 +41,7 @@ char* otr_start_query(void) void otr_poll(void) {} void otr_on_connect(ProfAccount *account) {} +void otr_on_message_recv(const char * const barejid, const char * const resource, const char * const message) {} void otr_keygen(ProfAccount *account) { @@ -53,7 +54,7 @@ gboolean otr_key_loaded(void) } char* otr_tag_message(const char * const msg) -{ +{ return NULL; } From 70d1756a1cf380a75d4e4191670787e5dc6bf182 Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 27 Apr 2015 23:30:33 +0100 Subject: [PATCH 3/3] Moved otr message send event to otr module --- src/event/client_events.c | 36 +++--------------------------------- src/otr/otr.c | 37 +++++++++++++++++++++++++++++++++++++ src/otr/otr.h | 3 +++ tests/otr/stub_otr.c | 1 + 4 files changed, 44 insertions(+), 33 deletions(-) diff --git a/src/event/client_events.c b/src/event/client_events.c index 2a1d349a..2aa66d08 100644 --- a/src/event/client_events.c +++ b/src/event/client_events.c @@ -62,44 +62,14 @@ client_connect_account(ProfAccount *account) void client_send_msg(const char * const barejid, const char * const msg) { - char *id = NULL; - #ifdef HAVE_LIBOTR - prof_otrpolicy_t policy = otr_get_policy(barejid); - - if (otr_is_secure(barejid)) { - char *encrypted = otr_encrypt_message(barejid, msg); - if (encrypted != NULL) { - id = message_send_chat_encrypted(barejid, encrypted); - chat_log_otr_msg_out(barejid, msg); - ui_outgoing_chat_msg(barejid, msg, id); - otr_free_message(encrypted); - } else { - cons_show_error("Failed to encrypt and send message."); - } - - } else if (policy == PROF_OTRPOLICY_ALWAYS) { - cons_show_error("Failed to send message. Please check OTR policy"); - - } else if (policy == PROF_OTRPOLICY_OPPORTUNISTIC) { - char *otr_tagged_msg = otr_tag_message(msg); - id = message_send_chat_encrypted(barejid, otr_tagged_msg); - ui_outgoing_chat_msg(barejid, msg, id); - chat_log_msg_out(barejid, msg); - free(otr_tagged_msg); - - } else { - id = message_send_chat(barejid, msg); - ui_outgoing_chat_msg(barejid, msg, id); - chat_log_msg_out(barejid, msg); - } + otr_on_message_send(barejid, msg); #else - id = message_send_chat(barejid, msg); + char *id = message_send_chat(barejid, msg); chat_log_msg_out(barejid, msg); ui_outgoing_chat_msg(barejid, msg, id); -#endif - free(id); +#endif } void diff --git a/src/otr/otr.c b/src/otr/otr.c index 70bf9761..c8518c70 100644 --- a/src/otr/otr.c +++ b/src/otr/otr.c @@ -313,6 +313,43 @@ otr_on_message_recv(const char * const barejid, const char * const resource, con otr_free_message(decrypted); } +void +otr_on_message_send(const char * const barejid, const char * const message) +{ + char *id = NULL; + + prof_otrpolicy_t policy = otr_get_policy(barejid); + + if (otr_is_secure(barejid)) { + char *encrypted = otr_encrypt_message(barejid, message); + if (encrypted != NULL) { + id = message_send_chat_encrypted(barejid, encrypted); + chat_log_otr_msg_out(barejid, message); + ui_outgoing_chat_msg(barejid, message, id); + otr_free_message(encrypted); + } else { + cons_show_error("Failed to encrypt and send message."); + } + + } else if (policy == PROF_OTRPOLICY_ALWAYS) { + cons_show_error("Failed to send message. Please check OTR policy"); + + } else if (policy == PROF_OTRPOLICY_OPPORTUNISTIC) { + char *otr_tagged_msg = otr_tag_message(message); + id = message_send_chat_encrypted(barejid, otr_tagged_msg); + ui_outgoing_chat_msg(barejid, message, id); + chat_log_msg_out(barejid, message); + free(otr_tagged_msg); + + } else { + id = message_send_chat(barejid, message); + ui_outgoing_chat_msg(barejid, message, id); + chat_log_msg_out(barejid, message); + } + + free(id); +} + void otr_keygen(ProfAccount *account) { diff --git a/src/otr/otr.h b/src/otr/otr.h index 8eb322ed..8e1d22df 100644 --- a/src/otr/otr.h +++ b/src/otr/otr.h @@ -56,7 +56,10 @@ char* otr_libotr_version(void); char* otr_start_query(void); void otr_poll(void); void otr_on_connect(ProfAccount *account); + void otr_on_message_recv(const char * const barejid, const char * const resource, const char * const message); +void otr_on_message_send(const char * const barejid, const char * const message); + void otr_keygen(ProfAccount *account); char* otr_tag_message(const char * const msg); diff --git a/tests/otr/stub_otr.c b/tests/otr/stub_otr.c index 8e0a5d75..eb676877 100644 --- a/tests/otr/stub_otr.c +++ b/tests/otr/stub_otr.c @@ -42,6 +42,7 @@ char* otr_start_query(void) void otr_poll(void) {} void otr_on_connect(ProfAccount *account) {} void otr_on_message_recv(const char * const barejid, const char * const resource, const char * const message) {} +void otr_on_message_send(const char * const barejid, const char * const message) {} void otr_keygen(ProfAccount *account) {