Progress on modpython global hooks.

OnModuleLoading, OnModuleUnloading, OnGetModInfo work fine.
Need to add: OnClientCapLs, OnGetAvailableMods, OnLoginAttempt

See #98
This commit is contained in:
Alexey Sokolov
2012-07-22 18:21:58 +07:00
parent 5b50003f33
commit 5656e14aa2
6 changed files with 62 additions and 6 deletions

View File

@@ -268,6 +268,13 @@ while (<$in>) {
say $out "Py_BuildValue(\"s\", $a->{var}.c_str());";
}
}
when (/^bool/) {
if ($a->{mod} eq '&') {
say $out "CPyRetBool::wrap($a->{var});";
} else {
say $out "Py_BuildValue(\"l\", (long int)$a->{var});";
}
}
when (/\*$/) {
(my $t = $a->{type}) =~ s/^const//;
say $out "SWIG_NewInstanceObj(const_cast<$t>($a->{var}), SWIG_TypeQuery(\"$t\"), 0);";
@@ -276,9 +283,6 @@ while (<$in>) {
(my $b = $a->{base}) =~ s/^const//;
say $out "SWIG_NewInstanceObj(const_cast<$b*>(&$a->{var}), SWIG_TypeQuery(\"$b*\"), 0);";
}
when ('bool') {
say $out "Py_BuildValue(\"l\", (long int)$a->{var});";
}
when (/(?:^|::)E/) {
say $out "Py_BuildValue(\"i\", (int)$a->{var});";
}

View File

@@ -31,7 +31,7 @@
#include "../include/znc/Buffer.h"
#include "modpython/module.h"
#include "modpython/retstring.h"
#include "modpython/ret.h"
#define stat struct stat
using std::allocator;
@@ -91,6 +91,17 @@ namespace std {
}
}
/*TODO %typemap(in) bool& to be able to call from python functions which get bool& */
%typemap(out) bool&, bool* {
if ($1) {
$result = CPyRetBool::wrap(*$1);
} else {
$result = Py_None;
Py_INCREF(Py_None);
}
}
#define u_short unsigned short
#define u_int unsigned int
#include "../include/znc/ZNCString.h"
@@ -139,6 +150,18 @@ public:
}
};
class CPyRetBool {
CPyRetBool();
public:
bool b;
};
%extend CPyRetBool {
bool __bool__() {
return $self->b;
}
}
%extend CModule {
CString __str__() {
return $self->GetModName();
@@ -211,6 +234,18 @@ public:
}
};
/* To allow module-loaders to be written on python.
* They can call CreatePyModule() to create CModule* object, but one of arguments to CreatePyModule() is "CModule* pModPython"
* Pointer to modpython is already accessible to python modules as self.GetModPython(), but it's just a pointer to something, not to CModule*.
* So make it known that CModPython is really a CModule.
*/
class CModPython : public CModule {
private:
CModPython();
CModPython(const CModPython&);
~CModPython();
};
/* Web */
%template(StrPair) std::pair<CString, CString>;

View File

@@ -122,6 +122,7 @@ public:
virtual EModRet OnModuleUnloading(CModule* pModule, bool& bSuccess, CString& sRetMsg);
virtual EModRet OnGetModInfo(CModInfo& ModInfo, const CString& sModule,
bool& bSuccess, CString& sRetMsg);
virtual void OnGetAvailableMods(set<CModInfo>& ssMods, CModInfo::EModuleType eType);
};
static inline CPyModule* AsPyModule(CModule* p) {

View File

@@ -18,4 +18,12 @@ public:
}
};
class CPyRetBool {
public:
bool& b;
CPyRetBool(bool& B) : b(B) {}
static PyObject* wrap(bool& B) {
CPyRetBool* x = new CPyRetBool(B);
return SWIG_NewInstanceObj(x, SWIG_TypeQuery("CPyRetBool*"), SWIG_POINTER_OWN);
}
};

View File

@@ -549,6 +549,8 @@ def load_module(modname, args, module_type, user, network, retmsg, modpython):
def unload_module(module):
if (module not in _py_modules):
return False
module.OnShutdown()
_py_modules.discard(module)
cmod = module._cmod
@@ -561,6 +563,7 @@ def unload_module(module):
del module._cmod
cmod.DeletePyModule()
del cmod
return True
def unload_all():