From 7af0e9b1e12422b161847a9166dbdcc2a8d4e1c8 Mon Sep 17 00:00:00 2001 From: John Hernandez <129467592+H3rnand3zzz@users.noreply.github.com> Date: Fri, 3 Nov 2023 11:27:30 +0100 Subject: [PATCH] Show encryption for historical messages Refactor getting char, introduce get_show_char() to follow DRY principle. In theory, performance might be negatively affected after this change. Ideally get_show_char should be optimized in the future. --- src/ui/chatwin.c | 10 +--------- src/ui/mucwin.c | 18 ++---------------- src/ui/window.c | 43 ++++++++++++++++++++++++++++++------------- src/ui/window.h | 2 ++ 4 files changed, 35 insertions(+), 38 deletions(-) diff --git a/src/ui/chatwin.c b/src/ui/chatwin.c index 8f37ce13..14a74051 100644 --- a/src/ui/chatwin.c +++ b/src/ui/chatwin.c @@ -433,16 +433,8 @@ chatwin_outgoing_msg(ProfChatWin* chatwin, const char* const message, char* id, auto_char char* enc_char; if (chatwin->outgoing_char) { enc_char = chatwin->outgoing_char; - } else if (enc_mode == PROF_MSG_ENC_OTR) { - enc_char = prefs_get_otr_char(); - } else if (enc_mode == PROF_MSG_ENC_PGP) { - enc_char = prefs_get_pgp_char(); - } else if (enc_mode == PROF_MSG_ENC_OMEMO) { - enc_char = prefs_get_omemo_char(); - } else if (enc_mode == PROF_MSG_ENC_OX) { - enc_char = prefs_get_ox_char(); } else { - enc_char = strdup("-"); + enc_char = get_show_char(enc_mode); } if (request_receipt && id) { diff --git a/src/ui/mucwin.c b/src/ui/mucwin.c index fb46f433..65fcbce4 100644 --- a/src/ui/mucwin.c +++ b/src/ui/mucwin.c @@ -518,16 +518,8 @@ mucwin_outgoing_msg(ProfMucWin* mucwin, const char* const message, const char* c auto_char char* ch; if (mucwin->message_char) { ch = strdup(mucwin->message_char); - } else if (enc_mode == PROF_MSG_ENC_OTR) { - ch = prefs_get_otr_char(); - } else if (enc_mode == PROF_MSG_ENC_PGP) { - ch = prefs_get_pgp_char(); - } else if (enc_mode == PROF_MSG_ENC_OMEMO) { - ch = prefs_get_omemo_char(); - } else if (enc_mode == PROF_MSG_ENC_OX) { - ch = prefs_get_omemo_char(); } else { - ch = strdup("-"); + ch = get_show_char(enc_mode); } win_print_outgoing_muc_msg(window, ch, mynick, id, replace_id, message); @@ -566,14 +558,8 @@ mucwin_incoming_msg(ProfMucWin* mucwin, const ProfMessage* const message, GSList auto_char char* ch; if (mucwin->message_char) { ch = strdup(mucwin->message_char); - } else if (message->enc == PROF_MSG_ENC_OTR) { - ch = prefs_get_otr_char(); - } else if (message->enc == PROF_MSG_ENC_PGP) { - ch = prefs_get_pgp_char(); - } else if (message->enc == PROF_MSG_ENC_OMEMO) { - ch = prefs_get_omemo_char(); } else { - ch = strdup("-"); + ch = get_show_char(message->enc); } win_insert_last_read_position_marker((ProfWin*)mucwin, mucwin->roomjid); diff --git a/src/ui/window.c b/src/ui/window.c index 808c26bc..5685457d 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -1394,16 +1394,8 @@ win_print_incoming(ProfWin* window, const char* const display_name_from, ProfMes if (chatwin->incoming_char) { enc_char = strdup(chatwin->incoming_char); - } else if (message->enc == PROF_MSG_ENC_OTR) { - enc_char = prefs_get_otr_char(); - } else if (message->enc == PROF_MSG_ENC_PGP) { - enc_char = prefs_get_pgp_char(); - } else if (message->enc == PROF_MSG_ENC_OX) { // XEP-0373: OpenPGP for XMPP - enc_char = prefs_get_ox_char(); - } else if (message->enc == PROF_MSG_ENC_OMEMO) { - enc_char = prefs_get_omemo_char(); } else { - enc_char = strdup("-"); + enc_char = get_show_char(message->enc); } if (prefs_get_boolean(PREF_CORRECTION_ALLOW) && message->replace_id) { @@ -1492,10 +1484,12 @@ win_print_history(ProfWin* window, const ProfMessage* const message) flags = NO_ME; } - buffer_append(window->layout->buffer, "-", 0, message->timestamp, flags, THEME_TEXT_HISTORY, display_name, NULL, message->plain, NULL, NULL); + auto_char char* ch = get_show_char(message->enc); + + buffer_append(window->layout->buffer, ch, 0, message->timestamp, flags, THEME_TEXT_HISTORY, display_name, NULL, message->plain, NULL, NULL); wins_add_urls_ac(window, message, FALSE); wins_add_quotes_ac(window, message->plain, FALSE); - _win_print_internal(window, "-", 0, message->timestamp, flags, THEME_TEXT_HISTORY, display_name, message->plain, NULL); + _win_print_internal(window, ch, 0, message->timestamp, flags, THEME_TEXT_HISTORY, display_name, message->plain, NULL); inp_nonblocking(TRUE); g_date_time_unref(message->timestamp); @@ -1518,10 +1512,12 @@ win_print_old_history(ProfWin* window, const ProfMessage* const message) flags = NO_ME; } - buffer_prepend(window->layout->buffer, "-", 0, message->timestamp, flags, THEME_TEXT_HISTORY, display_name, NULL, message->plain, NULL, NULL); + auto_char char* ch = get_show_char(message->enc); + + buffer_prepend(window->layout->buffer, ch, 0, message->timestamp, flags, THEME_TEXT_HISTORY, display_name, NULL, message->plain, NULL, NULL); wins_add_urls_ac(window, message, TRUE); wins_add_quotes_ac(window, message->plain, TRUE); - _win_print_internal(window, "-", 0, message->timestamp, flags, THEME_TEXT_HISTORY, display_name, message->plain, NULL); + _win_print_internal(window, ch, 0, message->timestamp, flags, THEME_TEXT_HISTORY, display_name, message->plain, NULL); inp_nonblocking(TRUE); g_date_time_unref(message->timestamp); @@ -2239,3 +2235,24 @@ win_quote_autocomplete(ProfWin* window, const char* const input, gboolean previo return g_strdup_printf("> %s\n", quoted_result); } + +// Derive encryption char from encryption mode. Output needs to be freed by caller. +char* +get_show_char(prof_enc_t encryption_mode) +{ + char* enc_char; + + if (encryption_mode == PROF_MSG_ENC_OTR) { + enc_char = prefs_get_otr_char(); + } else if (encryption_mode == PROF_MSG_ENC_PGP) { + enc_char = prefs_get_pgp_char(); + } else if (encryption_mode == PROF_MSG_ENC_OMEMO) { + enc_char = prefs_get_omemo_char(); + } else if (encryption_mode == PROF_MSG_ENC_OX) { + enc_char = prefs_get_ox_char(); + } else { + enc_char = strdup("-"); + } + + return enc_char; +} diff --git a/src/ui/window.h b/src/ui/window.h index 1c0a9007..6218ddc5 100644 --- a/src/ui/window.h +++ b/src/ui/window.h @@ -98,4 +98,6 @@ void win_remove_entry_message(ProfWin* window, const char* const id); char* win_quote_autocomplete(ProfWin* window, const char* const input, gboolean previous); +char* get_show_char(prof_enc_t encryption_mode); + #endif