about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2014-09-13 23:07:52 +0100
committerJames Booth <boothj5@gmail.com>2014-09-13 23:07:52 +0100
commite13940daf4626af7057db8b7239bc6e64c9fd588 (patch)
treefce7e544cfbd48d241cd2b2281447c85d0a098ea /src
parent524a52d0ac7699ac25453f30ae5a49030bb1948a (diff)
downloadprofani-tty-e13940daf4626af7057db8b7239bc6e64c9fd588.tar.gz
Added form validation for list-single type
Diffstat (limited to 'src')
-rw-r--r--src/command/commands.c14
-rw-r--r--src/xmpp/form.c26
-rw-r--r--src/xmpp/xmpp.h1
3 files changed, 39 insertions, 2 deletions
diff --git a/src/command/commands.c b/src/command/commands.c
index 673dfd4e..e5612d9c 100644
--- a/src/command/commands.c
+++ b/src/command/commands.c
@@ -1916,6 +1916,7 @@ cmd_room(gchar **args, struct cmd_help_t help)
                 ui_current_print_line("Form does not contain a field with tag %s", tag);
             } else {
                 form_field_type_t field_type = form_get_field_type_by_tag(current->form, tag);
+                gboolean valid = FALSE;
                 switch (field_type) {
                 case FIELD_TEXT_SINGLE:
                 case FIELD_TEXT_PRIVATE:
@@ -1930,11 +1931,20 @@ cmd_room(gchar **args, struct cmd_help_t help)
                         form_set_value_by_tag(current->form, tag, "0");
                         ui_current_print_line("%s set to %s", tag, value);
                     } else {
-                        ui_current_print_line("Value %s not valid for boolean field: %s", tag, value);
+                        ui_current_print_line("Value %s not valid for boolean field: %s", value, tag);
+                    }
+                    break;
+                case FIELD_LIST_SINGLE:
+                    valid = form_field_contains_option_by_tag(current->form, tag, value);
+                    if (valid == TRUE) {
+                        form_set_value_by_tag(current->form, tag, value);
+                        ui_current_print_line("%s set to %s", tag, value);
+                    } else {
+                        ui_current_print_line("Value %s not a valid option for field: %s", value, tag);
                     }
                     break;
                 default:
-                    ui_current_print_line("Value %s not valid for field: %s", tag, value);
+                    ui_current_print_line("Value %s not valid for field: %s", value, tag);
                     break;
                 }
             }
diff --git a/src/xmpp/form.c b/src/xmpp/form.c
index df3f2dd2..65d6e431 100644
--- a/src/xmpp/form.c
+++ b/src/xmpp/form.c
@@ -441,6 +441,31 @@ _form_set_value_by_tag(DataForm *form, const char * const tag, char *value)
     }
 }
 
+static gboolean
+_form_field_contains_option_by_tag(DataForm *form, const char * const tag, char *value)
+{
+    char *var = g_hash_table_lookup(form->tag_to_var, tag);
+    if (var != NULL) {
+        GSList *curr = form->fields;
+        while (curr != NULL) {
+            FormField *field = curr->data;
+            if (g_strcmp0(field->var, var) == 0) {
+                GSList *curr_option = field->options;
+                while (curr_option != NULL) {
+                    FormOption *option = curr_option->data;
+                    if (g_strcmp0(option->value, value) == 0) {
+                        return TRUE;
+                    }
+                    curr_option = g_slist_next(curr_option);
+                }
+            }
+            curr = g_slist_next(curr);
+        }
+    }
+
+    return FALSE;
+}
+
 void
 form_init_module(void)
 {
@@ -448,5 +473,6 @@ form_init_module(void)
     form_get_form_type_field = _form_get_form_type_field;
     form_get_field_type_by_tag = _form_get_field_type_by_tag;
     form_set_value_by_tag = _form_set_value_by_tag;
+    form_field_contains_option_by_tag = _form_field_contains_option_by_tag;
     form_tag_exists = _form_tag_exists;
 }
diff --git a/src/xmpp/xmpp.h b/src/xmpp/xmpp.h
index cf78bb45..52d0037a 100644
--- a/src/xmpp/xmpp.h
+++ b/src/xmpp/xmpp.h
@@ -211,5 +211,6 @@ char * (*form_get_form_type_field)(DataForm *form);
 void (*form_set_value_by_tag)(DataForm *form, const char * const tag, char *value);
 gboolean (*form_tag_exists)(DataForm *form, const char * const tag);
 form_field_type_t (*form_get_field_type_by_tag)(DataForm *form, const char * const tag);
+gboolean (*form_field_contains_option_by_tag)(DataForm *form, const char * const tag, char *value);
 
 #endif