// // Try running a script on the initiator bool try_reset_script(RESET_DATA *reset, void *initiator, int initiator_type, const char *locale) { PyObject *pyme = NULL; PyObject *dict = NULL; if(initiator_type == INITIATOR_ROOM) pyme = roomGetPyFormBorrowed(initiator); else if(initiator_type == INITIATOR_THEN_OBJ) pyme = objGetPyFormBorrowed(initiator); else if(initiator_type == INITIATOR_THEN_MOB) pyme = charGetPyFormBorrowed(initiator); else return FALSE; // build our dictionary and add ourself to it as 'me' dict = restricted_script_dict(); PyDict_SetItemString(dict, "me", pyme); // run the script run_script(dict, resetGetArg(reset), locale); // check to see if we had an error if(!last_script_ok()) log_pyerr("Reset script in locale %s terminated with an error:\r\n%s", locale, resetGetArg(reset)); // garbage collection and return our outcome Py_DECREF(dict); return last_script_ok(); }
// // Send a message with Python statements potentially embedded in it. For //evaluating PyObject *PySocket_send(PyObject *self, PyObject *args, PyObject *kwds) { static char *kwlist[ ] = { "mssg", "dict", "newline", NULL }; SOCKET_DATA *me = NULL; char *text = NULL; PyObject *dict = NULL; bool newline = TRUE; if(!PyArg_ParseTupleAndKeywords(args, kwds, "s|Ob", kwlist, &text, &dict, &newline)) { PyErr_Format(PyExc_TypeError, "Invalid arguments supplied to Mudsock.send"); return NULL; } // is dict None? set it to NULL for expand_to_char if(dict == Py_None) dict = NULL; // make sure the dictionary is a dictionary if(!(dict == NULL || PyDict_Check(dict))) { PyErr_Format(PyExc_TypeError, "Mudsock.send expects second argument to be a dict object."); return NULL; } // make sure we exist if( (me = PySocket_AsSocket(self)) == NULL) { PyErr_Format(PyExc_TypeError, "Tried to send nonexistent socket."); return NULL; } if(dict != NULL) PyDict_SetItemString(dict, "me", self); // build our script environment BUFFER *buf = newBuffer(1); bufferCat(buf, text); // expand out our dynamic descriptions if we have a dictionary supplied if(dict != NULL) { PyObject *env = restricted_script_dict(); PyDict_Update(env, dict); // do the expansion expand_dynamic_descs_dict(buf, env, get_script_locale()); Py_XDECREF(env); } if(newline == TRUE) bufferCat(buf, "\r\n"); text_to_buffer(me, bufferString(buf)); // garbage collection deleteBuffer(buf); return Py_BuildValue(""); }
void expand_dynamic_descs(BUFFER *desc, PyObject *me, CHAR_DATA *ch, const char *locale) { // set up our dictionary PyObject *dict = restricted_script_dict(); PyDict_SetItemString(dict, "me", me); PyDict_SetItemString(dict, "ch", charGetPyFormBorrowed(ch)); // expand the dynamic description expand_dynamic_descs_dict(desc, dict, locale); // garbage collection Py_XDECREF(dict); }
void expand_to_char(CHAR_DATA *ch, const char *mssg, PyObject *dict, const char *locale, bool newline) { BUFFER *buf = newBuffer(1); bufferCat(buf, mssg); if(dict != NULL) { // build the script dictionary PyObject *script_dict = restricted_script_dict(); PyDict_Update(script_dict, dict); // do the expansion expand_dynamic_descs_dict(buf, script_dict, locale); // garbage collection and end Py_XDECREF(script_dict); } if(newline == TRUE) bufferCat(buf, "\r\n"); text_to_char(ch, bufferString(buf)); // garbage collection deleteBuffer(buf); }
// // generalized function for setting up a dictionary and running a trigger. The // common types of variables can be supplied in the function. Additional ones // can be added in the optional list, which must be deleted after use void gen_do_trig(TRIGGER_DATA *trig, void *me, int me_type, CHAR_DATA *ch, OBJ_DATA *obj, ROOM_DATA *room, EXIT_DATA *exit, const char *command, const char *arg, LIST *optional) { // make our basic dictionary, and fill it up with these new variables PyObject *dict = restricted_script_dict(); LIST *varnames = newList(); // now, import all of our variables if(command) { PyObject *pycmd = PyString_FromString(command); PyDict_SetItemString(dict, "cmd", pycmd); listPut(varnames, strdup("cmd")); Py_DECREF(pycmd); } if(arg) { PyObject *pyarg = PyString_FromString(arg); PyDict_SetItemString(dict, "arg", pyarg); listPut(varnames, strdup("arg")); Py_DECREF(pyarg); } if(ch) { PyObject *pych = charGetPyForm(ch); PyDict_SetItemString(dict, "ch", pych); listPut(varnames, strdup("ch")); Py_DECREF(pych); } if(room) { PyObject *pyroom = roomGetPyForm(room); PyDict_SetItemString(dict, "room", pyroom); listPut(varnames, strdup("room")); Py_DECREF(pyroom); } if(obj) { PyObject *pyobj = objGetPyForm(obj); PyDict_SetItemString(dict, "obj", pyobj); listPut(varnames, strdup("obj")); Py_DECREF(pyobj); } if(exit) { PyObject *pyexit = newPyExit(exit); PyDict_SetItemString(dict, "ex", pyexit); listPut(varnames, strdup("ex")); Py_DECREF(pyexit); } // add the thing the trigger is attached to if(me) { PyObject *pyme = NULL; switch(me_type) { case TRIGVAR_CHAR: pyme = charGetPyForm(me); break; case TRIGVAR_OBJ: pyme = objGetPyForm(me); break; case TRIGVAR_ROOM: pyme = roomGetPyForm(me); break; } PyDict_SetItemString(dict, "me", pyme); listPut(varnames, strdup("me")); Py_DECREF(pyme); } // now, add any optional variables if(optional) { LIST_ITERATOR *opt_i = newListIterator(optional); OPT_VAR *opt = NULL; PyObject *pyopt = NULL; ITERATE_LIST(opt, opt_i) { pyopt = NULL; switch(opt->type) { case TRIGVAR_CHAR: pyopt = charGetPyForm(opt->data); break; case TRIGVAR_OBJ: pyopt = objGetPyForm(opt->data); break; case TRIGVAR_ROOM: pyopt = roomGetPyForm(opt->data); break; } PyDict_SetItemString(dict, opt->name, pyopt); listPut(varnames, strdup(opt->name)); Py_XDECREF(pyopt); } deleteListIterator(opt_i); }