Skip to content

Commit

Permalink
Make modpython support network modules
Browse files Browse the repository at this point in the history
  • Loading branch information
kylef committed Aug 25, 2011
1 parent 9a2fed6 commit 260421e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
5 changes: 3 additions & 2 deletions modules/modpython.cpp
Expand Up @@ -135,11 +135,12 @@ class CModPython: public CModule {
bSuccess = false;
return HALT;
}
PyObject* pyRes = PyObject_CallFunction(pyFunc, const_cast<char*>("ssiNNN"),
PyObject* pyRes = PyObject_CallFunction(pyFunc, const_cast<char*>("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<CModule*>(this), SWIG_TypeQuery("CModule*"), 0));
if (!pyRes) {
Expand Down
11 changes: 11 additions & 0 deletions modules/modpython/modpython.i
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -165,6 +167,15 @@ public:
}
};

%extend CIRCNetwork {
CString __str__() {
return $self->GetName();
}
CString __repr__() {
return "<CIRCNetwork " + $self->GetName() + ">";
}
}

%extend CChan {
CString __str__() {
return $self->GetName();
Expand Down
19 changes: 4 additions & 15 deletions modules/modpython/module.h
Expand Up @@ -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<CModPython*>(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<CModPython*>(pModPython);
Expand Down Expand Up @@ -122,12 +115,8 @@ static inline CPyModule* AsPyModule(CModule* p) {
return dynamic_cast<CPyModule*>(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 {
Expand Down
19 changes: 10 additions & 9 deletions modules/modpython/znc.py
Expand Up @@ -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 ' \
Expand All @@ -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)
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 260421e

Please sign in to comment.