From ff9abecd509f3a1d1f857a736dda28da9e94ad69 Mon Sep 17 00:00:00 2001 From: James Booth Date: Tue, 27 Oct 2015 22:25:02 +0000 Subject: [PATCH] Combined chatwin OTR SMP functions --- src/otr/otr.c | 6 +- src/otr/otr.h | 15 ++++- src/otr/otrlibv3.c | 18 +++--- src/otr/otrlibv4.c | 16 ++--- src/ui/chatwin.c | 122 ++++++++++++----------------------- src/ui/ui.h | 16 +---- src/ui/window.c | 12 ++++ tests/unittests/ui/stub_ui.c | 14 +--- 8 files changed, 93 insertions(+), 126 deletions(-) diff --git a/src/otr/otr.c b/src/otr/otr.c index c016501c..facfb4f7 100644 --- a/src/otr/otr.c +++ b/src/otr/otr.c @@ -577,13 +577,13 @@ otr_smp_secret(const char *const recipient, const char *secret) if (g_hash_table_contains(smp_initiators, recipient)) { otrl_message_respond_smp(user_state, &ops, NULL, context, (const unsigned char*)secret, strlen(secret)); if (chatwin) { - chatwin_otr_smp_authenticating(chatwin); + chatwin_otr_smp_event(chatwin, PROF_OTR_SMP_AUTH, NULL); } g_hash_table_remove(smp_initiators, context->username); } else { otrl_message_initiate_smp(user_state, &ops, NULL, context, (const unsigned char*)secret, strlen(secret)); if (chatwin) { - chatwin_otr_smp_authenticaton_wait(chatwin); + chatwin_otr_smp_event(chatwin, PROF_OTR_SMP_AUTH_WAIT, NULL); } } } @@ -604,7 +604,7 @@ otr_smp_question(const char *const recipient, const char *question, const char * otrl_message_initiate_smp_q(user_state, &ops, NULL, context, question, (const unsigned char*)answer, strlen(answer)); ProfChatWin *chatwin = wins_get_chat(recipient); if (chatwin) { - chatwin_otr_smp_authenticaton_wait(chatwin); + chatwin_otr_smp_event(chatwin, PROF_OTR_SMP_AUTH_WAIT, NULL); } } diff --git a/src/otr/otr.h b/src/otr/otr.h index f966239e..284b69b9 100644 --- a/src/otr/otr.h +++ b/src/otr/otr.h @@ -39,7 +39,7 @@ #include #include "config/accounts.h" -#include "ui/ui.h" +#include "ui/win_types.h" typedef enum { PROF_OTRPOLICY_MANUAL, @@ -47,6 +47,19 @@ typedef enum { PROF_OTRPOLICY_ALWAYS } prof_otrpolicy_t; +typedef enum { + PROF_OTR_SMP_INIT, + PROF_OTR_SMP_INIT_Q, + PROF_OTR_SMP_SENDER_FAIL, + PROF_OTR_SMP_RECEIVER_FAIL, + PROF_OTR_SMP_ABORT, + PROF_OTR_SMP_SUCCESS, + PROF_OTR_SMP_SUCCESS_Q, + PROF_OTR_SMP_FAIL_Q, + PROF_OTR_SMP_AUTH, + PROF_OTR_SMP_AUTH_WAIT +} prof_otr_smp_event_t; + OtrlUserState otr_userstate(void); OtrlMessageAppOps* otr_messageops(void); GHashTable* otr_smpinitators(void); diff --git a/src/otr/otrlibv3.c b/src/otr/otrlibv3.c index 8fa89a26..8af2b896 100644 --- a/src/otr/otrlibv3.c +++ b/src/otr/otrlibv3.c @@ -143,7 +143,7 @@ otrlib_handle_tlvs(OtrlUserState user_state, OtrlMessageAppOps *ops, ConnContext } else { ProfChatWin *chatwin = wins_get_chat(context->username); if (chatwin) { - chatwin_otr_smp_init(chatwin); + chatwin_otr_smp_event(chatwin, PROF_OTR_SMP_INIT. NULL); } g_hash_table_insert(smp_initiators, strdup(context->username), strdup(context->username)); } @@ -158,7 +158,7 @@ otrlib_handle_tlvs(OtrlUserState user_state, OtrlMessageAppOps *ops, ConnContext char *question = (char *)tlv->data; char *eoq = memchr(question, '\0', tlv->len); if (eoq) { - chatwin_otr_smp_init_q(chatwin, question); + chatwin_otr_smp_event(chatwin, PROF_OTR_SMP_INIT_Q, question); } } } @@ -181,17 +181,17 @@ otrlib_handle_tlvs(OtrlUserState user_state, OtrlMessageAppOps *ops, ConnContext if (chatwin) { if (context->smstate->received_question == 0) { if (context->active_fingerprint->trust && (context->active_fingerprint->trust[0] != '\0')) { - chatwin_otr_smp_success(chatwin); + chatwin_otr_smp_event(chatwin, PROF_OTR_SMP_SUCCESS, NULL); chatwin_otr_trust(chatwin); } else { - chatwin_otr_smp_sender_failed(chatwin); + chatwin_otr_smp_event(chatwin, PROF_OTR_SMP_SENDER_FAIL, NULL); chatwin_otr_untrust(chatwin); } } else { if (context->smstate->sm_prog_state == OTRL_SMP_PROG_SUCCEEDED) { - chatwin_otr_smp_answer_success(chatwin); + chatwin_otr_smp_event(chatwin, PROF_OTR_SMP_SUCCESS_Q, NULL); } else { - chatwin_otr_smp_answer_failure(chatwin); + chatwin_otr_smp_event(chatwin, PROF_OTR_SMP_FAIL_Q, NULL); } } } @@ -206,10 +206,10 @@ otrlib_handle_tlvs(OtrlUserState user_state, OtrlMessageAppOps *ops, ConnContext ProfChatWin *chatwin = wins_get_chat(context->username); if (chatwin) { if (context->active_fingerprint->trust && (context->active_fingerprint->trust[0] != '\0')) { - chatwin_otr_smp_success(chatwin); + chatwin_otr_smp_event(chatwin, PROF_OTR_SMP_SUCCESS, NULL); chatwin_otr_trust(chatwin); } else { - chatwin_otr_smp_receiver_failed(chatwin); + chatwin_otr_smp_event(chatwin, PROF_OTR_SMP_RECEIVER_FAIL, NULL); chatwin_otr_untrust(chatwin); } } @@ -220,7 +220,7 @@ otrlib_handle_tlvs(OtrlUserState user_state, OtrlMessageAppOps *ops, ConnContext context->smstate->nextExpected = OTRL_SMP_EXPECT1; ProfChatWin *chatwin = wins_get_chat(context->username); if (chatwin) { - chatwin_otr_smp_aborted(chatwin); + chatwin_otr_smp_event(chatwin, PROF_OTR_SMP_ABORT, NULL); chatwin_otr_untrust(chatwin); } otr_untrust(context->username); diff --git a/src/otr/otrlibv4.c b/src/otr/otrlibv4.c index cdfef5dd..548d2232 100644 --- a/src/otr/otrlibv4.c +++ b/src/otr/otrlibv4.c @@ -183,24 +183,24 @@ cb_handle_smp_event(void *opdata, OtrlSMPEvent smp_event, { case OTRL_SMPEVENT_ASK_FOR_SECRET: if (chatwin) { - chatwin_otr_smp_init(chatwin); + chatwin_otr_smp_event(chatwin, PROF_OTR_SMP_INIT, NULL); } g_hash_table_insert(smp_initiators, strdup(context->username), strdup(context->username)); break; case OTRL_SMPEVENT_ASK_FOR_ANSWER: if (chatwin) { - chatwin_otr_smp_init_q(chatwin, question); + chatwin_otr_smp_event(chatwin, PROF_OTR_SMP_INIT_Q, question); } break; case OTRL_SMPEVENT_SUCCESS: if (chatwin) { if (context->smstate->received_question == 0) { - chatwin_otr_smp_success(chatwin); + chatwin_otr_smp_event(chatwin, PROF_OTR_SMP_SUCCESS, NULL); chatwin_otr_trust(chatwin); } else { - chatwin_otr_smp_answer_success(chatwin); + chatwin_otr_smp_event(chatwin, PROF_OTR_SMP_SUCCESS_Q, NULL); } } break; @@ -209,13 +209,13 @@ cb_handle_smp_event(void *opdata, OtrlSMPEvent smp_event, if (chatwin) { if (context->smstate->received_question == 0) { if (nextMsg == OTRL_SMP_EXPECT3) { - chatwin_otr_smp_sender_failed(chatwin); + chatwin_otr_smp_event(chatwin, PROF_OTR_SMP_SENDER_FAIL, NULL); } else if (nextMsg == OTRL_SMP_EXPECT4) { - chatwin_otr_smp_receiver_failed(chatwin); + chatwin_otr_smp_event(chatwin, PROF_OTR_SMP_RECEIVER_FAIL, NULL); } chatwin_otr_untrust(chatwin); } else { - chatwin_otr_smp_answer_failure(chatwin); + chatwin_otr_smp_event(chatwin, PROF_OTR_SMP_FAIL_Q, NULL); } } break; @@ -230,7 +230,7 @@ cb_handle_smp_event(void *opdata, OtrlSMPEvent smp_event, case OTRL_SMPEVENT_ABORT: if (chatwin) { - chatwin_otr_smp_aborted(chatwin); + chatwin_otr_smp_event(chatwin, PROF_OTR_SMP_ABORT, NULL); chatwin_otr_untrust(chatwin); } break; diff --git a/src/ui/chatwin.c b/src/ui/chatwin.c index 004a24bd..7921e559 100644 --- a/src/ui/chatwin.c +++ b/src/ui/chatwin.c @@ -102,89 +102,51 @@ chatwin_otr_unsecured(ProfChatWin *chatwin) } void -chatwin_otr_smp_init(ProfChatWin *chatwin) +chatwin_otr_smp_event(ProfChatWin *chatwin, prof_otr_smp_event_t event, void *data) { assert(chatwin != NULL); - win_vprint((ProfWin*)chatwin, '!', 0, NULL, 0, 0, "", - "%s wants to authenticate your identity, use '/otr secret '.", chatwin->barejid); -} - -void -chatwin_otr_smp_init_q(ProfChatWin *chatwin, const char *question) -{ - assert(chatwin != NULL); - - win_vprint((ProfWin*)chatwin, '!', 0, NULL, 0, 0, "", - "%s wants to authenticate your identity with the following question:", chatwin->barejid); - win_vprint((ProfWin*)chatwin, '!', 0, NULL, 0, 0, "", " %s", question); - win_print((ProfWin*)chatwin, '!', 0, NULL, 0, 0, "", "use '/otr answer '."); -} - -void -chatwin_otr_smp_sender_failed(ProfChatWin *chatwin) -{ - assert(chatwin != NULL); - - win_vprint((ProfWin*)chatwin, '!', 0, NULL, 0, 0, "", - "Authentication failed, the secret you entered does not match the secret entered by %s.", chatwin->barejid); -} - -void -chatwin_otr_smp_receiver_failed(ProfChatWin *chatwin) -{ - assert(chatwin != NULL); - - win_vprint((ProfWin*)chatwin, '!', 0, NULL, 0, 0, "", - "Authentication failed, the secret entered by %s does not match yours.", chatwin->barejid); -} - -void -chatwin_otr_smp_aborted(ProfChatWin *chatwin) -{ - assert(chatwin != NULL); - - win_print((ProfWin*)chatwin, '!', 0, NULL, 0, 0, "", "SMP session aborted."); -} - -void -chatwin_otr_smp_success(ProfChatWin *chatwin) -{ - assert(chatwin != NULL); - - win_print((ProfWin*)chatwin, '!', 0, NULL, 0, 0, "", "Authentication successful."); -} - -void -chatwin_otr_smp_answer_success(ProfChatWin *chatwin) -{ - assert(chatwin != NULL); - - win_vprint((ProfWin*)chatwin, '!', 0, NULL, 0, 0, "", "%s successfully authenticated you.", chatwin->barejid); -} - -void -chatwin_otr_smp_answer_failure(ProfChatWin *chatwin) -{ - assert(chatwin != NULL); - - win_vprint((ProfWin*)chatwin, '!', 0, NULL, 0, 0, "", "%s failed to authenticate you.", chatwin->barejid); -} - -void -chatwin_otr_smp_authenticating(ProfChatWin *chatwin) -{ - assert(chatwin != NULL); - - win_vprint((ProfWin*)chatwin, '!', 0, NULL, 0, 0, "", "Authenticating %s...", chatwin->barejid); -} - -void -chatwin_otr_smp_authenticaton_wait(ProfChatWin *chatwin) -{ - assert(chatwin != NULL); - - win_vprint((ProfWin*)chatwin, '!', 0, NULL, 0, 0, "", "Awaiting authentication from %s...", chatwin->barejid); + switch (event) { + case PROF_OTR_SMP_INIT: + win_vprintln_ch((ProfWin*)chatwin, '!', + "%s wants to authenticate your identity, use '/otr secret '.", chatwin->barejid); + break; + case PROF_OTR_SMP_INIT_Q: + win_vprintln_ch((ProfWin*)chatwin, '!', + "%s wants to authenticate your identity with the following question:", chatwin->barejid); + win_vprintln_ch((ProfWin*)chatwin, '!', " %s", (char*)data); + win_vprintln_ch((ProfWin*)chatwin, '!', "use '/otr answer '."); + break; + case PROF_OTR_SMP_SENDER_FAIL: + win_vprintln_ch((ProfWin*)chatwin, '!', + "Authentication failed, the secret you entered does not match the secret entered by %s.", + chatwin->barejid); + break; + case PROF_OTR_SMP_RECEIVER_FAIL: + win_vprintln_ch((ProfWin*)chatwin, '!', + "Authentication failed, the secret entered by %s does not match yours.", chatwin->barejid); + break; + case PROF_OTR_SMP_ABORT: + win_vprintln_ch((ProfWin*)chatwin, '!', "SMP session aborted."); + break; + case PROF_OTR_SMP_SUCCESS: + win_vprintln_ch((ProfWin*)chatwin, '!', "Authentication successful."); + break; + case PROF_OTR_SMP_SUCCESS_Q: + win_vprintln_ch((ProfWin*)chatwin, '!', "%s successfully authenticated you.", chatwin->barejid); + break; + case PROF_OTR_SMP_FAIL_Q: + win_vprintln_ch((ProfWin*)chatwin, '!', "%s failed to authenticate you.", chatwin->barejid); + break; + case PROF_OTR_SMP_AUTH: + win_vprintln_ch((ProfWin*)chatwin, '!', "Authenticating %s...", chatwin->barejid); + break; + case PROF_OTR_SMP_AUTH_WAIT: + win_vprintln_ch((ProfWin*)chatwin, '!', "Awaiting authentication from %s...", chatwin->barejid); + break; + default: + break; + } } void diff --git a/src/ui/ui.h b/src/ui/ui.h index eed77ba7..5e634438 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -38,6 +38,7 @@ #include "command/commands.h" #include "ui/win_types.h" #include "muc.h" +#include "otr/otr.h" #define NO_ME 1 #define NO_DATE 2 @@ -66,19 +67,7 @@ void chatwin_otr_secured(ProfChatWin *chatwin, gboolean trusted); void chatwin_otr_unsecured(ProfChatWin *chatwin); void chatwin_otr_trust(ProfChatWin *chatwin); void chatwin_otr_untrust(ProfChatWin *chatwin); -void chatwin_otr_smp_init(ProfChatWin *chatwin); -void chatwin_otr_smp_init_q(ProfChatWin *chatwin, const char *question); - -void chatwin_otr_smp_success(ProfChatWin *chatwin); -void chatwin_otr_smp_sender_failed(ProfChatWin *chatwin); -void chatwin_otr_smp_receiver_failed(ProfChatWin *chatwin); -void chatwin_otr_smp_aborted(ProfChatWin *chatwin); - -void chatwin_otr_smp_answer_success(ProfChatWin *chatwin); -void chatwin_otr_smp_answer_failure(ProfChatWin *chatwin); - -void chatwin_otr_smp_authenticating(ProfChatWin *chatwin); -void chatwin_otr_smp_authenticaton_wait(ProfChatWin *chatwin); +void chatwin_otr_smp_event(ProfChatWin *chatwin, prof_otr_smp_event_t event, void *data); void ui_handle_otr_error(const char *const barejid, const char *const message); @@ -372,6 +361,7 @@ void win_show_occupant_info(ProfWin *window, const char *const room, Occupant *o void win_show_contact(ProfWin *window, PContact contact); void win_show_info(ProfWin *window, PContact contact); void win_println(ProfWin *window, int pad, const char *const message); +void win_vprintln_ch(ProfWin *window, char ch, const char *const message, ...); // desktop notifier actions void notifier_initialise(void); diff --git a/src/ui/window.c b/src/ui/window.c index 2b3a9c54..24baa63a 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -963,6 +963,18 @@ win_println(ProfWin *window, int pad, const char *const message) win_print(window, '-', pad, NULL, 0, 0, "", message); } +void +win_vprintln_ch(ProfWin *window, char ch, const char *const message, ...) +{ + va_list arg; + va_start(arg, message); + GString *fmt_msg = g_string_new(NULL); + g_string_vprintf(fmt_msg, message, arg); + win_print(window, ch, 0, NULL, 0, 0, "", fmt_msg->str); + g_string_free(fmt_msg, TRUE); + va_end(arg); +} + void win_newline(ProfWin *window) { diff --git a/tests/unittests/ui/stub_ui.c b/tests/unittests/ui/stub_ui.c index de36676a..14911282 100644 --- a/tests/unittests/ui/stub_ui.c +++ b/tests/unittests/ui/stub_ui.c @@ -70,19 +70,8 @@ void chatwin_otr_secured(ProfChatWin *chatwin, gboolean trusted) {} void chatwin_otr_unsecured(ProfChatWin *chatwin) {} void chatwin_otr_trust(ProfChatWin *chatwin) {} void chatwin_otr_untrust(ProfChatWin *chatwin) {} -void chatwin_otr_smp_init(ProfChatWin *chatwin) {} -void chatwin_otr_smp_init_q(ProfChatWin *chatwin, const char *question) {} +void chatwin_otr_smp_event(ProfChatWin *chatwin, prof_otr_smp_event_t event, void *data) {} -void chatwin_otr_smp_success(ProfChatWin *chatwin) {} -void chatwin_otr_smp_sender_failed(ProfChatWin *chatwin) {} -void chatwin_otr_smp_receiver_failed(ProfChatWin *chatwin) {} -void chatwin_otr_smp_aborted(ProfChatWin *chatwin) {} - -void chatwin_otr_smp_answer_success(ProfChatWin *chatwin) {} -void chatwin_otr_smp_answer_failure(ProfChatWin *chatwin) {} - -void chatwin_otr_smp_authenticating(ProfChatWin *chatwin) {} -void chatwin_otr_smp_authenticaton_wait(ProfChatWin *chatwin) {} void ui_sigwinch_handler(int sig) {} unsigned long ui_get_idle_time(void) @@ -552,6 +541,7 @@ void win_show_occupant_info(ProfWin *window, const char * const room, Occupant * void win_show_contact(ProfWin *window, PContact contact) {} void win_show_info(ProfWin *window, PContact contact) {} void win_println(ProfWin *window, int pad, const char * const message) {} +void win_vprintln_ch(ProfWin *window, char ch, const char *const message, ...) {} // desktop notifier actions void notifier_uninit(void) {}