1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-09 19:23:13 +02:00

Merge branch 'trigger'

This commit is contained in:
Sebastien Helleu
2014-02-21 15:36:14 +01:00
46 changed files with 6749 additions and 741 deletions
+8 -4
View File
@@ -96,10 +96,6 @@ IF(ENABLE_SCRIPT)
ADD_SUBDIRECTORY( script )
ENDIF(ENABLE_SCRIPT)
IF(ENABLE_XFER)
ADD_SUBDIRECTORY( xfer )
ENDIF(ENABLE_XFER)
IF(ENABLE_SCRIPTS AND ENABLE_PERL)
FIND_PACKAGE(Perl)
IF(PERL_FOUND)
@@ -142,4 +138,12 @@ IF(ENABLE_SCRIPTS AND ENABLE_GUILE)
ENDIF(GUILE_FOUND)
ENDIF(ENABLE_SCRIPTS AND ENABLE_GUILE)
IF(ENABLE_TRIGGER)
ADD_SUBDIRECTORY( trigger )
ENDIF(ENABLE_TRIGGER)
IF(ENABLE_XFER)
ADD_SUBDIRECTORY( xfer )
ENDIF(ENABLE_XFER)
INSTALL(FILES weechat-plugin.h DESTINATION ${INCLUDEDIR})
+10 -6
View File
@@ -75,10 +75,6 @@ if PLUGIN_SCRIPT
script_dir = script
endif
if PLUGIN_XFER
xfer_dir = xfer
endif
if PLUGIN_PERL
perl_dir = perl
endif
@@ -103,10 +99,18 @@ if PLUGIN_GUILE
guile_dir = guile
endif
if PLUGIN_TRIGGER
trigger_dir = trigger
endif
if PLUGIN_XFER
xfer_dir = xfer
endif
SUBDIRS = . $(alias_dir) $(aspell_dir) $(charset_dir) $(fifo_dir) $(irc_dir) \
$(logger_dir) $(relay_dir) $(rmodifier_dir) $(script_dir) $(xfer_dir) \
$(logger_dir) $(relay_dir) $(rmodifier_dir) $(script_dir) \
$(perl_dir) $(python_dir) $(ruby_dir) $(lua_dir) $(tcl_dir) \
$(guile_dir)
$(guile_dir) $(trigger_dir) $(xfer_dir)
EXTRA_DIST = CMakeLists.txt
+4
View File
@@ -516,12 +516,15 @@ plugin_load (const char *filename, int argc, char **argv)
new_plugin->string_expand_home = &string_expand_home;
new_plugin->string_remove_quotes = &string_remove_quotes;
new_plugin->string_strip = &string_strip;
new_plugin->string_convert_escaped_chars = &string_convert_escaped_chars;
new_plugin->string_mask_to_regex = &string_mask_to_regex;
new_plugin->string_regex_flags = &string_regex_flags;
new_plugin->string_regcomp = &string_regcomp;
new_plugin->string_has_highlight = &string_has_highlight;
new_plugin->string_has_highlight_regex = &string_has_highlight_regex;
new_plugin->string_replace_regex = &string_replace_regex;
new_plugin->string_split = &string_split;
new_plugin->string_split_shell = &string_split_shell;
new_plugin->string_free_split = &string_free_split;
new_plugin->string_build_with_split_string = &string_build_with_split_string;
new_plugin->string_split_command = &string_split_command;
@@ -587,6 +590,7 @@ plugin_load (const char *filename, int argc, char **argv)
new_plugin->hashtable_has_key = &hashtable_has_key;
new_plugin->hashtable_map = &hashtable_map;
new_plugin->hashtable_map_string = &hashtable_map_string;
new_plugin->hashtable_dup = &hashtable_dup;
new_plugin->hashtable_get_integer = &hashtable_get_integer;
new_plugin->hashtable_get_string = &hashtable_get_string;
new_plugin->hashtable_set_pointer = &hashtable_set_pointer;
+31
View File
@@ -0,0 +1,31 @@
#
# Copyright (C) 2014 Sébastien Helleu <flashcode@flashtux.org>
#
# 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 <http://www.gnu.org/licenses/>.
#
ADD_LIBRARY(trigger MODULE
trigger.c trigger.h
trigger-buffer.c trigger-buffer.h
trigger-callback.c trigger-callback.h
trigger-command.c trigger-command.h
trigger-completion.c trigger-completion.h
trigger-config.c trigger-config.h)
SET_TARGET_PROPERTIES(trigger PROPERTIES PREFIX "")
TARGET_LINK_LIBRARIES(trigger)
INSTALL(TARGETS trigger LIBRARY DESTINATION ${LIBDIR}/plugins)
+42
View File
@@ -0,0 +1,42 @@
#
# Copyright (C) 2014 Sébastien Helleu <flashcode@flashtux.org>
#
# 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 <http://www.gnu.org/licenses/>.
#
AM_CPPFLAGS = -DLOCALEDIR=\"$(datadir)/locale\" $(TRIGGER_CFLAGS)
libdir = ${weechat_libdir}/plugins
lib_LTLIBRARIES = trigger.la
trigger_la_SOURCES = trigger.c \
trigger.h \
trigger-buffer.c \
trigger-buffer.h \
trigger-callback.c \
trigger-callback.h \
trigger-command.c \
trigger-command.h \
trigger-completion.c \
trigger-completion.h \
trigger-config.c \
trigger-config.h
trigger_la_LDFLAGS = -module -no-undefined
trigger_la_LIBADD = $(TRIGGER_LFLAGS)
EXTRA_DIST = CMakeLists.txt
+220
View File
@@ -0,0 +1,220 @@
/*
* trigger-buffer.c - debug buffer for triggers
*
* Copyright (C) 2014 Sébastien Helleu <flashcode@flashtux.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "../weechat-plugin.h"
#include "trigger.h"
#include "trigger-buffer.h"
#include "trigger-config.h"
struct t_gui_buffer *trigger_buffer = NULL;
/*
* Callback called for each entry in hashtable.
*/
void
trigger_buffer_hashtable_map_cb (void *data,
struct t_hashtable *hashtable,
const void *key, const void *value)
{
const char *value_type;
char *value_no_color;
/* make C compiler happy */
(void) data;
(void) hashtable;
value_type = weechat_hashtable_get_string (hashtable, "type_values");
if (!value_type)
return;
if (strcmp (value_type, "string") == 0)
{
value_no_color = (weechat_config_boolean (trigger_config_look_monitor_strip_colors)) ?
weechat_string_remove_color ((const char *)value, NULL) : NULL;
weechat_printf_tags (trigger_buffer, "no_trigger",
"\t %s: %s\"%s%s%s\"",
(char *)key,
weechat_color ("chat_delimiters"),
weechat_color ("reset"),
(value_no_color) ? value_no_color : (const char *)value,
weechat_color ("chat_delimiters"));
if (value_no_color)
free (value_no_color);
}
else if (strcmp (value_type, "pointer") == 0)
{
weechat_printf_tags (trigger_buffer, "no_trigger",
"\t %s: 0x%lx",
(char *)key,
value);
}
}
/*
* Displays a hashtable on trigger buffer.
*/
void
trigger_buffer_display_hashtable (const char *name,
struct t_hashtable *hashtable)
{
if (!trigger_buffer)
return;
weechat_printf_tags (trigger_buffer, "no_trigger", " %s:", name);
weechat_hashtable_map (hashtable, &trigger_buffer_hashtable_map_cb, NULL);
}
/*
* Displays a trigger in trigger buffer.
*/
void
trigger_buffer_display_trigger (struct t_trigger *trigger,
struct t_gui_buffer *buffer,
struct t_hashtable *pointers,
struct t_hashtable *extra_vars)
{
if (!trigger_buffer)
return;
weechat_printf_tags (trigger_buffer, "no_trigger",
"%s\t%s%s %s(%s%s%s)",
trigger_hook_type_string[weechat_config_integer (trigger->options[TRIGGER_OPTION_HOOK])],
weechat_color (weechat_config_string (trigger_config_color_trigger)),
trigger->name,
weechat_color ("chat_delimiters"),
weechat_color ("reset"),
weechat_config_string (trigger->options[TRIGGER_OPTION_ARGUMENTS]),
weechat_color ("chat_delimiters"));
if (buffer)
{
weechat_printf_tags (trigger_buffer, "no_trigger",
"\t buffer: %s%s",
weechat_color ("chat_buffer"),
weechat_buffer_get_string (buffer, "full_name"));
}
if (pointers)
trigger_buffer_display_hashtable ("pointers", pointers);
if (extra_vars)
trigger_buffer_display_hashtable ("extra_vars", extra_vars);
}
/*
* Callback for user data in trigger buffer.
*/
int
trigger_buffer_input_cb (void *data, struct t_gui_buffer *buffer,
const char *input_data)
{
/* make C compiler happy */
(void) data;
/* close buffer */
if (strcmp (input_data, "q") == 0)
{
weechat_buffer_close (buffer);
return WEECHAT_RC_OK;
}
return WEECHAT_RC_OK;
}
/*
* Callback called when trigger buffer is closed.
*/
int
trigger_buffer_close_cb (void *data, struct t_gui_buffer *buffer)
{
/* make C compiler happy */
(void) data;
(void) buffer;
trigger_buffer = NULL;
return WEECHAT_RC_OK;
}
/*
* Restore buffer callbacks (input and close) for buffer created by trigger
* plugin.
*/
void
trigger_buffer_set_callbacks ()
{
struct t_gui_buffer *ptr_buffer;
ptr_buffer = weechat_buffer_search (TRIGGER_PLUGIN_NAME,
TRIGGER_BUFFER_NAME);
if (ptr_buffer)
{
trigger_buffer = ptr_buffer;
weechat_buffer_set_pointer (trigger_buffer, "close_callback",
&trigger_buffer_close_cb);
weechat_buffer_set_pointer (trigger_buffer, "input_callback",
&trigger_buffer_input_cb);
}
}
/*
* Opens script buffer.
*/
void
trigger_buffer_open (int switch_to_buffer)
{
if (!trigger_buffer)
{
trigger_buffer = weechat_buffer_new (TRIGGER_BUFFER_NAME,
&trigger_buffer_input_cb, NULL,
&trigger_buffer_close_cb, NULL);
/* failed to create buffer ? then return */
if (!trigger_buffer)
return;
weechat_buffer_set (trigger_buffer, "title", _("Trigger monitor"));
if (!weechat_buffer_get_integer (trigger_buffer, "short_name_is_set"))
weechat_buffer_set (trigger_buffer, "short_name", TRIGGER_BUFFER_NAME);
weechat_buffer_set (trigger_buffer, "localvar_set_type", "debug");
weechat_buffer_set (trigger_buffer, "localvar_set_server", TRIGGER_BUFFER_NAME);
weechat_buffer_set (trigger_buffer, "localvar_set_channel", TRIGGER_BUFFER_NAME);
weechat_buffer_set (trigger_buffer, "localvar_set_no_log", "1");
/* disable all highlights on this buffer */
weechat_buffer_set (trigger_buffer, "highlight_words", "-");
}
if (switch_to_buffer)
weechat_buffer_set (trigger_buffer, "display", "1");
}
+34
View File
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2014 Sébastien Helleu <flashcode@flashtux.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef __WEECHAT_TRIGGER_BUFFER_H
#define __WEECHAT_TRIGGER_BUFFER_H 1
#define TRIGGER_BUFFER_NAME "monitor"
struct t_gui_buffer *trigger_buffer;
extern void trigger_buffer_display_trigger (struct t_trigger *trigger,
struct t_gui_buffer *buffer,
struct t_hashtable *pointers,
struct t_hashtable *extra_vars);
extern void trigger_buffer_set_callbacks ();
extern void trigger_buffer_open (int switch_to_buffer);
#endif /* __WEECHAT_TRIGGER_BUFFER_H */
+913
View File
@@ -0,0 +1,913 @@
/*
* trigger-callback.c - callbacks for triggers
*
* Copyright (C) 2014 Sébastien Helleu <flashcode@flashtux.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "../weechat-plugin.h"
#include "trigger.h"
#include "trigger-callback.h"
#include "trigger-buffer.h"
/* one hashtable by hook, used in callback to evaluate "conditions" */
struct t_hashtable *trigger_callback_hashtable_options = NULL;
/*
* Parses an IRC message.
*
* Returns a hashtable with the parsed message, or NULL if error.
*
* Note: the hashtable must be freed after use.
*/
struct t_hashtable *
trigger_callback_irc_message_parse (const char *irc_message,
const char *irc_server)
{
struct t_hashtable *hashtable_in, *hashtable_out;
hashtable_out = NULL;
hashtable_in = weechat_hashtable_new (32,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING,
NULL,
NULL);
if (hashtable_in)
{
weechat_hashtable_set (hashtable_in, "message", irc_message);
weechat_hashtable_set (hashtable_in, "server", irc_server);
hashtable_out = weechat_info_get_hashtable ("irc_message_parse",
hashtable_in);
weechat_hashtable_free (hashtable_in);
}
return hashtable_out;
}
/*
* Sets variables in "extra_vars" hashtable using tags from message.
*
* Returns:
* 0: tag "no_trigger" was in tags, callback must NOT be executed
* 1: no tag "no_trigger", callback can be executed
*/
int
trigger_callback_set_tags (struct t_gui_buffer *buffer,
const char **tags, int tags_count,
struct t_hashtable *extra_vars)
{
const char *localvar_type;
char str_temp[128];
int i;
snprintf (str_temp, sizeof (str_temp), "%d", tags_count);
weechat_hashtable_set (extra_vars, "tg_tags_count", str_temp);
localvar_type = (buffer) ?
weechat_buffer_get_string (buffer, "localvar_type") : NULL;
for (i = 0; i < tags_count; i++)
{
if (strcmp (tags[i], "no_trigger") == 0)
{
return 0;
}
else if (strncmp (tags[i], "notify_", 7) == 0)
{
weechat_hashtable_set (extra_vars, "tg_tag_notify", tags[i] + 7);
if (strcmp (tags[i] + 7, "private") == 0)
{
snprintf (str_temp, sizeof (str_temp), "%d",
(localvar_type
&& (strcmp (localvar_type, "private") == 0)) ? 1 : 0);
weechat_hashtable_set (extra_vars, "tg_msg_pv", str_temp);
}
}
else if (strncmp (tags[i], "nick_", 5) == 0)
{
weechat_hashtable_set (extra_vars, "tg_tag_nick", tags[i] + 5);
}
else if (strncmp (tags[i], "prefix_nick_", 12) == 0)
{
weechat_hashtable_set (extra_vars, "tg_tag_prefix_nick",
tags[i] + 12);
}
}
return 1;
}
/*
* Checks conditions for a trigger.
*
* Returns:
* 1: conditions are true (or no condition set in trigger)
* 0: conditions are false
*/
int
trigger_callback_check_conditions (struct t_trigger *trigger,
struct t_hashtable *pointers,
struct t_hashtable *extra_vars)
{
const char *conditions;
char *value;
int rc;
conditions = weechat_config_string (trigger->options[TRIGGER_OPTION_CONDITIONS]);
if (!conditions || !conditions[0])
return 1;
value = weechat_string_eval_expression (conditions,
pointers,
extra_vars,
trigger_callback_hashtable_options);
rc = (value && (strcmp (value, "1") == 0));
if (value)
free (value);
return rc;
}
/*
* Replaces text using one or more regex in the trigger.
*/
void
trigger_callback_replace_regex (struct t_trigger *trigger,
struct t_hashtable *pointers,
struct t_hashtable *extra_vars)
{
char *value, *replace_eval;
const char *ptr_key, *ptr_value;
int i;
if (trigger->regex_count == 0)
return;
for (i = 0; i < trigger->regex_count; i++)
{
/* if regex is not set (invalid), skip it */
if (!trigger->regex[i].regex)
continue;
ptr_key = (trigger->regex[i].variable) ?
trigger->regex[i].variable :
trigger_hook_regex_default_var[weechat_config_integer (trigger->options[TRIGGER_OPTION_HOOK])];
if (!ptr_key || !ptr_key[0])
{
if (trigger_buffer)
{
weechat_printf_tags (trigger_buffer, "no_trigger",
"\t regex %d: %s",
i + 1, _("no variable"));
}
continue;
}
ptr_value = weechat_hashtable_get (extra_vars, ptr_key);
if (!ptr_value)
{
if (trigger_buffer)
{
weechat_printf_tags (trigger_buffer, "no_trigger",
"\t regex %d (%s): %s",
i + 1, ptr_key, _("empty variable"));
}
continue;
}
replace_eval = weechat_string_eval_expression (
trigger->regex[i].replace_escaped,
pointers,
extra_vars,
NULL);
if (replace_eval)
{
value = weechat_string_replace_regex (ptr_value,
trigger->regex[i].regex,
replace_eval,
'$');
if (value)
{
/* display debug info on trigger buffer */
if (trigger_buffer)
{
weechat_printf_tags (trigger_buffer, "no_trigger",
"\t regex %d %s(%s%s%s)%s: "
"%s\"%s%s%s\"",
i + 1,
weechat_color ("chat_delimiters"),
weechat_color ("reset"),
ptr_key,
weechat_color ("chat_delimiters"),
weechat_color ("reset"),
weechat_color ("chat_delimiters"),
weechat_color ("reset"),
value,
weechat_color ("chat_delimiters"));
}
weechat_hashtable_set (extra_vars, ptr_key, value);
free (value);
}
free (replace_eval);
}
}
}
/*
* Executes the trigger command(s).
*/
void
trigger_callback_run_command (struct t_trigger *trigger,
struct t_gui_buffer *buffer,
struct t_hashtable *pointers,
struct t_hashtable *extra_vars)
{
char *command_eval;
int i;
if (!trigger->commands)
return;
if (!buffer)
{
buffer = weechat_buffer_search_main ();
if (!buffer)
return;
}
for (i = 0; trigger->commands[i]; i++)
{
command_eval = weechat_string_eval_expression (trigger->commands[i],
pointers, extra_vars,
NULL);
if (command_eval)
{
/* display debug info on trigger buffer */
if (trigger_buffer)
{
weechat_printf_tags (trigger_buffer, "no_trigger",
_("%s running command %s\"%s%s%s\"%s "
"on buffer %s%s%s"),
"\t",
weechat_color ("chat_delimiters"),
weechat_color ("reset"),
command_eval,
weechat_color ("chat_delimiters"),
weechat_color ("reset"),
weechat_color ("chat_buffer"),
weechat_buffer_get_string (buffer,
"full_name"),
weechat_color ("reset"));
}
weechat_command (buffer, command_eval);
trigger->hook_count_cmd++;
}
free (command_eval);
}
}
/*
* Executes a trigger.
*
* Following actions are executed:
* 1. display debug info on trigger buffer
* 2. check conditions (if false, exit)
* 3. replace text with regex
* 4. execute command(s)
*/
void
trigger_callback_execute (struct t_trigger *trigger,
struct t_gui_buffer *buffer,
struct t_hashtable *pointers,
struct t_hashtable *extra_vars)
{
/* display debug info on trigger buffer */
if (!trigger_buffer && (weechat_trigger_plugin->debug >= 1))
trigger_buffer_open (0);
trigger_buffer_display_trigger (trigger, buffer, pointers, extra_vars);
/* check conditions */
if (trigger_callback_check_conditions (trigger, pointers, extra_vars))
{
/* replace text with regex */
trigger_callback_replace_regex (trigger, pointers, extra_vars);
/* execute command(s) */
trigger_callback_run_command (trigger, buffer, pointers, extra_vars);
}
}
/*
* Callback for a signal hooked.
*/
int
trigger_callback_signal_cb (void *data, const char *signal,
const char *type_data, void *signal_data)
{
const char *ptr_signal_data;
char str_data[128], *irc_server;
const char *pos, *ptr_irc_message;
TRIGGER_CALLBACK_CB_INIT(WEECHAT_RC_OK);
/* split IRC message (if signal_data is an IRC message) */
irc_server = NULL;
ptr_irc_message = NULL;
if (strcmp (type_data, WEECHAT_HOOK_SIGNAL_STRING) == 0)
{
if (strstr (signal, ",irc_in_")
|| strstr (signal, ",irc_in2_")
|| strstr (signal, ",irc_raw_in_")
|| strstr (signal, ",irc_raw_in2_")
|| strstr (signal, ",irc_out1_")
|| strstr (signal, ",irc_out_"))
{
pos = strchr (signal, ',');
if (pos)
{
irc_server = weechat_strndup (signal, pos - signal);
ptr_irc_message = (const char *)signal_data;
}
}
else
{
pos = strstr (signal, ",irc_outtags_");
if (pos)
{
irc_server = weechat_strndup (signal, pos - signal);
pos = strchr ((const char *)signal_data, ';');
if (pos)
ptr_irc_message = pos + 1;
}
}
}
if (irc_server && ptr_irc_message)
{
extra_vars = trigger_callback_irc_message_parse (ptr_irc_message,
irc_server);
if (extra_vars)
weechat_hashtable_set (extra_vars, "server", irc_server);
}
if (irc_server)
free (irc_server);
/* create hashtable (if not already created) */
if (!extra_vars)
{
TRIGGER_CALLBACK_CB_NEW_EXTRA_VARS;
}
/* add data in hashtable used for conditions/replace/command */
weechat_hashtable_set (extra_vars, "tg_signal", signal);
if (strcmp (type_data, WEECHAT_HOOK_SIGNAL_STRING) == 0)
{
ptr_signal_data = (const char *)signal_data;
}
else if (strcmp (type_data, WEECHAT_HOOK_SIGNAL_INT) == 0)
{
snprintf (str_data, sizeof (str_data),
"%d", *((int *)signal_data));
ptr_signal_data = str_data;
}
else if (strcmp (type_data, WEECHAT_HOOK_SIGNAL_POINTER) == 0)
{
str_data[0] = '\0';
if (signal_data)
{
snprintf (str_data, sizeof (str_data),
"0x%lx", (long unsigned int)signal_data);
}
ptr_signal_data = str_data;
}
weechat_hashtable_set (extra_vars, "tg_signal_data", ptr_signal_data);
/* execute the trigger (conditions, regex, command) */
trigger_callback_execute (trigger, NULL, pointers, extra_vars);
end:
TRIGGER_CALLBACK_CB_END(trigger_rc);
}
/*
* Callback for a hsignal hooked.
*/
int
trigger_callback_hsignal_cb (void *data, const char *signal,
struct t_hashtable *hashtable)
{
const char *type_values;
TRIGGER_CALLBACK_CB_INIT(WEECHAT_RC_OK);
/* duplicate hashtable */
if (hashtable
&& (strcmp (weechat_hashtable_get_string (hashtable, "type_keys"), "string") == 0))
{
type_values = weechat_hashtable_get_string (hashtable, "type_values");
if (strcmp (type_values, "pointer") == 0)
{
pointers = weechat_hashtable_dup (hashtable);
if (!pointers)
goto end;
}
else if (strcmp (type_values, "pointer") == 0)
{
extra_vars = weechat_hashtable_dup (hashtable);
if (!extra_vars)
goto end;
}
}
/* create hashtable (if not already created) */
if (!extra_vars)
{
TRIGGER_CALLBACK_CB_NEW_EXTRA_VARS;
}
/* add data in hashtable used for conditions/replace/command */
weechat_hashtable_set (extra_vars, "tg_signal", signal);
/* execute the trigger (conditions, regex, command) */
trigger_callback_execute (trigger, NULL, pointers, extra_vars);
end:
TRIGGER_CALLBACK_CB_END(trigger_rc);
}
/*
* Callback for a modifier hooked.
*/
char *
trigger_callback_modifier_cb (void *data, const char *modifier,
const char *modifier_data, const char *string)
{
struct t_gui_buffer *buffer;
const char *ptr_string;
char *string_modified, *pos, *pos2, *plugin_name, *buffer_name;
char *buffer_full_name, *str_tags, **tags, *prefix, *string_no_color;
int length, num_tags;
TRIGGER_CALLBACK_CB_INIT(NULL);
buffer = NULL;
tags = NULL;
num_tags = 0;
string_no_color = NULL;
/* split IRC message (if string is an IRC message) */
if ((strncmp (modifier, "irc_in_", 7) == 0)
|| (strncmp (modifier, "irc_in2_", 8) == 0)
|| (strncmp (modifier, "irc_out1_", 9) == 0)
|| (strncmp (modifier, "irc_out_", 8) == 0))
{
extra_vars = trigger_callback_irc_message_parse (string,
modifier_data);
if (extra_vars)
weechat_hashtable_set (extra_vars, "server", modifier_data);
}
TRIGGER_CALLBACK_CB_NEW_POINTERS;
if (!extra_vars)
{
TRIGGER_CALLBACK_CB_NEW_EXTRA_VARS;
}
/* add data in hashtable used for conditions/replace/command */
weechat_hashtable_set (extra_vars, "tg_modifier", modifier);
weechat_hashtable_set (extra_vars, "tg_modifier_data", modifier_data);
weechat_hashtable_set (extra_vars, "tg_string", string);
string_no_color = weechat_string_remove_color (string, NULL);
if (string_no_color)
{
weechat_hashtable_set (extra_vars,
"tg_string_nocolor", string_no_color);
}
/* add special variables for a WeeChat message */
if (strcmp (modifier, "weechat_print") == 0)
{
/* set "tg_prefix" and "tg_message" */
pos = strchr (string, '\t');
if (pos)
{
if (pos > string)
{
prefix = weechat_strndup (string, pos - string);
if (prefix)
{
weechat_hashtable_set (extra_vars, "tg_prefix", prefix);
free (prefix);
}
}
pos++;
if (pos[0] == '\t')
pos++;
weechat_hashtable_set (extra_vars, "tg_message", pos);
}
else
weechat_hashtable_set (extra_vars, "tg_message", string);
/* set "tg_prefix_nocolor" and "tg_message_nocolor" */
if (string_no_color)
{
pos = strchr (string_no_color, '\t');
if (pos)
{
if (pos > string_no_color)
{
prefix = weechat_strndup (string_no_color,
pos - string_no_color);
if (prefix)
{
weechat_hashtable_set (extra_vars,
"tg_prefix_nocolor", prefix);
free (prefix);
}
}
pos++;
if (pos[0] == '\t')
pos++;
weechat_hashtable_set (extra_vars, "tg_message_nocolor", pos);
}
else
{
weechat_hashtable_set (extra_vars,
"tg_message_nocolor", string_no_color);
}
}
/*
* extract buffer/tags from modifier data
* (format: "plugin;buffer_name;tags")
*/
pos = strchr (modifier_data, ';');
if (pos)
{
plugin_name = weechat_strndup (modifier_data, pos - modifier_data);
if (plugin_name)
{
weechat_hashtable_set (extra_vars, "tg_plugin", plugin_name);
pos++;
pos2 = strchr (pos, ';');
if (pos2)
{
buffer_name = weechat_strndup (pos, pos2 - pos);
if (buffer_name)
{
buffer = weechat_buffer_search (plugin_name,
buffer_name);
length = strlen (plugin_name) + 1 + strlen (buffer_name) + 1;
buffer_full_name = malloc (length);
if (buffer_full_name)
{
snprintf (buffer_full_name, length,
"%s.%s", plugin_name, buffer_name);
weechat_hashtable_set (extra_vars, "tg_buffer",
buffer_full_name);
free (buffer_full_name);
}
free (buffer_name);
}
pos2++;
if (pos2[0])
{
tags = weechat_string_split (pos2, ",", 0, 0, &num_tags);
length = 1 + strlen (pos2) + 1 + 1;
str_tags = malloc (length);
if (str_tags)
{
snprintf (str_tags, length, ",%s,", pos2);
weechat_hashtable_set (extra_vars, "tg_tags",
str_tags);
free (str_tags);
}
}
}
free (plugin_name);
}
}
weechat_hashtable_set (pointers, "buffer", buffer);
}
if (tags)
{
if (!trigger_callback_set_tags (buffer, (const char **)tags, num_tags,
extra_vars))
{
goto end;
}
}
/* execute the trigger (conditions, regex, command) */
trigger_callback_execute (trigger, buffer, pointers, extra_vars);
end:
ptr_string = weechat_hashtable_get (extra_vars, "tg_string");
string_modified = (ptr_string && (strcmp (ptr_string, string) != 0)) ?
strdup (ptr_string) : NULL;
if (tags)
weechat_string_free_split (tags);
if (string_no_color)
free (string_no_color);
TRIGGER_CALLBACK_CB_END(string_modified);
}
/*
* Callback for a print hooked.
*/
int
trigger_callback_print_cb (void *data, struct t_gui_buffer *buffer,
time_t date, int tags_count, const char **tags,
int displayed, int highlight, const char *prefix,
const char *message)
{
char *str_tags, *str_tags2, str_temp[128], *str_no_color;
int length;
struct tm *date_tmp;
TRIGGER_CALLBACK_CB_INIT(WEECHAT_RC_OK);
/* do nothing if the buffer does not match buffers defined in the trigger */
if (trigger->hook_print_buffers
&& !weechat_buffer_match_list (buffer, trigger->hook_print_buffers))
goto end;
TRIGGER_CALLBACK_CB_NEW_POINTERS;
TRIGGER_CALLBACK_CB_NEW_EXTRA_VARS;
/* add data in hashtables used for conditions/replace/command */
weechat_hashtable_set (pointers, "buffer", buffer);
date_tmp = localtime (&date);
if (date_tmp)
{
strftime (str_temp, sizeof (str_temp), "%Y-%m-%d %H:%M:%S", date_tmp);
weechat_hashtable_set (extra_vars, "tg_date", str_temp);
}
snprintf (str_temp, sizeof (str_temp), "%d", displayed);
weechat_hashtable_set (extra_vars, "tg_displayed", str_temp);
snprintf (str_temp, sizeof (str_temp), "%d", highlight);
weechat_hashtable_set (extra_vars, "tg_highlight", str_temp);
weechat_hashtable_set (extra_vars, "tg_prefix", prefix);
str_no_color = weechat_string_remove_color (prefix, NULL);
if (str_no_color)
{
weechat_hashtable_set (extra_vars, "tg_prefix_nocolor", str_no_color);
free (str_no_color);
}
weechat_hashtable_set (extra_vars, "tg_message", message);
str_no_color = weechat_string_remove_color (message, NULL);
if (str_no_color)
{
weechat_hashtable_set (extra_vars, "tg_message_nocolor", str_no_color);
free (str_no_color);
}
str_tags = weechat_string_build_with_split_string (tags, ",");
if (str_tags)
{
/* build string with tags and commas around: ",tag1,tag2,tag3," */
length = 1 + strlen (str_tags) + 1 + 1;
str_tags2 = malloc (length);
if (str_tags2)
{
snprintf (str_tags2, length, ",%s,", str_tags);
weechat_hashtable_set (extra_vars, "tg_tags", str_tags2);
free (str_tags2);
}
free (str_tags);
}
if (!trigger_callback_set_tags (buffer, tags, tags_count, extra_vars))
goto end;
/* execute the trigger (conditions, regex, command) */
trigger_callback_execute (trigger, buffer, pointers, extra_vars);
end:
TRIGGER_CALLBACK_CB_END(trigger_rc);
}
/*
* Callback for a command hooked.
*/
int
trigger_callback_command_cb (void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol)
{
char str_name[32];
int i;
TRIGGER_CALLBACK_CB_INIT(WEECHAT_RC_OK);
TRIGGER_CALLBACK_CB_NEW_POINTERS;
TRIGGER_CALLBACK_CB_NEW_EXTRA_VARS;
/* add data in hashtables used for conditions/replace/command */
weechat_hashtable_set (pointers, "buffer", buffer);
for (i = 0; i < argc; i++)
{
snprintf (str_name, sizeof (str_name), "tg_argv%d", i);
weechat_hashtable_set (extra_vars, str_name, argv[i]);
snprintf (str_name, sizeof (str_name), "tg_argv_eol%d", i);
weechat_hashtable_set (extra_vars, str_name, argv_eol[i]);
}
/* execute the trigger (conditions, regex, command) */
trigger_callback_execute (trigger, buffer, pointers, extra_vars);
end:
TRIGGER_CALLBACK_CB_END(trigger_rc);
}
/*
* Callback for a command_run hooked.
*/
int
trigger_callback_command_run_cb (void *data, struct t_gui_buffer *buffer,
const char *command)
{
TRIGGER_CALLBACK_CB_INIT(WEECHAT_RC_OK);
TRIGGER_CALLBACK_CB_NEW_POINTERS;
TRIGGER_CALLBACK_CB_NEW_EXTRA_VARS;
/* add data in hashtables used for conditions/replace/command */
weechat_hashtable_set (pointers, "buffer", buffer);
weechat_hashtable_set (extra_vars, "tg_command", command);
/* execute the trigger (conditions, regex, command) */
trigger_callback_execute (trigger, buffer, pointers, extra_vars);
end:
TRIGGER_CALLBACK_CB_END(trigger_rc);
}
/*
* Callback for a timer hooked.
*/
int
trigger_callback_timer_cb (void *data, int remaining_calls)
{
char str_temp[128];
int i;
time_t date;
struct tm *date_tmp;
TRIGGER_CALLBACK_CB_INIT(WEECHAT_RC_OK);
/*
* remove the hook if this is the last call to timer
* (because WeeChat will remove the hook after this call, so the pointer
* will become invalid)
*/
if ((remaining_calls == 0) && trigger->hooks)
{
for (i = 0; i < trigger->hooks_count; i++)
{
trigger->hooks[i] = NULL;
}
}
TRIGGER_CALLBACK_CB_NEW_EXTRA_VARS;
/* add data in hashtable used for conditions/replace/command */
snprintf (str_temp, sizeof (str_temp), "%d", remaining_calls);
weechat_hashtable_set (extra_vars, "tg_remaining_calls", str_temp);
date = time (NULL);
date_tmp = localtime (&date);
if (date_tmp)
{
strftime (str_temp, sizeof (str_temp), "%Y-%m-%d %H:%M:%S", date_tmp);
weechat_hashtable_set (extra_vars, "tg_date", str_temp);
}
/* execute the trigger (conditions, regex, command) */
trigger_callback_execute (trigger, NULL, pointers, extra_vars);
end:
TRIGGER_CALLBACK_CB_END(trigger_rc);
}
/*
* Callback for a config hooked.
*/
int
trigger_callback_config_cb (void *data, const char *option, const char *value)
{
TRIGGER_CALLBACK_CB_INIT(WEECHAT_RC_OK);
TRIGGER_CALLBACK_CB_NEW_EXTRA_VARS;
/* add data in hashtable used for conditions/replace/command */
weechat_hashtable_set (extra_vars, "tg_option", option);
weechat_hashtable_set (extra_vars, "tg_value", value);
/* execute the trigger (conditions, regex, command) */
trigger_callback_execute (trigger, NULL, pointers, extra_vars);
end:
TRIGGER_CALLBACK_CB_END(trigger_rc);
}
/*
* Callback for a focus hooked.
*/
struct t_hashtable *
trigger_callback_focus_cb (void *data, struct t_hashtable *info)
{
const char *ptr_value;
long unsigned int value;
int rc;
TRIGGER_CALLBACK_CB_INIT(info);
TRIGGER_CALLBACK_CB_NEW_POINTERS;
/* add data in hashtables used for conditions/replace/command */
ptr_value = weechat_hashtable_get (info, "_window");
if (ptr_value && ptr_value[0] && (strncmp (ptr_value, "0x", 2) == 0))
{
rc = sscanf (ptr_value + 2, "%lx", &value);
if ((rc != EOF) && (rc >= 1))
weechat_hashtable_set (pointers, "window", (void *)value);
}
ptr_value = weechat_hashtable_get (info, "_buffer");
if (ptr_value && ptr_value[0] && (strncmp (ptr_value, "0x", 2) == 0))
{
rc = sscanf (ptr_value + 2, "%lx", &value);
if ((rc != EOF) && (rc >= 1))
weechat_hashtable_set (pointers, "buffer", (void *)value);
}
/* execute the trigger (conditions, regex, command) */
trigger_callback_execute (trigger, NULL, pointers, info);
end:
TRIGGER_CALLBACK_CB_END(info);
}
/*
* Initializes trigger callback.
*/
void
trigger_callback_init ()
{
trigger_callback_hashtable_options = weechat_hashtable_new (32,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING,
NULL,
NULL);
if (trigger_callback_hashtable_options)
{
weechat_hashtable_set (trigger_callback_hashtable_options,
"type", "condition");
}
}
/*
* Ends trigger callback.
*/
void
trigger_callback_end ()
{
if (trigger_callback_hashtable_options)
weechat_hashtable_free (trigger_callback_hashtable_options);
}
+94
View File
@@ -0,0 +1,94 @@
/*
* Copyright (C) 2014 Sébastien Helleu <flashcode@flashtux.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef __WEECHAT_TRIGGER_CALLBACK_H
#define __WEECHAT_TRIGGER_CALLBACK_H 1
#define TRIGGER_CALLBACK_CB_INIT(__rc) \
struct t_trigger *trigger; \
struct t_hashtable *pointers, *extra_vars; \
int trigger_rc; \
pointers = NULL; \
extra_vars = NULL; \
(void) trigger_rc; \
if (!trigger_enabled) \
return __rc; \
trigger = (struct t_trigger *)data; \
if (!trigger || trigger->hook_running) \
return __rc; \
trigger->hook_count_cb++; \
trigger->hook_running = 1; \
trigger_rc = trigger_return_code[ \
weechat_config_integer ( \
trigger->options[TRIGGER_OPTION_RETURN_CODE])];
#define TRIGGER_CALLBACK_CB_NEW_POINTERS \
pointers = weechat_hashtable_new ( \
32, \
WEECHAT_HASHTABLE_STRING, \
WEECHAT_HASHTABLE_POINTER, \
NULL, \
NULL); \
if (!pointers) \
goto end;
#define TRIGGER_CALLBACK_CB_NEW_EXTRA_VARS \
extra_vars = weechat_hashtable_new ( \
32, \
WEECHAT_HASHTABLE_STRING, \
WEECHAT_HASHTABLE_STRING, \
NULL, \
NULL); \
if (!extra_vars) \
goto end;
#define TRIGGER_CALLBACK_CB_END(__rc) \
if (pointers) \
weechat_hashtable_free (pointers); \
if (extra_vars) \
weechat_hashtable_free (extra_vars); \
trigger->hook_running = 0; \
return __rc;
extern int trigger_callback_signal_cb (void *data, const char *signal,
const char *type_data, void *signal_data);
extern int trigger_callback_hsignal_cb (void *data, const char *signal,
struct t_hashtable *hashtable);
extern char *trigger_callback_modifier_cb (void *data, const char *modifier,
const char *modifier_data,
const char *string);
extern int trigger_callback_print_cb (void *data, struct t_gui_buffer *buffer,
time_t date, int tags_count,
const char **tags, int displayed,
int highlight, const char *prefix,
const char *message);
extern int trigger_callback_command_cb (void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol);
extern int trigger_callback_command_run_cb (void *data,
struct t_gui_buffer *buffer,
const char *command);
extern int trigger_callback_timer_cb (void *data, int remaining_calls);
extern int trigger_callback_config_cb (void *data, const char *option,
const char *value);
extern struct t_hashtable *trigger_callback_focus_cb (void *data,
struct t_hashtable *info);
extern void trigger_callback_init ();
extern void trigger_callback_end ();
#endif /* __WEECHAT_TRIGGER_CALLBACK_H */
+990
View File
@@ -0,0 +1,990 @@
/*
* trigger-command.c - trigger command
*
* Copyright (C) 2014 Sébastien Helleu <flashcode@flashtux.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "../weechat-plugin.h"
#include "trigger.h"
#include "trigger-buffer.h"
#include "trigger-config.h"
/*
* Displays the status of triggers (global status).
*/
void
trigger_command_display_status ()
{
weechat_printf_tags (NULL, "no_trigger",
(trigger_enabled) ?
_("Triggers enabled") : _("Triggers disabled"));
}
/*
* Displays one trigger (internal function, must not be called directly).
*/
void
trigger_command_display_trigger_internal (const char *name,
int enabled,
const char *hook,
const char *arguments,
const char *conditions,
int hooks_count,
int hook_count_cb,
int hook_count_cmd,
int regex_count,
struct t_trigger_regex *regex,
int commands_count,
char **commands,
int return_code,
int verbose)
{
char str_conditions[64], str_regex[64], str_command[64], str_rc[64];
char spaces[256];
int i, length;
if (verbose >= 1)
{
weechat_printf_tags (
NULL, "no_trigger",
" %s%s%s: %s%s%s%s%s%s%s",
(enabled) ?
weechat_color (weechat_config_string (trigger_config_color_trigger)) :
weechat_color (weechat_config_string (trigger_config_color_trigger_disabled)),
name,
weechat_color ("reset"),
hook,
weechat_color ("chat_delimiters"),
(arguments && arguments[0]) ? "(" : "",
weechat_color ("reset"),
arguments,
weechat_color ("chat_delimiters"),
(arguments && arguments[0]) ? ")" : "");
length = weechat_strlen_screen (name) + 3;
if (length >= (int)sizeof (spaces))
length = sizeof (spaces) - 1;
memset (spaces, ' ', length);
spaces[length] = '\0';
if (verbose >= 2)
{
weechat_printf_tags (NULL, "no_trigger",
"%s hooks: %d", spaces, hooks_count);
weechat_printf_tags (NULL, "no_trigger",
"%s callback: %d",
spaces, hook_count_cb);
weechat_printf_tags (NULL, "no_trigger",
"%s commands: %d",
spaces, hook_count_cmd);
}
if (conditions && conditions[0])
{
weechat_printf_tags (NULL, "no_trigger",
"%s %s=? %s\"%s%s%s\"",
spaces,
weechat_color (weechat_config_string (trigger_config_color_flag_conditions)),
weechat_color ("chat_delimiters"),
weechat_color ("reset"),
conditions,
weechat_color ("chat_delimiters"));
}
for (i = 0; i < regex_count; i++)
{
weechat_printf_tags (NULL, "no_trigger",
"%s %s~%d %s\"%s%s%s\" --> "
"\"%s%s%s\"%s%s%s%s",
spaces,
weechat_color (weechat_config_string (trigger_config_color_flag_regex)),
i + 1,
weechat_color ("chat_delimiters"),
weechat_color (weechat_config_string (trigger_config_color_regex)),
regex[i].str_regex,
weechat_color ("chat_delimiters"),
weechat_color (weechat_config_string (trigger_config_color_replace)),
regex[i].replace,
weechat_color ("chat_delimiters"),
weechat_color ("reset"),
(regex[i].variable) ? " (" : "",
(regex[i].variable) ? regex[i].variable : "",
(regex[i].variable) ? ")" : "");
}
if (commands)
{
for (i = 0; commands[i]; i++)
{
weechat_printf_tags (NULL, "no_trigger",
"%s %s/%d %s\"%s%s%s\"",
spaces,
weechat_color (weechat_config_string (trigger_config_color_flag_command)),
i + 1,
weechat_color ("chat_delimiters"),
weechat_color ("reset"),
commands[i],
weechat_color ("chat_delimiters"));
}
}
if ((return_code >= 0) && (return_code != TRIGGER_RC_OK))
{
weechat_printf_tags (NULL, "no_trigger",
"%s %s=> %s%s",
spaces,
weechat_color (weechat_config_string (trigger_config_color_flag_return_code)),
weechat_color ("reset"),
trigger_return_code_string[return_code]);
}
}
else
{
str_conditions[0] ='\0';
str_regex[0] = '\0';
str_command[0] = '\0';
str_rc[0] = '\0';
if (conditions && conditions[0])
{
snprintf (str_conditions, sizeof (str_conditions),
" %s=?%s",
weechat_color (weechat_config_string (trigger_config_color_flag_conditions)),
weechat_color ("reset"));
}
if (regex_count > 0)
{
snprintf (str_regex, sizeof (str_regex),
" %s~%d%s",
weechat_color (weechat_config_string (trigger_config_color_flag_regex)),
regex_count,
weechat_color ("reset"));
}
if (commands_count > 0)
{
snprintf (str_command, sizeof (str_command),
" %s/%d%s",
weechat_color (weechat_config_string (trigger_config_color_flag_command)),
commands_count,
weechat_color ("reset"));
}
if ((return_code >= 0) && (return_code != TRIGGER_RC_OK))
{
snprintf (str_rc, sizeof (str_rc),
" %s=>%s",
weechat_color (weechat_config_string (trigger_config_color_flag_return_code)),
weechat_color ("reset"));
}
weechat_printf_tags (
NULL, "no_trigger",
" %s%s%s: %s%s%s%s%s%s%s%s%s%s%s%s",
(enabled) ?
weechat_color (weechat_config_string (trigger_config_color_trigger)) :
weechat_color (weechat_config_string (trigger_config_color_trigger_disabled)),
name,
weechat_color ("reset"),
hook,
weechat_color ("chat_delimiters"),
(arguments && arguments[0]) ? "(" : "",
weechat_color ("reset"),
arguments,
weechat_color ("chat_delimiters"),
(arguments && arguments[0]) ? ")" : "",
weechat_color ("reset"),
str_conditions,
str_regex,
str_command,
str_rc);
}
}
/*
* Displays one trigger.
*/
void
trigger_command_display_trigger (struct t_trigger *trigger, int verbose)
{
trigger_command_display_trigger_internal (
trigger->name,
weechat_config_boolean (trigger->options[TRIGGER_OPTION_ENABLED]),
weechat_config_string (trigger->options[TRIGGER_OPTION_HOOK]),
weechat_config_string (trigger->options[TRIGGER_OPTION_ARGUMENTS]),
weechat_config_string (trigger->options[TRIGGER_OPTION_CONDITIONS]),
trigger->hooks_count,
trigger->hook_count_cb,
trigger->hook_count_cmd,
trigger->regex_count,
trigger->regex,
trigger->commands_count,
trigger->commands,
weechat_config_integer (trigger->options[TRIGGER_OPTION_RETURN_CODE]),
verbose);
}
/*
* Displays a list of triggers.
*/
void
trigger_command_list (const char *message, int verbose)
{
struct t_trigger *ptr_trigger;
weechat_printf_tags (NULL, "no_trigger", "");
trigger_command_display_status ();
if (!triggers)
{
weechat_printf_tags (NULL, "no_trigger", _("No trigger defined"));
return;
}
weechat_printf_tags (NULL, "no_trigger", message);
for (ptr_trigger = triggers; ptr_trigger;
ptr_trigger = ptr_trigger->next_trigger)
{
trigger_command_display_trigger (ptr_trigger, verbose);
}
}
/*
* Displays a list of default triggers.
*/
void
trigger_command_list_default (int verbose)
{
int i, regex_count, commands_count;
struct t_trigger_regex *regex;
char **commands;
regex_count = 0;
regex = NULL;
commands_count = 0;
commands = NULL;
weechat_printf_tags (NULL, "no_trigger", "");
weechat_printf_tags (NULL, "no_trigger", _("List of default triggers:"));
for (i = 0; trigger_config_default_list[i][0]; i++)
{
trigger_split_regex (trigger_config_default_list[i][0],
trigger_config_default_list[i][5],
&regex_count,
&regex);
trigger_split_command (trigger_config_default_list[i][6],
&commands_count,
&commands);
trigger_command_display_trigger_internal (
trigger_config_default_list[i][0],
weechat_config_string_to_boolean (trigger_config_default_list[i][1]),
trigger_config_default_list[i][2],
trigger_config_default_list[i][3],
trigger_config_default_list[i][4],
0,
0,
0,
regex_count,
regex,
commands_count,
commands,
trigger_search_return_code (trigger_config_default_list[i][7]),
verbose);
}
trigger_free_regex (&regex_count, &regex);
if (commands)
weechat_string_free_split (commands);
}
/*
* Set "enabled" value in a trigger.
*
* Argument "enable" can be:
* -1: toggle trigger
* 0: disable trigger
* 1: enable trigger
* 2: restart trigger (recreate hook(s))
*/
void
trigger_command_set_enabled (struct t_trigger *trigger, int enable,
int display_error)
{
if (enable == 2)
{
if (weechat_config_boolean (trigger->options[TRIGGER_OPTION_ENABLED]))
{
trigger_unhook (trigger);
trigger_hook (trigger);
weechat_printf_tags (NULL, "no_trigger",
_("Trigger \"%s\" restarted"),
trigger->name);
}
else if (display_error)
{
weechat_printf_tags (NULL, "no_trigger",
_("%sError: a disabled trigger can not be "
"restarted"),
weechat_prefix ("error"));
}
}
else
{
if (enable < 0)
{
enable = weechat_config_boolean (trigger->options[TRIGGER_OPTION_ENABLED]) ?
0 : 1;
}
weechat_config_option_set (trigger->options[TRIGGER_OPTION_ENABLED],
(enable) ? "on" : "off", 1);
weechat_printf_tags (NULL, "no_trigger",
(enable) ?
_("Trigger \"%s\" enabled") :
_("Trigger \"%s\" disabled"),
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.
*/
int
trigger_command_trigger (void *data, struct t_gui_buffer *buffer, int argc,
char **argv, char **argv_eol)
{
struct t_trigger *ptr_trigger, *ptr_trigger2;
char *value, **sargv, **items, input[1024], str_pos[16];
int rc, i, type, count, index_option, enable, sargc, num_items, add_rc;
/* make C compiler happy */
(void) data;
rc = WEECHAT_RC_OK;
sargv = NULL;
/* list all triggers */
if ((argc == 1)
|| ((argc == 2) && (weechat_strcasecmp (argv[1], "list") == 0)))
{
trigger_command_list (_("List of triggers:"), 0);
goto end;
}
/* full list of all triggers */
if ((argc == 2) && (weechat_strcasecmp (argv[1], "listfull") == 0))
{
trigger_command_list (_("List of triggers:"), 1);
goto end;
}
/* list of default triggers */
if ((argc == 2) && (weechat_strcasecmp (argv[1], "listdefault") == 0))
{
trigger_command_list_default (1);
goto end;
}
/* add a trigger */
if ((weechat_strcasecmp (argv[1], "add") == 0)
|| (weechat_strcasecmp (argv[1], "addoff") == 0)
|| (weechat_strcasecmp (argv[1], "addreplace") == 0))
{
sargv = weechat_string_split_shell (argv_eol[2], &sargc);
if (!sargv || (sargc < 2))
goto error;
if (!trigger_name_valid (sargv[0]))
{
weechat_printf_tags (NULL, "no_trigger",
_("%sError: invalid name for trigger"),
weechat_prefix ("error"));
goto end;
}
type = trigger_search_hook_type (sargv[1]);
if (type < 0)
{
weechat_printf_tags (NULL, "no_trigger",
_("%sError: invalid hook type \"%s\""),
weechat_prefix ("error"), sargv[1]);
goto end;
}
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_search (sargv[0]);
if (ptr_trigger)
{
if (weechat_strcasecmp (argv[1], "addreplace") == 0)
{
if (ptr_trigger)
trigger_free (ptr_trigger);
}
else
{
weechat_printf_tags (NULL, "no_trigger",
_("%sError: trigger \"%s\" already exists "
"(choose another name or use option "
"\"addreplace\" to overwrite it)"),
weechat_prefix ("error"), sargv[0]);
goto end;
}
}
ptr_trigger = trigger_alloc (sargv[0]);
if (!ptr_trigger)
{
weechat_printf_tags (NULL, "no_trigger",
_("%sError: failed to create trigger \"%s\""),
weechat_prefix ("error"), sargv[0]);
goto end;
}
ptr_trigger = trigger_new (
sargv[0], /* name */
(weechat_strcasecmp (argv[1], "addoff") == 0) ? "off" : "on",
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"), sargv[0]);
}
else
{
weechat_printf_tags (NULL, "no_trigger",
_("%sError: failed to create trigger \"%s\""),
weechat_prefix ("error"), sargv[0]);
}
goto end;
}
/* add trigger command in input (to help trigger creation) */
if (weechat_strcasecmp (argv[1], "addinput") == 0)
{
type = TRIGGER_HOOK_SIGNAL;
if (argc >= 3)
{
type = trigger_search_hook_type (argv[2]);
if (type < 0)
{
weechat_printf_tags (NULL, "no_trigger",
_("%sError: invalid hook type \"%s\""),
weechat_prefix ("error"), argv[2]);
goto end;
}
}
items = weechat_string_split (trigger_hook_default_rc[type], ",", 0, 0,
&num_items);
snprintf (input, sizeof (input),
"/trigger add name %s \"%s\" \"%s\" \"%s\" \"%s\"%s%s%s",
trigger_hook_type_string[type],
trigger_hook_default_arguments[type],
TRIGGER_HOOK_DEFAULT_CONDITIONS,
TRIGGER_HOOK_DEFAULT_REGEX,
TRIGGER_HOOK_DEFAULT_COMMAND,
(items && (num_items > 0)) ? " \"" : "",
(items && (num_items > 0)) ? items[0] : "",
(items && (num_items > 0)) ? "\"" : "");
weechat_buffer_set (buffer, "input", input);
weechat_buffer_set (buffer, "input_pos", "13");
goto end;
}
/*
* get command to create a trigger, and according to option:
* - input: put the command in input
* - output: send the command to the buffer
* - recreate: same as input, but the trigger is first deleted
*/
if ((weechat_strcasecmp (argv[1], "input") == 0)
|| (weechat_strcasecmp (argv[1], "output") == 0)
|| (weechat_strcasecmp (argv[1], "recreate") == 0))
{
if (argc < 3)
goto error;
ptr_trigger = trigger_search (argv[2]);
if (!ptr_trigger)
{
weechat_printf_tags (NULL, "no_trigger",
_("%sError: trigger \"%s\" not found"),
weechat_prefix ("error"), argv[2]);
goto end;
}
add_rc = trigger_hook_default_rc[weechat_config_integer (ptr_trigger->options[TRIGGER_OPTION_HOOK])][0];
snprintf (input, sizeof (input),
"//trigger %s %s %s \"%s\" \"%s\" \"%s\" \"%s\"%s%s%s",
(weechat_strcasecmp (argv[1], "recreate") == 0) ? "addreplace" : "add",
ptr_trigger->name,
weechat_config_string (ptr_trigger->options[TRIGGER_OPTION_HOOK]),
weechat_config_string (ptr_trigger->options[TRIGGER_OPTION_ARGUMENTS]),
weechat_config_string (ptr_trigger->options[TRIGGER_OPTION_CONDITIONS]),
weechat_config_string (ptr_trigger->options[TRIGGER_OPTION_REGEX]),
weechat_config_string (ptr_trigger->options[TRIGGER_OPTION_COMMAND]),
(add_rc) ? " \"" : "",
(add_rc) ? weechat_config_string (ptr_trigger->options[TRIGGER_OPTION_RETURN_CODE]) : "",
(add_rc) ? "\"" : "");
if (weechat_strcasecmp (argv[1], "output") == 0)
{
weechat_command (buffer, input);
}
else
{
weechat_buffer_set (buffer, "input", input + 1);
snprintf (str_pos, sizeof (str_pos),
"%d", weechat_utf8_strlen (input + 1));
weechat_buffer_set (buffer, "input_pos", str_pos);
}
goto end;
}
/* set option in a trigger */
if (weechat_strcasecmp (argv[1], "set") == 0)
{
if (argc < 5)
goto error;
ptr_trigger = trigger_search (argv[2]);
if (!ptr_trigger)
{
weechat_printf_tags (NULL, "no_trigger",
_("%sError: trigger \"%s\" not found"),
weechat_prefix ("error"), argv[2]);
goto end;
}
if (weechat_strcasecmp (argv[3], "name") == 0)
{
trigger_command_rename (ptr_trigger, argv[4]);
goto end;
}
value = weechat_string_remove_quotes (argv_eol[4], "'\"");
if (value)
{
index_option = trigger_search_option (argv[3]);
if (index_option >= 0)
{
weechat_config_option_set (ptr_trigger->options[index_option],
value, 1);
weechat_printf_tags (NULL, "no_trigger",
_("Trigger \"%s\" updated"),
ptr_trigger->name);
}
else
{
weechat_printf_tags (NULL, "no_trigger",
_("%sError: trigger option \"%s\" not "
"found"),
weechat_prefix ("error"), argv[3]);
}
free (value);
}
goto end;
}
/* rename a trigger */
if (weechat_strcasecmp (argv[1], "rename") == 0)
{
if (argc < 4)
goto error;
ptr_trigger = trigger_search (argv[2]);
if (!ptr_trigger)
{
weechat_printf_tags (NULL, "no_trigger",
_("%sError: trigger \"%s\" not found"),
weechat_prefix ("error"), argv[2]);
goto end;
}
trigger_command_rename (ptr_trigger, argv[3]);
goto end;
}
/* copy a trigger */
if (weechat_strcasecmp (argv[1], "copy") == 0)
{
if (argc < 4)
goto error;
ptr_trigger = trigger_search (argv[2]);
if (!ptr_trigger)
{
weechat_printf_tags (NULL, "no_trigger",
_("%sError: trigger \"%s\" not found"),
weechat_prefix ("error"), argv[2]);
goto end;
}
/* check that new name is valid */
if (!trigger_name_valid (argv[3]))
{
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 (argv[3]))
{
weechat_printf_tags (NULL, "no_trigger",
_("%sError: trigger \"%s\" already "
"exists"),
weechat_prefix ("error"), argv[3]);
goto end;
}
/* copy the trigger */
ptr_trigger2 = trigger_copy (ptr_trigger, argv[3]);
if (ptr_trigger2)
{
weechat_printf_tags (NULL, "no_trigger",
_("Trigger \"%s\" copied to \"%s\""),
ptr_trigger->name, ptr_trigger2->name);
}
else
{
weechat_printf_tags (NULL, "no_trigger",
_("%sError: failed to copy trigger "
"\"%s\""),
weechat_prefix ("error"), ptr_trigger->name);
}
goto end;
}
/* enable/disable/toggle/restart trigger(s) */
if ((weechat_strcasecmp (argv[1], "enable") == 0)
|| (weechat_strcasecmp (argv[1], "disable") == 0)
|| (weechat_strcasecmp (argv[1], "toggle") == 0)
|| (weechat_strcasecmp (argv[1], "restart") == 0))
{
if (argc < 3)
{
if (weechat_strcasecmp (argv[1], "restart") == 0)
goto error;
if (weechat_strcasecmp (argv[1], "enable") == 0)
weechat_config_option_set (trigger_config_look_enabled, "1", 1);
else if (weechat_strcasecmp (argv[1], "disable") == 0)
weechat_config_option_set (trigger_config_look_enabled, "0", 1);
else if (weechat_strcasecmp (argv[1], "toggle") == 0)
{
weechat_config_option_set (trigger_config_look_enabled,
(trigger_enabled) ? "0" : "1",
1);
}
trigger_command_display_status ();
goto end;
}
enable = -1;
if (weechat_strcasecmp (argv[1], "enable") == 0)
enable = 1;
else if (weechat_strcasecmp (argv[1], "disable") == 0)
enable = 0;
else if (weechat_strcasecmp (argv[1], "restart") == 0)
enable = 2;
if (weechat_strcasecmp (argv[2], "-all") == 0)
{
for (ptr_trigger = triggers; ptr_trigger;
ptr_trigger = ptr_trigger->next_trigger)
{
trigger_command_set_enabled (ptr_trigger, enable, 0);
}
}
else
{
for (i = 2; i < argc; i++)
{
ptr_trigger = trigger_search (argv[i]);
if (ptr_trigger)
trigger_command_set_enabled (ptr_trigger, enable, 1);
else
{
weechat_printf_tags (NULL, "no_trigger",
_("%sTrigger \"%s\" not found"),
weechat_prefix ("error"), argv[i]);
}
}
}
goto end;
}
/* delete trigger(s) */
if (weechat_strcasecmp (argv[1], "del") == 0)
{
if (argc < 3)
goto error;
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]);
}
}
}
goto end;
}
/* show detailed info on a trigger */
if (weechat_strcasecmp (argv[1], "show") == 0)
{
if (argc < 3)
goto error;
ptr_trigger = trigger_search (argv[2]);
if (!ptr_trigger)
{
weechat_printf_tags (NULL, "no_trigger",
_("%sError: trigger \"%s\" not found"),
weechat_prefix ("error"), argv[2]);
goto end;
}
weechat_printf_tags (NULL, "no_trigger", "");
weechat_printf_tags (NULL, "no_trigger", _("Trigger:"));
trigger_command_display_trigger (ptr_trigger, 2);
goto end;
}
/* restore default triggers */
if (weechat_strcasecmp (argv[1], "default") == 0)
{
if ((argc >= 3) && (weechat_strcasecmp (argv[2], "-yes") == 0))
{
trigger_free_all ();
trigger_create_default ();
trigger_command_list (_("Default triggers restored:"), 0);
}
else
{
weechat_printf (NULL,
_("%sError: \"-yes\" argument is required for "
"restoring default triggers (security reason)"),
weechat_prefix ("error"));
}
goto end;
}
/* open the trigger monitor buffer */
if (weechat_strcasecmp (argv[1], "monitor") == 0)
{
trigger_buffer_open (1);
goto end;
}
error:
rc = WEECHAT_RC_ERROR;
end:
if (sargv)
weechat_string_free_split (sargv);
return rc;
}
/*
* Hooks trigger commands.
*/
void
trigger_command_init ()
{
weechat_hook_command (
"trigger",
N_("manage triggers, the Swiss Army knife for WeeChat"),
N_("list|listfull|listdefault"
" || add|addoff|addreplace <name> <hook> [\"<arguments>\" "
"[\"<conditions>\" [\"<regex>\" [\"<command>\" "
"[\"<return_code>\"]]]]]"
" || addinput [<hook>]"
" || input|output|recreate <name>"
" || set <name> <option> <value>"
" || rename|copy <name> <new_name>"
" || enable|disable|toggle [<name>|-all [<name>...]]"
" || restart <name>|-all [<name>...]"
" || show <name>"
" || del <name>|-all [<name>...]"
" || default -yes"
" || monitor"),
N_(" list: list triggers (without argument, this list is displayed)\n"
" listfull: list triggers with detailed info for each trigger\n"
"listdefault: list default triggers\n"
" add: add a trigger\n"
" addoff: add a trigger (disabled)\n"
" addreplace: add or replace an existing trigger\n"
" name: name of trigger\n"
" hook: signal, hsignal, modifier, print, command, command_run, "
"timer, config, focus\n"
" arguments: arguments for the hook, depending on hook (separated "
"by semicolons):\n"
" signal: name(s) of signal (required)\n"
" hsignal: name(s) of hsignal (required)\n"
" modifier: name(s) of modifier (required)\n"
" print: buffer, tags, message, strip_colors\n"
" command: command (required), description, arguents, "
"description of arguments, completion\n"
" command_run: command (required)\n"
" timer: interval (required), align_second (required), "
"max_calls (required)\n"
" config: name of option (required)\n"
" focus: name(s) of area (required)\n"
" conditions: evaluated conditions for the trigger\n"
" regex: one or more regular expressions to replace strings "
"in variables\n"
" command: command to execute (many commands can be separated by "
"\";\"\n"
"return_code: return code in callback (ok (default), ok_eat, error)\n"
" addinput: set input with default arguments to create a trigger\n"
" input: set input with the command used to create the trigger\n"
" output: send the command to create the trigger on the buffer\n"
" recreate: same as input, with option \"addreplace\" instead of "
"\"add\"\n"
" set: set an option in a trigger\n"
" option: name of option: name, hook, arguments, conditions, "
"regex, command, return_code\n"
" (for help on option, you can do /help "
"trigger.trigger.<name>.<option>)\n"
" value: new value for the option\n"
" rename: rename a trigger\n"
" copy: copy a trigger\n"
" enable: enable trigger(s) (without arguments: enable triggers "
"globally)\n"
" disable: disable trigger(s) (without arguments: disable triggers "
"globally)\n"
" toggle: toggle trigger(s) (without arguments: toggle triggers "
"globally)\n"
" restart: restart trigger(s) (recreate the hooks)\n"
" show: show detailed info on a trigger (with some stats)\n"
" del: delete a trigger\n"
" -all: do action on all triggers\n"
" default: restore default triggers\n"
" monitor: open the trigger monitor buffer\n"
"\n"
"When a trigger callback is called, following actions are performed, "
"in this order:\n"
" 1. check conditions; if false, exit\n"
" 2. replace text using POSIX extended regular expression(s) (if "
"defined in trigger)\n"
" 3. execute command(s) (if defined in trigger)\n"
" 4. exit with a return code (except for modifiers)\n"
"\n"
"Examples (you can also look at default triggers with /trigger "
"listdefault):\n"
" add text attributes *bold*, _underline_ and /italic/ (only in "
"user messages):\n"
" /trigger add effects modifier weechat_print \"${tg_tag_nick}\" "
"\"==\\*(\\S+)\\*==*${color:bold}$1${color:-bold}*== "
"==_(\\S+)_==_${color:underline}$1${color:-underline}_== "
"==/(\\S+)/==/${color:italic}$1${color:-italic}/\"\n"
" hide nicklist bar on small terminals:\n"
" /trigger add resize_small signal signal_sigwinch "
"\"${info:term_width} < 100\" \"\" \"/bar hide nicklist\"\n"
" /trigger add resize_big signal signal_sigwinch "
"\"${info:term_width} >= 100\" \"\" \"/bar show nicklist\"\n"
" silently save config each hour:\n"
" /trigger add cfgsave timer 3600000;0;0 \"\" \"\" \"/mute /save\""),
"list|listfull|listdefault"
" || add|addoff|addreplace %(trigger_names) %(trigger_hooks) "
"%(trigger_hook_arguments) %(trigger_hook_conditions) "
"%(trigger_hook_regex) %(trigger_hook_command) %(trigger_hook_rc)"
" || addinput %(trigger_hooks)"
" || input|output|recreate %(trigger_names)"
" || set %(trigger_names) %(trigger_options)|name %(trigger_option_value)"
" || rename|copy %(trigger_names) %(trigger_names)"
" || enable|disable|toggle|restart|del %(trigger_names)|-all "
"%(trigger_names)|%*"
" || show %(trigger_names)"
" || default"
" || monitor",
&trigger_command_trigger, NULL);
}
+25
View File
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2014 Sébastien Helleu <flashcode@flashtux.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef __WEECHAT_TRIGGER_COMMAND_H
#define __WEECHAT_TRIGGER_COMMAND_H 1
extern void trigger_command_init ();
#endif /* __WEECHAT_TRIGGER_COMMAND_H */
+392
View File
@@ -0,0 +1,392 @@
/*
* trigger-completion.c - completion for trigger commands
*
* Copyright (C) 2014 Sébastien Helleu <flashcode@flashtux.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "../weechat-plugin.h"
#include "trigger.h"
/*
* Adds triggers to completion list.
*/
int
trigger_completion_triggers_cb (void *data, const char *completion_item,
struct t_gui_buffer *buffer,
struct t_gui_completion *completion)
{
struct t_trigger *ptr_trigger;
/* make C compiler happy */
(void) data;
(void) completion_item;
(void) buffer;
for (ptr_trigger = triggers; ptr_trigger;
ptr_trigger = ptr_trigger->next_trigger)
{
weechat_hook_completion_list_add (completion, ptr_trigger->name,
0, WEECHAT_LIST_POS_SORT);
}
return WEECHAT_RC_OK;
}
/*
* Adds options for triggers to completion list.
*/
int
trigger_completion_options_cb (void *data, const char *completion_item,
struct t_gui_buffer *buffer,
struct t_gui_completion *completion)
{
int i;
/* make C compiler happy */
(void) data;
(void) completion_item;
(void) buffer;
for (i = 0; i < TRIGGER_NUM_OPTIONS; i++)
{
weechat_hook_completion_list_add (completion,
trigger_option_string[i],
0, WEECHAT_LIST_POS_SORT);
}
return WEECHAT_RC_OK;
}
/*
* Adds value of a trigger option to completion list.
*/
int
trigger_completion_option_value_cb (void *data, const char *completion_item,
struct t_gui_buffer *buffer,
struct t_gui_completion *completion)
{
const char *args;
char **argv;
int argc, index_option;
struct t_trigger *ptr_trigger;
/* make C compiler happy */
(void) data;
(void) completion_item;
(void) buffer;
args = weechat_hook_completion_get_string (completion, "args");
if (!args)
return WEECHAT_RC_OK;
argv = weechat_string_split (args, " ", 0, 0, &argc);
if (!argv)
return WEECHAT_RC_OK;
if (argc >= 3)
{
ptr_trigger = trigger_search (argv[1]);
if (ptr_trigger)
{
if (weechat_strcasecmp (argv[2], "name") == 0)
{
weechat_hook_completion_list_add (completion,
ptr_trigger->name,
0,
WEECHAT_LIST_POS_BEGINNING);
}
else
{
index_option = trigger_search_option (argv[2]);
if (index_option >= 0)
{
weechat_hook_completion_list_add (completion,
weechat_config_string (ptr_trigger->options[index_option]),
0,
WEECHAT_LIST_POS_BEGINNING);
}
}
}
}
weechat_string_free_split (argv);
return WEECHAT_RC_OK;
}
/*
* Adds hooks for triggers to completion list.
*/
int
trigger_completion_hooks_cb (void *data, const char *completion_item,
struct t_gui_buffer *buffer,
struct t_gui_completion *completion)
{
int i;
/* make C compiler happy */
(void) data;
(void) completion_item;
(void) buffer;
for (i = 0; i < TRIGGER_NUM_HOOK_TYPES; i++)
{
weechat_hook_completion_list_add (completion,
trigger_hook_type_string[i],
0, WEECHAT_LIST_POS_END);
}
return WEECHAT_RC_OK;
}
/*
* Adds a word with quotes around to completion list.
*/
void
trigger_completion_add_quoted_word (struct t_gui_completion *completion,
const char *word)
{
char *temp;
int length;
length = 1 + strlen (word) + 1 + 1;
temp = malloc (length);
if (!temp)
return;
snprintf (temp, length, "\"%s\"", word);
weechat_hook_completion_list_add (completion, temp, 0,
WEECHAT_LIST_POS_END);
free (temp);
}
/*
* Adds a default string to completion list, depending on hook type.
*
* If split is not NULL, the default string found is split using this separator,
* and therefore many words can be added to completion list.
*/
void
trigger_completion_add_default_for_hook (struct t_gui_completion *completion,
char *default_strings[], char *split)
{
const char *args;
char **argv, **items;
int argc, num_items, type, i;
args = weechat_hook_completion_get_string (completion, "args");
if (!args)
return;
argv = weechat_string_split (args, " ", 0, 0, &argc);
if (!argv)
return;
if (argc >= 3)
{
type = trigger_search_hook_type (argv[2]);
if (type >= 0)
{
if (default_strings[type][0] && split && split[0])
{
items = weechat_string_split (default_strings[type], split,
0, 0, &num_items);
if (items)
{
for (i = 0; i < num_items; i++)
{
trigger_completion_add_quoted_word (completion,
items[i]);
}
weechat_string_free_split (items);
}
}
else
{
trigger_completion_add_quoted_word (completion,
default_strings[type]);
}
}
}
weechat_string_free_split (argv);
}
/*
* Adds default arguments for hook to completion list.
*/
int
trigger_completion_hook_arguments_cb (void *data, const char *completion_item,
struct t_gui_buffer *buffer,
struct t_gui_completion *completion)
{
/* make C compiler happy */
(void) data;
(void) completion_item;
(void) buffer;
trigger_completion_add_default_for_hook (completion,
trigger_hook_default_arguments,
NULL);
weechat_hook_completion_list_add (completion, "\"\"", 0,
WEECHAT_LIST_POS_END);
return WEECHAT_RC_OK;
}
/*
* Adds default conditions for hook to completion list.
*/
int
trigger_completion_hook_conditions_cb (void *data, const char *completion_item,
struct t_gui_buffer *buffer,
struct t_gui_completion *completion)
{
/* make C compiler happy */
(void) data;
(void) completion_item;
(void) buffer;
weechat_hook_completion_list_add (completion,
"\"" TRIGGER_HOOK_DEFAULT_CONDITIONS "\"",
0,
WEECHAT_LIST_POS_END);
weechat_hook_completion_list_add (completion, "\"\"", 0,
WEECHAT_LIST_POS_END);
return WEECHAT_RC_OK;
}
/*
* Adds default regular expression for hook to completion list.
*/
int
trigger_completion_hook_regex_cb (void *data, const char *completion_item,
struct t_gui_buffer *buffer,
struct t_gui_completion *completion)
{
/* make C compiler happy */
(void) data;
(void) completion_item;
(void) buffer;
weechat_hook_completion_list_add (completion,
"\"" TRIGGER_HOOK_DEFAULT_REGEX "\"",
0,
WEECHAT_LIST_POS_END);
weechat_hook_completion_list_add (completion, "\"\"", 0,
WEECHAT_LIST_POS_END);
return WEECHAT_RC_OK;
}
/*
* Adds default command for hook to completion list.
*/
int
trigger_completion_hook_command_cb (void *data, const char *completion_item,
struct t_gui_buffer *buffer,
struct t_gui_completion *completion)
{
/* make C compiler happy */
(void) data;
(void) completion_item;
(void) buffer;
weechat_hook_completion_list_add (completion,
"\"" TRIGGER_HOOK_DEFAULT_COMMAND "\"",
0,
WEECHAT_LIST_POS_END);
weechat_hook_completion_list_add (completion, "\"\"", 0,
WEECHAT_LIST_POS_END);
return WEECHAT_RC_OK;
}
/*
* Adds default return code(s) for hook to completion list.
*/
int
trigger_completion_hook_rc_cb (void *data, const char *completion_item,
struct t_gui_buffer *buffer,
struct t_gui_completion *completion)
{
/* make C compiler happy */
(void) data;
(void) completion_item;
(void) buffer;
trigger_completion_add_default_for_hook (completion,
trigger_hook_default_rc,
",");
return WEECHAT_RC_OK;
}
/*
* Hooks completions.
*/
void
trigger_completion_init ()
{
weechat_hook_completion ("trigger_names",
N_("triggers"),
&trigger_completion_triggers_cb, NULL);
weechat_hook_completion ("trigger_options",
N_("options for triggers"),
&trigger_completion_options_cb, NULL);
weechat_hook_completion ("trigger_option_value",
N_("value of a trigger option"),
&trigger_completion_option_value_cb, NULL);
weechat_hook_completion ("trigger_hooks",
N_("hooks for triggers"),
&trigger_completion_hooks_cb, NULL);
weechat_hook_completion ("trigger_hook_arguments",
N_("default arguments for a hook"),
&trigger_completion_hook_arguments_cb, NULL);
weechat_hook_completion ("trigger_hook_conditions",
N_("default conditions for a hook"),
&trigger_completion_hook_conditions_cb, NULL);
weechat_hook_completion ("trigger_hook_regex",
N_("default regular expression for a hook"),
&trigger_completion_hook_regex_cb, NULL);
weechat_hook_completion ("trigger_hook_command",
N_("default command for a hook"),
&trigger_completion_hook_command_cb, NULL);
weechat_hook_completion ("trigger_hook_rc",
N_("default return codes for hook callback"),
&trigger_completion_hook_rc_cb, NULL);
}
+25
View File
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2014 Sébastien Helleu <flashcode@flashtux.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef __WEECHAT_TRIGGER_COMPLETION_H
#define __WEECHAT_TRIGGER_COMPLETION_H 1
extern void trigger_completion_init ();
#endif /* __WEECHAT_TRIGGER_COMPLETION_H */
+692
View File
@@ -0,0 +1,692 @@
/*
* trigger-config.c - trigger configuration options (file trigger.conf)
*
* Copyright (C) 2014 Sébastien Helleu <flashcode@flashtux.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "../weechat-plugin.h"
#include "trigger.h"
#include "trigger-config.h"
struct t_config_file *trigger_config_file = NULL;
struct t_config_section *trigger_config_section_trigger = NULL;
/* trigger config, look section */
struct t_config_option *trigger_config_look_enabled;
struct t_config_option *trigger_config_look_monitor_strip_colors;
/* trigger config, color section */
struct t_config_option *trigger_config_color_flag_command;
struct t_config_option *trigger_config_color_flag_conditions;
struct t_config_option *trigger_config_color_flag_regex;
struct t_config_option *trigger_config_color_flag_return_code;
struct t_config_option *trigger_config_color_regex;
struct t_config_option *trigger_config_color_replace;
struct t_config_option *trigger_config_color_trigger;
struct t_config_option *trigger_config_color_trigger_disabled;
char *trigger_config_default_list[][1 + TRIGGER_NUM_OPTIONS] =
{
/* beep on highlight/private message */
{ "beep", "on",
"print",
"",
"${tg_highlight} || ${tg_msg_pv}",
"",
"/print -stderr \\a",
"ok" },
/* hide passwords in commands */
{ "cmd_pass", "on",
"modifier",
"5000|input_text_display;5000|history_add;5000|irc_command_auth",
"",
"==^("
"(/(msg|quote) +nickserv "
"+(id|identify|register|ghost \\S+|release \\S+|regain \\S+) +)|"
"/oper +\\S+ +|"
"/quote pass +|"
"/set +\\S*password\\S* +|"
"/secure +(passphrase|decrypt|set \\S+) +)"
"(.*)"
"==$1$.*+",
"",
"" },
/* hide password in IRC auth message displayed */
{ "msg_auth", "on",
"modifier",
"5000|irc_message_auth",
"",
"==^(.*(id|identify|register|ghost \\S+|release \\S+) +)(.*)==$1$.*+",
"",
"" },
/* hide server password in commands /server and /connect */
{ "server_pass", "on",
"modifier",
"5000|input_text_display;5000|history_add",
"",
"==^(/(server|connect) .*-(sasl_)?password=)(\\S+)(.*)==$1$.*4$5"
"",
"" },
{ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
};
/*
* Callback for changes on option "trigger.look.enabled".
*/
void
trigger_config_change_enabled (void *data, struct t_config_option *option)
{
/* make C compiler happy */
(void) data;
trigger_enabled = weechat_config_boolean (option);
}
/*
* Callback for changes on option "trigger.trigger.xxx.enabled".
*/
void
trigger_config_change_trigger_enabled (void *data,
struct t_config_option *option)
{
struct t_trigger *ptr_trigger;
/* make C compiler happy */
(void) data;
ptr_trigger = trigger_search_with_option (option);
if (!ptr_trigger)
return;
if (weechat_config_boolean (option))
trigger_hook (ptr_trigger);
else
trigger_unhook (ptr_trigger);
}
/*
* Callback for changes on option "trigger.trigger.xxx.hook".
*/
void
trigger_config_change_trigger_hook (void *data, struct t_config_option *option)
{
struct t_trigger *ptr_trigger;
/* make C compiler happy */
(void) data;
ptr_trigger = trigger_search_with_option (option);
if (!ptr_trigger)
return;
if (ptr_trigger->options[TRIGGER_OPTION_ARGUMENTS])
trigger_hook (ptr_trigger);
}
/*
* Callback for changes on option "trigger.trigger.xxx.arguments".
*/
void
trigger_config_change_trigger_arguments (void *data,
struct t_config_option *option)
{
struct t_trigger *ptr_trigger;
/* make C compiler happy */
(void) data;
ptr_trigger = trigger_search_with_option (option);
if (!ptr_trigger)
return;
if (ptr_trigger->options[TRIGGER_OPTION_HOOK])
trigger_hook (ptr_trigger);
}
/*
* Callback for changes on option "trigger.trigger.xxx.regex".
*/
void
trigger_config_change_trigger_regex (void *data, struct t_config_option *option)
{
struct t_trigger *ptr_trigger;
/* make C compiler happy */
(void) data;
ptr_trigger = trigger_search_with_option (option);
if (!ptr_trigger)
return;
trigger_split_regex (ptr_trigger->name, weechat_config_string (option),
&ptr_trigger->regex_count, &ptr_trigger->regex);
}
/*
* Callback for changes on option "trigger.trigger.xxx.command".
*/
void
trigger_config_change_trigger_command (void *data,
struct t_config_option *option)
{
struct t_trigger *ptr_trigger;
/* make C compiler happy */
(void) data;
ptr_trigger = trigger_search_with_option (option);
if (!ptr_trigger)
return;
trigger_split_command (weechat_config_string (option),
&ptr_trigger->commands_count,
&ptr_trigger->commands);
}
/*
* Creates an option for a trigger.
*
* Returns pointer to new option, NULL if error.
*/
struct t_config_option *
trigger_config_create_trigger_option (const char *trigger_name, int index_option,
const char *value)
{
struct t_config_option *ptr_option;
int length;
char *option_name;
ptr_option = NULL;
length = strlen (trigger_name) + 1 +
strlen (trigger_option_string[index_option]) + 1;
option_name = malloc (length);
if (!option_name)
return NULL;
snprintf (option_name, length, "%s.%s",
trigger_name, trigger_option_string[index_option]);
switch (index_option)
{
case TRIGGER_OPTION_ENABLED:
ptr_option = weechat_config_new_option (
trigger_config_file, trigger_config_section_trigger,
option_name, "boolean",
N_("if disabled, the hooks are removed from trigger, so it is "
"not called any more"),
NULL, 0, 0, value, NULL, 0, NULL, NULL,
&trigger_config_change_trigger_enabled, NULL, NULL, NULL);
break;
case TRIGGER_OPTION_HOOK:
ptr_option = weechat_config_new_option (
trigger_config_file, trigger_config_section_trigger,
option_name, "integer",
N_("type of hook used"),
trigger_hook_option_values,
0, 0, value, NULL, 0, NULL, NULL,
&trigger_config_change_trigger_hook, NULL, NULL, NULL);
break;
case TRIGGER_OPTION_ARGUMENTS:
ptr_option = weechat_config_new_option (
trigger_config_file, trigger_config_section_trigger,
option_name, "string",
N_("arguments for the hook (depend on the hook type, see /help "
"trigger)"),
NULL, 0, 0, value, NULL, 0, NULL, NULL,
&trigger_config_change_trigger_arguments, NULL, NULL, NULL);
break;
case TRIGGER_OPTION_CONDITIONS:
ptr_option = weechat_config_new_option (
trigger_config_file, trigger_config_section_trigger,
option_name, "string",
N_("condition(s) for running the command (it is checked in "
"hook callback) (note: content is evaluated when trigger is "
"run, see /help eval)"),
NULL, 0, 0, value, NULL, 0,
NULL, NULL, NULL, NULL, NULL, NULL);
break;
case TRIGGER_OPTION_REGEX:
ptr_option = weechat_config_new_option (
trigger_config_file, trigger_config_section_trigger,
option_name, "string",
N_("replace text with a POSIX extended regular expression (it "
"is done only if conditions are OK, and before running the "
"command) (note: content is evaluated on trigger creation, "
"see /help eval); format is: \"/regex/replace/var\" (var "
"is the hashtable variable to replace, it is optional), "
"many regex can be separated by a space, for example: "
"\"/regex1/replace1/var1 /regex2/replace2/var2\"; the "
"separator \"/\" can be replaced by any char (one or more "
"identical chars), except '\\' and parentheses; matching "
"groups can be used in replace: $0 to $99, $+ for last "
"match and $.cN to replace all chars of group N by char c"),
NULL, 0, 0, value, NULL, 0, NULL, NULL,
&trigger_config_change_trigger_regex, NULL, NULL, NULL);
break;
case TRIGGER_OPTION_COMMAND:
ptr_option = weechat_config_new_option (
trigger_config_file, trigger_config_section_trigger,
option_name, "string",
N_("command(s) to run if conditions are OK, after regex "
"replacements (many commands can be separated by semicolons)"),
NULL, 0, 0, value, NULL, 0, NULL, NULL,
&trigger_config_change_trigger_command, NULL, NULL, NULL);
break;
case TRIGGER_OPTION_RETURN_CODE:
ptr_option = weechat_config_new_option (
trigger_config_file, trigger_config_section_trigger,
option_name, "integer",
N_("return code for hook callback (see plugin API reference to "
"know where ok_eat or error can be used efficiently)"),
"ok|ok_eat|error", 0, 0, value, NULL, 0,
NULL, NULL, NULL, NULL, NULL, NULL);
break;
case TRIGGER_NUM_OPTIONS:
break;
}
free (option_name);
return ptr_option;
}
/*
* Creates option for a temporary trigger (when reading configuration file).
*/
void
trigger_config_create_option_temp (struct t_trigger *temp_trigger,
int index_option, const char *value)
{
struct t_config_option *new_option;
new_option = trigger_config_create_trigger_option (temp_trigger->name,
index_option, value);
if (new_option
&& (index_option >= 0) && (index_option < TRIGGER_NUM_OPTIONS))
{
temp_trigger->options[index_option] = new_option;
}
}
/*
* Uses temporary triggers (created by reading configuration file).
*/
void
trigger_config_use_temp_triggers ()
{
struct t_trigger *ptr_temp_trigger, *next_temp_trigger;
int i, num_options_ok;
for (ptr_temp_trigger = triggers_temp; ptr_temp_trigger;
ptr_temp_trigger = ptr_temp_trigger->next_trigger)
{
num_options_ok = 0;
for (i = 0; i < TRIGGER_NUM_OPTIONS; i++)
{
if (!ptr_temp_trigger->options[i])
{
ptr_temp_trigger->options[i] =
trigger_config_create_trigger_option (ptr_temp_trigger->name,
i,
trigger_option_default[i]);
}
if (ptr_temp_trigger->options[i])
num_options_ok++;
}
if (num_options_ok == TRIGGER_NUM_OPTIONS)
{
trigger_new_with_options (ptr_temp_trigger->name,
ptr_temp_trigger->options);
}
else
{
for (i = 0; i < TRIGGER_NUM_OPTIONS; i++)
{
if (ptr_temp_trigger->options[i])
{
weechat_config_option_free (ptr_temp_trigger->options[i]);
ptr_temp_trigger->options[i] = NULL;
}
}
}
}
/* free all temporary triggers */
while (triggers_temp)
{
next_temp_trigger = triggers_temp->next_trigger;
if (triggers_temp->name)
free (triggers_temp->name);
free (triggers_temp);
triggers_temp = next_temp_trigger;
}
last_trigger_temp = NULL;
}
/*
* Reads a trigger option in trigger configuration file.
*/
int
trigger_config_trigger_read_cb (void *data, struct t_config_file *config_file,
struct t_config_section *section,
const char *option_name, const char *value)
{
char *pos_option, *trigger_name;
struct t_trigger *ptr_temp_trigger;
int index_option;
/* make C compiler happy */
(void) data;
(void) config_file;
(void) section;
if (!option_name)
return WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE;
pos_option = strchr (option_name, '.');
if (!pos_option)
return WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE;
trigger_name = weechat_strndup (option_name, pos_option - option_name);
if (!trigger_name)
return WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE;
pos_option++;
/* search temporary trigger */
for (ptr_temp_trigger = triggers_temp; ptr_temp_trigger;
ptr_temp_trigger = ptr_temp_trigger->next_trigger)
{
if (strcmp (ptr_temp_trigger->name, trigger_name) == 0)
break;
}
if (!ptr_temp_trigger)
{
/* create new temporary trigger */
ptr_temp_trigger = trigger_alloc (trigger_name);
if (ptr_temp_trigger)
trigger_add (ptr_temp_trigger, &triggers_temp, &last_trigger_temp);
}
if (ptr_temp_trigger)
{
index_option = trigger_search_option (pos_option);
if (index_option >= 0)
{
trigger_config_create_option_temp (ptr_temp_trigger, index_option,
value);
}
else
{
weechat_printf (NULL,
_("%sWarning: unknown option for section \"%s\": "
"%s (value: \"%s\")"),
weechat_prefix ("error"),
TRIGGER_CONFIG_SECTION_TRIGGER,
option_name, value);
}
}
free (trigger_name);
return WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE;
}
/*
* Writes default triggers in trigger configuration file.
*/
int
trigger_config_trigger_write_default_cb (void *data,
struct t_config_file *config_file,
const char *section_name)
{
int i, j, quotes;
char option_name[512];
/* make C compiler happy */
(void) data;
if (!weechat_config_write_line (config_file, section_name, NULL))
return WEECHAT_CONFIG_WRITE_ERROR;
for (i = 0; trigger_config_default_list[i][0]; i++)
{
for (j = 0; j < TRIGGER_NUM_OPTIONS; j++)
{
snprintf (option_name, sizeof (option_name),
"%s.%s",
trigger_config_default_list[i][0],
trigger_option_string[j]);
quotes = (j & (TRIGGER_OPTION_ARGUMENTS | TRIGGER_OPTION_CONDITIONS |
TRIGGER_OPTION_REGEX | TRIGGER_OPTION_COMMAND));
if (!weechat_config_write_line (config_file, option_name, "%s%s%s",
(quotes) ? "\"" : "",
trigger_config_default_list[i][j + 1],
(quotes) ? "\"" : ""))
{
return WEECHAT_CONFIG_WRITE_ERROR;
}
}
}
return WEECHAT_CONFIG_WRITE_OK;
}
/*
* Reloads trigger configuration file.
*/
int
trigger_config_reload_cb (void *data, struct t_config_file *config_file)
{
int rc;
/* make C compiler happy */
(void) data;
trigger_free_all ();
rc = weechat_config_reload (config_file);
trigger_config_use_temp_triggers ();
return rc;
}
/*
* Initializes trigger configuration file.
*
* Returns:
* 1: OK
* 0: error
*/
int
trigger_config_init ()
{
struct t_config_section *ptr_section;
trigger_config_file = weechat_config_new (TRIGGER_CONFIG_NAME,
&trigger_config_reload_cb, NULL);
if (!trigger_config_file)
return 0;
/* look */
ptr_section = weechat_config_new_section (trigger_config_file, "look",
0, 0,
NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL,
NULL, NULL);
if (!ptr_section)
{
weechat_config_free (trigger_config_file);
return 0;
}
trigger_config_look_enabled = weechat_config_new_option (
trigger_config_file, ptr_section,
"enabled", "boolean",
N_("enable trigger support"),
NULL, 0, 0, "on", NULL, 0, NULL, NULL,
&trigger_config_change_enabled, NULL, NULL, NULL);
trigger_config_look_monitor_strip_colors = weechat_config_new_option (
trigger_config_file, ptr_section,
"monitor_strip_colors", "boolean",
N_("strip colors in hashtable values displayed on monitor buffer"),
NULL, 0, 0, "off", NULL, 0, NULL, NULL,
NULL, NULL, NULL, NULL);
/* color */
ptr_section = weechat_config_new_section (trigger_config_file, "color",
0, 0,
NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL,
NULL, NULL);
if (!ptr_section)
{
weechat_config_free (trigger_config_file);
return 0;
}
trigger_config_color_flag_command = weechat_config_new_option (
trigger_config_file, ptr_section,
"flag_command", "color",
N_("text color for command flag (in /trigger list)"),
NULL, 0, 0, "lightgreen", NULL, 0,
NULL, NULL, NULL, NULL, NULL, NULL);
trigger_config_color_flag_conditions = weechat_config_new_option (
trigger_config_file, ptr_section,
"flag_conditions", "color",
N_("text color for conditions flag (in /trigger list)"),
NULL, 0, 0, "yellow", NULL, 0,
NULL, NULL, NULL, NULL, NULL, NULL);
trigger_config_color_flag_regex = weechat_config_new_option (
trigger_config_file, ptr_section,
"flag_regex", "color",
N_("text color for regex flag (in /trigger list)"),
NULL, 0, 0, "lightcyan", NULL, 0,
NULL, NULL, NULL, NULL, NULL, NULL);
trigger_config_color_flag_return_code = weechat_config_new_option (
trigger_config_file, ptr_section,
"flag_return_code", "color",
N_("text color for return code flag (in /trigger list)"),
NULL, 0, 0, "lightmagenta", NULL, 0,
NULL, NULL, NULL, NULL, NULL, NULL);
trigger_config_color_regex = weechat_config_new_option (
trigger_config_file, ptr_section,
"regex", "color",
N_("text color for regular expressions"),
NULL, 0, 0, "white", NULL, 0,
NULL, NULL, NULL, NULL, NULL, NULL);
trigger_config_color_replace = weechat_config_new_option (
trigger_config_file, ptr_section,
"replace", "color",
N_("text color for replacement text (for regular expressions)"),
NULL, 0, 0, "cyan", NULL, 0,
NULL, NULL, NULL, NULL, NULL, NULL);
trigger_config_color_trigger = weechat_config_new_option (
trigger_config_file, ptr_section,
"trigger", "color",
N_("text color for trigger name"),
NULL, 0, 0, "green", NULL, 0,
NULL, NULL, NULL, NULL, NULL, NULL);
trigger_config_color_trigger_disabled = weechat_config_new_option (
trigger_config_file, ptr_section,
"trigger_disabled", "color",
N_("text color for disabled trigger name"),
NULL, 0, 0, "red", NULL, 0,
NULL, NULL, NULL, NULL, NULL, NULL);
/* trigger */
ptr_section = weechat_config_new_section (trigger_config_file,
TRIGGER_CONFIG_SECTION_TRIGGER,
0, 0,
&trigger_config_trigger_read_cb, NULL,
NULL, NULL,
&trigger_config_trigger_write_default_cb, NULL,
NULL, NULL,
NULL, NULL);
if (!ptr_section)
{
weechat_config_free (trigger_config_file);
return 0;
}
trigger_config_section_trigger = ptr_section;
return 1;
}
/*
* Reads trigger configuration file.
*/
int
trigger_config_read ()
{
int rc;
rc = weechat_config_read (trigger_config_file);
trigger_config_use_temp_triggers ();
return rc;
}
/*
* Writes trigger configuration file.
*/
int
trigger_config_write ()
{
return weechat_config_write (trigger_config_file);
}
/*
* Frees trigger configuration.
*/
void
trigger_config_free ()
{
weechat_config_free (trigger_config_file);
}
+51
View File
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2014 Sébastien Helleu <flashcode@flashtux.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef __WEECHAT_TRIGGER_CONFIG_H
#define __WEECHAT_TRIGGER_CONFIG_H 1
#define TRIGGER_CONFIG_NAME "trigger"
#define TRIGGER_CONFIG_SECTION_TRIGGER "trigger"
extern struct t_config_file *trigger_config_file;
extern struct t_config_section *trigger_config_section_trigger;
extern struct t_config_option *trigger_config_look_enabled;
extern struct t_config_option *trigger_config_look_monitor_strip_colors;
extern struct t_config_option *trigger_config_color_flag_command;
extern struct t_config_option *trigger_config_color_flag_conditions;
extern struct t_config_option *trigger_config_color_flag_regex;
extern struct t_config_option *trigger_config_color_flag_return_code;
extern struct t_config_option *trigger_config_color_regex;
extern struct t_config_option *trigger_config_color_replace;
extern struct t_config_option *trigger_config_color_trigger;
extern struct t_config_option *trigger_config_color_trigger_disabled;
extern char *trigger_config_default_list[][1 + TRIGGER_NUM_OPTIONS];
extern struct t_config_option *trigger_config_create_trigger_option (const char *trigger_name,
int index_option,
const char *value);
extern int trigger_config_init ();
extern int trigger_config_read ();
extern int trigger_config_write ();
extern void trigger_config_free ();
#endif /* __WEECHAT_TRIGGER_CONFIG_H */
File diff suppressed because it is too large Load Diff
+161
View File
@@ -0,0 +1,161 @@
/*
* Copyright (C) 2014 Sébastien Helleu <flashcode@flashtux.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef __WEECHAT_TRIGGER_H
#define __WEECHAT_TRIGGER_H 1
#include <regex.h>
#define weechat_plugin weechat_trigger_plugin
#define TRIGGER_PLUGIN_NAME "trigger"
#define TRIGGER_HOOK_DEFAULT_CONDITIONS "${...}"
#define TRIGGER_HOOK_DEFAULT_REGEX "/abc/def"
#define TRIGGER_HOOK_DEFAULT_COMMAND "/cmd"
enum t_trigger_option
{
TRIGGER_OPTION_ENABLED = 0, /* true if trigger is enabled */
TRIGGER_OPTION_HOOK, /* hook (signal, modifier, ...) */
TRIGGER_OPTION_ARGUMENTS, /* arguments for hook */
TRIGGER_OPTION_CONDITIONS, /* conditions for trigger */
TRIGGER_OPTION_REGEX, /* replace text with 1 or more regex */
TRIGGER_OPTION_COMMAND, /* command run if conditions are OK */
TRIGGER_OPTION_RETURN_CODE, /* return code for hook callback */
/* number of trigger options */
TRIGGER_NUM_OPTIONS,
};
enum t_trigger_hook_type
{
TRIGGER_HOOK_SIGNAL = 0,
TRIGGER_HOOK_HSIGNAL,
TRIGGER_HOOK_MODIFIER,
TRIGGER_HOOK_PRINT,
TRIGGER_HOOK_COMMAND,
TRIGGER_HOOK_COMMAND_RUN,
TRIGGER_HOOK_TIMER,
TRIGGER_HOOK_CONFIG,
TRIGGER_HOOK_FOCUS,
/* number of hook types */
TRIGGER_NUM_HOOK_TYPES,
};
enum t_trigger_return_code
{
TRIGGER_RC_OK = 0,
TRIGGER_RC_OK_EAT,
TRIGGER_RC_ERROR,
/* number of return codes */
TRIGGER_NUM_RETURN_CODES,
};
struct t_trigger_regex
{
char *variable; /* the hashtable key used */
char *str_regex; /* regex to search for replacement */
regex_t *regex; /* compiled regex */
char *replace; /* replacement text */
char *replace_escaped; /* repl. text (with chars escaped) */
};
struct t_trigger
{
/* user choices */
char *name; /* trigger name */
struct t_config_option *options[TRIGGER_NUM_OPTIONS];
/* internal vars */
/* hooks */
int hooks_count; /* number of hooks */
struct t_hook **hooks; /* array of hooks (signal, ...) */
unsigned long hook_count_cb; /* number of calls made to callback */
unsigned long hook_count_cmd; /* number of commands run in callback*/
int hook_running; /* 1 if one hook callback is running */
char *hook_print_buffers; /* buffers (for hook_print only) */
/* regular expressions with their replacement text */
int regex_count; /* number of regex */
struct t_trigger_regex *regex; /* array of regex */
/* commands */
int commands_count; /* number of commands */
char **commands; /* commands */
/* links to other triggers */
struct t_trigger *prev_trigger; /* link to previous trigger */
struct t_trigger *next_trigger; /* link to next trigger */
};
extern struct t_weechat_plugin *weechat_trigger_plugin;
extern char *trigger_option_string[];
extern char *trigger_option_default[];
extern char *trigger_hook_type_string[];
extern char *trigger_hook_option_values;
extern char *trigger_hook_default_arguments[];
extern char *trigger_hook_default_rc[];
extern char *trigger_hook_regex_default_var[];
extern char *trigger_return_code_string[];
extern int trigger_return_code[];
extern struct t_trigger *triggers;
extern struct t_trigger *last_trigger;
extern int triggers_count;
extern struct t_trigger *triggers_temp;
extern struct t_trigger *last_trigger_temp;
extern int trigger_enabled;
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_free_regex (int *regex_count,
struct t_trigger_regex **regex);
extern void trigger_split_regex (const char *trigger_name,
const char *str_regex,
int *regex_count,
struct t_trigger_regex **regex);
extern void trigger_split_command (const char *command,
int *commands_count, char ***commands);
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 **list_triggers,
struct t_trigger **last_list_trigger);
extern struct t_trigger *trigger_new_with_options (const char *name,
struct t_config_option **options);
extern struct t_trigger *trigger_new (const char *name,
const char *enabled,
const char *hook,
const char *arguments,
const char *conditions,
const char *replace,
const char *command,
const char *return_code);
extern void trigger_create_default ();
extern int trigger_rename (struct t_trigger *trigger, const char *name);
extern struct t_trigger *trigger_copy (struct t_trigger *trigger,
const char *name);
extern void trigger_free (struct t_trigger *trigger);
extern void trigger_free_all ();
#endif /* __WEECHAT_TRIGGER_H */
+17 -1
View File
@@ -57,7 +57,7 @@ struct timeval;
* please change the date with current one; for a second change at same
* date, increment the 01, otherwise please keep 01.
*/
#define WEECHAT_PLUGIN_API_VERSION "20140210-01"
#define WEECHAT_PLUGIN_API_VERSION "20140221-01"
/* macros for defining plugin infos */
#define WEECHAT_PLUGIN_NAME(__name) \
@@ -240,6 +240,7 @@ struct t_weechat_plugin
char *(*string_remove_quotes) (const char *string, const char *quotes);
char *(*string_strip) (const char *string, int left, int right,
const char *chars);
char *(*string_convert_escaped_chars) (const char *string);
char *(*string_mask_to_regex) (const char *mask);
const char *(*string_regex_flags) (const char *regex, int default_flags,
int *flags);
@@ -247,8 +248,12 @@ struct t_weechat_plugin
int (*string_has_highlight) (const char *string,
const char *highlight_words);
int (*string_has_highlight_regex) (const char *string, const char *regex);
char *(*string_replace_regex) (const char *string, void *regex,
const char *replace,
const char reference_char);
char **(*string_split) (const char *string, const char *separators,
int keep_eol, int num_items_max, int *num_items);
char **(*string_split_shell) (const char *string, int *num_items);
void (*string_free_split) (char **split_string);
char *(*string_build_with_split_string) (const char **split_string,
const char *separator);
@@ -356,6 +361,7 @@ struct t_weechat_plugin
const char *key,
const char *value),
void *callback_map_data);
struct t_hashtable *(*hashtable_dup) (struct t_hashtable *hashtable);
int (*hashtable_get_integer) (struct t_hashtable *hashtable,
const char *property);
const char *(*hashtable_get_string) (struct t_hashtable *hashtable,
@@ -997,6 +1003,8 @@ extern int weechat_plugin_end (struct t_weechat_plugin *plugin);
weechat_plugin->string_remove_quotes(__string, __quotes)
#define weechat_string_strip(__string, __left, __right, __chars) \
weechat_plugin->string_strip(__string, __left, __right, __chars)
#define weechat_string_convert_escaped_chars(__string) \
weechat_plugin->string_convert_escaped_chars(__string)
#define weechat_string_mask_to_regex(__mask) \
weechat_plugin->string_mask_to_regex(__mask)
#define weechat_string_regex_flags(__regex, __default_flags, __flags) \
@@ -1008,10 +1016,16 @@ extern int weechat_plugin_end (struct t_weechat_plugin *plugin);
weechat_plugin->string_has_highlight(__string, __highlight_words)
#define weechat_string_has_highlight_regex(__string, __regex) \
weechat_plugin->string_has_highlight_regex(__string, __regex)
#define weechat_string_replace_regex(__string, __regex, __replace, \
__reference_char) \
weechat_plugin->string_replace_regex(__string, __regex, __replace, \
__reference_char)
#define weechat_string_split(__string, __separator, __eol, __max, \
__num_items) \
weechat_plugin->string_split(__string, __separator, __eol, \
__max, __num_items)
#define weechat_string_split_shell(__string, __num_items) \
weechat_plugin->string_split_shell(__string, __num_items)
#define weechat_string_free_split(__split_string) \
weechat_plugin->string_free_split(__split_string)
#define weechat_string_build_with_split_string(__split_string, \
@@ -1155,6 +1169,8 @@ extern int weechat_plugin_end (struct t_weechat_plugin *plugin);
__cb_map_data) \
weechat_plugin->hashtable_map_string(__hashtable, __cb_map, \
__cb_map_data)
#define weechat_hashtable_dup(__hashtable) \
weechat_plugin->hashtable_dup(__hashtable)
#define weechat_hashtable_get_integer(__hashtable, __property) \
weechat_plugin->hashtable_get_integer(__hashtable, __property)
#define weechat_hashtable_get_string(__hashtable, __property) \