about summary refs log tree commit diff stats
path: root/src/command
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2014-10-19 03:27:34 +0100
committerJames Booth <boothj5@gmail.com>2014-10-19 03:27:34 +0100
commit7a88898a21305573accf6fdd80ec6df20ce03026 (patch)
treec48bc18b86844a2cde5e7733cb1f41b13cc419c7 /src/command
parent272fbf2f0fd03e8dbd4075e4902ad0ef11b40893 (diff)
downloadprofani-tty-7a88898a21305573accf6fdd80ec6df20ce03026.tar.gz
Implemented new field commands
Diffstat (limited to 'src/command')
-rw-r--r--src/command/command.c17
-rw-r--r--src/command/commands.c402
-rw-r--r--src/command/commands.h2
3 files changed, 220 insertions, 201 deletions
diff --git a/src/command/command.c b/src/command/command.c
index eddbc033..5726b49f 100644
--- a/src/command/command.c
+++ b/src/command/command.c
@@ -1586,6 +1586,23 @@ cmd_reset_autocomplete()
 gboolean
 cmd_execute(const char * const command, const char * const inp)
 {
+    if (g_str_has_prefix(command, "/field") && ui_current_win_type() == WIN_MUC_CONFIG) {
+        gboolean result = FALSE;
+        gchar **args = parse_args_with_freetext(inp, 1, 2, &result);
+        if (!result) {
+            ui_current_print_formatted_line('!', 0, "Invalid command, see /form help");
+            result = TRUE;
+        } else {
+            gchar **tokens = g_strsplit(inp, " ", 2);
+            char *field = tokens[0] + 1;
+            result = cmd_form_field(field, args);
+            g_strfreev(tokens);
+        }
+
+        g_strfreev(args);
+        return result;
+    }
+
     Command *cmd = g_hash_table_lookup(commands, command);
     gboolean result = FALSE;
 
diff --git a/src/command/commands.c b/src/command/commands.c
index 74d2bbe8..40640485 100644
--- a/src/command/commands.c
+++ b/src/command/commands.c
@@ -1796,6 +1796,206 @@ cmd_decline(gchar **args, struct cmd_help_t help)
 }
 
 gboolean
+cmd_form_field(char *tag, gchar **args)
+{
+    ProfWin *current = wins_get_current();
+    DataForm *form = current->form;
+    if (form) {
+        if (!form_tag_exists(form, tag)) {
+            ui_current_print_line("Form does not contain a field with tag %s", tag);
+            return TRUE;
+        }
+
+        form_field_type_t field_type = form_get_field_type(form, tag);
+        char *cmd = NULL;
+        char *value = NULL;
+        gboolean valid = FALSE;
+        gboolean added = FALSE;
+        gboolean removed = FALSE;
+
+        switch (field_type) {
+        case FIELD_BOOLEAN:
+            value = args[0];
+            if (g_strcmp0(value, "on") == 0) {
+                form_set_value(form, tag, "1");
+                ui_current_print_line("Field updated...");
+                ui_show_form_field(current, form, tag);
+            } else if (g_strcmp0(value, "off") == 0) {
+                form_set_value(form, tag, "0");
+                ui_current_print_line("Field updated...");
+                ui_show_form_field(current, form, tag);
+            } else {
+                ui_current_print_line("Invalid command, usage:");
+                ui_show_form_field_help(current, form, tag);
+            }
+            break;
+
+        case FIELD_TEXT_PRIVATE:
+        case FIELD_TEXT_SINGLE:
+        case FIELD_JID_SINGLE:
+            value = args[0];
+            if (value == NULL) {
+                ui_current_print_line("Invalid command, usage:");
+                ui_show_form_field_help(current, form, tag);
+            } else {
+                form_set_value(form, tag, value);
+                ui_current_print_line("Field updated...");
+                ui_show_form_field(current, form, tag);
+            }
+            break;
+        case FIELD_LIST_SINGLE:
+            value = args[0];
+            if ((value == NULL) || !form_field_contains_option(form, tag, value)) {
+                ui_current_print_line("Invalid command, usage:");
+                ui_show_form_field_help(current, form, tag);
+            } else {
+                form_set_value(form, tag, value);
+                ui_current_print_line("Field updated...");
+                ui_show_form_field(current, form, tag);
+            }
+            break;
+
+        case FIELD_TEXT_MULTI:
+            cmd = args[0];
+            if (cmd) {
+                value = args[1];
+            }
+            if ((g_strcmp0(cmd, "add") != 0) && (g_strcmp0(cmd, "remove"))) {
+                ui_current_print_line("Invalid command, usage:");
+                ui_show_form_field_help(current, form, tag);
+                break;
+            }
+            if (value == NULL) {
+                ui_current_print_line("Invalid command, usage:");
+                ui_show_form_field_help(current, form, tag);
+                break;
+            }
+            if (g_strcmp0(cmd, "add") == 0) {
+                form_add_value(form, tag, value);
+                ui_current_print_line("Field updated...");
+                ui_show_form_field(current, form, tag);
+                break;
+            }
+            if (g_strcmp0(args[0], "remove") == 0) {
+                if (!g_str_has_prefix(value, "val")) {
+                    ui_current_print_line("Invalid command, usage:");
+                    ui_show_form_field_help(current, form, tag);
+                    break;
+                }
+                if (strlen(value) < 4) {
+                    ui_current_print_line("Invalid command, usage:");
+                    ui_show_form_field_help(current, form, tag);
+                    break;
+                }
+
+                int index = strtol(&value[3], NULL, 10);
+                if ((index < 1) || (index > form_get_value_count(form, tag))) {
+                    ui_current_print_line("Invalid command, usage:");
+                    ui_show_form_field_help(current, form, tag);
+                    break;
+                }
+
+                removed = form_remove_text_multi_value(form, tag, index);
+                if (removed) {
+                    ui_current_print_line("Field updated...");
+                    ui_show_form_field(current, form, tag);
+                } else {
+                    ui_current_print_line("Could not remove %s from %s", value, tag);
+                }
+            }
+            break;
+        case FIELD_LIST_MULTI:
+            cmd = args[0];
+            if (cmd) {
+                value = args[1];
+            }
+            if ((g_strcmp0(cmd, "add") != 0) && (g_strcmp0(cmd, "remove"))) {
+                ui_current_print_line("Invalid command, usage:");
+                ui_show_form_field_help(current, form, tag);
+                break;
+            }
+            if (value == NULL) {
+                ui_current_print_line("Invalid command, usage:");
+                ui_show_form_field_help(current, form, tag);
+                break;
+            }
+            if (g_strcmp0(args[0], "add") == 0) {
+                valid = form_field_contains_option(form, tag, value);
+                if (valid) {
+                    added = form_add_unique_value(form, tag, value);
+                    if (added) {
+                        ui_current_print_line("Field updated...");
+                        ui_show_form_field(current, form, tag);
+                    } else {
+                        ui_current_print_line("Value %s already selected for %s", value, tag);
+                    }
+                } else {
+                    ui_current_print_line("Invalid command, usage:");
+                    ui_show_form_field_help(current, form, tag);
+                }
+                break;
+            }
+            if (g_strcmp0(args[0], "remove") == 0) {
+                valid = form_field_contains_option(form, tag, value);
+                if (valid == TRUE) {
+                    removed = form_remove_value(form, tag, value);
+                    if (removed) {
+                        ui_current_print_line("Field updated...");
+                        ui_show_form_field(current, form, tag);
+                    } else {
+                        ui_current_print_line("Value %s is not currently set for %s", value, tag);
+                    }
+                } else {
+                    ui_current_print_line("Invalid command, usage:");
+                    ui_show_form_field_help(current, form, tag);
+                }
+            }
+            break;
+        case FIELD_JID_MULTI:
+            cmd = args[0];
+            if (cmd) {
+                value = args[1];
+            }
+            if ((g_strcmp0(cmd, "add") != 0) && (g_strcmp0(cmd, "remove"))) {
+                ui_current_print_line("Invalid command, usage:");
+                ui_show_form_field_help(current, form, tag);
+                break;
+            }
+            if (value == NULL) {
+                ui_current_print_line("Invalid command, usage:");
+                ui_show_form_field_help(current, form, tag);
+                break;
+            }
+            if (g_strcmp0(args[0], "add") == 0) {
+                added = form_add_unique_value(form, tag, value);
+                if (added) {
+                    ui_current_print_line("Field updated...");
+                    ui_show_form_field(current, form, tag);
+                } else {
+                    ui_current_print_line("JID %s already exists in %s", value, tag);
+                }
+                break;
+            }
+            if (g_strcmp0(args[0], "remove") == 0) {
+                removed = form_remove_value(current->form, tag, value);
+                if (removed) {
+                    ui_current_print_line("Field updated...");
+                    ui_show_form_field(current, current->form, tag);
+                } else {
+                    ui_current_print_line("Field %s does not contain %s", tag, value);
+                }
+            }
+            break;
+
+        default:
+            break;
+        }
+    }
+
+    return TRUE;
+}
+
+gboolean
 cmd_form(gchar **args, struct cmd_help_t help)
 {
     jabber_conn_status_t conn_status = jabber_get_connection_status();
@@ -1814,10 +2014,7 @@ cmd_form(gchar **args, struct cmd_help_t help)
     if ((g_strcmp0(args[0], "submit") != 0) &&
             (g_strcmp0(args[0], "cancel") != 0) &&
             (g_strcmp0(args[0], "show") != 0) &&
-            (g_strcmp0(args[0], "help") != 0) &&
-            (g_strcmp0(args[0], "set") != 0) &&
-            (g_strcmp0(args[0], "add") != 0) &&
-            (g_strcmp0(args[0], "remove") != 0)) {
+            (g_strcmp0(args[0], "help") != 0)) {
         cons_show("Usage: %s", help.usage);
         return TRUE;
     }
@@ -1861,203 +2058,6 @@ cmd_form(gchar **args, struct cmd_help_t help)
     if (g_strcmp0(args[0], "cancel") == 0) {
         iq_room_config_cancel(room);
     }
-    if (g_strcmp0(args[0], "set") == 0) {
-        char *tag = NULL;
-        char *value = NULL;
-        if (args[1] != NULL) {
-            tag = args[1];
-        } else {
-            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 {
-            ui_current_print_line("/room set command requires a field tag and value");
-            g_strfreev(split_recipient);
-            return TRUE;
-        }
-        if (!form_tag_exists(current->form, tag)) {
-            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(current->form, tag);
-            gboolean valid = FALSE;
-            switch (field_type) {
-            case FIELD_TEXT_SINGLE:
-            case FIELD_TEXT_PRIVATE:
-            case FIELD_JID_SINGLE:
-                form_set_value(current->form, tag, value);
-                ui_current_print_line("Field updated...");
-                ui_show_form_field(current, current->form, tag);
-                break;
-            case FIELD_BOOLEAN:
-                if (g_strcmp0(value, "on") == 0) {
-                    form_set_value(current->form, tag, "1");
-                    ui_current_print_line("Field updated...");
-                    ui_show_form_field(current, current->form, tag);
-                } else if (g_strcmp0(value, "off") == 0) {
-                    form_set_value(current->form, tag, "0");
-                    ui_current_print_line("Field updated...");
-                    ui_show_form_field(current, current->form, tag);
-                } else {
-                    ui_current_print_line("Value %s not valid for boolean field: %s", value, tag);
-                }
-                break;
-            case FIELD_LIST_SINGLE:
-                valid = form_field_contains_option(current->form, tag, value);
-                if (valid == TRUE) {
-                    form_set_value(current->form, tag, value);
-                    ui_current_print_line("Field updated...");
-                    ui_show_form_field(current, current->form, tag);
-                } else {
-                    ui_current_print_line("Value %s not a valid option for field: %s", value, tag);
-                }
-                break;
-            default:
-                ui_current_print_line("Set command not valid for field: %s", tag);
-                break;
-            }
-        }
-    }
-
-    if (g_strcmp0(args[0], "add") == 0) {
-        char *tag = NULL;
-        char *value = NULL;
-        if (args[1] != NULL) {
-            tag = args[1];
-        } else {
-            ui_current_print_line("/room add command requires a field tag and value");
-            g_strfreev(split_recipient);
-            return TRUE;
-        }
-        if (args[2] != NULL) {
-            value = args[2];
-        } else {
-            ui_current_print_line("/room add command requires a field tag and value");
-            g_strfreev(split_recipient);
-            return TRUE;
-        }
-        if (!form_tag_exists(current->form, tag)) {
-            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(current->form, tag);
-            gboolean valid = FALSE;
-            gboolean added = FALSE;
-            switch (field_type) {
-            case FIELD_LIST_MULTI:
-                valid = form_field_contains_option(current->form, tag, value);
-                if (valid) {
-                    added = form_add_unique_value(current->form, tag, value);
-                    if (added) {
-                        ui_current_print_line("Field updated...");
-                        ui_show_form_field(current, current->form, tag);
-                    } else {
-                        ui_current_print_line("Value %s already selected for %s", value, tag);
-                    }
-                } else {
-                    ui_current_print_line("Value %s not a valid option for field: %s", value, tag);
-                }
-                break;
-            case FIELD_TEXT_MULTI:
-                form_add_value(current->form, tag, value);
-                ui_current_print_line("Field updated...");
-                ui_show_form_field(current, current->form, tag);
-                break;
-            case FIELD_JID_MULTI:
-                added = form_add_unique_value(current->form, tag, value);
-                if (added) {
-                    ui_current_print_line("Field updated...");
-                    ui_show_form_field(current, current->form, tag);
-                } else {
-                    ui_current_print_line("JID %s already exists in %s", value, tag);
-                }
-                break;
-            default:
-                ui_current_print_line("Add command not valid for field: %s", tag);
-                break;
-            }
-        }
-    }
-
-    if (g_strcmp0(args[0], "remove") == 0) {
-        char *tag = NULL;
-        char *value = NULL;
-        if (args[1] != NULL) {
-            tag = args[1];
-        } else {
-            ui_current_print_line("/room remove command requires a field tag and value");
-            g_strfreev(split_recipient);
-            return TRUE;
-        }
-        if (args[2] != NULL) {
-            value = args[2];
-        } else {
-            ui_current_print_line("/room remove command requires a field tag and value");
-            g_strfreev(split_recipient);
-            return TRUE;
-        }
-        if (!form_tag_exists(current->form, tag)) {
-            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(current->form, tag);
-            gboolean valid = FALSE;
-            gboolean removed = FALSE;
-            switch (field_type) {
-            case FIELD_LIST_MULTI:
-                valid = form_field_contains_option(current->form, tag, value);
-                if (valid == TRUE) {
-                    removed = form_remove_value(current->form, tag, value);
-                    if (removed) {
-                        ui_current_print_line("Field updated...");
-                        ui_show_form_field(current, current->form, tag);
-                    } else {
-                        ui_current_print_line("Value %s is not currently set for %s", value, tag);
-                    }
-                } else {
-                    ui_current_print_line("Value %s not a valid option for field: %s", value, tag);
-                }
-                break;
-            case FIELD_TEXT_MULTI:
-                if (!g_str_has_prefix(value, "val")) {
-                    ui_current_print_line("No such value %s for %s", value, tag);
-                    break;
-                }
-                if (strlen(value) < 4) {
-                    ui_current_print_line("No such value %s for %s", value, tag);
-                    break;
-                }
-
-                int index = strtol(&value[3], NULL, 10);
-                if ((index < 1) || (index > form_get_value_count(current->form, tag))) {
-                    ui_current_print_line("No such value %s for %s", value, tag);
-                    break;
-                }
-
-                removed = form_remove_text_multi_value(current->form, tag, index);
-                if (removed) {
-                    ui_current_print_line("Field updated...");
-                    ui_show_form_field(current, current->form, tag);
-                } else {
-                    ui_current_print_line("Could not remove %s from %s", value, tag);
-                }
-                break;
-            case FIELD_JID_MULTI:
-                removed = form_remove_value(current->form, tag, value);
-                if (removed) {
-                    ui_current_print_line("Field updated...");
-                    ui_show_form_field(current, current->form, tag);
-                } else {
-                    ui_current_print_line("Field %s does not contain %s", tag, value);
-                }
-                break;
-            default:
-                ui_current_print_line("Remove command not valid for field: %s", tag);
-                break;
-            }
-        }
-    }
-
     if ((g_strcmp0(args[0], "submit") == 0) ||
             (g_strcmp0(args[0], "cancel") == 0)) {
         if (current->form) {
diff --git a/src/command/commands.h b/src/command/commands.h
index ed393f8a..55f11df1 100644
--- a/src/command/commands.h
+++ b/src/command/commands.h
@@ -133,4 +133,6 @@ gboolean cmd_affiliation(gchar **args, struct cmd_help_t help);
 gboolean cmd_role(gchar **args, struct cmd_help_t help);
 gboolean cmd_privileges(gchar **args, struct cmd_help_t help);
 
+gboolean cmd_form_field(char *tag, gchar **args);
+
 #endif
\ No newline at end of file