From 30942f7f62e47eec761a63ca4da7767859c82ce6 Mon Sep 17 00:00:00 2001 From: Sebastien Helleu Date: Sat, 25 Jan 2014 13:29:03 +0100 Subject: trigger: add trigger plugin --- src/plugins/trigger/trigger-command.c | 352 ++++++++++++++++++++++++++++++++++ 1 file changed, 352 insertions(+) create mode 100644 src/plugins/trigger/trigger-command.c (limited to 'src/plugins/trigger/trigger-command.c') diff --git a/src/plugins/trigger/trigger-command.c b/src/plugins/trigger/trigger-command.c new file mode 100644 index 000000000..a38ffcc28 --- /dev/null +++ b/src/plugins/trigger/trigger-command.c @@ -0,0 +1,352 @@ +/* + * trigger-command.c - trigger command + * + * Copyright (C) 2014 Sébastien Helleu + * + * This file is part of WeeChat, the extensible chat client. + * + * WeeChat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * WeeChat is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with WeeChat. If not, see . + */ + +#include +#include + +#include "../weechat-plugin.h" +#include "trigger.h" +#include "trigger-buffer.h" +#include "trigger-config.h" + + +/* + * Callback for command "/trigger": manage triggers. + */ + +int +trigger_command_trigger (void *data, struct t_gui_buffer *buffer, int argc, + char **argv, char **argv_eol) +{ + struct t_trigger *ptr_trigger; + const char *option; + int i, type, count, index_option, enabled; + + /* make C compiler happy */ + (void) data; + (void) buffer; + + /* list all triggers */ + if ((argc == 1) + || ((argc == 2) && (weechat_strcasecmp (argv[1], "list") == 0))) + { + if (triggers) + { + weechat_printf_tags (NULL, "no_trigger", ""); + weechat_printf_tags (NULL, "no_trigger", _("List of triggers:")); + for (ptr_trigger = triggers; ptr_trigger; + ptr_trigger = ptr_trigger->next_trigger) + { + weechat_printf_tags (NULL, "no_trigger", + " %s: %s, \"%s\" (%d hooks, %d/%d) %s", + ptr_trigger->name, + weechat_config_string (ptr_trigger->options[TRIGGER_OPTION_HOOK]), + weechat_config_string (ptr_trigger->options[TRIGGER_OPTION_ARGUMENTS]), + ptr_trigger->hooks_count, + ptr_trigger->hook_count_cb, + ptr_trigger->hook_count_cmd, + weechat_config_boolean (ptr_trigger->options[TRIGGER_OPTION_ENABLED]) ? + "" : _("(disabled)")); + option = weechat_config_string (ptr_trigger->options[TRIGGER_OPTION_CONDITIONS]); + if (option && option[0]) + { + weechat_printf_tags (NULL, "no_trigger", + " conditions: \"%s\"", option); + } + if (ptr_trigger->regex_count > 0) + { + weechat_printf_tags (NULL, "no_trigger", + " %d regex:", + ptr_trigger->regex_count); + for (i = 0; i < ptr_trigger->regex_count; i++) + { + weechat_printf_tags (NULL, "no_trigger", + " %d: %s%s %s-->%s %s", + i + 1, + weechat_color (weechat_config_string (trigger_config_color_regex)), + ptr_trigger->regex[i].str_regex, + weechat_color ("chat_delimiters"), + weechat_color (weechat_config_string (trigger_config_color_replace)), + ptr_trigger->regex[i].replace); + } + } + option = weechat_config_string (ptr_trigger->options[TRIGGER_OPTION_COMMAND]); + if (option && option[0]) + { + weechat_printf_tags (NULL, "no_trigger", + " command: \"%s\"", option); + } + } + } + else + { + weechat_printf_tags (NULL, "no_trigger", _("No trigger defined")); + } + return WEECHAT_RC_OK; + } + + /* add a trigger */ + if (weechat_strcasecmp (argv[1], "add") == 0) + { + if (argc < 4) + { + weechat_printf_tags (NULL, "no_trigger", + _("%sError: missing arguments for \"%s\" " + "command"), + weechat_prefix ("error"), "trigger"); + return WEECHAT_RC_OK; + } + type = trigger_search_hook_type (argv[3]); + 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; + } + ptr_trigger = trigger_alloc (argv[2]); + 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; + } + if (trigger_new (argv[2], "on", argv[3], + (argc > 4) ? argv_eol[4] : "", + "", "", "", "ok")) + { + weechat_printf_tags (NULL, "no_trigger", + _("Trigger \"%s\" created"), argv[2]); + } + else + { + weechat_printf_tags (NULL, "no_trigger", + _("%sError: failed to create trigger \"%s\""), + weechat_prefix ("error"), argv[2]); + } + return WEECHAT_RC_OK; + } + + /* set option in a trigger */ + if (weechat_strcasecmp (argv[1], "set") == 0) + { + if (argc < 5) + { + weechat_printf_tags (NULL, "no_trigger", + _("%sError: missing arguments for \"%s\" " + "command"), + weechat_prefix ("error"), "trigger"); + return WEECHAT_RC_OK; + } + ptr_trigger = trigger_search (argv[2]); + if (!ptr_trigger) + { + weechat_printf_tags (NULL, "no_trigger", + _("%sTrigger \"%s\" not found"), + weechat_prefix ("error"), argv[2]); + return WEECHAT_RC_OK; + } + if (weechat_strcasecmp (argv[3], "name") == 0) + { + trigger_rename (ptr_trigger, argv_eol[4]); + } + else + { + index_option = trigger_search_option (argv[3]); + if (index_option < 0) + { + weechat_printf_tags (NULL, "no_trigger", + _("%sTrigger option \"%s\" not found"), + weechat_prefix ("error"), argv[3]); + return WEECHAT_RC_OK; + } + weechat_config_option_set (ptr_trigger->options[index_option], + argv_eol[4], 1); + } + weechat_printf_tags (NULL, "no_trigger", + _("Trigger \"%s\" updated"), ptr_trigger->name); + return WEECHAT_RC_OK; + } + + /* delete a trigger */ + if (weechat_strcasecmp (argv[1], "del") == 0) + { + if (argc < 3) + { + weechat_printf_tags (NULL, "no_trigger", + _("%sError: missing arguments for \"%s\" " + "command"), + weechat_prefix ("error"), "trigger"); + return WEECHAT_RC_OK; + } + if (weechat_strcasecmp (argv[2], "-all") == 0) + { + count = triggers_count; + trigger_free_all (); + if (count > 0) + weechat_printf_tags (NULL, "no_trigger", + _("%d triggers removed"), count); + } + else + { + for (i = 2; i < argc; i++) + { + ptr_trigger = trigger_search (argv[i]); + if (ptr_trigger) + { + trigger_free (ptr_trigger); + weechat_printf_tags (NULL, "no_trigger", + _("Trigger \"%s\" removed"), argv[i]); + } + else + { + weechat_printf_tags (NULL, "no_trigger", + _("%sTrigger \"%s\" not found"), + weechat_prefix ("error"), argv[i]); + } + } + } + return WEECHAT_RC_OK; + } + + /* enable/disable/toggle a trigger */ + if ((weechat_strcasecmp (argv[1], "enable") == 0) + || (weechat_strcasecmp (argv[1], "disable") == 0) + || (weechat_strcasecmp (argv[1], "toggle") == 0)) + { + if (argc < 3) + { + weechat_printf_tags (NULL, "no_trigger", + _("%sError: missing arguments for \"%s\" " + "command"), + weechat_prefix ("error"), "trigger"); + return WEECHAT_RC_OK; + } + ptr_trigger = trigger_search (argv[2]); + if (!ptr_trigger) + { + weechat_printf_tags (NULL, "no_trigger", + _("%sTrigger \"%s\" not found"), + weechat_prefix ("error"), argv[2]); + return WEECHAT_RC_OK; + } + if (weechat_strcasecmp (argv[1], "enable") == 0) + enabled = 1; + else if (weechat_strcasecmp (argv[1], "disable") == 0) + enabled = 0; + else + { + enabled = weechat_config_boolean (ptr_trigger->options[TRIGGER_OPTION_ENABLED]) ? + 0 : 1; + } + weechat_config_option_set (ptr_trigger->options[TRIGGER_OPTION_ENABLED], + (enabled) ? "on" : "off", 1); + weechat_printf_tags (NULL, "no_trigger", + (enabled) ? + _("Trigger \"%s\" enabled") : + _("Trigger \"%s\" disabled"), + ptr_trigger->name); + return WEECHAT_RC_OK; + } + + /* open the trigger monitor buffer */ + if (weechat_strcasecmp (argv[1], "monitor") == 0) + { + trigger_buffer_open (1); + return WEECHAT_RC_OK; + } + + return WEECHAT_RC_OK; +} + +/* + * Hooks trigger commands. + */ + +void +trigger_command_init () +{ + weechat_hook_command ( + "trigger", + N_("manage triggers"), + N_("list" + " || add []" + " || set