diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/jabber.c | 61 | ||||
-rw-r--r-- | src/stanza.c | 34 | ||||
-rw-r--r-- | 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); - } - - 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); + 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; } - 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 <string.h> + #include <strophe.h> #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 |