]>
Commit | Line | Data |
---|---|---|
0b350353 | 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 | |
4 | * | |
5 | * This file is part of x3. | |
6 | * | |
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. | |
11 | * | |
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. | |
16 | * | |
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. | |
20 | */ | |
21 | ||
22 | #include "config.h" | |
23 | #ifdef WITH_PYTHON /* just disable this file if python doesnt exist */ | |
24 | ||
d8f8d3b6 | 25 | #ifndef WITH_PROTOCOL_P10 |
26 | #error mod-python is only supported with p10 protocol enabled | |
27 | #endif /* WITH_PROTOCOL_P10 */ | |
0b350353 | 28 | |
413fd8ea | 29 | #include <Python.h> |
0b350353 | 30 | #include "chanserv.h" |
31 | #include "conf.h" | |
32 | #include "modcmd.h" | |
33 | #include "nickserv.h" | |
34 | #include "opserv.h" | |
35 | #include "saxdb.h" | |
1136f709 | 36 | #include "mail.h" |
0b350353 | 37 | #include "timeq.h" |
039a6658 | 38 | #include "compat.h" |
0b350353 | 39 | |
a2c8c575 | 40 | /* TODO notes |
41 | * | |
f0e11521 | 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. | |
4c216694 | 45 | * - modpython.py calls for everything you can reg_ a handler for in x3 |
a2c8c575 | 46 | * - Some kind of system for getting needed binds bound automagicaly to make it easier |
f0e11521 | 47 | * to run peoples' scripts and mod-python in general. |
039a6658 | 48 | * - An interface to reading/writing data to x3.db. Maybe generic, or attached to account or channel reg records? |
a2c8c575 | 49 | */ |
0b350353 | 50 | |
51 | static const struct message_entry msgtab[] = { | |
caf97651 | 52 | { "PYMSG_RELOAD_SUCCESS", "Reloaded Python scripts successfully." }, |
53 | { "PYMSG_RELOAD_FAILED", "Error reloading Python scripts." }, | |
413fd8ea | 54 | { "PYMSG_RUN_UNKNOWN_EXCEPTION", "Error running python: unknown exception." }, |
46f628b1 | 55 | { "PYMSG_RUN_EXCEPTION", "Error running python: %s: %s." }, |
f0e11521 | 56 | { NULL, NULL } /* sentinel */ |
0b350353 | 57 | }; |
58 | ||
ef5e0305 | 59 | #define MODPYTHON_CONF_NAME "modules/python" |
60 | ||
61 | static | |
62 | struct { | |
63 | char const* scripts_dir; | |
ed8d873c | 64 | char const* main_module; |
ef5e0305 | 65 | } modpython_conf; |
66 | ||
0b350353 | 67 | static struct log_type *PY_LOG; |
68 | const char *python_module_deps[] = { NULL }; | |
69 | static struct module *python_module; | |
70 | ||
caf97651 | 71 | PyObject *base_module = NULL; /* Base python handling library */ |
f0e11521 | 72 | PyObject *handler_object = NULL; /* instance of handler class */ |
caf97651 | 73 | |
4c216694 | 74 | |
039a6658 | 75 | extern struct userNode *global, *chanserv, *opserv, *nickserv, *spamserv; |
76 | ||
f0e11521 | 77 | /* |
78 | Some hooks you can call from modpython.py to interact with the | |
79 | service. These emb_* functions are available as _svc.* in python. */ | |
4c216694 | 80 | |
6d94ce8b | 81 | struct _tuple_dict_extra { |
82 | PyObject* data; | |
83 | size_t* extra; | |
84 | }; | |
85 | ||
86 | static int _dict_iter_get_users(char const* key, UNUSED_ARG(void* data), void* extra) { | |
5345ea76 | 87 | PyObject* tmp; |
6d94ce8b | 88 | struct _tuple_dict_extra* real_extra = (struct _tuple_dict_extra*)extra; |
89 | ||
5345ea76 | 90 | if ((tmp = PyString_FromString(key)) == NULL) |
91 | return 1; | |
92 | ||
318ec177 | 93 | if (PyTuple_SetItem(real_extra->data, *(int*)real_extra->extra, tmp)) { |
94 | Py_DECREF(tmp); | |
5345ea76 | 95 | return 1; |
318ec177 | 96 | } |
5345ea76 | 97 | |
6d94ce8b | 98 | *real_extra->extra = *real_extra->extra + 1; |
5345ea76 | 99 | |
6d94ce8b | 100 | return 0; |
101 | } | |
102 | ||
cc0b2b7f | 103 | static int _dict_iter_get_channels(char const* key, UNUSED_ARG(void* data), void* extra) { |
d12756d7 | 104 | PyObject* tmp; |
cc0b2b7f | 105 | struct _tuple_dict_extra* real_extra = (struct _tuple_dict_extra*)extra; |
106 | ||
d12756d7 | 107 | if ((tmp = PyString_FromString(key)) == NULL) |
108 | return 1; | |
109 | ||
318ec177 | 110 | if (PyTuple_SetItem(real_extra->data, *(int*)real_extra->extra, tmp)) { |
111 | Py_DECREF(tmp); | |
d12756d7 | 112 | return 1; |
318ec177 | 113 | } |
d12756d7 | 114 | |
cc0b2b7f | 115 | *real_extra->extra = *real_extra->extra + 1; |
116 | return 0; | |
117 | } | |
118 | ||
dcc1df5e | 119 | static int _dict_iter_get_servers(char const* key, UNUSED_ARG(void* data), void* extra) { |
318ec177 | 120 | PyObject* tmp; |
dcc1df5e | 121 | struct _tuple_dict_extra* real_extra = (struct _tuple_dict_extra*)extra; |
122 | ||
318ec177 | 123 | if ((tmp = PyString_FromString(key)) == NULL) |
124 | return 1; | |
125 | ||
126 | if (PyTuple_SetItem(real_extra->data, *(int*)real_extra->extra, tmp)) { | |
127 | Py_DECREF(tmp); | |
128 | return 1; | |
129 | } | |
130 | ||
dcc1df5e | 131 | *real_extra->extra = *real_extra->extra + 1; |
132 | return 0; | |
133 | } | |
134 | ||
6d94ce8b | 135 | /* get a tuple with all users in it */ |
136 | static PyObject* | |
137 | emb_get_users(UNUSED_ARG(PyObject *self), PyObject *args) { | |
138 | PyObject* retval; | |
5345ea76 | 139 | PyObject* tmp; |
140 | size_t num_clients, n = 0, i; | |
6d94ce8b | 141 | struct _tuple_dict_extra extra; |
142 | ||
143 | if (!PyArg_ParseTuple(args, "")) | |
144 | return NULL; | |
145 | ||
146 | num_clients = dict_size(clients); | |
147 | retval = PyTuple_New(num_clients); | |
5345ea76 | 148 | if (retval == NULL) |
149 | return NULL; | |
6d94ce8b | 150 | |
151 | extra.extra = &n; | |
152 | extra.data = retval; | |
153 | ||
5345ea76 | 154 | if (dict_foreach(clients, _dict_iter_get_users, (void*)&extra) != NULL) { |
155 | for (i = 0; i < n; ++i) { | |
156 | tmp = PyTuple_GetItem(retval, i); | |
157 | PyTuple_SET_ITEM(retval, i, NULL); | |
158 | Py_DECREF(tmp); | |
159 | } | |
160 | Py_DECREF(retval); | |
161 | return NULL; | |
162 | } | |
6d94ce8b | 163 | |
164 | return retval; | |
165 | } | |
166 | ||
cc0b2b7f | 167 | /* get a tuple with all channels in it */ |
168 | static PyObject* | |
169 | emb_get_channels(UNUSED_ARG(PyObject* self), PyObject* args) { | |
170 | PyObject* retval; | |
d12756d7 | 171 | PyObject* tmp; |
172 | size_t num_channels, n = 0, i; | |
cc0b2b7f | 173 | struct _tuple_dict_extra extra; |
174 | ||
175 | if (!PyArg_ParseTuple(args, "")) | |
176 | return NULL; | |
177 | ||
178 | num_channels = dict_size(channels); | |
179 | retval = PyTuple_New(num_channels); | |
d12756d7 | 180 | if (retval == NULL) |
181 | return NULL; | |
cc0b2b7f | 182 | |
183 | extra.extra = &n; | |
184 | extra.data = retval; | |
185 | ||
d12756d7 | 186 | if (dict_foreach(channels, _dict_iter_get_channels, (void*)&extra) != NULL) { |
187 | for (i = 0; i < n; ++i) { | |
188 | tmp = PyTuple_GetItem(retval, i); | |
189 | PyTuple_SET_ITEM(retval, i, NULL); | |
190 | Py_DECREF(tmp); | |
191 | } | |
192 | Py_DECREF(retval); | |
193 | return NULL; | |
194 | } | |
cc0b2b7f | 195 | |
196 | return retval; | |
197 | } | |
198 | ||
dcc1df5e | 199 | static PyObject* |
200 | emb_get_servers(UNUSED_ARG(PyObject* self), PyObject* args) { | |
201 | PyObject* retval; | |
318ec177 | 202 | PyObject* tmp; |
203 | size_t n = 0, i; | |
dcc1df5e | 204 | struct _tuple_dict_extra extra; |
205 | ||
206 | if (!PyArg_ParseTuple(args, "")) | |
207 | return NULL; | |
208 | ||
209 | retval = PyTuple_New(dict_size(servers)); | |
210 | ||
211 | extra.extra = &n; | |
212 | extra.data = retval; | |
213 | ||
318ec177 | 214 | if (dict_foreach(servers, _dict_iter_get_servers, (void*)&extra) != NULL) { |
215 | for (i = 0; i < n; ++i) { | |
216 | tmp = PyTuple_GetItem(retval, i); | |
217 | PyTuple_SET_ITEM(retval, i, NULL); | |
218 | Py_DECREF(tmp); | |
219 | } | |
220 | Py_DECREF(retval); | |
221 | return NULL; | |
222 | } | |
dcc1df5e | 223 | |
224 | return retval; | |
225 | } | |
226 | ||
a2c8c575 | 227 | static PyObject* |
039a6658 | 228 | emb_dump(UNUSED_ARG(PyObject *self), PyObject *args) |
a2c8c575 | 229 | { |
4c216694 | 230 | /* Dump a raw string into the socket |
50d61a79 | 231 | usage: _svc.dump(<P10 string>) |
4c216694 | 232 | */ |
a2c8c575 | 233 | char *buf; |
234 | int ret = 0; | |
235 | char linedup[MAXLEN]; | |
236 | ||
039a6658 | 237 | |
a2c8c575 | 238 | if(!PyArg_ParseTuple(args, "s:dump", &buf )) |
239 | return NULL; | |
a957511b | 240 | |
a2c8c575 | 241 | safestrncpy(linedup, buf, sizeof(linedup)); |
a957511b | 242 | |
a2c8c575 | 243 | if(parse_line(linedup, 1)) { |
244 | irc_raw(buf); | |
245 | ret = 1; | |
a957511b | 246 | } else { |
247 | PyErr_SetString(PyExc_Exception, "invalid protocol message"); | |
248 | return NULL; | |
a2c8c575 | 249 | } |
a957511b | 250 | |
a2c8c575 | 251 | return Py_BuildValue("i", ret); |
252 | } | |
253 | ||
254 | static PyObject* | |
039a6658 | 255 | emb_send_target_privmsg(UNUSED_ARG(PyObject *self), PyObject *args) |
a2c8c575 | 256 | { |
4c216694 | 257 | /* Send a privmsg |
50d61a79 | 258 | usage: _svc.send_target_privmsg(<servicenick_from>, <nick_to>, <message>) |
4c216694 | 259 | */ |
a2c8c575 | 260 | int ret = 0; |
261 | char *servicenick; | |
262 | char *channel; | |
263 | char *buf; | |
264 | ||
265 | struct service *service; | |
266 | ||
039a6658 | 267 | |
a2c8c575 | 268 | if(!PyArg_ParseTuple(args, "sss:reply", &servicenick, &channel, &buf )) |
269 | return NULL; | |
e7af1e12 | 270 | |
271 | if (buf == NULL || strlen(buf) == 0) { | |
272 | PyErr_SetString(PyExc_Exception, "invalid empty message"); | |
273 | return NULL; | |
274 | } | |
275 | ||
a2c8c575 | 276 | if(!(service = service_find(servicenick))) { |
e7af1e12 | 277 | PyErr_SetString(PyExc_Exception, "no such service nick"); |
8d670803 | 278 | return NULL; |
a2c8c575 | 279 | } |
e7af1e12 | 280 | |
281 | ret = send_target_message(5, channel, service->bot, "%s", buf); | |
a2c8c575 | 282 | return Py_BuildValue("i", ret); |
283 | } | |
284 | ||
d4e0f0c4 | 285 | static PyObject* |
039a6658 | 286 | emb_send_target_notice(UNUSED_ARG(PyObject *self), PyObject *args) |
d4e0f0c4 | 287 | { |
4c216694 | 288 | /* send a notice |
50d61a79 | 289 | usage: _svc.send_target_notice(<servicenick_from>, <nick_to>, <message>) |
4c216694 | 290 | */ |
d4e0f0c4 | 291 | int ret = 0; |
292 | char *servicenick; | |
293 | char *target; | |
294 | char *buf; | |
295 | ||
296 | struct service *service; | |
297 | ||
298 | if(!PyArg_ParseTuple(args, "sss:reply", &servicenick, &target, &buf )) | |
299 | return NULL; | |
66f68f65 | 300 | |
301 | if (buf == NULL || strlen(buf) == 0) { | |
302 | PyErr_SetString(PyExc_Exception, "invalid empty message"); | |
303 | return NULL; | |
304 | } | |
305 | ||
d4e0f0c4 | 306 | if(!(service = service_find(servicenick))) { |
66f68f65 | 307 | PyErr_SetString(PyExc_Exception, "no such service nick"); |
d4e0f0c4 | 308 | return NULL; |
309 | } | |
66f68f65 | 310 | |
311 | ret = send_target_message(4, target, service->bot, "%s", buf); | |
312 | ||
d4e0f0c4 | 313 | return Py_BuildValue("i", ret); |
314 | } | |
315 | ||
bfdfd1c3 | 316 | static PyObject* |
317 | pyobj_from_usernode(struct userNode* user) { | |
82089e3f | 318 | unsigned int n, i; |
bfdfd1c3 | 319 | struct modeNode *mn; |
82089e3f | 320 | PyObject* retval = NULL; |
bfdfd1c3 | 321 | PyObject* pChanList = PyTuple_New(user->channels.used); |
322 | ||
82089e3f | 323 | if (pChanList == NULL) |
324 | return NULL; | |
325 | ||
bfdfd1c3 | 326 | for (n=0; n < user->channels.used; n++) { |
327 | mn = user->channels.list[n]; | |
82089e3f | 328 | if (PyTuple_SetItem(pChanList, n, Py_BuildValue("s", mn->channel->name))) |
329 | goto cleanup; | |
bfdfd1c3 | 330 | } |
331 | ||
82089e3f | 332 | retval = Py_BuildValue("{" |
bfdfd1c3 | 333 | "s: s, " /* nick */ |
334 | "s: s, " /* ident */ | |
335 | "s: s, " /* info */ | |
336 | "s: s, " /* hostname */ | |
337 | "s: s, " /* ip */ | |
338 | "s: s, " /* fakehost */ | |
339 | "s: s, " /* sethost */ | |
340 | "s: s, " /* crypthost */ | |
341 | "s: s, " /* cryptip */ | |
bfdfd1c3 | 342 | "s: s, " /* numeric */ |
bfdfd1c3 | 343 | "s: i, " /* loc */ |
344 | "s: i, " /* no_notice */ | |
345 | "s: s, " /* mark */ | |
346 | "s: s, " /* version_reply */ | |
347 | "s: s, " /* account */ | |
348 | "s: O}", /* channels */ | |
349 | "nick", user->nick, | |
350 | "ident", user->ident, | |
351 | "info", user->info, | |
352 | "hostname", user->hostname, | |
353 | "ip", irc_ntoa(&user->ip), | |
354 | "fakehost", user->fakehost, | |
355 | "sethost", user->sethost, | |
356 | "crypthost", user->crypthost, | |
357 | "cryptip", user->cryptip, | |
bfdfd1c3 | 358 | "numeric", user->numeric, |
bfdfd1c3 | 359 | "loc", user->loc, |
360 | "no_notice", user->no_notice, | |
361 | "mark", user->mark, | |
362 | "version_reply", user->version_reply, | |
363 | "account", user->handle_info ? user->handle_info->handle : NULL, | |
364 | "channels", pChanList); | |
82089e3f | 365 | |
366 | if (retval == NULL) | |
367 | goto cleanup; | |
368 | ||
369 | return retval; | |
370 | ||
371 | cleanup: | |
372 | Py_XDECREF(retval); | |
373 | for (i = 0; i < n; ++i) { | |
374 | retval = PyTuple_GetItem(pChanList, i); | |
375 | PyTuple_SET_ITEM(pChanList, i, NULL); | |
376 | Py_XDECREF(retval); | |
377 | } | |
378 | Py_DECREF(pChanList); | |
379 | ||
380 | return NULL; | |
bfdfd1c3 | 381 | } |
382 | ||
8d670803 | 383 | static PyObject* |
039a6658 | 384 | emb_get_user(UNUSED_ARG(PyObject *self), PyObject *args) |
8d670803 | 385 | { |
4c216694 | 386 | /* Get a python object containing everything x3 knows about a user, by nick. |
50d61a79 | 387 | usage: _svc.get_user(<nick>) |
4c216694 | 388 | */ |
54d2fd3d | 389 | char const* nick; |
8d670803 | 390 | struct userNode *user; |
039a6658 | 391 | |
8d670803 | 392 | if(!PyArg_ParseTuple(args, "s", &nick)) |
393 | return NULL; | |
bfdfd1c3 | 394 | |
8d670803 | 395 | if(!(user = GetUserH(nick))) { |
54d2fd3d | 396 | PyErr_SetString(PyExc_Exception, "no such user"); |
8d670803 | 397 | return NULL; |
398 | } | |
bfdfd1c3 | 399 | |
400 | return pyobj_from_usernode(user); | |
8d670803 | 401 | } |
402 | ||
d8f8d3b6 | 403 | static PyObject* |
404 | pyobj_from_server(struct server* srv) { | |
405 | size_t n, idx; | |
406 | PyObject* tmp = NULL; | |
407 | PyObject* retval = NULL; | |
408 | PyObject* users = PyTuple_New(srv->clients); | |
409 | ||
410 | if (users == NULL) | |
411 | return NULL; | |
412 | ||
413 | idx = 0; | |
414 | for (n = 0; n < srv->num_mask; ++n) { | |
415 | if (srv->users[n] == NULL) | |
416 | continue; | |
417 | ||
418 | tmp = PyString_FromString(srv->users[n]->nick); | |
419 | if (tmp == NULL) | |
420 | goto cleanup; | |
421 | ||
422 | if (PyTuple_SetItem(users, idx++, tmp)) | |
423 | goto cleanup; | |
424 | } | |
425 | ||
426 | retval = Py_BuildValue("{" | |
427 | "s:s," /* name */ | |
428 | "s:l," /* boot */ | |
429 | "s:l," /* link_time */ | |
430 | "s:s," /* description */ | |
431 | "s:s," /* numeric */ | |
432 | "s:I," /* num_mask */ | |
433 | "s:I," /* hops */ | |
434 | "s:I," /* clients */ | |
435 | "s:I," /* max_clients */ | |
436 | "s:I," /* burst */ | |
437 | "s:I," /* self_burst */ | |
438 | "s:s" /* uplink */ | |
439 | "s:O" /* users */ | |
440 | /* TODO: Children */ | |
441 | "}", | |
442 | "name", srv->name, | |
443 | "boot", srv->boot, | |
444 | "link_time", srv->link_time, | |
445 | "description", srv->description, | |
446 | "numeric", srv->numeric, | |
447 | "num_mask", srv->num_mask, | |
448 | "hops", srv->hops, | |
449 | "clients", srv->clients, | |
450 | "max_clients", srv->max_clients, | |
451 | "burst", srv->burst, | |
452 | "self_burst", srv->self_burst, | |
453 | "uplink", srv->uplink ? srv->uplink->name : NULL, | |
454 | "users", users | |
455 | ); | |
456 | ||
457 | if (retval == NULL) | |
458 | goto cleanup; | |
459 | ||
460 | return retval; | |
461 | ||
462 | cleanup: | |
463 | Py_XDECREF(retval); | |
464 | ||
465 | for (n = 0; n < idx; ++n) { | |
466 | tmp = PyTuple_GetItem(users, n); | |
467 | PyTuple_SetItem(users, n, NULL); | |
468 | Py_DECREF(tmp); | |
469 | } | |
470 | Py_DECREF(users); | |
471 | ||
472 | return NULL; | |
473 | } | |
474 | ||
475 | static PyObject* | |
476 | emb_get_server(UNUSED_ARG(PyObject* self), PyObject* args) { | |
477 | struct server* srv; | |
478 | char const* name; | |
479 | ||
480 | if (!PyArg_ParseTuple(args, "s", &name)) | |
481 | return NULL; | |
482 | ||
483 | if (name == NULL || strlen(name) == 0) { | |
484 | PyErr_SetString(PyExc_Exception, "invalid server name"); | |
485 | return NULL; | |
486 | } | |
487 | ||
488 | if ((srv = GetServerH(name)) == NULL) { | |
489 | PyErr_SetString(PyExc_Exception, "unknown server"); | |
490 | return NULL; | |
491 | } | |
492 | ||
493 | return pyobj_from_server(srv); | |
494 | } | |
495 | ||
8d670803 | 496 | static PyObject* |
039a6658 | 497 | emb_get_channel(UNUSED_ARG(PyObject *self), PyObject *args) |
8d670803 | 498 | { |
4c216694 | 499 | /* Returns a python dict object with all sorts of info about a channel. |
50d61a79 | 500 | usage: _svc.get_channel(<name>) |
4c216694 | 501 | */ |
8d670803 | 502 | char *name; |
503 | struct chanNode *channel; | |
cbfd323c | 504 | unsigned int n; |
8d670803 | 505 | PyObject *pChannelMembers; |
506 | PyObject *pChannelBans; | |
507 | PyObject *pChannelExempts; | |
508 | ||
039a6658 | 509 | |
8d670803 | 510 | if(!PyArg_ParseTuple(args, "s", &name)) |
511 | return NULL; | |
3f24e818 | 512 | |
8d670803 | 513 | if(!(channel = GetChannel(name))) { |
3f24e818 | 514 | PyErr_SetString(PyExc_Exception, "unknown channel"); |
8d670803 | 515 | return NULL; |
516 | } | |
517 | ||
518 | /* build tuple of nicks in channel */ | |
519 | pChannelMembers = PyTuple_New(channel->members.used); | |
520 | for(n=0;n < channel->members.used;n++) { | |
521 | struct modeNode *mn = channel->members.list[n]; | |
522 | PyTuple_SetItem(pChannelMembers, n, Py_BuildValue("s", mn->user->nick)); | |
523 | } | |
524 | ||
525 | /* build tuple of bans */ | |
526 | pChannelBans = PyTuple_New(channel->banlist.used); | |
527 | for(n=0; n < channel->banlist.used;n++) { | |
528 | struct banNode *bn = channel->banlist.list[n]; | |
529 | PyTuple_SetItem(pChannelBans, n, | |
530 | Py_BuildValue("{s:s,s:s,s:i}", | |
531 | "ban", bn->ban, | |
532 | "who", bn->who, | |
533 | "set", bn->set) | |
534 | ); | |
535 | } | |
536 | ||
8d670803 | 537 | /* build tuple of exempts */ |
538 | pChannelExempts = PyTuple_New(channel->exemptlist.used); | |
539 | for(n=0; n < channel->exemptlist.used;n++) { | |
540 | struct exemptNode *en = channel->exemptlist.list[n]; | |
541 | PyTuple_SetItem(pChannelExempts, n, | |
542 | Py_BuildValue("{s:s,s:s,s:i}", | |
d4e0f0c4 | 543 | "ban", en->exempt, |
8d670803 | 544 | "who", en->who, |
545 | "set", en->set) | |
546 | ); | |
547 | } | |
548 | ||
8d670803 | 549 | return Py_BuildValue("{s:s,s:s,s:s,s:i" |
550 | ",s:i,s:i,s:O,s:O,s:O}", | |
551 | ||
552 | "name", channel->name, | |
553 | "topic", channel->topic, | |
554 | "topic_nick", channel->topic_nick, | |
555 | "topic_time", channel->topic_time, | |
556 | ||
557 | "timestamp", channel->timestamp, | |
558 | "modes", channel->modes, | |
559 | "members", pChannelMembers, | |
560 | "bans", pChannelBans, | |
561 | "exempts", pChannelExempts | |
562 | ); | |
563 | } | |
564 | ||
8d670803 | 565 | static PyObject* |
039a6658 | 566 | emb_get_account(UNUSED_ARG(PyObject *self), PyObject *args) |
8d670803 | 567 | { |
4c216694 | 568 | /* Returns a python dict object with all sorts of info about an account. |
50d61a79 | 569 | usage: _svc.get_account(<account name>) |
4c216694 | 570 | */ |
8d670803 | 571 | char *name; |
4c216694 | 572 | struct handle_info *hi; |
573 | ||
039a6658 | 574 | |
8d670803 | 575 | if(!PyArg_ParseTuple(args, "s", &name)) |
576 | return NULL; | |
4c216694 | 577 | |
578 | hi = get_handle_info(name); | |
5b2b1df2 | 579 | |
4c216694 | 580 | if(!hi) { |
5b2b1df2 | 581 | PyErr_SetString(PyExc_Exception, "unknown account name"); |
4c216694 | 582 | return NULL; |
583 | } | |
5b2b1df2 | 584 | |
4c216694 | 585 | return Py_BuildValue("{s:s,s:i,s:s,s:s,s:s" |
586 | ",s:s,s:s}", | |
587 | ||
588 | "account", hi->handle, | |
589 | "registered", hi->registered, | |
590 | "last_seen", hi->lastseen, | |
591 | "infoline", hi->infoline ? hi->infoline : "", | |
592 | "email", hi->email_addr ? hi->email_addr : "", | |
593 | ||
594 | "fakehost", hi->fakehost ? hi->fakehost : "", | |
595 | "last_quit_host", hi->last_quit_host | |
596 | ||
597 | /* TODO: */ | |
598 | /* users online authed to this account */ | |
599 | /* cookies */ | |
600 | /* nicks (nickserv nets only?) */ | |
601 | /* masks */ | |
602 | /* ignores */ | |
603 | /* channels */ | |
604 | ); | |
8d670803 | 605 | } |
8d670803 | 606 | |
07559983 | 607 | static PyObject* |
039a6658 | 608 | emb_get_info(UNUSED_ARG(PyObject *self), UNUSED_ARG(PyObject *args)) |
609 | { | |
610 | /* return some info about the general setup | |
611 | * of X3, such as what the chanserv's nickname | |
612 | * is. | |
613 | */ | |
614 | ||
615 | ||
616 | return Py_BuildValue("{s:s,s:s,s:s,s:s,s:s}", | |
617 | "chanserv", chanserv? chanserv->nick : "ChanServ", | |
618 | "nickserv", nickserv?nickserv->nick : "NickServ", | |
619 | "opserv", opserv?opserv->nick : "OpServ", | |
620 | "global", global?global->nick : "Global", | |
621 | "spamserv", spamserv?spamserv->nick : "SpamServ"); | |
622 | } | |
623 | ||
624 | static PyObject* | |
625 | emb_log_module(UNUSED_ARG(PyObject *self), PyObject *args) | |
07559983 | 626 | { |
627 | /* a gateway to standard X3 logging subsystem. | |
628 | * level is a value 0 to 9 as defined by the log_severity enum in log.h. | |
07559983 | 629 | * |
630 | * for now, all logs go to the PY_LOG log. In the future this will change. | |
631 | */ | |
632 | char *message; | |
633 | int ret = 0; | |
634 | int level; | |
635 | ||
039a6658 | 636 | |
07559983 | 637 | if(!PyArg_ParseTuple(args, "is", &level, &message)) |
638 | return NULL; | |
639 | ||
640 | log_module(PY_LOG, level, "%s", message); | |
641 | ||
642 | return Py_BuildValue("i", ret); | |
643 | } | |
8d670803 | 644 | |
0076604e | 645 | static PyObject* |
646 | emb_kill(UNUSED_ARG(PyObject* self), PyObject* args) { | |
647 | char const* from_nick, *target_nick, *message; | |
648 | struct userNode *target; | |
649 | struct service *service; | |
650 | ||
651 | if (!PyArg_ParseTuple(args, "sss", &from_nick, &target_nick, &message)) | |
652 | return NULL; | |
653 | ||
654 | if(!(service = service_find(from_nick))) { | |
655 | PyErr_SetString(PyExc_Exception, "unknown service user specified as from user"); | |
656 | return NULL; | |
657 | } | |
658 | ||
659 | if ((target = GetUserH(target_nick)) == NULL) { | |
660 | PyErr_SetString(PyExc_Exception, "unknown target user"); | |
661 | return NULL; | |
662 | } | |
663 | ||
664 | irc_kill(service->bot, target, message); | |
665 | ||
666 | return Py_None; | |
667 | } | |
668 | ||
a2c8c575 | 669 | static PyMethodDef EmbMethods[] = { |
07559983 | 670 | /* Communication methods */ |
a2c8c575 | 671 | {"dump", emb_dump, METH_VARARGS, "Dump raw P10 line to server"}, |
672 | {"send_target_privmsg", emb_send_target_privmsg, METH_VARARGS, "Send a message to somewhere"}, | |
d4e0f0c4 | 673 | {"send_target_notice", emb_send_target_notice, METH_VARARGS, "Send a notice to somewhere"}, |
07559983 | 674 | {"log_module", emb_log_module, METH_VARARGS, "Log something using the X3 log subsystem"}, |
039a6658 | 675 | //TODO: {"exec_cmd", emb_exec_cmd, METH_VARARGS, "execute x3 command provided"}, |
676 | // This should use environment from "python command" call to pass in, if available | |
0076604e | 677 | {"kill", emb_kill, METH_VARARGS, "Kill someone"}, |
039a6658 | 678 | //TODO: {"shun" |
679 | //TODO: {"unshun" | |
680 | //TODO: {"gline", emb_gline, METH_VARARGS, "gline a mask"}, | |
681 | //TODO: {"ungline", emb_ungline, METH_VARARGS, "remove a gline"}, | |
682 | //TODO: {"kick", emb_kick, METH_VARARGS, "kick someone from a channel"}, | |
683 | //TODO: {"channel_mode", emb_channel_mode, METH_VARARGS, "set modes on a channel"}, | |
684 | //TODO: {"user_mode", emb_user_mode, METH_VARARGS, "Have x3 set usermodes on one of its own nicks"}, | |
685 | // | |
686 | //TODO: {"get_config", emb_get_config, METH_VARARGS, "get x3.conf settings into a nested dict"}, | |
687 | //TODO: {"config_set", emb_config_set, METH_VARARGS, "change a config setting 'on-the-fly'."}, | |
688 | // | |
689 | //TODO: {"timeq_add", emb_timeq_new, METH_VARARGS, "some kind of interface to the timed event system."}, | |
690 | //TODO: {"timeq_del", emb_timeq_new, METH_VARARGS, "some kind of interface to the timed event system."}, | |
07559983 | 691 | /* Information gathering methods */ |
8d670803 | 692 | {"get_user", emb_get_user, METH_VARARGS, "Get details about a nickname"}, |
f0e11521 | 693 | {"get_users", emb_get_users, METH_VARARGS, "Get all connected users"}, |
8d670803 | 694 | {"get_channel", emb_get_channel, METH_VARARGS, "Get details about a channel"}, |
f0e11521 | 695 | {"get_channels", emb_get_channels, METH_VARARGS, "Get all channels"}, |
d8f8d3b6 | 696 | {"get_server", emb_get_server, METH_VARARGS, "Get details about a server"}, |
dcc1df5e | 697 | {"get_servers", emb_get_servers, METH_VARARGS, "Get all server names"}, |
4c216694 | 698 | {"get_account", emb_get_account, METH_VARARGS, "Get details about an account"}, |
039a6658 | 699 | {"get_info", emb_get_info, METH_VARARGS, "Get various misc info about x3"}, |
07559983 | 700 | /* null terminator */ |
a2c8c575 | 701 | {NULL, NULL, 0, NULL} |
702 | }; | |
703 | ||
704 | ||
f0e11521 | 705 | /* |
706 | These functions set up the embedded environment for us to call out to | |
707 | modpython.py class methods. | |
4c216694 | 708 | */ |
cbfd323c | 709 | |
07559983 | 710 | void python_log_module() { |
711 | /* Attempt to convert python errors to x3 log system */ | |
712 | PyObject *exc, *tb, *value, *tmp; | |
713 | char *str_exc = "NONE"; | |
714 | char *str_value = "NONE"; | |
715 | char *str_tb = "NONE"; | |
716 | ||
717 | PyErr_Fetch(&exc, &value, &tb); | |
718 | ||
719 | if(exc) { | |
720 | if((tmp = PyObject_Str(exc))) | |
721 | str_exc = PyString_AsString(tmp); | |
722 | } | |
723 | if(value) { | |
724 | if((tmp = PyObject_Str(value))) | |
725 | str_value = PyString_AsString(tmp); | |
726 | } | |
727 | if(tb) { | |
728 | if((tmp = PyObject_Str(tb))) | |
729 | str_tb = PyString_AsString(tmp); | |
730 | } | |
731 | ||
732 | /* Now restore it so we can print it (just in case) | |
733 | * (should we do this only when running in debug mode?) */ | |
734 | PyErr_Restore(exc, value, tb); | |
735 | PyErr_Print(); /* which of course, clears it again.. */ | |
736 | ||
737 | log_module(PY_LOG, LOG_WARNING, "PYTHON error: %s, value: %s", str_exc, str_value); | |
738 | ||
739 | /* TODO: get the traceback using the traceback module via C api so we can add it to the X3 logs. See | |
740 | * http://mail.python.org/pipermail/python-list/2003-March/192226.html */ | |
741 | // (this doesnt work, str_tb is just an object hashid) log_module(PY_LOG, LOG_INFO, "PYTHON error, traceback: %s", str_tb); | |
742 | } | |
743 | ||
744 | ||
e0f76584 | 745 | PyObject *python_build_handler_args(size_t argc, char *args[], PyObject *pIrcObj) { |
4c216694 | 746 | /* Sets up a python tuple with passed in arguments, prefixed by the Irc instance |
f0e11521 | 747 | which handlers use to interact with C. |
4c216694 | 748 | argc = number of args |
749 | args = array of args | |
750 | pIrcObj = instance of the irc class | |
751 | */ | |
e0f76584 | 752 | size_t i = 0, n; |
753 | PyObject *pArgs = NULL; | |
cbfd323c | 754 | |
e0f76584 | 755 | pArgs = PyTuple_New(argc + 1); |
756 | Py_INCREF(pIrcObj); | |
757 | PyTuple_SetItem(pArgs, i++, pIrcObj); | |
cbfd323c | 758 | |
e0f76584 | 759 | if(args && argc) { |
cbfd323c | 760 | PyObject *pValue; |
e0f76584 | 761 | for(n = 0; n < argc; ++n) { |
762 | pValue = PyString_FromString(args[n]); | |
cbfd323c | 763 | if(!pValue) { |
e0f76584 | 764 | Py_DECREF(pArgs); |
765 | log_module(PY_LOG, LOG_INFO, "Unable to convert '%s' to python string", args[n]); | |
766 | return NULL; | |
cbfd323c | 767 | } |
e0f76584 | 768 | PyTuple_SetItem(pArgs, n+i, pValue); |
cbfd323c | 769 | } |
cbfd323c | 770 | } |
e0f76584 | 771 | return pArgs; |
cbfd323c | 772 | } |
773 | ||
774 | PyObject *python_build_args(size_t argc, char *args[]) { | |
4c216694 | 775 | /* Builds the passed in arguments into a python argument tuple. |
776 | argc = number of args | |
777 | args = array of args | |
778 | */ | |
cbfd323c | 779 | size_t i; |
780 | PyObject *pArgs = NULL; | |
781 | ||
782 | if(args && argc) { | |
783 | pArgs = PyTuple_New(argc); | |
784 | PyObject *pValue; | |
785 | for(i = 0; i< argc; ++i) { | |
786 | pValue = PyString_FromString(args[i]); | |
787 | if(!pValue) { | |
788 | Py_DECREF(pArgs); | |
789 | log_module(PY_LOG, LOG_INFO, "Unable to convert '%s' to python string", args[i]); | |
790 | return NULL; | |
791 | } | |
792 | PyTuple_SetItem(pArgs, i, pValue); | |
793 | } | |
794 | } | |
795 | return pArgs; | |
d4e0f0c4 | 796 | } |
caf97651 | 797 | |
cbfd323c | 798 | |
e0f76584 | 799 | PyObject *new_irc_object(char *command_service, char *command_caller, char *command_target) { |
4c216694 | 800 | /* Creates a new instance of the irc class (from modpython.py) which is initalized |
801 | with current environment details like which service were talking to. | |
802 | command_service = which service we are talking to, or empty string if none | |
803 | command_caller = nick of user generating message, or empty string if none | |
804 | command_target = If were reacting to something on a channel, this will | |
805 | be set to the name of the channel. Otherwise empty | |
806 | */ | |
e0f76584 | 807 | PyObject *pIrcArgs = NULL; |
808 | PyObject *pIrcClass; | |
809 | PyObject *pIrcObj; | |
810 | ||
4c216694 | 811 | log_module(PY_LOG, LOG_INFO, "Attempting to instanciate irc class; %s %s %s", command_service, command_caller, command_target); |
e0f76584 | 812 | pIrcClass = PyObject_GetAttrString(base_module, "irc"); |
813 | /* pIrcClass is a new reference */ | |
814 | if(pIrcClass && PyCallable_Check(pIrcClass)) { | |
815 | //size_t i; | |
816 | char *ircargs[] = {command_service, command_caller, command_target}; | |
817 | //PyObject *pValue; | |
818 | ||
819 | pIrcArgs = python_build_args(3, ircargs); | |
e0f76584 | 820 | pIrcObj = PyObject_CallObject(pIrcClass, pIrcArgs); |
821 | if(!pIrcObj) { | |
822 | log_module(PY_LOG, LOG_ERROR, "IRC Class failed to load"); | |
07559983 | 823 | python_log_module(); |
824 | //PyErr_Print(); | |
e0f76584 | 825 | } |
826 | if(pIrcArgs != NULL) { | |
827 | Py_DECREF(pIrcArgs); | |
828 | } | |
4c216694 | 829 | Py_DECREF(pIrcClass); |
e0f76584 | 830 | return pIrcObj; |
831 | } | |
832 | else { | |
4c216694 | 833 | /* need to free pIrcClass here if it WAS found but was NOT callable? */ |
e0f76584 | 834 | log_module(PY_LOG, LOG_ERROR, "Unable to find irc class"); |
835 | return NULL; | |
836 | } | |
837 | } | |
838 | ||
cbfd323c | 839 | int python_call_handler(char *handler, char *args[], size_t argc, char *command_service, char *command_caller, char *command_target) { |
4c216694 | 840 | /* This is how we talk to modpython.c. First a new instance of the irc class is created using these |
841 | arguments to setup the current environment. Then the named method of the handler object is called | |
842 | with the givin arguments. | |
cbfd323c | 843 | */ |
844 | PyObject *pIrcObj; | |
845 | PyObject *pArgs; | |
846 | PyObject *pMethod; | |
847 | PyObject *pValue; | |
848 | ||
e0f76584 | 849 | log_module(PY_LOG, LOG_INFO, "attempting to call handler %s.", handler); |
07559983 | 850 | if(base_module != NULL && handler_object != NULL) { |
cbfd323c | 851 | pIrcObj = new_irc_object(command_service, command_caller, command_target); |
e0f76584 | 852 | if(!pIrcObj) { |
853 | log_module(PY_LOG, LOG_INFO, "Can't get irc object. Bailing."); | |
854 | return 0; | |
855 | } | |
cbfd323c | 856 | |
e0f76584 | 857 | pArgs = python_build_handler_args(argc, args, pIrcObj); |
cbfd323c | 858 | pMethod = PyObject_GetAttrString(handler_object, handler); |
859 | if(pMethod && PyCallable_Check(pMethod)) { | |
07559983 | 860 | /* Call the method, with the arguments */ |
cbfd323c | 861 | pValue = PyObject_CallObject(pMethod, pArgs); |
862 | if(pArgs) { | |
863 | Py_DECREF(pArgs); | |
864 | } | |
865 | if(pValue != NULL) { | |
866 | int ret; | |
867 | ret = PyInt_AsLong(pValue); | |
868 | if(ret == -1 && PyErr_Occurred()) { | |
07559983 | 869 | //PyErr_Print(); |
cbfd323c | 870 | log_module(PY_LOG, LOG_INFO, "error converting return value of handler %s to type long. ", handler); |
07559983 | 871 | python_log_module(); |
cbfd323c | 872 | ret = 0; |
873 | } | |
874 | log_module(PY_LOG, LOG_INFO, "handler %s was run successfully, returned %d.", handler, ret); | |
cbfd323c | 875 | Py_DECREF(pValue); |
4c216694 | 876 | Py_DECREF(pIrcObj); |
877 | Py_DECREF(pMethod); | |
cbfd323c | 878 | return ret; |
879 | } | |
880 | else { | |
cbfd323c | 881 | /* TODO: instead of print errors, get them as strings |
882 | * and deal with them with normal x3 log system. */ | |
cbfd323c | 883 | log_module(PY_LOG, LOG_WARNING, "call to handler %s failed", handler); |
07559983 | 884 | //PyErr_Print(); |
885 | python_log_module(); | |
4c216694 | 886 | Py_DECREF(pIrcObj); |
887 | Py_DECREF(pMethod); | |
cbfd323c | 888 | return 0; |
889 | } | |
890 | } | |
891 | else { /* couldn't find handler methed */ | |
4c216694 | 892 | Py_DECREF(pArgs); |
893 | /* Free pMethod if it was found but not callable? */ | |
cbfd323c | 894 | log_module(PY_LOG, LOG_ERROR, "Cannot find handler %s.", handler); |
895 | return 0; | |
896 | ||
897 | } | |
898 | } | |
899 | else { /* No base module.. no python? */ | |
e0f76584 | 900 | log_module(PY_LOG, LOG_INFO, "Cannot handle %s, Python is not initialized.", handler); |
cbfd323c | 901 | return 0; |
902 | } | |
903 | } | |
904 | ||
905 | PyObject *python_new_handler_object() { | |
4c216694 | 906 | /* Create a new instance of the handler class. |
907 | This is called during python initilization (or reload) | |
908 | and the result is saved and re-used. | |
909 | */ | |
cbfd323c | 910 | PyObject *pHandlerClass, *pHandlerObj; |
911 | ||
912 | log_module(PY_LOG, LOG_INFO, "Attempting to instanciate python class handler"); | |
913 | pHandlerClass = PyObject_GetAttrString(base_module, "handler"); | |
914 | /* Class is a new reference */ | |
915 | if(pHandlerClass && PyCallable_Check(pHandlerClass)) { | |
e0f76584 | 916 | /*PyObject *pValue; */ |
cbfd323c | 917 | |
918 | pHandlerObj = PyObject_CallObject(pHandlerClass, NULL); | |
07559983 | 919 | if(pHandlerObj != NULL) { |
920 | log_module(PY_LOG, LOG_INFO, "Created new python handler object."); | |
921 | return pHandlerObj; | |
922 | } | |
923 | else { | |
924 | log_module(PY_LOG, LOG_ERROR, "Unable to instanciate handler object"); | |
925 | //PyErr_Print(); | |
926 | python_log_module(); | |
927 | return NULL; | |
928 | } | |
cbfd323c | 929 | } |
930 | else { | |
931 | log_module(PY_LOG, LOG_ERROR, "Unable to find handler class"); | |
07559983 | 932 | //PyErr_Print(); |
933 | python_log_module(); | |
934 | if(pHandlerClass) { | |
935 | Py_DECREF(pHandlerClass); | |
936 | } | |
cbfd323c | 937 | return NULL; |
938 | } | |
939 | } | |
940 | ||
4c216694 | 941 | /* ------------------------------------------------------------------------------- * |
942 | Some gateway functions to convert x3 callbacks into modpython.py callbacks. | |
943 | Mostly we just build relevant args and call the proper handler method | |
944 | ||
945 | debate: do we just register these and check them in python | |
946 | for every one (slow?) or actually work out if a plugin needs | |
947 | it first? We will start by doing it every time. | |
cbfd323c | 948 | */ |
0b350353 | 949 | static int |
950 | python_handle_join(struct modeNode *mNode) | |
951 | { | |
4c216694 | 952 | /* callback for handle_join events. |
953 | */ | |
0b350353 | 954 | struct userNode *user = mNode->user; |
955 | struct chanNode *channel = mNode->channel; | |
956 | ||
a2c8c575 | 957 | |
caf97651 | 958 | log_module(PY_LOG, LOG_INFO, "python module handle_join"); |
a2c8c575 | 959 | if(!channel||!user) { |
4c216694 | 960 | log_module(PY_LOG, LOG_WARNING, "Python code got join without channel or user!"); |
a2c8c575 | 961 | return 0; |
962 | } | |
963 | else { | |
964 | char *args[] = {channel->name, user->nick}; | |
e0f76584 | 965 | return python_call_handler("join", args, 2, "", "", ""); |
a2c8c575 | 966 | } |
0b350353 | 967 | } |
968 | ||
0ab7b4bc | 969 | static int |
970 | python_handle_server_link(struct server *server) | |
971 | { | |
972 | log_module(PY_LOG, LOG_INFO, "python module handle_server_link"); | |
973 | if(!server) { | |
974 | log_module(PY_LOG, LOG_WARNING, "Python code got server link without server!"); | |
975 | return 0; | |
976 | } | |
977 | else { | |
978 | char *args[] = {server->name, server->description}; | |
979 | return python_call_handler("server_link", args, 2, "", "", ""); | |
980 | } | |
981 | } | |
982 | ||
983 | static int | |
984 | python_handle_new_user(struct userNode *user) | |
985 | { | |
986 | log_module(PY_LOG, LOG_INFO, "Python module handle_new_user"); | |
987 | if(!user) { | |
988 | log_module(PY_LOG, LOG_WARNING, "Python code got new_user without the user"); | |
989 | return 0; | |
990 | } | |
991 | else { | |
992 | char *args[] = {user->nick, user->ident, user->hostname, user->info}; | |
993 | return python_call_handler("new_user", args, 4, "", "", ""); | |
994 | } | |
995 | } | |
996 | ||
997 | static void | |
998 | python_handle_nick_change(struct userNode *user, const char *old_nick) | |
999 | { | |
1000 | log_module(PY_LOG, LOG_INFO, "Python module handle_nick_change"); | |
1001 | if(!user) { | |
1002 | log_module(PY_LOG, LOG_WARNING, "Python code got nick_change without the user!"); | |
1003 | } | |
1004 | else { | |
1005 | char *args[] = {user->nick, (char *)old_nick}; | |
1006 | python_call_handler("nick_change", args, 2, "", "", ""); | |
1007 | } | |
1008 | } | |
1009 | ||
4c216694 | 1010 | /* ----------------------------------------------------------------------------- */ |
1011 | ||
cbfd323c | 1012 | |
caf97651 | 1013 | int python_load() { |
4c216694 | 1014 | /* Init the python engine and do init work on modpython.py |
1015 | This is called during x3 startup, and on a python reload | |
1016 | */ | |
caf97651 | 1017 | PyObject *pName; |
ef5e0305 | 1018 | char* buffer; |
1019 | char* env = getenv("PYTHONPATH"); | |
1020 | ||
1021 | if (env) | |
1022 | env = strdup(env); | |
1023 | ||
1024 | if (!env) | |
1025 | setenv("PYTHONPATH", modpython_conf.scripts_dir, 1); | |
1026 | else if (!strstr(env, modpython_conf.scripts_dir)) { | |
1027 | buffer = (char*)malloc(strlen(env) + strlen(modpython_conf.scripts_dir) + 2); | |
1028 | sprintf(buffer, "%s:%s", modpython_conf.scripts_dir, env); | |
1029 | setenv("PYTHONPATH", buffer, 1); | |
1030 | free(buffer); | |
1031 | free(env); | |
1032 | } | |
caf97651 | 1033 | |
caf97651 | 1034 | Py_Initialize(); |
50d61a79 | 1035 | Py_InitModule("_svc", EmbMethods); |
ed8d873c | 1036 | pName = PyString_FromString(modpython_conf.main_module); |
caf97651 | 1037 | base_module = PyImport_Import(pName); |
1038 | Py_DECREF(pName); | |
bc2f52df | 1039 | |
1040 | Py_XDECREF(handler_object); | |
1041 | handler_object = NULL; | |
1042 | ||
caf97651 | 1043 | if(base_module != NULL) { |
cbfd323c | 1044 | handler_object = python_new_handler_object(); |
1045 | if(handler_object) { | |
e0f76584 | 1046 | python_call_handler("init", NULL, 0, "", "", ""); |
cbfd323c | 1047 | return 1; |
1048 | } | |
1049 | else { | |
1050 | /* error handler class not found */ | |
1051 | log_module(PY_LOG, LOG_WARNING, "Failed to create handler object"); | |
1052 | return 0; | |
1053 | } | |
caf97651 | 1054 | } |
1055 | else { | |
07559983 | 1056 | //PyErr_Print(); |
1057 | python_log_module(); | |
d4e0f0c4 | 1058 | log_module(PY_LOG, LOG_WARNING, "Failed to load modpython.py"); |
a2c8c575 | 1059 | return 0; |
caf97651 | 1060 | } |
cbfd323c | 1061 | return 0; |
caf97651 | 1062 | } |
1063 | ||
caf97651 | 1064 | int |
1065 | python_finalize(void) { | |
4c216694 | 1066 | /* Called after X3 is fully up and running. |
1067 | Code can be put here that needs to run to init things, but | |
1068 | which is sensitive to everything else in x3 being up and ready | |
1069 | to go. | |
1070 | */ | |
caf97651 | 1071 | |
1072 | PyRun_SimpleString("print 'Hello, World of Python!'"); | |
1073 | log_module(PY_LOG, LOG_INFO, "python module finalize"); | |
1074 | ||
1075 | return 1; | |
1076 | } | |
1077 | ||
caf97651 | 1078 | static void |
4c216694 | 1079 | python_cleanup(void) { |
1080 | /* Called on shutdown of the python module (or before reloading) | |
1081 | */ | |
413fd8ea | 1082 | |
caf97651 | 1083 | log_module(PY_LOG, LOG_INFO, "python module cleanup"); |
413fd8ea | 1084 | if (PyErr_Occurred()) |
1085 | PyErr_Clear(); | |
f0e11521 | 1086 | Py_Finalize(); /* Shut down python enterpreter */ |
caf97651 | 1087 | } |
1088 | ||
4c216694 | 1089 | /* ---------------------------------------------------------------------------------- * |
1090 | Python module command handlers. | |
1091 | */ | |
caf97651 | 1092 | static MODCMD_FUNC(cmd_reload) { |
4c216694 | 1093 | /* reload the python system completely |
1094 | */ | |
caf97651 | 1095 | log_module(PY_LOG, LOG_INFO, "Shutting python down"); |
1096 | python_cleanup(); | |
1097 | log_module(PY_LOG, LOG_INFO, "Loading python stuff"); | |
1098 | if(python_load()) { | |
a2c8c575 | 1099 | reply("PYMSG_RELOAD_SUCCESS"); |
caf97651 | 1100 | } |
1101 | else { | |
a2c8c575 | 1102 | reply("PYMSG_RELOAD_FAILED"); |
caf97651 | 1103 | } |
1104 | return 1; | |
1105 | } | |
1106 | ||
413fd8ea | 1107 | static char* format_python_error(int space_nls) { |
1108 | PyObject* extype = NULL, *exvalue = NULL, *extraceback = NULL; | |
1109 | PyObject* pextypestr = NULL, *pexvaluestr = NULL; | |
1110 | char* extypestr = NULL, *exvaluestr = NULL; | |
1111 | size_t retvallen = 0; | |
1112 | char* retval = NULL, *tmp; | |
1113 | ||
1114 | PyErr_Fetch(&extype, &exvalue, &extraceback); | |
1115 | if (!extype) | |
1116 | goto cleanup; | |
1117 | ||
1118 | pextypestr = PyObject_Str(extype); | |
1119 | if (!pextypestr) | |
1120 | goto cleanup; | |
1121 | extypestr = PyString_AsString(pextypestr); | |
1122 | if (!extypestr) | |
1123 | goto cleanup; | |
1124 | ||
1125 | pexvaluestr = PyObject_Str(exvalue); | |
1126 | if (pexvaluestr) | |
1127 | exvaluestr = PyString_AsString(pexvaluestr); | |
1128 | ||
1129 | retvallen = strlen(extypestr) + (exvaluestr ? strlen(exvaluestr) + 2 : 0) + 1; | |
1130 | retval = (char*)malloc(retvallen); | |
1131 | if (exvaluestr) | |
1132 | snprintf(retval, retvallen, "%s: %s", extypestr, exvaluestr); | |
1133 | else | |
1134 | strncpy(retval, extypestr, retvallen); | |
1135 | ||
1136 | if (space_nls) { | |
1137 | tmp = retval; | |
1138 | while (*tmp) { | |
1139 | if (*tmp == '\n') | |
1140 | *tmp = ' '; | |
1141 | ++tmp; | |
1142 | } | |
1143 | } | |
1144 | ||
1145 | cleanup: | |
1146 | if (PyErr_Occurred()) | |
1147 | PyErr_Clear(); /* ignore errors caused by formatting */ | |
1148 | Py_XDECREF(extype); | |
1149 | Py_XDECREF(exvalue); | |
1150 | Py_XDECREF(extraceback); | |
1151 | Py_XDECREF(pextypestr); | |
1152 | Py_XDECREF(pexvaluestr); | |
1153 | ||
1154 | if (retval) | |
1155 | return retval; | |
1156 | ||
1157 | return strdup("unknown exception"); | |
1158 | } | |
1159 | ||
8d670803 | 1160 | static MODCMD_FUNC(cmd_run) { |
413fd8ea | 1161 | /* this method allows running arbitrary python commands. |
1162 | * use with care. | |
1163 | */ | |
1164 | char* msg; | |
46f628b1 | 1165 | PyObject* py_main_module; |
1166 | PyObject* py_globals; | |
1167 | PyObject* py_locals; | |
1168 | PyObject* py_retval; | |
1169 | PyObject* extype, *exvalue, *extraceback; | |
1170 | PyObject* exvaluestr = NULL; | |
1171 | char* exmsg = NULL, *exmsgptr; | |
1172 | ||
1173 | py_main_module = PyImport_AddModule("__main__"); | |
1174 | py_globals = py_locals = PyModule_GetDict(py_main_module); | |
413fd8ea | 1175 | |
a2c8c575 | 1176 | msg = unsplit_string(argv + 1, argc - 1, NULL); |
413fd8ea | 1177 | |
46f628b1 | 1178 | py_retval = PyRun_String(msg, Py_file_input, py_globals, py_locals); |
1179 | if (py_retval == NULL) { | |
1180 | PyErr_Fetch(&extype, &exvalue, &extraceback); | |
1181 | if (exvalue != NULL) { | |
1182 | exvaluestr = PyObject_Str(exvalue); | |
1183 | exmsg = strdup(PyString_AS_STRING(exvaluestr)); | |
1184 | exmsgptr = exmsg; | |
1185 | while (exmsgptr && *exmsgptr) { | |
1186 | if (*exmsgptr == '\n' || *exmsgptr == '\r' || *exmsgptr == '\t') | |
1187 | *exmsgptr = ' '; | |
1188 | exmsgptr++; | |
1189 | } | |
1190 | } | |
1191 | if (extype != NULL && exvalue != NULL && PyType_Check(extype)) { | |
1192 | reply("PYMSG_RUN_EXCEPTION", ((PyTypeObject*)extype)->tp_name, exmsg); | |
413fd8ea | 1193 | } else |
1194 | reply("PYMSG_RUN_UNKNOWN_EXCEPTION"); | |
46f628b1 | 1195 | |
1196 | if (extype != NULL) | |
1197 | Py_DECREF(extype); | |
1198 | if (exvalue != NULL) | |
1199 | Py_DECREF(exvalue); | |
1200 | if (extraceback != NULL) | |
1201 | Py_DECREF(extraceback); | |
1202 | if (exvaluestr != NULL) | |
1203 | Py_DECREF(exvaluestr); | |
1204 | if (exmsg) | |
1205 | free(exmsg); | |
1206 | } else { | |
1207 | Py_DECREF(py_retval); | |
413fd8ea | 1208 | } |
1209 | ||
a2c8c575 | 1210 | return 1; |
1211 | } | |
1212 | ||
07559983 | 1213 | #define numstrargs(X) sizeof(X) / sizeof(*X) |
1214 | static MODCMD_FUNC(cmd_command) { | |
1215 | char *plugin = argv[1]; | |
1216 | char *command = argv[2]; | |
1217 | char *msg; /* args */ | |
1218 | if(argc > 3) { | |
1219 | msg = unsplit_string(argv + 3, argc - 3, NULL); | |
1220 | } | |
1221 | else { | |
1222 | msg = ""; | |
1223 | } | |
1224 | char *args[] = {plugin, command, msg}; | |
1225 | python_call_handler("cmd_command", args, numstrargs(args), cmd->parent->bot->nick, user?user->nick:"", channel?channel->name:""); | |
1226 | return 1; | |
1227 | } | |
1228 | ||
ef5e0305 | 1229 | static void modpython_conf_read(void) { |
1230 | dict_t conf_node; | |
1231 | char const* str; | |
1232 | ||
1233 | if (!(conf_node = conf_get_data(MODPYTHON_CONF_NAME, RECDB_OBJECT))) { | |
1234 | log_module(PY_LOG, LOG_ERROR, "config node '%s' is missing or has wrong type", MODPYTHON_CONF_NAME); | |
1235 | return; | |
1236 | } | |
1237 | ||
1238 | str = database_get_data(conf_node, "scripts_dir", RECDB_QSTRING); | |
1239 | modpython_conf.scripts_dir = str ? str : "./"; | |
ed8d873c | 1240 | |
1241 | str = database_get_data(conf_node, "main_module", RECDB_QSTRING); | |
1242 | modpython_conf.main_module = str ? str : "modpython"; | |
ef5e0305 | 1243 | } |
1244 | ||
0b350353 | 1245 | int python_init(void) { |
4c216694 | 1246 | /* X3 calls this function on init of the module during startup. We use it to |
1247 | do all our setup tasks and bindings | |
1248 | */ | |
0b350353 | 1249 | |
0b350353 | 1250 | PY_LOG = log_register_type("Python", "file:python.log"); |
caf97651 | 1251 | python_module = module_register("python", PY_LOG, "mod-python.help", NULL); |
ef5e0305 | 1252 | conf_register_reload(modpython_conf_read); |
1253 | ||
caf97651 | 1254 | log_module(PY_LOG, LOG_INFO, "python module init"); |
1255 | message_register_table(msgtab); | |
1256 | ||
0b350353 | 1257 | /* |
1258 | reg_auth_func(python_check_messages); | |
1259 | reg_handle_rename_func(python_rename_account); | |
1260 | reg_unreg_func(python_unreg_account); | |
1261 | conf_register_reload(python_conf_read); | |
0b350353 | 1262 | saxdb_register("python", python_saxdb_read, python_saxdb_write); |
0b350353 | 1263 | modcmd_register(python_module, "send", cmd_send, 3, MODCMD_REQUIRE_AUTHED, NULL); |
1264 | */ | |
caf97651 | 1265 | modcmd_register(python_module, "reload", cmd_reload, 1, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL); |
8d670803 | 1266 | modcmd_register(python_module, "run", cmd_run, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL); |
07559983 | 1267 | modcmd_register(python_module, "command", cmd_command, 3, MODCMD_REQUIRE_STAFF, NULL); |
039a6658 | 1268 | |
f0e11521 | 1269 | // Please help us by implementing any of the callbacks listed as TODO below. They already exist |
1270 | // in x3, they just need handle_ bridges implemented. (see python_handle_join for an example) | |
0ab7b4bc | 1271 | reg_server_link_func(python_handle_server_link); |
1272 | reg_new_user_func(python_handle_new_user); | |
1273 | reg_nick_change_func(python_handle_nick_change); | |
039a6658 | 1274 | //TODO: reg_del_user_func(python_handle_del_user); |
1275 | //TODO: reg_account_func(python_handle_account); /* stamping of account name to the ircd */ | |
1276 | //TODO: reg_handle_rename_func(python_handle_handle_rename); /* handle used to ALSO mean account name */ | |
1277 | //TODO: reg_failpw_func(python_handle_failpw); | |
1278 | //TODO: reg_allowauth_func(python_handle_allowauth); | |
1279 | //TODO: reg_handle_merge_func(python_handle_merge); | |
1280 | // | |
1281 | //TODO: reg_oper_func(python_handle_oper); | |
1282 | //TODO: reg_new_channel_func(python_handle_new_channel); | |
caf97651 | 1283 | reg_join_func(python_handle_join); |
039a6658 | 1284 | //TODO: reg_del_channel_func(python_handle_del_channel); |
1285 | //TODO: reg_part_func(python_handle_part); | |
1286 | //TODO: reg_kick_func(python_handle_kick); | |
1287 | //TODO: reg_topic_func(python_handle_topic); | |
1288 | //TODO: reg_channel_mode_func(python_handle_channel_mode); | |
1289 | ||
1290 | //TODO: reg_privmsg_func(python_handle_privmsg); | |
1291 | //TODO: reg_notice_func | |
1292 | //TODO: reg_svccmd_unbind_func(python_handle_svccmd_unbind); | |
1293 | //TODO: reg_chanmsg_func(python_handle_chanmsg); | |
1294 | //TODO: reg_allchanmsg_func | |
1295 | //TODO: reg_user_mode_func | |
1296 | ||
caf97651 | 1297 | reg_exit_func(python_cleanup); |
0b350353 | 1298 | |
caf97651 | 1299 | python_load(); |
0b350353 | 1300 | return 1; |
1301 | } | |
1302 | ||
1303 | #endif /* WITH_PYTHON */ |