1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

Added offline handler for chat sessions

This commit is contained in:
James Booth 2015-01-06 23:19:56 +00:00
parent 36790dc26b
commit 95ad564372
9 changed files with 122 additions and 11 deletions

View File

@ -62,7 +62,6 @@ typedef struct chat_session_t {
chat_state_t state; chat_state_t state;
GTimer *active_timer; GTimer *active_timer;
gboolean sent; gboolean sent;
gboolean includes_message;
} ChatSession; } ChatSession;
static GHashTable *sessions; static GHashTable *sessions;
@ -81,7 +80,6 @@ _chat_session_new(const char * const barejid, const char * const resource, gbool
new_session->state = CHAT_STATE_STARTED; new_session->state = CHAT_STATE_STARTED;
new_session->active_timer = g_timer_new(); new_session->active_timer = g_timer_new();
new_session->sent = FALSE; new_session->sent = FALSE;
new_session->includes_message = FALSE;
return new_session; return new_session;
} }
@ -173,7 +171,6 @@ chat_session_on_incoming_message(const char * const barejid, const char * const
} }
g_hash_table_remove(sessions, session); g_hash_table_remove(sessions, session);
session = _chat_session_new(barejid, resource, send_states); session = _chat_session_new(barejid, resource, send_states);
session->includes_message = TRUE;
g_hash_table_insert(sessions, strdup(barejid), session); g_hash_table_insert(sessions, strdup(barejid), session);
// session exists for resource, update state // session exists for resource, update state
@ -185,7 +182,6 @@ chat_session_on_incoming_message(const char * const barejid, const char * const
g_string_append(log_msg, session->resource); g_string_append(log_msg, session->resource);
} }
session->send_states = send_states; session->send_states = send_states;
session->includes_message = TRUE;
} }
if (send_states) { if (send_states) {
g_string_append(log_msg, ", chat states supported"); g_string_append(log_msg, ", chat states supported");
@ -212,7 +208,6 @@ chat_session_on_message_send(const char * const barejid)
session->state = CHAT_STATE_ACTIVE; session->state = CHAT_STATE_ACTIVE;
g_timer_start(session->active_timer); g_timer_start(session->active_timer);
session->sent = TRUE; session->sent = TRUE;
session->includes_message = TRUE;
} }
void void
@ -221,7 +216,7 @@ chat_session_on_window_close(const char * const barejid)
ChatSession *session = g_hash_table_lookup(sessions, barejid); ChatSession *session = g_hash_table_lookup(sessions, barejid);
if (session) { if (session) {
if (prefs_get_boolean(PREF_STATES) && session->send_states && session->includes_message) { if (prefs_get_boolean(PREF_STATES) && session->send_states) {
GString *jid = g_string_new(session->barejid); GString *jid = g_string_new(session->barejid);
if (session->resource) { if (session->resource) {
g_string_append(jid, "/"); g_string_append(jid, "/");
@ -242,6 +237,45 @@ chat_session_on_window_close(const char * const barejid)
} }
} }
void
chat_session_on_offline(const char * const barejid, const char * const resource)
{
if (!resource) {
return;
}
ChatSession *session = g_hash_table_lookup(sessions, barejid);
if (session && (g_strcmp0(session->resource, resource) == 0)) {
GString *log_msg = g_string_new("Removing chat session for ");
g_string_append(log_msg, barejid);
if (session->resource) {
g_string_append(log_msg, "/");
g_string_append(log_msg, session->resource);
}
log_debug(log_msg->str);
g_string_free(log_msg, TRUE);
g_hash_table_remove(sessions, barejid);
}
}
void
chat_session_on_gone(const char * const barejid)
{
ChatSession *session = g_hash_table_lookup(sessions, barejid);
if (session) {
GString *log_msg = g_string_new("Removing chat session for ");
g_string_append(log_msg, barejid);
if (session->resource) {
g_string_append(log_msg, "/");
g_string_append(log_msg, session->resource);
}
log_debug(log_msg->str);
g_string_free(log_msg, TRUE);
g_hash_table_remove(sessions, barejid);
}
}
void void
chat_session_on_activity(const char * const barejid) chat_session_on_activity(const char * const barejid)
{ {
@ -312,7 +346,7 @@ chat_session_on_inactivity(const char * const barejid)
g_string_append(jid, session->resource); g_string_append(jid, session->resource);
} }
if (session->state == CHAT_STATE_GONE) { if (session->state == CHAT_STATE_GONE) {
if (prefs_get_boolean(PREF_STATES) && session->send_states && session->includes_message) { if (prefs_get_boolean(PREF_STATES) && session->send_states) {
message_send_gone(jid->str); message_send_gone(jid->str);
} }
session->sent = TRUE; session->sent = TRUE;

View File

@ -47,7 +47,9 @@ gboolean chat_session_send_states(const char * const barejid);
void chat_session_on_message_send(const char * const barejid); void chat_session_on_message_send(const char * const barejid);
void chat_session_on_window_close(const char * const barejid); void chat_session_on_window_close(const char * const barejid);
void chat_session_on_incoming_message(const char * const barejid, const char * const resource, gboolean send_states); void chat_session_on_incoming_message(const char * const barejid, const char * const resource, gboolean send_states);
void chat_session_on_offline(const char * const barejid, const char * const resource);
void chat_session_on_cancel(const char * const jid); void chat_session_on_cancel(const char * const jid);
void chat_session_on_gone(const char * const barejid);
void chat_session_on_activity(const char * const barejid); void chat_session_on_activity(const char * const barejid);
void chat_session_on_inactivity(const char * const barejid); void chat_session_on_inactivity(const char * const barejid);

View File

@ -393,6 +393,7 @@ handle_typing(char *from)
void void
handle_gone(const char * const from) handle_gone(const char * const from)
{ {
chat_session_on_gone(from);
ui_recipient_gone(from); ui_recipient_gone(from);
} }
@ -463,6 +464,7 @@ handle_contact_offline(char *barejid, char *resource, char *status)
} }
rosterwin_roster(); rosterwin_roster();
chat_session_on_offline(barejid, resource);
} }
void void

View File

@ -472,7 +472,9 @@ _chat_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
} }
// create or update chat session // create or update chat session
if (xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_ACTIVE) != NULL) {
chat_session_on_incoming_message(jid->barejid, jid->resourcepart, recipient_supports); chat_session_on_incoming_message(jid->barejid, jid->resourcepart, recipient_supports);
}
// determine if the notifications happened whilst offline // determine if the notifications happened whilst offline
GTimeVal tv_stamp; GTimeVal tv_stamp;
@ -488,7 +490,7 @@ _chat_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
// do something // do something
} else if (xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_INACTIVE) != NULL) { } else if (xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_INACTIVE) != NULL) {
// do something // do something
} else { // handle <active/> } else { // <active/>
// do something // do something
} }
} }

View File

@ -123,3 +123,41 @@ void removes_chat_session_on_cancel_for_fulljid(void **state)
assert_false(exists); assert_false(exists);
} }
void removes_chat_session_on_offline_matching_resource(void **state)
{
char *barejid = "myjid@server.org";
char *resource = "work";
chat_session_on_message_send(barejid);
chat_session_on_incoming_message(barejid, resource, TRUE);
chat_session_on_offline(barejid, resource);
gboolean exists = chat_session_exists(barejid);
assert_false(exists);
}
void does_not_remove_chat_session_on_offline_different_resource(void **state)
{
char *barejid = "myjid@server.org";
char *resource = "work";
char *offline_resource = "home";
chat_session_on_message_send(barejid);
chat_session_on_incoming_message(barejid, resource, TRUE);
chat_session_on_offline(barejid, offline_resource);
gboolean exists = chat_session_exists(barejid);
assert_true(exists);
}
void does_not_remove_chat_session_on_offline_null_resource(void **state)
{
char *barejid = "myjid@server.org";
chat_session_on_message_send(barejid);
chat_session_on_offline(barejid, NULL);
gboolean exists = chat_session_exists(barejid);
assert_true(exists);
}

View File

@ -9,3 +9,6 @@ void replaces_chat_session_when_new_resource(void **state);
void removes_chat_session_on_window_close(void **state); void removes_chat_session_on_window_close(void **state);
void removes_chat_session_on_cancel_for_barejid(void **state); void removes_chat_session_on_cancel_for_barejid(void **state);
void removes_chat_session_on_cancel_for_fulljid(void **state); void removes_chat_session_on_cancel_for_fulljid(void **state);
void removes_chat_session_on_offline_matching_resource(void **state);
void does_not_remove_chat_session_on_offline_different_resource(void **state);
void does_not_remove_chat_session_on_offline_null_resource(void **state);

View File

@ -175,3 +175,22 @@ void handle_presence_error_when_from_recipient(void **state)
handle_presence_error(from, type, err_msg); handle_presence_error(from, type, err_msg);
} }
void handle_offline_removes_chat_session(void **state)
{
chat_sessions_init();
char *barejid = "friend@server.chat.com";
char *resource = "home";
roster_init();
roster_add(barejid, "bob", NULL, "both", FALSE);
Resource *resourcep = resource_new(resource, RESOURCE_ONLINE, NULL, 10);
roster_update_presence(barejid, resourcep, NULL);
chat_session_on_incoming_message(barejid, resource, TRUE);
handle_contact_offline(barejid, resource, NULL);
gboolean exists = chat_session_exists(barejid);
assert_false(exists);
roster_clear();
chat_sessions_clear();
}

View File

@ -10,3 +10,4 @@ void handle_message_error_when_recipient_cancel_disables_chat_session(void **sta
void handle_message_error_when_recipient_and_no_type(void **state); void handle_message_error_when_recipient_and_no_type(void **state);
void handle_presence_error_when_no_recipient(void **state); void handle_presence_error_when_no_recipient(void **state);
void handle_presence_error_when_from_recipient(void **state); void handle_presence_error_when_from_recipient(void **state);
void handle_offline_removes_chat_session(void **state);

View File

@ -239,6 +239,15 @@ int main(int argc, char* argv[]) {
unit_test_setup_teardown(removes_chat_session_on_cancel_for_fulljid, unit_test_setup_teardown(removes_chat_session_on_cancel_for_fulljid,
init_chat_sessions, init_chat_sessions,
close_chat_sessions), close_chat_sessions),
unit_test_setup_teardown(removes_chat_session_on_offline_matching_resource,
init_chat_sessions,
close_chat_sessions),
unit_test_setup_teardown(does_not_remove_chat_session_on_offline_different_resource,
init_chat_sessions,
close_chat_sessions),
unit_test_setup_teardown(does_not_remove_chat_session_on_offline_null_resource,
init_chat_sessions,
close_chat_sessions),
unit_test_setup_teardown(cmd_connect_shows_message_when_disconnecting, unit_test_setup_teardown(cmd_connect_shows_message_when_disconnecting,
load_preferences, load_preferences,
@ -475,6 +484,7 @@ int main(int argc, char* argv[]) {
unit_test(handle_message_error_when_recipient_and_no_type), unit_test(handle_message_error_when_recipient_and_no_type),
unit_test(handle_presence_error_when_no_recipient), unit_test(handle_presence_error_when_no_recipient),
unit_test(handle_presence_error_when_from_recipient), unit_test(handle_presence_error_when_from_recipient),
unit_test(handle_offline_removes_chat_session),
unit_test(cmd_alias_add_shows_usage_when_no_args), unit_test(cmd_alias_add_shows_usage_when_no_args),
unit_test(cmd_alias_add_shows_usage_when_no_value), unit_test(cmd_alias_add_shows_usage_when_no_value),