+ PyTuple_SetItem(pArgs, i, pValue);
+ }
+ }
+ return pArgs;
+}
+
+
+PyObject *new_irc_object(char *command_service, char *command_caller, char *command_target) {
+ /* Creates a new instance of the irc class (from modpython.py) which is initalized
+ with current environment details like which service were talking to.
+ command_service = which service we are talking to, or empty string if none
+ command_caller = nick of user generating message, or empty string if none
+ command_target = If were reacting to something on a channel, this will
+ be set to the name of the channel. Otherwise empty
+ */
+ PyObject *pIrcArgs = NULL;
+ PyObject *pIrcClass;
+ PyObject *pIrcObj;
+
+ log_module(PY_LOG, LOG_INFO, "Attempting to instanciate irc class; %s %s %s", command_service, command_caller, command_target);
+ pIrcClass = PyObject_GetAttrString(base_module, "irc");
+ /* pIrcClass is a new reference */
+ if(pIrcClass && PyCallable_Check(pIrcClass)) {
+ //size_t i;
+ char *ircargs[] = {command_service, command_caller, command_target};
+ //PyObject *pValue;
+
+ pIrcArgs = python_build_args(3, ircargs);
+ pIrcObj = PyObject_CallObject(pIrcClass, pIrcArgs);
+ if(!pIrcObj) {
+ log_module(PY_LOG, LOG_ERROR, "IRC Class failed to load");
+ python_log_module();
+ //PyErr_Print();
+ }
+ if(pIrcArgs != NULL) {
+ Py_DECREF(pIrcArgs);
+ }
+ Py_DECREF(pIrcClass);
+ return pIrcObj;
+ }
+ else {
+ /* need to free pIrcClass here if it WAS found but was NOT callable? */
+ log_module(PY_LOG, LOG_ERROR, "Unable to find irc class");
+ return NULL;
+ }
+}
+
+int python_call_handler(char *handler, char *args[], size_t argc, char *command_service, char *command_caller, char *command_target) {
+ /* This is how we talk to modpython.c. First a new instance of the irc class is created using these
+ arguments to setup the current environment. Then the named method of the handler object is called
+ with the givin arguments.
+ */
+ PyObject *pIrcObj;
+ PyObject *pArgs;
+ PyObject *pMethod;
+ PyObject *pValue;
+
+ log_module(PY_LOG, LOG_INFO, "attempting to call handler %s.", handler);
+ if(base_module != NULL && handler_object != NULL) {
+ pIrcObj = new_irc_object(command_service, command_caller, command_target);
+ if(!pIrcObj) {
+ log_module(PY_LOG, LOG_INFO, "Can't get irc object. Bailing.");
+ return 0;
+ }
+
+ pArgs = python_build_handler_args(argc, args, pIrcObj);
+ pMethod = PyObject_GetAttrString(handler_object, handler);
+ if(pMethod && PyCallable_Check(pMethod)) {
+ /* Call the method, with the arguments */
+ pValue = PyObject_CallObject(pMethod, pArgs);
+ if(pArgs) {
+ Py_DECREF(pArgs);