From 7bd12ea7b23f0fd05242a9fd332c5b8c8c9bfe73 Mon Sep 17 00:00:00 2001 From: John Hernandez <129467592+H3rnand3zzz@users.noreply.github.com> Date: Thu, 19 Oct 2023 17:45:19 +0200 Subject: [PATCH] Add params support for aliases Before aliases used spaces in their name, now the alias part is being read only before the first space, thus allowing execution of complex command with aliases. Example (with plugin): `/alias add echo "/system exec echo"` will allow execution of `/echo test` as opposed to prior state when the Profanity will search for alias "echo tests" and output `Unknown command: /echo test` Minor change: removed an example with invalid command (`/away`) --- src/command/cmd_defs.c | 4 ++-- src/command/cmd_funcs.c | 23 ++++++++++++++++------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/command/cmd_defs.c b/src/command/cmd_defs.c index ea59bb6d3..0d2d9b958 100644 --- a/src/command/cmd_defs.c +++ b/src/command/cmd_defs.c @@ -1643,12 +1643,12 @@ static const struct cmd_t command_defs[] = { "Add, remove or list command aliases.") CMD_ARGS( { "list", "List all aliases." }, - { "add ", "Add a new command alias." }, + { "add ", "Add a new command alias. The alias name should not contain any space characters." }, { "remove ", "Remove a command alias." }) CMD_EXAMPLES( "/alias add friends /who online friends", "/alias add /q /quit", - "/alias add a /away \"I'm in a meeting.\"", + "/alias add urg /msg odin@valhalla.edda [URGENT]", "/alias remove q", "/alias list") }, diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c index e778211bf..1871f3a75 100644 --- a/src/command/cmd_funcs.c +++ b/src/command/cmd_funcs.c @@ -5091,6 +5091,10 @@ cmd_alias(ProfWin* window, const char* const command, gchar** args) cons_bad_cmd_usage(command); return TRUE; } else { + if (strchr(alias, ' ')) { + cons_bad_cmd_usage(command); + return TRUE; + } char* alias_p = alias; GString* ac_value = g_string_new(""); if (alias[0] == '/') { @@ -8565,15 +8569,20 @@ _cmd_execute_alias(ProfWin* window, const char* const inp, gboolean* ran) } auto_char char* alias = strdup(inp + 1); - auto_gchar gchar* value = prefs_get_alias(alias); - if (value) { - *ran = TRUE; - gboolean result = cmd_process_input(window, value); - return result; + auto_gcharv char** alias_parts = g_strsplit(alias, " ", 2); + auto_gchar gchar* value = prefs_get_alias(alias_parts[0]); + + if (!value) { + *ran = FALSE; + return TRUE; } - *ran = FALSE; - return TRUE; + char* params = alias_parts[1]; + auto_gchar gchar* full_cmd = params ? g_strdup_printf("%s %s", value, params) : g_strdup(value); + + *ran = TRUE; + gboolean result = cmd_process_input(window, full_cmd); + return result; } // helper function for status change commands