diff options
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/perl/wee-perl.c | 39 | ||||
-rw-r--r-- | src/plugins/plugins.c | 137 | ||||
-rw-r--r-- | src/plugins/plugins.h | 24 |
3 files changed, 151 insertions, 49 deletions
diff --git a/src/plugins/perl/wee-perl.c b/src/plugins/perl/wee-perl.c index 2433e8fab..21926bf4a 100644 --- a/src/plugins/perl/wee-perl.c +++ b/src/plugins/perl/wee-perl.c @@ -31,8 +31,9 @@ #include <perl.h> #include <XSUB.h> #include "../../common/weechat.h" -#include "wee-perl.h" #include "../plugins.h" +#include "wee-perl.h" +#include "../../common/command.h" #include "../../gui/gui.h" @@ -81,7 +82,7 @@ static XS (XS_IRC_register) name, version, description); } else - gui_printf (NULL, + gui_printf (gui_current_window, _("%s unable to load Perl script \"%s\" (not enough memory)\n"), WEECHAT_ERROR, name); XST_mPV (0, VERSION); @@ -121,7 +122,34 @@ static XS (XS_IRC_add_message_handler) name = SvPV (ST (0), integer); function = SvPV (ST (1), integer); - plugins_msg_handler_add (PLUGIN_PERL, name, function); + plugin_handler_add (&plugin_msg_handlers, &last_plugin_msg_handler, + PLUGIN_PERL, name, function); + XSRETURN_EMPTY; +} + +/* + * IRC::add_command_handler: add command handler (define/redefine commands) + */ + +static XS (XS_IRC_add_command_handler) +{ + char *name, *function; + int integer; + dXSARGS; + + name = SvPV (ST (0), integer); + function = SvPV (ST (1), integer); + if (index_command_search (name)) + { + gui_printf (gui_current_window, + _("Perl error: alias or command \"%s\" already exists!\n"), + name); + } + else + { + plugin_handler_add (&plugin_cmd_handlers, &last_plugin_cmd_handler, + PLUGIN_PERL, name, function); + } XSRETURN_EMPTY; } @@ -136,6 +164,7 @@ xs_init (pTHX) newXS ("IRC::register", XS_IRC_register, "IRC"); newXS ("IRC::print", XS_IRC_print, "IRC"); newXS ("IRC::add_message_handler", XS_IRC_add_message_handler, "IRC"); + newXS ("IRC::add_command_handler", XS_IRC_add_command_handler, "IRC"); } /* @@ -232,7 +261,7 @@ wee_perl_exec (char *function, char *arguments) return_code = 1; if (SvTRUE (sv)) { - gui_printf (NULL, + gui_printf (gui_current_window, _("Perl error: %s\n"), SvPV (sv, count)); POPs; @@ -241,7 +270,7 @@ wee_perl_exec (char *function, char *arguments) { if (count != 1) { - gui_printf (NULL, + gui_printf (gui_current_window, _("Perl error: too much values from \"%s\" (%d). Expected: 1.\n"), function, count); } diff --git a/src/plugins/plugins.c b/src/plugins/plugins.c index 8ef0627e4..704ccf474 100644 --- a/src/plugins/plugins.c +++ b/src/plugins/plugins.c @@ -37,18 +37,19 @@ #endif -t_plugin_handler *plugins_msg_handlers = NULL; +t_plugin_handler *plugin_msg_handlers = NULL; t_plugin_handler *last_plugin_msg_handler = NULL; -t_plugin_handler *plugins_cmd_handlers = NULL; + +t_plugin_handler *plugin_cmd_handlers = NULL; t_plugin_handler *last_plugin_cmd_handler = NULL; /* - * plugins_init: initialize all plugins + * plugin_init: initialize all plugins */ void -plugins_init () +plugin_init () { #ifdef PLUGIN_PERL wee_perl_init(); @@ -56,11 +57,11 @@ plugins_init () } /* - * plugins_load: load a plugin + * plugin_load: load a plugin */ void -plugins_load (int plugin_type, char *filename) +plugin_load (int plugin_type, char *filename) { #ifdef PLUGINS switch (plugin_type) @@ -81,11 +82,11 @@ plugins_load (int plugin_type, char *filename) } /* - * plugins_unload: unload a plugin + * plugin_unload: unload a plugin */ void -plugins_unload (int plugin_type, char *scriptname) +plugin_unload (int plugin_type, char *scriptname) { #ifdef PLUGINS switch (plugin_type) @@ -106,11 +107,33 @@ plugins_unload (int plugin_type, char *scriptname) } /* - * plugins_msg_handler_add: add a message handler + * plugin_handler_search: look for message/command handler + */ + +t_plugin_handler * +plugin_handler_search (t_plugin_handler *plugin_handlers, char *name) +{ + t_plugin_handler *ptr_plugin_handler; + + for (ptr_plugin_handler = plugin_handlers; ptr_plugin_handler; + ptr_plugin_handler = ptr_plugin_handler->next_handler) + { + /* handler found */ + if (strcasecmp (ptr_plugin_handler->name, name) == 0) + return ptr_plugin_handler; + } + /* handler not found */ + return NULL; +} + +/* + * plugin_handler_add: add a message/command handler */ void -plugins_msg_handler_add (int plugin_type, char *message, char *function) +plugin_handler_add (t_plugin_handler **plugin_handlers, + t_plugin_handler **last_plugin_handler, + int plugin_type, char *name, char *function) { t_plugin_handler *new_plugin_handler; @@ -118,43 +141,45 @@ plugins_msg_handler_add (int plugin_type, char *message, char *function) if (new_plugin_handler) { new_plugin_handler->plugin_type = plugin_type; - new_plugin_handler->name = strdup (message); + new_plugin_handler->name = strdup (name); new_plugin_handler->function_name = strdup (function); /* add new handler to list */ - new_plugin_handler->prev_handler = last_plugin_msg_handler; + new_plugin_handler->prev_handler = *last_plugin_handler; new_plugin_handler->next_handler = NULL; - if (plugins_msg_handlers) - last_plugin_msg_handler->next_handler = new_plugin_handler; + if (*plugin_handlers) + (*last_plugin_handler)->next_handler = new_plugin_handler; else - plugins_msg_handlers = new_plugin_handler; - last_plugin_msg_handler = new_plugin_handler; + *plugin_handlers = new_plugin_handler; + *last_plugin_handler = new_plugin_handler; } else gui_printf (NULL, _("%s unable to add handler for \"%s\" message (not enough memory)\n"), - WEECHAT_ERROR, message); + WEECHAT_ERROR, name); } /* - * plugins_msg_handler_free: free message handler + * plugin_handler_free: free message/command handler */ void -plugins_msg_handler_free (t_plugin_handler *ptr_plugin_handler) +plugin_handler_free (t_plugin_handler **plugin_handlers, + t_plugin_handler **last_plugin_handler, + t_plugin_handler *ptr_plugin_handler) { - t_plugin_handler *new_plugins_msg_handlers; + t_plugin_handler *new_plugin_handlers; /* remove handler from list */ - if (last_plugin_msg_handler == ptr_plugin_handler) - last_plugin_msg_handler = ptr_plugin_handler->prev_handler; + if (*last_plugin_handler == ptr_plugin_handler) + *last_plugin_handler = ptr_plugin_handler->prev_handler; if (ptr_plugin_handler->prev_handler) { (ptr_plugin_handler->prev_handler)->next_handler = ptr_plugin_handler->next_handler; - new_plugins_msg_handlers = plugins_msg_handlers; + new_plugin_handlers = *plugin_handlers; } else - new_plugins_msg_handlers = ptr_plugin_handler->next_handler; + new_plugin_handlers = ptr_plugin_handler->next_handler; if (ptr_plugin_handler->next_handler) (ptr_plugin_handler->next_handler)->prev_handler = ptr_plugin_handler->prev_handler; @@ -163,55 +188,93 @@ plugins_msg_handler_free (t_plugin_handler *ptr_plugin_handler) free (ptr_plugin_handler->name); free (ptr_plugin_handler->function_name); free (ptr_plugin_handler); - plugins_msg_handlers = new_plugins_msg_handlers; + *plugin_handlers = new_plugin_handlers; } /* - * plugins_remove_all_msg_handlers: remove all message handlers + * plugin_handler_free_all: remove all message/command handlers */ void -plugins_msg_handlers_free_all () +plugin_handler_free_all (t_plugin_handler **plugin_handlers, + t_plugin_handler **last_plugin_handler) { - while (plugins_msg_handlers) - plugins_msg_handler_free (plugins_msg_handlers); + while (*plugin_handlers) + plugin_handler_free (plugin_handlers, last_plugin_handler, + *plugin_handlers); } /* - * plugins_event_msg: IRC message received => call all handlers for this message + * plugin_event_msg: IRC message received => call all handlers for this message */ void -plugins_event_msg (char *command, char *arguments) +plugin_event_msg (char *irc_command, char *arguments) +{ + #ifdef PLUGINS + t_plugin_handler *ptr_plugin_handler; + + for (ptr_plugin_handler = plugin_msg_handlers; ptr_plugin_handler; + ptr_plugin_handler = ptr_plugin_handler->next_handler) + { + if (strcasecmp (ptr_plugin_handler->name, irc_command) == 0) + { + #ifdef PLUGIN_PERL + if (ptr_plugin_handler->plugin_type == PLUGIN_PERL) + wee_perl_exec (ptr_plugin_handler->function_name, arguments); + #endif + } + } + #else + /* make gcc happy */ + (void) irc_command; + (void) arguments; + #endif +} + +/* + * plugin_exec_command: execute a command handler + */ + +int +plugin_exec_command (char *user_command, char *arguments) { #ifdef PLUGINS t_plugin_handler *ptr_plugin_handler; - for (ptr_plugin_handler = plugins_msg_handlers; ptr_plugin_handler; + for (ptr_plugin_handler = plugin_cmd_handlers; ptr_plugin_handler; ptr_plugin_handler = ptr_plugin_handler->next_handler) { - if (strcasecmp (ptr_plugin_handler->name, command) == 0) + if (strcasecmp (ptr_plugin_handler->name, user_command) == 0) { #ifdef PLUGIN_PERL if (ptr_plugin_handler->plugin_type == PLUGIN_PERL) wee_perl_exec (ptr_plugin_handler->function_name, arguments); #endif + + /* command executed */ + return 1; } } #else /* make gcc happy */ - (void) command; + (void) user_command; + (void) arguments; #endif + + /* no command executed */ + return 0; } /* - * plugins_end: shutdown plugin interface + * plugin_end: shutdown plugin interface */ void -plugins_end () +plugin_end () { - plugins_msg_handlers_free_all (); + plugin_handler_free_all (&plugin_msg_handlers, &last_plugin_msg_handler); + plugin_handler_free_all (&plugin_cmd_handlers, &last_plugin_cmd_handler); #ifdef PLUGIN_PERL wee_perl_end(); diff --git a/src/plugins/plugins.h b/src/plugins/plugins.h index aa1553f1e..9c9fea300 100644 --- a/src/plugins/plugins.h +++ b/src/plugins/plugins.h @@ -33,18 +33,28 @@ typedef struct t_plugin_handler t_plugin_handler; struct t_plugin_handler { int plugin_type; /* plugin type (Perl, Python, Ruby) */ - char *name; /* name (message or command) */ + char *name; /* name of IRC command (PRIVMSG, ..) + or command (without first '/') */ char *function_name; /* name of function (handler) */ t_plugin_handler *prev_handler; /* link to previous handler */ t_plugin_handler *next_handler; /* link to next handler */ }; +extern t_plugin_handler *plugin_msg_handlers; +extern t_plugin_handler *last_plugin_msg_handler; -extern void plugins_init (); -extern void plugins_load (int, char *); -extern void plugins_unload (int, char *); -extern void plugins_msg_handler_add (int, char *, char *); -extern void plugins_event_msg (char *, char *); -extern void plugins_end (); +extern t_plugin_handler *plugin_cmd_handlers; +extern t_plugin_handler *last_plugin_cmd_handler; + + +extern void plugin_init (); +extern void plugin_load (int, char *); +extern void plugin_unload (int, char *); +extern t_plugin_handler *plugin_handler_search (t_plugin_handler *, char *); +extern void plugin_handler_add (t_plugin_handler **, t_plugin_handler **, + int, char *, char *); +extern void plugin_event_msg (char *, char *); +extern int plugin_exec_command (char *, char *); +extern void plugin_end (); #endif /* plugins.h */ |