diff --git a/modules/modpython.cpp b/modules/modpython.cpp index f4b858e5..19b8cdeb 100644 --- a/modules/modpython.cpp +++ b/modules/modpython.cpp @@ -135,11 +135,12 @@ public: bSuccess = false; return HALT; } - PyObject* pyRes = PyObject_CallFunction(pyFunc, const_cast("ssiNNN"), + PyObject* pyRes = PyObject_CallFunction(pyFunc, const_cast("ssiNNNN"), sModName.c_str(), sArgs.c_str(), (int)eType, - (eType == CModInfo::UserModule ? SWIG_NewInstanceObj(GetUser(), SWIG_TypeQuery("CUser*"), 0) : Py_None), + (eType == CModInfo::GlobalModule ? Py_None : SWIG_NewInstanceObj(GetUser(), SWIG_TypeQuery("CUser*"), 0)), + (eType == CModInfo::NetworkModule ? SWIG_NewInstanceObj(GetNetwork(), SWIG_TypeQuery("CNetwork*"), 0) : Py_None), CPyRetString::wrap(sRetMsg), SWIG_NewInstanceObj(reinterpret_cast(this), SWIG_TypeQuery("CModule*"), 0)); if (!pyRes) { diff --git a/modules/modpython/modpython.i b/modules/modpython/modpython.i index db43812f..80800fc8 100644 --- a/modules/modpython/modpython.i +++ b/modules/modpython/modpython.i @@ -15,6 +15,7 @@ #include "../Nick.h" #include "../Chan.h" #include "../User.h" +#include "../IRCNetwork.h" #include "../Client.h" #include "../IRCSock.h" #include "../Listener.h" @@ -97,6 +98,7 @@ namespace std { %include "../Nick.h" %include "../Chan.h" %include "../User.h" +%include "../IRCNetwork.h" %include "../Client.h" %include "../IRCSock.h" %include "../Listener.h" @@ -165,6 +167,15 @@ public: } }; +%extend CIRCNetwork { + CString __str__() { + return $self->GetName(); + } + CString __repr__() { + return "GetName() + ">"; + } +} + %extend CChan { CString __str__() { return $self->GetName(); diff --git a/modules/modpython/module.h b/modules/modpython/module.h index 7e8df7c2..150103af 100644 --- a/modules/modpython/module.h +++ b/modules/modpython/module.h @@ -23,16 +23,9 @@ class CPyModule : public CModule { CModPython* m_pModPython; VWebSubPages* _GetSubPages(); public: - CPyModule(const CString& sModName, const CString& sDataPath, + CPyModule(CUser* pUser, CIRCNetwork* pNetwork, const CString& sModName, const CString& sDataPath, PyObject* pyObj, CModule* pModPython) - : CModule(NULL, NULL, sModName, sDataPath) { - m_pyObj = pyObj; - Py_INCREF(pyObj); - m_pModPython = reinterpret_cast(pModPython); - } - CPyModule(CUser* pUser, const CString& sModName, const CString& sDataPath, - PyObject* pyObj, CModule* pModPython) - : CModule(NULL, pUser, sModName, sDataPath) { + : CModule(NULL, pUser, pNetwork, sModName, sDataPath) { m_pyObj = pyObj; Py_INCREF(pyObj); m_pModPython = reinterpret_cast(pModPython); @@ -122,12 +115,8 @@ static inline CPyModule* AsPyModule(CModule* p) { return dynamic_cast(p); } -inline CPyModule* CreateUserPyModule(CUser* pUser, const CString& sModName, const CString& sDataPath, PyObject* pyObj, CModule* pModPython) { - return new CPyModule(pUser, sModName, sDataPath, pyObj, pModPython); -} - -inline CPyModule* CreateGlobalPyModule(const CString& sModName, const CString& sDataPath, PyObject* pyObj, CModule* pModPython) { - return new CPyModule(sModName, sDataPath, pyObj, pModPython); +inline CPyModule* CreatePyModule(CUser* pUser, CIRCNetwork* pNetwork, const CString& sModName, const CString& sDataPath, PyObject* pyObj, CModule* pModPython) { + return new CPyModule(pUser, pNetwork, sModName, sDataPath, pyObj, pModPython); } class CPyTimer : public CTimer { diff --git a/modules/modpython/znc.py b/modules/modpython/znc.py index 66f7b1e8..da117071 100644 --- a/modules/modpython/znc.py +++ b/modules/modpython/znc.py @@ -423,7 +423,7 @@ def find_open(modname): return (None, None) -def load_module(modname, args, module_type, user, retmsg, modpython): +def load_module(modname, args, module_type, user, network, retmsg, modpython): '''Returns 0 if not found, 1 on loading error, 2 on success''' if re.search(r'[^a-zA-Z0-9_]', modname) is not None: retmsg.s = 'Module names can only contain letters, numbers and ' \ @@ -443,14 +443,7 @@ def load_module(modname, args, module_type, user, retmsg, modpython): return 1 module = cl() - if module_type == CModInfo.UserModule: - module._cmod = CreateUserPyModule(user, modname, datapath, module, modpython) - elif module_type == CModInfo.GlobalModule: - module._cmod = CreateGlobalPyModule(modname, datapath, module, modpython) - else: - retmsg.s = "Module [modpython] doesn't support module type." - return 1 - + module._cmod = CreatePyModule(user, network, modname, datapath, module, modpython) module.nv = ModuleNV(module._cmod) module.SetDescription(cl.description) module.SetArgs(args) @@ -463,6 +456,12 @@ def load_module(modname, args, module_type, user, retmsg, modpython): unload_module(module) return 1 user.GetModules().push_back(module._cmod) + elif module_type == CModInfo.NetworkModule: + if not network: + retmsg.s = "Module [modpython] needs a network for for NetworkModule." + unload_module(module) + return 1 + network.GetModules().push_back(module._cmod) elif module_type == CModInfo.GlobalModule: CZNC.Get().GetModules().push_back(module._cmod) else: @@ -511,6 +510,8 @@ def unload_module(module): cmod = module._cmod if module.GetType() == CModInfo.UserModule: cmod.GetUser().GetModules().removeModule(cmod) + elif module.GetType() == CModInfo.NetworkModule: + cmod.GetNetwork().GetModules().removeModule(cmod) elif module.GetType() == CModInfo.GlobalModule: CZNC.Get().GetModules().removeModule(cmod) del module._cmod