about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2012-11-29 22:33:47 +0000
committerJames Booth <boothj5@gmail.com>2012-11-29 22:33:47 +0000
commit5496b25735e8fad016250b980b9efd348a5909e0 (patch)
tree78e667eff6eae2e67716e026bed16cb482daa875
parent23e4ba73d4bf6d9ca0ab686528d8f329ccf012ab (diff)
downloadprofani-tty-5496b25735e8fad016250b980b9efd348a5909e0.tar.gz
Respond to ping requests
-rw-r--r--src/jabber.c99
-rw-r--r--src/stanza.h2
2 files changed, 68 insertions, 33 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) {
-            return TRUE;
-        }
+        // handle roster update
+        if (strcmp(type, STANZA_TYPE_SET) == 0) {
 
-        xmpp_stanza_t *query =
-            xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY);
+            xmpp_stanza_t *query =
+                xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY);
+            if (query == NULL) {
+                return TRUE;
+            }
 
-        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;
+            }
 
-        char *xmlns = xmpp_stanza_get_attribute(query, STANZA_ATTR_XMLNS);
+            xmpp_stanza_t *item =
+                xmpp_stanza_get_child_by_name(query, STANZA_NAME_ITEM);
+            if (item == NULL) {
+                return TRUE;
+            }
 
-        if (xmlns == 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;
+            }
 
-        if (strcmp(xmlns, XMPP_NS_ROSTER) != 0) {
-            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;
+            }
 
-        xmpp_stanza_t *item =
-            xmpp_stanza_get_child_by_name(query, STANZA_NAME_ITEM);
+            contact_list_update_subscription(jid, sub, pending_out);
 
-        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);
+        // 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;
+            }
 
-        if (g_strcmp0(sub, "remove") == 0) {
-            contact_list_remove(jid);
-            return TRUE;
-        }
+            char *xmlns = xmpp_stanza_get_attribute(ping, STANZA_ATTR_XMLNS);
+            if (xmlns == NULL) {
+                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;
-        }
+            if (strcmp(xmlns, STANZA_NS_PING) != 0) {
+                return TRUE;
+            }
 
-        contact_list_update_subscription(jid, sub, pending_out);
+            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;
+            return TRUE;
+        } else {
+            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"