diff --git a/src/chat_session.c b/src/chat_session.c
index 67104fa6..eae3f577 100644
--- a/src/chat_session.c
+++ b/src/chat_session.c
@@ -62,7 +62,6 @@ typedef struct chat_session_t {
chat_state_t state;
GTimer *active_timer;
gboolean sent;
- gboolean includes_message;
} ChatSession;
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->active_timer = g_timer_new();
new_session->sent = FALSE;
- new_session->includes_message = FALSE;
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);
session = _chat_session_new(barejid, resource, send_states);
- session->includes_message = TRUE;
g_hash_table_insert(sessions, strdup(barejid), session);
// 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);
}
session->send_states = send_states;
- session->includes_message = TRUE;
}
if (send_states) {
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;
g_timer_start(session->active_timer);
session->sent = TRUE;
- session->includes_message = TRUE;
}
void
@@ -221,7 +216,7 @@ chat_session_on_window_close(const char * const barejid)
ChatSession *session = g_hash_table_lookup(sessions, barejid);
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);
if (session->resource) {
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
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);
}
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);
}
session->sent = TRUE;
diff --git a/src/chat_session.h b/src/chat_session.h
index 441f9096..dbffad9f 100644
--- a/src/chat_session.h
+++ b/src/chat_session.h
@@ -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_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_offline(const char * const barejid, const char * const resource);
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_inactivity(const char * const barejid);
diff --git a/src/server_events.c b/src/server_events.c
index 86838ad1..edd58724 100644
--- a/src/server_events.c
+++ b/src/server_events.c
@@ -393,6 +393,7 @@ handle_typing(char *from)
void
handle_gone(const char * const from)
{
+ chat_session_on_gone(from);
ui_recipient_gone(from);
}
@@ -463,6 +464,7 @@ handle_contact_offline(char *barejid, char *resource, char *status)
}
rosterwin_roster();
+ chat_session_on_offline(barejid, resource);
}
void
diff --git a/src/xmpp/message.c b/src/xmpp/message.c
index c3cbc14f..e436dbc1 100644
--- a/src/xmpp/message.c
+++ b/src/xmpp/message.c
@@ -472,7 +472,9 @@ _chat_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
}
// create or update chat session
- chat_session_on_incoming_message(jid->barejid, jid->resourcepart, recipient_supports);
+ if (xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_ACTIVE) != NULL) {
+ chat_session_on_incoming_message(jid->barejid, jid->resourcepart, recipient_supports);
+ }
// determine if the notifications happened whilst offline
GTimeVal tv_stamp;
@@ -488,7 +490,7 @@ _chat_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
// do something
} else if (xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_INACTIVE) != NULL) {
// do something
- } else { // handle
+ } else { //
// do something
}
}
diff --git a/tests/test_chat_session.c b/tests/test_chat_session.c
index a93c64b6..bcb8dcce 100644
--- a/tests/test_chat_session.c
+++ b/tests/test_chat_session.c
@@ -122,4 +122,42 @@ void removes_chat_session_on_cancel_for_fulljid(void **state)
gboolean exists = chat_session_exists(barejid);
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);
}
\ No newline at end of file
diff --git a/tests/test_chat_session.h b/tests/test_chat_session.h
index 8916f6b4..068f4d5f 100644
--- a/tests/test_chat_session.h
+++ b/tests/test_chat_session.h
@@ -8,4 +8,7 @@ void sets_send_states_on_incoming_message(void **state);
void replaces_chat_session_when_new_resource(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_fulljid(void **state);
\ No newline at end of file
+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);
\ No newline at end of file
diff --git a/tests/test_server_events.c b/tests/test_server_events.c
index fcf6fb1c..20ad246c 100644
--- a/tests/test_server_events.c
+++ b/tests/test_server_events.c
@@ -175,3 +175,22 @@ void handle_presence_error_when_from_recipient(void **state)
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();
+}
diff --git a/tests/test_server_events.h b/tests/test_server_events.h
index 68e78557..15a8f597 100644
--- a/tests/test_server_events.h
+++ b/tests/test_server_events.h
@@ -9,4 +9,5 @@ void handle_message_error_when_recipient_cancel(void **state);
void handle_message_error_when_recipient_cancel_disables_chat_session(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_from_recipient(void **state);
\ No newline at end of file
+void handle_presence_error_when_from_recipient(void **state);
+void handle_offline_removes_chat_session(void **state);
\ No newline at end of file
diff --git a/tests/testsuite.c b/tests/testsuite.c
index db05639d..3b86d5ed 100644
--- a/tests/testsuite.c
+++ b/tests/testsuite.c
@@ -239,6 +239,15 @@ int main(int argc, char* argv[]) {
unit_test_setup_teardown(removes_chat_session_on_cancel_for_fulljid,
init_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,
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_presence_error_when_no_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_value),