From 6c45f8ffae8adae7edc827f8f97f66e7355aff7a Mon Sep 17 00:00:00 2001 From: James Booth Date: Fri, 12 Sep 2014 00:39:37 +0100 Subject: [PATCH] Check room config tag exists before setting --- src/command/commands.c | 12 ++++++++---- src/xmpp/form.c | 15 +++++++++++++++ src/xmpp/xmpp.h | 1 + 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/command/commands.c b/src/command/commands.c index 33b93dbb..d0e906d8 100644 --- a/src/command/commands.c +++ b/src/command/commands.c @@ -1901,19 +1901,23 @@ cmd_room(gchar **args, struct cmd_help_t help) if (args[1] != NULL) { tag = args[1]; } else { - cons_show("Usage: %s", help.usage); + ui_current_print_line("/room set command requires a field tag and value"); g_strfreev(split_recipient); return TRUE; } if (args[2] != NULL) { value = args[2]; } else { - cons_show("Usage: %s", help.usage); + ui_current_print_line("/room set command requires a field tag and value"); g_strfreev(split_recipient); return TRUE; } - form_set_value_by_tag(current->form, tag, value); - cons_show("Field set."); + if (!form_tag_exists(current->form, tag)) { + ui_current_print_line("Form does not contain a field with tag %s", tag); + } else { + form_set_value_by_tag(current->form, tag, value); + ui_current_print_line("%s set to %s", tag, value); + } } if ((g_strcmp0(args[0], "submit") == 0) || diff --git a/src/xmpp/form.c b/src/xmpp/form.c index c23ffa26..d3f588c7 100644 --- a/src/xmpp/form.c +++ b/src/xmpp/form.c @@ -382,6 +382,20 @@ _form_get_field_by_var(DataForm *form, const char * const var) return NULL; } +static gboolean +_form_tag_exists(DataForm *form, const char * const tag) +{ + GList *tags = g_hash_table_get_keys(form->tag_to_var); + GList *curr = tags; + while (curr != NULL) { + if (g_strcmp0(curr->data, tag) == 0) { + return TRUE; + } + curr = g_list_next(curr); + } + return FALSE; +} + static void _form_set_value_by_tag(DataForm *form, const char * const tag, char *value) { @@ -410,4 +424,5 @@ form_init_module(void) form_destroy = _form_destroy; form_get_field_by_var = _form_get_field_by_var; form_set_value_by_tag = _form_set_value_by_tag; + form_tag_exists = _form_tag_exists; } diff --git a/src/xmpp/xmpp.h b/src/xmpp/xmpp.h index 97a405ed..1637e87d 100644 --- a/src/xmpp/xmpp.h +++ b/src/xmpp/xmpp.h @@ -207,5 +207,6 @@ void (*roster_send_remove)(const char * const barejid); void (*form_destroy)(DataForm *form); char * (*form_get_field_by_var)(DataForm *form, const char * const var); void (*form_set_value_by_tag)(DataForm *form, const char * const tag, char *value); +gboolean (*form_tag_exists)(DataForm *form, const char * const tag); #endif