From bee708c9d253524d4af3a9d1599300722ff69044 Mon Sep 17 00:00:00 2001 From: James Booth Date: Tue, 30 Oct 2012 01:50:39 +0000 Subject: [PATCH] Create chat session on send if none exists --- src/chat_session.c | 12 ++++++++++++ src/chat_session.h | 1 + src/jabber.c | 17 ++++++++++++----- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/chat_session.c b/src/chat_session.c index f352fea0..283860de 100644 --- a/src/chat_session.c +++ b/src/chat_session.c @@ -52,6 +52,18 @@ chat_sessions_clear(void) g_hash_table_remove_all(sessions); } +gboolean +chat_session_exists(const char * const recipient) +{ + ChatSession session = g_hash_table_lookup(sessions, recipient); + + if (session != NULL) { + return TRUE; + } else { + return FALSE; + } +} + void chat_session_start(const char * const recipient, gboolean recipient_supports) { diff --git a/src/chat_session.h b/src/chat_session.h index aecc47da..7cfc8de5 100644 --- a/src/chat_session.h +++ b/src/chat_session.h @@ -31,6 +31,7 @@ void chat_sessions_init(void); void chat_sessions_clear(void); void chat_session_start(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_recipient_supports(const char * const recipient); diff --git a/src/jabber.c b/src/jabber.c index 472aa015..23f7f444 100644 --- a/src/jabber.c +++ b/src/jabber.c @@ -137,16 +137,16 @@ jabber_process_events(void) void jabber_send(const char * const msg, const char * const recipient) { + if (!chat_session_exists(recipient)) { + chat_session_start(recipient, TRUE); + } + char *coded_msg = str_replace(msg, "&", "&"); char *coded_msg2 = str_replace(coded_msg, "<", "<"); char *coded_msg3 = str_replace(coded_msg2, ">", ">"); xmpp_stanza_t *reply, *body, *text, *active; - active = xmpp_stanza_new(jabber_conn.ctx); - xmpp_stanza_set_name(active, "active"); - xmpp_stanza_set_ns(active, "http://jabber.org/protocol/chatstates"); - reply = xmpp_stanza_new(jabber_conn.ctx); xmpp_stanza_set_name(reply, "message"); xmpp_stanza_set_type(reply, "chat"); @@ -157,7 +157,14 @@ jabber_send(const char * const msg, const char * const recipient) text = xmpp_stanza_new(jabber_conn.ctx); xmpp_stanza_set_text(text, coded_msg3); - xmpp_stanza_add_child(reply, active); + + if (chat_session_recipient_supports(recipient)) { + active = xmpp_stanza_new(jabber_conn.ctx); + xmpp_stanza_set_name(active, "active"); + xmpp_stanza_set_ns(active, "http://jabber.org/protocol/chatstates"); + xmpp_stanza_add_child(reply, active); + } + xmpp_stanza_add_child(body, text); xmpp_stanza_add_child(reply, body);