about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2013-01-20 18:23:29 +0000
committerJames Booth <boothj5@gmail.com>2013-01-20 18:23:29 +0000
commiteed09109164bf05b6a3d40604205ce5cc2cfbde1 (patch)
treec1499137d5894ea3628eb245e5ae76d40510aa9d
parent64d81c7c4c07f527e76a64477a5b21a1385dbfef (diff)
downloadprofani-tty-eed09109164bf05b6a3d40604205ce5cc2cfbde1.tar.gz
First implementation of sha1 generation from stanza
-rw-r--r--src/jabber.c28
-rw-r--r--src/stanza.c54
-rw-r--r--src/stanza.h13
3 files changed, 94 insertions, 1 deletions
diff --git a/src/jabber.c b/src/jabber.c
index 353247f4..f5cb9d04 100644
--- a/src/jabber.c
+++ b/src/jabber.c
@@ -208,6 +208,8 @@ sha1_caps_str(xmpp_stanza_t *query)
 {
     GSList *identities = NULL;
     GSList *features = NULL;
+    GSList *form_names = NULL;
+    GHashTable *forms = g_hash_table_new(g_str_hash, g_str_equal);
 
     GString *s = g_string_new("");
     unsigned char hash[SHA_DIGEST_LENGTH];
@@ -240,7 +242,11 @@ sha1_caps_str(xmpp_stanza_t *query)
             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
+            if (strcmp(xmpp_stanza_get_ns(child), STANZA_NS_DATA) == 0) {
+                DataForm *form = stanza_get_form(child);
+                form_names = g_slist_insert_sorted(form_names, form->form_type, (GCompareFunc)octet_compare);
+                g_hash_table_insert(forms, form->form_type, form);
+            }
         }
         child = xmpp_stanza_get_next(child);
     }
@@ -259,6 +265,26 @@ sha1_caps_str(xmpp_stanza_t *query)
         curr = g_slist_next(curr);
     }
 
+    curr = form_names;
+    while (curr != NULL) {
+        DataForm *form = g_hash_table_lookup(forms, curr->data);
+        g_string_append(s, form->form_type);
+        g_string_append(s, "<");
+
+        GSList *curr_field = form->fields;
+        while (curr_field != NULL) {
+            FormField *field = curr_field->data;
+            g_string_append(s, field->var);
+            GSList *curr_value = field->values;
+            while (curr_value != NULL) {
+                g_string_append(s, curr_value->data);
+                g_string_append(s, "<");
+                curr_value = g_slist_next(curr_value);
+            }
+            curr_field = g_slist_next(curr_value);
+        }
+    }
+
     SHA1((unsigned char *)s->str, strlen(s->str), hash);
 
     char *result = g_base64_encode(hash, strlen((char *)hash));
diff --git a/src/stanza.c b/src/stanza.c
index ea592127..e27aa40d 100644
--- a/src/stanza.c
+++ b/src/stanza.c
@@ -29,6 +29,8 @@
 #include "common.h"
 #include "stanza.h"
 
+static int _field_compare(FormField *f1, FormField *f2);
+
 xmpp_stanza_t *
 stanza_create_chat_state(xmpp_ctx_t *ctx, const char * const recipient,
     const char * const state)
@@ -441,3 +443,55 @@ stanza_get_caps_str(xmpp_stanza_t * const stanza)
 
     return  caps_str;
 }
+
+DataForm *
+stanza_get_form(xmpp_stanza_t * const stanza)
+{
+    DataForm *result = NULL;
+
+    xmpp_stanza_t *child = xmpp_stanza_get_children(stanza);
+
+    if (child != NULL) {
+        result = malloc(sizeof(struct data_form_t));
+        result->form_type = NULL;
+        result->fields = NULL;
+    }
+
+    //handle fields
+    while (child != NULL) {
+        char *var = xmpp_stanza_get_attribute(child, "var");
+
+        // handle FORM_TYPE
+        if (g_strcmp0(var, "FORM_TYPE")) {
+            xmpp_stanza_t *value = xmpp_stanza_get_child_by_name(child, "value");
+            char *value_text = xmpp_stanza_get_text(value);
+            result->form_type = strdup(value_text);
+
+        // handle regular fields
+        } else {
+            FormField *field = malloc(sizeof(struct form_field_t));
+            field->var = strdup(var);
+            field->values = NULL;
+            xmpp_stanza_t *value = xmpp_stanza_get_children(child);
+
+            // handle values
+            while (value != NULL) {
+                char *text = xmpp_stanza_get_text(value);
+                field->values = g_slist_insert_sorted(field->values, text, (GCompareFunc)octet_compare);
+                value = xmpp_stanza_get_next(value);
+            }
+
+            result->fields = g_slist_insert_sorted(result->fields, field, (GCompareFunc)_field_compare);
+        }
+
+        child = xmpp_stanza_get_next(child);
+    }
+
+    return result;
+}
+
+static int
+_field_compare(FormField *f1, FormField *f2)
+{
+    return octet_compare((unsigned char *)f1->var, (unsigned char *)f2->var);
+}
diff --git a/src/stanza.h b/src/stanza.h
index 4522e3e3..0d9edf07 100644
--- a/src/stanza.h
+++ b/src/stanza.h
@@ -90,6 +90,17 @@
 #define STANZA_NS_CAPS "http://jabber.org/protocol/caps"
 #define STANZA_NS_PING "urn:xmpp:ping"
 #define STANZA_NS_LASTACTIVITY "jabber:iq:last"
+#define STANZA_NS_DATA "jabber:x:data"
+
+typedef struct form_field_t {
+    char *var;
+    GSList *values;
+} FormField;
+
+typedef struct data_form_t {
+    char *form_type;
+    GSList *fields;
+} DataForm;
 
 xmpp_stanza_t* stanza_create_chat_state(xmpp_ctx_t *ctx,
     const char * const recipient, const char * const state);
@@ -128,4 +139,6 @@ char * stanza_get_new_nick(xmpp_stanza_t * const stanza);
 int stanza_get_idle_time(xmpp_stanza_t * const stanza);
 char * stanza_get_caps_str(xmpp_stanza_t * const stanza);
 
+DataForm * stanza_get_form(xmpp_stanza_t * const stanza);
+
 #endif