about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2014-09-11 00:01:44 +0100
committerJames Booth <boothj5@gmail.com>2014-09-11 00:01:44 +0100
commit1e26b7a4ffb1fb0e07e7e8c4c4451d6c610e4f9b (patch)
tree8691289d6af96d093df60fe772dcd94275e3a5e3 /src
parenteba3a7cb303af47624ea890e2c3164010684e60f (diff)
downloadprofani-tty-1e26b7a4ffb1fb0e07e7e8c4c4451d6c610e4f9b.tar.gz
Added /room command validation, reduced to one subcommand
Diffstat (limited to 'src')
-rw-r--r--src/command/command.c20
-rw-r--r--src/command/commands.c121
2 files changed, 75 insertions, 66 deletions
diff --git a/src/command/command.c b/src/command/command.c
index f6080468..75808847 100644
--- a/src/command/command.c
+++ b/src/command/command.c
@@ -305,7 +305,7 @@ static struct cmd_t command_defs[] =
           NULL } } },
 
     { "/room",
-        cmd_room, parse_args, 2, 2, NULL,
+        cmd_room, parse_args, 1, 1, NULL,
         { "/room config accept|destroy|edit|cancel", "Room configuration.",
         { "/room config accept|destroy|edit|cancel",
           "---------------------------------------",
@@ -956,7 +956,6 @@ static Autocomplete alias_ac;
 static Autocomplete aliases_ac;
 static Autocomplete join_property_ac;
 static Autocomplete room_ac;
-static Autocomplete room_config_ac;
 
 /*
  * Initialise command autocompleter and history
@@ -1209,13 +1208,11 @@ cmd_init(void)
     autocomplete_add(alias_ac, "list");
 
     room_ac = autocomplete_new();
+    autocomplete_add(room_ac, "accept");
+    autocomplete_add(room_ac, "destroy");
     autocomplete_add(room_ac, "config");
-
-    room_config_ac = autocomplete_new();
-    autocomplete_add(room_config_ac, "accept");
-    autocomplete_add(room_config_ac, "destroy");
-    autocomplete_add(room_config_ac, "edit");
-    autocomplete_add(room_config_ac, "cancel");
+    autocomplete_add(room_ac, "submit");
+    autocomplete_add(room_ac, "cancel");
 
     cmd_history_init();
 }
@@ -1261,7 +1258,6 @@ cmd_uninit(void)
     autocomplete_free(aliases_ac);
     autocomplete_free(join_property_ac);
     autocomplete_free(room_ac);
-    autocomplete_free(room_config_ac);
 }
 
 gboolean
@@ -1386,7 +1382,6 @@ cmd_reset_autocomplete()
     autocomplete_reset(aliases_ac);
     autocomplete_reset(join_property_ac);
     autocomplete_reset(room_ac);
-    autocomplete_reset(room_config_ac);
     bookmark_autocomplete_reset();
 }
 
@@ -2080,11 +2075,6 @@ _room_autocomplete(char *input, int *size)
 {
     char *result = NULL;
 
-    result = autocomplete_param_with_ac(input, size, "/room config", room_config_ac, TRUE);
-    if (result != NULL) {
-        return result;
-    }
-
     result = autocomplete_param_with_ac(input, size, "/room", room_ac, TRUE);
     if (result != NULL) {
         return result;
diff --git a/src/command/commands.c b/src/command/commands.c
index 8dc351aa..1d3c3241 100644
--- a/src/command/commands.c
+++ b/src/command/commands.c
@@ -1797,84 +1797,103 @@ cmd_room(gchar **args, struct cmd_help_t help)
         return TRUE;
     }
 
+    // room command allowed in window
     win_type_t win_type = ui_current_win_type();
     if (win_type != WIN_MUC && win_type != WIN_MUC_CONFIG) {
-        cons_show("Command /room only usable in chat rooms.");
+        cons_show("Command '/room' does not apply to this window.");
         return TRUE;
     }
 
-    if (g_strcmp0(args[0], "config") != 0) {
+    // validate subcommand
+    if ((g_strcmp0(args[0], "accept") != 0) &&
+            (g_strcmp0(args[0], "destroy") != 0) &&
+            (g_strcmp0(args[0], "config") != 0) &&
+            (g_strcmp0(args[0], "submit") != 0) &&
+            (g_strcmp0(args[0], "cancel") != 0)) {
         cons_show("Usage: %s", help.usage);
         return TRUE;
     }
 
-    if ((g_strcmp0(args[1], "accept") != 0) &&
-            (g_strcmp0(args[1], "cancel") != 0) &&
-            (g_strcmp0(args[1], "destroy") != 0) &&
-            (g_strcmp0(args[1], "submit") != 0) &&
-            (g_strcmp0(args[1], "edit") != 0)) {
-        cons_show("Usage: %s", help.usage);
+    // validate subcommand for window type
+    if (win_type == WIN_MUC &&
+            ((g_strcmp0(args[0], "submit") == 0) ||
+            (g_strcmp0(args[0], "cancel") == 0))) {
+        cons_show("Command '/room %s' only allowed in room configuration windows.", args[0]);
+        return TRUE;
+    }
+    if (win_type == WIN_MUC_CONFIG &&
+            ((g_strcmp0(args[0], "accept") == 0) ||
+            (g_strcmp0(args[0], "destroy") == 0) ||
+            (g_strcmp0(args[0], "config") == 0))) {
+        cons_show("Command '/room %s' only allowed in chat room windows.", args[0]);
         return TRUE;
     }
 
     char *room = ui_current_recipient();
-    ProfWin *window = wins_get_by_recipient(room);
-    int num = wins_get_num(window);
-    int ui_index = num;
-    if (ui_index == 10) {
-        ui_index = 0;
-    }
 
-    if (g_strcmp0(args[1], "accept") == 0) {
-        gboolean requires_config = muc_requires_config(room);
-        if (!requires_config) {
-            win_save_vprint(window, '!', NULL, 0, COLOUR_ROOMINFO, "", "Current room does not require configuration.");
-            return TRUE;
-        } else {
-            iq_confirm_instant_room(room);
-            muc_set_requires_config(room, FALSE);
-            win_save_vprint(window, '!', NULL, 0, COLOUR_ROOMINFO, "", "Room unlocked.");
-            cons_show("Room unlocked: %s (%d)", room, ui_index);
-            return TRUE;
+    // commands available in room
+    if ((g_strcmp0(args[0], "accept") == 0) ||
+            (g_strcmp0(args[0], "destroy") == 0) ||
+            (g_strcmp0(args[0], "config") == 0)) {
+
+        ProfWin *window = wins_get_by_recipient(room);
+        int num = wins_get_num(window);
+        int ui_index = num;
+        if (ui_index == 10) {
+            ui_index = 0;
         }
-    }
 
-    if (g_strcmp0(args[1], "destroy") == 0) {
-        iq_destroy_instant_room(room);
-        return TRUE;
-    }
+        if (g_strcmp0(args[0], "accept") == 0) {
+            gboolean requires_config = muc_requires_config(room);
+            if (!requires_config) {
+                win_save_vprint(window, '!', NULL, 0, COLOUR_ROOMINFO, "", "Current room does not require configuration.");
+                return TRUE;
+            } else {
+                iq_confirm_instant_room(room);
+                muc_set_requires_config(room, FALSE);
+                win_save_vprint(window, '!', NULL, 0, COLOUR_ROOMINFO, "", "Room unlocked.");
+                cons_show("Room unlocked: %s (%d)", room, ui_index);
+                return TRUE;
+            }
+        }
 
-    if (g_strcmp0(args[1], "edit") == 0) {
-        GString *win_title = g_string_new(room);
-        g_string_append(win_title, " config");
-        ProfWin *window = wins_get_by_recipient(win_title->str);
-        g_string_free(win_title, TRUE);
-        if (window != NULL) {
-            int num = wins_get_num(window);
-            ui_switch_win(num);
-        } else {
-            iq_request_room_config_form(room);
+        if (g_strcmp0(args[0], "destroy") == 0) {
+            iq_destroy_instant_room(room);
+            return TRUE;
         }
-        return TRUE;
-    }
 
-    if (g_strcmp0(args[1], "submit") == 0) {
-        ProfWin *current = wins_get_current();
-        if (current->type != WIN_MUC_CONFIG) {
-            cons_show("Room configuration can only be submitted when in the room configuration window.");
+        if (g_strcmp0(args[0], "config") == 0) {
+            GString *win_title = g_string_new(room);
+            g_string_append(win_title, " config");
+            ProfWin *window = wins_get_by_recipient(win_title->str);
+            g_string_free(win_title, TRUE);
+            if (window != NULL) {
+                num = wins_get_num(window);
+                ui_switch_win(num);
+            } else {
+                iq_request_room_config_form(room);
+            }
             return TRUE;
-        } else {
+        }
+    }
+
+    // commands allowed in room config
+    if ((g_strcmp0(args[0], "submit") == 0) ||
+            (g_strcmp0(args[0], "cancel") == 0)) {
+
+        if (g_strcmp0(args[0], "submit") == 0) {
+            ProfWin *current = wins_get_current();
             gchar **split_recipient = g_strsplit(room, " ", 2);
             room = split_recipient[0];
             iq_submit_room_config(room, current->form);
             g_strfreev(split_recipient);
             return TRUE;
         }
-    }
 
-    if (g_strcmp0(args[1], "cancel") == 0) {
-        iq_room_config_cancel(room);
-        return TRUE;
+        if (g_strcmp0(args[0], "cancel") == 0) {
+            iq_room_config_cancel(room);
+            return TRUE;
+        }
     }
 
     return TRUE;