diff options
author | James Booth <boothj5@gmail.com> | 2014-12-10 22:23:33 +0000 |
---|---|---|
committer | James Booth <boothj5@gmail.com> | 2014-12-10 22:23:33 +0000 |
commit | 9ba5a576bab1c56713fc9683c0adc8cdfaab5e50 (patch) | |
tree | fcd7d42b71d62223ae3a83de8deb30121530b5e0 | |
parent | c8ae55a88c0d013eabb684395e5293c608de5574 (diff) | |
download | profani-tty-9ba5a576bab1c56713fc9683c0adc8cdfaab5e50.tar.gz |
Split private message handling to separate functions
-rw-r--r-- | src/server_events.c | 91 | ||||
-rw-r--r-- | src/server_events.h | 7 | ||||
-rw-r--r-- | src/ui/core.c | 102 | ||||
-rw-r--r-- | src/ui/ui.h | 5 | ||||
-rw-r--r-- | src/xmpp/message.c | 8 |
5 files changed, 136 insertions, 77 deletions
diff --git a/src/server_events.c b/src/server_events.c index 335ecf2d..be0b7d5e 100644 --- a/src/server_events.c +++ b/src/server_events.c @@ -292,96 +292,97 @@ handle_room_message(const char * const room_jid, const char * const nick, } void -handle_incoming_message(char *from, char *message, gboolean priv) +handle_incoming_private_message(char *fulljid, char *message) +{ + ui_incoming_private_msg(fulljid, message, NULL); +} + +void +handle_incoming_message(char *barejid, char *message) { #ifdef HAVE_LIBOTR gboolean was_decrypted = FALSE; char *newmessage; - prof_otrpolicy_t policy = otr_get_policy(from); + prof_otrpolicy_t policy = otr_get_policy(barejid); char *whitespace_base = strstr(message,OTRL_MESSAGE_TAG_BASE); - if (!priv) { - //check for OTR whitespace (opportunistic or always) - if (policy == PROF_OTRPOLICY_OPPORTUNISTIC || policy == PROF_OTRPOLICY_ALWAYS) { - if (whitespace_base) { - if (strstr(message, OTRL_MESSAGE_TAG_V2) || strstr(message, OTRL_MESSAGE_TAG_V1)) { - // Remove whitespace pattern for proper display in UI - // Handle both BASE+TAGV1/2(16+8) and BASE+TAGV1+TAGV2(16+8+8) - int tag_length = 24; - if (strstr(message, OTRL_MESSAGE_TAG_V2) && strstr(message, OTRL_MESSAGE_TAG_V1)) { - tag_length = 32; - } - 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..."); - message_send(otr_query_message, from); + //check for OTR whitespace (opportunistic or always) + if (policy == PROF_OTRPOLICY_OPPORTUNISTIC || policy == PROF_OTRPOLICY_ALWAYS) { + if (whitespace_base) { + if (strstr(message, OTRL_MESSAGE_TAG_V2) || strstr(message, OTRL_MESSAGE_TAG_V1)) { + // Remove whitespace pattern for proper display in UI + // Handle both BASE+TAGV1/2(16+8) and BASE+TAGV1+TAGV2(16+8+8) + int tag_length = 24; + if (strstr(message, OTRL_MESSAGE_TAG_V2) && strstr(message, OTRL_MESSAGE_TAG_V1)) { + tag_length = 32; } + 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..."); + message_send(otr_query_message, barejid); } } - newmessage = otr_decrypt_message(from, message, &was_decrypted); + } + newmessage = otr_decrypt_message(barejid, message, &was_decrypted); - // internal OTR message - if (newmessage == NULL) { - return; - } - } else { - newmessage = message; + // internal OTR message + if (newmessage == NULL) { + return; } + if (policy == PROF_OTRPOLICY_ALWAYS && !was_decrypted && !whitespace_base) { char *otr_query_message = otr_start_query(); cons_show("Attempting to start OTR session..."); - message_send(otr_query_message, from); + message_send(otr_query_message, barejid); } - ui_incoming_msg(from, newmessage, NULL, priv); + ui_incoming_msg(barejid, newmessage, NULL); - if (prefs_get_boolean(PREF_CHLOG) && !priv) { - Jid *from_jid = jid_create(from); + 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, from_jid->barejid, newmessage, PROF_IN_LOG, NULL); + chat_log_chat(jidp->barejid, barejid, newmessage, PROF_IN_LOG, NULL); } else if (strcmp(pref_otr_log, "redact") == 0) { - chat_log_chat(jidp->barejid, from_jid->barejid, "[redacted]", PROF_IN_LOG, NULL); + chat_log_chat(jidp->barejid, barejid, "[redacted]", PROF_IN_LOG, NULL); } prefs_free_string(pref_otr_log); jid_destroy(jidp); - jid_destroy(from_jid); } - if (!priv) - otr_free_message(newmessage); + otr_free_message(newmessage); #else - ui_incoming_msg(from, message, NULL, priv); + ui_incoming_msg(barejid, message, NULL); - if (prefs_get_boolean(PREF_CHLOG) && !priv) { - Jid *from_jid = jid_create(from); + if (prefs_get_boolean(PREF_CHLOG)) { const char *jid = jabber_get_fulljid(); Jid *jidp = jid_create(jid); - chat_log_chat(jidp->barejid, from_jid->barejid, message, PROF_IN_LOG, NULL); + chat_log_chat(jidp->barejid, barejid, message, PROF_IN_LOG, NULL); jid_destroy(jidp); - jid_destroy(from_jid); } #endif } void -handle_delayed_message(char *from, char *message, GTimeVal tv_stamp, - gboolean priv) +handle_delayed_private_message(char *fulljid, char *message, GTimeVal tv_stamp) +{ + ui_incoming_private_msg(fulljid, message, &tv_stamp); +} + +void +handle_delayed_message(char *barejid, char *message, GTimeVal tv_stamp) { - ui_incoming_msg(from, message, &tv_stamp, priv); + ui_incoming_msg(barejid, message, &tv_stamp); - if (prefs_get_boolean(PREF_CHLOG) && !priv) { - Jid *from_jid = jid_create(from); + if (prefs_get_boolean(PREF_CHLOG)) { const char *jid = jabber_get_fulljid(); Jid *jidp = jid_create(jid); - chat_log_chat(jidp->barejid, from_jid->barejid, message, PROF_IN_LOG, &tv_stamp); + chat_log_chat(jidp->barejid, barejid, message, PROF_IN_LOG, &tv_stamp); jid_destroy(jidp); - jid_destroy(from_jid); } } diff --git a/src/server_events.h b/src/server_events.h index e750ecf1..32ac62f3 100644 --- a/src/server_events.h +++ b/src/server_events.h @@ -69,9 +69,10 @@ void handle_room_role_list(const char * const from, const char * const role, GSL void handle_room_role_set_error(const char * const room, const char * const nick, const char * const role, const char * const error); void handle_room_kick_result_error(const char * const room, const char * const nick, const char * const error); -void handle_incoming_message(char *from, char *message, gboolean priv); -void handle_delayed_message(char *from, char *message, GTimeVal tv_stamp, - gboolean priv); +void handle_incoming_message(char *barejid, char *message); +void handle_incoming_private_message(char *fulljid, char *message); +void handle_delayed_message(char *fulljid, char *message, GTimeVal tv_stamp); +void handle_delayed_private_message(char *fulljid, char *message, GTimeVal tv_stamp); void handle_typing(char *from); void handle_gone(const char * const from); void handle_subscription(const char *from, jabber_subscr_t type); diff --git a/src/ui/core.c b/src/ui/core.c index 2838edf3..234d8f8e 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -315,35 +315,27 @@ _ui_get_recipients(void) } static void -_ui_incoming_msg(const char * const from, const char * const message, - GTimeVal *tv_stamp, gboolean priv) +_ui_incoming_msg(const char * const barejid, const char * const message, GTimeVal *tv_stamp) { gboolean win_created = FALSE; char *display_from = NULL; - win_type_t win_type; - if (priv) { - win_type = WIN_PRIVATE; - display_from = get_nick_from_full_jid(from); - } else { - win_type = WIN_CHAT; - PContact contact = roster_get_contact(from); - if (contact != NULL) { - if (p_contact_name(contact) != NULL) { - display_from = strdup(p_contact_name(contact)); - } else { - display_from = strdup(from); - } + PContact contact = roster_get_contact(barejid); + if (contact != NULL) { + if (p_contact_name(contact) != NULL) { + display_from = strdup(p_contact_name(contact)); } else { - display_from = strdup(from); + display_from = strdup(barejid); } + } else { + display_from = strdup(barejid); } - ProfWin *window = wins_get_by_recipient(from); + ProfWin *window = wins_get_by_recipient(barejid); if (window == NULL) { - window = wins_new(from, win_type); + window = wins_new(barejid, WIN_CHAT); #ifdef HAVE_LIBOTR - if (win_type == WIN_CHAT && otr_is_secure(from)) { + if (otr_is_secure(barejid)) { window->wins.chat.is_otr = TRUE; } #endif @@ -362,17 +354,19 @@ _ui_incoming_msg(const char * const from, const char * const message, } else { status_bar_new(num); cons_show_incoming_message(display_from, num); - if (prefs_get_boolean(PREF_FLASH)) + + if (prefs_get_boolean(PREF_FLASH)) { flash(); + } window->unread++; if (prefs_get_boolean(PREF_CHLOG) && prefs_get_boolean(PREF_HISTORY)) { - _win_show_history(num, from); + _win_show_history(num, barejid); } // show users status first, when receiving message via delayed delivery if ((tv_stamp != NULL) && (win_created)) { - PContact pcontact = roster_get_contact(from); + PContact pcontact = roster_get_contact(barejid); if (pcontact != NULL) { win_show_contact(window, pcontact); } @@ -386,8 +380,69 @@ _ui_incoming_msg(const char * const from, const char * const message, ui_index = 0; } - if (prefs_get_boolean(PREF_BEEP)) + if (prefs_get_boolean(PREF_BEEP)) { + beep(); + } + + if (prefs_get_boolean(PREF_NOTIFY_MESSAGE)) { + gboolean is_current = wins_is_current(window); + if ( !is_current || (is_current && prefs_get_boolean(PREF_NOTIFY_MESSAGE_CURRENT)) ) { + if (prefs_get_boolean(PREF_NOTIFY_MESSAGE_TEXT)) { + notify_message(display_from, ui_index, message); + } else { + notify_message(display_from, ui_index, NULL); + } + } + } + + free(display_from); +} + +static void +_ui_incoming_private_msg(const char * const fulljid, const char * const message, GTimeVal *tv_stamp) +{ + char *display_from = NULL; + display_from = get_nick_from_full_jid(fulljid); + + ProfWin *window = wins_get_by_recipient(fulljid); + if (window == NULL) { + window = wins_new(fulljid, WIN_PRIVATE); + } + + int num = wins_get_num(window); + + // currently viewing chat window with sender + if (wins_is_current(window)) { + win_print_incoming_message(window, tv_stamp, display_from, message); + title_bar_set_typing(FALSE); + status_bar_active(num); + + // not currently viewing chat window with sender + } else { + status_bar_new(num); + cons_show_incoming_message(display_from, num); + + if (prefs_get_boolean(PREF_FLASH)) { + flash(); + } + + window->unread++; + if (prefs_get_boolean(PREF_CHLOG) && prefs_get_boolean(PREF_HISTORY)) { + _win_show_history(num, fulljid); + } + + win_print_incoming_message(window, tv_stamp, display_from, message); + } + + int ui_index = num; + if (ui_index == 10) { + ui_index = 0; + } + + if (prefs_get_boolean(PREF_BEEP)) { beep(); + } + if (prefs_get_boolean(PREF_NOTIFY_MESSAGE)) { gboolean is_current = wins_is_current(window); if ( !is_current || (is_current && prefs_get_boolean(PREF_NOTIFY_MESSAGE_CURRENT)) ) { @@ -3355,6 +3410,7 @@ ui_init_module(void) ui_contact_typing = _ui_contact_typing; ui_get_recipients = _ui_get_recipients; ui_incoming_msg = _ui_incoming_msg; + ui_incoming_private_msg = _ui_incoming_private_msg; ui_roster_add = _ui_roster_add; ui_roster_remove = _ui_roster_remove; ui_contact_already_in_group = _ui_contact_already_in_group; diff --git a/src/ui/ui.h b/src/ui/ui.h index 265806a7..b1535def 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -120,8 +120,9 @@ void (*ui_handle_stanza)(const char * const msg); // ui events void (*ui_contact_typing)(const char * const from); -void (*ui_incoming_msg)(const char * const from, const char * const message, - GTimeVal *tv_stamp, gboolean priv); +void (*ui_incoming_msg)(const char * const from, const char * const message, GTimeVal *tv_stamp); +void (*ui_incoming_private_msg)(const char * const fulljid, const char * const message, GTimeVal *tv_stamp); + void (*ui_disconnected)(void); void (*ui_recipient_gone)(const char * const barejid); void (*ui_outgoing_msg)(const char * const from, const char * const to, diff --git a/src/xmpp/message.c b/src/xmpp/message.c index b0b17214..a2dadb9f 100644 --- a/src/xmpp/message.c +++ b/src/xmpp/message.c @@ -438,9 +438,9 @@ _chat_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, char *message = xmpp_stanza_get_text(body); if (message != NULL) { if (delayed) { - handle_delayed_message(jid->str, message, tv_stamp, TRUE); + handle_delayed_private_message(jid->str, message, tv_stamp); } else { - handle_incoming_message(jid->str, message, TRUE); + handle_incoming_private_message(jid->str, message); } xmpp_free(ctx, message); } @@ -489,9 +489,9 @@ _chat_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, char *message = xmpp_stanza_get_text(body); if (message != NULL) { if (delayed) { - handle_delayed_message(jid->barejid, message, tv_stamp, FALSE); + handle_delayed_message(jid->barejid, message, tv_stamp); } else { - handle_incoming_message(jid->barejid, message, FALSE); + handle_incoming_message(jid->barejid, message); } xmpp_free(ctx, message); } |