From 5496b25735e8fad016250b980b9efd348a5909e0 Mon Sep 17 00:00:00 2001 From: James Booth Date: Thu, 29 Nov 2012 22:33:47 +0000 Subject: [PATCH] Respond to ping requests --- src/jabber.c | 121 ++++++++++++++++++++++++++++++++------------------- src/stanza.h | 2 + 2 files changed, 79 insertions(+), 44 deletions(-) diff --git a/src/jabber.c b/src/jabber.c index 8ade22be..654db520 100644 --- a/src/jabber.c +++ b/src/jabber.c @@ -765,58 +765,91 @@ _iq_handler(xmpp_conn_t * const conn, if ((id != NULL) && (strcmp(id, "roster") == 0)) { return _roster_handler(conn, stanza, userdata); - // handle roster updates + // handle iq } else { char *type = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_TYPE); if (type == NULL) { return TRUE; } - if (strcmp(type, "set") != 0) { + // handle roster update + if (strcmp(type, STANZA_TYPE_SET) == 0) { + + xmpp_stanza_t *query = + xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY); + if (query == NULL) { + return TRUE; + } + + char *xmlns = xmpp_stanza_get_attribute(query, STANZA_ATTR_XMLNS); + if (xmlns == NULL) { + return TRUE; + } + if (strcmp(xmlns, XMPP_NS_ROSTER) != 0) { + return TRUE; + } + + xmpp_stanza_t *item = + xmpp_stanza_get_child_by_name(query, STANZA_NAME_ITEM); + if (item == NULL) { + return TRUE; + } + + const char *jid = xmpp_stanza_get_attribute(item, STANZA_ATTR_JID); + const char *sub = xmpp_stanza_get_attribute(item, STANZA_ATTR_SUBSCRIPTION); + if (g_strcmp0(sub, "remove") == 0) { + contact_list_remove(jid); + return TRUE; + } + + gboolean pending_out = FALSE; + const char *ask = xmpp_stanza_get_attribute(item, STANZA_ATTR_ASK); + if ((ask != NULL) && (strcmp(ask, "subscribe") == 0)) { + pending_out = TRUE; + } + + contact_list_update_subscription(jid, sub, pending_out); + + return TRUE; + + // handle server ping + } else if (strcmp(type, STANZA_TYPE_GET) == 0) { + xmpp_stanza_t *ping = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_PING); + if (ping == NULL) { + return TRUE; + } + + char *xmlns = xmpp_stanza_get_attribute(ping, STANZA_ATTR_XMLNS); + if (xmlns == NULL) { + return TRUE; + } + + if (strcmp(xmlns, STANZA_NS_PING) != 0) { + return TRUE; + } + + char *to = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_TO); + char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); + if ((from == NULL) || (to == NULL)) { + return TRUE; + } + + xmpp_stanza_t *pong = xmpp_stanza_new(jabber_conn.ctx); + xmpp_stanza_set_name(pong, STANZA_NAME_IQ); + xmpp_stanza_set_attribute(pong, STANZA_ATTR_TO, from); + xmpp_stanza_set_attribute(pong, STANZA_ATTR_FROM, to); + xmpp_stanza_set_attribute(pong, STANZA_ATTR_TYPE, STANZA_TYPE_RESULT); + if (id != NULL) { + xmpp_stanza_set_attribute(pong, STANZA_ATTR_ID, id); + } + + xmpp_send(jabber_conn.conn, pong); + xmpp_stanza_release(pong); + + return TRUE; + } else { return TRUE; } - - xmpp_stanza_t *query = - xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY); - - if (query == NULL) { - return TRUE; - } - - char *xmlns = xmpp_stanza_get_attribute(query, STANZA_ATTR_XMLNS); - - if (xmlns == NULL) { - return TRUE; - } - - if (strcmp(xmlns, XMPP_NS_ROSTER) != 0) { - return TRUE; - } - - xmpp_stanza_t *item = - xmpp_stanza_get_child_by_name(query, STANZA_NAME_ITEM); - - if (item == NULL) { - return TRUE; - } - - const char *jid = xmpp_stanza_get_attribute(item, STANZA_ATTR_JID); - const char *sub = xmpp_stanza_get_attribute(item, STANZA_ATTR_SUBSCRIPTION); - - if (g_strcmp0(sub, "remove") == 0) { - contact_list_remove(jid); - return TRUE; - } - - gboolean pending_out = FALSE; - const char *ask = xmpp_stanza_get_attribute(item, STANZA_ATTR_ASK); - if ((ask != NULL) && (strcmp(ask, "subscribe") == 0)) { - pending_out = TRUE; - } - - contact_list_update_subscription(jid, sub, pending_out); - - return TRUE; } } diff --git a/src/stanza.h b/src/stanza.h index 50d5e4a9..d31ff519 100644 --- a/src/stanza.h +++ b/src/stanza.h @@ -55,7 +55,9 @@ #define STANZA_TYPE_SUBSCRIBED "subscribed" #define STANZA_TYPE_UNSUBSCRIBED "unsubscribed" #define STANZA_TYPE_GET "get" +#define STANZA_TYPE_SET "set" #define STANZA_TYPE_ERROR "error" +#define STANZA_TYPE_RESULT "result" #define STANZA_ATTR_TO "to" #define STANZA_ATTR_FROM "from"