From ac4b59eda08c66d037c794022fff7e3231768c72 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 28 Dec 2014 22:40:59 +0000 Subject: [PATCH] Fixed chat state for non OTR --- src/chat_session.c | 21 +++++++- src/chat_session.h | 3 +- src/command/command.c | 34 ++---------- src/command/commands.c | 106 +++++-------------------------------- src/otr/otr.c | 11 +--- src/server_events.c | 22 +------- src/xmpp/message.c | 2 +- tests/test_server_events.c | 2 +- 8 files changed, 41 insertions(+), 160 deletions(-) diff --git a/src/chat_session.c b/src/chat_session.c index 14436f6c..9e9056a8 100644 --- a/src/chat_session.c +++ b/src/chat_session.c @@ -82,7 +82,7 @@ chat_sessions_clear(void) } void -chat_session_start(const char * const recipient, gboolean recipient_supports) +chat_session_new(const char * const recipient, gboolean recipient_supports) { ChatSession *new_session = malloc(sizeof(struct chat_session_t)); new_session->recipient = strdup(recipient); @@ -260,6 +260,23 @@ chat_session_set_recipient_supports(const char * const recipient, } } +gboolean +chat_session_on_message_send(const char * const barejid) +{ + gboolean send_state = FALSE; + if (prefs_get_boolean(PREF_STATES)) { + if (!chat_session_exists(barejid)) { + chat_session_new(barejid, TRUE); + } + if (chat_session_get_recipient_supports(barejid)) { + chat_session_set_active(barejid); + send_state = TRUE; + } + } + + return send_state; +} + static void _chat_session_free(ChatSession *session) { @@ -271,4 +288,4 @@ _chat_session_free(ChatSession *session) } free(session); } -} +} \ No newline at end of file diff --git a/src/chat_session.h b/src/chat_session.h index 43148ca9..efb2b6ce 100644 --- a/src/chat_session.h +++ b/src/chat_session.h @@ -39,7 +39,7 @@ void chat_sessions_init(void); void chat_sessions_clear(void); -void chat_session_start(const char * const recipient, +void chat_session_new(const char * const recipient, gboolean recipient_supports); gboolean chat_session_exists(const char * const recipient); void chat_session_end(const char * const recipient); @@ -56,5 +56,6 @@ gboolean chat_session_is_gone(const char * const recipient); void chat_session_set_gone(const char * const recipient); void chat_session_set_sent(const char * const recipient); gboolean chat_session_get_sent(const char * const recipient); +gboolean chat_session_on_message_send(const char * const barejid); #endif diff --git a/src/command/command.c b/src/command/command.c index a390ae1b..720ff19f 100644 --- a/src/command/command.c +++ b/src/command/command.c @@ -1803,17 +1803,7 @@ cmd_execute_default(const char * inp) if (otr_is_secure(chatwin->barejid)) { char *encrypted = otr_encrypt_message(chatwin->barejid, inp); if (encrypted != NULL) { - gboolean send_state = FALSE; - if (prefs_get_boolean(PREF_STATES)) { - if (!chat_session_exists(chatwin->barejid)) { - chat_session_start(chatwin->barejid, TRUE); - } - if (chat_session_get_recipient_supports(chatwin->barejid)) { - chat_session_set_active(chatwin->barejid); - send_state = TRUE; - } - } - + gboolean send_state = chat_session_on_message_send(chatwin->barejid); message_send_chat(chatwin->barejid, chatwin->barejid, encrypted, send_state); otr_free_message(encrypted); if (prefs_get_boolean(PREF_CHLOG)) { @@ -1834,16 +1824,7 @@ cmd_execute_default(const char * inp) cons_show_error("Failed to send message."); } } else { - gboolean send_state = FALSE; - if (prefs_get_boolean(PREF_STATES)) { - if (!chat_session_exists(chatwin->barejid)) { - chat_session_start(chatwin->barejid, TRUE); - } - if (chat_session_get_recipient_supports(chatwin->barejid)) { - chat_session_set_active(chatwin->barejid); - send_state = TRUE; - } - } + gboolean send_state = chat_session_on_message_send(chatwin->barejid); message_send_chat(chatwin->barejid, chatwin->resource, inp, send_state); if (prefs_get_boolean(PREF_CHLOG)) { const char *jid = jabber_get_fulljid(); @@ -1855,16 +1836,7 @@ cmd_execute_default(const char * inp) ui_outgoing_chat_msg("me", chatwin->barejid, inp); } #else - gboolean send_state = FALSE; - if (prefs_get_boolean(PREF_STATES)) { - if (!chat_session_exists(chatwin->barejid)) { - chat_session_start(chatwin->barejid, TRUE); - } - if (chat_session_get_recipient_supports(chatwin->barejid)) { - chat_session_set_active(chatwin->barejid); - send_state = TRUE; - } - } + gboolean send_state = chat_session_on_message_send(chatwin->barejid); message_send_chat(chatwin->barejid, chatwin->resource, inp, send_state); 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 44e91492..a6280b93 100644 --- a/src/command/commands.c +++ b/src/command/commands.c @@ -1168,16 +1168,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) { - gboolean send_state = FALSE; - if (prefs_get_boolean(PREF_STATES)) { - if (!chat_session_exists(barejid)) { - chat_session_start(barejid, TRUE); - } - if (chat_session_get_recipient_supports(barejid)) { - chat_session_set_active(barejid); - send_state = TRUE; - } - } + gboolean send_state = chat_session_on_message_send(barejid); message_send_chat(barejid, resource, encrypted, send_state); otr_free_message(encrypted); ui_outgoing_chat_msg("me", barejid, msg); @@ -1207,31 +1198,12 @@ 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); - - gboolean send_state = FALSE; - if (prefs_get_boolean(PREF_STATES)) { - if (!chat_session_exists(barejid)) { - chat_session_start(barejid, TRUE); - } - if (chat_session_get_recipient_supports(barejid)) { - chat_session_set_active(barejid); - send_state = TRUE; - } - } + gboolean send_state = chat_session_on_message_send(barejid); message_send_chat(barejid, resource, otr_message->str, send_state); g_string_free(otr_message, TRUE); } else { - gboolean send_state = FALSE; - if (prefs_get_boolean(PREF_STATES)) { - if (!chat_session_exists(barejid)) { - chat_session_start(barejid, TRUE); - } - if (chat_session_get_recipient_supports(barejid)) { - chat_session_set_active(barejid); - send_state = TRUE; - } - } + gboolean send_state = chat_session_on_message_send(barejid); message_send_chat(barejid, resource, msg, send_state); } ui_outgoing_chat_msg("me", barejid, msg); @@ -1245,16 +1217,7 @@ cmd_msg(gchar **args, struct cmd_help_t help) } return TRUE; #else - gboolean send_state = FALSE; - if (prefs_get_boolean(PREF_STATES)) { - if (!chat_session_exists(barejid)) { - chat_session_start(barejid, TRUE); - } - if (chat_session_get_recipient_supports(barejid)) { - chat_session_set_active(barejid); - send_state = TRUE; - } - } + gboolean send_state = chat_session_on_message_send(barejid); message_send_chat(barejid, resource, msg, send_state); ui_outgoing_chat_msg("me", barejid, msg); @@ -1270,7 +1233,7 @@ cmd_msg(gchar **args, struct cmd_help_t help) } else { // msg == NULL if (prefs_get_boolean(PREF_STATES)) { if (!chat_session_exists(barejid)) { - chat_session_start(barejid, TRUE); + chat_session_new(barejid, TRUE); } } @@ -3000,16 +2963,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) { - gboolean send_state = FALSE; - if (prefs_get_boolean(PREF_STATES)) { - if (!chat_session_exists(chatwin->barejid)) { - chat_session_start(chatwin->barejid, TRUE); - } - if (chat_session_get_recipient_supports(chatwin->barejid)) { - chat_session_set_active(chatwin->barejid); - send_state = TRUE; - } - } + gboolean send_state = chat_session_on_message_send(chatwin->barejid); message_send_chat(chatwin->barejid, chatwin->resource, encrypted, send_state); otr_free_message(encrypted); if (prefs_get_boolean(PREF_CHLOG)) { @@ -3030,16 +2984,7 @@ cmd_tiny(gchar **args, struct cmd_help_t help) cons_show_error("Failed to send message."); } } else { - gboolean send_state = FALSE; - if (prefs_get_boolean(PREF_STATES)) { - if (!chat_session_exists(chatwin->barejid)) { - chat_session_start(chatwin->barejid, TRUE); - } - if (chat_session_get_recipient_supports(chatwin->barejid)) { - chat_session_set_active(chatwin->barejid); - send_state = TRUE; - } - } + gboolean send_state = chat_session_on_message_send(chatwin->barejid); message_send_chat(chatwin->barejid, chatwin->resource, tiny, send_state); if (prefs_get_boolean(PREF_CHLOG)) { const char *jid = jabber_get_fulljid(); @@ -3051,17 +2996,8 @@ cmd_tiny(gchar **args, struct cmd_help_t help) ui_outgoing_chat_msg("me", chatwin->barejid, tiny); } #else - gboolean send_state = FALSE; - if (prefs_get_boolean(PREF_STATES)) { - if (!chat_session_exists(chatwin->barejid)) { - chat_session_start(chatwin->barejid, TRUE); - } - if (chat_session_get_recipient_supports(chatwin->barejid)) { - chat_session_set_active(chatwin->barejid); - send_state = TRUE; - } - } - message_send_chat(chatwin->barejid, chatwin->resource, tiny); + gboolean send_state = chat_session_on_message_send(chatwin->barejid); + message_send_chat(chatwin->barejid, chatwin->resource, tiny, send_state); if (prefs_get_boolean(PREF_CHLOG)) { const char *jid = jabber_get_fulljid(); Jid *jidp = jid_create(jid); @@ -3971,7 +3907,7 @@ cmd_otr(gchar **args, struct cmd_help_t help) if (prefs_get_boolean(PREF_STATES)) { if (!chat_session_exists(barejid)) { - chat_session_start(barejid, TRUE); + chat_session_new(barejid, TRUE); } } @@ -3984,16 +3920,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(); - gboolean send_state = FALSE; - if (prefs_get_boolean(PREF_STATES)) { - if (!chat_session_exists(barejid)) { - chat_session_start(barejid, TRUE); - } - if (chat_session_get_recipient_supports(barejid)) { - chat_session_set_active(barejid); - send_state = TRUE; - } - } + gboolean send_state = chat_session_on_message_send(barejid); message_send_chat(barejid, NULL, otr_query_message, send_state); } else { ui_gone_secure(barejid, otr_is_trusted(barejid)); @@ -4012,16 +3939,7 @@ cmd_otr(gchar **args, struct cmd_help_t help) } else { ProfChatWin *chatwin = ui_get_current_chat(); char *otr_query_message = otr_start_query(); - gboolean send_state = FALSE; - if (prefs_get_boolean(PREF_STATES)) { - if (!chat_session_exists(chatwin->barejid)) { - chat_session_start(chatwin->barejid, TRUE); - } - if (chat_session_get_recipient_supports(chatwin->barejid)) { - chat_session_set_active(chatwin->barejid); - send_state = TRUE; - } - } + gboolean send_state = chat_session_on_message_send(chatwin->barejid); message_send_chat(chatwin->barejid, NULL, otr_query_message, send_state); } } diff --git a/src/otr/otr.c b/src/otr/otr.c index 3c0fe990..680c4c10 100644 --- a/src/otr/otr.c +++ b/src/otr/otr.c @@ -110,16 +110,7 @@ static void cb_inject_message(void *opdata, const char *accountname, const char *protocol, const char *recipient, const char *message) { - gboolean send_state = FALSE; - if (prefs_get_boolean(PREF_STATES)) { - if (!chat_session_exists(recipient)) { - chat_session_start(recipient, TRUE); - } - if (chat_session_get_recipient_supports(recipient)) { - chat_session_set_active(recipient); - send_state = TRUE; - } - } + gboolean send_state = chat_session_on_message_send(recipient); message_send_chat(recipient, NULL, message, send_state); } diff --git a/src/server_events.c b/src/server_events.c index e7360f0d..96014bf4 100644 --- a/src/server_events.c +++ b/src/server_events.c @@ -320,16 +320,7 @@ handle_incoming_message(char *barejid, 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..."); - gboolean send_state = FALSE; - if (prefs_get_boolean(PREF_STATES)) { - if (!chat_session_exists(barejid)) { - chat_session_start(barejid, TRUE); - } - if (chat_session_get_recipient_supports(barejid)) { - chat_session_set_active(barejid); - send_state = TRUE; - } - } + gboolean send_state = chat_session_on_message_send(barejid); message_send_chat(barejid, NULL, otr_query_message, send_state); } } @@ -344,16 +335,7 @@ handle_incoming_message(char *barejid, 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..."); - gboolean send_state = FALSE; - if (prefs_get_boolean(PREF_STATES)) { - if (!chat_session_exists(barejid)) { - chat_session_start(barejid, TRUE); - } - if (chat_session_get_recipient_supports(barejid)) { - chat_session_set_active(barejid); - send_state = TRUE; - } - } + gboolean send_state = chat_session_on_message_send(barejid); message_send_chat(barejid, NULL, otr_query_message, send_state); } diff --git a/src/xmpp/message.c b/src/xmpp/message.c index d2748a13..2cf7c4d3 100644 --- a/src/xmpp/message.c +++ b/src/xmpp/message.c @@ -460,7 +460,7 @@ _chat_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, // create or update chat session if (!chat_session_exists(jid->barejid)) { - chat_session_start(jid->barejid, recipient_supports); + chat_session_new(jid->barejid, recipient_supports); } else { chat_session_set_recipient_supports(jid->barejid, recipient_supports); } diff --git a/tests/test_server_events.c b/tests/test_server_events.c index a0ac61ef..f3c571b8 100644 --- a/tests/test_server_events.c +++ b/tests/test_server_events.c @@ -134,7 +134,7 @@ void handle_message_error_when_recipient_cancel_disables_chat_session(void **sta prefs_set_boolean(PREF_STATES, TRUE); chat_sessions_init(); - chat_session_start(from, TRUE); + chat_session_new(from, TRUE); expect_any(ui_handle_recipient_not_found, recipient); expect_any(ui_handle_recipient_not_found, err_msg);