mirror of
https://github.com/weechat/weechat.git
synced 2026-07-01 23:36:37 +02:00
api: allow to catch multiple signals in functions hook_signal and hook_hsignal (closes #1780)
This commit is contained in:
@@ -43,7 +43,8 @@
|
||||
char *
|
||||
hook_hsignal_get_description (struct t_hook *hook)
|
||||
{
|
||||
return strdup (HOOK_HSIGNAL(hook, signal));
|
||||
return string_build_with_split_string (
|
||||
(const char **)(HOOK_HSIGNAL(hook, signals)), ";");
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -82,13 +83,43 @@ hook_hsignal (struct t_weechat_plugin *plugin, const char *signal,
|
||||
|
||||
new_hook->hook_data = new_hook_hsignal;
|
||||
new_hook_hsignal->callback = callback;
|
||||
new_hook_hsignal->signal = strdup ((ptr_signal) ? ptr_signal : signal);
|
||||
new_hook_hsignal->signals = string_split (
|
||||
(ptr_signal) ? ptr_signal : signal,
|
||||
";",
|
||||
NULL,
|
||||
WEECHAT_STRING_SPLIT_STRIP_LEFT
|
||||
| WEECHAT_STRING_SPLIT_STRIP_RIGHT
|
||||
| WEECHAT_STRING_SPLIT_COLLAPSE_SEPS,
|
||||
0, &new_hook_hsignal->num_signals);
|
||||
|
||||
hook_add_to_list (new_hook);
|
||||
|
||||
return new_hook;
|
||||
}
|
||||
|
||||
/*
|
||||
* Checks if a hooked hsignal matches a signal sent: it matches if at least
|
||||
* one of the signal masks are matching the signal sent.
|
||||
*
|
||||
* Returns:
|
||||
* 1: hook matches signal sent
|
||||
* 0: hook does not match signal sent
|
||||
*/
|
||||
|
||||
int
|
||||
hook_hsignal_match (const char *signal, struct t_hook *hook)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < HOOK_HSIGNAL(hook, num_signals); i++)
|
||||
{
|
||||
if (string_match (signal, HOOK_HSIGNAL(hook, signals)[i], 0))
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Sends a hsignal (signal with hashtable).
|
||||
*/
|
||||
@@ -110,7 +141,7 @@ hook_hsignal_send (const char *signal, struct t_hashtable *hashtable)
|
||||
|
||||
if (!ptr_hook->deleted
|
||||
&& !ptr_hook->running
|
||||
&& (string_match (signal, HOOK_HSIGNAL(ptr_hook, signal), 0)))
|
||||
&& (hook_hsignal_match (signal, ptr_hook)))
|
||||
{
|
||||
ptr_hook->running = 1;
|
||||
rc = (HOOK_HSIGNAL(ptr_hook, callback))
|
||||
@@ -142,11 +173,12 @@ hook_hsignal_free_data (struct t_hook *hook)
|
||||
if (!hook || !hook->hook_data)
|
||||
return;
|
||||
|
||||
if (HOOK_HSIGNAL(hook, signal))
|
||||
if (HOOK_HSIGNAL(hook, signals))
|
||||
{
|
||||
free (HOOK_HSIGNAL(hook, signal));
|
||||
HOOK_HSIGNAL(hook, signal) = NULL;
|
||||
string_free_split (HOOK_HSIGNAL(hook, signals));
|
||||
HOOK_HSIGNAL(hook, signals) = NULL;
|
||||
}
|
||||
HOOK_HSIGNAL(hook, num_signals) = 0;
|
||||
|
||||
free (hook->hook_data);
|
||||
hook->hook_data = NULL;
|
||||
@@ -164,12 +196,24 @@ int
|
||||
hook_hsignal_add_to_infolist (struct t_infolist_item *item,
|
||||
struct t_hook *hook)
|
||||
{
|
||||
int i;
|
||||
char option_name[64];
|
||||
|
||||
if (!item || !hook || !hook->hook_data)
|
||||
return 0;
|
||||
|
||||
if (!infolist_new_var_pointer (item, "callback", HOOK_HSIGNAL(hook, callback)))
|
||||
return 0;
|
||||
if (!infolist_new_var_string (item, "signal", HOOK_HSIGNAL(hook, signal)))
|
||||
i = 0;
|
||||
for (i = 0; i < HOOK_HSIGNAL(hook, num_signals); i++)
|
||||
{
|
||||
snprintf (option_name, sizeof (option_name), "signal_%05d", i);
|
||||
if (!infolist_new_var_string (item, option_name,
|
||||
HOOK_HSIGNAL(hook, signals)[i]))
|
||||
return 0;
|
||||
}
|
||||
if (!infolist_new_var_integer (item, "num_signals",
|
||||
HOOK_HSIGNAL(hook, num_signals)))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
@@ -182,10 +226,16 @@ hook_hsignal_add_to_infolist (struct t_infolist_item *item,
|
||||
void
|
||||
hook_hsignal_print_log (struct t_hook *hook)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!hook || !hook->hook_data)
|
||||
return;
|
||||
|
||||
log_printf (" signal data:");
|
||||
log_printf (" callback. . . . . . . : 0x%lx", HOOK_HSIGNAL(hook, callback));
|
||||
log_printf (" signal. . . . . . . . : '%s'", HOOK_HSIGNAL(hook, signal));
|
||||
log_printf (" signals:");
|
||||
for (i = 0; i < HOOK_HSIGNAL(hook, num_signals); i++)
|
||||
{
|
||||
log_printf (" '%s'", HOOK_HSIGNAL(hook, signals)[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,8 +32,10 @@ typedef int (t_hook_callback_hsignal)(const void *pointer, void *data,
|
||||
struct t_hook_hsignal
|
||||
{
|
||||
t_hook_callback_hsignal *callback; /* signal callback */
|
||||
char *signal; /* signal selected (may begin or end */
|
||||
/* with "*", "*" == any signal) */
|
||||
char **signals; /* signals selected; each one may */
|
||||
/* begin or end with "*", */
|
||||
/* "*" == any signal */
|
||||
int num_signals; /* number of signals */
|
||||
};
|
||||
|
||||
extern char *hook_hsignal_get_description (struct t_hook *hook);
|
||||
|
||||
@@ -43,7 +43,8 @@
|
||||
char *
|
||||
hook_signal_get_description (struct t_hook *hook)
|
||||
{
|
||||
return strdup (HOOK_SIGNAL(hook, signal));
|
||||
return string_build_with_split_string (
|
||||
(const char **)(HOOK_SIGNAL(hook, signals)), ";");
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -82,13 +83,43 @@ hook_signal (struct t_weechat_plugin *plugin, const char *signal,
|
||||
|
||||
new_hook->hook_data = new_hook_signal;
|
||||
new_hook_signal->callback = callback;
|
||||
new_hook_signal->signal = strdup ((ptr_signal) ? ptr_signal : signal);
|
||||
new_hook_signal->signals = string_split (
|
||||
(ptr_signal) ? ptr_signal : signal,
|
||||
";",
|
||||
NULL,
|
||||
WEECHAT_STRING_SPLIT_STRIP_LEFT
|
||||
| WEECHAT_STRING_SPLIT_STRIP_RIGHT
|
||||
| WEECHAT_STRING_SPLIT_COLLAPSE_SEPS,
|
||||
0, &new_hook_signal->num_signals);
|
||||
|
||||
hook_add_to_list (new_hook);
|
||||
|
||||
return new_hook;
|
||||
}
|
||||
|
||||
/*
|
||||
* Checks if a hooked signal matches a signal sent: it matches if at least
|
||||
* one of the signal masks are matching the signal sent.
|
||||
*
|
||||
* Returns:
|
||||
* 1: hook matches signal sent
|
||||
* 0: hook does not match signal sent
|
||||
*/
|
||||
|
||||
int
|
||||
hook_signal_match (const char *signal, struct t_hook *hook)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < HOOK_SIGNAL(hook, num_signals); i++)
|
||||
{
|
||||
if (string_match (signal, HOOK_SIGNAL(hook, signals)[i], 0))
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Sends a signal.
|
||||
*/
|
||||
@@ -110,7 +141,7 @@ hook_signal_send (const char *signal, const char *type_data, void *signal_data)
|
||||
|
||||
if (!ptr_hook->deleted
|
||||
&& !ptr_hook->running
|
||||
&& (string_match (signal, HOOK_SIGNAL(ptr_hook, signal), 0)))
|
||||
&& hook_signal_match (signal, ptr_hook))
|
||||
{
|
||||
ptr_hook->running = 1;
|
||||
rc = (HOOK_SIGNAL(ptr_hook, callback))
|
||||
@@ -143,11 +174,12 @@ hook_signal_free_data (struct t_hook *hook)
|
||||
if (!hook || !hook->hook_data)
|
||||
return;
|
||||
|
||||
if (HOOK_SIGNAL(hook, signal))
|
||||
if (HOOK_SIGNAL(hook, signals))
|
||||
{
|
||||
free (HOOK_SIGNAL(hook, signal));
|
||||
HOOK_SIGNAL(hook, signal) = NULL;
|
||||
string_free_split (HOOK_SIGNAL(hook, signals));
|
||||
HOOK_SIGNAL(hook, signals) = NULL;
|
||||
}
|
||||
HOOK_SIGNAL(hook, num_signals) = 0;
|
||||
|
||||
free (hook->hook_data);
|
||||
hook->hook_data = NULL;
|
||||
@@ -165,12 +197,24 @@ int
|
||||
hook_signal_add_to_infolist (struct t_infolist_item *item,
|
||||
struct t_hook *hook)
|
||||
{
|
||||
int i;
|
||||
char option_name[64];
|
||||
|
||||
if (!item || !hook || !hook->hook_data)
|
||||
return 0;
|
||||
|
||||
if (!infolist_new_var_pointer (item, "callback", HOOK_SIGNAL(hook, callback)))
|
||||
return 0;
|
||||
if (!infolist_new_var_string (item, "signal", HOOK_SIGNAL(hook, signal)))
|
||||
i = 0;
|
||||
for (i = 0; i < HOOK_SIGNAL(hook, num_signals); i++)
|
||||
{
|
||||
snprintf (option_name, sizeof (option_name), "signal_%05d", i);
|
||||
if (!infolist_new_var_string (item, option_name,
|
||||
HOOK_SIGNAL(hook, signals)[i]))
|
||||
return 0;
|
||||
}
|
||||
if (!infolist_new_var_integer (item, "num_signals",
|
||||
HOOK_SIGNAL(hook, num_signals)))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
@@ -183,10 +227,16 @@ hook_signal_add_to_infolist (struct t_infolist_item *item,
|
||||
void
|
||||
hook_signal_print_log (struct t_hook *hook)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!hook || !hook->hook_data)
|
||||
return;
|
||||
|
||||
log_printf (" signal data:");
|
||||
log_printf (" callback. . . . . . . : 0x%lx", HOOK_SIGNAL(hook, callback));
|
||||
log_printf (" signal. . . . . . . . : '%s'", HOOK_SIGNAL(hook, signal));
|
||||
log_printf (" signals:");
|
||||
for (i = 0; i < HOOK_SIGNAL(hook, num_signals); i++)
|
||||
{
|
||||
log_printf (" '%s'", HOOK_SIGNAL(hook, signals)[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,8 +32,10 @@ typedef int (t_hook_callback_signal)(const void *pointer, void *data,
|
||||
struct t_hook_signal
|
||||
{
|
||||
t_hook_callback_signal *callback; /* signal callback */
|
||||
char *signal; /* signal selected (may begin or end */
|
||||
/* with "*", "*" == any signal) */
|
||||
char **signals; /* signals selected; each one may */
|
||||
/* begin or end with "*", */
|
||||
/* "*" == any signal */
|
||||
int num_signals; /* number of signals */
|
||||
};
|
||||
|
||||
extern char *hook_signal_get_description (struct t_hook *hook);
|
||||
|
||||
Reference in New Issue
Block a user