1 /* mod-python.c - Script module for x3
2 * Copyright 2003-2004 Martijn Smit and srvx Development Team
3 * Copyright 2005-2006 X3 Development Team
5 * This file is part of x3.
7 * x3 is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with srvx; if not, write to the Free Software Foundation,
19 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 #ifdef WITH_PYTHON /* just disable this file if python doesnt exist */
39 * - Implement most of proto-p10 irc_* commands for calling from scripts
40 * - Implement functions to look up whois, channel, account, and reg-channel info for scripts
41 * - Implement x3.conf settings for python variables like include path, etc.
42 * - modpython.py calls for everything you can reg_ a handler for in x3
43 * - Some kind of system for getting needed binds bound automagicaly to make it easier
44 * to run peoples' scripts and mod-python in general.
45 * - An interface to reading/writing data to x3.db. Maybe generic, or attached to account or channel reg records?
48 static const struct message_entry msgtab
[] = {
49 { "PYMSG_RELOAD_SUCCESS", "Reloaded Python scripts successfully." },
50 { "PYMSG_RELOAD_FAILED", "Error reloading Python scripts." },
51 { "PYMSG_RUN_UNKNOWN_EXCEPTION", "Error running python: unknown exception." },
52 { "PYMSG_RUN_EXCEPTION", "Error running python: %s: %s." },
53 { NULL
, NULL
} /* sentinel */
56 #define MODPYTHON_CONF_NAME "modules/python"
60 char const* scripts_dir
;
61 char const* main_module
;
64 static struct log_type
*PY_LOG
;
65 const char *python_module_deps
[] = { NULL
};
66 static struct module *python_module
;
68 PyObject
*base_module
= NULL
; /* Base python handling library */
69 PyObject
*handler_object
= NULL
; /* instance of handler class */
72 extern struct userNode
*global
, *chanserv
, *opserv
, *nickserv
, *spamserv
;
75 Some hooks you can call from modpython.py to interact with the
76 service. These emb_* functions are available as _svc.* in python. */
78 struct _tuple_dict_extra
{
83 static int _dict_iter_get_users(char const* key
, UNUSED_ARG(void* data
), void* extra
) {
84 struct _tuple_dict_extra
* real_extra
= (struct _tuple_dict_extra
*)extra
;
86 PyTuple_SetItem(real_extra
->data
, *(int*)real_extra
->extra
,
87 PyString_FromString(key
));
88 *real_extra
->extra
= *real_extra
->extra
+ 1;
92 static int _dict_iter_get_channels(char const* key
, UNUSED_ARG(void* data
), void* extra
) {
93 struct _tuple_dict_extra
* real_extra
= (struct _tuple_dict_extra
*)extra
;
95 PyTuple_SetItem(real_extra
->data
, *(int*)real_extra
->extra
,
96 PyString_FromString(key
));
97 *real_extra
->extra
= *real_extra
->extra
+ 1;
101 /* get a tuple with all users in it */
103 emb_get_users(UNUSED_ARG(PyObject
*self
), PyObject
*args
) {
105 size_t num_clients
, n
= 0;
106 struct _tuple_dict_extra extra
;
108 if (!PyArg_ParseTuple(args
, ""))
111 num_clients
= dict_size(clients
);
112 retval
= PyTuple_New(num_clients
);
117 dict_foreach(clients
, _dict_iter_get_users
, (void*)&extra
);
122 /* get a tuple with all channels in it */
124 emb_get_channels(UNUSED_ARG(PyObject
* self
), PyObject
* args
) {
126 size_t num_channels
, n
= 0;
127 struct _tuple_dict_extra extra
;
129 if (!PyArg_ParseTuple(args
, ""))
132 num_channels
= dict_size(channels
);
133 retval
= PyTuple_New(num_channels
);
138 dict_foreach(channels
, _dict_iter_get_channels
, (void*)&extra
);
144 emb_dump(UNUSED_ARG(PyObject
*self
), PyObject
*args
)
146 /* Dump a raw string into the socket
147 usage: _svc.dump(<P10 string>)
151 char linedup
[MAXLEN
];
154 if(!PyArg_ParseTuple(args
, "s:dump", &buf
))
157 safestrncpy(linedup
, buf
, sizeof(linedup
));
159 if(parse_line(linedup
, 1)) {
163 PyErr_SetString(PyExc_Exception
, "invalid protocol message");
167 return Py_BuildValue("i", ret
);
171 emb_send_target_privmsg(UNUSED_ARG(PyObject
*self
), PyObject
*args
)
174 usage: _svc.send_target_privmsg(<servicenick_from>, <nick_to>, <message>)
181 struct service
*service
;
184 if(!PyArg_ParseTuple(args
, "sss:reply", &servicenick
, &channel
, &buf
))
187 if (buf
== NULL
|| strlen(buf
) == 0) {
188 PyErr_SetString(PyExc_Exception
, "invalid empty message");
192 if(!(service
= service_find(servicenick
))) {
193 PyErr_SetString(PyExc_Exception
, "no such service nick");
197 ret
= send_target_message(5, channel
, service
->bot
, "%s", buf
);
198 return Py_BuildValue("i", ret
);
202 emb_send_target_notice(UNUSED_ARG(PyObject
*self
), PyObject
*args
)
205 usage: _svc.send_target_notice(<servicenick_from>, <nick_to>, <message>)
212 struct service
*service
;
214 if(!PyArg_ParseTuple(args
, "sss:reply", &servicenick
, &target
, &buf
))
217 if (buf
== NULL
|| strlen(buf
) == 0) {
218 PyErr_SetString(PyExc_Exception
, "invalid empty message");
222 if(!(service
= service_find(servicenick
))) {
223 PyErr_SetString(PyExc_Exception
, "no such service nick");
227 ret
= send_target_message(4, target
, service
->bot
, "%s", buf
);
229 return Py_BuildValue("i", ret
);
233 pyobj_from_usernode(struct userNode
* user
) {
236 PyObject
* pChanList
= PyTuple_New(user
->channels
.used
);
238 for (n
=0; n
< user
->channels
.used
; n
++) {
239 mn
= user
->channels
.list
[n
];
240 PyTuple_SetItem(pChanList
, n
, Py_BuildValue("s", mn
->channel
->name
));
243 return Py_BuildValue("{"
247 "s: s, " /* hostname */
249 "s: s, " /* fakehost */
250 "s: s, " /* sethost */
251 "s: s, " /* crypthost */
252 "s: s, " /* cryptip */
253 #ifdef WITH_PROTOCOL_P10
254 "s: s, " /* numeric */
255 #endif /* WITH_PROTOCOL_P10 */
257 "s: i, " /* no_notice */
259 "s: s, " /* version_reply */
260 "s: s, " /* account */
261 "s: O}", /* channels */
263 "ident", user
->ident
,
265 "hostname", user
->hostname
,
266 "ip", irc_ntoa(&user
->ip
),
267 "fakehost", user
->fakehost
,
268 "sethost", user
->sethost
,
269 "crypthost", user
->crypthost
,
270 "cryptip", user
->cryptip
,
271 #ifdef WITH_PROTOCOL_P10
272 "numeric", user
->numeric
,
273 #endif /* WITH_PROTOCOL_P10 */
275 "no_notice", user
->no_notice
,
277 "version_reply", user
->version_reply
,
278 "account", user
->handle_info
? user
->handle_info
->handle
: NULL
,
279 "channels", pChanList
);
283 emb_get_user(UNUSED_ARG(PyObject
*self
), PyObject
*args
)
285 /* Get a python object containing everything x3 knows about a user, by nick.
286 usage: _svc.get_user(<nick>)
289 struct userNode
*user
;
291 if(!PyArg_ParseTuple(args
, "s", &nick
))
294 if(!(user
= GetUserH(nick
))) {
295 PyErr_SetString(PyExc_Exception
, "no such user");
299 return pyobj_from_usernode(user
);
303 emb_get_channel(UNUSED_ARG(PyObject
*self
), PyObject
*args
)
305 /* Returns a python dict object with all sorts of info about a channel.
306 usage: _svc.get_channel(<name>)
309 struct chanNode
*channel
;
311 PyObject
*pChannelMembers
;
312 PyObject
*pChannelBans
;
313 PyObject
*pChannelExempts
;
316 if(!PyArg_ParseTuple(args
, "s", &name
))
319 if(!(channel
= GetChannel(name
))) {
320 PyErr_SetString(PyExc_Exception
, "unknown channel");
324 /* build tuple of nicks in channel */
325 pChannelMembers
= PyTuple_New(channel
->members
.used
);
326 for(n
=0;n
< channel
->members
.used
;n
++) {
327 struct modeNode
*mn
= channel
->members
.list
[n
];
328 PyTuple_SetItem(pChannelMembers
, n
, Py_BuildValue("s", mn
->user
->nick
));
331 /* build tuple of bans */
332 pChannelBans
= PyTuple_New(channel
->banlist
.used
);
333 for(n
=0; n
< channel
->banlist
.used
;n
++) {
334 struct banNode
*bn
= channel
->banlist
.list
[n
];
335 PyTuple_SetItem(pChannelBans
, n
,
336 Py_BuildValue("{s:s,s:s,s:i}",
343 /* build tuple of exempts */
344 pChannelExempts
= PyTuple_New(channel
->exemptlist
.used
);
345 for(n
=0; n
< channel
->exemptlist
.used
;n
++) {
346 struct exemptNode
*en
= channel
->exemptlist
.list
[n
];
347 PyTuple_SetItem(pChannelExempts
, n
,
348 Py_BuildValue("{s:s,s:s,s:i}",
355 return Py_BuildValue("{s:s,s:s,s:s,s:i"
356 ",s:i,s:i,s:O,s:O,s:O}",
358 "name", channel
->name
,
359 "topic", channel
->topic
,
360 "topic_nick", channel
->topic_nick
,
361 "topic_time", channel
->topic_time
,
363 "timestamp", channel
->timestamp
,
364 "modes", channel
->modes
,
365 "members", pChannelMembers
,
366 "bans", pChannelBans
,
367 "exempts", pChannelExempts
372 emb_get_account(UNUSED_ARG(PyObject
*self
), PyObject
*args
)
374 /* Returns a python dict object with all sorts of info about an account.
375 usage: _svc.get_account(<account name>)
378 struct handle_info
*hi
;
381 if(!PyArg_ParseTuple(args
, "s", &name
))
384 hi
= get_handle_info(name
);
387 PyErr_SetString(PyExc_Exception
, "unknown account name");
391 return Py_BuildValue("{s:s,s:i,s:s,s:s,s:s"
394 "account", hi
->handle
,
395 "registered", hi
->registered
,
396 "last_seen", hi
->lastseen
,
397 "infoline", hi
->infoline
? hi
->infoline
: "",
398 "email", hi
->email_addr
? hi
->email_addr
: "",
400 "fakehost", hi
->fakehost
? hi
->fakehost
: "",
401 "last_quit_host", hi
->last_quit_host
404 /* users online authed to this account */
406 /* nicks (nickserv nets only?) */
414 emb_get_info(UNUSED_ARG(PyObject
*self
), UNUSED_ARG(PyObject
*args
))
416 /* return some info about the general setup
417 * of X3, such as what the chanserv's nickname
422 return Py_BuildValue("{s:s,s:s,s:s,s:s,s:s}",
423 "chanserv", chanserv
? chanserv
->nick
: "ChanServ",
424 "nickserv", nickserv
?nickserv
->nick
: "NickServ",
425 "opserv", opserv
?opserv
->nick
: "OpServ",
426 "global", global
?global
->nick
: "Global",
427 "spamserv", spamserv
?spamserv
->nick
: "SpamServ");
431 emb_log_module(UNUSED_ARG(PyObject
*self
), PyObject
*args
)
433 /* a gateway to standard X3 logging subsystem.
434 * level is a value 0 to 9 as defined by the log_severity enum in log.h.
436 * for now, all logs go to the PY_LOG log. In the future this will change.
443 if(!PyArg_ParseTuple(args
, "is", &level
, &message
))
446 log_module(PY_LOG
, level
, "%s", message
);
448 return Py_BuildValue("i", ret
);
451 static PyMethodDef EmbMethods
[] = {
452 /* Communication methods */
453 {"dump", emb_dump
, METH_VARARGS
, "Dump raw P10 line to server"},
454 {"send_target_privmsg", emb_send_target_privmsg
, METH_VARARGS
, "Send a message to somewhere"},
455 {"send_target_notice", emb_send_target_notice
, METH_VARARGS
, "Send a notice to somewhere"},
456 {"log_module", emb_log_module
, METH_VARARGS
, "Log something using the X3 log subsystem"},
457 //TODO: {"exec_cmd", emb_exec_cmd, METH_VARARGS, "execute x3 command provided"},
458 // This should use environment from "python command" call to pass in, if available
462 //TODO: {"gline", emb_gline, METH_VARARGS, "gline a mask"},
463 //TODO: {"ungline", emb_ungline, METH_VARARGS, "remove a gline"},
464 //TODO: {"kick", emb_kick, METH_VARARGS, "kick someone from a channel"},
465 //TODO: {"channel_mode", emb_channel_mode, METH_VARARGS, "set modes on a channel"},
466 //TODO: {"user_mode", emb_user_mode, METH_VARARGS, "Have x3 set usermodes on one of its own nicks"},
468 //TODO: {"get_config", emb_get_config, METH_VARARGS, "get x3.conf settings into a nested dict"},
469 //TODO: {"config_set", emb_config_set, METH_VARARGS, "change a config setting 'on-the-fly'."},
471 //TODO: {"timeq_add", emb_timeq_new, METH_VARARGS, "some kind of interface to the timed event system."},
472 //TODO: {"timeq_del", emb_timeq_new, METH_VARARGS, "some kind of interface to the timed event system."},
473 /* Information gathering methods */
474 {"get_user", emb_get_user
, METH_VARARGS
, "Get details about a nickname"},
475 {"get_users", emb_get_users
, METH_VARARGS
, "Get all connected users"},
476 {"get_channel", emb_get_channel
, METH_VARARGS
, "Get details about a channel"},
477 {"get_channels", emb_get_channels
, METH_VARARGS
, "Get all channels"},
478 {"get_account", emb_get_account
, METH_VARARGS
, "Get details about an account"},
479 {"get_info", emb_get_info
, METH_VARARGS
, "Get various misc info about x3"},
480 /* null terminator */
481 {NULL
, NULL
, 0, NULL
}
486 These functions set up the embedded environment for us to call out to
487 modpython.py class methods.
490 void python_log_module() {
491 /* Attempt to convert python errors to x3 log system */
492 PyObject
*exc
, *tb
, *value
, *tmp
;
493 char *str_exc
= "NONE";
494 char *str_value
= "NONE";
495 char *str_tb
= "NONE";
497 PyErr_Fetch(&exc
, &value
, &tb
);
500 if((tmp
= PyObject_Str(exc
)))
501 str_exc
= PyString_AsString(tmp
);
504 if((tmp
= PyObject_Str(value
)))
505 str_value
= PyString_AsString(tmp
);
508 if((tmp
= PyObject_Str(tb
)))
509 str_tb
= PyString_AsString(tmp
);
512 /* Now restore it so we can print it (just in case)
513 * (should we do this only when running in debug mode?) */
514 PyErr_Restore(exc
, value
, tb
);
515 PyErr_Print(); /* which of course, clears it again.. */
517 log_module(PY_LOG
, LOG_WARNING
, "PYTHON error: %s, value: %s", str_exc
, str_value
);
519 /* TODO: get the traceback using the traceback module via C api so we can add it to the X3 logs. See
520 * http://mail.python.org/pipermail/python-list/2003-March/192226.html */
521 // (this doesnt work, str_tb is just an object hashid) log_module(PY_LOG, LOG_INFO, "PYTHON error, traceback: %s", str_tb);
525 PyObject
*python_build_handler_args(size_t argc
, char *args
[], PyObject
*pIrcObj
) {
526 /* Sets up a python tuple with passed in arguments, prefixed by the Irc instance
527 which handlers use to interact with C.
528 argc = number of args
530 pIrcObj = instance of the irc class
533 PyObject
*pArgs
= NULL
;
535 pArgs
= PyTuple_New(argc
+ 1);
537 PyTuple_SetItem(pArgs
, i
++, pIrcObj
);
541 for(n
= 0; n
< argc
; ++n
) {
542 pValue
= PyString_FromString(args
[n
]);
545 log_module(PY_LOG
, LOG_INFO
, "Unable to convert '%s' to python string", args
[n
]);
548 PyTuple_SetItem(pArgs
, n
+i
, pValue
);
554 PyObject
*python_build_args(size_t argc
, char *args
[]) {
555 /* Builds the passed in arguments into a python argument tuple.
556 argc = number of args
560 PyObject
*pArgs
= NULL
;
563 pArgs
= PyTuple_New(argc
);
565 for(i
= 0; i
< argc
; ++i
) {
566 pValue
= PyString_FromString(args
[i
]);
569 log_module(PY_LOG
, LOG_INFO
, "Unable to convert '%s' to python string", args
[i
]);
572 PyTuple_SetItem(pArgs
, i
, pValue
);
579 PyObject
*new_irc_object(char *command_service
, char *command_caller
, char *command_target
) {
580 /* Creates a new instance of the irc class (from modpython.py) which is initalized
581 with current environment details like which service were talking to.
582 command_service = which service we are talking to, or empty string if none
583 command_caller = nick of user generating message, or empty string if none
584 command_target = If were reacting to something on a channel, this will
585 be set to the name of the channel. Otherwise empty
587 PyObject
*pIrcArgs
= NULL
;
591 log_module(PY_LOG
, LOG_INFO
, "Attempting to instanciate irc class; %s %s %s", command_service
, command_caller
, command_target
);
592 pIrcClass
= PyObject_GetAttrString(base_module
, "irc");
593 /* pIrcClass is a new reference */
594 if(pIrcClass
&& PyCallable_Check(pIrcClass
)) {
596 char *ircargs
[] = {command_service
, command_caller
, command_target
};
599 pIrcArgs
= python_build_args(3, ircargs
);
600 pIrcObj
= PyObject_CallObject(pIrcClass
, pIrcArgs
);
602 log_module(PY_LOG
, LOG_ERROR
, "IRC Class failed to load");
606 if(pIrcArgs
!= NULL
) {
609 Py_DECREF(pIrcClass
);
613 /* need to free pIrcClass here if it WAS found but was NOT callable? */
614 log_module(PY_LOG
, LOG_ERROR
, "Unable to find irc class");
619 int python_call_handler(char *handler
, char *args
[], size_t argc
, char *command_service
, char *command_caller
, char *command_target
) {
620 /* This is how we talk to modpython.c. First a new instance of the irc class is created using these
621 arguments to setup the current environment. Then the named method of the handler object is called
622 with the givin arguments.
629 log_module(PY_LOG
, LOG_INFO
, "attempting to call handler %s.", handler
);
630 if(base_module
!= NULL
&& handler_object
!= NULL
) {
631 pIrcObj
= new_irc_object(command_service
, command_caller
, command_target
);
633 log_module(PY_LOG
, LOG_INFO
, "Can't get irc object. Bailing.");
637 pArgs
= python_build_handler_args(argc
, args
, pIrcObj
);
638 pMethod
= PyObject_GetAttrString(handler_object
, handler
);
639 if(pMethod
&& PyCallable_Check(pMethod
)) {
640 /* Call the method, with the arguments */
641 pValue
= PyObject_CallObject(pMethod
, pArgs
);
647 ret
= PyInt_AsLong(pValue
);
648 if(ret
== -1 && PyErr_Occurred()) {
650 log_module(PY_LOG
, LOG_INFO
, "error converting return value of handler %s to type long. ", handler
);
654 log_module(PY_LOG
, LOG_INFO
, "handler %s was run successfully, returned %d.", handler
, ret
);
661 /* TODO: instead of print errors, get them as strings
662 * and deal with them with normal x3 log system. */
663 log_module(PY_LOG
, LOG_WARNING
, "call to handler %s failed", handler
);
671 else { /* couldn't find handler methed */
673 /* Free pMethod if it was found but not callable? */
674 log_module(PY_LOG
, LOG_ERROR
, "Cannot find handler %s.", handler
);
679 else { /* No base module.. no python? */
680 log_module(PY_LOG
, LOG_INFO
, "Cannot handle %s, Python is not initialized.", handler
);
685 PyObject
*python_new_handler_object() {
686 /* Create a new instance of the handler class.
687 This is called during python initilization (or reload)
688 and the result is saved and re-used.
690 PyObject
*pHandlerClass
, *pHandlerObj
;
692 log_module(PY_LOG
, LOG_INFO
, "Attempting to instanciate python class handler");
693 pHandlerClass
= PyObject_GetAttrString(base_module
, "handler");
694 /* Class is a new reference */
695 if(pHandlerClass
&& PyCallable_Check(pHandlerClass
)) {
696 /*PyObject *pValue; */
698 pHandlerObj
= PyObject_CallObject(pHandlerClass
, NULL
);
699 if(pHandlerObj
!= NULL
) {
700 log_module(PY_LOG
, LOG_INFO
, "Created new python handler object.");
704 log_module(PY_LOG
, LOG_ERROR
, "Unable to instanciate handler object");
711 log_module(PY_LOG
, LOG_ERROR
, "Unable to find handler class");
715 Py_DECREF(pHandlerClass
);
721 /* ------------------------------------------------------------------------------- *
722 Some gateway functions to convert x3 callbacks into modpython.py callbacks.
723 Mostly we just build relevant args and call the proper handler method
725 debate: do we just register these and check them in python
726 for every one (slow?) or actually work out if a plugin needs
727 it first? We will start by doing it every time.
730 python_handle_join(struct modeNode
*mNode
)
732 /* callback for handle_join events.
734 struct userNode
*user
= mNode
->user
;
735 struct chanNode
*channel
= mNode
->channel
;
738 log_module(PY_LOG
, LOG_INFO
, "python module handle_join");
739 if(!channel
||!user
) {
740 log_module(PY_LOG
, LOG_WARNING
, "Python code got join without channel or user!");
744 char *args
[] = {channel
->name
, user
->nick
};
745 return python_call_handler("join", args
, 2, "", "", "");
750 python_handle_server_link(struct server
*server
)
752 log_module(PY_LOG
, LOG_INFO
, "python module handle_server_link");
754 log_module(PY_LOG
, LOG_WARNING
, "Python code got server link without server!");
758 char *args
[] = {server
->name
, server
->description
};
759 return python_call_handler("server_link", args
, 2, "", "", "");
764 python_handle_new_user(struct userNode
*user
)
766 log_module(PY_LOG
, LOG_INFO
, "Python module handle_new_user");
768 log_module(PY_LOG
, LOG_WARNING
, "Python code got new_user without the user");
772 char *args
[] = {user
->nick
, user
->ident
, user
->hostname
, user
->info
};
773 return python_call_handler("new_user", args
, 4, "", "", "");
778 python_handle_nick_change(struct userNode
*user
, const char *old_nick
)
780 log_module(PY_LOG
, LOG_INFO
, "Python module handle_nick_change");
782 log_module(PY_LOG
, LOG_WARNING
, "Python code got nick_change without the user!");
785 char *args
[] = {user
->nick
, (char *)old_nick
};
786 python_call_handler("nick_change", args
, 2, "", "", "");
790 /* ----------------------------------------------------------------------------- */
794 /* Init the python engine and do init work on modpython.py
795 This is called during x3 startup, and on a python reload
799 char* env
= getenv("PYTHONPATH");
805 setenv("PYTHONPATH", modpython_conf
.scripts_dir
, 1);
806 else if (!strstr(env
, modpython_conf
.scripts_dir
)) {
807 buffer
= (char*)malloc(strlen(env
) + strlen(modpython_conf
.scripts_dir
) + 2);
808 sprintf(buffer
, "%s:%s", modpython_conf
.scripts_dir
, env
);
809 setenv("PYTHONPATH", buffer
, 1);
815 Py_InitModule("_svc", EmbMethods
);
816 pName
= PyString_FromString(modpython_conf
.main_module
);
817 base_module
= PyImport_Import(pName
);
820 Py_XDECREF(handler_object
);
821 handler_object
= NULL
;
823 if(base_module
!= NULL
) {
824 handler_object
= python_new_handler_object();
826 python_call_handler("init", NULL
, 0, "", "", "");
830 /* error handler class not found */
831 log_module(PY_LOG
, LOG_WARNING
, "Failed to create handler object");
838 log_module(PY_LOG
, LOG_WARNING
, "Failed to load modpython.py");
845 python_finalize(void) {
846 /* Called after X3 is fully up and running.
847 Code can be put here that needs to run to init things, but
848 which is sensitive to everything else in x3 being up and ready
852 PyRun_SimpleString("print 'Hello, World of Python!'");
853 log_module(PY_LOG
, LOG_INFO
, "python module finalize");
859 python_cleanup(void) {
860 /* Called on shutdown of the python module (or before reloading)
863 log_module(PY_LOG
, LOG_INFO
, "python module cleanup");
864 if (PyErr_Occurred())
866 Py_Finalize(); /* Shut down python enterpreter */
869 /* ---------------------------------------------------------------------------------- *
870 Python module command handlers.
872 static MODCMD_FUNC(cmd_reload
) {
873 /* reload the python system completely
875 log_module(PY_LOG
, LOG_INFO
, "Shutting python down");
877 log_module(PY_LOG
, LOG_INFO
, "Loading python stuff");
879 reply("PYMSG_RELOAD_SUCCESS");
882 reply("PYMSG_RELOAD_FAILED");
887 static char* format_python_error(int space_nls
) {
888 PyObject
* extype
= NULL
, *exvalue
= NULL
, *extraceback
= NULL
;
889 PyObject
* pextypestr
= NULL
, *pexvaluestr
= NULL
;
890 char* extypestr
= NULL
, *exvaluestr
= NULL
;
891 size_t retvallen
= 0;
892 char* retval
= NULL
, *tmp
;
894 PyErr_Fetch(&extype
, &exvalue
, &extraceback
);
898 pextypestr
= PyObject_Str(extype
);
901 extypestr
= PyString_AsString(pextypestr
);
905 pexvaluestr
= PyObject_Str(exvalue
);
907 exvaluestr
= PyString_AsString(pexvaluestr
);
909 retvallen
= strlen(extypestr
) + (exvaluestr
? strlen(exvaluestr
) + 2 : 0) + 1;
910 retval
= (char*)malloc(retvallen
);
912 snprintf(retval
, retvallen
, "%s: %s", extypestr
, exvaluestr
);
914 strncpy(retval
, extypestr
, retvallen
);
926 if (PyErr_Occurred())
927 PyErr_Clear(); /* ignore errors caused by formatting */
930 Py_XDECREF(extraceback
);
931 Py_XDECREF(pextypestr
);
932 Py_XDECREF(pexvaluestr
);
937 return strdup("unknown exception");
940 static MODCMD_FUNC(cmd_run
) {
941 /* this method allows running arbitrary python commands.
945 PyObject
* py_main_module
;
946 PyObject
* py_globals
;
949 PyObject
* extype
, *exvalue
, *extraceback
;
950 PyObject
* exvaluestr
= NULL
;
951 char* exmsg
= NULL
, *exmsgptr
;
953 py_main_module
= PyImport_AddModule("__main__");
954 py_globals
= py_locals
= PyModule_GetDict(py_main_module
);
956 msg
= unsplit_string(argv
+ 1, argc
- 1, NULL
);
958 py_retval
= PyRun_String(msg
, Py_file_input
, py_globals
, py_locals
);
959 if (py_retval
== NULL
) {
960 PyErr_Fetch(&extype
, &exvalue
, &extraceback
);
961 if (exvalue
!= NULL
) {
962 exvaluestr
= PyObject_Str(exvalue
);
963 exmsg
= strdup(PyString_AS_STRING(exvaluestr
));
965 while (exmsgptr
&& *exmsgptr
) {
966 if (*exmsgptr
== '\n' || *exmsgptr
== '\r' || *exmsgptr
== '\t')
971 if (extype
!= NULL
&& exvalue
!= NULL
&& PyType_Check(extype
)) {
972 reply("PYMSG_RUN_EXCEPTION", ((PyTypeObject
*)extype
)->tp_name
, exmsg
);
974 reply("PYMSG_RUN_UNKNOWN_EXCEPTION");
980 if (extraceback
!= NULL
)
981 Py_DECREF(extraceback
);
982 if (exvaluestr
!= NULL
)
983 Py_DECREF(exvaluestr
);
987 Py_DECREF(py_retval
);
993 #define numstrargs(X) sizeof(X) / sizeof(*X)
994 static MODCMD_FUNC(cmd_command
) {
995 char *plugin
= argv
[1];
996 char *command
= argv
[2];
997 char *msg
; /* args */
999 msg
= unsplit_string(argv
+ 3, argc
- 3, NULL
);
1004 char *args
[] = {plugin
, command
, msg
};
1005 python_call_handler("cmd_command", args
, numstrargs(args
), cmd
->parent
->bot
->nick
, user
?user
->nick
:"", channel
?channel
->name
:"");
1009 static void modpython_conf_read(void) {
1013 if (!(conf_node
= conf_get_data(MODPYTHON_CONF_NAME
, RECDB_OBJECT
))) {
1014 log_module(PY_LOG
, LOG_ERROR
, "config node '%s' is missing or has wrong type", MODPYTHON_CONF_NAME
);
1018 str
= database_get_data(conf_node
, "scripts_dir", RECDB_QSTRING
);
1019 modpython_conf
.scripts_dir
= str
? str
: "./";
1021 str
= database_get_data(conf_node
, "main_module", RECDB_QSTRING
);
1022 modpython_conf
.main_module
= str
? str
: "modpython";
1025 int python_init(void) {
1026 /* X3 calls this function on init of the module during startup. We use it to
1027 do all our setup tasks and bindings
1030 PY_LOG
= log_register_type("Python", "file:python.log");
1031 python_module
= module_register("python", PY_LOG
, "mod-python.help", NULL
);
1032 conf_register_reload(modpython_conf_read
);
1034 log_module(PY_LOG
, LOG_INFO
, "python module init");
1035 message_register_table(msgtab
);
1038 reg_auth_func(python_check_messages);
1039 reg_handle_rename_func(python_rename_account);
1040 reg_unreg_func(python_unreg_account);
1041 conf_register_reload(python_conf_read);
1042 saxdb_register("python", python_saxdb_read, python_saxdb_write);
1043 modcmd_register(python_module, "send", cmd_send, 3, MODCMD_REQUIRE_AUTHED, NULL);
1045 modcmd_register(python_module
, "reload", cmd_reload
, 1, MODCMD_REQUIRE_AUTHED
, "flags", "+oper", NULL
);
1046 modcmd_register(python_module
, "run", cmd_run
, 2, MODCMD_REQUIRE_AUTHED
, "flags", "+oper", NULL
);
1047 modcmd_register(python_module
, "command", cmd_command
, 3, MODCMD_REQUIRE_STAFF
, NULL
);
1049 // Please help us by implementing any of the callbacks listed as TODO below. They already exist
1050 // in x3, they just need handle_ bridges implemented. (see python_handle_join for an example)
1051 reg_server_link_func(python_handle_server_link
);
1052 reg_new_user_func(python_handle_new_user
);
1053 reg_nick_change_func(python_handle_nick_change
);
1054 //TODO: reg_del_user_func(python_handle_del_user);
1055 //TODO: reg_account_func(python_handle_account); /* stamping of account name to the ircd */
1056 //TODO: reg_handle_rename_func(python_handle_handle_rename); /* handle used to ALSO mean account name */
1057 //TODO: reg_failpw_func(python_handle_failpw);
1058 //TODO: reg_allowauth_func(python_handle_allowauth);
1059 //TODO: reg_handle_merge_func(python_handle_merge);
1061 //TODO: reg_oper_func(python_handle_oper);
1062 //TODO: reg_new_channel_func(python_handle_new_channel);
1063 reg_join_func(python_handle_join
);
1064 //TODO: reg_del_channel_func(python_handle_del_channel);
1065 //TODO: reg_part_func(python_handle_part);
1066 //TODO: reg_kick_func(python_handle_kick);
1067 //TODO: reg_topic_func(python_handle_topic);
1068 //TODO: reg_channel_mode_func(python_handle_channel_mode);
1070 //TODO: reg_privmsg_func(python_handle_privmsg);
1071 //TODO: reg_notice_func
1072 //TODO: reg_svccmd_unbind_func(python_handle_svccmd_unbind);
1073 //TODO: reg_chanmsg_func(python_handle_chanmsg);
1074 //TODO: reg_allchanmsg_func
1075 //TODO: reg_user_mode_func
1077 reg_exit_func(python_cleanup
);
1083 #endif /* WITH_PYTHON */