From 34c21c0a2ee2b06f0a6f81536398f93be07668c0 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 10 Nov 2012 00:16:56 +0000 Subject: [PATCH] Moved update presence handler creation to stanza module --- src/jabber.c | 61 ++++++++++++++++++---------------------------------- src/stanza.c | 34 +++++++++++++++++++++++++++++ src/stanza.h | 4 ++++ 3 files changed, 59 insertions(+), 40 deletions(-) diff --git a/src/jabber.c b/src/jabber.c index 7d9528f4..a97e695b 100644 --- a/src/jabber.c +++ b/src/jabber.c @@ -260,48 +260,29 @@ jabber_update_presence(jabber_presence_t status, const char * const msg) { jabber_conn.presence = status; - xmpp_stanza_t *pres, *show; - - pres = xmpp_stanza_new(jabber_conn.ctx); - xmpp_stanza_set_name(pres, STANZA_NAME_PRESENCE); - - if (status != PRESENCE_ONLINE) { - show = xmpp_stanza_new(jabber_conn.ctx); - xmpp_stanza_set_name(show, STANZA_NAME_SHOW); - xmpp_stanza_t *text = xmpp_stanza_new(jabber_conn.ctx); - - if (status == PRESENCE_AWAY) - xmpp_stanza_set_text(text, STANZA_TEXT_AWAY); - else if (status == PRESENCE_DND) - xmpp_stanza_set_text(text, STANZA_TEXT_DND); - else if (status == PRESENCE_CHAT) - xmpp_stanza_set_text(text, STANZA_TEXT_CHAT); - else if (status == PRESENCE_XA) - xmpp_stanza_set_text(text, STANZA_TEXT_XA); - else - xmpp_stanza_set_text(text, STANZA_TEXT_ONLINE); - - xmpp_stanza_add_child(show, text); - xmpp_stanza_add_child(pres, show); - xmpp_stanza_release(text); - xmpp_stanza_release(show); + char *show = NULL; + switch(status) + { + case PRESENCE_AWAY: + show = STANZA_TEXT_AWAY; + break; + case PRESENCE_DND: + show = STANZA_TEXT_DND; + break; + case PRESENCE_CHAT: + show = STANZA_TEXT_CHAT; + break; + case PRESENCE_XA: + show = STANZA_TEXT_XA; + break; + default: + show = STANZA_TEXT_ONLINE; + break; } - if (msg != NULL) { - xmpp_stanza_t *status = xmpp_stanza_new(jabber_conn.ctx); - xmpp_stanza_set_name(status, STANZA_NAME_STATUS); - xmpp_stanza_t *text = xmpp_stanza_new(jabber_conn.ctx); - - xmpp_stanza_set_text(text, msg); - - xmpp_stanza_add_child(status, text); - xmpp_stanza_add_child(pres, status); - xmpp_stanza_release(text); - xmpp_stanza_release(status); - } - - xmpp_send(jabber_conn.conn, pres); - xmpp_stanza_release(pres); + xmpp_stanza_t *presence = stanza_create_presence(jabber_conn.ctx, show, msg); + xmpp_send(jabber_conn.conn, presence); + xmpp_stanza_release(presence); } jabber_conn_status_t diff --git a/src/stanza.c b/src/stanza.c index 4fc4479a..23b54992 100644 --- a/src/stanza.c +++ b/src/stanza.c @@ -20,6 +20,8 @@ * */ +#include + #include #include "common.h" @@ -116,3 +118,35 @@ stanza_create_room_leave(xmpp_ctx_t *ctx, const char * const room, return presence; } + +xmpp_stanza_t * +stanza_create_presence(xmpp_ctx_t *ctx, const char * const show, + const char * const status) +{ + xmpp_stanza_t *presence = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(presence, STANZA_NAME_PRESENCE); + + if (strcmp(show, STANZA_TEXT_ONLINE) != 0) { + xmpp_stanza_t *show_stanza = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(show_stanza, STANZA_NAME_SHOW); + xmpp_stanza_t *text = xmpp_stanza_new(ctx); + xmpp_stanza_set_text(text, show); + xmpp_stanza_add_child(show_stanza, text); + xmpp_stanza_add_child(presence, show_stanza); + xmpp_stanza_release(text); + xmpp_stanza_release(show_stanza); + } + + if (status != NULL) { + xmpp_stanza_t *status_stanza = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(status_stanza, STANZA_NAME_STATUS); + xmpp_stanza_t *text = xmpp_stanza_new(ctx); + xmpp_stanza_set_text(text, status); + xmpp_stanza_add_child(status_stanza, text); + xmpp_stanza_add_child(presence, status_stanza); + xmpp_stanza_release(text); + xmpp_stanza_release(status_stanza); + } + + return presence; +} diff --git a/src/stanza.h b/src/stanza.h index 98ca7bd9..18332364 100644 --- a/src/stanza.h +++ b/src/stanza.h @@ -81,4 +81,8 @@ xmpp_stanza_t* stanza_create_room_presence(xmpp_ctx_t *ctx, xmpp_stanza_t* stanza_create_room_leave(xmpp_ctx_t *ctx, const char * const room, const char * const nick); +xmpp_stanza_t * +stanza_create_presence(xmpp_ctx_t *ctx, const char * const show, + const char * const status); + #endif