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

Added /room command validation, reduced to one subcommand

This commit is contained in:
James Booth 2014-09-11 00:01:44 +01:00
parent eba3a7cb30
commit 1e26b7a4ff
2 changed files with 75 additions and 66 deletions

View File

@ -305,7 +305,7 @@ static struct cmd_t command_defs[] =
NULL } } }, NULL } } },
{ "/room", { "/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", "Room configuration.",
{ "/room config accept|destroy|edit|cancel", { "/room config accept|destroy|edit|cancel",
"---------------------------------------", "---------------------------------------",
@ -956,7 +956,6 @@ static Autocomplete alias_ac;
static Autocomplete aliases_ac; static Autocomplete aliases_ac;
static Autocomplete join_property_ac; static Autocomplete join_property_ac;
static Autocomplete room_ac; static Autocomplete room_ac;
static Autocomplete room_config_ac;
/* /*
* Initialise command autocompleter and history * Initialise command autocompleter and history
@ -1209,13 +1208,11 @@ cmd_init(void)
autocomplete_add(alias_ac, "list"); autocomplete_add(alias_ac, "list");
room_ac = autocomplete_new(); room_ac = autocomplete_new();
autocomplete_add(room_ac, "accept");
autocomplete_add(room_ac, "destroy");
autocomplete_add(room_ac, "config"); autocomplete_add(room_ac, "config");
autocomplete_add(room_ac, "submit");
room_config_ac = autocomplete_new(); autocomplete_add(room_ac, "cancel");
autocomplete_add(room_config_ac, "accept");
autocomplete_add(room_config_ac, "destroy");
autocomplete_add(room_config_ac, "edit");
autocomplete_add(room_config_ac, "cancel");
cmd_history_init(); cmd_history_init();
} }
@ -1261,7 +1258,6 @@ cmd_uninit(void)
autocomplete_free(aliases_ac); autocomplete_free(aliases_ac);
autocomplete_free(join_property_ac); autocomplete_free(join_property_ac);
autocomplete_free(room_ac); autocomplete_free(room_ac);
autocomplete_free(room_config_ac);
} }
gboolean gboolean
@ -1386,7 +1382,6 @@ cmd_reset_autocomplete()
autocomplete_reset(aliases_ac); autocomplete_reset(aliases_ac);
autocomplete_reset(join_property_ac); autocomplete_reset(join_property_ac);
autocomplete_reset(room_ac); autocomplete_reset(room_ac);
autocomplete_reset(room_config_ac);
bookmark_autocomplete_reset(); bookmark_autocomplete_reset();
} }
@ -2080,11 +2075,6 @@ _room_autocomplete(char *input, int *size)
{ {
char *result = NULL; 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); result = autocomplete_param_with_ac(input, size, "/room", room_ac, TRUE);
if (result != NULL) { if (result != NULL) {
return result; return result;

View File

@ -1797,27 +1797,45 @@ cmd_room(gchar **args, struct cmd_help_t help)
return TRUE; return TRUE;
} }
// room command allowed in window
win_type_t win_type = ui_current_win_type(); win_type_t win_type = ui_current_win_type();
if (win_type != WIN_MUC && win_type != WIN_MUC_CONFIG) { 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; 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); cons_show("Usage: %s", help.usage);
return TRUE; return TRUE;
} }
if ((g_strcmp0(args[1], "accept") != 0) && // validate subcommand for window type
(g_strcmp0(args[1], "cancel") != 0) && if (win_type == WIN_MUC &&
(g_strcmp0(args[1], "destroy") != 0) && ((g_strcmp0(args[0], "submit") == 0) ||
(g_strcmp0(args[1], "submit") != 0) && (g_strcmp0(args[0], "cancel") == 0))) {
(g_strcmp0(args[1], "edit") != 0)) { cons_show("Command '/room %s' only allowed in room configuration windows.", args[0]);
cons_show("Usage: %s", help.usage); 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; return TRUE;
} }
char *room = ui_current_recipient(); char *room = ui_current_recipient();
// 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); ProfWin *window = wins_get_by_recipient(room);
int num = wins_get_num(window); int num = wins_get_num(window);
int ui_index = num; int ui_index = num;
@ -1825,7 +1843,7 @@ cmd_room(gchar **args, struct cmd_help_t help)
ui_index = 0; ui_index = 0;
} }
if (g_strcmp0(args[1], "accept") == 0) { if (g_strcmp0(args[0], "accept") == 0) {
gboolean requires_config = muc_requires_config(room); gboolean requires_config = muc_requires_config(room);
if (!requires_config) { if (!requires_config) {
win_save_vprint(window, '!', NULL, 0, COLOUR_ROOMINFO, "", "Current room does not require configuration."); win_save_vprint(window, '!', NULL, 0, COLOUR_ROOMINFO, "", "Current room does not require configuration.");
@ -1839,43 +1857,44 @@ cmd_room(gchar **args, struct cmd_help_t help)
} }
} }
if (g_strcmp0(args[1], "destroy") == 0) { if (g_strcmp0(args[0], "destroy") == 0) {
iq_destroy_instant_room(room); iq_destroy_instant_room(room);
return TRUE; return TRUE;
} }
if (g_strcmp0(args[1], "edit") == 0) { if (g_strcmp0(args[0], "config") == 0) {
GString *win_title = g_string_new(room); GString *win_title = g_string_new(room);
g_string_append(win_title, " config"); g_string_append(win_title, " config");
ProfWin *window = wins_get_by_recipient(win_title->str); ProfWin *window = wins_get_by_recipient(win_title->str);
g_string_free(win_title, TRUE); g_string_free(win_title, TRUE);
if (window != NULL) { if (window != NULL) {
int num = wins_get_num(window); num = wins_get_num(window);
ui_switch_win(num); ui_switch_win(num);
} else { } else {
iq_request_room_config_form(room); iq_request_room_config_form(room);
} }
return TRUE; return TRUE;
} }
}
if (g_strcmp0(args[1], "submit") == 0) { // 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(); 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.");
return TRUE;
} else {
gchar **split_recipient = g_strsplit(room, " ", 2); gchar **split_recipient = g_strsplit(room, " ", 2);
room = split_recipient[0]; room = split_recipient[0];
iq_submit_room_config(room, current->form); iq_submit_room_config(room, current->form);
g_strfreev(split_recipient); g_strfreev(split_recipient);
return TRUE; return TRUE;
} }
}
if (g_strcmp0(args[1], "cancel") == 0) { if (g_strcmp0(args[0], "cancel") == 0) {
iq_room_config_cancel(room); iq_room_config_cancel(room);
return TRUE; return TRUE;
} }
}
return TRUE; return TRUE;
} }