Create chat session when no resource

This commit is contained in:
James Booth
2015-01-05 00:48:30 +00:00
parent 8326c8b3a2
commit c2dccad818
4 changed files with 109 additions and 84 deletions

View File

@@ -71,7 +71,11 @@ _chat_session_new(const char * const barejid, const char * const resource, gbool
{ {
ChatSession *new_session = malloc(sizeof(struct chat_session_t)); ChatSession *new_session = malloc(sizeof(struct chat_session_t));
new_session->barejid = strdup(barejid); new_session->barejid = strdup(barejid);
new_session->resource = strdup(resource); if (resource) {
new_session->resource = strdup(resource);
} else {
new_session->resource = NULL;
}
new_session->send_states = send_states; new_session->send_states = send_states;
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();
@@ -139,24 +143,15 @@ chat_session_on_incoming_message(const char * const barejid, const char * const
{ {
ChatSession *session = g_hash_table_lookup(sessions, barejid); ChatSession *session = g_hash_table_lookup(sessions, barejid);
if (resource) { if (!session) {
if (session) { session = _chat_session_new(barejid, resource, send_states);
if (g_strcmp0(session->resource, resource) != 0) { g_hash_table_insert(sessions, strdup(barejid), session);
log_info("Replacing chat session %s/%s, with new session %s/%s", session->barejid, session->resource, barejid, resource); } else if (g_strcmp0(session->resource, resource) != 0) {
g_hash_table_remove(sessions, session);
session = _chat_session_new(barejid, resource, send_states);
g_hash_table_insert(sessions, strdup(barejid), session);
} else {
session->send_states = send_states;
}
} else {
log_info("Starting chat session with %s/%s", barejid, resource);
session = _chat_session_new(barejid, resource, send_states);
g_hash_table_insert(sessions, strdup(barejid), session);
}
} else if (session) {
log_info("Ending chat session with %s/%s, message received with no resource", session->barejid, session->resource);
g_hash_table_remove(sessions, session); g_hash_table_remove(sessions, session);
session = _chat_session_new(barejid, resource, send_states);
g_hash_table_insert(sessions, strdup(barejid), session);
} else {
session->send_states = send_states;
} }
} }
@@ -164,13 +159,28 @@ void
chat_session_on_message_send(const char * const barejid) chat_session_on_message_send(const char * const barejid)
{ {
ChatSession *session = g_hash_table_lookup(sessions, barejid); ChatSession *session = g_hash_table_lookup(sessions, barejid);
assert(session != NULL);
// if no session exists, create one with no resource, and send states
if (!session) {
session = _chat_session_new(barejid, NULL, TRUE);
g_hash_table_insert(sessions, strdup(barejid), session);
}
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;
} }
void
chat_session_on_window_open(const char * const barejid)
{
ChatSession *session = g_hash_table_lookup(sessions, barejid);
if (!session) {
session = _chat_session_new(barejid, NULL, TRUE);
g_hash_table_insert(sessions, strdup(barejid), session);
}
}
void void
chat_session_on_window_close(const char * const barejid) chat_session_on_window_close(const char * const barejid)
{ {
@@ -178,16 +188,25 @@ chat_session_on_window_close(const char * const barejid)
assert(session != NULL); assert(session != NULL);
if (prefs_get_boolean(PREF_STATES) && session->send_states) { if (prefs_get_boolean(PREF_STATES) && session->send_states) {
message_send_gone(barejid); GString *jid = g_string_new(session->barejid);
if (session->resource) {
g_string_append(jid, "/");
g_string_append(jid, session->resource);
}
message_send_gone(jid->str);
g_string_free(jid, TRUE);
} }
} }
void void
chat_session_on_cancel(const char * const jid) chat_session_on_cancel(const char * const jid)
{ {
ChatSession *session = g_hash_table_lookup(sessions, jid); Jid *jidp = jid_create(jid);
if (session) { if (jidp) {
session->send_states = FALSE; ChatSession *session = g_hash_table_lookup(sessions, jidp->barejid);
if (session) {
session->send_states = FALSE;
}
} }
} }
@@ -195,24 +214,28 @@ void
chat_session_on_activity(const char * const barejid) chat_session_on_activity(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 (session->state != CHAT_STATE_COMPOSING) { return;
session->sent = FALSE; }
}
session->state = CHAT_STATE_COMPOSING; if (session->state != CHAT_STATE_COMPOSING) {
g_timer_start(session->active_timer); session->sent = FALSE;
}
if (!session->sent || session->state == CHAT_STATE_PAUSED) { session->state = CHAT_STATE_COMPOSING;
if (prefs_get_boolean(PREF_STATES) && prefs_get_boolean(PREF_OUTTYPE) && session->send_states) { g_timer_start(session->active_timer);
Jid *jidp = jid_create_from_bare_and_resource(session->barejid, session->resource);
message_send_composing(jidp->fulljid); if (!session->sent || session->state == CHAT_STATE_PAUSED) {
jid_destroy(jidp); if (prefs_get_boolean(PREF_STATES) && prefs_get_boolean(PREF_OUTTYPE) && session->send_states) {
GString *jid = g_string_new(session->barejid);
if (session->resource) {
g_string_append(jid, "/");
g_string_append(jid, session->resource);
} }
session->sent = TRUE; message_send_composing(jid->str);
g_string_free(jid, TRUE);
} }
} else if (prefs_get_boolean(PREF_STATES) && prefs_get_boolean(PREF_OUTTYPE)) { session->sent = TRUE;
message_send_composing(barejid);
} }
} }
@@ -220,49 +243,55 @@ void
chat_session_on_inactivity(const char * const barejid) chat_session_on_inactivity(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 (session->active_timer != NULL) { return;
gdouble elapsed = g_timer_elapsed(session->active_timer, NULL); }
if ((prefs_get_gone() != 0) && (elapsed > (prefs_get_gone() * 60.0))) { if (session->active_timer != NULL) {
if (session->state != CHAT_STATE_GONE) { gdouble elapsed = g_timer_elapsed(session->active_timer, NULL);
session->sent = FALSE;
}
session->state = CHAT_STATE_GONE;
} else if (elapsed > INACTIVE_TIMOUT) { if ((prefs_get_gone() != 0) && (elapsed > (prefs_get_gone() * 60.0))) {
if (session->state != CHAT_STATE_INACTIVE) { if (session->state != CHAT_STATE_GONE) {
session->sent = FALSE; 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;
}
} }
} session->state = CHAT_STATE_GONE;
if (session->sent == FALSE) { } else if (elapsed > INACTIVE_TIMOUT) {
Jid *jidp = jid_create_from_bare_and_resource(session->barejid, session->resource); if (session->state != CHAT_STATE_INACTIVE) {
if (session->state == CHAT_STATE_GONE) { session->sent = FALSE;
if (prefs_get_boolean(PREF_STATES) && session->send_states) { }
message_send_gone(jidp->fulljid); session->state = CHAT_STATE_INACTIVE;
}
session->sent = TRUE; } else if (elapsed > PAUSED_TIMOUT) {
} else if (session->state == CHAT_STATE_INACTIVE) { if (session->state == CHAT_STATE_COMPOSING) {
if (prefs_get_boolean(PREF_STATES) && session->send_states) { session->sent = FALSE;
message_send_inactive(jidp->fulljid); session->state = CHAT_STATE_PAUSED;
}
session->sent = TRUE;
} else if (session->state == CHAT_STATE_PAUSED && prefs_get_boolean(PREF_OUTTYPE)) {
if (prefs_get_boolean(PREF_STATES) && session->send_states) {
message_send_paused(jidp->fulljid);
}
session->sent = TRUE;
} }
jid_destroy(jidp);
} }
} }
if (session->sent == FALSE) {
GString *jid = g_string_new(session->barejid);
if (session->resource) {
g_string_append(jid, "/");
g_string_append(jid, session->resource);
}
if (session->state == CHAT_STATE_GONE) {
if (prefs_get_boolean(PREF_STATES) && session->send_states) {
message_send_gone(jid->str);
}
session->sent = TRUE;
} else if (session->state == CHAT_STATE_INACTIVE) {
if (prefs_get_boolean(PREF_STATES) && session->send_states) {
message_send_inactive(jid->str);
}
session->sent = TRUE;
} else if (session->state == CHAT_STATE_PAUSED && prefs_get_boolean(PREF_OUTTYPE)) {
if (prefs_get_boolean(PREF_STATES) && session->send_states) {
message_send_paused(jid->str);
}
session->sent = TRUE;
}
g_string_free(jid, TRUE);
}
} }

View File

@@ -45,6 +45,7 @@ char* chat_session_get_resource(const char * const barejid);
gboolean chat_session_send_states(const char * const barejid); 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_open(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_cancel(const char * const jid); void chat_session_on_cancel(const char * const jid);

View File

@@ -1333,6 +1333,7 @@ ui_new_chat_win(const char * const barejid)
// create new window // create new window
if (window == NULL) { if (window == NULL) {
window = wins_new_chat(barejid); window = wins_new_chat(barejid);
chat_session_on_window_open(barejid);
num = wins_get_num(window); num = wins_get_num(window);

View File

@@ -86,15 +86,9 @@ message_send_chat(const char * const barejid, const char * const msg)
xmpp_conn_t * const conn = connection_get_conn(); xmpp_conn_t * const conn = connection_get_conn();
xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_ctx_t * const ctx = connection_get_ctx();
char *resource = NULL; chat_session_on_message_send(barejid);
gboolean send_state = FALSE; char *resource = chat_session_get_resource(barejid);
if (chat_session_exists(barejid)) { gboolean send_state = chat_session_send_states(barejid);
chat_session_on_message_send(barejid);
resource = chat_session_get_resource(barejid);
send_state = chat_session_send_states(barejid);
} else {
send_state = TRUE;
}
GString *jid = g_string_new(barejid); GString *jid = g_string_new(barejid);
if (resource) { if (resource) {