1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-06-23 21:45:30 +00:00

Created form module

This commit is contained in:
James Booth 2014-09-05 21:04:16 +01:00
parent fa28741b74
commit 157a1b5ff7
7 changed files with 194 additions and 102 deletions

View File

@ -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 \

View File

@ -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);
}

144
src/xmpp/form.c Normal file
View File

@ -0,0 +1,144 @@
/*
* form.c
*
* Copyright (C) 2012 - 2014 James Booth <boothj5@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*
* 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 <string.h>
#include <stdlib.h>
#include <strophe.h>
#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;
}

40
src/xmpp/form.h Normal file
View File

@ -0,0 +1,40 @@
/*
* form.h
*
* Copyright (C) 2012 - 2014 James Booth <boothj5@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*
* 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

View File

@ -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);

View File

@ -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);
}

View File

@ -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