1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-30 06:46:38 +02:00

api: add function hook_line

This commit is contained in:
Sébastien Helleu
2018-08-12 21:45:00 +02:00
parent 12a6f74ec0
commit 42be1a74a0
47 changed files with 3836 additions and 1226 deletions
+138 -11
View File
@@ -227,6 +227,7 @@ void
trigger_callback_replace_regex (struct t_trigger *trigger,
struct t_hashtable *pointers,
struct t_hashtable *extra_vars,
struct t_weelist *vars_updated,
int display_monitor)
{
char *value;
@@ -312,6 +313,11 @@ trigger_callback_replace_regex (struct t_trigger *trigger,
weechat_color ("chat_delimiters"));
}
weechat_hashtable_set (extra_vars, ptr_key, value);
if (vars_updated)
{
weechat_list_add (vars_updated, ptr_key, WEECHAT_LIST_POS_END,
NULL);
}
free (value);
}
}
@@ -392,7 +398,8 @@ void
trigger_callback_execute (struct t_trigger *trigger,
struct t_gui_buffer *buffer,
struct t_hashtable *pointers,
struct t_hashtable *extra_vars)
struct t_hashtable *extra_vars,
struct t_weelist *vars_updated)
{
int display_monitor;
@@ -409,7 +416,7 @@ trigger_callback_execute (struct t_trigger *trigger,
{
/* replace text with regex */
trigger_callback_replace_regex (trigger, pointers, extra_vars,
display_monitor);
vars_updated, display_monitor);
/* execute command(s) */
trigger_callback_run_command (trigger, buffer, pointers, extra_vars,
@@ -521,7 +528,7 @@ trigger_callback_signal_cb (const void *pointer, void *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);
trigger_callback_execute (trigger, NULL, pointers, extra_vars, NULL);
end:
TRIGGER_CALLBACK_CB_END(trigger_rc);
@@ -569,7 +576,7 @@ trigger_callback_hsignal_cb (const void *pointer, void *data,
weechat_hashtable_set (extra_vars, "tg_signal", signal);
/* execute the trigger (conditions, regex, command) */
trigger_callback_execute (trigger, NULL, pointers, extra_vars);
trigger_callback_execute (trigger, NULL, pointers, extra_vars, NULL);
end:
TRIGGER_CALLBACK_CB_END(trigger_rc);
@@ -753,7 +760,7 @@ trigger_callback_modifier_cb (const void *pointer, void *data,
}
/* execute the trigger (conditions, regex, command) */
trigger_callback_execute (trigger, buffer, pointers, extra_vars);
trigger_callback_execute (trigger, buffer, pointers, extra_vars, NULL);
end:
ptr_string = weechat_hashtable_get (extra_vars, "tg_string");
@@ -768,6 +775,126 @@ end:
TRIGGER_CALLBACK_CB_END(string_modified);
}
/*
* Callback for a line hooked.
*/
struct t_hashtable *
trigger_callback_line_cb (const void *pointer, void *data,
struct t_hashtable *line)
{
struct t_hashtable *hashtable;
struct t_gui_buffer *buffer;
struct t_weelist_item *ptr_item;
long unsigned int value;
const char *ptr_key, *ptr_value;
char **tags, *str_tags;
int rc, num_tags, length;
TRIGGER_CALLBACK_CB_INIT(NULL);
hashtable = NULL;
TRIGGER_CALLBACK_CB_NEW_POINTERS;
TRIGGER_CALLBACK_CB_NEW_VARS_UPDATED;
extra_vars = weechat_hashtable_dup (line);
weechat_hashtable_remove (extra_vars, "buffer");
weechat_hashtable_remove (extra_vars, "tags_count");
weechat_hashtable_remove (extra_vars, "tags");
/* add data in hashtables used for conditions/replace/command */
ptr_value = weechat_hashtable_get (line, "buffer");
if (!ptr_value || (ptr_value[0] != '0') || (ptr_value[1] != 'x'))
goto end;
rc = sscanf (ptr_value + 2, "%lx", &value);
if ((rc == EOF) || (rc < 1))
goto end;
buffer = (void *)value;
weechat_hashtable_set (pointers, "buffer", buffer);
ptr_value = weechat_hashtable_get (line, "tags");
tags = weechat_string_split ((ptr_value) ? ptr_value : "", ",", 0, 0,
&num_tags);
/* build string with tags and commas around: ",tag1,tag2,tag3," */
length = 1 + strlen ((ptr_value) ? ptr_value : "") + 1 + 1;
str_tags = malloc (length);
if (str_tags)
{
snprintf (str_tags, length, ",%s,",
(ptr_value) ? ptr_value : "");
weechat_hashtable_set (extra_vars, "tg_tags", str_tags);
free (str_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,
vars_updated);
hashtable = weechat_hashtable_new (32,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING,
NULL, NULL);
if (hashtable)
{
/* copy updated variables into the result "hashtable" */
for (ptr_item = weechat_list_get (vars_updated, 0); ptr_item;
ptr_item = weechat_list_next (ptr_item))
{
ptr_key = weechat_list_string (ptr_item);
if (weechat_hashtable_has_key (extra_vars, ptr_key))
{
if (strcmp (ptr_key, "tg_tags") == 0)
{
/*
* remove commas at the beginning/end of tg_tags and
* rename the key to "tags"
*/
ptr_value = weechat_hashtable_get (extra_vars, ptr_key);
if (ptr_value && ptr_value[0])
{
str_tags = strdup (
(ptr_value[0] == ',') ? ptr_value + 1 : ptr_value);
if (str_tags)
{
if (str_tags[0]
&& (str_tags[strlen (str_tags) - 1] == ','))
{
str_tags[strlen (str_tags) - 1] = '\0';
}
weechat_hashtable_set (hashtable,
"tags", str_tags);
free (str_tags);
}
}
else
{
weechat_hashtable_set (hashtable, "tags", ptr_value);
}
}
else
{
weechat_hashtable_set (
hashtable,
ptr_key,
weechat_hashtable_get (extra_vars, ptr_key));
}
}
}
}
end:
TRIGGER_CALLBACK_CB_END(hashtable);
}
/*
* Callback for a print hooked.
*/
@@ -840,7 +967,7 @@ trigger_callback_print_cb (const void *pointer, void *data,
goto end;
/* execute the trigger (conditions, regex, command) */
trigger_callback_execute (trigger, buffer, pointers, extra_vars);
trigger_callback_execute (trigger, buffer, pointers, extra_vars, NULL);
end:
TRIGGER_CALLBACK_CB_END(trigger_rc);
@@ -874,7 +1001,7 @@ trigger_callback_command_cb (const void *pointer, void *data,
}
/* execute the trigger (conditions, regex, command) */
trigger_callback_execute (trigger, buffer, pointers, extra_vars);
trigger_callback_execute (trigger, buffer, pointers, extra_vars, NULL);
end:
TRIGGER_CALLBACK_CB_END(trigger_rc);
@@ -899,7 +1026,7 @@ trigger_callback_command_run_cb (const void *pointer, void *data,
weechat_hashtable_set (extra_vars, "tg_command", command);
/* execute the trigger (conditions, regex, command) */
trigger_callback_execute (trigger, buffer, pointers, extra_vars);
trigger_callback_execute (trigger, buffer, pointers, extra_vars, NULL);
end:
TRIGGER_CALLBACK_CB_END(trigger_rc);
@@ -949,7 +1076,7 @@ trigger_callback_timer_cb (const void *pointer, void *data,
}
/* execute the trigger (conditions, regex, command) */
trigger_callback_execute (trigger, NULL, pointers, extra_vars);
trigger_callback_execute (trigger, NULL, pointers, extra_vars, NULL);
end:
TRIGGER_CALLBACK_CB_END(trigger_rc);
@@ -972,7 +1099,7 @@ trigger_callback_config_cb (const void *pointer, void *data,
weechat_hashtable_set (extra_vars, "tg_value", value);
/* execute the trigger (conditions, regex, command) */
trigger_callback_execute (trigger, NULL, pointers, extra_vars);
trigger_callback_execute (trigger, NULL, pointers, extra_vars, NULL);
end:
TRIGGER_CALLBACK_CB_END(trigger_rc);
@@ -1011,7 +1138,7 @@ trigger_callback_focus_cb (const void *pointer, void *data,
}
/* execute the trigger (conditions, regex, command) */
trigger_callback_execute (trigger, NULL, pointers, info);
trigger_callback_execute (trigger, NULL, pointers, info, NULL);
end:
TRIGGER_CALLBACK_CB_END(info);
+12
View File
@@ -25,10 +25,13 @@
#define TRIGGER_CALLBACK_CB_INIT(__rc) \
struct t_trigger *trigger; \
struct t_hashtable *pointers, *extra_vars; \
struct t_weelist *vars_updated; \
int trigger_rc; \
pointers = NULL; \
extra_vars = NULL; \
vars_updated = NULL; \
(void) data; \
(void) vars_updated; \
(void) trigger_rc; \
if (!trigger_enabled) \
return __rc; \
@@ -59,11 +62,18 @@
if (!extra_vars) \
goto end;
#define TRIGGER_CALLBACK_CB_NEW_VARS_UPDATED \
vars_updated = weechat_list_new (); \
if (!vars_updated) \
goto end;
#define TRIGGER_CALLBACK_CB_END(__rc) \
if (pointers) \
weechat_hashtable_free (pointers); \
if (extra_vars) \
weechat_hashtable_free (extra_vars); \
if (vars_updated) \
weechat_list_free (vars_updated); \
trigger->hook_running = 0; \
switch (weechat_config_integer ( \
trigger->options[TRIGGER_OPTION_POST_ACTION])) \
@@ -93,6 +103,8 @@ extern char *trigger_callback_modifier_cb (const void *pointer, void *data,
const char *modifier,
const char *modifier_data,
const char *string);
extern struct t_hashtable *trigger_callback_line_cb (const void *pointer, void *data,
struct t_hashtable *line);
extern int trigger_callback_print_cb (const void *pointer, void *data,
struct t_gui_buffer *buffer,
time_t date, int tags_count,
+4 -3
View File
@@ -1153,13 +1153,14 @@ trigger_command_init ()
" 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"
" hook: signal, hsignal, modifier, line, 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"
" line: list of buffer masks, tags\n"
" print: buffer, tags, message, strip colors\n"
" command: command (required), description, arguments, "
"description of arguments, completion\n"
@@ -1212,7 +1213,7 @@ trigger_command_init ()
" 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 and focus)\n"
" 4. exit with a return code (except for modifier, line and focus)\n"
" 5. perform post action\n"
"\n"
"Examples (you can also look at default triggers with /trigger "
+30 -4
View File
@@ -49,10 +49,11 @@ char *trigger_option_default[TRIGGER_NUM_OPTIONS] =
{ "on", "signal", "", "", "", "", "ok", "none" };
char *trigger_hook_type_string[TRIGGER_NUM_HOOK_TYPES] =
{ "signal", "hsignal", "modifier", "print", "command", "command_run", "timer",
"config", "focus" };
{ "signal", "hsignal", "modifier", "line", "print", "command", "command_run",
"timer", "config", "focus" };
char *trigger_hook_option_values =
"signal|hsignal|modifier|print|command|command_run|timer|config|focus";
"signal|hsignal|modifier|line|print|command|command_run|timer|config|"
"focus";
char *trigger_hook_default_arguments[TRIGGER_NUM_HOOK_TYPES] =
{ "xxx", "xxx", "xxx", "", "cmd;desc;args;args_desc;%(buffers_names)", "/cmd",
"60000;0;0", "xxx", "chat" };
@@ -262,7 +263,8 @@ trigger_unhook (struct t_trigger *trigger)
void
trigger_hook (struct t_trigger *trigger)
{
char **argv, **argv_eol, *tags, *message, *error1, *error2, *error3;
char **argv, **argv_eol, *buffer_type, *buffer_name, *tags, *message;
char *error1, *error2, *error3;
int i, argc, strip_colors;
long interval, align_second, max_calls;
@@ -329,6 +331,30 @@ trigger_hook (struct t_trigger *trigger)
}
}
break;
case TRIGGER_HOOK_LINE:
buffer_type = NULL;
buffer_name = NULL;
tags = NULL;
if (argv && (argc >= 1))
{
buffer_type = argv[0];
if ((argc >= 2) && (strcmp (argv[1], "*") != 0))
buffer_name = argv[1];
if ((argc >= 3) && (strcmp (argv[2], "*") != 0))
tags = argv[2];
}
trigger->hooks = malloc (sizeof (trigger->hooks[0]));
if (trigger->hooks)
{
trigger->hooks_count = 1;
trigger->hooks[0] = weechat_hook_line (
buffer_type,
buffer_name,
tags,
&trigger_callback_line_cb,
trigger, NULL);
}
break;
case TRIGGER_HOOK_PRINT:
tags = NULL;
message = NULL;
+1
View File
@@ -48,6 +48,7 @@ enum t_trigger_hook_type
TRIGGER_HOOK_SIGNAL = 0,
TRIGGER_HOOK_HSIGNAL,
TRIGGER_HOOK_MODIFIER,
TRIGGER_HOOK_LINE,
TRIGGER_HOOK_PRINT,
TRIGGER_HOOK_COMMAND,
TRIGGER_HOOK_COMMAND_RUN,