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

Rewrite form parser

This commit is contained in:
James Booth 2014-09-06 22:40:57 +01:00
parent 157a1b5ff7
commit 2599c43d66
6 changed files with 234 additions and 81 deletions

View File

@ -63,6 +63,7 @@ _init_modules(void)
message_init_module(); message_init_module();
presence_init_module(); presence_init_module();
roster_init_module(); roster_init_module();
form_init_module();
ui_init_module(); ui_init_module();
console_init_module(); console_init_module();

View File

@ -467,11 +467,6 @@ void
handle_room_configure(const char * const room, DataForm *form) handle_room_configure(const char * const room, DataForm *form)
{ {
cons_show("Recieved configuration form for %s", room); cons_show("Recieved configuration form for %s", room);
if (form->form_type != NULL) {
cons_show("Form type: %s", form->form_type);
} else {
cons_show("No form type specified");
}
GSList *fields = form->fields; GSList *fields = form->fields;
GSList *curr = fields; GSList *curr = fields;
@ -492,6 +487,8 @@ handle_room_configure(const char * const room, DataForm *form)
curr = g_slist_next(curr); curr = g_slist_next(curr);
} }
form_destroy(form);
} }
void void

View File

@ -172,8 +172,9 @@ caps_create_sha1_str(xmpp_stanza_t * const query)
} else if (g_strcmp0(xmpp_stanza_get_name(child), STANZA_NAME_X) == 0) { } else if (g_strcmp0(xmpp_stanza_get_name(child), STANZA_NAME_X) == 0) {
if (strcmp(xmpp_stanza_get_ns(child), STANZA_NS_DATA) == 0) { if (strcmp(xmpp_stanza_get_ns(child), STANZA_NS_DATA) == 0) {
form = form_create(child); form = form_create(child);
form_names = g_slist_insert_sorted(form_names, g_strdup(form->form_type), (GCompareFunc)strcmp); char *form_type = form_get_field_by_var(form, "FORM_TYPE");
g_hash_table_insert(forms, g_strdup(form->form_type), form); form_names = g_slist_insert_sorted(form_names, g_strdup(form_type), (GCompareFunc)strcmp);
g_hash_table_insert(forms, g_strdup(form_type), form);
} }
} }
child = xmpp_stanza_get_next(child); child = xmpp_stanza_get_next(child);
@ -195,7 +196,8 @@ caps_create_sha1_str(xmpp_stanza_t * const query)
curr = form_names; curr = form_names;
while (curr != NULL) { while (curr != NULL) {
form = g_hash_table_lookup(forms, curr->data); form = g_hash_table_lookup(forms, curr->data);
g_string_append(s, form->form_type); char *form_type = form_get_field_by_var(form, "FORM_TYPE");
g_string_append(s, form_type);
g_string_append(s, "<"); g_string_append(s, "<");
GSList *curr_field = form->fields; GSList *curr_field = form->fields;

View File

@ -36,109 +36,249 @@
#include <stdlib.h> #include <stdlib.h>
#include <strophe.h> #include <strophe.h>
#include <glib.h>
#include "log.h"
#include "xmpp/xmpp.h" #include "xmpp/xmpp.h"
#include "xmpp/stanza.h"
#include "xmpp/connection.h" #include "xmpp/connection.h"
static int _field_compare(FormField *f1, FormField *f2); static gboolean
_is_valid_form_element(xmpp_stanza_t *stanza)
DataForm *
form_create(xmpp_stanza_t * const stanza)
{ {
DataForm *result = NULL; char *name = xmpp_stanza_get_name(stanza);
xmpp_ctx_t *ctx = connection_get_ctx(); if (g_strcmp0(name, STANZA_NAME_X) != 0) {
log_error("Error parsing form, root element not <x/>.");
xmpp_stanza_t *child = xmpp_stanza_get_children(stanza); return FALSE;
if (child != NULL) {
result = malloc(sizeof(DataForm));
result->form_type = NULL;
result->fields = NULL;
} }
//handle fields char *ns = xmpp_stanza_get_ns(stanza);
while (child != NULL) { if (g_strcmp0(ns, STANZA_NS_DATA) != 0) {
char *label = xmpp_stanza_get_attribute(child, "label"); log_error("Error parsing form, namespace not %s.", STANZA_NS_DATA);
char *type = xmpp_stanza_get_attribute(child, "type"); return FALSE;
char *var = xmpp_stanza_get_attribute(child, "var"); }
// handle FORM_TYPE char *type = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_TYPE);
if (g_strcmp0(var, "FORM_TYPE") == 0) { if ((g_strcmp0(type, "form") != 0) &&
xmpp_stanza_t *value = xmpp_stanza_get_child_by_name(child, "value"); (g_strcmp0(type, "submit") != 0) &&
char *value_text = xmpp_stanza_get_text(value); (g_strcmp0(type, "cancel") != 0) &&
if (value_text != NULL) { (g_strcmp0(type, "result") != 0)) {
result->form_type = strdup(value_text); log_error("Error parsing form, unknown type.");
xmpp_free(ctx, value_text); return FALSE;
} }
// handle regular fields return TRUE;
} else { }
FormField *field = malloc(sizeof(FormField));
field->label = NULL;
field->type = NULL;
field->var = NULL;
if (label != NULL) { static DataForm *
field->label = strdup(label); _form_new(void)
} {
if (type != NULL) { DataForm *form = malloc(sizeof(DataForm));
field->type = strdup(type); form->type = NULL;
} form->title = NULL;
if (var != NULL) { form->instructions = NULL;
field->var = strdup(var); form->fields = NULL;
}
// handle values return form;
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); static FormField *
_field_new(void)
{
FormField *field = malloc(sizeof(FormField));
field->label = NULL;
field->type = NULL;
field->var = NULL;
field->description = NULL;
field->required = FALSE;
field->values = NULL;
field->options = NULL;
return field;
}
static char *
_get_property(xmpp_stanza_t * const stanza, const char * const property)
{
char *result = NULL;
xmpp_ctx_t *ctx = connection_get_ctx();
xmpp_stanza_t *child = xmpp_stanza_get_child_by_name(stanza, property);
if (child != NULL) {
char *child_text = xmpp_stanza_get_text(child);
if (child_text != NULL) {
result = strdup(child_text);
xmpp_free(ctx, child_text);
} }
child = xmpp_stanza_get_next(child);
} }
return result; return result;
} }
void static char *
_get_attr(xmpp_stanza_t * const stanza, const char * const attr)
{
char *result = xmpp_stanza_get_attribute(stanza, attr);
if (result != NULL) {
return strdup(result);
} else {
return NULL;
}
}
static gboolean
_is_required(xmpp_stanza_t * const stanza)
{
xmpp_stanza_t *child = xmpp_stanza_get_child_by_name(stanza, "required");
if (child != NULL) {
return TRUE;
} else {
return FALSE;
}
}
DataForm *
form_create(xmpp_stanza_t * const form_stanza)
{
xmpp_ctx_t *ctx = connection_get_ctx();
if (!_is_valid_form_element(form_stanza)) {
return NULL;
}
DataForm *form = _form_new();
form->type = _get_attr(form_stanza, "type");
form->title = _get_property(form_stanza, "title");
form->instructions = _get_property(form_stanza, "instructions");
// get fields
xmpp_stanza_t *form_child = xmpp_stanza_get_children(form_stanza);
while (form_child != NULL) {
char *child_name = xmpp_stanza_get_name(form_child);
if (g_strcmp0(child_name, "field") == 0) {
xmpp_stanza_t *field_stanza = form_child;
FormField *field = _field_new();
field->label = _get_attr(field_stanza, "label");
field->type = _get_attr(field_stanza, "type");
field->var = _get_attr(field_stanza, "var");
field->description = _get_property(field_stanza, "desc");
field->required = _is_required(field_stanza);
// handle repeated field children
xmpp_stanza_t *field_child = xmpp_stanza_get_children(field_stanza);
while (field_child != NULL) {
// handle values
if (g_strcmp0(child_name, "value") == 0) {
char *value = xmpp_stanza_get_text(field_child);
if (value != NULL) {
field->values = g_slist_append(field->values, value);
xmpp_free(ctx, value);
}
// handle options
} else if (g_strcmp0(child_name, "option") == 0) {
FormOption *option = malloc(sizeof(FormOption));
option->label = _get_attr(field_child, "label");
option->value = _get_property(field_child, "value");
field->options = g_slist_append(field->options, option);
}
field_child = xmpp_stanza_get_next(field_child);
}
form->fields = g_slist_append(form->fields, field);
}
form_child = xmpp_stanza_get_next(form_child);
}
return form;
}
static void
_free_option(FormOption *option)
{
if (option != NULL) {
if (option->label != NULL) {
free(option->label);
}
if (option->value != NULL) {
free(option->value);
}
free(option);
}
}
static void
_free_field(FormField *field)
{
if (field != NULL) {
if (field->label != NULL) {
free(field->label);
}
if (field->type != NULL) {
free(field->type);
}
if (field->var != NULL) {
free(field->var);
}
if (field->description != NULL) {
free(field->description);
}
if (field->values != NULL) {
g_slist_free_full(field->values, free);
}
if (field->options != NULL) {
g_slist_free_full(field->options, (GDestroyNotify)_free_option);
}
free(field);
}
}
static void
_form_destroy(DataForm *form) _form_destroy(DataForm *form)
{ {
if (form != NULL) { if (form != NULL) {
if (form->fields != NULL) { if (form->type != NULL) {
GSList *curr_field = form->fields; free(form->type);
while (curr_field != NULL) { }
FormField *field = curr_field->data; if (form->title != NULL) {
free(field->var); free(form->title);
if ((field->values) != NULL) { }
g_slist_free_full(field->values, free); if (form->instructions != NULL) {
} free(form->instructions);
curr_field = curr_field->next; }
}
g_slist_free_full(form->fields, free); if (form->fields != NULL) {
g_slist_free_full(form->fields, (GDestroyNotify)_free_field);
} }
free(form->form_type);
free(form); free(form);
} }
} }
static int static char *
_field_compare(FormField *f1, FormField *f2) _form_get_field_by_var(DataForm *form, const char * const var)
{ {
return strcmp(f1->var, f2->var); GSList *curr = form->fields;
while (curr != NULL) {
FormField *field = curr->data;
if (g_strcmp0(field->var, var) == 0) {
return field->values->data;
}
curr = g_slist_next(curr);
}
return NULL;
} }
void void
form_init_module(void) form_init_module(void)
{ {
form_destroy = _form_destroy; form_destroy = _form_destroy;
form_get_field_by_var = _form_get_field_by_var;
} }

View File

@ -770,7 +770,8 @@ _disco_info_result_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanz
DataForm *form = form_create(softwareinfo); DataForm *form = form_create(softwareinfo);
FormField *formField = NULL; FormField *formField = NULL;
if (g_strcmp0(form->form_type, STANZA_DATAFORM_SOFTWARE) == 0) { char *form_type = form_get_field_by_var(form, "FORM_TYPE");
if (g_strcmp0(form_type, STANZA_DATAFORM_SOFTWARE) == 0) {
GSList *field = form->fields; GSList *field = form->fields;
while (field != NULL) { while (field != NULL) {
formField = field->data; formField = field->data;

View File

@ -86,15 +86,25 @@ typedef struct disco_identity_t {
char *category; char *category;
} DiscoIdentity; } DiscoIdentity;
typedef struct form_option_t {
char *label;
char *value;
} FormOption;
typedef struct form_field_t { typedef struct form_field_t {
char *label; char *label;
char *type; char *type;
char *var; char *var;
char *description;
gboolean required;
GSList *values; GSList *values;
GSList *options;
} FormField; } FormField;
typedef struct data_form_t { typedef struct data_form_t {
char *form_type; char *type;
char *title;
char *instructions;
GSList *fields; GSList *fields;
} DataForm; } DataForm;
@ -105,6 +115,7 @@ void iq_init_module(void);
void message_init_module(void); void message_init_module(void);
void presence_init_module(void); void presence_init_module(void);
void roster_init_module(void); void roster_init_module(void);
void form_init_module(void);
// connection functions // connection functions
void (*jabber_init)(const int disable_tls); void (*jabber_init)(const int disable_tls);
@ -176,5 +187,6 @@ void (*roster_send_add_new)(const char * const barejid, const char * const name)
void (*roster_send_remove)(const char * const barejid); void (*roster_send_remove)(const char * const barejid);
void (*form_destroy)(DataForm *form); void (*form_destroy)(DataForm *form);
char * (*form_get_field_by_var)(DataForm *form, const char * const var);
#endif #endif