1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-28 05:46:38 +02:00

Added Python plugin support

This commit is contained in:
Sebastien Helleu
2005-05-01 18:53:23 +00:00
parent dca4064502
commit eda55fbdb4
30 changed files with 3548 additions and 622 deletions
+142 -3
View File
@@ -77,6 +77,11 @@ t_weechat_command weechat_commands[] =
N_("filename: Perl script (file) to load\n"
"Without argument, /perl command lists all loaded Perl scripts."),
0, 2, weechat_cmd_perl, NULL },
{ "python", N_("list/load/unload Python scripts"),
N_("[load filename] | [autoload] | [unload]"),
N_("filename: Python script (file) to load\n"
"Without argument, /python command lists all loaded Python scripts."),
0, 2, weechat_cmd_python, NULL },
{ "server", N_("list, add or remove servers"),
N_("[servername] | "
"[servername hostname port [-auto | -noauto] [-pwd password] [-nicks nick1 "
@@ -1286,13 +1291,12 @@ weechat_cmd_help (int argc, char **argv)
int
weechat_cmd_perl (int argc, char **argv)
{
#ifdef PLUGINS
#ifdef PLUGIN_PERL
t_plugin_script *ptr_plugin_script;
t_plugin_handler *ptr_plugin_handler;
int handler_found, path_length;
char *path_script;
#ifdef PLUGIN_PERL
switch (argc)
{
case 0:
@@ -1418,11 +1422,146 @@ weechat_cmd_perl (int argc, char **argv)
(void) argv;
#endif /* PLUGIN_PERL */
return 0;
}
/*
* weechat_cmd_python: list/load/unload Python scripts
*/
int
weechat_cmd_python (int argc, char **argv)
{
#ifdef PLUGIN_PYTHON
t_plugin_script *ptr_plugin_script;
t_plugin_handler *ptr_plugin_handler;
int handler_found, path_length;
char *path_script;
switch (argc)
{
case 0:
/* list registered Python scripts */
gui_printf (NULL, "\n");
gui_printf (NULL, _("Registered Python scripts:\n"));
if (python_scripts)
{
for (ptr_plugin_script = python_scripts; ptr_plugin_script;
ptr_plugin_script = ptr_plugin_script->next_script)
{
irc_display_prefix (NULL, PREFIX_PLUGIN);
gui_printf (NULL, " %s v%s%s%s\n",
ptr_plugin_script->name,
ptr_plugin_script->version,
(ptr_plugin_script->description[0]) ? " - " : "",
ptr_plugin_script->description);
}
}
else
{
irc_display_prefix (NULL, PREFIX_PLUGIN);
gui_printf (NULL, _(" (none)\n"));
}
/* list Python message handlers */
gui_printf (NULL, "\n");
gui_printf (NULL, _("Python message handlers:\n"));
handler_found = 0;
for (ptr_plugin_handler = plugin_msg_handlers; ptr_plugin_handler;
ptr_plugin_handler = ptr_plugin_handler->next_handler)
{
if (ptr_plugin_handler->plugin_type == PLUGIN_TYPE_PYTHON)
{
handler_found = 1;
irc_display_prefix (NULL, PREFIX_PLUGIN);
gui_printf (NULL, _(" IRC(%s) => Python(%s)\n"),
ptr_plugin_handler->name,
ptr_plugin_handler->function_name);
}
}
if (!handler_found)
{
irc_display_prefix (NULL, PREFIX_PLUGIN);
gui_printf (NULL, _(" (none)\n"));
}
/* list Python command handlers */
gui_printf (NULL, "\n");
gui_printf (NULL, _("Python command handlers:\n"));
handler_found = 0;
for (ptr_plugin_handler = plugin_cmd_handlers; ptr_plugin_handler;
ptr_plugin_handler = ptr_plugin_handler->next_handler)
{
if (ptr_plugin_handler->plugin_type == PLUGIN_TYPE_PYTHON)
{
handler_found = 1;
irc_display_prefix (NULL, PREFIX_PLUGIN);
gui_printf (NULL, _(" Command /%s => Python(%s)\n"),
ptr_plugin_handler->name,
ptr_plugin_handler->function_name);
}
}
if (!handler_found)
{
irc_display_prefix (NULL, PREFIX_PLUGIN);
gui_printf (NULL, _(" (none)\n"));
}
break;
case 1:
if (strcasecmp (argv[0], "autoload") == 0)
plugin_auto_load (PLUGIN_TYPE_PYTHON, "python/autoload");
if (strcasecmp (argv[0], "unload") == 0)
{
/* unload all Python scripts */
plugin_unload (PLUGIN_TYPE_PYTHON, NULL);
irc_display_prefix (NULL, PREFIX_PLUGIN);
gui_printf (NULL, _("Python scripts unloaded\n"));
}
break;
case 2:
if (strcasecmp (argv[0], "load") == 0)
{
/* load Python script */
if (strstr(argv[1], DIR_SEPARATOR))
path_script = NULL;
else
{
path_length = strlen (weechat_home) + strlen (argv[1]) + 7;
path_script = (char *) malloc (path_length * sizeof (char));
snprintf (path_script, path_length, "%s%s%s%s%s",
weechat_home, DIR_SEPARATOR, "python",
DIR_SEPARATOR, argv[1]);
}
plugin_load (PLUGIN_TYPE_PYTHON,
(path_script) ? path_script : argv[1]);
if (path_script)
free (path_script);
}
else
{
irc_display_prefix (NULL, PREFIX_ERROR);
gui_printf (NULL,
_("%s unknown option for \"%s\" command\n"),
WEECHAT_ERROR, "python");
}
break;
default:
irc_display_prefix (NULL, PREFIX_ERROR);
gui_printf (NULL,
_("%s wrong argument count for \"%s\" command\n"),
WEECHAT_ERROR, "python");
}
#else
irc_display_prefix (NULL, PREFIX_ERROR);
gui_printf (NULL,
_("WeeChat was build without Python support.\n"
"Please rebuild WeeChat with "
"\"--enable-python\" option for ./configure script\n"));
/* make gcc happy */
(void) argc;
(void) argv;
#endif /* PLUGINS */
#endif /* PLUGIN_PYTHON */
return 0;
}
+1
View File
@@ -70,6 +70,7 @@ extern int weechat_cmd_debug (int, char **);
extern int weechat_cmd_disconnect (int, char **);
extern int weechat_cmd_help (int, char **);
extern int weechat_cmd_perl (int, char **);
extern int weechat_cmd_python (int, char **);
extern int weechat_cmd_save (int, char **);
extern int weechat_cmd_server (int, char **);
extern int weechat_cmd_set (char *);
+14
View File
@@ -207,6 +207,20 @@ completion_build_list (t_completion *completion, void *channel)
"unload");
return;
}
if ((strcasecmp (completion->base_command, "python") == 0)
&& (completion->base_command_arg == 1))
{
weelist_add (&completion->completion_list,
&completion->last_completion,
"load");
weelist_add (&completion->completion_list,
&completion->last_completion,
"autoload");
weelist_add (&completion->completion_list,
&completion->last_completion,
"unload");
return;
}
if ((strcasecmp (completion->base_command, "set") == 0)
&& (completion->base_command_arg == 1))
{
+4 -4
View File
@@ -21,16 +21,16 @@ if PLUGIN_PERL
perl_dir = perl
endif
# if PLUGIN_PYTHON
# python_dir = python
# endif
if PLUGIN_PYTHON
python_dir = python
endif
# if PLUGIN_RUBY
# ruby_dir = ruby
# endif
# SUBDIRS = $(perl_dir) $(python_dir) $(ruby_dir)
SUBDIRS = $(perl_dir)
SUBDIRS = $(perl_dir) $(python_dir)
noinst_LIBRARIES = lib_weechat_plugins.a
+28 -2
View File
@@ -39,6 +39,10 @@
#include "perl/wee-perl.h"
#endif
#ifdef PLUGIN_PYTHON
#include "python/wee-python.h"
#endif
char *plugin_name[3] = { "Perl", "Python", "Ruby" };
@@ -115,6 +119,11 @@ plugin_init ()
wee_perl_init();
plugin_auto_load (PLUGIN_TYPE_PERL, "perl/autoload");
#endif
#ifdef PLUGIN_PYTHON
wee_python_init();
plugin_auto_load (PLUGIN_TYPE_PYTHON, "python/autoload");
#endif
}
/*
@@ -133,7 +142,9 @@ plugin_load (int plugin_type, char *filename)
#endif
break;
case PLUGIN_TYPE_PYTHON:
/* TODO: load Python script */
#ifdef PLUGIN_PYTHON
wee_python_load (filename);
#endif
break;
case PLUGIN_TYPE_RUBY:
/* TODO: load Ruby script */
@@ -292,6 +303,10 @@ plugin_event_msg (char *irc_command, char *arguments, char *server)
if (ptr_plugin_handler->plugin_type == PLUGIN_TYPE_PERL)
wee_perl_exec (ptr_plugin_handler->function_name, arguments, server);
#endif
#ifdef PLUGIN_PYTHON
if (ptr_plugin_handler->plugin_type == PLUGIN_TYPE_PYTHON)
wee_python_exec (ptr_plugin_handler->function_name, arguments, server);
#endif
}
}
#else
@@ -321,6 +336,10 @@ plugin_exec_command (char *user_command, char *arguments, char *server)
if (ptr_plugin_handler->plugin_type == PLUGIN_TYPE_PERL)
wee_perl_exec (ptr_plugin_handler->function_name, arguments, server);
#endif
#ifdef PLUGIN_PYTHON
if (ptr_plugin_handler->plugin_type == PLUGIN_TYPE_PYTHON)
wee_python_exec (ptr_plugin_handler->function_name, arguments, server);
#endif
/* command executed */
return 1;
@@ -358,7 +377,10 @@ plugin_unload (int plugin_type, char *scriptname)
#endif
break;
case PLUGIN_TYPE_PYTHON:
/* TODO: unload Python scripts */
#ifdef PLUGIN_PYTHON
wee_python_end ();
wee_python_init ();
#endif
break;
case PLUGIN_TYPE_RUBY:
/* TODO: unload Ruby scripts */
@@ -383,4 +405,8 @@ plugin_end ()
#ifdef PLUGIN_PERL
wee_perl_end();
#endif
#ifdef PLUGIN_PYTHON
wee_python_end();
#endif
}
+23
View File
@@ -0,0 +1,23 @@
# Copyright (c) 2003-2005 FlashCode <flashcode@flashtux.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
INCLUDES = -DLOCALEDIR=\"$(datadir)/locale\" $(PYTHON_CFLAGS)
noinst_LIBRARIES = lib_weechat_python.a
lib_weechat_python_a_SOURCES = wee-python.h \
wee-python.c
+713
View File
@@ -0,0 +1,713 @@
/*
* Copyright (c) 2003-2005 by FlashCode <flashcode@flashtux.org>
* See README for License detail, AUTHORS for developers list.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* wee-python.c: Python plugin support for WeeChat */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <Python.h>
#include <stdlib.h>
#include <string.h>
#undef _
#include "../../common/weechat.h"
#include "../plugins.h"
#include "wee-python.h"
#include "../../common/command.h"
#include "../../irc/irc.h"
#include "../../gui/gui.h"
t_plugin_script *python_scripts = NULL;
t_plugin_script *last_python_script = NULL;
/*
* weechat.register(nom, version, fonction, description):
* startup function for all WeeChat Python scripts
*/
static PyObject *
wee_python_register (PyObject *self, PyObject *args)
{
char *name, *version, *shutdown_func, *description;
t_plugin_script *ptr_python_script, *python_script_found, *new_python_script;
/* make gcc happy */
(void) self;
if (!PyArg_ParseTuple (args, "ssss", &name, &version, &shutdown_func, &description))
{
irc_display_prefix (NULL, PREFIX_ERROR);
gui_printf (NULL,
_("Python error: wrong parameters for 'print_with_channel' Python function\n"));
return NULL;
}
python_script_found = NULL;
for (ptr_python_script = python_scripts; ptr_python_script;
ptr_python_script = ptr_python_script->next_script)
{
if (strcasecmp (ptr_python_script->name, name) == 0)
{
python_script_found = ptr_python_script;
break;
}
}
if (python_script_found)
{
/* error: another scripts already exists with this name! */
irc_display_prefix (NULL, PREFIX_ERROR);
gui_printf (NULL,
_("Python error: unable to register Python script \"%s\" (another script "
"already exists with this name)\n"),
name);
}
else
{
/* registering script */
new_python_script = (t_plugin_script *)malloc (sizeof (t_plugin_script));
if (new_python_script)
{
new_python_script->name = strdup (name);
new_python_script->version = strdup (version);
new_python_script->shutdown_func = strdup (shutdown_func);
new_python_script->description = strdup (description);
/* add new script to list */
new_python_script->prev_script = last_python_script;
new_python_script->next_script = NULL;
if (python_scripts)
last_python_script->next_script = new_python_script;
else
python_scripts = new_python_script;
last_python_script = new_python_script;
wee_log_printf (_("registered Python script: \"%s\", version %s (%s)\n"),
name, version, description);
}
else
{
irc_display_prefix (NULL, PREFIX_ERROR);
gui_printf (NULL,
_("%s unable to load Python script \"%s\" (not enough memory)\n"),
WEECHAT_ERROR, name);
}
}
Py_INCREF (Py_None);
return Py_None;
}
/*
* weechat.print(message): print message to current buffer
*/
static PyObject *
wee_python_print (PyObject *self, PyObject *args)
{
char *message;
/* make gcc happy */
(void) self;
if (!PyArg_ParseTuple (args, "s", &message))
{
irc_display_prefix (NULL, PREFIX_ERROR);
gui_printf (NULL,
_("Python error: wrong parameters for 'print' Python function\n"));
return NULL;
}
irc_display_prefix (gui_current_window->buffer, PREFIX_PLUGIN);
gui_printf (gui_current_window->buffer, "%s\n", message);
Py_INCREF (Py_None);
return Py_None;
}
/*
* weechat.print_with_channel(message, channel, server=None):
* print message to a specific channel/server
* (server is optional)
*/
static PyObject *
wee_python_print_with_channel (PyObject *self, PyObject *args)
{
char *message, *channel, *server = NULL;
int ret = 0;
t_gui_buffer *ptr_buffer;
t_irc_server *ptr_server;
t_irc_channel *ptr_channel;
/* make gcc happy */
(void) self;
if (!PyArg_ParseTuple (args, "ss|s", &message, &channel, &server))
{
irc_display_prefix (NULL, PREFIX_ERROR);
gui_printf (NULL,
_("Python error: wrong parameters for 'print_with_channel' Python function\n"));
return NULL;
}
ptr_buffer = NULL;
for (ptr_server = irc_servers; ptr_server;
ptr_server = ptr_server->next_server)
{
if (!server || (strcasecmp (ptr_server->name, server)) == 0)
{
for (ptr_channel = ptr_server->channels; ptr_channel;
ptr_channel = ptr_channel->next_channel)
{
if (strcasecmp (ptr_channel->name, channel) == 0)
{
ptr_buffer = ptr_channel->buffer;
break;
}
}
}
if (ptr_buffer)
break;
}
/* buffer found => display message & return 1 ~= True */
if (ptr_buffer)
{
irc_display_prefix (ptr_buffer, PREFIX_PLUGIN);
gui_printf (ptr_buffer, "%s", message);
ret = 1;
}
return Py_BuildValue ("i", ret);
}
/*
* weechat.print_infobar(delay, message): print message to infobar
*/
static PyObject *
wee_python_print_infobar (PyObject *self, PyObject *args)
{
int delay;
char *message;
/* make gcc happy */
(void) self;
if (!PyArg_ParseTuple (args, "is", &delay, &message))
{
irc_display_prefix (NULL, PREFIX_ERROR);
gui_printf (NULL,
_("Python error: wrong parameters for 'print_infobar' Python function\n"));
return NULL;
}
gui_infobar_printf (delay, COLOR_WIN_INFOBAR, message);
Py_INCREF (Py_None);
return Py_None;
}
/*
* weechat.command(command, server=None): send command to server
*/
static PyObject *
wee_python_command (PyObject *self, PyObject *args)
{
char *server = NULL, *command, *command2;
t_irc_server *ptr_server;
/* make gcc happy */
(void) self;
if (!PyArg_ParseTuple (args, "s|s", &command, &server))
{
irc_display_prefix (NULL, PREFIX_ERROR);
gui_printf (NULL,
_("Python error: wrong parameters for 'command' Python function\n"));
return NULL;
}
if (server == NULL)
{
ptr_server = SERVER(gui_current_window->buffer);
}
else
{
for (ptr_server = irc_servers; ptr_server; ptr_server = ptr_server->next_server)
{
if (strcasecmp (ptr_server->name, server) == 0)
break;
}
if (!ptr_server)
{
irc_display_prefix (NULL, PREFIX_ERROR);
gui_printf (NULL,
_("Python error: server not found for 'command' Python function\n"));
}
}
if (ptr_server)
{
command2 = (char *) malloc (strlen (command) + 8);
strcpy (command2, command);
if (!strstr (command2, "\r\n"))
strcat (command2, "\r\n");
server_sendf (ptr_server, command2);
free (command2);
}
Py_INCREF (Py_None);
return Py_None;
}
/*
* weechat.add_message_handler(message, function):
* add handler for messages
*/
static PyObject *
wee_python_add_message_handler (PyObject *self, PyObject *args)
{
char *message, *function;
/* make gcc happy */
(void) self;
if (!PyArg_ParseTuple (args, "ss", &message, &function))
{
irc_display_prefix (NULL, PREFIX_ERROR);
gui_printf (NULL,
_("Python error: wrong parameters for 'add_message_handler' Python function\n"));
return NULL;
}
plugin_handler_add (&plugin_msg_handlers, &last_plugin_msg_handler,
PLUGIN_TYPE_PYTHON, message, function);
Py_INCREF (Py_None);
return Py_None;
}
/*
* weechat.add_command_handler(name, function):
* define/redefines commands
*/
static PyObject *
wee_python_add_command_handler(PyObject *self, PyObject *args)
{
char *name, *function;
t_plugin_handler *ptr_plugin_handler;
/* make gcc happy */
(void) self;
if (!PyArg_ParseTuple (args, "ss", &name, &function))
{
irc_display_prefix (NULL, PREFIX_ERROR);
gui_printf (NULL,
_("Python error: wrong parameters for 'add_command_handler' Python function\n"));
return NULL;
}
if (!weelist_search (index_commands, name))
weelist_add (&index_commands, &last_index_command, name);
ptr_plugin_handler = plugin_handler_search (plugin_cmd_handlers, name);
if (ptr_plugin_handler)
{
free (ptr_plugin_handler->function_name);
ptr_plugin_handler->function_name = strdup (function);
}
else
plugin_handler_add (&plugin_cmd_handlers, &last_plugin_cmd_handler,
PLUGIN_TYPE_PYTHON, name, function);
Py_INCREF (Py_None);
return Py_None;
}
/*
* weechat.get_info(info, server=None):
* get various infos
*/
static PyObject *
wee_python_get_info (PyObject *self, PyObject *args)
{
char *arg, *info = NULL, *server = NULL;
t_irc_server *ptr_server;
/* make gcc happy */
(void) self;
if (!PyArg_ParseTuple (args, "s|s", &arg, &server))
{
irc_display_prefix (NULL, PREFIX_ERROR);
gui_printf (NULL,
_("Python error: wrong parameters for 'get_info' Python function\n"));
return NULL;
}
if (server == NULL)
{
ptr_server = SERVER(gui_current_window->buffer);
}
else
{
for (ptr_server = irc_servers; ptr_server; ptr_server = ptr_server->next_server)
{
if (strcasecmp (ptr_server->name, server) == 0)
break;
}
if (!ptr_server)
{
irc_display_prefix (NULL, PREFIX_ERROR);
gui_printf (NULL,
_("Python error: server not found for 'get_info' Python function\n"));
}
}
if (ptr_server && arg)
{
if ( (strcasecmp (arg, "0") == 0) || (strcasecmp (arg, "version") == 0) )
{
info = PACKAGE_STRING;
}
else if ( (strcasecmp (arg, "1") == 0) || (strcasecmp (arg, "nick") == 0) )
{
if (ptr_server->nick)
info = ptr_server->nick;
}
else if ( (strcasecmp (arg, "2") == 0) || (strcasecmp (arg, "channel") == 0) )
{
if (BUFFER_IS_CHANNEL (gui_current_window->buffer))
info = CHANNEL (gui_current_window->buffer)->name;
}
else if ( (strcasecmp (arg, "3") == 0) || (strcasecmp (arg, "server") == 0) )
{
if (ptr_server->name)
info = ptr_server->name;
}
else if ( (strcasecmp (arg, "4") == 0) || (strcasecmp (arg, "weechatdir") == 0) )
{
info = weechat_home;
}
else if ( (strcasecmp (arg, "5") == 0) || (strcasecmp (arg, "away") == 0) )
{
return Py_BuildValue ("i", SERVER(gui_current_window->buffer)->is_away);
}
if (info)
return Py_BuildValue ("s", info);
else
return Py_BuildValue ("s", "");
}
return Py_BuildValue ("i", 1);
}
/*
* initialize subroutines
*/
static
PyMethodDef weechat_funcs[] = {
{"register", wee_python_register, METH_VARARGS, ""},
{"prnt", wee_python_print, METH_VARARGS, ""},
{"print_with_channel", wee_python_print_with_channel, METH_VARARGS, ""},
{"print_infobar", wee_python_print_infobar, METH_VARARGS, ""},
{"command", wee_python_command, METH_VARARGS, ""},
{"add_message_handler", wee_python_add_message_handler, METH_VARARGS, ""},
{"add_command_handler", wee_python_add_command_handler, METH_VARARGS, ""},
{"get_info", wee_python_get_info, METH_VARARGS, ""},
{NULL, NULL, 0, NULL}
};
/*
* wee_python_init: initialize Python interface for WeeChat
*/
void
wee_python_init ()
{
Py_Initialize ();
if (Py_IsInitialized () == 0)
{
irc_display_prefix (NULL, PREFIX_PLUGIN);
gui_printf (NULL, _("Python error: error while launching Python interpreter\n"));
}
else
{
Py_InitModule ("weechat", weechat_funcs);
irc_display_prefix (NULL, PREFIX_PLUGIN);
gui_printf (NULL, _("Loading Python module \"weechat\"\n"));
}
}
/*
* wee_python_search: search a (loaded) Python script by name
*/
t_plugin_script *
wee_python_search (char *name)
{
t_plugin_script *ptr_python_script;
for (ptr_python_script = python_scripts; ptr_python_script;
ptr_python_script = ptr_python_script->next_script)
{
if (strcmp (ptr_python_script->name, name) == 0)
return ptr_python_script;
}
/* script not found */
return NULL;
}
/*
* wee_python_exec: execute a Python script
*/
int
wee_python_exec (char *function, char *server, char *arguments)
{
int len, return_code, i, j, alen;
char *srv, *args, *runstring;
return_code = 1;
if (arguments == NULL)
{
alen = 0;
args = strdup ("");
}
else
{
alen = (int) strlen (arguments);
args = (char *) malloc ( 2 * alen * sizeof (*args));
}
if (!args)
{
irc_display_prefix (NULL, PREFIX_ERROR);
gui_printf (NULL,
_("Python error: unable to run function \"%s\"in Python script (not enough memory)\n"),
function);
return 0;
}
// copy arguments in args to escape double quotes
j = 0;
for (i=0; i < alen; ++i)
{
if (arguments[i] == '"' || arguments[i] == '\\')
{
args[j] = '\\';
++j;
}
args[j] = arguments[i];
++j;
}
args[j] = 0;
if (server == NULL)
srv = strdup ("");
else
srv = strdup (server);
if (!srv)
{
irc_display_prefix (NULL, PREFIX_ERROR);
gui_printf (NULL,
_("Python error: unable to run function \"%s\"in Python script (not enough memory)\n"),
function);
free (args);
return 0;
}
len = (int) strlen (function);
len += (int) strlen ("(\"\",\"\")");
len += (int) strlen (srv);
len += (int) strlen (args);
len += 1;
runstring = (char *) malloc ( len * sizeof (*runstring));
if (runstring)
{
sprintf (runstring, "%s(\"%s\",\"%s\")", function, srv, args);
if (PyRun_SimpleString (runstring) != 0)
{
irc_display_prefix (NULL, PREFIX_ERROR);
gui_printf (NULL,
_("Python error: error while running function \"%s\"\n"), function);
return_code = 0;
}
free (runstring);
}
else
{
irc_display_prefix (NULL, PREFIX_ERROR);
gui_printf (NULL,
_("Python error: unable to run function \"%s\"in Python script (not enough memory)\n"),
function);
return_code = 0;
}
free (args);
free (srv);
return return_code;
}
/*
* wee_python_load: load a Python script
*/
int
wee_python_load (char *filename)
{
FILE *fp;
/* execute Python script */
wee_log_printf (_("loading Python script \"%s\"\n"), filename);
irc_display_prefix (NULL, PREFIX_PLUGIN);
gui_printf (NULL, _("Loading Python script \"%s\"\n"), filename);
if ((fp = fopen (filename, "r")) == NULL)
{
irc_display_prefix (NULL, PREFIX_ERROR);
gui_printf (NULL,
_("Python error: error while opening file \"%s\"\n"), filename);
return 1;
}
if (PyRun_SimpleFile (fp, filename) != 0)
{
irc_display_prefix (NULL, PREFIX_ERROR);
gui_printf (NULL,
_("Python error: error while parsing file \"%s\"\n"), filename);
return 1;
}
fclose (fp);
return 0;
}
/*
* wee_python_script_free: free a Python script
*/
void
wee_python_script_free (t_plugin_script *ptr_python_script)
{
t_plugin_script *new_python_scripts;
/* remove script from list */
if (last_python_script == ptr_python_script)
last_python_script = ptr_python_script->prev_script;
if (ptr_python_script->prev_script)
{
(ptr_python_script->prev_script)->next_script = ptr_python_script->next_script;
new_python_scripts = python_scripts;
}
else
new_python_scripts = ptr_python_script->next_script;
if (ptr_python_script->next_script)
(ptr_python_script->next_script)->prev_script = ptr_python_script->prev_script;
/* free data */
if (ptr_python_script->name)
free (ptr_python_script->name);
if (ptr_python_script->version)
free (ptr_python_script->version);
if (ptr_python_script->shutdown_func)
free (ptr_python_script->shutdown_func);
if (ptr_python_script->description)
free (ptr_python_script->description);
free (ptr_python_script);
python_scripts = new_python_scripts;
}
/*
* wee_python_unload: unload a Python script
*/
void
wee_python_unload (t_plugin_script *ptr_python_script)
{
if (ptr_python_script)
{
wee_log_printf (_("unloading Python script \"%s\"\n"),
ptr_python_script->name);
/* call shutdown callback function */
if (ptr_python_script->shutdown_func[0])
wee_python_exec (ptr_python_script->shutdown_func, "", "");
wee_python_script_free (ptr_python_script);
}
}
/*
* wee_python_unload_all: unload all Python scripts
*/
void
wee_python_unload_all ()
{
wee_log_printf (_("unloading all Python scripts...\n"));
while (python_scripts)
wee_python_unload (python_scripts);
}
/*
* wee_python_end: shutdown Python interface
*/
void
wee_python_end ()
{
/* unload all scripts */
wee_python_unload_all ();
/* free all handlers */
plugin_handler_free_all_type (&plugin_msg_handlers,
&last_plugin_msg_handler,
PLUGIN_TYPE_PYTHON);
plugin_handler_free_all_type (&plugin_cmd_handlers,
&last_plugin_cmd_handler,
PLUGIN_TYPE_PYTHON);
Py_Finalize ();
if (Py_IsInitialized () != 0) {
irc_display_prefix (NULL, PREFIX_PLUGIN);
gui_printf (NULL, _("Python error: error while freeing Python interpreter\n"));
}
}
+34
View File
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2003-2005 by FlashCode <flashcode@flashtux.org>
* See README for License detail, AUTHORS for developers list.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __WEECHAT_PYTHON_H
#define __WEECHAT_PYTHON_H 1
#include "../plugins.h"
extern void wee_python_init ();
extern t_plugin_script *wee_python_search (char *);
extern int wee_python_exec (char *, char *, char *);
extern int wee_python_load (char *);
extern void wee_python_unload (t_plugin_script *);
extern void wee_python_unload_all ();
extern void wee_python_end ();
#endif /* wee-python.h */