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 */
25 #ifndef WITH_PROTOCOL_P10
26 #error mod-python is only supported with p10 protocol enabled
27 #endif /* WITH_PROTOCOL_P10 */
42 * - Implement most of proto-p10 irc_* commands for calling from scripts
43 * - Implement functions to look up whois, channel, account, and reg-channel info for scripts
44 * - Implement x3.conf settings for python variables like include path, etc.
45 * - modpython.py calls for everything you can reg_ a handler for in x3
46 * - Some kind of system for getting needed binds bound automagicaly to make it easier
47 * to run peoples' scripts and mod-python in general.
48 * - An interface to reading/writing data to x3.db. Maybe generic, or attached to account or channel reg records?
51 static const struct message_entry msgtab
[] = {
52 { "PYMSG_RELOAD_SUCCESS", "Reloaded Python scripts successfully." },
53 { "PYMSG_RELOAD_FAILED", "Error reloading Python scripts." },
54 { "PYMSG_RUN_UNKNOWN_EXCEPTION", "Error running python: unknown exception." },
55 { "PYMSG_RUN_EXCEPTION", "Error running python: %s: %s." },
56 { NULL
, NULL
} /* sentinel */
59 #define MODPYTHON_CONF_NAME "modules/python"
63 char const* scripts_dir
;
64 char const* main_module
;
67 static struct log_type
*PY_LOG
;
68 const char *python_module_deps
[] = { NULL
};
69 static struct module *python_module
;
71 PyObject
*base_module
= NULL
; /* Base python handling library */
72 PyObject
*handler_object
= NULL
; /* instance of handler class */
75 extern struct userNode
*global
, *chanserv
, *opserv
, *nickserv
, *spamserv
;
78 Some hooks you can call from modpython.py to interact with the
79 service. These emb_* functions are available as _svc.* in python. */
81 struct _tuple_dict_extra
{
86 static int _dict_iter_get_users(char const* key
, UNUSED_ARG(void* data
), void* extra
) {
88 struct _tuple_dict_extra
* real_extra
= (struct _tuple_dict_extra
*)extra
;
90 if ((tmp
= PyString_FromString(key
)) == NULL
)
93 if (PyTuple_SetItem(real_extra
->data
, *(int*)real_extra
->extra
, tmp
))
96 *real_extra
->extra
= *real_extra
->extra
+ 1;
101 static int _dict_iter_get_channels(char const* key
, UNUSED_ARG(void* data
), void* extra
) {
103 struct _tuple_dict_extra
* real_extra
= (struct _tuple_dict_extra
*)extra
;
105 if ((tmp
= PyString_FromString(key
)) == NULL
)
108 if (PyTuple_SetItem(real_extra
->data
, *(int*)real_extra
->extra
, tmp
))
111 *real_extra
->extra
= *real_extra
->extra
+ 1;
115 static int _dict_iter_get_servers(char const* key
, UNUSED_ARG(void* data
), void* extra
) {
116 struct _tuple_dict_extra
* real_extra
= (struct _tuple_dict_extra
*)extra
;
118 PyTuple_SetItem(real_extra
->data
, *(int*)real_extra
->extra
,
119 PyString_FromString(key
));
120 *real_extra
->extra
= *real_extra
->extra
+ 1;
124 /* get a tuple with all users in it */
126 emb_get_users(UNUSED_ARG(PyObject
*self
), PyObject
*args
) {
129 size_t num_clients
, n
= 0, i
;
130 struct _tuple_dict_extra extra
;
132 if (!PyArg_ParseTuple(args
, ""))
135 num_clients
= dict_size(clients
);
136 retval
= PyTuple_New(num_clients
);
143 if (dict_foreach(clients
, _dict_iter_get_users
, (void*)&extra
) != NULL
) {
144 for (i
= 0; i
< n
; ++i
) {
145 tmp
= PyTuple_GetItem(retval
, i
);
146 PyTuple_SET_ITEM(retval
, i
, NULL
);
156 /* get a tuple with all channels in it */
158 emb_get_channels(UNUSED_ARG(PyObject
* self
), PyObject
* args
) {
161 size_t num_channels
, n
= 0, i
;
162 struct _tuple_dict_extra extra
;
164 if (!PyArg_ParseTuple(args
, ""))
167 num_channels
= dict_size(channels
);
168 retval
= PyTuple_New(num_channels
);
175 if (dict_foreach(channels
, _dict_iter_get_channels
, (void*)&extra
) != NULL
) {
176 for (i
= 0; i
< n
; ++i
) {
177 tmp
= PyTuple_GetItem(retval
, i
);
178 PyTuple_SET_ITEM(retval
, i
, NULL
);
189 emb_get_servers(UNUSED_ARG(PyObject
* self
), PyObject
* args
) {
192 struct _tuple_dict_extra extra
;
194 if (!PyArg_ParseTuple(args
, ""))
197 retval
= PyTuple_New(dict_size(servers
));
202 dict_foreach(servers
, _dict_iter_get_servers
, (void*)&extra
);
208 emb_dump(UNUSED_ARG(PyObject
*self
), PyObject
*args
)
210 /* Dump a raw string into the socket
211 usage: _svc.dump(<P10 string>)
215 char linedup
[MAXLEN
];
218 if(!PyArg_ParseTuple(args
, "s:dump", &buf
))
221 safestrncpy(linedup
, buf
, sizeof(linedup
));
223 if(parse_line(linedup
, 1)) {
227 PyErr_SetString(PyExc_Exception
, "invalid protocol message");
231 return Py_BuildValue("i", ret
);
235 emb_send_target_privmsg(UNUSED_ARG(PyObject
*self
), PyObject
*args
)
238 usage: _svc.send_target_privmsg(<servicenick_from>, <nick_to>, <message>)
245 struct service
*service
;
248 if(!PyArg_ParseTuple(args
, "sss:reply", &servicenick
, &channel
, &buf
))
251 if (buf
== NULL
|| strlen(buf
) == 0) {
252 PyErr_SetString(PyExc_Exception
, "invalid empty message");
256 if(!(service
= service_find(servicenick
))) {
257 PyErr_SetString(PyExc_Exception
, "no such service nick");
261 ret
= send_target_message(5, channel
, service
->bot
, "%s", buf
);
262 return Py_BuildValue("i", ret
);
266 emb_send_target_notice(UNUSED_ARG(PyObject
*self
), PyObject
*args
)
269 usage: _svc.send_target_notice(<servicenick_from>, <nick_to>, <message>)
276 struct service
*service
;
278 if(!PyArg_ParseTuple(args
, "sss:reply", &servicenick
, &target
, &buf
))
281 if (buf
== NULL
|| strlen(buf
) == 0) {
282 PyErr_SetString(PyExc_Exception
, "invalid empty message");
286 if(!(service
= service_find(servicenick
))) {
287 PyErr_SetString(PyExc_Exception
, "no such service nick");
291 ret
= send_target_message(4, target
, service
->bot
, "%s", buf
);
293 return Py_BuildValue("i", ret
);
297 pyobj_from_usernode(struct userNode
* user
) {
300 PyObject
* pChanList
= PyTuple_New(user
->channels
.used
);
302 for (n
=0; n
< user
->channels
.used
; n
++) {
303 mn
= user
->channels
.list
[n
];
304 PyTuple_SetItem(pChanList
, n
, Py_BuildValue("s", mn
->channel
->name
));
307 return Py_BuildValue("{"
311 "s: s, " /* hostname */
313 "s: s, " /* fakehost */
314 "s: s, " /* sethost */
315 "s: s, " /* crypthost */
316 "s: s, " /* cryptip */
317 "s: s, " /* numeric */
319 "s: i, " /* no_notice */
321 "s: s, " /* version_reply */
322 "s: s, " /* account */
323 "s: O}", /* channels */
325 "ident", user
->ident
,
327 "hostname", user
->hostname
,
328 "ip", irc_ntoa(&user
->ip
),
329 "fakehost", user
->fakehost
,
330 "sethost", user
->sethost
,
331 "crypthost", user
->crypthost
,
332 "cryptip", user
->cryptip
,
333 "numeric", user
->numeric
,
335 "no_notice", user
->no_notice
,
337 "version_reply", user
->version_reply
,
338 "account", user
->handle_info
? user
->handle_info
->handle
: NULL
,
339 "channels", pChanList
);
343 emb_get_user(UNUSED_ARG(PyObject
*self
), PyObject
*args
)
345 /* Get a python object containing everything x3 knows about a user, by nick.
346 usage: _svc.get_user(<nick>)
349 struct userNode
*user
;
351 if(!PyArg_ParseTuple(args
, "s", &nick
))
354 if(!(user
= GetUserH(nick
))) {
355 PyErr_SetString(PyExc_Exception
, "no such user");
359 return pyobj_from_usernode(user
);
363 pyobj_from_server(struct server
* srv
) {
365 PyObject
* tmp
= NULL
;
366 PyObject
* retval
= NULL
;
367 PyObject
* users
= PyTuple_New(srv
->clients
);
373 for (n
= 0; n
< srv
->num_mask
; ++n
) {
374 if (srv
->users
[n
] == NULL
)
377 tmp
= PyString_FromString(srv
->users
[n
]->nick
);
381 if (PyTuple_SetItem(users
, idx
++, tmp
))
385 retval
= Py_BuildValue("{"
388 "s:l," /* link_time */
389 "s:s," /* description */
391 "s:I," /* num_mask */
394 "s:I," /* max_clients */
396 "s:I," /* self_burst */
403 "link_time", srv
->link_time
,
404 "description", srv
->description
,
405 "numeric", srv
->numeric
,
406 "num_mask", srv
->num_mask
,
408 "clients", srv
->clients
,
409 "max_clients", srv
->max_clients
,
411 "self_burst", srv
->self_burst
,
412 "uplink", srv
->uplink
? srv
->uplink
->name
: NULL
,
424 for (n
= 0; n
< idx
; ++n
) {
425 tmp
= PyTuple_GetItem(users
, n
);
426 PyTuple_SetItem(users
, n
, NULL
);
435 emb_get_server(UNUSED_ARG(PyObject
* self
), PyObject
* args
) {
439 if (!PyArg_ParseTuple(args
, "s", &name
))
442 if (name
== NULL
|| strlen(name
) == 0) {
443 PyErr_SetString(PyExc_Exception
, "invalid server name");
447 if ((srv
= GetServerH(name
)) == NULL
) {
448 PyErr_SetString(PyExc_Exception
, "unknown server");
452 return pyobj_from_server(srv
);
456 emb_get_channel(UNUSED_ARG(PyObject
*self
), PyObject
*args
)
458 /* Returns a python dict object with all sorts of info about a channel.
459 usage: _svc.get_channel(<name>)
462 struct chanNode
*channel
;
464 PyObject
*pChannelMembers
;
465 PyObject
*pChannelBans
;
466 PyObject
*pChannelExempts
;
469 if(!PyArg_ParseTuple(args
, "s", &name
))
472 if(!(channel
= GetChannel(name
))) {
473 PyErr_SetString(PyExc_Exception
, "unknown channel");
477 /* build tuple of nicks in channel */
478 pChannelMembers
= PyTuple_New(channel
->members
.used
);
479 for(n
=0;n
< channel
->members
.used
;n
++) {
480 struct modeNode
*mn
= channel
->members
.list
[n
];
481 PyTuple_SetItem(pChannelMembers
, n
, Py_BuildValue("s", mn
->user
->nick
));
484 /* build tuple of bans */
485 pChannelBans
= PyTuple_New(channel
->banlist
.used
);
486 for(n
=0; n
< channel
->banlist
.used
;n
++) {
487 struct banNode
*bn
= channel
->banlist
.list
[n
];
488 PyTuple_SetItem(pChannelBans
, n
,
489 Py_BuildValue("{s:s,s:s,s:i}",
496 /* build tuple of exempts */
497 pChannelExempts
= PyTuple_New(channel
->exemptlist
.used
);
498 for(n
=0; n
< channel
->exemptlist
.used
;n
++) {
499 struct exemptNode
*en
= channel
->exemptlist
.list
[n
];
500 PyTuple_SetItem(pChannelExempts
, n
,
501 Py_BuildValue("{s:s,s:s,s:i}",
508 return Py_BuildValue("{s:s,s:s,s:s,s:i"
509 ",s:i,s:i,s:O,s:O,s:O}",
511 "name", channel
->name
,
512 "topic", channel
->topic
,
513 "topic_nick", channel
->topic_nick
,
514 "topic_time", channel
->topic_time
,
516 "timestamp", channel
->timestamp
,
517 "modes", channel
->modes
,
518 "members", pChannelMembers
,
519 "bans", pChannelBans
,
520 "exempts", pChannelExempts
525 emb_get_account(UNUSED_ARG(PyObject
*self
), PyObject
*args
)
527 /* Returns a python dict object with all sorts of info about an account.
528 usage: _svc.get_account(<account name>)
531 struct handle_info
*hi
;
534 if(!PyArg_ParseTuple(args
, "s", &name
))
537 hi
= get_handle_info(name
);
540 PyErr_SetString(PyExc_Exception
, "unknown account name");
544 return Py_BuildValue("{s:s,s:i,s:s,s:s,s:s"
547 "account", hi
->handle
,
548 "registered", hi
->registered
,
549 "last_seen", hi
->lastseen
,
550 "infoline", hi
->infoline
? hi
->infoline
: "",
551 "email", hi
->email_addr
? hi
->email_addr
: "",
553 "fakehost", hi
->fakehost
? hi
->fakehost
: "",
554 "last_quit_host", hi
->last_quit_host
557 /* users online authed to this account */
559 /* nicks (nickserv nets only?) */
567 emb_get_info(UNUSED_ARG(PyObject
*self
), UNUSED_ARG(PyObject
*args
))
569 /* return some info about the general setup
570 * of X3, such as what the chanserv's nickname
575 return Py_BuildValue("{s:s,s:s,s:s,s:s,s:s}",
576 "chanserv", chanserv
? chanserv
->nick
: "ChanServ",
577 "nickserv", nickserv
?nickserv
->nick
: "NickServ",
578 "opserv", opserv
?opserv
->nick
: "OpServ",
579 "global", global
?global
->nick
: "Global",
580 "spamserv", spamserv
?spamserv
->nick
: "SpamServ");
584 emb_log_module(UNUSED_ARG(PyObject
*self
), PyObject
*args
)
586 /* a gateway to standard X3 logging subsystem.
587 * level is a value 0 to 9 as defined by the log_severity enum in log.h.
589 * for now, all logs go to the PY_LOG log. In the future this will change.
596 if(!PyArg_ParseTuple(args
, "is", &level
, &message
))
599 log_module(PY_LOG
, level
, "%s", message
);
601 return Py_BuildValue("i", ret
);
604 static PyMethodDef EmbMethods
[] = {
605 /* Communication methods */
606 {"dump", emb_dump
, METH_VARARGS
, "Dump raw P10 line to server"},
607 {"send_target_privmsg", emb_send_target_privmsg
, METH_VARARGS
, "Send a message to somewhere"},
608 {"send_target_notice", emb_send_target_notice
, METH_VARARGS
, "Send a notice to somewhere"},
609 {"log_module", emb_log_module
, METH_VARARGS
, "Log something using the X3 log subsystem"},
610 //TODO: {"exec_cmd", emb_exec_cmd, METH_VARARGS, "execute x3 command provided"},
611 // This should use environment from "python command" call to pass in, if available
615 //TODO: {"gline", emb_gline, METH_VARARGS, "gline a mask"},
616 //TODO: {"ungline", emb_ungline, METH_VARARGS, "remove a gline"},
617 //TODO: {"kick", emb_kick, METH_VARARGS, "kick someone from a channel"},
618 //TODO: {"channel_mode", emb_channel_mode, METH_VARARGS, "set modes on a channel"},
619 //TODO: {"user_mode", emb_user_mode, METH_VARARGS, "Have x3 set usermodes on one of its own nicks"},
621 //TODO: {"get_config", emb_get_config, METH_VARARGS, "get x3.conf settings into a nested dict"},
622 //TODO: {"config_set", emb_config_set, METH_VARARGS, "change a config setting 'on-the-fly'."},
624 //TODO: {"timeq_add", emb_timeq_new, METH_VARARGS, "some kind of interface to the timed event system."},
625 //TODO: {"timeq_del", emb_timeq_new, METH_VARARGS, "some kind of interface to the timed event system."},
626 /* Information gathering methods */
627 {"get_user", emb_get_user
, METH_VARARGS
, "Get details about a nickname"},
628 {"get_users", emb_get_users
, METH_VARARGS
, "Get all connected users"},
629 {"get_channel", emb_get_channel
, METH_VARARGS
, "Get details about a channel"},
630 {"get_channels", emb_get_channels
, METH_VARARGS
, "Get all channels"},
631 {"get_server", emb_get_server
, METH_VARARGS
, "Get details about a server"},
632 {"get_servers", emb_get_servers
, METH_VARARGS
, "Get all server names"},
633 {"get_account", emb_get_account
, METH_VARARGS
, "Get details about an account"},
634 {"get_info", emb_get_info
, METH_VARARGS
, "Get various misc info about x3"},
635 /* null terminator */
636 {NULL
, NULL
, 0, NULL
}
641 These functions set up the embedded environment for us to call out to
642 modpython.py class methods.
645 void python_log_module() {
646 /* Attempt to convert python errors to x3 log system */
647 PyObject
*exc
, *tb
, *value
, *tmp
;
648 char *str_exc
= "NONE";
649 char *str_value
= "NONE";
650 char *str_tb
= "NONE";
652 PyErr_Fetch(&exc
, &value
, &tb
);
655 if((tmp
= PyObject_Str(exc
)))
656 str_exc
= PyString_AsString(tmp
);
659 if((tmp
= PyObject_Str(value
)))
660 str_value
= PyString_AsString(tmp
);
663 if((tmp
= PyObject_Str(tb
)))
664 str_tb
= PyString_AsString(tmp
);
667 /* Now restore it so we can print it (just in case)
668 * (should we do this only when running in debug mode?) */
669 PyErr_Restore(exc
, value
, tb
);
670 PyErr_Print(); /* which of course, clears it again.. */
672 log_module(PY_LOG
, LOG_WARNING
, "PYTHON error: %s, value: %s", str_exc
, str_value
);
674 /* TODO: get the traceback using the traceback module via C api so we can add it to the X3 logs. See
675 * http://mail.python.org/pipermail/python-list/2003-March/192226.html */
676 // (this doesnt work, str_tb is just an object hashid) log_module(PY_LOG, LOG_INFO, "PYTHON error, traceback: %s", str_tb);
680 PyObject
*python_build_handler_args(size_t argc
, char *args
[], PyObject
*pIrcObj
) {
681 /* Sets up a python tuple with passed in arguments, prefixed by the Irc instance
682 which handlers use to interact with C.
683 argc = number of args
685 pIrcObj = instance of the irc class
688 PyObject
*pArgs
= NULL
;
690 pArgs
= PyTuple_New(argc
+ 1);
692 PyTuple_SetItem(pArgs
, i
++, pIrcObj
);
696 for(n
= 0; n
< argc
; ++n
) {
697 pValue
= PyString_FromString(args
[n
]);
700 log_module(PY_LOG
, LOG_INFO
, "Unable to convert '%s' to python string", args
[n
]);
703 PyTuple_SetItem(pArgs
, n
+i
, pValue
);
709 PyObject
*python_build_args(size_t argc
, char *args
[]) {
710 /* Builds the passed in arguments into a python argument tuple.
711 argc = number of args
715 PyObject
*pArgs
= NULL
;
718 pArgs
= PyTuple_New(argc
);
720 for(i
= 0; i
< argc
; ++i
) {
721 pValue
= PyString_FromString(args
[i
]);
724 log_module(PY_LOG
, LOG_INFO
, "Unable to convert '%s' to python string", args
[i
]);
727 PyTuple_SetItem(pArgs
, i
, pValue
);
734 PyObject
*new_irc_object(char *command_service
, char *command_caller
, char *command_target
) {
735 /* Creates a new instance of the irc class (from modpython.py) which is initalized
736 with current environment details like which service were talking to.
737 command_service = which service we are talking to, or empty string if none
738 command_caller = nick of user generating message, or empty string if none
739 command_target = If were reacting to something on a channel, this will
740 be set to the name of the channel. Otherwise empty
742 PyObject
*pIrcArgs
= NULL
;
746 log_module(PY_LOG
, LOG_INFO
, "Attempting to instanciate irc class; %s %s %s", command_service
, command_caller
, command_target
);
747 pIrcClass
= PyObject_GetAttrString(base_module
, "irc");
748 /* pIrcClass is a new reference */
749 if(pIrcClass
&& PyCallable_Check(pIrcClass
)) {
751 char *ircargs
[] = {command_service
, command_caller
, command_target
};
754 pIrcArgs
= python_build_args(3, ircargs
);
755 pIrcObj
= PyObject_CallObject(pIrcClass
, pIrcArgs
);
757 log_module(PY_LOG
, LOG_ERROR
, "IRC Class failed to load");
761 if(pIrcArgs
!= NULL
) {
764 Py_DECREF(pIrcClass
);
768 /* need to free pIrcClass here if it WAS found but was NOT callable? */
769 log_module(PY_LOG
, LOG_ERROR
, "Unable to find irc class");
774 int python_call_handler(char *handler
, char *args
[], size_t argc
, char *command_service
, char *command_caller
, char *command_target
) {
775 /* This is how we talk to modpython.c. First a new instance of the irc class is created using these
776 arguments to setup the current environment. Then the named method of the handler object is called
777 with the givin arguments.
784 log_module(PY_LOG
, LOG_INFO
, "attempting to call handler %s.", handler
);
785 if(base_module
!= NULL
&& handler_object
!= NULL
) {
786 pIrcObj
= new_irc_object(command_service
, command_caller
, command_target
);
788 log_module(PY_LOG
, LOG_INFO
, "Can't get irc object. Bailing.");
792 pArgs
= python_build_handler_args(argc
, args
, pIrcObj
);
793 pMethod
= PyObject_GetAttrString(handler_object
, handler
);
794 if(pMethod
&& PyCallable_Check(pMethod
)) {
795 /* Call the method, with the arguments */
796 pValue
= PyObject_CallObject(pMethod
, pArgs
);
802 ret
= PyInt_AsLong(pValue
);
803 if(ret
== -1 && PyErr_Occurred()) {
805 log_module(PY_LOG
, LOG_INFO
, "error converting return value of handler %s to type long. ", handler
);
809 log_module(PY_LOG
, LOG_INFO
, "handler %s was run successfully, returned %d.", handler
, ret
);
816 /* TODO: instead of print errors, get them as strings
817 * and deal with them with normal x3 log system. */
818 log_module(PY_LOG
, LOG_WARNING
, "call to handler %s failed", handler
);
826 else { /* couldn't find handler methed */
828 /* Free pMethod if it was found but not callable? */
829 log_module(PY_LOG
, LOG_ERROR
, "Cannot find handler %s.", handler
);
834 else { /* No base module.. no python? */
835 log_module(PY_LOG
, LOG_INFO
, "Cannot handle %s, Python is not initialized.", handler
);
840 PyObject
*python_new_handler_object() {
841 /* Create a new instance of the handler class.
842 This is called during python initilization (or reload)
843 and the result is saved and re-used.
845 PyObject
*pHandlerClass
, *pHandlerObj
;
847 log_module(PY_LOG
, LOG_INFO
, "Attempting to instanciate python class handler");
848 pHandlerClass
= PyObject_GetAttrString(base_module
, "handler");
849 /* Class is a new reference */
850 if(pHandlerClass
&& PyCallable_Check(pHandlerClass
)) {
851 /*PyObject *pValue; */
853 pHandlerObj
= PyObject_CallObject(pHandlerClass
, NULL
);
854 if(pHandlerObj
!= NULL
) {
855 log_module(PY_LOG
, LOG_INFO
, "Created new python handler object.");
859 log_module(PY_LOG
, LOG_ERROR
, "Unable to instanciate handler object");
866 log_module(PY_LOG
, LOG_ERROR
, "Unable to find handler class");
870 Py_DECREF(pHandlerClass
);
876 /* ------------------------------------------------------------------------------- *
877 Some gateway functions to convert x3 callbacks into modpython.py callbacks.
878 Mostly we just build relevant args and call the proper handler method
880 debate: do we just register these and check them in python
881 for every one (slow?) or actually work out if a plugin needs
882 it first? We will start by doing it every time.
885 python_handle_join(struct modeNode
*mNode
)
887 /* callback for handle_join events.
889 struct userNode
*user
= mNode
->user
;
890 struct chanNode
*channel
= mNode
->channel
;
893 log_module(PY_LOG
, LOG_INFO
, "python module handle_join");
894 if(!channel
||!user
) {
895 log_module(PY_LOG
, LOG_WARNING
, "Python code got join without channel or user!");
899 char *args
[] = {channel
->name
, user
->nick
};
900 return python_call_handler("join", args
, 2, "", "", "");
905 python_handle_server_link(struct server
*server
)
907 log_module(PY_LOG
, LOG_INFO
, "python module handle_server_link");
909 log_module(PY_LOG
, LOG_WARNING
, "Python code got server link without server!");
913 char *args
[] = {server
->name
, server
->description
};
914 return python_call_handler("server_link", args
, 2, "", "", "");
919 python_handle_new_user(struct userNode
*user
)
921 log_module(PY_LOG
, LOG_INFO
, "Python module handle_new_user");
923 log_module(PY_LOG
, LOG_WARNING
, "Python code got new_user without the user");
927 char *args
[] = {user
->nick
, user
->ident
, user
->hostname
, user
->info
};
928 return python_call_handler("new_user", args
, 4, "", "", "");
933 python_handle_nick_change(struct userNode
*user
, const char *old_nick
)
935 log_module(PY_LOG
, LOG_INFO
, "Python module handle_nick_change");
937 log_module(PY_LOG
, LOG_WARNING
, "Python code got nick_change without the user!");
940 char *args
[] = {user
->nick
, (char *)old_nick
};
941 python_call_handler("nick_change", args
, 2, "", "", "");
945 /* ----------------------------------------------------------------------------- */
949 /* Init the python engine and do init work on modpython.py
950 This is called during x3 startup, and on a python reload
954 char* env
= getenv("PYTHONPATH");
960 setenv("PYTHONPATH", modpython_conf
.scripts_dir
, 1);
961 else if (!strstr(env
, modpython_conf
.scripts_dir
)) {
962 buffer
= (char*)malloc(strlen(env
) + strlen(modpython_conf
.scripts_dir
) + 2);
963 sprintf(buffer
, "%s:%s", modpython_conf
.scripts_dir
, env
);
964 setenv("PYTHONPATH", buffer
, 1);
970 Py_InitModule("_svc", EmbMethods
);
971 pName
= PyString_FromString(modpython_conf
.main_module
);
972 base_module
= PyImport_Import(pName
);
975 Py_XDECREF(handler_object
);
976 handler_object
= NULL
;
978 if(base_module
!= NULL
) {
979 handler_object
= python_new_handler_object();
981 python_call_handler("init", NULL
, 0, "", "", "");
985 /* error handler class not found */
986 log_module(PY_LOG
, LOG_WARNING
, "Failed to create handler object");
993 log_module(PY_LOG
, LOG_WARNING
, "Failed to load modpython.py");
1000 python_finalize(void) {
1001 /* Called after X3 is fully up and running.
1002 Code can be put here that needs to run to init things, but
1003 which is sensitive to everything else in x3 being up and ready
1007 PyRun_SimpleString("print 'Hello, World of Python!'");
1008 log_module(PY_LOG
, LOG_INFO
, "python module finalize");
1014 python_cleanup(void) {
1015 /* Called on shutdown of the python module (or before reloading)
1018 log_module(PY_LOG
, LOG_INFO
, "python module cleanup");
1019 if (PyErr_Occurred())
1021 Py_Finalize(); /* Shut down python enterpreter */
1024 /* ---------------------------------------------------------------------------------- *
1025 Python module command handlers.
1027 static MODCMD_FUNC(cmd_reload
) {
1028 /* reload the python system completely
1030 log_module(PY_LOG
, LOG_INFO
, "Shutting python down");
1032 log_module(PY_LOG
, LOG_INFO
, "Loading python stuff");
1034 reply("PYMSG_RELOAD_SUCCESS");
1037 reply("PYMSG_RELOAD_FAILED");
1042 static char* format_python_error(int space_nls
) {
1043 PyObject
* extype
= NULL
, *exvalue
= NULL
, *extraceback
= NULL
;
1044 PyObject
* pextypestr
= NULL
, *pexvaluestr
= NULL
;
1045 char* extypestr
= NULL
, *exvaluestr
= NULL
;
1046 size_t retvallen
= 0;
1047 char* retval
= NULL
, *tmp
;
1049 PyErr_Fetch(&extype
, &exvalue
, &extraceback
);
1053 pextypestr
= PyObject_Str(extype
);
1056 extypestr
= PyString_AsString(pextypestr
);
1060 pexvaluestr
= PyObject_Str(exvalue
);
1062 exvaluestr
= PyString_AsString(pexvaluestr
);
1064 retvallen
= strlen(extypestr
) + (exvaluestr
? strlen(exvaluestr
) + 2 : 0) + 1;
1065 retval
= (char*)malloc(retvallen
);
1067 snprintf(retval
, retvallen
, "%s: %s", extypestr
, exvaluestr
);
1069 strncpy(retval
, extypestr
, retvallen
);
1081 if (PyErr_Occurred())
1082 PyErr_Clear(); /* ignore errors caused by formatting */
1084 Py_XDECREF(exvalue
);
1085 Py_XDECREF(extraceback
);
1086 Py_XDECREF(pextypestr
);
1087 Py_XDECREF(pexvaluestr
);
1092 return strdup("unknown exception");
1095 static MODCMD_FUNC(cmd_run
) {
1096 /* this method allows running arbitrary python commands.
1100 PyObject
* py_main_module
;
1101 PyObject
* py_globals
;
1102 PyObject
* py_locals
;
1103 PyObject
* py_retval
;
1104 PyObject
* extype
, *exvalue
, *extraceback
;
1105 PyObject
* exvaluestr
= NULL
;
1106 char* exmsg
= NULL
, *exmsgptr
;
1108 py_main_module
= PyImport_AddModule("__main__");
1109 py_globals
= py_locals
= PyModule_GetDict(py_main_module
);
1111 msg
= unsplit_string(argv
+ 1, argc
- 1, NULL
);
1113 py_retval
= PyRun_String(msg
, Py_file_input
, py_globals
, py_locals
);
1114 if (py_retval
== NULL
) {
1115 PyErr_Fetch(&extype
, &exvalue
, &extraceback
);
1116 if (exvalue
!= NULL
) {
1117 exvaluestr
= PyObject_Str(exvalue
);
1118 exmsg
= strdup(PyString_AS_STRING(exvaluestr
));
1120 while (exmsgptr
&& *exmsgptr
) {
1121 if (*exmsgptr
== '\n' || *exmsgptr
== '\r' || *exmsgptr
== '\t')
1126 if (extype
!= NULL
&& exvalue
!= NULL
&& PyType_Check(extype
)) {
1127 reply("PYMSG_RUN_EXCEPTION", ((PyTypeObject
*)extype
)->tp_name
, exmsg
);
1129 reply("PYMSG_RUN_UNKNOWN_EXCEPTION");
1133 if (exvalue
!= NULL
)
1135 if (extraceback
!= NULL
)
1136 Py_DECREF(extraceback
);
1137 if (exvaluestr
!= NULL
)
1138 Py_DECREF(exvaluestr
);
1142 Py_DECREF(py_retval
);
1148 #define numstrargs(X) sizeof(X) / sizeof(*X)
1149 static MODCMD_FUNC(cmd_command
) {
1150 char *plugin
= argv
[1];
1151 char *command
= argv
[2];
1152 char *msg
; /* args */
1154 msg
= unsplit_string(argv
+ 3, argc
- 3, NULL
);
1159 char *args
[] = {plugin
, command
, msg
};
1160 python_call_handler("cmd_command", args
, numstrargs(args
), cmd
->parent
->bot
->nick
, user
?user
->nick
:"", channel
?channel
->name
:"");
1164 static void modpython_conf_read(void) {
1168 if (!(conf_node
= conf_get_data(MODPYTHON_CONF_NAME
, RECDB_OBJECT
))) {
1169 log_module(PY_LOG
, LOG_ERROR
, "config node '%s' is missing or has wrong type", MODPYTHON_CONF_NAME
);
1173 str
= database_get_data(conf_node
, "scripts_dir", RECDB_QSTRING
);
1174 modpython_conf
.scripts_dir
= str
? str
: "./";
1176 str
= database_get_data(conf_node
, "main_module", RECDB_QSTRING
);
1177 modpython_conf
.main_module
= str
? str
: "modpython";
1180 int python_init(void) {
1181 /* X3 calls this function on init of the module during startup. We use it to
1182 do all our setup tasks and bindings
1185 PY_LOG
= log_register_type("Python", "file:python.log");
1186 python_module
= module_register("python", PY_LOG
, "mod-python.help", NULL
);
1187 conf_register_reload(modpython_conf_read
);
1189 log_module(PY_LOG
, LOG_INFO
, "python module init");
1190 message_register_table(msgtab
);
1193 reg_auth_func(python_check_messages);
1194 reg_handle_rename_func(python_rename_account);
1195 reg_unreg_func(python_unreg_account);
1196 conf_register_reload(python_conf_read);
1197 saxdb_register("python", python_saxdb_read, python_saxdb_write);
1198 modcmd_register(python_module, "send", cmd_send, 3, MODCMD_REQUIRE_AUTHED, NULL);
1200 modcmd_register(python_module
, "reload", cmd_reload
, 1, MODCMD_REQUIRE_AUTHED
, "flags", "+oper", NULL
);
1201 modcmd_register(python_module
, "run", cmd_run
, 2, MODCMD_REQUIRE_AUTHED
, "flags", "+oper", NULL
);
1202 modcmd_register(python_module
, "command", cmd_command
, 3, MODCMD_REQUIRE_STAFF
, NULL
);
1204 // Please help us by implementing any of the callbacks listed as TODO below. They already exist
1205 // in x3, they just need handle_ bridges implemented. (see python_handle_join for an example)
1206 reg_server_link_func(python_handle_server_link
);
1207 reg_new_user_func(python_handle_new_user
);
1208 reg_nick_change_func(python_handle_nick_change
);
1209 //TODO: reg_del_user_func(python_handle_del_user);
1210 //TODO: reg_account_func(python_handle_account); /* stamping of account name to the ircd */
1211 //TODO: reg_handle_rename_func(python_handle_handle_rename); /* handle used to ALSO mean account name */
1212 //TODO: reg_failpw_func(python_handle_failpw);
1213 //TODO: reg_allowauth_func(python_handle_allowauth);
1214 //TODO: reg_handle_merge_func(python_handle_merge);
1216 //TODO: reg_oper_func(python_handle_oper);
1217 //TODO: reg_new_channel_func(python_handle_new_channel);
1218 reg_join_func(python_handle_join
);
1219 //TODO: reg_del_channel_func(python_handle_del_channel);
1220 //TODO: reg_part_func(python_handle_part);
1221 //TODO: reg_kick_func(python_handle_kick);
1222 //TODO: reg_topic_func(python_handle_topic);
1223 //TODO: reg_channel_mode_func(python_handle_channel_mode);
1225 //TODO: reg_privmsg_func(python_handle_privmsg);
1226 //TODO: reg_notice_func
1227 //TODO: reg_svccmd_unbind_func(python_handle_svccmd_unbind);
1228 //TODO: reg_chanmsg_func(python_handle_chanmsg);
1229 //TODO: reg_allchanmsg_func
1230 //TODO: reg_user_mode_func
1232 reg_exit_func(python_cleanup
);
1238 #endif /* WITH_PYTHON */