about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2013-01-20 17:16:45 +0000
committerJames Booth <boothj5@gmail.com>2013-01-20 17:16:45 +0000
commit64d81c7c4c07f527e76a64477a5b21a1385dbfef (patch)
treea200c3a4868fcdb46245d39dd055f56e0f2ca16d
parent4ecb69bfab47b5c08f8b5fca97008fe10abc96ba (diff)
downloadprofani-tty-64d81c7c4c07f527e76a64477a5b21a1385dbfef.tar.gz
Started work on creating sha-1 caps hash
-rw-r--r--src/common.c26
-rw-r--r--src/common.h1
-rw-r--r--src/jabber.c70
-rw-r--r--src/jabber.h5
-rw-r--r--src/stanza.h2
5 files changed, 102 insertions, 2 deletions
diff --git a/src/common.c b/src/common.c
index d79bff64..f0e14f45 100644
--- a/src/common.c
+++ b/src/common.c
@@ -167,3 +167,29 @@ prof_getline(FILE *stream)
     free(buf);
     return s;
 }
+
+int
+octet_compare(unsigned char *str1, unsigned char *str2)
+{
+    if ((strcmp((char *)str1, "") == 0) && (strcmp((char *)str2, "") == 0)) {
+        return 0;
+    }
+
+    if ((strcmp((char *)str1, "") == 0) && (strcmp((char *)str2, "") != 0)) {
+        return -1;
+    }
+
+    if ((strcmp((char *)str1, "") != 0) && (strcmp((char *)str2, "") == 0)) {
+        return 1;
+    }
+
+    if (str1[0] == str2[0]) {
+        return octet_compare(&str1[1], &str2[1]);
+    }
+
+    if (str1[0] < str2[0]) {
+        return -1;
+    }
+
+    return 1;
+}
diff --git a/src/common.h b/src/common.h
index e12045ad..5c615256 100644
--- a/src/common.h
+++ b/src/common.h
@@ -51,5 +51,6 @@ char * str_replace(const char *string, const char *substr,
 int str_contains(char str[], int size, char ch);
 char* encode_xml(const char * const xml);
 char * prof_getline(FILE *stream);
+int octet_compare(unsigned char *str1, unsigned char *str2);
 
 #endif
diff --git a/src/jabber.c b/src/jabber.c
index f6f104b8..353247f4 100644
--- a/src/jabber.c
+++ b/src/jabber.c
@@ -181,7 +181,7 @@ jabber_disconnect(void)
 }
 
 char *
-jabber_get_sha1_caps_str(void)
+jabber_get_local_sha1_caps_str(void)
 {
     GString *str = g_string_new("");
     unsigned char hash[SHA_DIGEST_LENGTH];
@@ -202,6 +202,74 @@ jabber_get_sha1_caps_str(void)
     return result;
 }
 
+
+char *
+sha1_caps_str(xmpp_stanza_t *query)
+{
+    GSList *identities = NULL;
+    GSList *features = NULL;
+
+    GString *s = g_string_new("");
+    unsigned char hash[SHA_DIGEST_LENGTH];
+
+    xmpp_stanza_t *child = xmpp_stanza_get_children(query);
+    while (child != NULL) {
+        if (strcmp(xmpp_stanza_get_name(child), STANZA_NAME_IDENTITY) == 0) {
+            char *category = xmpp_stanza_get_attribute(child, "category");
+            char *type = xmpp_stanza_get_attribute(child, "type");
+            char *lang = xmpp_stanza_get_attribute(child, "xml:lang");
+            char *name = xmpp_stanza_get_attribute(child, "name");
+
+            GString *identity_str = g_string_new(category);
+            g_string_append(identity_str, "/");
+            if (type != NULL) {
+                g_string_append(identity_str, type);
+            }
+            g_string_append(identity_str, "/");
+            if (lang != NULL) {
+                g_string_append(identity_str, lang);
+            }
+            g_string_append(identity_str, "/");
+            if (name != NULL) {
+                g_string_append(identity_str, name);
+            }
+            g_string_append(identity_str, "<");
+            identities = g_slist_insert_sorted(identities, identity_str, (GCompareFunc)octet_compare);
+            g_string_free(identity_str, TRUE);
+        } else if (strcmp(xmpp_stanza_get_name(child), STANZA_NAME_FEATURE) == 0) {
+            char *feature_str = xmpp_stanza_get_attribute(child, "var");
+            features = g_slist_insert_sorted(features, feature_str, (GCompareFunc)octet_compare);
+        } else if (strcmp(xmpp_stanza_get_name(child), STANZA_NAME_X) == 0) {
+            // check for and add form to string
+        }
+        child = xmpp_stanza_get_next(child);
+    }
+
+    GSList *curr = identities;
+    while (curr != NULL) {
+        g_string_append(s, curr->data);
+        g_string_append(s, "<");
+        curr = g_slist_next(curr);
+    }
+
+    curr = features;
+    while (curr != NULL) {
+        g_string_append(s, curr->data);
+        g_string_append(s, "<");
+        curr = g_slist_next(curr);
+    }
+
+    SHA1((unsigned char *)s->str, strlen(s->str), hash);
+
+    char *result = g_base64_encode(hash, strlen((char *)hash));
+
+    g_string_free(s, TRUE);
+    g_slist_free_full(identities, free);
+    g_slist_free_full(features, free);
+
+    return result;
+}
+
 void
 jabber_process_events(void)
 {
diff --git a/src/jabber.h b/src/jabber.h
index e1fc04f0..73817841 100644
--- a/src/jabber.h
+++ b/src/jabber.h
@@ -23,6 +23,8 @@
 #ifndef JABBER_H
 #define JABBER_H
 
+#include <strophe.h>
+
 #include "accounts.h"
 #include "jid.h"
 
@@ -81,6 +83,7 @@ char * jabber_get_status(void);
 void jabber_free_resources(void);
 void jabber_restart(void);
 void jabber_set_autoping(int seconds);
-char * jabber_get_sha1_caps_str(void);
+char * jabber_get_local_sha1_caps_str(void);
+char * sha1_caps_str(xmpp_stanza_t *query);
 
 #endif
diff --git a/src/stanza.h b/src/stanza.h
index adebc37d..4522e3e3 100644
--- a/src/stanza.h
+++ b/src/stanza.h
@@ -48,6 +48,8 @@
 #define STANZA_NAME_SUBJECT "subject"
 #define STANZA_NAME_ITEM "item"
 #define STANZA_NAME_C "c"
+#define STANZA_NAME_IDENTITY "identity"
+#define STANZA_NAME_FEATURE "feature"
 
 #define STANZA_TYPE_CHAT "chat"
 #define STANZA_TYPE_GROUPCHAT "groupchat"