diff --git a/modules/modpython/functions.in b/modules/modpython/functions.in index d03c03ff..fe55c4b1 100644 --- a/modules/modpython/functions.in +++ b/modules/modpython/functions.in @@ -61,3 +61,4 @@ void OnServerCapResult(const CString& sCap, bool bSuccess) EModRet OnTimerAutoJoin(CChan& Channel) bool OnEmbeddedWebRequest(CWebSock& WebSock, const CString& sPageName, CTemplate& Tmpl)=false +EModRet OnModuleLoading(const CString& sModName, const CString& sArgs, bool& bSuccess, CString& sRetMsg) diff --git a/modules/modpython/modpython.i b/modules/modpython/modpython.i index ccc24165..9a2ca318 100644 --- a/modules/modpython/modpython.i +++ b/modules/modpython/modpython.i @@ -73,6 +73,15 @@ namespace std { } } +%typemap(out) CString&, CString* { + if ($1) { + $result = CPyRetString::wrap(*$1); + } else { + $result = Py_None; + Py_INCREF(Py_None); + } +} + #define u_short unsigned short #define u_int unsigned int #include "../ZNCString.h" diff --git a/modules/modpython/module.h b/modules/modpython/module.h index de2cbe98..11da0351 100644 --- a/modules/modpython/module.h +++ b/modules/modpython/module.h @@ -23,6 +23,13 @@ class CPyModule : public CModule { CModPython* m_pModPython; VWebSubPages* _GetSubPages(); public: + CPyModule(const CString& sModName, const CString& sDataPath, + PyObject* pyObj, CModule* pModPython) + : CModule(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) { @@ -108,16 +115,21 @@ public: virtual void OnServerCapResult(const CString& sCap, bool bSuccess); virtual EModRet OnTimerAutoJoin(CChan& Channel); bool OnEmbeddedWebRequest(CWebSock&, const CString&, CTemplate&); +EModRet OnModuleLoading(const CString& sModName, const CString& sArgs, bool& bSuccess, CString& sRetMsg); }; static inline CPyModule* AsPyModule(CModule* p) { return dynamic_cast(p); } -inline CPyModule* CreatePyModule(CUser* pUser, const CString& sModName, const CString& sDataPath, PyObject* pyObj, CModule* pModPython) { +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); +} + class CPyTimer : public CTimer { PyObject* m_pyObj; CModPython* m_pModPython; diff --git a/modules/modpython/znc.py b/modules/modpython/znc.py index 100ce165..d102eb33 100644 --- a/modules/modpython/znc.py +++ b/modules/modpython/znc.py @@ -438,11 +438,15 @@ def load_module(modname, args, user, retmsg, modpython): return 1 cl = pymodule.__dict__[modname] module = cl() - module._cmod = CreatePyModule(user, modname, datapath, module, modpython) + if user: + module._cmod = CreateUserPyModule(user, modname, datapath, module, modpython) + else: + module._cmod = CreateGlobalPyModule(modname, datapath, module, modpython) module.nv = ModuleNV(module._cmod) module.SetDescription(cl.description) module.SetArgs(args) module.SetModPath(pymodule.__file__) + module.SetType(cl.module_type) if user: user.GetModules().push_back(module._cmod) @@ -488,8 +492,11 @@ def load_module(modname, args, user, retmsg, modpython): def unload_module(module): module.OnShutdown() cmod = module._cmod + if module.GetType() == ModuleTypeUser: + cmod.GetUser().GetModules().removeModule(cmod) + elif module.GetType() == ModuleTypeGlobal: + CZNC.Get().GetModules().removeModule(cmod) del module._cmod - cmod.GetUser().GetModules().removeModule(cmod) cmod.DeletePyModule() del cmod