From 8944a3b5bb1eece467870f90e434134e1a3fb23a Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 15 Mar 2015 23:18:50 +0000 Subject: [PATCH] Move common chat logging code to log.c --- src/command/command.c | 30 +------ src/command/commands.c | 180 ++++++++++++++++------------------------- src/log.c | 78 +++++++++++++++++- src/log.h | 10 ++- src/server_events.c | 33 +------- tests/log/stub_log.c | 10 ++- 6 files changed, 167 insertions(+), 174 deletions(-) diff --git a/src/command/command.c b/src/command/command.c index 5cf3f35d..5a882a20 100644 --- a/src/command/command.c +++ b/src/command/command.c @@ -1956,43 +1956,19 @@ _cmd_execute_default(const char * inp) if (encrypted != NULL) { char *id = message_send_chat_encrypted(chatwin->barejid, encrypted); otr_free_message(encrypted); - if (prefs_get_boolean(PREF_CHLOG)) { - const char *jid = jabber_get_fulljid(); - Jid *jidp = jid_create(jid); - char *pref_otr_log = prefs_get_string(PREF_OTR_LOG); - if (strcmp(pref_otr_log, "on") == 0) { - chat_log_chat(jidp->barejid, chatwin->barejid, inp, PROF_OUT_LOG, NULL); - } else if (strcmp(pref_otr_log, "redact") == 0) { - chat_log_chat(jidp->barejid, chatwin->barejid, "[redacted]", PROF_OUT_LOG, NULL); - } - prefs_free_string(pref_otr_log); - jid_destroy(jidp); - } - + chat_log_otr_msg_out(chatwin->barejid, inp); ui_outgoing_chat_msg(chatwin->barejid, inp, id); } else { cons_show_error("Failed to send message."); } } else { char *id = message_send_chat(chatwin->barejid, inp); - if (prefs_get_boolean(PREF_CHLOG)) { - const char *jid = jabber_get_fulljid(); - Jid *jidp = jid_create(jid); - chat_log_chat(jidp->barejid, chatwin->barejid, inp, PROF_OUT_LOG, NULL); - jid_destroy(jidp); - } - + chat_log_msg_out(chatwin->barejid, inp); ui_outgoing_chat_msg(chatwin->barejid, inp, id); } #else char *id = message_send_chat(chatwin->barejid, inp); - if (prefs_get_boolean(PREF_CHLOG)) { - const char *jid = jabber_get_fulljid(); - Jid *jidp = jid_create(jid); - chat_log_chat(jidp->barejid, chatwin->barejid, inp, PROF_OUT_LOG, NULL); - jid_destroy(jidp); - } - + chat_log_msg_out(chatwin->barejid, inp); ui_outgoing_chat_msg(chatwin->barejid, inp, id); #endif } diff --git a/src/command/commands.c b/src/command/commands.c index 528f44e2..05d3ee53 100644 --- a/src/command/commands.c +++ b/src/command/commands.c @@ -1224,114 +1224,100 @@ cmd_msg(gchar **args, struct cmd_help_t help) char *msg = args[1]; jabber_conn_status_t conn_status = jabber_get_connection_status(); - win_type_t win_type = ui_current_win_type(); if (conn_status != JABBER_CONNECTED) { cons_show("You are not currently connected."); return TRUE; } - if (win_type == WIN_MUC) { - ProfMucWin *mucwin = wins_get_current_muc(); - if (muc_roster_contains_nick(mucwin->roomjid, usr)) { - GString *full_jid = g_string_new(mucwin->roomjid); - g_string_append(full_jid, "/"); - g_string_append(full_jid, usr); + win_type_t win_type = ui_current_win_type(); + + switch (win_type) { + case WIN_MUC: { + ProfMucWin *mucwin = wins_get_current_muc(); + if (muc_roster_contains_nick(mucwin->roomjid, usr)) { + GString *full_jid = g_string_new(mucwin->roomjid); + g_string_append(full_jid, "/"); + g_string_append(full_jid, usr); + + if (msg) { + message_send_private(full_jid->str, msg); + ui_outgoing_private_msg(full_jid->str, msg); + } else { + ui_new_private_win(full_jid->str); + } + + g_string_free(full_jid, TRUE); - if (msg != NULL) { - message_send_private(full_jid->str, msg); - ui_outgoing_private_msg(full_jid->str, msg); } else { - ui_new_private_win(full_jid->str); + ui_current_print_line("No such participant \"%s\" in room.", usr); } - g_string_free(full_jid, TRUE); - - } else { - ui_current_print_line("No such participant \"%s\" in room.", usr); + return TRUE; } + default: { + char *barejid = roster_barejid_from_name(usr); + if (barejid == NULL) { + barejid = usr; + } - return TRUE; - - } else { - // get barejid - char *barejid = roster_barejid_from_name(usr); - if (barejid == NULL) { - barejid = usr; - } - - if (msg != NULL) { + if (msg) { #ifdef HAVE_LIBOTR - if (otr_is_secure(barejid)) { - char *encrypted = otr_encrypt_message(barejid, msg); - if (encrypted != NULL) { - char *id = message_send_chat_encrypted(barejid, encrypted); - otr_free_message(encrypted); - ui_outgoing_chat_msg(barejid, msg, id); - - if (((win_type == WIN_CHAT) || (win_type == WIN_CONSOLE)) && prefs_get_boolean(PREF_CHLOG)) { - const char *jid = jabber_get_fulljid(); - Jid *jidp = jid_create(jid); - char *pref_otr_log = prefs_get_string(PREF_OTR_LOG); - if (strcmp(pref_otr_log, "on") == 0) { - chat_log_chat(jidp->barejid, barejid, msg, PROF_OUT_LOG, NULL); - } else if (strcmp(pref_otr_log, "redact") == 0) { - chat_log_chat(jidp->barejid, barejid, "[redacted]", PROF_OUT_LOG, NULL); + if (otr_is_secure(barejid)) { + char *encrypted = otr_encrypt_message(barejid, msg); + if (encrypted) { + char *id = message_send_chat_encrypted(barejid, encrypted); + otr_free_message(encrypted); + ui_outgoing_chat_msg(barejid, msg, id); + if (win_type == WIN_CHAT || win_type == WIN_CONSOLE) { + chat_log_otr_msg_out(barejid, msg); } - prefs_free_string(pref_otr_log); - jid_destroy(jidp); + } else { + cons_show_error("Failed to encrypt and send message,"); } } else { - cons_show_error("Failed to encrypt and send message,"); - } - } else { - prof_otrpolicy_t policy = otr_get_policy(barejid); - char *id = NULL; + prof_otrpolicy_t policy = otr_get_policy(barejid); + char *id = NULL; - if (policy == PROF_OTRPOLICY_ALWAYS) { - cons_show_error("Failed to send message. Please check OTR policy"); - return TRUE; - } else if (policy == PROF_OTRPOLICY_OPPORTUNISTIC) { - 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); - id = message_send_chat_encrypted(barejid, otr_message->str); + if (policy == PROF_OTRPOLICY_ALWAYS) { + cons_show_error("Failed to send message. Please check OTR policy"); + return TRUE; + } else if (policy == PROF_OTRPOLICY_OPPORTUNISTIC) { + 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); + id = message_send_chat_encrypted(barejid, otr_message->str); - g_string_free(otr_message, TRUE); - } else { - id = message_send_chat(barejid, msg); + g_string_free(otr_message, TRUE); + } else { + id = message_send_chat(barejid, msg); + } + ui_outgoing_chat_msg(barejid, msg, id); + + if (win_type == WIN_CHAT || win_type == WIN_CONSOLE) { + chat_log_msg_out(barejid, msg); + } } + return TRUE; +#else + char *id = message_send_chat(barejid, msg); ui_outgoing_chat_msg(barejid, msg, id); - if (((win_type == WIN_CHAT) || (win_type == WIN_CONSOLE)) && prefs_get_boolean(PREF_CHLOG)) { - const char *jid = jabber_get_fulljid(); - Jid *jidp = jid_create(jid); - chat_log_chat(jidp->barejid, barejid, msg, PROF_OUT_LOG, NULL); - jid_destroy(jidp); + if (win_type == WIN_CHAT || win_type == WIN_CONSOLE) { + chat_log_msg_out(barejid, msg); } - } - return TRUE; -#else - char *id = message_send_chat(barejid, msg); - ui_outgoing_chat_msg(barejid, msg, id); - - if (((win_type == WIN_CHAT) || (win_type == WIN_CONSOLE)) && prefs_get_boolean(PREF_CHLOG)) { - const char *jid = jabber_get_fulljid(); - Jid *jidp = jid_create(jid); - chat_log_chat(jidp->barejid, barejid, msg, PROF_OUT_LOG, NULL); - jid_destroy(jidp); - } - return TRUE; + return TRUE; #endif - } else { // msg == NULL - ui_new_chat_win(barejid); + } else { // msg == NULL + ui_new_chat_win(barejid); #ifdef HAVE_LIBOTR - if (otr_is_secure(barejid)) { - ui_gone_secure(barejid, otr_is_trusted(barejid)); - } + if (otr_is_secure(barejid)) { + ui_gone_secure(barejid, otr_is_trusted(barejid)); + } #endif - return TRUE; + return TRUE; + } } } } @@ -3076,43 +3062,19 @@ cmd_tiny(gchar **args, struct cmd_help_t help) if (encrypted != NULL) { char *id = message_send_chat_encrypted(chatwin->barejid, encrypted); otr_free_message(encrypted); - if (prefs_get_boolean(PREF_CHLOG)) { - const char *jid = jabber_get_fulljid(); - Jid *jidp = jid_create(jid); - char *pref_otr_log = prefs_get_string(PREF_OTR_LOG); - if (strcmp(pref_otr_log, "on") == 0) { - chat_log_chat(jidp->barejid, chatwin->barejid, tiny, PROF_OUT_LOG, NULL); - } else if (strcmp(pref_otr_log, "redact") == 0) { - chat_log_chat(jidp->barejid, chatwin->barejid, "[redacted]", PROF_OUT_LOG, NULL); - } - prefs_free_string(pref_otr_log); - jid_destroy(jidp); - } - + chat_log_otr_msg_out(chatwin->barejid, tiny); ui_outgoing_chat_msg(chatwin->barejid, tiny, id); } else { cons_show_error("Failed to send message."); } } else { char *id = message_send_chat(chatwin->barejid, tiny); - if (prefs_get_boolean(PREF_CHLOG)) { - const char *jid = jabber_get_fulljid(); - Jid *jidp = jid_create(jid); - chat_log_chat(jidp->barejid, chatwin->barejid, tiny, PROF_OUT_LOG, NULL); - jid_destroy(jidp); - } - + chat_log_msg_out(chatwin->barejid, tiny); ui_outgoing_chat_msg(chatwin->barejid, tiny, id); } #else char *id = message_send_chat(chatwin->barejid, tiny); - if (prefs_get_boolean(PREF_CHLOG)) { - const char *jid = jabber_get_fulljid(); - Jid *jidp = jid_create(jid); - chat_log_chat(jidp->barejid, chatwin->barejid, tiny, PROF_OUT_LOG, NULL); - jid_destroy(jidp); - } - + chat_log_msg_out(chatwin->barejid, tiny); ui_outgoing_chat_msg(chatwin->barejid, tiny, id); #endif } else if (win_type == WIN_PRIVATE) { diff --git a/src/log.c b/src/log.c index 004d4dcf..ac0dfd2e 100644 --- a/src/log.c +++ b/src/log.c @@ -46,6 +46,7 @@ #include "common.h" #include "config/preferences.h" +#include "xmpp/xmpp.h" #define PROF "prof" @@ -66,7 +67,7 @@ struct dated_chat_log { }; static gboolean _log_roll_needed(struct dated_chat_log *dated_log); -static struct dated_chat_log * _create_log(char *other, const char * const login); +static struct dated_chat_log * _create_log(const char * const other, const char * const login); static struct dated_chat_log * _create_groupchat_log(char *room, const char * const login); static void _free_chat_log(struct dated_chat_log *dated_log); static gboolean _key_equals(void *key1, void *key2); @@ -78,6 +79,8 @@ static gchar * _get_chatlog_dir(void); static gchar * _get_main_log_file(void); static void _rotate_log_file(void); static char* _log_string_from_level(log_level_t level); +static void _chat_log_chat(const char * const login, const char * const other, + const gchar * const msg, chat_log_direction_t direction, GTimeVal *tv_stamp); void log_debug(const char * const msg, ...) @@ -249,8 +252,75 @@ groupchat_log_init(void) } void -chat_log_chat(const gchar * const login, gchar *other, - const gchar * const msg, chat_log_direction_t direction, GTimeVal *tv_stamp) +chat_log_msg_out(const char * const barejid, const char * const msg) +{ + if (prefs_get_boolean(PREF_CHLOG)) { + const char *jid = jabber_get_fulljid(); + Jid *jidp = jid_create(jid); + _chat_log_chat(jidp->barejid, barejid, msg, PROF_OUT_LOG, NULL); + jid_destroy(jidp); + } +} + +void +chat_log_otr_msg_out(const char * const barejid, const char * const msg) +{ + if (prefs_get_boolean(PREF_CHLOG)) { + const char *jid = jabber_get_fulljid(); + Jid *jidp = jid_create(jid); + char *pref_otr_log = prefs_get_string(PREF_OTR_LOG); + if (strcmp(pref_otr_log, "on") == 0) { + _chat_log_chat(jidp->barejid, barejid, msg, PROF_OUT_LOG, NULL); + } else if (strcmp(pref_otr_log, "redact") == 0) { + _chat_log_chat(jidp->barejid, barejid, "[redacted]", PROF_OUT_LOG, NULL); + } + prefs_free_string(pref_otr_log); + jid_destroy(jidp); + } +} + +void +chat_log_otr_msg_in(const char * const barejid, const char * const msg, gboolean was_decrypted) +{ + if (prefs_get_boolean(PREF_CHLOG)) { + const char *jid = jabber_get_fulljid(); + Jid *jidp = jid_create(jid); + char *pref_otr_log = prefs_get_string(PREF_OTR_LOG); + if (!was_decrypted || (strcmp(pref_otr_log, "on") == 0)) { + _chat_log_chat(jidp->barejid, barejid, msg, PROF_IN_LOG, NULL); + } else if (strcmp(pref_otr_log, "redact") == 0) { + _chat_log_chat(jidp->barejid, barejid, "[redacted]", PROF_IN_LOG, NULL); + } + prefs_free_string(pref_otr_log); + jid_destroy(jidp); + } +} + +void +chat_log_msg_in(const char * const barejid, const char * const msg) +{ + if (prefs_get_boolean(PREF_CHLOG)) { + const char *jid = jabber_get_fulljid(); + Jid *jidp = jid_create(jid); + _chat_log_chat(jidp->barejid, barejid, msg, PROF_IN_LOG, NULL); + jid_destroy(jidp); + } +} + +void +chat_log_msg_in_delayed(const char * const barejid, const char * msg, GTimeVal *tv_stamp) +{ + if (prefs_get_boolean(PREF_CHLOG)) { + const char *jid = jabber_get_fulljid(); + Jid *jidp = jid_create(jid); + _chat_log_chat(jidp->barejid, barejid, msg, PROF_IN_LOG, tv_stamp); + jid_destroy(jidp); + } +} + +static void +_chat_log_chat(const char * const login, const char * const other, + const char * const msg, chat_log_direction_t direction, GTimeVal *tv_stamp) { struct dated_chat_log *dated_log = g_hash_table_lookup(logs, other); @@ -402,7 +472,7 @@ chat_log_close(void) } static struct dated_chat_log * -_create_log(char *other, const char * const login) +_create_log(const char * const other, const char * const login) { GDateTime *now = g_date_time_new_now_local(); char *filename = _get_log_filename(other, login, now, TRUE); diff --git a/src/log.h b/src/log.h index fdfd6caa..0689e2e6 100644 --- a/src/log.h +++ b/src/log.h @@ -64,8 +64,14 @@ void log_msg(log_level_t level, const char * const area, log_level_t log_level_from_string(char *log_level); void chat_log_init(void); -void chat_log_chat(const gchar * const login, gchar *other, - const gchar * const msg, chat_log_direction_t direction, GTimeVal *tv_stamp); + +void chat_log_msg_out(const char * const barejid, const char * const msg); +void chat_log_otr_msg_out(const char * const barejid, const char * const msg); + +void chat_log_msg_in(const char * const barejid, const char * const msg); +void chat_log_msg_in_delayed(const char * const barejid, const char * msg, GTimeVal *tv_stamp); +void chat_log_otr_msg_in(const char * const barejid, const char * const msg, gboolean was_decrypted); + void chat_log_close(void); GSList * chat_log_get_previous(const gchar * const login, const gchar * const recipient); diff --git a/src/server_events.c b/src/server_events.c index cf6ecb75..a97e0786 100644 --- a/src/server_events.c +++ b/src/server_events.c @@ -355,32 +355,11 @@ handle_incoming_message(char *barejid, char *resource, char *message) } ui_incoming_msg(barejid, resource, newmessage, NULL); - - if (prefs_get_boolean(PREF_CHLOG)) { - const char *jid = jabber_get_fulljid(); - Jid *jidp = jid_create(jid); - - char *pref_otr_log = prefs_get_string(PREF_OTR_LOG); - if (!was_decrypted || (strcmp(pref_otr_log, "on") == 0)) { - chat_log_chat(jidp->barejid, barejid, newmessage, PROF_IN_LOG, NULL); - } else if (strcmp(pref_otr_log, "redact") == 0) { - chat_log_chat(jidp->barejid, barejid, "[redacted]", PROF_IN_LOG, NULL); - } - prefs_free_string(pref_otr_log); - - jid_destroy(jidp); - } - + chat_log_otr_msg_in(barejid, newmessage, was_decrypted); otr_free_message(newmessage); #else ui_incoming_msg(barejid, resource, message, NULL); - - if (prefs_get_boolean(PREF_CHLOG)) { - const char *jid = jabber_get_fulljid(); - Jid *jidp = jid_create(jid); - chat_log_chat(jidp->barejid, barejid, message, PROF_IN_LOG, NULL); - jid_destroy(jidp); - } + chat_log_msg_in(barejid, message); #endif } @@ -394,13 +373,7 @@ void handle_delayed_message(char *barejid, char *message, GTimeVal tv_stamp) { ui_incoming_msg(barejid, NULL, message, &tv_stamp); - - if (prefs_get_boolean(PREF_CHLOG)) { - const char *jid = jabber_get_fulljid(); - Jid *jidp = jid_create(jid); - chat_log_chat(jidp->barejid, barejid, message, PROF_IN_LOG, &tv_stamp); - jid_destroy(jidp); - } + chat_log_msg_in_delayed(barejid, message, &tv_stamp); } void diff --git a/tests/log/stub_log.c b/tests/log/stub_log.c index 8ace164a..f88871a7 100644 --- a/tests/log/stub_log.c +++ b/tests/log/stub_log.c @@ -50,8 +50,14 @@ log_level_t log_level_from_string(char *log_level) } void chat_log_init(void) {} -void chat_log_chat(const gchar * const login, gchar *other, - const gchar * const msg, chat_log_direction_t direction, GTimeVal *tv_stamp) {} + +void chat_log_msg_out(const char * const barejid, const char * const msg) {} +void chat_log_otr_msg_out(const char * const barejid, const char * const msg) {} + +void chat_log_msg_in(const char * const barejid, const char * const msg) {} +void chat_log_msg_in_delayed(const char * const barejid, const char * msg, GTimeVal *tv_stamp) {} +void chat_log_otr_msg_in(const char * const barejid, const char * const msg, gboolean was_decrypted) {} + void chat_log_close(void) {} GSList * chat_log_get_previous(const gchar * const login, const gchar * const recipient)