From 29b5abfe06861c848fdaf3503edd1aa81e2eb6f3 Mon Sep 17 00:00:00 2001 From: James Booth Date: Thu, 8 Nov 2012 23:31:21 +0000 Subject: [PATCH] Added function to create message stanzas --- src/jabber.c | 42 ++++++++++++------------------------------ src/stanza.c | 44 +++++++++++++++++++++++++++++++++++++------- src/stanza.h | 3 +++ 3 files changed, 52 insertions(+), 37 deletions(-) diff --git a/src/jabber.c b/src/jabber.c index 7bb53add..f8749930 100644 --- a/src/jabber.c +++ b/src/jabber.c @@ -144,44 +144,26 @@ jabber_process_events(void) void jabber_send(const char * const msg, const char * const recipient) { + char *encoded_xml = encode_xml(msg); + if (prefs_get_states()) { if (!chat_session_exists(recipient)) { chat_session_start(recipient, TRUE); } } - char *encoded_xml = encode_xml(msg); - - xmpp_stanza_t *reply, *body, *text, *active; - - reply = xmpp_stanza_new(jabber_conn.ctx); - xmpp_stanza_set_name(reply, "message"); - xmpp_stanza_set_type(reply, "chat"); - xmpp_stanza_set_attribute(reply, "to", recipient); - - body = xmpp_stanza_new(jabber_conn.ctx); - xmpp_stanza_set_name(body, "body"); - - text = xmpp_stanza_new(jabber_conn.ctx); - xmpp_stanza_set_text(text, encoded_xml); - - if (prefs_get_states()) { - - // always send with messages when recipient supports chat states - if (chat_session_get_recipient_supports(recipient)) { - chat_session_set_active(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_t *message; + if (prefs_get_states() && chat_session_get_recipient_supports(recipient)) { + chat_session_set_active(recipient); + message = stanza_create_message(jabber_conn.ctx, recipient, "chat", + encoded_xml, "active"); + } else { + message = stanza_create_message(jabber_conn.ctx, recipient, "chat", + encoded_xml, NULL); } - xmpp_stanza_add_child(body, text); - xmpp_stanza_add_child(reply, body); - - xmpp_send(jabber_conn.conn, reply); - xmpp_stanza_release(reply); + xmpp_send(jabber_conn.conn, message); + xmpp_stanza_release(message); free(encoded_xml); } diff --git a/src/stanza.c b/src/stanza.c index a65365ff..bf9665cd 100644 --- a/src/stanza.c +++ b/src/stanza.c @@ -26,18 +26,48 @@ xmpp_stanza_t * stanza_create_chat_state(xmpp_ctx_t *ctx, const char * const recipient, const char * const state) { - xmpp_stanza_t *message, *chat_state; + xmpp_stanza_t *msg, *chat_state; - message = xmpp_stanza_new(ctx); - xmpp_stanza_set_name(message, "message"); - xmpp_stanza_set_type(message, "chat"); - xmpp_stanza_set_attribute(message, "to", recipient); + msg = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(msg, "message"); + xmpp_stanza_set_type(msg, "chat"); + xmpp_stanza_set_attribute(msg, "to", recipient); chat_state = xmpp_stanza_new(ctx); xmpp_stanza_set_name(chat_state, state); xmpp_stanza_set_ns(chat_state, "http://jabber.org/protocol/chatstates"); - xmpp_stanza_add_child(message, chat_state); + xmpp_stanza_add_child(msg, chat_state); - return message; + return msg; +} + +xmpp_stanza_t * +stanza_create_message(xmpp_ctx_t *ctx, const char * const recipient, + const char * const type, const char * const message, + const char * const state) +{ + xmpp_stanza_t *msg, *body, *text; + + msg = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(msg, "message"); + xmpp_stanza_set_type(msg, "chat"); + xmpp_stanza_set_attribute(msg, "to", recipient); + + body = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(body, "body"); + + text = xmpp_stanza_new(ctx); + xmpp_stanza_set_text(text, message); + xmpp_stanza_add_child(body, text); + xmpp_stanza_add_child(msg, body); + + if (state != NULL) { + xmpp_stanza_t *chat_state = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(chat_state, state); + xmpp_stanza_set_ns(chat_state, "http://jabber.org/protocol/chatstates"); + xmpp_stanza_add_child(msg, chat_state); + } + + return msg; } diff --git a/src/stanza.h b/src/stanza.h index 4f768f3a..662a1059 100644 --- a/src/stanza.h +++ b/src/stanza.h @@ -28,4 +28,7 @@ xmpp_stanza_t* stanza_create_chat_state(xmpp_ctx_t *ctx, const char * const recipient, const char * const state); +xmpp_stanza_t* stanza_create_message(xmpp_ctx_t *ctx, + const char * const recipient, const char * const type, + const char * const message, const char * const state); #endif