mirror of
https://github.com/weechat/weechat.git
synced 2026-07-08 18:53:12 +02:00
Add hook type "command_run", add new function "string_remove_color" in plugin API (task #9089)
This commit is contained in:
@@ -2090,6 +2090,21 @@ command_plugin_list (const char *name, int full)
|
||||
}
|
||||
}
|
||||
|
||||
/* command_run hooked */
|
||||
hook_found = 0;
|
||||
for (ptr_hook = weechat_hooks[HOOK_TYPE_COMMAND_RUN]; ptr_hook;
|
||||
ptr_hook = ptr_hook->next_hook)
|
||||
{
|
||||
if (!ptr_hook->deleted && (ptr_hook->plugin == ptr_plugin))
|
||||
{
|
||||
if (!hook_found)
|
||||
gui_chat_printf (NULL, _(" command_run hooked:"));
|
||||
hook_found = 1;
|
||||
gui_chat_printf (NULL, " %s",
|
||||
HOOK_COMMAND_RUN(ptr_hook, command));
|
||||
}
|
||||
}
|
||||
|
||||
/* timers hooked */
|
||||
hook_found = 0;
|
||||
for (ptr_hook = weechat_hooks[HOOK_TYPE_TIMER]; ptr_hook;
|
||||
|
||||
+99
-5
@@ -47,8 +47,8 @@
|
||||
|
||||
|
||||
char *hook_type_string[HOOK_NUM_TYPES] =
|
||||
{ "command", "timer", "fd", "connect", "print", "signal", "config",
|
||||
"completion", "modifier", "info", "infolist" };
|
||||
{ "command", "command_run", "timer", "fd", "connect", "print", "signal",
|
||||
"config", "completion", "modifier", "info", "infolist" };
|
||||
struct t_hook *weechat_hooks[HOOK_NUM_TYPES]; /* list of hooks */
|
||||
struct t_hook *last_weechat_hook[HOOK_NUM_TYPES]; /* last hook */
|
||||
int hook_exec_recursion = 0; /* 1 when a hook is executed */
|
||||
@@ -328,7 +328,7 @@ hook_command (struct t_weechat_plugin *plugin, const char *command,
|
||||
const char *completion,
|
||||
t_hook_callback_command *callback, void *callback_data)
|
||||
{
|
||||
struct t_hook *ptr_hook,*new_hook;
|
||||
struct t_hook *ptr_hook, *new_hook;
|
||||
struct t_hook_command *new_hook_command;
|
||||
|
||||
if ((string_strcasecmp (command, "builtin") == 0)
|
||||
@@ -399,11 +399,15 @@ hook_command_exec (struct t_gui_buffer *buffer, int any_plugin,
|
||||
char **argv, **argv_eol;
|
||||
int argc, rc, command_is_running;
|
||||
|
||||
rc = -1;
|
||||
|
||||
if (!buffer || !string || !string[0])
|
||||
return -1;
|
||||
|
||||
rc = hook_command_run_exec (buffer, string);
|
||||
if (rc == WEECHAT_RC_OK_EAT)
|
||||
return 1;
|
||||
|
||||
rc = -1;
|
||||
|
||||
argv = string_explode (string, " ", 0, 0, &argc);
|
||||
if (argc == 0)
|
||||
{
|
||||
@@ -493,6 +497,74 @@ hook_command_exec (struct t_gui_buffer *buffer, int any_plugin,
|
||||
return rc;
|
||||
}
|
||||
|
||||
/*
|
||||
* hook_command_run: hook a command when it's run by WeeChat
|
||||
*/
|
||||
|
||||
struct t_hook *
|
||||
hook_command_run (struct t_weechat_plugin *plugin, const char *command,
|
||||
t_hook_callback_command_run *callback, void *callback_data)
|
||||
{
|
||||
struct t_hook *new_hook;
|
||||
struct t_hook_command_run *new_hook_command_run;
|
||||
|
||||
new_hook = malloc (sizeof (*new_hook));
|
||||
if (!new_hook)
|
||||
return NULL;
|
||||
new_hook_command_run = malloc (sizeof (*new_hook_command_run));
|
||||
if (!new_hook_command_run)
|
||||
{
|
||||
free (new_hook);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
hook_init_data (new_hook, plugin, HOOK_TYPE_COMMAND_RUN, callback_data);
|
||||
|
||||
new_hook->hook_data = new_hook_command_run;
|
||||
new_hook_command_run->callback = callback;
|
||||
new_hook_command_run->command = (command) ?
|
||||
strdup (command) : strdup ("");
|
||||
|
||||
hook_add_to_list (new_hook);
|
||||
|
||||
return new_hook;
|
||||
}
|
||||
|
||||
/*
|
||||
* hook_command_run_exec: execute command_run hook
|
||||
*/
|
||||
|
||||
int
|
||||
hook_command_run_exec (struct t_gui_buffer *buffer, const char *command)
|
||||
{
|
||||
struct t_hook *ptr_hook, *next_hook;
|
||||
int rc;
|
||||
|
||||
ptr_hook = weechat_hooks[HOOK_TYPE_COMMAND_RUN];
|
||||
while (ptr_hook)
|
||||
{
|
||||
next_hook = ptr_hook->next_hook;
|
||||
|
||||
if (!ptr_hook->deleted
|
||||
&& !ptr_hook->running
|
||||
&& HOOK_COMMAND_RUN(ptr_hook, command)
|
||||
&& (string_match (command, HOOK_COMMAND_RUN(ptr_hook, command), 0)))
|
||||
{
|
||||
ptr_hook->running = 1;
|
||||
rc = (HOOK_COMMAND_RUN(ptr_hook, callback)) (ptr_hook->callback_data,
|
||||
buffer,
|
||||
command);
|
||||
ptr_hook->running = 0;
|
||||
if (rc == WEECHAT_RC_OK_EAT)
|
||||
return rc;
|
||||
}
|
||||
|
||||
ptr_hook = next_hook;
|
||||
}
|
||||
|
||||
return WEECHAT_RC_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* hook_timer_init: init a timer hook
|
||||
*/
|
||||
@@ -1630,6 +1702,11 @@ unhook (struct t_hook *hook)
|
||||
free (HOOK_COMMAND(hook, completion));
|
||||
free ((struct t_hook_command *)hook->hook_data);
|
||||
break;
|
||||
case HOOK_TYPE_COMMAND_RUN:
|
||||
if (HOOK_COMMAND_RUN(hook, command))
|
||||
free (HOOK_COMMAND_RUN(hook, command));
|
||||
free ((struct t_hook_command *)hook->hook_data);
|
||||
break;
|
||||
case HOOK_TYPE_TIMER:
|
||||
free ((struct t_hook_timer *)hook->hook_data);
|
||||
break;
|
||||
@@ -1834,6 +1911,15 @@ hook_add_to_infolist_type (struct t_infolist *infolist,
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
case HOOK_TYPE_COMMAND_RUN:
|
||||
if (!ptr_hook->deleted)
|
||||
{
|
||||
if (!infolist_new_var_pointer (ptr_item, "callback", HOOK_COMMAND_RUN(ptr_hook, callback)))
|
||||
return 0;
|
||||
if (!infolist_new_var_string (ptr_item, "command", HOOK_COMMAND_RUN(ptr_hook, command)))
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
case HOOK_TYPE_TIMER:
|
||||
if (!ptr_hook->deleted)
|
||||
{
|
||||
@@ -2058,6 +2144,14 @@ hook_print_log ()
|
||||
log_printf (" completion . . . . . : '%s'", HOOK_COMMAND(ptr_hook, completion));
|
||||
}
|
||||
break;
|
||||
case HOOK_TYPE_COMMAND_RUN:
|
||||
if (!ptr_hook->deleted)
|
||||
{
|
||||
log_printf (" command_run data:");
|
||||
log_printf (" callback . . . . . . : 0x%lx", HOOK_COMMAND_RUN(ptr_hook, callback));
|
||||
log_printf (" command. . . . . . . : '%s'", HOOK_COMMAND_RUN(ptr_hook, command));
|
||||
}
|
||||
break;
|
||||
case HOOK_TYPE_TIMER:
|
||||
if (!ptr_hook->deleted)
|
||||
{
|
||||
|
||||
+26
-4
@@ -35,6 +35,7 @@ struct t_infolist;
|
||||
enum t_hook_type
|
||||
{
|
||||
HOOK_TYPE_COMMAND = 0, /* new command */
|
||||
HOOK_TYPE_COMMAND_RUN, /* when a command is executed */
|
||||
HOOK_TYPE_TIMER, /* timer */
|
||||
HOOK_TYPE_FD, /* socket of file descriptor */
|
||||
HOOK_TYPE_CONNECT, /* connect to peer with fork */
|
||||
@@ -59,6 +60,7 @@ enum t_hook_type
|
||||
|
||||
/* macros to access hook specific data */
|
||||
#define HOOK_COMMAND(hook, var) (((struct t_hook_command *)hook->hook_data)->var)
|
||||
#define HOOK_COMMAND_RUN(hook, var) (((struct t_hook_command_run *)hook->hook_data)->var)
|
||||
#define HOOK_TIMER(hook, var) (((struct t_hook_timer *)hook->hook_data)->var)
|
||||
#define HOOK_FD(hook, var) (((struct t_hook_fd *)hook->hook_data)->var)
|
||||
#define HOOK_CONNECT(hook, var) (((struct t_hook_connect *)hook->hook_data)->var)
|
||||
@@ -101,6 +103,16 @@ struct t_hook_command
|
||||
char *completion; /* template for completion */
|
||||
};
|
||||
|
||||
typedef int (t_hook_callback_command_run)(void *data,
|
||||
struct t_gui_buffer *buffer,
|
||||
const char *command);
|
||||
|
||||
struct t_hook_command_run
|
||||
{
|
||||
t_hook_callback_command_run *callback; /* command_run callback */
|
||||
char *command; /* name of command (without '/') */
|
||||
};
|
||||
|
||||
typedef int (t_hook_callback_timer)(void *data);
|
||||
|
||||
struct t_hook_timer
|
||||
@@ -232,13 +244,22 @@ extern struct t_hook *last_weechat_hook[];
|
||||
|
||||
extern void hook_init ();
|
||||
extern struct t_hook *hook_command (struct t_weechat_plugin *plugin,
|
||||
const char *command, const char *description,
|
||||
const char *args, const char *args_description,
|
||||
const char *command,
|
||||
const char *description,
|
||||
const char *args,
|
||||
const char *args_description,
|
||||
const char *completion,
|
||||
t_hook_callback_command *callback,
|
||||
void *callback_data);
|
||||
extern int hook_command_exec (struct t_gui_buffer *buffer, int any_plugin,
|
||||
struct t_weechat_plugin *plugin, const char *string);
|
||||
struct t_weechat_plugin *plugin,
|
||||
const char *string);
|
||||
extern struct t_hook *hook_command_run (struct t_weechat_plugin *plugin,
|
||||
const char *command,
|
||||
t_hook_callback_command_run *callback,
|
||||
void *callback_data);
|
||||
extern int hook_command_run_exec (struct t_gui_buffer *buffer,
|
||||
const char *command);
|
||||
extern struct t_hook *hook_timer (struct t_weechat_plugin *plugin,
|
||||
long interval, int align_second,
|
||||
int max_calls,
|
||||
@@ -276,7 +297,8 @@ extern struct t_hook *hook_signal (struct t_weechat_plugin *plugin,
|
||||
void *callback_data);
|
||||
extern void hook_signal_send (const char *signal, const char *type_data,
|
||||
void *signal_data);
|
||||
extern struct t_hook *hook_config (struct t_weechat_plugin *, const char *option,
|
||||
extern struct t_hook *hook_config (struct t_weechat_plugin *plugin,
|
||||
const char *option,
|
||||
t_hook_callback_config *callback,
|
||||
void *callback_data);
|
||||
extern void hook_config_exec (const char *option, const char *value);
|
||||
|
||||
@@ -51,6 +51,7 @@
|
||||
#include "weechat.h"
|
||||
#include "wee-string.h"
|
||||
#include "wee-utf8.h"
|
||||
#include "../gui/gui-color.h"
|
||||
|
||||
|
||||
/*
|
||||
@@ -1280,3 +1281,13 @@ string_format_size (unsigned long size)
|
||||
|
||||
return strdup (str_size);
|
||||
}
|
||||
|
||||
/*
|
||||
* string_remove_color: remove WeeChat color codes in string
|
||||
*/
|
||||
|
||||
char *
|
||||
string_remove_color (const char *string)
|
||||
{
|
||||
return (char *)gui_color_decode ((unsigned char *)string);
|
||||
}
|
||||
|
||||
@@ -56,5 +56,6 @@ extern char *string_iconv_from_internal (const char *charset,
|
||||
const char *string);
|
||||
extern void string_iconv_fprintf (FILE *file, const char *data, ...);
|
||||
extern char *string_format_size (unsigned long size);
|
||||
extern char *string_remove_color (const char *string);
|
||||
|
||||
#endif /* wee-string.h */
|
||||
|
||||
Reference in New Issue
Block a user