From: rubin Date: Tue, 25 Nov 2008 07:24:24 +0000 (+0000) Subject: python reply() added X-Git-Tag: 1.9~263 X-Git-Url: https://jfr.im/git/irc/evilnet/x3.git/commitdiff_plain/d4e0f0c40f2670ffb52bb26e4a83f3c608ffe69a python reply() added --- diff --git a/ChangeLog b/ChangeLog index d763f1f..8b937ab 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,15 @@ /*********************************************************************** X3 ChangeLog +2008-11-25 Alex Schumann + + * src/mod-python.c: call run function instead of using + pyrun_simplestring. Set global info for simple reply() to have what it + needs to run. + + * src/modpython.py: new name as - interferes with module loading. + added reply() function + 2008-11-22 Alex Schumann * src/mod-python.c: Added ability to query info about nick or channel diff --git a/src/mod-python.c b/src/mod-python.c index aee9dd5..ac3b410 100644 --- a/src/mod-python.c +++ b/src/mod-python.c @@ -92,6 +92,27 @@ emb_send_target_privmsg(PyObject *self, PyObject *args) return Py_BuildValue("i", ret); } +static PyObject* +emb_send_target_notice(PyObject *self, PyObject *args) +{ + int ret = 0; + char *servicenick; + char *target; + char *buf; + + struct service *service; + + if(!PyArg_ParseTuple(args, "sss:reply", &servicenick, &target, &buf )) + return NULL; + if(!(service = service_find(servicenick))) { + /* TODO: generate python exception here */ + return NULL; + } + send_target_message(4, target, service->bot, "%s", buf); + return Py_BuildValue("i", ret); +} + + static PyObject* emb_get_user(PyObject *self, PyObject *args) { @@ -179,7 +200,7 @@ emb_get_channel(PyObject *self, PyObject *args) struct exemptNode *en = channel->exemptlist.list[n]; PyTuple_SetItem(pChannelExempts, n, Py_BuildValue("{s:s,s:s,s:i}", - "ban", en->ban, + "ban", en->exempt, "who", en->who, "set", en->set) ); @@ -217,13 +238,15 @@ emb_get_account(PyObject *self, PyObject *args) static PyMethodDef EmbMethods[] = { {"dump", emb_dump, METH_VARARGS, "Dump raw P10 line to server"}, {"send_target_privmsg", emb_send_target_privmsg, METH_VARARGS, "Send a message to somewhere"}, + {"send_target_notice", emb_send_target_notice, METH_VARARGS, "Send a notice to somewhere"}, {"get_user", emb_get_user, METH_VARARGS, "Get details about a nickname"}, {"get_channel", emb_get_channel, METH_VARARGS, "Get details about a channel"}, {NULL, NULL, 0, NULL} }; -int python_call_func(char *function, char *args[], size_t argc) { + +int python_call_func_real(char *function, char *args[], size_t argc) { /* TODO: get arguments, pass through to python function */ PyObject *pFunc, *pValue; PyObject *pArgs = NULL; @@ -246,6 +269,7 @@ int python_call_func(char *function, char *args[], size_t argc) { PyTuple_SetItem(pArgs, i, pValue); } } + pValue = PyObject_CallObject(pFunc, pArgs); if(pArgs != NULL) { Py_DECREF(pArgs); @@ -284,6 +308,14 @@ int python_call_func(char *function, char *args[], size_t argc) { } } +int python_call_func(char *function, char *args[], size_t argc, char *command_caller, char *command_target, char *command_service) { + char *setargs[] = {command_caller?command_caller:"", + command_target?command_target:"", + command_service?command_service:""}; + python_call_func_real("command_set", setargs, 3); + python_call_func_real(function, args, argc); + python_call_func_real("command_clear", NULL, 0); +} static int python_handle_join(struct modeNode *mNode) @@ -298,7 +330,7 @@ python_handle_join(struct modeNode *mNode) } else { char *args[] = {channel->name, user->nick}; - return python_call_func("handle_join", args, 2); + return python_call_func("handle_join", args, 2, NULL, NULL, NULL); } } @@ -308,18 +340,18 @@ int python_load() { setenv("PYTHONPATH", "/home/rubin/afternet/services/x3/x3-run/", 1); Py_Initialize(); Py_InitModule("svc", EmbMethods); - PyRun_SimpleString("import svc"); - /* TODO: get "mod-python" from x3.conf */ - pName = PyString_FromString("mod-python"); + //PyRun_SimpleString("import svc"); + /* TODO: get "modpython" from x3.conf */ + pName = PyString_FromString("modpython"); base_module = PyImport_Import(pName); Py_DECREF(pName); if(base_module != NULL) { - python_call_func("handle_init", NULL, 0); + python_call_func("handle_init", NULL, 0, NULL, NULL, NULL); return 1; } else { PyErr_Print(); - log_module(PY_LOG, LOG_WARNING, "Failed to load mod-python.py"); + log_module(PY_LOG, LOG_WARNING, "Failed to load modpython.py"); return 0; } } @@ -360,7 +392,9 @@ static MODCMD_FUNC(cmd_run) { char *msg; msg = unsplit_string(argv + 1, argc - 1, NULL); - PyRun_SimpleString(msg); + /* PyRun_SimpleString(msg); */ + char *args[] = {msg}; + python_call_func("run", args, 1, user?user->nick:"", channel?channel->name:"", cmd->parent->bot->nick); return 1; } diff --git a/src/mod-python.py b/src/modpython.py similarity index 57% rename from src/mod-python.py rename to src/modpython.py index 2c31268..602e432 100644 --- a/src/mod-python.py +++ b/src/modpython.py @@ -13,19 +13,53 @@ import svc +import math + print "This is mod-python.py" +caller = '' +target = '' +service = '' + def handle_init(): print "This is x3init in python" return 0 +def command_set(command_caller, command_target, command_service): + global caller, target, service + caller = command_caller + target = command_target + service = command_service + return 0; + +def command_clear(): + global caller, target, service + caller = None + target = None + service = None + return 0; + + def handle_join(channel, nick): + global caller, target, service print "This is handle_join() in python" user = svc.get_user(nick) + svc.send_target_privmsg("x3", channel, "test %s "%(service)) svc.send_target_privmsg("x3", channel, " %s joined %s: %s"%(nick, channel, user)) svc.send_target_privmsg("x3", channel, "Welcome to %s %s (*%s)! Your IP is %s. You are now in %d channels!"%(channel, user['nick'], user['account'], user['ip'], len(user['channels']) )) chan = svc.get_channel(channel) svc.send_target_privmsg("x3", channel, "Channel details: %s"%chan) return 0 +def run(command): + eval(command) + return 0 +def reply(message): + global caller, target, service + print "DEBUG: %s / %s / %s : %s" %(caller, target, service, message); + if(len(target) > 0): + svc.send_target_privmsg(service, target, "%s: %s"%(caller, message)); + else: + svc.send_target_notice(service, caller, message); + return 0