mirror of
https://github.com/profanity-im/profanity.git
synced 2024-11-03 19:37:16 -05:00
First implementation of sha1 generation from stanza
This commit is contained in:
parent
64d81c7c4c
commit
eed0910916
28
src/jabber.c
28
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));
|
||||
|
54
src/stanza.c
54
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);
|
||||
}
|
||||
|
13
src/stanza.h
13
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
|
||||
|
Loading…
Reference in New Issue
Block a user