diff --git a/src/chat_session.c b/src/chat_session.c index 5aa37d4b..49a89a5d 100644 --- a/src/chat_session.c +++ b/src/chat_session.c @@ -38,9 +38,9 @@ #include #include "chat_session.h" - #include "config/preferences.h" #include "log.h" +#include "xmpp/xmpp.h" #define PAUSED_TIMOUT 10.0 #define INACTIVE_TIMOUT 30.0 @@ -65,6 +65,19 @@ typedef struct chat_session_t { static GHashTable *sessions; +static void _chat_session_new(const char * const recipient, gboolean recipient_supports); +static gboolean _chat_session_exists(const char * const recipient); +static void _chat_session_set_composing(const char * const recipient); +static void _chat_session_set_sent(const char * const recipient); +static gboolean _chat_session_get_sent(const char * const recipient); +static void _chat_session_end(const char * const recipient); +static gboolean _chat_session_is_inactive(const char * const recipient); +static void _chat_session_set_active(const char * const recipient); +static gboolean _chat_session_is_paused(const char * const recipient); +static gboolean _chat_session_is_gone(const char * const recipient); +static void _chat_session_set_gone(const char * const recipient); +static gboolean _chat_session_get_recipient_supports(const char * const recipient); +static void _chat_session_set_recipient_supports(const char * const recipient, gboolean recipient_supports); static void _chat_session_free(ChatSession *session); void @@ -81,8 +94,8 @@ chat_sessions_clear(void) g_hash_table_remove_all(sessions); } -void -chat_session_new(const char * const recipient, gboolean recipient_supports) +static void +_chat_session_new(const char * const recipient, gboolean recipient_supports) { ChatSession *new_session = malloc(sizeof(struct chat_session_t)); new_session->recipient = strdup(recipient); @@ -93,8 +106,8 @@ chat_session_new(const char * const recipient, gboolean recipient_supports) g_hash_table_insert(sessions, strdup(recipient), new_session); } -gboolean -chat_session_exists(const char * const recipient) +static gboolean +_chat_session_exists(const char * const recipient) { ChatSession *session = g_hash_table_lookup(sessions, recipient); @@ -105,8 +118,8 @@ chat_session_exists(const char * const recipient) } } -void -chat_session_set_composing(const char * const recipient) +static void +_chat_session_set_composing(const char * const recipient) { ChatSession *session = g_hash_table_lookup(sessions, recipient); @@ -119,40 +132,8 @@ chat_session_set_composing(const char * const recipient) } } -void -chat_session_no_activity(const char * const recipient) -{ - ChatSession *session = g_hash_table_lookup(sessions, recipient); - - if (session != NULL) { - if (session->active_timer != NULL) { - gdouble elapsed = g_timer_elapsed(session->active_timer, NULL); - - if ((prefs_get_gone() != 0) && (elapsed > (prefs_get_gone() * 60.0))) { - if (session->state != CHAT_STATE_GONE) { - session->sent = FALSE; - } - session->state = CHAT_STATE_GONE; - - } else if (elapsed > INACTIVE_TIMOUT) { - if (session->state != CHAT_STATE_INACTIVE) { - session->sent = FALSE; - } - session->state = CHAT_STATE_INACTIVE; - - } else if (elapsed > PAUSED_TIMOUT) { - - if (session->state == CHAT_STATE_COMPOSING) { - session->sent = FALSE; - session->state = CHAT_STATE_PAUSED; - } - } - } - } -} - -void -chat_session_set_sent(const char * const recipient) +static void +_chat_session_set_sent(const char * const recipient) { ChatSession *session = g_hash_table_lookup(sessions, recipient); @@ -161,8 +142,8 @@ chat_session_set_sent(const char * const recipient) } } -gboolean -chat_session_get_sent(const char * const recipient) +static gboolean +_chat_session_get_sent(const char * const recipient) { ChatSession *session = g_hash_table_lookup(sessions, recipient); @@ -173,14 +154,14 @@ chat_session_get_sent(const char * const recipient) } } -void -chat_session_end(const char * const recipient) +static void +_chat_session_end(const char * const recipient) { g_hash_table_remove(sessions, recipient); } -gboolean -chat_session_is_inactive(const char * const recipient) +static gboolean +_chat_session_is_inactive(const char * const recipient) { ChatSession *session = g_hash_table_lookup(sessions, recipient); @@ -191,8 +172,8 @@ chat_session_is_inactive(const char * const recipient) } } -void -chat_session_set_active(const char * const recipient) +static void +_chat_session_set_active(const char * const recipient) { ChatSession *session = g_hash_table_lookup(sessions, recipient); @@ -203,8 +184,8 @@ chat_session_set_active(const char * const recipient) } } -gboolean -chat_session_is_paused(const char * const recipient) +static gboolean +_chat_session_is_paused(const char * const recipient) { ChatSession *session = g_hash_table_lookup(sessions, recipient); @@ -215,8 +196,8 @@ chat_session_is_paused(const char * const recipient) } } -gboolean -chat_session_is_gone(const char * const recipient) +static gboolean +_chat_session_is_gone(const char * const recipient) { ChatSession *session = g_hash_table_lookup(sessions, recipient); @@ -227,8 +208,8 @@ chat_session_is_gone(const char * const recipient) } } -void -chat_session_set_gone(const char * const recipient) +static void +_chat_session_set_gone(const char * const recipient) { ChatSession *session = g_hash_table_lookup(sessions, recipient); @@ -237,8 +218,8 @@ chat_session_set_gone(const char * const recipient) } } -gboolean -chat_session_get_recipient_supports(const char * const recipient) +static gboolean +_chat_session_get_recipient_supports(const char * const recipient) { ChatSession *session = g_hash_table_lookup(sessions, recipient); @@ -249,9 +230,8 @@ chat_session_get_recipient_supports(const char * const recipient) } } -void -chat_session_set_recipient_supports(const char * const recipient, - gboolean recipient_supports) +static void +_chat_session_set_recipient_supports(const char * const recipient, gboolean recipient_supports) { ChatSession *session = g_hash_table_lookup(sessions, recipient); @@ -265,11 +245,11 @@ chat_session_on_message_send(const char * const barejid) { gboolean send_state = FALSE; if (prefs_get_boolean(PREF_STATES)) { - if (!chat_session_exists(barejid)) { - chat_session_new(barejid, TRUE); + if (!_chat_session_exists(barejid)) { + _chat_session_new(barejid, TRUE); } - if (chat_session_get_recipient_supports(barejid)) { - chat_session_set_active(barejid); + if (_chat_session_get_recipient_supports(barejid)) { + _chat_session_set_active(barejid); send_state = TRUE; } } @@ -280,10 +260,10 @@ chat_session_on_message_send(const char * const barejid) void chat_session_on_incoming_message(const char * const barejid, gboolean recipient_supports) { - if (!chat_session_exists(barejid)) { - chat_session_new(barejid, recipient_supports); + if (!_chat_session_exists(barejid)) { + _chat_session_new(barejid, recipient_supports); } else { - chat_session_set_recipient_supports(barejid, recipient_supports); + _chat_session_set_recipient_supports(barejid, recipient_supports); } } @@ -291,8 +271,86 @@ void chat_session_on_window_open(const char * const barejid) { if (prefs_get_boolean(PREF_STATES)) { - if (!chat_session_exists(barejid)) { - chat_session_new(barejid, TRUE); + if (!_chat_session_exists(barejid)) { + _chat_session_new(barejid, TRUE); + } + } +} + +void +chat_session_on_window_close(const char * const barejid) +{ + if (prefs_get_boolean(PREF_STATES)) { + // send chat state before closing + if (_chat_session_get_recipient_supports(barejid)) { + _chat_session_set_gone(barejid); + message_send_gone(barejid); + _chat_session_set_sent(barejid); + _chat_session_end(barejid); + } + } +} + +void +chat_session_on_cancel(const char * const jid) +{ + if (prefs_get_boolean(PREF_STATES) && _chat_session_exists(jid)) { + _chat_session_set_recipient_supports(jid, FALSE); + } +} + +void +chat_session_on_activity(const char * const barejid) +{ + if (_chat_session_get_recipient_supports(barejid)) { + _chat_session_set_composing(barejid); + if (!_chat_session_get_sent(barejid) || _chat_session_is_paused(barejid)) { + message_send_composing(barejid); + _chat_session_set_sent(barejid); + } + } +} + +void +chat_session_on_inactivity(const char * const barejid) +{ + if (_chat_session_get_recipient_supports(barejid)) { + ChatSession *session = g_hash_table_lookup(sessions, barejid); + if (session != NULL) { + if (session->active_timer != NULL) { + gdouble elapsed = g_timer_elapsed(session->active_timer, NULL); + + if ((prefs_get_gone() != 0) && (elapsed > (prefs_get_gone() * 60.0))) { + if (session->state != CHAT_STATE_GONE) { + session->sent = FALSE; + } + session->state = CHAT_STATE_GONE; + + } else if (elapsed > INACTIVE_TIMOUT) { + if (session->state != CHAT_STATE_INACTIVE) { + session->sent = FALSE; + } + session->state = CHAT_STATE_INACTIVE; + + } else if (elapsed > PAUSED_TIMOUT) { + + if (session->state == CHAT_STATE_COMPOSING) { + session->sent = FALSE; + session->state = CHAT_STATE_PAUSED; + } + } + } + } + + if (_chat_session_is_gone(barejid) && !_chat_session_get_sent(barejid)) { + message_send_gone(barejid); + _chat_session_set_sent(barejid); + } else if (_chat_session_is_inactive(barejid) && !_chat_session_get_sent(barejid)) { + message_send_inactive(barejid); + _chat_session_set_sent(barejid); + } else if (prefs_get_boolean(PREF_OUTTYPE) && _chat_session_is_paused(barejid) && !_chat_session_get_sent(barejid)) { + message_send_paused(barejid); + _chat_session_set_sent(barejid); } } } diff --git a/src/chat_session.h b/src/chat_session.h index 7f0a0e44..a44027a8 100644 --- a/src/chat_session.h +++ b/src/chat_session.h @@ -39,25 +39,12 @@ void chat_sessions_init(void); void chat_sessions_clear(void); -void chat_session_new(const char * const recipient, - gboolean recipient_supports); -gboolean chat_session_exists(const char * const recipient); -void chat_session_end(const char * const recipient); -gboolean chat_session_get_recipient_supports(const char * const recipient); -void chat_session_set_recipient_supports(const char * const recipient, - gboolean recipient_supports); - -void chat_session_set_composing(const char * const recipient); -void chat_session_no_activity(const char * const recipient); -gboolean chat_session_is_inactive(const char * const recipient); -void chat_session_set_active(const char * const recipient); -gboolean chat_session_is_paused(const char * const recipient); -gboolean chat_session_is_gone(const char * const recipient); -void chat_session_set_gone(const char * const recipient); -void chat_session_set_sent(const char * const recipient); -gboolean chat_session_get_sent(const char * const recipient); gboolean chat_session_on_message_send(const char * const barejid); void chat_session_on_window_open(const char * const barejid); +void chat_session_on_window_close(const char * const barejid); void chat_session_on_incoming_message(const char * const barejid, gboolean recipient_supports); +void chat_session_on_cancel(const char * const jid); +void chat_session_on_activity(const char * const barejid); +void chat_session_on_inactivity(const char * const recipient); #endif diff --git a/src/profanity.c b/src/profanity.c index eae08828..fb352879 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -137,31 +137,12 @@ prof_handle_idle(void) { jabber_conn_status_t status = jabber_get_connection_status(); if (status == JABBER_CONNECTED) { - // TODO get chat only recipients - GSList *recipients = ui_get_recipients(); + GSList *recipients = ui_get_chat_recipients(); GSList *curr = recipients; while (curr != NULL) { - char *recipient = curr->data; - if (chat_session_get_recipient_supports(recipient)) { - chat_session_no_activity(recipient); - - if (chat_session_is_gone(recipient) && - !chat_session_get_sent(recipient)) { - message_send_gone(recipient); - chat_session_set_sent(recipient); - } else if (chat_session_is_inactive(recipient) && - !chat_session_get_sent(recipient)) { - message_send_inactive(recipient); - chat_session_set_sent(recipient); - } else if (prefs_get_boolean(PREF_OUTTYPE) && - chat_session_is_paused(recipient) && - !chat_session_get_sent(recipient)) { - message_send_paused(recipient); - chat_session_set_sent(recipient); - } - } - + char *barejid = curr->data; + chat_session_on_inactivity(barejid); curr = g_slist_next(curr); } @@ -179,14 +160,7 @@ prof_handle_activity(void) if ((status == JABBER_CONNECTED) && (win_type == WIN_CHAT)) { ProfChatWin *chatwin = wins_get_current_chat(); - if (chat_session_get_recipient_supports(chatwin->barejid)) { - chat_session_set_composing(chatwin->barejid); - if (!chat_session_get_sent(chatwin->barejid) || - chat_session_is_paused(chatwin->barejid)) { - message_send_composing(chatwin->barejid); - chat_session_set_sent(chatwin->barejid); - } - } + chat_session_on_activity(chatwin->barejid); } } diff --git a/src/server_events.c b/src/server_events.c index 96014bf4..9d7883d6 100644 --- a/src/server_events.c +++ b/src/server_events.c @@ -77,23 +77,21 @@ handle_presence_error(const char *from, const char * const type, // handle message stanza errors void -handle_message_error(const char * const from, const char * const type, +handle_message_error(const char * const jid, const char * const type, const char * const err_msg) { // handle errors from no recipient - if (from == NULL) { + if (jid == NULL) { ui_handle_error(err_msg); // handle recipient not found ('from' contains a value and type is 'cancel') } else if (type != NULL && (strcmp(type, "cancel") == 0)) { - ui_handle_recipient_not_found(from, err_msg); - if (prefs_get_boolean(PREF_STATES) && chat_session_exists(from)) { - chat_session_set_recipient_supports(from, FALSE); - } + ui_handle_recipient_not_found(jid, err_msg); + chat_session_on_cancel(jid); // handle any other error from recipient } else { - ui_handle_recipient_error(from, err_msg); + ui_handle_recipient_error(jid, err_msg); } } diff --git a/src/ui/core.c b/src/ui/core.c index e7264435..c3f185f6 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -315,7 +315,7 @@ ui_contact_typing(const char * const barejid) } GSList * -ui_get_recipients(void) +ui_get_chat_recipients(void) { GSList *recipients = wins_get_chat_recipients(); return recipients; @@ -705,15 +705,7 @@ ui_close_connected_win(int index) otr_end_session(chatwin->barejid); } #endif - if (prefs_get_boolean(PREF_STATES)) { - // send chat state before closing - if (chat_session_get_recipient_supports(chatwin->barejid)) { - chat_session_set_gone(chatwin->barejid); - message_send_gone(chatwin->barejid); - chat_session_set_sent(chatwin->barejid); - chat_session_end(chatwin->barejid); - } - } + chat_session_on_window_close(chatwin->barejid); } } } @@ -1169,17 +1161,8 @@ ui_prune_wins(void) ProfWin *window = curr->data; if (window->type == WIN_CHAT) { if (conn_status == JABBER_CONNECTED) { - if (prefs_get_boolean(PREF_STATES)) { - ProfChatWin *chatwin = (ProfChatWin*)window; - - // send chat state before closing - if (chat_session_get_recipient_supports(chatwin->barejid)) { - chat_session_set_gone(chatwin->barejid); - message_send_gone(chatwin->barejid); - chat_session_set_sent(chatwin->barejid); - chat_session_end(chatwin->barejid); - } - } + ProfChatWin *chatwin = (ProfChatWin*)window; + chat_session_on_window_close(chatwin->barejid); } } diff --git a/src/ui/ui.h b/src/ui/ui.h index 7db2227f..e7b46766 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -60,7 +60,7 @@ void ui_update(void); void ui_close(void); void ui_redraw(void); void ui_resize(void); -GSList* ui_get_recipients(void); +GSList* ui_get_chat_recipients(void); void ui_handle_special_keys(const wint_t * const ch, const int result); gboolean ui_switch_win(const int i); void ui_next_win(void); diff --git a/src/xmpp/message.c b/src/xmpp/message.c index 0b40b979..c1c1ac14 100644 --- a/src/xmpp/message.c +++ b/src/xmpp/message.c @@ -201,7 +201,7 @@ _message_error_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) { char *id = xmpp_stanza_get_id(stanza); - char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); + char *jid = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); xmpp_stanza_t *error_stanza = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_ERROR); char *type = NULL; if (error_stanza != NULL) { @@ -216,9 +216,9 @@ _message_error_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, g_string_append(log_msg, " id="); g_string_append(log_msg, id); } - if (from != NULL) { + if (jid != NULL) { g_string_append(log_msg, " from="); - g_string_append(log_msg, from); + g_string_append(log_msg, jid); } if (type != NULL) { g_string_append(log_msg, " type="); @@ -231,7 +231,7 @@ _message_error_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, g_string_free(log_msg, TRUE); - handle_message_error(from, type, err_msg); + handle_message_error(jid, type, err_msg); free(err_msg); diff --git a/tests/test_server_events.c b/tests/test_server_events.c index f3c571b8..435493a2 100644 --- a/tests/test_server_events.c +++ b/tests/test_server_events.c @@ -134,13 +134,13 @@ void handle_message_error_when_recipient_cancel_disables_chat_session(void **sta prefs_set_boolean(PREF_STATES, TRUE); chat_sessions_init(); - chat_session_new(from, TRUE); + chat_session_on_incoming_message(from, TRUE); expect_any(ui_handle_recipient_not_found, recipient); expect_any(ui_handle_recipient_not_found, err_msg); handle_message_error(from, type, err_msg); - gboolean chat_session_supported = chat_session_get_recipient_supports(from); + gboolean chat_session_supported = chat_session_on_message_send(from); assert_false(chat_session_supported); chat_sessions_clear(); diff --git a/tests/ui/stub_ui.c b/tests/ui/stub_ui.c index 0238b287..5361c08d 100644 --- a/tests/ui/stub_ui.c +++ b/tests/ui/stub_ui.c @@ -47,7 +47,7 @@ void ui_update(void) {} void ui_close(void) {} void ui_redraw(void) {} void ui_resize(void) {} -GSList* ui_get_recipients(void) +GSList* ui_get_chat_recipients(void) { return NULL; }