about summary refs log tree commit diff stats
path: root/src/xmpp
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2013-03-17 23:04:36 +0000
committerJames Booth <boothj5@gmail.com>2013-03-17 23:04:36 +0000
commit1c8cba352e5f53e2670134edcee61060da1faebe (patch)
treedd11a1a2fbdafc02029175d13ca3a746a83a2eb9 /src/xmpp
parent8d2e0656b497d2e72ed9df15303f3ba692bdfd42 (diff)
downloadprofani-tty-1c8cba352e5f53e2670134edcee61060da1faebe.tar.gz
Refactor getting caps_key
Diffstat (limited to 'src/xmpp')
-rw-r--r--src/xmpp/presence.c146
1 files changed, 60 insertions, 86 deletions
diff --git a/src/xmpp/presence.c b/src/xmpp/presence.c
index 55fc9189..ea7c51b6 100644
--- a/src/xmpp/presence.c
+++ b/src/xmpp/presence.c
@@ -58,6 +58,7 @@ static int _room_presence_handler(xmpp_conn_t * const conn,
 
 static char* _get_caps_key(xmpp_stanza_t * const stanza);
 static void _send_room_presence(xmpp_conn_t *conn, xmpp_stanza_t *presence);
+void _send_caps_request(char *node, char *caps_key, char *id, char *from);
 
 void
 presence_init(void)
@@ -382,10 +383,14 @@ _available_handler(xmpp_conn_t * const conn,
 
     char *show_str = stanza_get_show(stanza, "online");
     char *status_str = stanza_get_status(stanza, NULL);
-    char *caps_key = _get_caps_key(stanza);
     int idle_seconds = stanza_get_idle_time(stanza);
     GDateTime *last_activity = NULL;
 
+    char *caps_key = NULL;
+    if (stanza_contains_caps(stanza)) {
+        caps_key = _get_caps_key(stanza);
+    }
+
     if (idle_seconds > 0) {
         GDateTime *now = g_date_time_new_now_local();
         last_activity = g_date_time_add_seconds(now, 0 - idle_seconds);
@@ -431,103 +436,68 @@ _available_handler(xmpp_conn_t * const conn,
     return 1;
 }
 
+void
+_send_caps_request(char *node, char *caps_key, char *id, char *from)
+{
+    xmpp_ctx_t *ctx = connection_get_ctx();
+    xmpp_conn_t *conn = connection_get_conn();
+
+    if (node != NULL) {
+        log_debug("Node string: %s.", node);
+        if (!caps_contains(caps_key)) {
+            log_debug("Capabilities not cached for '%s', sending discovery IQ.", from);
+            xmpp_stanza_t *iq = stanza_create_disco_info_iq(ctx, id, from, node);
+            xmpp_send(conn, iq);
+            xmpp_stanza_release(iq);
+        } else {
+            log_debug("Capabilities already cached, for %s", caps_key);
+        }
+    } else {
+        log_debug("No node string, not sending discovery IQ.");
+    }
+}
 
 static char *
 _get_caps_key(xmpp_stanza_t * const stanza)
 {
-    xmpp_ctx_t *ctx = connection_get_ctx();
-    xmpp_conn_t *conn = connection_get_conn();
-    char *caps_key = NULL;
-    char *node = NULL;
     char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
+    char *hash_type = stanza_caps_get_hash(stanza);
+    char *node = stanza_get_caps_str(stanza);
+    char *caps_key = NULL;
+    char *id = NULL;
 
-    if (stanza_contains_caps(stanza)) {
-        log_debug("Presence contains capabilities.");
-        char *hash_type = stanza_caps_get_hash(stanza);
-
-        // xep-0115
-        if (hash_type != NULL) {
-            log_debug("Hash type: %s", hash_type);
-
-            // supported hash
-            if (strcmp(hash_type, "sha-1") == 0) {
-                log_debug("Hash type supported.");
-                node = stanza_get_caps_str(stanza);
-                caps_key = node;
-
-                if (node != NULL) {
-                    log_debug("Node string: %s.", node);
-                    if (!caps_contains(caps_key)) {
-                        log_debug("Capabilities not cached for '%s', sending discovery IQ.", caps_key);
-                        xmpp_stanza_t *iq = stanza_create_disco_info_iq(ctx, "capsreq", from, node);
-                        xmpp_send(conn, iq);
-                        xmpp_stanza_release(iq);
-                    } else {
-                        log_debug("Capabilities already cached, for %s", caps_key);
-                    }
-                } else {
-                    log_debug("No node string, not sending discovery IQ.");
-                }
+    log_debug("Presence contains capabilities.");
 
-            // unsupported hash
-            } else {
-                log_debug("Hash type unsupported.");
-                node = stanza_get_caps_str(stanza);
-                guint from_hash = g_str_hash(from);
-                char from_hash_str[9];
-                g_sprintf(from_hash_str, "%08x", from_hash);
-                caps_key = strdup(from_hash_str);
-
-                if (node != NULL) {
-                    log_debug("Node string: %s.", node);
-                    if (!caps_contains(caps_key)) {
-                        log_debug("Capabilities not cached for '%s', sending discovery IQ.", from);
-                        GString *id = g_string_new("capsreq_");
-                        g_string_append(id, from_hash_str);
-                        xmpp_stanza_t *iq = stanza_create_disco_info_iq(ctx, id->str, from, node);
-                        xmpp_send(conn, iq);
-                        xmpp_stanza_release(iq);
-                        g_string_free(id, TRUE);
-                    } else {
-                        log_debug("Capabilities already cached, for %s", from);
-                    }
-                } else {
-                    log_debug("No node string, not sending discovery IQ.");
-                }
-            }
+    // xep-0115
+    if ((hash_type != NULL) && (strcmp(hash_type, "sha-1") == 0)) {
+        log_debug("Hash type %s supported.", hash_type);
+        caps_key = strdup(node);
+        id = "capsreq";
 
-            return strdup(caps_key);
+        _send_caps_request(node, caps_key, id, from);
 
-        //ignore or handle legacy caps
+    // unsupported hash or legacy capabilities
+    } else {
+        if (hash_type != NULL) {
+            log_debug("Hash type %s unsupported.", hash_type);
         } else {
             log_debug("No hash type, using legacy capabilities.");
-            node = stanza_get_caps_str(stanza);
-            guint from_hash = g_str_hash(from);
-            char from_hash_str[9];
-            g_sprintf(from_hash_str, "%08x", from_hash);
-            caps_key = strdup(from_hash_str);
-
-            if (node != NULL) {
-                log_debug("Node string: %s.", node);
-                if (!caps_contains(caps_key)) {
-                    log_debug("Capabilities not cached for '%s', sending discovery IQ.", from);
-                    GString *id = g_string_new("capsreq_");
-                    g_string_append(id, from_hash_str);
-                    xmpp_stanza_t *iq = stanza_create_disco_info_iq(ctx, id->str, from, node);
-                    xmpp_send(conn, iq);
-                    xmpp_stanza_release(iq);
-                    g_string_free(id, TRUE);
-                } else {
-                    log_debug("Capabilities already cached, for %s", from);
-                }
-            } else {
-                log_debug("No node string, not sending discovery IQ.");
-            }
-
-            return caps_key;
         }
+
+        guint from_hash = g_str_hash(from);
+        char from_hash_str[9];
+        g_sprintf(from_hash_str, "%08x", from_hash);
+        caps_key = strdup(from_hash_str);
+        GString *id_str = g_string_new("capsreq_");
+        g_string_append(id_str, from_hash_str);
+        id = id_str->str;
+
+        _send_caps_request(node, caps_key, id, from);
+
+        g_string_free(id_str, TRUE);
     }
-    return NULL;
+
+    return caps_key;
 }
 
 static int
@@ -576,7 +546,11 @@ _room_presence_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
     } else {
         char *type = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_TYPE);
         char *show_str, *status_str;
-        char *caps_key = _get_caps_key(stanza);
+
+        char *caps_key = NULL;
+        if (stanza_contains_caps(stanza)) {
+            caps_key = _get_caps_key(stanza);
+        }
 
         log_debug("Room presence received from %s", from_jid->fulljid);