From bb48af29b60abde617ec51244db8207f467ab40c Mon Sep 17 00:00:00 2001 From: Sebastien Helleu Date: Fri, 31 Jan 2014 13:08:59 +0100 Subject: [PATCH] trigger: split arguments like shell in "/trigger add", to create a whole trigger with one command --- src/plugins/trigger/trigger-command.c | 218 ++++++++++++++++---------- src/plugins/trigger/trigger.c | 44 ++++-- 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 @@ -55,6 +55,64 @@ trigger_command_set_enabled (struct t_trigger *trigger, int enable) trigger->name); } +/* + * 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 []" + " || add [\"\" [\"\" " + "[\"\" [\"\" [\"\"]]]]]" " || set