1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Tidied up memory allocation in capabilities

This commit is contained in:
James Booth 2013-01-23 22:24:54 +00:00
parent a48b48b787
commit e2e805dd4f
5 changed files with 43 additions and 19 deletions

View File

@ -70,7 +70,7 @@ caps_get(const char * const caps_str)
}
char *
caps_get_sha1_str(xmpp_stanza_t *query)
caps_create_sha1_str(xmpp_stanza_t * const query)
{
char *category = NULL;
char *type = NULL;
@ -82,17 +82,17 @@ caps_get_sha1_str(xmpp_stanza_t *query)
GSList *form_names = NULL;
DataForm *form = NULL;
FormField *field = NULL;
GHashTable *forms = g_hash_table_new(g_str_hash, g_str_equal);
GHashTable *forms = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)stanza_destroy_form);
GString *s = g_string_new("");
xmpp_stanza_t *child = xmpp_stanza_get_children(query);
while (child != NULL) {
if (g_strcmp0(xmpp_stanza_get_name(child), STANZA_NAME_IDENTITY) == 0) {
category = g_strdup(xmpp_stanza_get_attribute(child, "category"));
type = g_strdup(xmpp_stanza_get_attribute(child, "type"));
lang = g_strdup(xmpp_stanza_get_attribute(child, "xml:lang"));
name = g_strdup(xmpp_stanza_get_attribute(child, "name"));
category = xmpp_stanza_get_attribute(child, "category");
type = xmpp_stanza_get_attribute(child, "type");
lang = xmpp_stanza_get_attribute(child, "xml:lang");
name = xmpp_stanza_get_attribute(child, "name");
GString *identity_str = g_string_new(g_strdup(category));
g_string_append(identity_str, "/");
@ -109,15 +109,10 @@ caps_get_sha1_str(xmpp_stanza_t *query)
}
g_string_append(identity_str, "<");
identities = g_slist_insert_sorted(identities, g_strdup(identity_str->str), (GCompareFunc)octet_compare);
GFREE_SET_NULL(category);
GFREE_SET_NULL(type);
GFREE_SET_NULL(lang);
GFREE_SET_NULL(name);
g_string_free(identity_str, TRUE);
} else if (g_strcmp0(xmpp_stanza_get_name(child), STANZA_NAME_FEATURE) == 0) {
feature_str = g_strdup(xmpp_stanza_get_attribute(child, "var"));
feature_str = xmpp_stanza_get_attribute(child, "var");
features = g_slist_insert_sorted(features, g_strdup(feature_str), (GCompareFunc)octet_compare);
GFREE_SET_NULL(feature_str);
} else if (g_strcmp0(xmpp_stanza_get_name(child), STANZA_NAME_X) == 0) {
if (strcmp(xmpp_stanza_get_ns(child), STANZA_NS_DATA) == 0) {
form = stanza_create_form(child);
@ -179,12 +174,14 @@ caps_get_sha1_str(xmpp_stanza_t *query)
g_string_free(s, TRUE);
g_slist_free_full(identities, free);
g_slist_free_full(features, free);
g_slist_free_full(form_names, free);
g_hash_table_destroy(forms);
return result;
}
xmpp_stanza_t *
caps_get_query_response_stanza(xmpp_ctx_t *ctx)
caps_create_query_response_stanza(xmpp_ctx_t * const ctx)
{
xmpp_stanza_t *query = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(query, STANZA_NAME_QUERY);
@ -224,6 +221,12 @@ caps_get_query_response_stanza(xmpp_ctx_t *ctx)
xmpp_stanza_add_child(query, feature_caps);
xmpp_stanza_add_child(query, feature_version);
xmpp_stanza_release(identity);
xmpp_stanza_release(feature_muc);
xmpp_stanza_release(feature_discoinfo);
xmpp_stanza_release(feature_caps);
xmpp_stanza_release(feature_version);
return query;
}

View File

@ -34,8 +34,8 @@ void caps_init(void);
void caps_add(const char * const caps_str, const char * const client);
gboolean caps_contains(const char * const caps_str);
Capabilities* caps_get(const char * const caps_str);
char* caps_get_sha1_str(xmpp_stanza_t * const query);
xmpp_stanza_t* caps_get_query_response_stanza(xmpp_ctx_t *ctx);
char* caps_create_sha1_str(xmpp_stanza_t * const query);
xmpp_stanza_t* caps_create_query_response_stanza(xmpp_ctx_t * const ctx);
void caps_close(void);
#endif

View File

@ -428,8 +428,8 @@ jabber_update_presence(jabber_presence_t status, const char * const msg,
xmpp_stanza_set_ns(caps, STANZA_NS_CAPS);
xmpp_stanza_set_attribute(caps, STANZA_ATTR_HASH, "sha-1");
xmpp_stanza_set_attribute(caps, STANZA_ATTR_NODE, "http://www.profanity.im");
xmpp_stanza_t *query = caps_get_query_response_stanza(jabber_conn.ctx);
char *sha1 = caps_get_sha1_str(query);
xmpp_stanza_t *query = caps_create_query_response_stanza(jabber_conn.ctx);
char *sha1 = caps_create_sha1_str(query);
xmpp_stanza_set_attribute(caps, STANZA_ATTR_VER, sha1);
xmpp_stanza_add_child(presence, caps);
xmpp_send(jabber_conn.conn, presence);
@ -1015,7 +1015,7 @@ _disco_request_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
xmpp_stanza_set_id(response, xmpp_stanza_get_id(stanza));
xmpp_stanza_set_attribute(response, STANZA_ATTR_TO, from);
xmpp_stanza_set_type(response, STANZA_TYPE_RESULT);
xmpp_stanza_t *query = caps_get_query_response_stanza(ctx);
xmpp_stanza_t *query = caps_create_query_response_stanza(ctx);
xmpp_stanza_set_attribute(query, STANZA_ATTR_NODE, node_str);
xmpp_stanza_add_child(response, query);
xmpp_send(conn, response);
@ -1050,7 +1050,7 @@ _disco_response_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
// validate sha1
gchar **split = g_strsplit(node, "#", -1);
char *given_sha1 = split[1];
char *generated_sha1 = caps_get_sha1_str(query);
char *generated_sha1 = caps_create_sha1_str(query);
if (g_strcmp0(given_sha1, generated_sha1) != 0) {
log_info("Invalid SHA1 recieved for caps.");

View File

@ -581,6 +581,26 @@ stanza_create_form(xmpp_stanza_t * const stanza)
return result;
}
void
stanza_destroy_form(DataForm *form)
{
if (form != NULL) {
FREE_SET_NULL(form->form_type);
if (form->fields != NULL) {
GSList *curr_field = form->fields;
while (curr_field != NULL) {
FormField *field = curr_field->data;
FREE_SET_NULL(field->var);
if ((field->values) != NULL) {
g_slist_free_full(field->values, free);
}
}
}
form = NULL;
}
}
static int
_field_compare(FormField *f1, FormField *f2)
{

View File

@ -148,5 +148,6 @@ gboolean stanza_is_caps_request(xmpp_stanza_t * const stanza);
gboolean stanza_is_version_request(xmpp_stanza_t * const stanza);
DataForm * stanza_create_form(xmpp_stanza_t * const stanza);
void stanza_destroy_form(DataForm *form);
#endif