diff options
author | Sebastien Helleu <flashcode@flashtux.org> | 2007-11-11 13:34:43 +0100 |
---|---|---|
committer | Sebastien Helleu <flashcode@flashtux.org> | 2007-11-11 13:34:43 +0100 |
commit | 4478777876a5b285c78a3daef02fd970eb2d5597 (patch) | |
tree | 7ef1da66efcc7b37ab7ea0cfc113e64c3f64c55d | |
parent | 60428b0d2ea2f42d056a13f3380844a6032f5e81 (diff) | |
download | weechat-4478777876a5b285c78a3daef02fd970eb2d5597.zip |
Event hook added
-rw-r--r-- | src/core/wee-command.c | 129 | ||||
-rw-r--r-- | src/core/wee-hook.c | 395 | ||||
-rw-r--r-- | src/core/wee-hook.h | 75 | ||||
-rw-r--r-- | src/gui/gui-buffer.c | 33 | ||||
-rw-r--r-- | src/plugins/demo/demo.c | 66 | ||||
-rw-r--r-- | src/plugins/fifo/fifo.c | 6 | ||||
-rw-r--r-- | src/plugins/plugin-api.c | 111 | ||||
-rw-r--r-- | src/plugins/plugin-api.h | 19 | ||||
-rw-r--r-- | src/plugins/plugin.c | 15 | ||||
-rw-r--r-- | src/plugins/weechat-plugin.h | 35 |
10 files changed, 555 insertions, 329 deletions
diff --git a/src/core/wee-command.c b/src/core/wee-command.c index 2b68be93a..41b3bfeb1 100644 --- a/src/core/wee-command.c +++ b/src/core/wee-command.c @@ -41,7 +41,9 @@ #include "wee-list.h" #include "../gui/gui-chat.h" #include "../gui/gui-history.h" +#include "../gui/gui-input.h" #include "../gui/gui-keyboard.h" +#include "../gui/gui-status.h" #include "../gui/gui-window.h" #include "../plugins/plugin.h" #include "../plugins/plugin-config.h" @@ -520,6 +522,20 @@ command_buffer (struct t_gui_buffer *buffer, return -1; } } + else if (string_strcasecmp (argv[0], "close") == 0) + { + if (!buffer->plugin) + { + gui_chat_printf (NULL, + _("%sError: WeeChat main buffer can't be " + "closed"), + gui_chat_prefix[GUI_CHAT_PREFIX_ERROR]); + return -1; + } + gui_buffer_free (buffer, 1); + gui_status_draw (gui_current_window->buffer, 1); + gui_input_draw (gui_current_window->buffer, 1); + } else if (string_strcasecmp (argv[0], "notify") == 0) { if (argc < 2) @@ -1276,54 +1292,6 @@ command_plugin_list (char *name, int full) } } - /* prints hooked */ - hook_found = 0; - for (ptr_hook = weechat_hooks; ptr_hook; - ptr_hook = ptr_hook->next_hook) - { - if ((ptr_hook->plugin == ptr_plugin) - && (ptr_hook->type == HOOK_TYPE_PRINT)) - { - if (!hook_found) - gui_chat_printf (NULL, _(" prints hooked:")); - hook_found = 1; - if (HOOK_PRINT(ptr_hook, buffer)) - gui_chat_printf (NULL, - _(" buffer: %s / %s, message: \"%s\""), - HOOK_PRINT(ptr_hook, buffer)->category, - HOOK_PRINT(ptr_hook, buffer)->name, - HOOK_PRINT(ptr_hook, message) ? - HOOK_PRINT(ptr_hook, message) : _("(none)")); - else - gui_chat_printf (NULL, - _(" message: \"%s\""), - HOOK_PRINT(ptr_hook, message) ? - HOOK_PRINT(ptr_hook, message) : _("(none)")); - } - } - - /* config options hooked */ - hook_found = 0; - for (ptr_hook = weechat_hooks; ptr_hook; - ptr_hook = ptr_hook->next_hook) - { - if ((ptr_hook->plugin == ptr_plugin) - && (ptr_hook->type == HOOK_TYPE_CONFIG)) - { - if (!hook_found) - gui_chat_printf (NULL, - _(" configuration otions " - "hooked:")); - hook_found = 1; - gui_chat_printf (NULL, - " (%s) %s", - HOOK_CONFIG(ptr_hook, type) ? - HOOK_CONFIG(ptr_hook, type) : "*", - HOOK_CONFIG(ptr_hook, option) ? - HOOK_CONFIG(ptr_hook, option) : "*"); - } - } - /* timers hooked */ hook_found = 0; for (ptr_hook = weechat_hooks; ptr_hook; @@ -1385,18 +1353,71 @@ command_plugin_list (char *name, int full) } } - /* keyboards hooked */ + /* prints hooked */ + hook_found = 0; + for (ptr_hook = weechat_hooks; ptr_hook; + ptr_hook = ptr_hook->next_hook) + { + if ((ptr_hook->plugin == ptr_plugin) + && (ptr_hook->type == HOOK_TYPE_PRINT)) + { + if (!hook_found) + gui_chat_printf (NULL, _(" prints hooked:")); + hook_found = 1; + if (HOOK_PRINT(ptr_hook, buffer)) + gui_chat_printf (NULL, + _(" buffer: %s / %s, message: \"%s\""), + HOOK_PRINT(ptr_hook, buffer)->category, + HOOK_PRINT(ptr_hook, buffer)->name, + HOOK_PRINT(ptr_hook, message) ? + HOOK_PRINT(ptr_hook, message) : _("(none)")); + else + gui_chat_printf (NULL, + _(" message: \"%s\""), + HOOK_PRINT(ptr_hook, message) ? + HOOK_PRINT(ptr_hook, message) : _("(none)")); + } + } + + /* events hooked */ hook_found = 0; for (ptr_hook = weechat_hooks; ptr_hook; ptr_hook = ptr_hook->next_hook) { if ((ptr_hook->plugin == ptr_plugin) - && (ptr_hook->type == HOOK_TYPE_KEYBOARD)) - hook_found++; + && (ptr_hook->type == HOOK_TYPE_EVENT)) + { + if (!hook_found) + gui_chat_printf (NULL, _(" events hooked:")); + hook_found = 1; + gui_chat_printf (NULL, + _(" event: %s"), + HOOK_EVENT(ptr_hook, event) ? + HOOK_EVENT(ptr_hook, event) : _("(all)")); + } + } + + /* config options hooked */ + hook_found = 0; + for (ptr_hook = weechat_hooks; ptr_hook; + ptr_hook = ptr_hook->next_hook) + { + if ((ptr_hook->plugin == ptr_plugin) + && (ptr_hook->type == HOOK_TYPE_CONFIG)) + { + if (!hook_found) + gui_chat_printf (NULL, + _(" configuration otions " + "hooked:")); + hook_found = 1; + gui_chat_printf (NULL, + " (%s) %s", + HOOK_CONFIG(ptr_hook, type) ? + HOOK_CONFIG(ptr_hook, type) : "*", + HOOK_CONFIG(ptr_hook, option) ? + HOOK_CONFIG(ptr_hook, option) : "*"); + } } - if (hook_found) - gui_chat_printf (NULL, _(" %d keyboards hooked"), - hook_found); } } } diff --git a/src/core/wee-hook.c b/src/core/wee-hook.c index 6f7763cba..e8452b6dc 100644 --- a/src/core/wee-hook.c +++ b/src/core/wee-hook.c @@ -34,6 +34,7 @@ #include "wee-log.h" #include "wee-string.h" #include "wee-util.h" +#include "../gui/gui-color.h" #include "../plugins/plugin.h" @@ -222,140 +223,6 @@ hook_command_exec (void *plugin, char *string) } /* - * hook_print: hook a message printed by WeeChat - */ - -struct t_hook * -hook_print (void *plugin, void *buffer, char *message, - t_hook_callback_print *callback, void *callback_data) -{ - struct t_hook *new_hook; - struct t_hook_print *new_hook_print; - - new_hook = (struct t_hook *)malloc (sizeof (struct t_hook)); - if (!new_hook) - return NULL; - new_hook_print = (struct t_hook_print *)malloc (sizeof (struct t_hook_print)); - if (!new_hook_print) - { - free (new_hook); - return NULL; - } - - hook_init (new_hook, plugin, HOOK_TYPE_PRINT, callback_data); - - new_hook->hook_data = new_hook_print; - new_hook_print->callback = callback; - new_hook_print->buffer = (struct t_gui_buffer *)buffer; - new_hook_print->message = (message) ? strdup (message) : NULL; - - hook_add_to_list (new_hook); - - return new_hook; -} - -/* - * hook_print_exec: execute print hook - */ - -void -hook_print_exec (void *buffer, time_t date, char *prefix, char *message) -{ - struct t_hook *ptr_hook, *next_hook; - - if (!message || !message[0]) - return; - - ptr_hook = weechat_hooks; - while (ptr_hook) - { - next_hook = ptr_hook->next_hook; - - if ((ptr_hook->type == HOOK_TYPE_PRINT) - && (!ptr_hook->running) - && (!HOOK_PRINT(ptr_hook, buffer) - || ((struct t_gui_buffer *)buffer == HOOK_PRINT(ptr_hook, buffer))) - && (string_strcasestr (message, HOOK_PRINT(ptr_hook, message)))) - { - ptr_hook->running = 1; - (void) (HOOK_PRINT(ptr_hook, callback)) - (ptr_hook->callback_data, buffer, date, prefix, message); - if (hook_valid (ptr_hook)) - ptr_hook->running = 0; - } - - ptr_hook = next_hook; - } -} - -/* - * hook_config: hook a config option - */ - -struct t_hook * -hook_config (void *plugin, char *type, char *option, - t_hook_callback_config *callback, void *callback_data) -{ - struct t_hook *new_hook; - struct t_hook_config *new_hook_config; - - new_hook = (struct t_hook *)malloc (sizeof (struct t_hook)); - if (!new_hook) - return NULL; - new_hook_config = (struct t_hook_config *)malloc (sizeof (struct t_hook_config)); - if (!new_hook_config) - { - free (new_hook); - return NULL; - } - - hook_init (new_hook, plugin, HOOK_TYPE_CONFIG, callback_data); - - new_hook->hook_data = new_hook_config; - new_hook_config->callback = callback; - new_hook_config->type = (type) ? strdup (type) : strdup (""); - new_hook_config->option = (option) ? strdup (option) : strdup (""); - - hook_add_to_list (new_hook); - - return new_hook; -} - -/* - * hook_config_exec: execute config hooks - */ - -void -hook_config_exec (char *type, char *option, char *value) -{ - struct t_hook *ptr_hook, *next_hook; - - ptr_hook = weechat_hooks; - while (ptr_hook) - { - next_hook = ptr_hook->next_hook; - - if ((ptr_hook->type == HOOK_TYPE_CONFIG) - && (!ptr_hook->running) - && (!HOOK_CONFIG(ptr_hook, type) - || (string_strcasecmp (HOOK_CONFIG(ptr_hook, type), - type) == 0)) - && (!HOOK_CONFIG(ptr_hook, option) - || (string_strcasecmp (HOOK_CONFIG(ptr_hook, option), - option) == 0))) - { - ptr_hook->running = 1; - (void) (HOOK_CONFIG(ptr_hook, callback)) - (ptr_hook->callback_data, type, option, value); - if (hook_valid (ptr_hook)) - ptr_hook->running = 0; - } - - ptr_hook = next_hook; - } -} - -/* * hook_timer: hook a timer */ @@ -550,6 +417,222 @@ hook_fd_exec (fd_set *read_fds, fd_set *write_fds, fd_set *except_fds) } /* + * hook_print: hook a message printed by WeeChat + */ + +struct t_hook * +hook_print (void *plugin, void *buffer, char *message, int strip_colors, + t_hook_callback_print *callback, void *callback_data) +{ + struct t_hook *new_hook; + struct t_hook_print *new_hook_print; + + new_hook = (struct t_hook *)malloc (sizeof (struct t_hook)); + if (!new_hook) + return NULL; + new_hook_print = (struct t_hook_print *)malloc (sizeof (struct t_hook_print)); + if (!new_hook_print) + { + free (new_hook); + return NULL; + } + + hook_init (new_hook, plugin, HOOK_TYPE_PRINT, callback_data); + + new_hook->hook_data = new_hook_print; + new_hook_print->callback = callback; + new_hook_print->buffer = (struct t_gui_buffer *)buffer; + new_hook_print->message = (message) ? strdup (message) : NULL; + new_hook_print->strip_colors = strip_colors; + + hook_add_to_list (new_hook); + + return new_hook; +} + +/* + * hook_print_exec: execute print hook + */ + +void +hook_print_exec (void *buffer, time_t date, char *prefix, char *message) +{ + struct t_hook *ptr_hook, *next_hook; + char *prefix_no_color, *message_no_color; + + if (!message || !message[0]) + return; + + prefix_no_color = (char *)gui_color_decode ((unsigned char *)prefix); + if (!prefix_no_color) + return; + + message_no_color = (char *)gui_color_decode ((unsigned char *)message); + if (!message_no_color) + { + free (prefix_no_color); + return; + } + + ptr_hook = weechat_hooks; + while (ptr_hook) + { + next_hook = ptr_hook->next_hook; + + if ((ptr_hook->type == HOOK_TYPE_PRINT) + && (!ptr_hook->running) + && (!HOOK_PRINT(ptr_hook, buffer) + || ((struct t_gui_buffer *)buffer == HOOK_PRINT(ptr_hook, buffer))) + && (!HOOK_PRINT(ptr_hook, message) + || string_strcasestr (prefix_no_color, HOOK_PRINT(ptr_hook, message)) + || string_strcasestr (message_no_color, HOOK_PRINT(ptr_hook, message)))) + { + ptr_hook->running = 1; + (void) (HOOK_PRINT(ptr_hook, callback)) + (ptr_hook->callback_data, buffer, date, + (HOOK_PRINT(ptr_hook, strip_colors)) ? prefix_no_color : prefix, + (HOOK_PRINT(ptr_hook, strip_colors)) ? message_no_color : message); + if (hook_valid (ptr_hook)) + ptr_hook->running = 0; + } + + ptr_hook = next_hook; + } +} + +/* + * hook_event: hook an event + */ + +struct t_hook * +hook_event (void *plugin, char *event, + t_hook_callback_event *callback, void *callback_data) +{ + struct t_hook *new_hook; + struct t_hook_event *new_hook_event; + + if (!event || !event[0]) + return NULL; + + new_hook = (struct t_hook *)malloc (sizeof (struct t_hook)); + if (!new_hook) + return NULL; + new_hook_event = (struct t_hook_event *)malloc (sizeof (struct t_hook_event)); + if (!new_hook_event) + { + free (new_hook); + return NULL; + } + + hook_init (new_hook, plugin, HOOK_TYPE_EVENT, callback_data); + + new_hook->hook_data = new_hook_event; + new_hook_event->callback = callback; + new_hook_event->event = strdup (event); + + hook_add_to_list (new_hook); + + return new_hook; +} + +/* + * hook_event_exec: execute event hook + */ + +void +hook_event_exec (char *event, void *pointer) +{ + struct t_hook *ptr_hook, *next_hook; + + ptr_hook = weechat_hooks; + while (ptr_hook) + { + next_hook = ptr_hook->next_hook; + + if ((ptr_hook->type == HOOK_TYPE_EVENT) + && (!ptr_hook->running) + && ((string_strcasecmp (HOOK_EVENT(ptr_hook, event), "*") == 0) + || (string_strcasecmp (HOOK_EVENT(ptr_hook, event), event) == 0))) + { + ptr_hook->running = 1; + (void) (HOOK_EVENT(ptr_hook, callback)) + (ptr_hook->callback_data, event, pointer); + if (hook_valid (ptr_hook)) + ptr_hook->running = 0; + } + + ptr_hook = next_hook; + } +} + +/* + * hook_config: hook a config option + */ + +struct t_hook * +hook_config (void *plugin, char *type, char *option, + t_hook_callback_config *callback, void *callback_data) +{ + struct t_hook *new_hook; + struct t_hook_config *new_hook_config; + + new_hook = (struct t_hook *)malloc (sizeof (struct t_hook)); + if (!new_hook) + return NULL; + new_hook_config = (struct t_hook_config *)malloc (sizeof (struct t_hook_config)); + if (!new_hook_config) + { + free (new_hook); + return NULL; + } + + hook_init (new_hook, plugin, HOOK_TYPE_CONFIG, callback_data); + + new_hook->hook_data = new_hook_config; + new_hook_config->callback = callback; + new_hook_config->type = (type) ? strdup (type) : strdup (""); + new_hook_config->option = (option) ? strdup (option) : strdup (""); + + hook_add_to_list (new_hook); + + return new_hook; +} + +/* + * hook_config_exec: execute config hooks + */ + +void +hook_config_exec (char *type, char *option, char *value) +{ + struct t_hook *ptr_hook, *next_hook; + + ptr_hook = weechat_hooks; + while (ptr_hook) + { + next_hook = ptr_hook->next_hook; + + if ((ptr_hook->type == HOOK_TYPE_CONFIG) + && (!ptr_hook->running) + && (!HOOK_CONFIG(ptr_hook, type) + || (string_strcasecmp (HOOK_CONFIG(ptr_hook, type), + type) == 0)) + && (!HOOK_CONFIG(ptr_hook, option) + || (string_strcasecmp (HOOK_CONFIG(ptr_hook, option), + option) == 0))) + { + ptr_hook->running = 1; + (void) (HOOK_CONFIG(ptr_hook, callback)) + (ptr_hook->callback_data, type, option, value); + if (hook_valid (ptr_hook)) + ptr_hook->running = 0; + } + + ptr_hook = next_hook; + } +} + +/* * unhook: unhook something */ @@ -579,11 +662,22 @@ unhook (struct t_hook *hook) free (HOOK_COMMAND(hook, completion)); free ((struct t_hook_command *)hook->hook_data); break; + case HOOK_TYPE_TIMER: + free ((struct t_hook_timer *)hook->hook_data); + break; + case HOOK_TYPE_FD: + free ((struct t_hook_fd *)hook->hook_data); + break; case HOOK_TYPE_PRINT: if (HOOK_PRINT(hook, message)) free (HOOK_PRINT(hook, message)); free ((struct t_hook_print *)hook->hook_data); break; + case HOOK_TYPE_EVENT: + if (HOOK_EVENT(hook, event)) + free (HOOK_EVENT(hook, event)); + free ((struct t_hook_event *)hook->hook_data); + break; case HOOK_TYPE_CONFIG: if (HOOK_CONFIG(hook, type)) free (HOOK_CONFIG(hook, type)); @@ -591,14 +685,6 @@ unhook (struct t_hook *hook) free (HOOK_CONFIG(hook, option)); free ((struct t_hook_config *)hook->hook_data); break; - case HOOK_TYPE_TIMER: - free ((struct t_hook_timer *)hook->hook_data); - break; - case HOOK_TYPE_FD: - free ((struct t_hook_fd *)hook->hook_data); - break; - case HOOK_TYPE_KEYBOARD: - break; } } @@ -681,16 +767,6 @@ hook_print_log () weechat_log_printf (" command_args_desc. . : '%s'\n", HOOK_COMMAND(ptr_hook, args_description)); weechat_log_printf (" command_completion . : '%s'\n", HOOK_COMMAND(ptr_hook, completion)); break; - case HOOK_TYPE_PRINT: - weechat_log_printf (" print data:\n"); - weechat_log_printf (" buffer . . . . . . . : 0x%X\n", HOOK_PRINT(ptr_hook, buffer)); - weechat_log_printf (" message. . . . . . . : '%s'\n", HOOK_PRINT(ptr_hook, message)); - break; - case HOOK_TYPE_CONFIG: - weechat_log_printf (" config data:\n"); - weechat_log_printf (" type . . . . . . . . : '%s'\n", HOOK_CONFIG(ptr_hook, type)); - weechat_log_printf (" option . . . . . . . : '%s'\n", HOOK_CONFIG(ptr_hook, option)); - break; case HOOK_TYPE_TIMER: weechat_log_printf (" timer data:\n"); weechat_log_printf (" interval . . . . . . : %ld\n", HOOK_TIMER(ptr_hook, interval)); @@ -702,8 +778,19 @@ hook_print_log () weechat_log_printf (" fd . . . . . . . . . : %ld\n", HOOK_FD(ptr_hook, fd)); weechat_log_printf (" flags. . . . . . . . : %ld\n", HOOK_FD(ptr_hook, flags)); break; - case HOOK_TYPE_KEYBOARD: - weechat_log_printf (" keyboard data:\n"); + case HOOK_TYPE_PRINT: + weechat_log_printf (" print data:\n"); + weechat_log_printf (" buffer . . . . . . . : 0x%X\n", HOOK_PRINT(ptr_hook, buffer)); + weechat_log_printf (" message. . . . . . . : '%s'\n", HOOK_PRINT(ptr_hook, message)); + break; + case HOOK_TYPE_EVENT: + weechat_log_printf (" event data:\n"); + weechat_log_printf (" event. . . . . . . . : '%s'\n", HOOK_EVENT(ptr_hook, event)); + break; + case HOOK_TYPE_CONFIG: + weechat_log_printf (" config data:\n"); + weechat_log_printf (" type . . . . . . . . : '%s'\n", HOOK_CONFIG(ptr_hook, type)); + weechat_log_printf (" option . . . . . . . : '%s'\n", HOOK_CONFIG(ptr_hook, option)); break; } weechat_log_printf (" running. . . . . . . . : %d\n", ptr_hook->running); diff --git a/src/core/wee-hook.h b/src/core/wee-hook.h index 38784a11a..ece199fb6 100644 --- a/src/core/wee-hook.h +++ b/src/core/wee-hook.h @@ -25,11 +25,11 @@ enum t_hook_type { HOOK_TYPE_COMMAND = 0, /* new command */ - HOOK_TYPE_PRINT, /* printed messages */ - HOOK_TYPE_CONFIG, /* config option */ HOOK_TYPE_TIMER, /* timer */ HOOK_TYPE_FD, /* socket of file descriptor */ - HOOK_TYPE_KEYBOARD, /* keyboard handler */ + HOOK_TYPE_PRINT, /* printed message */ + HOOK_TYPE_EVENT, /* event */ + HOOK_TYPE_CONFIG, /* config option */ }; #define HOOK_FD_FLAG_READ 1 @@ -37,10 +37,11 @@ enum t_hook_type #define HOOK_FD_FLAG_EXCEPTION 4 #define HOOK_COMMAND(hook, var) (((struct t_hook_command *)hook->hook_data)->var) -#define HOOK_PRINT(hook, var) (((struct t_hook_print *)hook->hook_data)->var) -#define HOOK_CONFIG(hook, var) (((struct t_hook_config *)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_PRINT(hook, var) (((struct t_hook_print *)hook->hook_data)->var) +#define HOOK_EVENT(hook, var) (((struct t_hook_event *)hook->hook_data)->var) +#define HOOK_CONFIG(hook, var) (((struct t_hook_config *)hook->hook_data)->var) struct t_hook { @@ -70,25 +71,6 @@ struct t_hook_command char *completion; /* template for completion */ }; -typedef int (t_hook_callback_print)(void *, void *, time_t, char *, char *); - -struct t_hook_print -{ - t_hook_callback_print *callback; /* print callback */ - struct t_gui_buffer *buffer; /* buffer selected (NULL = all) */ - char *message; /* part of message (NULL/empty = all)*/ -}; - -typedef int (t_hook_callback_config)(void *, char *, char *, char *); - -struct t_hook_config -{ - t_hook_callback_config *callback; /* config callback */ - char *type; /* "weechat" or "plugin" */ - char *option; /* config option for hook */ - /* (NULL = hook for all options) */ -}; - typedef int (t_hook_callback_timer)(void *); struct t_hook_timer @@ -108,6 +90,34 @@ struct t_hook_fd int flags; /* fd flags (read,write,..) */ }; +typedef int (t_hook_callback_print)(void *, void *, time_t, char *, char *); + +struct t_hook_print +{ + t_hook_callback_print *callback; /* print callback */ + struct t_gui_buffer *buffer; /* buffer selected (NULL = all) */ + char *message; /* part of message (NULL/empty = all)*/ + int strip_colors; /* strip colors in msg for callback? */ +}; + +typedef int (t_hook_callback_event)(void *, char *, void *); + +struct t_hook_event +{ + t_hook_callback_event *callback; /* event callback */ + char *event; /* event selected ("*" = any event) */ +}; + +typedef int (t_hook_callback_config)(void *, char *, char *, char *); + +struct t_hook_config +{ + t_hook_callback_config *callback; /* config callback */ + char *type; /* "weechat" or "plugin" */ + char *option; /* config option for hook */ + /* (NULL = hook for all options) */ +}; + /* hook variables */ extern struct t_hook *weechat_hooks; @@ -117,21 +127,26 @@ extern struct t_hook *last_weechat_hook; extern int hook_valid (struct t_hook *); extern int hook_valid_for_plugin (void *, struct t_hook *); + extern struct t_hook *hook_command (void *, char *, char *, char *, char *, char *, t_hook_callback_command *, void *); extern int hook_command_exec (void *, char *); -extern struct t_hook *hook_print (void *, void *, char *, - t_hook_callback_print *, void *); -extern void hook_print_exec (void *, time_t, char *, char *); -extern struct t_hook *hook_config (void *, char *, char *, - t_hook_callback_config *, void *); -extern void hook_config_exec (char *, char *, char *); extern struct t_hook *hook_timer (void *, long, int, t_hook_callback_timer *, void *); extern void hook_timer_exec (struct timeval *); extern struct t_hook *hook_fd (void *, int, int, t_hook_callback_fd *,void *); extern void hook_fd_set (fd_set *, fd_set *, fd_set *); extern void hook_fd_exec (fd_set *, fd_set *, fd_set *); +extern struct t_hook *hook_print (void *, void *, char *, int, + t_hook_callback_print *, void *); +extern void hook_print_exec (void *, time_t, char *, char *); +extern struct t_hook *hook_event (void *, char *, + t_hook_callback_event *, void *); +extern void hook_event_exec (char *, void *); +extern struct t_hook *hook_config (void *, char *, char *, + t_hook_callback_config *, void *); +extern void hook_config_exec (char *, char *, char *); + extern void unhook (struct t_hook *); extern void unhook_all_plugin (void *); extern void unhook_all (); diff --git a/src/gui/gui-buffer.c b/src/gui/gui-buffer.c index 09ad6df6c..aee0c03be 100644 --- a/src/gui/gui-buffer.c +++ b/src/gui/gui-buffer.c @@ -44,6 +44,7 @@ #include "gui-window.h" #include "../core/wee-command.h" #include "../core/wee-config.h" +#include "../core/wee-hook.h" #include "../core/wee-log.h" #include "../core/wee-string.h" #include "../core/wee-utf8.h" @@ -67,7 +68,6 @@ gui_buffer_new (void *plugin, char *category, char *name) { struct t_gui_buffer *new_buffer; struct t_gui_completion *new_completion; - char buffer_str[16], *argv[1] = { NULL }; #ifdef DEBUG weechat_log_printf ("Creating new buffer\n"); @@ -76,6 +76,16 @@ gui_buffer_new (void *plugin, char *category, char *name) if (!category || !name) return NULL; + if (gui_buffer_search_by_category_name (category, name)) + { + gui_chat_printf (NULL, + _("%sError: a buffer with same name already exists " + "(%s / %s)"), + gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], + category, name); + return NULL; + } + /* create new buffer */ if ((new_buffer = (struct t_gui_buffer *)(malloc (sizeof (struct t_gui_buffer))))) { @@ -166,10 +176,7 @@ gui_buffer_new (void *plugin, char *category, char *name) gui_window_redraw_buffer (new_buffer); } - snprintf (buffer_str, sizeof (buffer_str) - 1, "%d", new_buffer->number); - argv[0] = buffer_str; - /* TODO: send buffer_open event */ - /*(void) plugin_event_handler_exec ("buffer_open", 1, argv);*/ + (void) hook_event_exec ("buffer_open", new_buffer); } else return NULL; @@ -384,15 +391,15 @@ gui_buffer_search_by_category_name (char *category, char *name) struct t_gui_buffer *ptr_buffer; if (!category && !name) - return gui_current_window->buffer; + return NULL; for (ptr_buffer = gui_buffers; ptr_buffer; ptr_buffer = ptr_buffer->next_buffer) { - if ((category && ptr_buffer->category - && strcmp (ptr_buffer->category, category) == 0) - || (name && ptr_buffer->name - && strcmp (ptr_buffer->name, name) == 0)) + if ((!category || (ptr_buffer->category + && (strcmp (ptr_buffer->category, category) == 0))) + && (!name || (ptr_buffer->name + && (strcmp (ptr_buffer->name, name) == 0)))) return ptr_buffer; } @@ -576,12 +583,8 @@ gui_buffer_free (struct t_gui_buffer *buffer, int switch_to_another) struct t_gui_window *ptr_window; struct t_gui_buffer *ptr_buffer; struct t_gui_line *ptr_line; - char buffer_str[16], *argv[1] = { NULL }; - snprintf (buffer_str, sizeof (buffer_str) - 1, "%d", buffer->number); - argv[0] = buffer_str; - /* TODO: send buffer_close event */ - /*(void) plugin_event_handler_exec ("buffer_close", 1, argv);*/ + (void) hook_event_exec ("buffer_close", buffer); if (switch_to_another) { diff --git a/src/plugins/demo/demo.c b/src/plugins/demo/demo.c index 5c5902110..fed51f515 100644 --- a/src/plugins/demo/demo.c +++ b/src/plugins/demo/demo.c @@ -40,17 +40,15 @@ static struct t_weechat_plugin *weechat_plugin = NULL; /* - * demo_printf_command: demo command for printf + * demo_printf_command_cb: demo command for printf */ static int -demo_printf_command (void *data, int argc, char **argv, char **argv_eol) +demo_printf_command_cb (void *data, int argc, char **argv, char **argv_eol) { (void) data; - (void) argc; (void) argv; - (void) argv_eol; - + if (argc > 1) weechat_printf (weechat_current_buffer, "demo_printf: %s", argv_eol[1]); @@ -78,6 +76,28 @@ demo_printf_command (void *data, int argc, char **argv, char **argv_eol) } /* + * demo_buffer_command_cb: demo command for creatig new buffer + */ + +static int +demo_buffer_command_cb (void *data, int argc, char **argv, char **argv_eol) +{ + struct t_gui_buffer *buffer; + + (void) data; + (void) argv_eol; + + if (argc > 2) + { + buffer = weechat_buffer_new (argv[1], argv[2]); + if (buffer) + weechat_buffer_set (buffer, "display", "1"); + } + + return PLUGIN_RC_SUCCESS; +} + +/* * demo_print_list: display a list */ @@ -138,11 +158,11 @@ demo_print_list (void *list, char *item_name) } /* - * demo_list_command: demo command for list + * demo_list_command_cb: demo command for list */ static int -demo_list_command (void *data, int argc, char **argv, char **argv_eol) +demo_list_command_cb (void *data, int argc, char **argv, char **argv_eol) { struct t_plugin_list *list; @@ -182,11 +202,11 @@ demo_list_command (void *data, int argc, char **argv, char **argv_eol) } /* - * demo_info_command: demo command for info_get + * demo_info_command_cb: demo command for info_get */ static int -demo_info_command (void *data, int argc, char **argv, char **argv_eol) +demo_info_command_cb (void *data, int argc, char **argv, char **argv_eol) { /* make C compiler happy */ (void) data; @@ -206,6 +226,21 @@ demo_info_command (void *data, int argc, char **argv, char **argv_eol) } /* + * demo_event_cb: callback for event hook + */ + +static int +demo_event_cb (void *data, char *event, void *pointer) +{ + (void) data; + + weechat_printf (NULL, "demo_event: event: %s, pointer: %X", + event, pointer); + + return PLUGIN_RC_SUCCESS; +} + +/* * weechat_plugin_init: init demo plugin */ @@ -217,14 +252,19 @@ weechat_plugin_init (struct t_weechat_plugin *plugin) weechat_hook_command ("demo_printf", "demo command: print some messages", "[text]", "text: write some text on current buffer", "", - demo_printf_command, NULL); + demo_printf_command_cb, NULL); + + weechat_hook_command ("demo_buffer", "open a new buffer", + "category name", "", + "", + demo_buffer_command_cb, NULL); weechat_hook_command ("demo_list", "demo command: get and display list", "list", "list: list to display (values: buffer, " "buffer_lines)", "buffer|buffer_lines", - demo_list_command, NULL); + demo_list_command_cb, NULL); weechat_hook_command ("demo_info", "demo command: get and display info", "info", @@ -235,7 +275,9 @@ weechat_plugin_init (struct t_weechat_plugin *plugin) "version|weechat_dir|weechat_libdir|" "weechat_sharedir|charset_terminal|charset_internal|" "inactivity|input|input_mask|input_pos", - demo_info_command, NULL); + demo_info_command_cb, NULL); + + weechat_hook_event ("*", demo_event_cb, NULL); return PLUGIN_RC_SUCCESS; } diff --git a/src/plugins/fifo/fifo.c b/src/plugins/fifo/fifo.c index 55f77279c..a82ae86ab 100644 --- a/src/plugins/fifo/fifo.c +++ b/src/plugins/fifo/fifo.c @@ -295,11 +295,11 @@ fifo_read () } /* - * fifo_config: fifo config callback (called when fifo option is changed) + * fifo_config_cb: fifo config callback (called when fifo option is changed) */ static int -fifo_config (void *data, char *type, char *option, char *value) +fifo_config_cb (void *data, char *type, char *option, char *value) { /* make C compiler happy */ (void) data; @@ -333,7 +333,7 @@ weechat_plugin_init (struct t_weechat_plugin *plugin) fifo_fd_hook = weechat_hook_fd (fifo_fd, 1, 0, 0, fifo_read, NULL); - weechat_hook_config ("plugin", "fifo.fifo", fifo_config, NULL); + weechat_hook_config ("plugin", "fifo.fifo", fifo_config_cb, NULL); return PLUGIN_RC_SUCCESS; } diff --git a/src/plugins/plugin-api.c b/src/plugins/plugin-api.c index 70469a55a..4b0a11c13 100644 --- a/src/plugins/plugin-api.c +++ b/src/plugins/plugin-api.c @@ -155,6 +155,20 @@ plugin_api_strncasecmp (struct t_weechat_plugin *plugin, } /* + * plugin_api_string_replace: replace a string by new one in a string + */ + +char * +plugin_api_string_replace (struct t_weechat_plugin *plugin, + char *string, char *search, char *replace) +{ + /* make C compiler happy */ + (void) plugin; + + return string_replace (string, search, replace); +} + +/* * plugin_api_string_explode: explode a string */ @@ -402,40 +416,6 @@ plugin_api_hook_command (struct t_weechat_plugin *plugin, char *command, } /* - * plugin_api_hook_print: hook a printed message - */ - -struct t_hook * -plugin_api_hook_print (struct t_weechat_plugin *plugin, void *buffer, - char *message, - int (*callback)(void *, void *, time_t, char *, char *), - void *data) -{ - if (plugin && gui_buffer_valid ((struct t_gui_buffer *)buffer) - && callback) - return hook_print (plugin, buffer, message, callback, data); - - return NULL; -} - -/* - * plugin_api_hook_config: hook a config option - */ - -struct t_hook * -plugin_api_hook_config (struct t_weechat_plugin *plugin, char *config_type, - char *config_option, - int (*callback)(void *, char *, char *, char *), - void *data) -{ - if (plugin && callback) - return hook_config (plugin, config_type, config_option, - callback, data); - - return NULL; -} - -/* * plugin_api_hook_timer: hook a timer */ @@ -476,6 +456,56 @@ plugin_api_hook_fd (struct t_weechat_plugin *plugin, int fd, } /* + * plugin_api_hook_print: hook a printed message + */ + +struct t_hook * +plugin_api_hook_print (struct t_weechat_plugin *plugin, void *buffer, + char *message, int strip_colors, + int (*callback)(void *, void *, time_t, char *, char *), + void *data) +{ + if (plugin && gui_buffer_valid ((struct t_gui_buffer *)buffer) + && callback) + return hook_print (plugin, buffer, message, strip_colors, + callback, data); + + return NULL; +} + +/* + * plugin_api_hook_event: hook an event + */ + +struct t_hook * +plugin_api_hook_event (struct t_weechat_plugin *plugin, char *event, + int (*callback)(void *, char *, void *), + void *data) +{ + if (plugin && event && event[0] && callback) + return hook_event (plugin, event, callback, data); + + return NULL; +} + +/* + * plugin_api_hook_config: hook a config option + */ + +struct t_hook * +plugin_api_hook_config (struct t_weechat_plugin *plugin, char *config_type, + char *config_option, + int (*callback)(void *, char *, char *, char *), + void *data) +{ + if (plugin && callback) + return hook_config (plugin, config_type, config_option, + callback, data); + + return NULL; +} + +/* * plugin_api_unhook: unhook something */ @@ -520,8 +550,15 @@ struct t_gui_buffer * plugin_api_buffer_search (struct t_weechat_plugin *plugin, char *category, char *name) { + struct t_gui_buffer *ptr_buffer; + if (plugin) - return gui_buffer_search_by_category_name (category, name); + { + ptr_buffer = gui_buffer_search_by_category_name (category, name); + if (ptr_buffer) + return ptr_buffer; + return gui_current_window->buffer; + } return NULL; } @@ -646,6 +683,10 @@ plugin_api_info_get (struct t_weechat_plugin *plugin, char *info) { return strdup (PACKAGE_VERSION); } + else if (string_strcasecmp (info, "dir_separator") == 0) + { + return strdup (DIR_SEPARATOR); + } else if (string_strcasecmp (info, "weechat_dir") == 0) { return strdup (weechat_home); diff --git a/src/plugins/plugin-api.h b/src/plugins/plugin-api.h index e1a5dad57..6cb555cdf 100644 --- a/src/plugins/plugin-api.h +++ b/src/plugins/plugin-api.h @@ -32,6 +32,8 @@ extern char *plugin_api_ngettext (struct t_weechat_plugin *, char *, char *, extern int plugin_api_strcasecmp (struct t_weechat_plugin *,char *, char *); extern int plugin_api_strncasecmp (struct t_weechat_plugin *,char *, char *, int); +extern char *plugin_api_string_replace (struct t_weechat_plugin *,char *, + char *, char *); extern char **plugin_api_string_explode (struct t_weechat_plugin *, char *, char *, int, int, int *); extern void plugin_api_string_free_exploded (struct t_weechat_plugin *, @@ -59,20 +61,23 @@ extern struct t_hook *plugin_api_hook_command (struct t_weechat_plugin *, char *, int (*)(void *, int, char **, char **), void *); +extern struct t_hook *plugin_api_hook_timer (struct t_weechat_plugin *, + long, int, + int (*)(void *), void *); +extern struct t_hook *plugin_api_hook_fd (struct t_weechat_plugin *, + int, int, int, int, + int (*)(void *), void *); extern struct t_hook *plugin_api_hook_print (struct t_weechat_plugin *, - void *, char *, + void *, char *, int, int (*)(void *, void *, time_t, char *, char *), void *); +extern struct t_hook *plugin_api_hook_event (struct t_weechat_plugin *, char *, + int (*)(void *, char *, void *), + void *); extern struct t_hook *plugin_api_hook_config (struct t_weechat_plugin *, char *, char *, int (*)(void *, char *, char *, char *), void *); -extern struct t_hook *plugin_api_hook_timer (struct t_weechat_plugin *, - long, int, - int (*)(void *), void *); -extern struct t_hook *plugin_api_hook_fd (struct t_weechat_plugin *, - int, int, int, int, - int (*)(void *), void *); extern void plugin_api_unhook (struct t_weechat_plugin *, void *); extern void plugin_api_unhook_all (struct t_weechat_plugin *); diff --git a/src/plugins/plugin.c b/src/plugins/plugin.c index 157c42613..adc5bb67c 100644 --- a/src/plugins/plugin.c +++ b/src/plugins/plugin.c @@ -231,6 +231,7 @@ plugin_load (char *filename) new_plugin->ngettext = &plugin_api_ngettext; new_plugin->strcasecmp = &plugin_api_strcasecmp; new_plugin->strncasecmp = &plugin_api_strncasecmp; + new_plugin->string_replace = &plugin_api_string_replace; new_plugin->string_explode = &plugin_api_string_explode; new_plugin->string_free_exploded = &plugin_api_string_free_exploded; @@ -245,10 +246,11 @@ plugin_load (char *filename) new_plugin->infobar_remove = &plugin_api_infobar_remove; new_plugin->hook_command = &plugin_api_hook_command; - new_plugin->hook_print = &plugin_api_hook_print; - new_plugin->hook_config = &plugin_api_hook_config; new_plugin->hook_timer = &plugin_api_hook_timer; new_plugin->hook_fd = &plugin_api_hook_fd; + new_plugin->hook_print = &plugin_api_hook_print; + new_plugin->hook_event = &plugin_api_hook_event; + new_plugin->hook_config = &plugin_api_hook_config; new_plugin->unhook = &plugin_api_unhook; new_plugin->unhook_all = &plugin_api_unhook_all; @@ -289,11 +291,6 @@ plugin_load (char *filename) weechat_plugins = new_plugin; last_weechat_plugin = new_plugin; - gui_chat_printf (NULL, - _("%sInitializing plugin \"%s\" %s\n"), - gui_chat_prefix[GUI_CHAT_PREFIX_INFO], - new_plugin->name, new_plugin->version); - /* init plugin */ if (((t_weechat_init_func *)init_func) (new_plugin) < 0) { @@ -319,9 +316,9 @@ plugin_load (char *filename) } gui_chat_printf (NULL, - _("%sPlugin \"%s\" (%s) loaded.\n"), + _("%sPlugin \"%s\" %s loaded.\n"), gui_chat_prefix[GUI_CHAT_PREFIX_INFO], - name, full_name); + name, new_plugin->version); free (full_name); diff --git a/src/plugins/weechat-plugin.h b/src/plugins/weechat-plugin.h index 7bf86672c..3fcbf7dd7 100644 --- a/src/plugins/weechat-plugin.h +++ b/src/plugins/weechat-plugin.h @@ -63,6 +63,7 @@ struct t_weechat_plugin char *(*ngettext) (struct t_weechat_plugin *, char *, char *, int); int (*strcasecmp) (struct t_weechat_plugin *, char *, char *); int (*strncasecmp) (struct t_weechat_plugin *, char *, char *, int); + char *(*string_replace) (struct t_weechat_plugin *, char *, char *, char *); char **(*string_explode) (struct t_weechat_plugin *, char *, char *, int, int, int *); void (*string_free_exploded) (struct t_weechat_plugin *, char **); @@ -86,16 +87,19 @@ struct t_weechat_plugin char *, char *, char *, int (*)(void *, int, char **, char **), void *); + struct t_hook *(*hook_timer) (struct t_weechat_plugin *, long, int, + int (*)(void *), void *); + struct t_hook *(*hook_fd) (struct t_weechat_plugin *, int, int, int, int, + int (*)(void *), void *); struct t_hook *(*hook_print) (struct t_weechat_plugin *, void *, char *, + int, int (*)(void *, void *, time_t, char *, char *), void *); + struct t_hook *(*hook_event) (struct t_weechat_plugin *, char *, + int (*)(void *, char *, void *), void *); struct t_hook *(*hook_config) (struct t_weechat_plugin *, char *, char *, int (*)(void *, char *, char *, char *), void *); - struct t_hook *(*hook_timer) (struct t_weechat_plugin *, long, int, - int (*)(void *), void *); - struct t_hook *(*hook_fd) (struct t_weechat_plugin *, int, int, int, int, - int (*)(void *), void *); void (*unhook) (struct t_weechat_plugin *, void *); void (*unhook_all) (struct t_weechat_plugin *); @@ -142,6 +146,12 @@ struct t_weechat_plugin /* macros for easy call to plugin API */ +#define weechat_charset_set(chset, string) \ + weechat_plugin->charset_set(weechat_plugin, string) +#define weechat_iconv_to_internal(chset, string) \ + weechat_plugin->iconv_to_internal(weechat_plugin, chset, string) +#define weechat_iconv_from_internal(chset, string) \ + weechat_plugin->iconv_from_internal(weechat_plugin, chset, string) #ifndef __WEECHAT_H #define _(string) weechat_plugin->gettext(weechat_plugin, string) #define N_(string) (string) @@ -152,6 +162,9 @@ struct t_weechat_plugin weechat_plugin->strcasecmp(weechat_plugin, string1, string2) #define weechat_strncasecmp(string1, string2, max) \ weechat_plugin->strncasecmp(weechat_plugin, string1, string2, max) +#define weechat_string_replace(string1, search1, replace1) \ + weechat_plugin->string_replace(weechat_plugin, string1, search1, \ + replace1) #define weechat_string_explode(string1, separator, eol, max, \ num_items) \ weechat_plugin->string_explode(weechat_plugin, string1, separator, \ @@ -174,12 +187,6 @@ struct t_weechat_plugin weechat_plugin->hook_command(weechat_plugin, command, description, \ args, args_desc, completion, callback, \ data) -#define weechat_hook_print(buffer, msg, callback, data) \ - weechat_plugin->hook_print(weechat_plugin, buffer, msg, \ - callback, data) -#define weechat_hook_config(type, option, callback, data) \ - weechat_plugin->hook_config(weechat_plugin, type, option, \ - callback, data) #define weechat_hook_timer(interval, max_calls, callback, data) \ weechat_plugin->hook_timer(weechat_plugin, interval, max_calls, \ callback, data) @@ -187,6 +194,14 @@ struct t_weechat_plugin callback, data) \ weechat_plugin->hook_fd(weechat_plugin, fd, flag_read, flag_write, \ flag_exception, callback, data) +#define weechat_hook_print(buffer, msg, strip_colors, callback, data) \ + weechat_plugin->hook_print(weechat_plugin, buffer, msg, \ + strip_colors, callback, data) +#define weechat_hook_event(evnt, callback, data) \ + weechat_plugin->hook_event(weechat_plugin, evnt, callback, data) +#define weechat_hook_config(type, option, callback, data) \ + weechat_plugin->hook_config(weechat_plugin, type, option, \ + callback, data) #define weechat_unhook(hook) \ weechat_plugin->unhook(weechat_plugin, hook) #define weechat_unhook_all() \ |