From 1b25aa84cb17fd52d1983701553de27e38ebf3eb Mon Sep 17 00:00:00 2001 From: James Booth Date: Thu, 19 Jan 2017 22:33:29 +0000 Subject: [PATCH] Add titlebar encryption text to plugins api --- apidocs/c/profapi.h | 16 ++++++++++++ apidocs/python/src/prof.py | 32 +++++++++++++++++++++++ src/plugins/api.c | 38 ++++++++++++++++++++++++++++ src/plugins/api.h | 3 +++ src/plugins/c_api.c | 14 +++++++++++ src/plugins/profapi.c | 4 +++ src/plugins/profapi.h | 3 +++ src/plugins/python_api.c | 49 ++++++++++++++++++++++++++++++++++++ src/ui/chatwin.c | 18 +++++++++++++ src/ui/titlebar.c | 48 ++++++++++++++++++++++++----------- src/ui/ui.h | 2 ++ src/ui/win_types.h | 1 + src/ui/window.c | 2 ++ tests/unittests/ui/stub_ui.c | 3 +++ 14 files changed, 219 insertions(+), 14 deletions(-) diff --git a/apidocs/c/profapi.h b/apidocs/c/profapi.h index 7d5cad0e..9ef1a795 100644 --- a/apidocs/c/profapi.h +++ b/apidocs/c/profapi.h @@ -339,3 +339,19 @@ End any encrypted session with the specified user. @param barejid Jabber ID of the recipient */ void prof_encryption_reset(char *barejid); + +/** +Set the text to display in the titlebar encryption indicator. +@param barejid Jabber ID of the recipient +@param enctext The text to display +@return 1 on success, 0 on failure +*/ +int prof_chat_set_titlebar_enctext(char *barejid, char *enctext); + +/** +Let profanity decide what to show in the titlebar encryption indicator +@param barejid Jabber ID of the recipient +@return 1 on success, 0 on failure +*/ +int prof_chat_unset_titlebar_enctext(char *barejid); + diff --git a/apidocs/python/src/prof.py b/apidocs/python/src/prof.py index 22488f4b..64468eb0 100644 --- a/apidocs/python/src/prof.py +++ b/apidocs/python/src/prof.py @@ -630,3 +630,35 @@ def encryption_reset(barejid): :: prof.encryption_reset("alice@server.org") """ + + +def chat_set_titlebar_enctext(barejid, enctext): + """Set the text to display in the titlebar encryption indicator. + + :param barejid: Jabber ID of the recipient + :param enctext: The text to display + :type barejid: str or unicode + :type enctext: str or unicode + :return: ``True`` if the text was set successfully, ``False`` otherwise + :rtype: boolean + + + Example: + :: + prof.chat_set_titlebar_enctext("bob@chat.org", "safe") + """ + + +def chat_unset_titlebar_enctext(barejid): + """Let profanity decide what to show in the titlebar encryption indicator. + + :param barejid: Jabber ID of the recipient + :type barejid: str or unicode + :return: ``True`` if the text was unset successfully, ``False`` otherwise + :rtype: boolean + + Example: + :: + prof.chat_unset_titlebar_enctext("bob@chat.org") + """ + diff --git a/src/plugins/api.c b/src/plugins/api.c index 3488d84c..e0b2afb1 100644 --- a/src/plugins/api.c +++ b/src/plugins/api.c @@ -523,3 +523,41 @@ api_encryption_reset(const char *const barejid) } #endif } + +int +api_chat_set_titlebar_enctext(const char *const barejid, const char *const enctext) +{ + if (enctext == NULL) { + return 0; + } + + if (barejid == NULL) { + return 0; + } + + ProfChatWin *chatwin = wins_get_chat(barejid); + if (chatwin == NULL) { + return 0; + } + + chatwin_set_enctext(chatwin, enctext); + + return 1; +} + +int +api_chat_unset_titlebar_enctext(const char *const barejid) +{ + if (barejid == NULL) { + return 0; + } + + ProfChatWin *chatwin = wins_get_chat(barejid); + if (chatwin == NULL) { + return 0; + } + + chatwin_unset_enctext(chatwin); + + return 1; +} diff --git a/src/plugins/api.h b/src/plugins/api.h index c05e7038..64784924 100644 --- a/src/plugins/api.h +++ b/src/plugins/api.h @@ -98,4 +98,7 @@ void api_disco_add_feature(char *plugin_name, char *feature); void api_encryption_reset(const char *const barejid); +int api_chat_set_titlebar_enctext(const char *const barejid, const char *const enctext); +int api_chat_unset_titlebar_enctext(const char *const barejid); + #endif diff --git a/src/plugins/c_api.c b/src/plugins/c_api.c index ed613389..3721c726 100644 --- a/src/plugins/c_api.c +++ b/src/plugins/c_api.c @@ -347,6 +347,18 @@ c_api_encryption_reset(const char *barejid) api_encryption_reset(barejid); } +static int +c_api_chat_set_titlebar_enctext(const char *barejid, const char *enctext) +{ + return api_chat_set_titlebar_enctext(barejid, enctext); +} + +static int +c_api_chat_unset_titlebar_enctext(const char *barejid) +{ + return api_chat_unset_titlebar_enctext(barejid); +} + void c_command_callback(PluginCommand *command, gchar **args) { @@ -415,6 +427,8 @@ c_api_init(void) prof_incoming_message = c_api_incoming_message; _prof_disco_add_feature = c_api_disco_add_feature; prof_encryption_reset = c_api_encryption_reset; + prof_chat_set_titlebar_enctext = c_api_chat_set_titlebar_enctext; + prof_chat_unset_titlebar_enctext = c_api_chat_unset_titlebar_enctext; } static char * diff --git a/src/plugins/profapi.c b/src/plugins/profapi.c index 8a6ef5aa..b9dd9e6f 100644 --- a/src/plugins/profapi.c +++ b/src/plugins/profapi.c @@ -94,3 +94,7 @@ void (*prof_incoming_message)(char *barejid, char *resource, char *message) = NU void (*_prof_disco_add_feature)(const char *filename, char *feature) = NULL; void (*prof_encryption_reset)(const char *barejid) = NULL; + +int (*prof_chat_set_titlebar_enctext)(const char *barejid, const char *enctext) = NULL; +int (*prof_chat_unset_titlebar_enctext)(const char *barejid) = NULL; + diff --git a/src/plugins/profapi.h b/src/plugins/profapi.h index 223af77f..291f8958 100644 --- a/src/plugins/profapi.h +++ b/src/plugins/profapi.h @@ -107,4 +107,7 @@ void (*_prof_disco_add_feature)(const char *filename, char *feature); void (*prof_encryption_reset)(const char *barejid); +int (*prof_chat_set_titlebar_enctext)(const char *barejid, const char *enctext); +int (*prof_chat_unset_titlebar_enctext)(const char *barejid); + #endif diff --git a/src/plugins/python_api.c b/src/plugins/python_api.c index b92cdc88..27b4adf8 100644 --- a/src/plugins/python_api.c +++ b/src/plugins/python_api.c @@ -1049,6 +1049,53 @@ python_api_encryption_reset(PyObject *self, PyObject *args) Py_RETURN_NONE; } +static PyObject* +python_api_chat_set_titlebar_enctext(PyObject *self, PyObject *args) +{ + PyObject *barejid = NULL; + PyObject *enctext = NULL; + if (!PyArg_ParseTuple(args, "OO", &barejid, &enctext)) { + Py_RETURN_NONE; + } + + char *barejid_str = python_str_or_unicode_to_string(barejid); + char *enctext_str = python_str_or_unicode_to_string(enctext); + + allow_python_threads(); + int res = api_chat_set_titlebar_enctext(barejid_str, enctext_str); + free(barejid_str); + free(enctext_str); + disable_python_threads(); + + if (res) { + return Py_BuildValue("O", Py_True); + } else { + return Py_BuildValue("O", Py_False); + } +} + +static PyObject* +python_api_chat_unset_titlebar_enctext(PyObject *self, PyObject *args) +{ + PyObject *barejid = NULL; + if (!PyArg_ParseTuple(args, "O", &barejid)) { + Py_RETURN_NONE; + } + + char *barejid_str = python_str_or_unicode_to_string(barejid); + + allow_python_threads(); + int res = api_chat_unset_titlebar_enctext(barejid_str); + free(barejid_str); + disable_python_threads(); + + if (res) { + return Py_BuildValue("O", Py_True); + } else { + return Py_BuildValue("O", Py_False); + } +} + void python_command_callback(PluginCommand *command, gchar **args) { @@ -1158,6 +1205,8 @@ static PyMethodDef apiMethods[] = { { "incoming_message", python_api_incoming_message, METH_VARARGS, "Show an incoming message." }, { "disco_add_feature", python_api_disco_add_feature, METH_VARARGS, "Add a feature to disco info response." }, { "encryption_reset", python_api_encryption_reset, METH_VARARGS, "End encrypted chat session with barejid, if one exists" }, + { "chat_set_titlebar_enctext", python_api_chat_set_titlebar_enctext, METH_VARARGS, "Set the encryption status in the title bar for the specified contact" }, + { "chat_unset_titlebar_enctext", python_api_chat_unset_titlebar_enctext, METH_VARARGS, "Reset the encryption status in the title bar for the specified recipient" }, { NULL, NULL, 0, NULL } }; diff --git a/src/ui/chatwin.c b/src/ui/chatwin.c index 8eee3dee..74acd79b 100644 --- a/src/ui/chatwin.c +++ b/src/ui/chatwin.c @@ -385,6 +385,24 @@ chatwin_get_string(ProfChatWin *chatwin) return resstr; } +void +chatwin_set_enctext(ProfChatWin *chatwin, const char *const enctext) +{ + if (chatwin->enctext) { + free(chatwin->enctext); + } + chatwin->enctext = strdup(enctext); +} + +void +chatwin_unset_enctext(ProfChatWin *chatwin) +{ + if (chatwin->enctext) { + free(chatwin->enctext); + chatwin->enctext = NULL; + } +} + static void _chatwin_history(ProfChatWin *chatwin, const char *const contact) { diff --git a/src/ui/titlebar.c b/src/ui/titlebar.c index 436b574c..39624a0a 100644 --- a/src/ui/titlebar.c +++ b/src/ui/titlebar.c @@ -319,6 +319,20 @@ _show_privacy(ProfChatWin *chatwin) int trusted_attrs = theme_attrs(THEME_TITLE_TRUSTED); int untrusted_attrs = theme_attrs(THEME_TITLE_UNTRUSTED); + if (chatwin->enctext) { + wprintw(win, " "); + wattron(win, bracket_attrs); + wprintw(win, "["); + wattroff(win, bracket_attrs); + wattron(win, encrypted_attrs); + wprintw(win, chatwin->enctext); + wattroff(win, encrypted_attrs); + wattron(win, bracket_attrs); + wprintw(win, "]"); + + return; + } + if (chatwin->is_otr) { wprintw(win, " "); wattron(win, bracket_attrs); @@ -353,7 +367,11 @@ _show_privacy(ProfChatWin *chatwin) wprintw(win, "]"); wattroff(win, bracket_attrs); } - } else if (chatwin->pgp_send || chatwin->pgp_recv) { + + return; + } + + if (chatwin->pgp_send || chatwin->pgp_recv) { GString *pgpmsg = g_string_new("PGP "); if (chatwin->pgp_send && !chatwin->pgp_recv) { g_string_append(pgpmsg, "send"); @@ -373,19 +391,21 @@ _show_privacy(ProfChatWin *chatwin) wprintw(win, "]"); wattroff(win, bracket_attrs); g_string_free(pgpmsg, TRUE); - } else { - if (prefs_get_boolean(PREF_ENC_WARN)) { - wprintw(win, " "); - wattron(win, bracket_attrs); - wprintw(win, "["); - wattroff(win, bracket_attrs); - wattron(win, unencrypted_attrs); - wprintw(win, "unencrypted"); - wattroff(win, unencrypted_attrs); - wattron(win, bracket_attrs); - wprintw(win, "]"); - wattroff(win, bracket_attrs); - } + + return; + } + + if (prefs_get_boolean(PREF_ENC_WARN)) { + wprintw(win, " "); + wattron(win, bracket_attrs); + wprintw(win, "["); + wattroff(win, bracket_attrs); + wattron(win, unencrypted_attrs); + wprintw(win, "unencrypted"); + wattroff(win, unencrypted_attrs); + wattron(win, bracket_attrs); + wprintw(win, "]"); + wattroff(win, bracket_attrs); } } diff --git a/src/ui/ui.h b/src/ui/ui.h index 44d87966..56f9f4a3 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -140,6 +140,8 @@ void chatwin_otr_trust(ProfChatWin *chatwin); void chatwin_otr_untrust(ProfChatWin *chatwin); void chatwin_otr_smp_event(ProfChatWin *chatwin, prof_otr_smp_event_t event, void *data); #endif +void chatwin_set_enctext(ProfChatWin *chatwin, const char *const enctext); +void chatwin_unset_enctext(ProfChatWin *chatwin); // MUC window void mucwin_role_change(ProfMucWin *mucwin, const char *const role, const char *const actor, const char *const reason); diff --git a/src/ui/win_types.h b/src/ui/win_types.h index 47bc7657..56e6d129 100644 --- a/src/ui/win_types.h +++ b/src/ui/win_types.h @@ -155,6 +155,7 @@ typedef struct prof_chat_win_t { char *resource_override; gboolean history_shown; unsigned long memcheck; + char *enctext; } ProfChatWin; typedef struct prof_muc_win_t { diff --git a/src/ui/window.c b/src/ui/window.c index a000f1aa..83172bbf 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -146,6 +146,7 @@ win_create_chat(const char *const barejid) new_win->history_shown = FALSE; new_win->unread = 0; new_win->state = chat_state_new(); + new_win->enctext = NULL; new_win->memcheck = PROFCHATWIN_MEMCHECK; @@ -430,6 +431,7 @@ win_free(ProfWin* window) ProfChatWin *chatwin = (ProfChatWin*)window; free(chatwin->barejid); free(chatwin->resource_override); + free(chatwin->enctext); chat_state_free(chatwin->state); break; } diff --git a/tests/unittests/ui/stub_ui.c b/tests/unittests/ui/stub_ui.c index e4f56c85..04c6300c 100644 --- a/tests/unittests/ui/stub_ui.c +++ b/tests/unittests/ui/stub_ui.c @@ -67,6 +67,9 @@ void chatwin_otr_untrust(ProfChatWin *chatwin) {} void chatwin_otr_smp_event(ProfChatWin *chatwin, prof_otr_smp_event_t event, void *data) {} #endif +void chatwin_set_enctext(ProfChatWin *chatwin, const char *const enctext) {} +void chatwin_unset_enctext(ProfChatWin *chatwin) {} + void ui_sigwinch_handler(int sig) {} unsigned long ui_get_idle_time(void)