From fe4ca016156b01a1c66410dcecce5a970aa55bd3 Mon Sep 17 00:00:00 2001 From: Sebastien Helleu Date: Sun, 16 Nov 2003 23:46:48 +0000 Subject: [PATCH] Added Perl handlers --- po/fr.po | 34 ++++++++--- po/weechat.pot | 25 ++++++-- src/common/weeconfig.c | 2 +- src/common/weeconfig.h | 2 +- src/plugins/perl/wee-perl.c | 93 ++++++++++++++++++++++------- src/plugins/perl/wee-perl.h | 1 + src/plugins/plugins.c | 83 +++++++++++++++++++++++++ src/plugins/plugins.h | 17 +++++- weechat/po/fr.po | 34 ++++++++--- weechat/po/weechat.pot | 25 ++++++-- weechat/src/common/weeconfig.c | 2 +- weechat/src/common/weeconfig.h | 2 +- weechat/src/plugins/perl/wee-perl.c | 93 ++++++++++++++++++++++------- weechat/src/plugins/perl/wee-perl.h | 1 + weechat/src/plugins/plugins.c | 83 +++++++++++++++++++++++++ weechat/src/plugins/plugins.h | 17 +++++- 16 files changed, 434 insertions(+), 80 deletions(-) diff --git a/po/fr.po b/po/fr.po index da0f914f6..cf67666bc 100644 --- a/po/fr.po +++ b/po/fr.po @@ -5,9 +5,9 @@ # msgid "" msgstr "" -"Project-Id-Version: 0.0.2\n" +"Project-Id-Version: WeeChat 0.0.4-pre1\n" "Report-Msgid-Bugs-To: flashcode@flashtux.org\n" -"POT-Creation-Date: 2003-11-16 20:00+0100\n" +"POT-Creation-Date: 2003-11-16 21:36+0100\n" "PO-Revision-Date: 2003-10-04 21:21+0200\n" "Last-Translator: FlashCode \n" "Language-Team: weechat-dev \n" @@ -1471,23 +1471,38 @@ msgid "" "server!\n" msgstr "" "%s: tous les noms d'utilisateurs déclarés sont déjà en cours d'utilisation, " -"fermeture de la connexion avec server!\n" +"fermeture de la connexion avec le serveur !\n" -#: src/plugins/perl/wee-perl.c:73 +#: src/plugins/perl/wee-perl.c:69 #, c-format -msgid "%s unable to load Perl script \"%s\"\n" -msgstr "%s impossible de charger le script Perl \"%s\"\n" +msgid "registered Perl script: \"%s\", version %s (%s)\n" +msgstr "script Perl enregistré: \"%s\", version %s (%s)\n" -#: src/plugins/perl/wee-perl.c:201 +#: src/plugins/perl/wee-perl.c:74 +#, c-format +msgid "%s unable to load Perl script \"%s\" (not enough memory)\n" +msgstr "%s impossible de charger le script Perl \"%s\" (mémoire insuffisante)\n" + +#: src/plugins/perl/wee-perl.c:206 #, c-format msgid "Perl error: %s\n" msgstr "Erreur Perl: %s\n" -#: src/plugins/perl/wee-perl.c:210 +#: src/plugins/perl/wee-perl.c:215 #, c-format msgid "Perl error: too much values from \"%s\" (%d). Expected: 1.\n" msgstr "Erreur Perl: trop de valeurs de \"%s\" (%d). Attendue: 1.\n" +#: src/plugins/perl/wee-perl.c:237 +#, c-format +msgid "loading Perl script \"%s\"\n" +msgstr "chargement du script Perl \"%s\"\n" + +#: src/plugins/perl/wee-perl.c:250 +#, c-format +msgid "unloading Perl script \"%s\"\n" +msgstr "déchargement du script Perl \"%s\"\n" + #: src/gui/curses/gui-display.c:863 msgid "[not connected] " msgstr "[non connecté] " @@ -1819,7 +1834,8 @@ msgid "" "Please rebuild WeeChat with \"--enable-perl\" option for ./configure script\n" msgstr "" "WeeChat a été construit sans le support Perl.\n" -"Merci de reconstruire WeeChat avec l'option \"--enable-perl\" pour le script ./configure\n" +"Merci de reconstruire WeeChat avec l'option \"--enable-perl\" pour le " +"script ./configure\n" #: src/common/command.c:1062 msgid "Server: " diff --git a/po/weechat.pot b/po/weechat.pot index aa6d1f638..acae85fd8 100644 --- a/po/weechat.pot +++ b/po/weechat.pot @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: flashcode@flashtux.org\n" -"POT-Creation-Date: 2003-11-16 20:00+0100\n" +"POT-Creation-Date: 2003-11-16 21:36+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -1375,21 +1375,36 @@ msgid "" "server!\n" msgstr "" -#: src/plugins/perl/wee-perl.c:73 +#: src/plugins/perl/wee-perl.c:69 +#, c-format +msgid "registered Perl script \"%s\", version %s (%s)\n" +msgstr "" + +#: src/plugins/perl/wee-perl.c:74 #, c-format msgid "%s unable to load Perl script \"%s\"\n" msgstr "" -#: src/plugins/perl/wee-perl.c:201 +#: src/plugins/perl/wee-perl.c:206 #, c-format msgid "Perl error: %s\n" msgstr "" -#: src/plugins/perl/wee-perl.c:210 +#: src/plugins/perl/wee-perl.c:215 #, c-format msgid "Perl error: too much values from \"%s\" (%d). Expected: 1.\n" msgstr "" +#: src/plugins/perl/wee-perl.c:237 +#, c-format +msgid "loading Perl script \"%s\"\n" +msgstr "" + +#: src/plugins/perl/wee-perl.c:250 +#, c-format +msgid "unloading Perl script \"%s\"\n" +msgstr "" + #: src/gui/curses/gui-display.c:863 msgid "[not connected] " msgstr "" @@ -1480,7 +1495,7 @@ msgstr "" #: src/common/command.c:65 msgid "" -"filename: Perl script (file) to load/unload\n" +"filename: Perl script (file) to load\n" "scriptname: name of script to unload\n" "Without argument, /perl command lists all loaded Perl scripts." msgstr "" diff --git a/src/common/weeconfig.c b/src/common/weeconfig.c index 062735d50..6a5464f5f 100644 --- a/src/common/weeconfig.c +++ b/src/common/weeconfig.c @@ -19,7 +19,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* config.c: WeeChat configuration */ +/* weeconfig.c: WeeChat configuration */ #ifdef HAVE_CONFIG_H diff --git a/src/common/weeconfig.h b/src/common/weeconfig.h index 692698797..993cb66c1 100644 --- a/src/common/weeconfig.h +++ b/src/common/weeconfig.h @@ -154,4 +154,4 @@ extern int config_read (); extern int config_create_default (); extern int config_write (); -#endif /* config.h */ +#endif /* weeconfig.h */ diff --git a/src/plugins/perl/wee-perl.c b/src/plugins/perl/wee-perl.c index 709f575ad..f0d105843 100644 --- a/src/plugins/perl/wee-perl.c +++ b/src/plugins/perl/wee-perl.c @@ -32,11 +32,14 @@ #include #include "../../common/weechat.h" #include "wee-perl.h" +#include "../plugins.h" #include "../../gui/gui.h" static PerlInterpreter *my_perl = NULL; -static t_perl_script *perl_scripts = NULL; + +t_perl_script *perl_scripts = NULL; +t_perl_script *last_perl_script = NULL; extern void boot_DynaLoader (pTHX_ CV* cv); @@ -64,13 +67,22 @@ static XS (XS_IRC_register) new_perl_script->version = strdup (version); new_perl_script->shutdown_func = strdup (shutdown_func); new_perl_script->description = strdup (description); - new_perl_script->next_script = perl_scripts; - perl_scripts = new_perl_script; - wee_log_printf (_("registered Perl script: \"%s\"\n"), name); + + /* add new script to list */ + new_perl_script->prev_script = last_perl_script; + new_perl_script->next_script = NULL; + if (perl_scripts) + last_perl_script->next_script = new_perl_script; + else + perl_scripts = new_perl_script; + last_perl_script = new_perl_script; + + wee_log_printf (_("registered Perl script: \"%s\", version %s (%s)\n"), + name, version, description); } else gui_printf (NULL, - _("%s unable to load Perl script \"%s\"\n"), + _("%s unable to load Perl script \"%s\" (not enough memory)\n"), WEECHAT_ERROR, name); XST_mPV (0, VERSION); XSRETURN (1); @@ -95,6 +107,22 @@ static XS (XS_IRC_print) XSRETURN_EMPTY; } +/* + * IRC::add_message_handler: add handler for messages (privmsg, ...) + */ + +static XS (XS_IRC_add_message_handler) +{ + char *name, *function; + int integer; + dXSARGS; + + name = SvPV (ST (0), integer); + function = SvPV (ST (1), integer); + plugins_msg_handler_add (PLUGIN_PERL, name, function); + XSRETURN_EMPTY; +} + /* * xs_init: initialize subroutines */ @@ -105,6 +133,7 @@ xs_init (pTHX) newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, __FILE__); 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"); } /* @@ -237,6 +266,42 @@ wee_perl_load (char *filename) return wee_perl_exec ("wee_perl_load_eval_file", filename); } +/* + * wee_perl_script_free: free a Perl script + */ + +void +wee_perl_script_free (t_perl_script *ptr_perl_script) +{ + t_perl_script *new_perl_scripts; + + /* remove script from list */ + if (last_perl_script == ptr_perl_script) + last_perl_script = ptr_perl_script->prev_script; + if (ptr_perl_script->prev_script) + { + (ptr_perl_script->prev_script)->next_script = ptr_perl_script->next_script; + new_perl_scripts = perl_scripts; + } + else + new_perl_scripts = ptr_perl_script->next_script; + + if (ptr_perl_script->next_script) + (ptr_perl_script->next_script)->prev_script = ptr_perl_script->prev_script; + + /* free data */ + if (ptr_perl_script->name) + free (ptr_perl_script->name); + if (ptr_perl_script->version) + free (ptr_perl_script->version); + if (ptr_perl_script->shutdown_func) + free (ptr_perl_script->shutdown_func); + if (ptr_perl_script->description) + free (ptr_perl_script->description); + free (ptr_perl_script); + perl_scripts = new_perl_scripts; +} + /* * wee_perl_unload: unload a Perl script */ @@ -252,16 +317,7 @@ wee_perl_unload (t_perl_script *ptr_perl_script) /* call shutdown callback function */ if (ptr_perl_script->shutdown_func[0]) wee_perl_exec (ptr_perl_script->shutdown_func, ""); - - /* free data */ - if (ptr_perl_script->name) - free (ptr_perl_script->name); - if (ptr_perl_script->version) - free (ptr_perl_script->version); - if (ptr_perl_script->shutdown_func) - free (ptr_perl_script->shutdown_func); - if (ptr_perl_script->description) - free (ptr_perl_script->description); + wee_perl_script_free (ptr_perl_script); } } @@ -272,15 +328,8 @@ wee_perl_unload (t_perl_script *ptr_perl_script) void wee_perl_unload_all () { - t_perl_script *ptr_perl_script; - while (perl_scripts) - { wee_perl_unload (perl_scripts); - ptr_perl_script = perl_scripts->next_script; - free (perl_scripts); - perl_scripts = ptr_perl_script; - } } /* diff --git a/src/plugins/perl/wee-perl.h b/src/plugins/perl/wee-perl.h index 31a40481b..5898285ca 100644 --- a/src/plugins/perl/wee-perl.h +++ b/src/plugins/perl/wee-perl.h @@ -31,6 +31,7 @@ struct t_perl_script char *version; /* version of script */ char *shutdown_func; /* function when script ends */ char *description; /* description of script */ + t_perl_script *prev_script; /* link to previous Perl script */ t_perl_script *next_script; /* link to next Perl script */ }; diff --git a/src/plugins/plugins.c b/src/plugins/plugins.c index 50b8e60ef..f7cde6d6d 100644 --- a/src/plugins/plugins.c +++ b/src/plugins/plugins.c @@ -27,13 +27,22 @@ #endif #include +#include +#include "../common/weechat.h" #include "plugins.h" +#include "../gui/gui.h" #ifdef PLUGIN_PERL #include "perl/wee-perl.h" #endif +t_plugin_handler *plugins_msg_handlers = NULL; +t_plugin_handler *last_plugin_msg_handler = NULL; +t_plugin_handler *plugins_cmd_handlers = NULL; +t_plugin_handler *last_plugin_cmd_handler = NULL; + + /* * plugins_init: initialize all plugins */ @@ -92,6 +101,78 @@ plugins_unload (int plugin_type, char *scriptname) } } +/* + * plugins_msg_handler_add: add a message handler + */ + +void +plugins_msg_handler_add (int plugin_type, char *message, char *function) +{ + t_plugin_handler *new_plugin_handler; + + new_plugin_handler = (t_plugin_handler *)malloc (sizeof (t_plugin_handler)); + if (new_plugin_handler) + { + new_plugin_handler->plugin_type = plugin_type; + new_plugin_handler->name = strdup (message); + new_plugin_handler->function_name = strdup (function); + + /* add new handler to list */ + new_plugin_handler->prev_handler = last_plugin_msg_handler; + new_plugin_handler->next_handler = NULL; + if (plugins_msg_handlers) + last_plugin_msg_handler->next_handler = new_plugin_handler; + else + plugins_msg_handlers = new_plugin_handler; + last_plugin_msg_handler = new_plugin_handler; + } + else + gui_printf (NULL, + _("%s unable to add handler for \"%s\" message (not enough memory)\n"), + WEECHAT_ERROR, message); +} + +/* + * plugins_msg_handler_free: free message handler + */ + +void +plugins_msg_handler_free (t_plugin_handler *ptr_plugin_handler) +{ + t_plugin_handler *new_plugins_msg_handlers; + + /* remove handler from list */ + if (last_plugin_msg_handler == ptr_plugin_handler) + last_plugin_msg_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; + } + else + new_plugins_msg_handlers = ptr_plugin_handler->next_handler; + + if (ptr_plugin_handler->next_handler) + (ptr_plugin_handler->next_handler)->prev_handler = ptr_plugin_handler->prev_handler; + + /* free data */ + free (ptr_plugin_handler->name); + free (ptr_plugin_handler->function_name); + free (ptr_plugin_handler); + plugins_msg_handlers = new_plugins_msg_handlers; +} + +/* + * plugins_remove_all_msg_handlers: remove all message handlers + */ + +void +plugins_msg_handlers_free_all () +{ + while (plugins_msg_handlers) + plugins_msg_handler_free (plugins_msg_handlers); +} + /* * plugins_end: shutdown plugin interface */ @@ -99,6 +180,8 @@ plugins_unload (int plugin_type, char *scriptname) void plugins_end () { + plugins_msg_handlers_free_all (); + #ifdef PLUGIN_PERL wee_perl_end(); #endif diff --git a/src/plugins/plugins.h b/src/plugins/plugins.h index fcd6309f7..68ea44c27 100644 --- a/src/plugins/plugins.h +++ b/src/plugins/plugins.h @@ -20,17 +20,30 @@ */ -#ifndef __WEECHAT_PLUGIN_H -#define __WEECHAT_PLUGIN_H 1 +#ifndef __WEECHAT_PLUGINS_H +#define __WEECHAT_PLUGINS_H 1 #define PLUGIN_UNKNOWN 0 #define PLUGIN_PERL 1 #define PLUGIN_PYTHON 2 #define PLUGIN_RUBY 3 +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 *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 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_end (); #endif /* plugins.h */ diff --git a/weechat/po/fr.po b/weechat/po/fr.po index da0f914f6..cf67666bc 100644 --- a/weechat/po/fr.po +++ b/weechat/po/fr.po @@ -5,9 +5,9 @@ # msgid "" msgstr "" -"Project-Id-Version: 0.0.2\n" +"Project-Id-Version: WeeChat 0.0.4-pre1\n" "Report-Msgid-Bugs-To: flashcode@flashtux.org\n" -"POT-Creation-Date: 2003-11-16 20:00+0100\n" +"POT-Creation-Date: 2003-11-16 21:36+0100\n" "PO-Revision-Date: 2003-10-04 21:21+0200\n" "Last-Translator: FlashCode \n" "Language-Team: weechat-dev \n" @@ -1471,23 +1471,38 @@ msgid "" "server!\n" msgstr "" "%s: tous les noms d'utilisateurs déclarés sont déjà en cours d'utilisation, " -"fermeture de la connexion avec server!\n" +"fermeture de la connexion avec le serveur !\n" -#: src/plugins/perl/wee-perl.c:73 +#: src/plugins/perl/wee-perl.c:69 #, c-format -msgid "%s unable to load Perl script \"%s\"\n" -msgstr "%s impossible de charger le script Perl \"%s\"\n" +msgid "registered Perl script: \"%s\", version %s (%s)\n" +msgstr "script Perl enregistré: \"%s\", version %s (%s)\n" -#: src/plugins/perl/wee-perl.c:201 +#: src/plugins/perl/wee-perl.c:74 +#, c-format +msgid "%s unable to load Perl script \"%s\" (not enough memory)\n" +msgstr "%s impossible de charger le script Perl \"%s\" (mémoire insuffisante)\n" + +#: src/plugins/perl/wee-perl.c:206 #, c-format msgid "Perl error: %s\n" msgstr "Erreur Perl: %s\n" -#: src/plugins/perl/wee-perl.c:210 +#: src/plugins/perl/wee-perl.c:215 #, c-format msgid "Perl error: too much values from \"%s\" (%d). Expected: 1.\n" msgstr "Erreur Perl: trop de valeurs de \"%s\" (%d). Attendue: 1.\n" +#: src/plugins/perl/wee-perl.c:237 +#, c-format +msgid "loading Perl script \"%s\"\n" +msgstr "chargement du script Perl \"%s\"\n" + +#: src/plugins/perl/wee-perl.c:250 +#, c-format +msgid "unloading Perl script \"%s\"\n" +msgstr "déchargement du script Perl \"%s\"\n" + #: src/gui/curses/gui-display.c:863 msgid "[not connected] " msgstr "[non connecté] " @@ -1819,7 +1834,8 @@ msgid "" "Please rebuild WeeChat with \"--enable-perl\" option for ./configure script\n" msgstr "" "WeeChat a été construit sans le support Perl.\n" -"Merci de reconstruire WeeChat avec l'option \"--enable-perl\" pour le script ./configure\n" +"Merci de reconstruire WeeChat avec l'option \"--enable-perl\" pour le " +"script ./configure\n" #: src/common/command.c:1062 msgid "Server: " diff --git a/weechat/po/weechat.pot b/weechat/po/weechat.pot index aa6d1f638..acae85fd8 100644 --- a/weechat/po/weechat.pot +++ b/weechat/po/weechat.pot @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: flashcode@flashtux.org\n" -"POT-Creation-Date: 2003-11-16 20:00+0100\n" +"POT-Creation-Date: 2003-11-16 21:36+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -1375,21 +1375,36 @@ msgid "" "server!\n" msgstr "" -#: src/plugins/perl/wee-perl.c:73 +#: src/plugins/perl/wee-perl.c:69 +#, c-format +msgid "registered Perl script \"%s\", version %s (%s)\n" +msgstr "" + +#: src/plugins/perl/wee-perl.c:74 #, c-format msgid "%s unable to load Perl script \"%s\"\n" msgstr "" -#: src/plugins/perl/wee-perl.c:201 +#: src/plugins/perl/wee-perl.c:206 #, c-format msgid "Perl error: %s\n" msgstr "" -#: src/plugins/perl/wee-perl.c:210 +#: src/plugins/perl/wee-perl.c:215 #, c-format msgid "Perl error: too much values from \"%s\" (%d). Expected: 1.\n" msgstr "" +#: src/plugins/perl/wee-perl.c:237 +#, c-format +msgid "loading Perl script \"%s\"\n" +msgstr "" + +#: src/plugins/perl/wee-perl.c:250 +#, c-format +msgid "unloading Perl script \"%s\"\n" +msgstr "" + #: src/gui/curses/gui-display.c:863 msgid "[not connected] " msgstr "" @@ -1480,7 +1495,7 @@ msgstr "" #: src/common/command.c:65 msgid "" -"filename: Perl script (file) to load/unload\n" +"filename: Perl script (file) to load\n" "scriptname: name of script to unload\n" "Without argument, /perl command lists all loaded Perl scripts." msgstr "" diff --git a/weechat/src/common/weeconfig.c b/weechat/src/common/weeconfig.c index 062735d50..6a5464f5f 100644 --- a/weechat/src/common/weeconfig.c +++ b/weechat/src/common/weeconfig.c @@ -19,7 +19,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* config.c: WeeChat configuration */ +/* weeconfig.c: WeeChat configuration */ #ifdef HAVE_CONFIG_H diff --git a/weechat/src/common/weeconfig.h b/weechat/src/common/weeconfig.h index 692698797..993cb66c1 100644 --- a/weechat/src/common/weeconfig.h +++ b/weechat/src/common/weeconfig.h @@ -154,4 +154,4 @@ extern int config_read (); extern int config_create_default (); extern int config_write (); -#endif /* config.h */ +#endif /* weeconfig.h */ diff --git a/weechat/src/plugins/perl/wee-perl.c b/weechat/src/plugins/perl/wee-perl.c index 709f575ad..f0d105843 100644 --- a/weechat/src/plugins/perl/wee-perl.c +++ b/weechat/src/plugins/perl/wee-perl.c @@ -32,11 +32,14 @@ #include #include "../../common/weechat.h" #include "wee-perl.h" +#include "../plugins.h" #include "../../gui/gui.h" static PerlInterpreter *my_perl = NULL; -static t_perl_script *perl_scripts = NULL; + +t_perl_script *perl_scripts = NULL; +t_perl_script *last_perl_script = NULL; extern void boot_DynaLoader (pTHX_ CV* cv); @@ -64,13 +67,22 @@ static XS (XS_IRC_register) new_perl_script->version = strdup (version); new_perl_script->shutdown_func = strdup (shutdown_func); new_perl_script->description = strdup (description); - new_perl_script->next_script = perl_scripts; - perl_scripts = new_perl_script; - wee_log_printf (_("registered Perl script: \"%s\"\n"), name); + + /* add new script to list */ + new_perl_script->prev_script = last_perl_script; + new_perl_script->next_script = NULL; + if (perl_scripts) + last_perl_script->next_script = new_perl_script; + else + perl_scripts = new_perl_script; + last_perl_script = new_perl_script; + + wee_log_printf (_("registered Perl script: \"%s\", version %s (%s)\n"), + name, version, description); } else gui_printf (NULL, - _("%s unable to load Perl script \"%s\"\n"), + _("%s unable to load Perl script \"%s\" (not enough memory)\n"), WEECHAT_ERROR, name); XST_mPV (0, VERSION); XSRETURN (1); @@ -95,6 +107,22 @@ static XS (XS_IRC_print) XSRETURN_EMPTY; } +/* + * IRC::add_message_handler: add handler for messages (privmsg, ...) + */ + +static XS (XS_IRC_add_message_handler) +{ + char *name, *function; + int integer; + dXSARGS; + + name = SvPV (ST (0), integer); + function = SvPV (ST (1), integer); + plugins_msg_handler_add (PLUGIN_PERL, name, function); + XSRETURN_EMPTY; +} + /* * xs_init: initialize subroutines */ @@ -105,6 +133,7 @@ xs_init (pTHX) newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, __FILE__); 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"); } /* @@ -237,6 +266,42 @@ wee_perl_load (char *filename) return wee_perl_exec ("wee_perl_load_eval_file", filename); } +/* + * wee_perl_script_free: free a Perl script + */ + +void +wee_perl_script_free (t_perl_script *ptr_perl_script) +{ + t_perl_script *new_perl_scripts; + + /* remove script from list */ + if (last_perl_script == ptr_perl_script) + last_perl_script = ptr_perl_script->prev_script; + if (ptr_perl_script->prev_script) + { + (ptr_perl_script->prev_script)->next_script = ptr_perl_script->next_script; + new_perl_scripts = perl_scripts; + } + else + new_perl_scripts = ptr_perl_script->next_script; + + if (ptr_perl_script->next_script) + (ptr_perl_script->next_script)->prev_script = ptr_perl_script->prev_script; + + /* free data */ + if (ptr_perl_script->name) + free (ptr_perl_script->name); + if (ptr_perl_script->version) + free (ptr_perl_script->version); + if (ptr_perl_script->shutdown_func) + free (ptr_perl_script->shutdown_func); + if (ptr_perl_script->description) + free (ptr_perl_script->description); + free (ptr_perl_script); + perl_scripts = new_perl_scripts; +} + /* * wee_perl_unload: unload a Perl script */ @@ -252,16 +317,7 @@ wee_perl_unload (t_perl_script *ptr_perl_script) /* call shutdown callback function */ if (ptr_perl_script->shutdown_func[0]) wee_perl_exec (ptr_perl_script->shutdown_func, ""); - - /* free data */ - if (ptr_perl_script->name) - free (ptr_perl_script->name); - if (ptr_perl_script->version) - free (ptr_perl_script->version); - if (ptr_perl_script->shutdown_func) - free (ptr_perl_script->shutdown_func); - if (ptr_perl_script->description) - free (ptr_perl_script->description); + wee_perl_script_free (ptr_perl_script); } } @@ -272,15 +328,8 @@ wee_perl_unload (t_perl_script *ptr_perl_script) void wee_perl_unload_all () { - t_perl_script *ptr_perl_script; - while (perl_scripts) - { wee_perl_unload (perl_scripts); - ptr_perl_script = perl_scripts->next_script; - free (perl_scripts); - perl_scripts = ptr_perl_script; - } } /* diff --git a/weechat/src/plugins/perl/wee-perl.h b/weechat/src/plugins/perl/wee-perl.h index 31a40481b..5898285ca 100644 --- a/weechat/src/plugins/perl/wee-perl.h +++ b/weechat/src/plugins/perl/wee-perl.h @@ -31,6 +31,7 @@ struct t_perl_script char *version; /* version of script */ char *shutdown_func; /* function when script ends */ char *description; /* description of script */ + t_perl_script *prev_script; /* link to previous Perl script */ t_perl_script *next_script; /* link to next Perl script */ }; diff --git a/weechat/src/plugins/plugins.c b/weechat/src/plugins/plugins.c index 50b8e60ef..f7cde6d6d 100644 --- a/weechat/src/plugins/plugins.c +++ b/weechat/src/plugins/plugins.c @@ -27,13 +27,22 @@ #endif #include +#include +#include "../common/weechat.h" #include "plugins.h" +#include "../gui/gui.h" #ifdef PLUGIN_PERL #include "perl/wee-perl.h" #endif +t_plugin_handler *plugins_msg_handlers = NULL; +t_plugin_handler *last_plugin_msg_handler = NULL; +t_plugin_handler *plugins_cmd_handlers = NULL; +t_plugin_handler *last_plugin_cmd_handler = NULL; + + /* * plugins_init: initialize all plugins */ @@ -92,6 +101,78 @@ plugins_unload (int plugin_type, char *scriptname) } } +/* + * plugins_msg_handler_add: add a message handler + */ + +void +plugins_msg_handler_add (int plugin_type, char *message, char *function) +{ + t_plugin_handler *new_plugin_handler; + + new_plugin_handler = (t_plugin_handler *)malloc (sizeof (t_plugin_handler)); + if (new_plugin_handler) + { + new_plugin_handler->plugin_type = plugin_type; + new_plugin_handler->name = strdup (message); + new_plugin_handler->function_name = strdup (function); + + /* add new handler to list */ + new_plugin_handler->prev_handler = last_plugin_msg_handler; + new_plugin_handler->next_handler = NULL; + if (plugins_msg_handlers) + last_plugin_msg_handler->next_handler = new_plugin_handler; + else + plugins_msg_handlers = new_plugin_handler; + last_plugin_msg_handler = new_plugin_handler; + } + else + gui_printf (NULL, + _("%s unable to add handler for \"%s\" message (not enough memory)\n"), + WEECHAT_ERROR, message); +} + +/* + * plugins_msg_handler_free: free message handler + */ + +void +plugins_msg_handler_free (t_plugin_handler *ptr_plugin_handler) +{ + t_plugin_handler *new_plugins_msg_handlers; + + /* remove handler from list */ + if (last_plugin_msg_handler == ptr_plugin_handler) + last_plugin_msg_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; + } + else + new_plugins_msg_handlers = ptr_plugin_handler->next_handler; + + if (ptr_plugin_handler->next_handler) + (ptr_plugin_handler->next_handler)->prev_handler = ptr_plugin_handler->prev_handler; + + /* free data */ + free (ptr_plugin_handler->name); + free (ptr_plugin_handler->function_name); + free (ptr_plugin_handler); + plugins_msg_handlers = new_plugins_msg_handlers; +} + +/* + * plugins_remove_all_msg_handlers: remove all message handlers + */ + +void +plugins_msg_handlers_free_all () +{ + while (plugins_msg_handlers) + plugins_msg_handler_free (plugins_msg_handlers); +} + /* * plugins_end: shutdown plugin interface */ @@ -99,6 +180,8 @@ plugins_unload (int plugin_type, char *scriptname) void plugins_end () { + plugins_msg_handlers_free_all (); + #ifdef PLUGIN_PERL wee_perl_end(); #endif diff --git a/weechat/src/plugins/plugins.h b/weechat/src/plugins/plugins.h index fcd6309f7..68ea44c27 100644 --- a/weechat/src/plugins/plugins.h +++ b/weechat/src/plugins/plugins.h @@ -20,17 +20,30 @@ */ -#ifndef __WEECHAT_PLUGIN_H -#define __WEECHAT_PLUGIN_H 1 +#ifndef __WEECHAT_PLUGINS_H +#define __WEECHAT_PLUGINS_H 1 #define PLUGIN_UNKNOWN 0 #define PLUGIN_PERL 1 #define PLUGIN_PYTHON 2 #define PLUGIN_RUBY 3 +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 *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 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_end (); #endif /* plugins.h */