From 157a1b5ff75d706d5dda11c4208b05daa1552791 Mon Sep 17 00:00:00 2001 From: James Booth Date: Fri, 5 Sep 2014 21:04:16 +0100 Subject: [PATCH] Created form module --- Makefile.am | 1 + src/xmpp/capabilities.c | 5 +- src/xmpp/form.c | 144 ++++++++++++++++++++++++++++++++++++++++ src/xmpp/form.h | 40 +++++++++++ src/xmpp/iq.c | 7 +- src/xmpp/stanza.c | 97 --------------------------- src/xmpp/xmpp.h | 2 + 7 files changed, 194 insertions(+), 102 deletions(-) create mode 100644 src/xmpp/form.c create mode 100644 src/xmpp/form.h diff --git a/Makefile.am b/Makefile.am index 29cf709a..219c1010 100644 --- a/Makefile.am +++ b/Makefile.am @@ -11,6 +11,7 @@ core_sources = \ src/xmpp/capabilities.h src/xmpp/connection.h \ src/xmpp/roster.c src/xmpp/roster.h \ src/xmpp/bookmark.c src/xmpp/bookmark.h \ + src/xmpp/form.c src/xmpp/form.h \ src/server_events.c src/server_events.h \ src/ui/ui.h src/ui/window.c src/ui/window.h src/ui/core.c \ src/ui/titlebar.c src/ui/statusbar.c src/ui/inputwin.c \ diff --git a/src/xmpp/capabilities.c b/src/xmpp/capabilities.c index 2fb9f065..c0c46ab7 100644 --- a/src/xmpp/capabilities.c +++ b/src/xmpp/capabilities.c @@ -47,6 +47,7 @@ #include "common.h" #include "xmpp/xmpp.h" #include "xmpp/stanza.h" +#include "xmpp/form.h" static GHashTable *capabilities; @@ -137,7 +138,7 @@ caps_create_sha1_str(xmpp_stanza_t * const query) GSList *form_names = NULL; DataForm *form = NULL; FormField *field = NULL; - GHashTable *forms = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)stanza_destroy_form); + GHashTable *forms = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)form_destroy); GString *s = g_string_new(""); @@ -170,7 +171,7 @@ caps_create_sha1_str(xmpp_stanza_t * const query) features = g_slist_insert_sorted(features, g_strdup(feature_str), (GCompareFunc)strcmp); } 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); + form = form_create(child); form_names = g_slist_insert_sorted(form_names, g_strdup(form->form_type), (GCompareFunc)strcmp); g_hash_table_insert(forms, g_strdup(form->form_type), form); } diff --git a/src/xmpp/form.c b/src/xmpp/form.c new file mode 100644 index 00000000..237cbdd7 --- /dev/null +++ b/src/xmpp/form.c @@ -0,0 +1,144 @@ +/* + * form.c + * + * Copyright (C) 2012 - 2014 James Booth + * + * This file is part of Profanity. + * + * Profanity is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Profanity is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Profanity. If not, see . + * + * In addition, as a special exception, the copyright holders give permission to + * link the code of portions of this program with the OpenSSL library under + * certain conditions as described in each individual source file, and + * distribute linked combinations including the two. + * + * You must obey the GNU General Public License in all respects for all of the + * code used other than OpenSSL. If you modify file(s) with this exception, you + * may extend this exception to your version of the file(s), but you are not + * obligated to do so. If you do not wish to do so, delete this exception + * statement from your version. If you delete this exception statement from all + * source files in the program, then also delete it here. + * + */ + +#include +#include + +#include + +#include "xmpp/xmpp.h" +#include "xmpp/connection.h" + +static int _field_compare(FormField *f1, FormField *f2); + +DataForm * +form_create(xmpp_stanza_t * const stanza) +{ + DataForm *result = NULL; + xmpp_ctx_t *ctx = connection_get_ctx(); + + xmpp_stanza_t *child = xmpp_stanza_get_children(stanza); + + if (child != NULL) { + result = malloc(sizeof(DataForm)); + result->form_type = NULL; + result->fields = NULL; + } + + //handle fields + while (child != NULL) { + char *label = xmpp_stanza_get_attribute(child, "label"); + char *type = xmpp_stanza_get_attribute(child, "type"); + char *var = xmpp_stanza_get_attribute(child, "var"); + + // handle FORM_TYPE + if (g_strcmp0(var, "FORM_TYPE") == 0) { + xmpp_stanza_t *value = xmpp_stanza_get_child_by_name(child, "value"); + char *value_text = xmpp_stanza_get_text(value); + if (value_text != NULL) { + result->form_type = strdup(value_text); + xmpp_free(ctx, value_text); + } + + // handle regular fields + } else { + FormField *field = malloc(sizeof(FormField)); + field->label = NULL; + field->type = NULL; + field->var = NULL; + + if (label != NULL) { + field->label = strdup(label); + } + if (type != NULL) { + field->type = strdup(type); + } + if (var != NULL) { + field->var = strdup(var); + } + + // handle values + field->values = NULL; + xmpp_stanza_t *value = xmpp_stanza_get_children(child); + while (value != NULL) { + char *text = xmpp_stanza_get_text(value); + if (text != NULL) { + field->values = g_slist_insert_sorted(field->values, strdup(text), (GCompareFunc)strcmp); + xmpp_free(ctx, text); + } + 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; +} + +void +_form_destroy(DataForm *form) +{ + if (form != NULL) { + if (form->fields != NULL) { + GSList *curr_field = form->fields; + while (curr_field != NULL) { + FormField *field = curr_field->data; + free(field->var); + if ((field->values) != NULL) { + g_slist_free_full(field->values, free); + } + curr_field = curr_field->next; + } + g_slist_free_full(form->fields, free); + } + + free(form->form_type); + free(form); + } +} + +static int +_field_compare(FormField *f1, FormField *f2) +{ + return strcmp(f1->var, f2->var); +} + +void +form_init_module(void) +{ + form_destroy = _form_destroy; +} diff --git a/src/xmpp/form.h b/src/xmpp/form.h new file mode 100644 index 00000000..b0c32675 --- /dev/null +++ b/src/xmpp/form.h @@ -0,0 +1,40 @@ +/* + * form.h + * + * Copyright (C) 2012 - 2014 James Booth + * + * This file is part of Profanity. + * + * Profanity is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Profanity is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Profanity. If not, see . + * + * In addition, as a special exception, the copyright holders give permission to + * link the code of portions of this program with the OpenSSL library under + * certain conditions as described in each individual source file, and + * distribute linked combinations including the two. + * + * You must obey the GNU General Public License in all respects for all of the + * code used other than OpenSSL. If you modify file(s) with this exception, you + * may extend this exception to your version of the file(s), but you are not + * obligated to do so. If you do not wish to do so, delete this exception + * statement from your version. If you delete this exception statement from all + * source files in the program, then also delete it here. + * + */ + +#ifndef FORM_H +#define FROM_H + +DataForm* form_create(xmpp_stanza_t * const stanza); + +#endif diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c index ab3a29a0..cff0da1b 100644 --- a/src/xmpp/iq.c +++ b/src/xmpp/iq.c @@ -52,6 +52,7 @@ #include "xmpp/capabilities.h" #include "xmpp/connection.h" #include "xmpp/stanza.h" +#include "xmpp/form.h" #include "roster_list.h" #include "xmpp/xmpp.h" @@ -613,7 +614,7 @@ _room_config_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, return 0; } - DataForm *form = stanza_create_form(x); + DataForm *form = form_create(x); handle_room_configure(from, form); } @@ -766,7 +767,7 @@ _disco_info_result_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanz xmpp_stanza_t *softwareinfo = xmpp_stanza_get_child_by_ns(query, STANZA_NS_DATA); if (softwareinfo != NULL) { - DataForm *form = stanza_create_form(softwareinfo); + DataForm *form = form_create(softwareinfo); FormField *formField = NULL; if (g_strcmp0(form->form_type, STANZA_DATAFORM_SOFTWARE) == 0) { @@ -788,7 +789,7 @@ _disco_info_result_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanz } } - stanza_destroy_form(form); + form_destroy(form); } xmpp_stanza_t *child = xmpp_stanza_get_children(query); diff --git a/src/xmpp/stanza.c b/src/xmpp/stanza.c index e78f5b9f..f3ad2f99 100644 --- a/src/xmpp/stanza.c +++ b/src/xmpp/stanza.c @@ -46,8 +46,6 @@ #include "muc.h" -static int _field_compare(FormField *f1, FormField *f2); - #if 0 xmpp_stanza_t * stanza_create_bookmarks_pubsub_request(xmpp_ctx_t *ctx) @@ -1071,95 +1069,6 @@ stanza_get_error_message(xmpp_stanza_t *stanza) return strdup("unknown"); } -DataForm * -stanza_create_form(xmpp_stanza_t * const stanza) -{ - DataForm *result = NULL; - xmpp_ctx_t *ctx = connection_get_ctx(); - - xmpp_stanza_t *child = xmpp_stanza_get_children(stanza); - - if (child != NULL) { - result = malloc(sizeof(DataForm)); - result->form_type = NULL; - result->fields = NULL; - } - - //handle fields - while (child != NULL) { - char *label = xmpp_stanza_get_attribute(child, "label"); - char *type = xmpp_stanza_get_attribute(child, "type"); - char *var = xmpp_stanza_get_attribute(child, "var"); - - // handle FORM_TYPE - if (g_strcmp0(var, "FORM_TYPE") == 0) { - xmpp_stanza_t *value = xmpp_stanza_get_child_by_name(child, "value"); - char *value_text = xmpp_stanza_get_text(value); - if (value_text != NULL) { - result->form_type = strdup(value_text); - xmpp_free(ctx, value_text); - } - - // handle regular fields - } else { - FormField *field = malloc(sizeof(FormField)); - field->label = NULL; - field->type = NULL; - field->var = NULL; - - if (label != NULL) { - field->label = strdup(label); - } - if (type != NULL) { - field->type = strdup(type); - } - if (var != NULL) { - field->var = strdup(var); - } - - // handle values - field->values = NULL; - xmpp_stanza_t *value = xmpp_stanza_get_children(child); - while (value != NULL) { - char *text = xmpp_stanza_get_text(value); - if (text != NULL) { - field->values = g_slist_insert_sorted(field->values, strdup(text), (GCompareFunc)strcmp); - xmpp_free(ctx, text); - } - 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; -} - -void -stanza_destroy_form(DataForm *form) -{ - if (form != NULL) { - if (form->fields != NULL) { - GSList *curr_field = form->fields; - while (curr_field != NULL) { - FormField *field = curr_field->data; - free(field->var); - if ((field->values) != NULL) { - g_slist_free_full(field->values, free); - } - curr_field = curr_field->next; - } - g_slist_free_full(form->fields, free); - } - - free(form->form_type); - free(form); - } -} - void stanza_attach_priority(xmpp_ctx_t * const ctx, xmpp_stanza_t * const presence, const int pri) @@ -1263,9 +1172,3 @@ stanza_get_presence_string_from_type(resource_presence_t presence_type) return NULL; } } - -static int -_field_compare(FormField *f1, FormField *f2) -{ - return strcmp(f1->var, f2->var); -} diff --git a/src/xmpp/xmpp.h b/src/xmpp/xmpp.h index cdb19c27..063062a0 100644 --- a/src/xmpp/xmpp.h +++ b/src/xmpp/xmpp.h @@ -175,4 +175,6 @@ void (*roster_send_remove_from_group)(const char * const group, PContact contact void (*roster_send_add_new)(const char * const barejid, const char * const name); void (*roster_send_remove)(const char * const barejid); +void (*form_destroy)(DataForm *form); + #endif