diff options
author | Sebastien Helleu <flashcode@flashtux.org> | 2014-01-31 13:08:59 +0100 |
---|---|---|
committer | Sebastien Helleu <flashcode@flashtux.org> | 2014-02-09 12:55:40 +0100 |
commit | bb48af29b60abde617ec51244db8207f467ab40c (patch) | |
tree | cd872b185116d040195244f44fe9e830b7b7e296 /src/plugins | |
parent | b5bfeb7b8daeac9c27c2ecc63b38cdfab1a8e189 (diff) | |
download | weechat-bb48af29b60abde617ec51244db8207f467ab40c.zip |
trigger: split arguments like shell in "/trigger add", to create a whole trigger with one command
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/trigger/trigger-command.c | 218 | ||||
-rw-r--r-- | src/plugins/trigger/trigger.c | 44 | ||||
-rw-r--r-- | src/plugins/trigger/trigger.h | 2 |
3 files changed, 171 insertions, 93 deletions
diff --git a/src/plugins/trigger/trigger-command.c b/src/plugins/trigger/trigger-command.c index cd1beb80a..281b2de75 100644 --- a/src/plugins/trigger/trigger-command.c +++ b/src/plugins/trigger/trigger-command.c @@ -56,6 +56,64 @@ trigger_command_set_enabled (struct t_trigger *trigger, int enable) } /* + * Renames a trigger and displays error if the rename is not possible. + * + * This function is called by commands: + * /trigger set OLD name NEW + * /trigger rename OLD NEW + */ + +void +trigger_command_rename (struct t_trigger *trigger, const char *new_name) +{ + char *name, *name2; + + name = strdup (trigger->name); + name2 = weechat_string_remove_quotes (new_name, "'\""); + + if (name && name2) + { + /* check that new name is valid */ + if (!trigger_name_valid (name2)) + { + weechat_printf_tags (NULL, "no_trigger", + _("%sError: invalid name for trigger"), + weechat_prefix ("error")); + goto end; + } + /* check that no trigger already exists with the new name */ + if (trigger_search (name2)) + { + weechat_printf_tags (NULL, "no_trigger", + _("%sError: trigger \"%s\" already " + "exists"), + weechat_prefix ("error"), name2); + goto end; + } + /* rename the trigger */ + if (trigger_rename (trigger, name2)) + { + weechat_printf_tags (NULL, "no_trigger", + _("Trigger \"%s\" renamed to \"%s\""), + name, trigger->name); + } + else + { + weechat_printf_tags (NULL, "no_trigger", + _("%sError: failed to rename trigger " + "\"%s\""), + weechat_prefix ("error"), name); + } + } + +end: + if (name) + free (name); + if (name2) + free (name2); +} + +/* * Callback for command "/trigger": manage triggers. */ @@ -65,13 +123,15 @@ trigger_command_trigger (void *data, struct t_gui_buffer *buffer, int argc, { struct t_trigger *ptr_trigger; const char *option; - char *name, *value; - int i, type, count, index_option, enable; + char *value, **sargv; + int i, type, count, index_option, enable, sargc; /* make C compiler happy */ (void) data; (void) buffer; + sargv = NULL; + /* list all triggers */ if ((argc == 1) || ((argc == 2) && (weechat_strcasecmp (argv[1], "list") == 0))) @@ -128,59 +188,72 @@ trigger_command_trigger (void *data, struct t_gui_buffer *buffer, int argc, { weechat_printf_tags (NULL, "no_trigger", _("No trigger defined")); } - return WEECHAT_RC_OK; + goto end; } /* add a trigger */ if (weechat_strcasecmp (argv[1], "add") == 0) { - if (argc < 4) + sargv = weechat_string_split_shell (argv_eol[2], &sargc); + if (!sargv || (sargc < 2)) { weechat_printf_tags (NULL, "no_trigger", _("%sError: missing arguments for \"%s\" " "command"), weechat_prefix ("error"), "trigger"); - return WEECHAT_RC_OK; + goto end; } - if (argv[2][0] == '-') + if (!trigger_name_valid (sargv[0])) { - weechat_printf (NULL, - _("%s%s: name can not start with \"-\""), - weechat_prefix ("error"), TRIGGER_PLUGIN_NAME); - return WEECHAT_RC_OK; + weechat_printf_tags (NULL, "no_trigger", + _("%sError: invalid name for trigger"), + weechat_prefix ("error")); + goto end; } - type = trigger_search_hook_type (argv[3]); + type = trigger_search_hook_type (sargv[1]); if (type < 0) { weechat_printf_tags (NULL, "no_trigger", - _("%s%s: invalid hook type \"%s\""), - weechat_prefix ("error"), TRIGGER_PLUGIN_NAME, - argv[3]); - return WEECHAT_RC_OK; + _("%sError: invalid hook type \"%s\""), + weechat_prefix ("error"), sargv[1]); + goto end; } - ptr_trigger = trigger_alloc (argv[2]); + if ((sargc > 6) && (trigger_search_return_code (sargv[6]) < 0)) + { + weechat_printf_tags (NULL, "no_trigger", + _("%sError: invalid return code \"%s\""), + weechat_prefix ("error"), sargv[6]); + goto end; + } + ptr_trigger = trigger_alloc (sargv[0]); if (!ptr_trigger) { weechat_printf_tags (NULL, "no_trigger", - _("%s%s: error creating trigger \"%s\""), - weechat_prefix ("error"), TRIGGER_PLUGIN_NAME, - argv[2]); - return WEECHAT_RC_OK; + _("%sError: failed to create trigger \"%s\""), + weechat_prefix ("error"), sargv[0]); + goto end; } - if (trigger_new (argv[2], "on", argv[3], - (argc > 4) ? argv_eol[4] : "", - "", "", "", "ok")) + ptr_trigger = trigger_new ( + sargv[0], /* name */ + "on", /* enabled */ + sargv[1], /* hook */ + (sargc > 2) ? sargv[2] : "", /* arguments */ + (sargc > 3) ? sargv[3] : "", /* conditions */ + (sargc > 4) ? sargv[4] : "", /* regex */ + (sargc > 5) ? sargv[5] : "", /* command */ + (sargc > 6) ? sargv[6] : ""); /* return code */ + if (ptr_trigger) { weechat_printf_tags (NULL, "no_trigger", - _("Trigger \"%s\" created"), argv[2]); + _("Trigger \"%s\" created"), sargv[0]); } else { weechat_printf_tags (NULL, "no_trigger", _("%sError: failed to create trigger \"%s\""), - weechat_prefix ("error"), argv[2]); + weechat_prefix ("error"), sargv[0]); } - return WEECHAT_RC_OK; + goto end; } /* set option in a trigger */ @@ -192,34 +265,20 @@ trigger_command_trigger (void *data, struct t_gui_buffer *buffer, int argc, _("%sError: missing arguments for \"%s\" " "command"), weechat_prefix ("error"), "trigger"); - return WEECHAT_RC_OK; + goto end; } ptr_trigger = trigger_search (argv[2]); if (!ptr_trigger) { weechat_printf_tags (NULL, "no_trigger", - _("%sTrigger \"%s\" not found"), + _("%sError: trigger \"%s\" not found"), weechat_prefix ("error"), argv[2]); - return WEECHAT_RC_OK; + goto end; } if (weechat_strcasecmp (argv[3], "name") == 0) { - value = weechat_string_remove_quotes (argv[4], "'\""); - name = strdup (ptr_trigger->name); - if (value && name) - { - if (trigger_rename (ptr_trigger, value)) - { - weechat_printf_tags (NULL, "no_trigger", - _("Trigger \"%s\" renamed to \"%s\""), - name, ptr_trigger->name); - } - } - if (name) - free (name); - if (value) - free (value); - return WEECHAT_RC_OK; + trigger_command_rename (ptr_trigger, argv[4]); + goto end; } value = weechat_string_remove_quotes (argv_eol[4], "'\""); if (value) @@ -236,12 +295,13 @@ trigger_command_trigger (void *data, struct t_gui_buffer *buffer, int argc, else { weechat_printf_tags (NULL, "no_trigger", - _("%sTrigger option \"%s\" not found"), + _("%sError: trigger option \"%s\" not " + "found"), weechat_prefix ("error"), argv[3]); } free (value); } - return WEECHAT_RC_OK; + goto end; } /* rename a trigger */ @@ -253,28 +313,18 @@ trigger_command_trigger (void *data, struct t_gui_buffer *buffer, int argc, _("%sError: missing arguments for \"%s\" " "command"), weechat_prefix ("error"), "trigger"); - return WEECHAT_RC_OK; + goto end; } ptr_trigger = trigger_search (argv[2]); if (!ptr_trigger) { weechat_printf_tags (NULL, "no_trigger", - _("%sTrigger \"%s\" not found"), + _("%sError: trigger \"%s\" not found"), weechat_prefix ("error"), argv[2]); - return WEECHAT_RC_OK; + goto end; } - name = strdup (ptr_trigger->name); - if (name) - { - if (trigger_rename (ptr_trigger, argv[3])) - { - weechat_printf_tags (NULL, "no_trigger", - _("Trigger \"%s\" renamed to \"%s\""), - name, ptr_trigger->name); - } - free (name); - } - return WEECHAT_RC_OK; + trigger_command_rename (ptr_trigger, argv[3]); + goto end; } /* enable/disable/toggle trigger(s) */ @@ -288,7 +338,7 @@ trigger_command_trigger (void *data, struct t_gui_buffer *buffer, int argc, _("%sError: missing arguments for \"%s\" " "command"), weechat_prefix ("error"), "trigger"); - return WEECHAT_RC_OK; + goto end; } enable = -1; if (weechat_strcasecmp (argv[1], "enable") == 0) @@ -318,7 +368,7 @@ trigger_command_trigger (void *data, struct t_gui_buffer *buffer, int argc, } } } - return WEECHAT_RC_OK; + goto end; } /* delete trigger(s) */ @@ -330,7 +380,7 @@ trigger_command_trigger (void *data, struct t_gui_buffer *buffer, int argc, _("%sError: missing arguments for \"%s\" " "command"), weechat_prefix ("error"), "trigger"); - return WEECHAT_RC_OK; + goto end; } if (weechat_strcasecmp (argv[2], "-all") == 0) { @@ -359,16 +409,20 @@ trigger_command_trigger (void *data, struct t_gui_buffer *buffer, int argc, } } } - return WEECHAT_RC_OK; + goto end; } /* open the trigger monitor buffer */ if (weechat_strcasecmp (argv[1], "monitor") == 0) { trigger_buffer_open (1); - return WEECHAT_RC_OK; + goto end; } +end: + if (sargv) + weechat_string_free_split (sargv); + return WEECHAT_RC_OK; } @@ -383,7 +437,8 @@ trigger_command_init () "trigger", N_("manage triggers"), N_("list" - " || add <name> <hook> [<arguments>]" + " || add <name> <hook> [\"<arguments>\" [\"<conditions>\" " + "[\"<regex>\" [\"<command>\" [\"<return_code>\"]]]]]" " || set <name> <option> <value>" " || rename <name> <new_name>" " || enable|disable|toggle <name>|-all [<name>...]" @@ -392,10 +447,11 @@ trigger_command_init () N_(" add: add a trigger\n" " name: name of trigger\n" " hook: signal, hsignal, modifier, print, timer\n" - "arguments: arguments for the hook, depending on hook:\n" - " signal: name of signal\n" - " hsignal: name of hsignal\n" - " modifier: name of modifier\n" + "arguments: arguments for the hook, depending on hook (separated by " + "semicolons):\n" + " signal: name(s) of signal\n" + " hsignal: name(s) of hsignal\n" + " modifier: name(s) of modifier\n" " print: buffer, tags, message, strip_colors\n" " timer: interval, align_second, max_calls\n" " set: set an option in a trigger\n" @@ -423,19 +479,19 @@ trigger_command_init () "\n" "Examples:\n" " send alert (BEL) on highlight or private message:\n" - " /trigger add beep print\n" - " /trigger set beep conditions ${tg_highlight} || ${tg_msg_pv}\n" - " /trigger set beep command /print -stderr \\a\n" + " /trigger add beep print \"\" \"${tg_highlight} || ${tg_msg_pv}\" " + "\"\" \"/print -stderr \\a\"\n" " replace password with '*' in /oper command (in command line and " "command history):\n" - " /trigger add oper modifier input_text_display;history_add\n" - " /trigger set oper regex ==^(/oper +\\S+ +)(.*)==\\1\\*2\n" + " /trigger add oper modifier input_text_display;history_add " + "\"\" \"==^(/oper +\\S+ +)(.*)==\\1\\*2\"\n" " add text attributes in *bold*, _underline_ and /italic/:\n" - " /trigger add effects modifier weechat_print\n" - " /trigger set effects regex " - "==\\*(\\S+)\\*==*${color:bold}\\1${color:-bold}*== " + " /trigger add effects modifier weechat_print \"\" " + "\"==\\*(\\S+)\\*==*${color:bold}\\1${color:-bold}*== " "==_(\\S+)_==_${color:underline}\\1${color:-underline}_== " - "==/(\\S+)/==/${color:italic}\\1${color:-italic}/"), + "==/(\\S+)/==/${color:italic}\\1${color:-italic}/\"\n" + " silently save config each hour:\n" + " /trigger add cfgsave timer 3600000;0;0 \"\" \"\" \"/mute /save\""), "list" " || add %(trigger_names) %(trigger_hooks)" " || set %(trigger_names) %(trigger_options)|name %(trigger_option_value)" diff --git a/src/plugins/trigger/trigger.c b/src/plugins/trigger/trigger.c index c4118cc07..62235a94f 100644 --- a/src/plugins/trigger/trigger.c +++ b/src/plugins/trigger/trigger.c @@ -558,6 +558,29 @@ trigger_hook (struct t_trigger *trigger) } /* + * Checks if a trigger name is valid: it must not start with "-" and not have + * any spaces. + * + * Returns: + * 1: name is valid + * 0: name is invalid + */ + +int +trigger_name_valid (const char *name) +{ + if (!name || !name[0] || (name[0] == '-')) + return 0; + + /* no spaces allowed */ + if (strchr (name, ' ')) + return 0; + + /* name is valid */ + return 1; +} + +/* * Allocates and initializes new trigger structure. * * Returns pointer to new trigger, NULL if error. @@ -569,6 +592,9 @@ trigger_alloc (const char *name) struct t_trigger *new_trigger; int i; + if (!trigger_name_valid (name)) + return NULL; + if (trigger_search (name)) return NULL; @@ -660,17 +686,16 @@ trigger_new (const char *name, const char *enabled, const char *hook, struct t_trigger *new_trigger; int i; - /* it's not possible to create 2 triggers with same name */ - if (trigger_search (name)) - return NULL; - /* look for type */ if (trigger_search_hook_type (hook) < 0) return NULL; /* look for return code */ - if (trigger_search_return_code (return_code) < 0) + if (return_code && return_code[0] + && (trigger_search_return_code (return_code) < 0)) + { return NULL; + } value[TRIGGER_OPTION_ENABLED] = enabled; value[TRIGGER_OPTION_HOOK] = hook; @@ -711,14 +736,9 @@ trigger_rename (struct t_trigger *trigger, const char *name) int length, i; char *option_name; - if (!name || !name[0]) - return 0; - - if (name[0] == '-') + if (!name || !name[0] || !trigger_name_valid (name) + || trigger_search (name)) { - weechat_printf_tags (NULL, "no_trigger", - _("%s%s: name can not start with \"-\""), - weechat_prefix ("error"), TRIGGER_PLUGIN_NAME); return 0; } diff --git a/src/plugins/trigger/trigger.h b/src/plugins/trigger/trigger.h index ed7a05b54..14f61b3fe 100644 --- a/src/plugins/trigger/trigger.h +++ b/src/plugins/trigger/trigger.h @@ -106,11 +106,13 @@ extern struct t_trigger *last_trigger_temp; extern int trigger_search_option (const char *option_name); extern int trigger_search_hook_type (const char *type); +extern int trigger_search_return_code (const char *return_code); extern struct t_trigger *trigger_search (const char *name); extern struct t_trigger *trigger_search_with_option (struct t_config_option *option); extern void trigger_set_regex (struct t_trigger *trigger); extern void trigger_unhook (struct t_trigger *trigger); extern void trigger_hook (struct t_trigger *trigger); +extern int trigger_name_valid (const char *name); extern struct t_trigger *trigger_alloc (const char *name); extern void trigger_add (struct t_trigger *trigger, struct t_trigger **triggers, struct t_trigger **last_trigger); |