Skip to content

Commit

Permalink
Make modpython support different module types
Browse files Browse the repository at this point in the history
  • Loading branch information
kylef committed Aug 21, 2011
1 parent 9e33e4b commit 274d3eb
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 62 deletions.
135 changes: 97 additions & 38 deletions ClientCommand.cpp
Expand Up @@ -562,19 +562,28 @@ void CClient::UserCommand(CString& sLine) {
}
return;
} else if (sCommand.Equals("LOADMOD") || sCommand.Equals("LOADMODULE")) {
CString sMod;
CString sArgs;

sMod = sLine.Token(1);
sArgs = sLine.Token(2, true);
EModuleType eType;
CString sType = sLine.Token(1);
CString sMod = sLine.Token(2);
CString sArgs = sLine.Token(3, true);

if (sType.Equals("global")) {
eType = ModuleTypeGlobal;
} else if (sType.Equals("user")) {
eType = ModuleTypeUser;
} else {
sMod = sType;
sArgs = sLine.Token(2, true);
sType = "default";
}

if (m_pUser->DenyLoadMod()) {
PutStatus("Unable to load [" + sMod + "] Access Denied.");
return;
}

if (sMod.empty()) {
PutStatus("Usage: LoadMod <module> [args]");
PutStatus("Usage: LoadMod [type] <module> [args]");
return;
}

Expand All @@ -585,23 +594,28 @@ void CClient::UserCommand(CString& sLine) {
return;
}

if (sType.Equals("default")) {
eType = ModInfo.DefaultType();
}

if (eType == ModuleTypeGlobal && !m_pUser->IsAdmin()) {
PutStatus("Unable to load global module [" + sMod + "] Access Denied.");
return;
}

CString sModRet;
bool b = false;

switch (ModInfo.GetType()) {
switch (eType) {
case ModuleTypeGlobal:
if (m_pUser->IsAdmin()) {
b = CZNC::Get().GetModules().LoadModule(sMod, sArgs, ModuleTypeGlobal, NULL, sModRet);
} else {
sModRet = "Unable to load global module [" + sMod + "] Access Denied.";
}
b = CZNC::Get().GetModules().LoadModule(sMod, sArgs, eType, NULL, sModRet);
break;
case ModuleTypeUser:
b = m_pUser->GetModules().LoadModule(sMod, sArgs, ModuleTypeUser, m_pUser, sModRet);
b = m_pUser->GetModules().LoadModule(sMod, sArgs, eType, m_pUser, sModRet);
break;
default:
sModRet = "Unable to load module [" + sMod + "] Unknown module type";
break;

}

if (b)
Expand All @@ -610,44 +624,84 @@ void CClient::UserCommand(CString& sLine) {
PutStatus(sModRet);
return;
} else if (sCommand.Equals("UNLOADMOD") || sCommand.Equals("UNLOADMODULE")) {
CString sMod;
sMod = sLine.Token(1);
EModuleType eType = ModuleTypeUser;
CString sType = sLine.Token(1);
CString sMod = sLine.Token(2);

if (sType.Equals("global")) {
eType = ModuleTypeGlobal;
} else if (sType.Equals("user")) {
eType = ModuleTypeUser;
} else {
sMod = sType;
sType = "default";
}

if (m_pUser->DenyLoadMod()) {
PutStatus("Unable to unload [" + sMod + "] Access Denied.");
return;
}

if (sMod.empty()) {
PutStatus("Usage: UnloadMod <module>");
PutStatus("Usage: UnloadMod [type] <module>");
return;
}

CModInfo ModInfo;
CString sRetMsg;
if (!CZNC::Get().GetModules().GetModInfo(ModInfo, sMod, sRetMsg)) {
PutStatus("Unable to find modinfo [" + sMod + "] [" + sRetMsg + "]");
return;
}

if (sType.Equals("default")) {
eType = ModInfo.DefaultType();
}

if (eType == ModuleTypeGlobal && !m_pUser->IsAdmin()) {
PutStatus("Unable to unload global module [" + sMod + "] Access Denied.");
return;
}

CString sModRet;
bool b;

// First, try to unload the user module
b = m_pUser->GetModules().UnloadModule(sMod, sModRet);
if (!b && m_pUser->IsAdmin()) {
// If that failed and the user is an admin, try to unload a global module
b = CZNC::Get().GetModules().UnloadModule(sMod, sModRet);
switch (eType) {
case ModuleTypeGlobal:
CZNC::Get().GetModules().UnloadModule(sMod, sModRet);
break;
case ModuleTypeUser:
m_pUser->GetModules().UnloadModule(sMod, sModRet);
break;
default:
sModRet = "Unable to unload module [" + sMod + "] Unknown module type";

}

PutStatus(sModRet);
return;
} else if (sCommand.Equals("RELOADMOD") || sCommand.Equals("RELOADMODULE")) {
CString sMod;
CString sArgs;

sMod = sLine.Token(1);
sArgs = sLine.Token(2, true);
EModuleType eType;
CString sType = sLine.Token(1);
CString sMod = sLine.Token(2);
CString sArgs = sLine.Token(3, true);

if (m_pUser->DenyLoadMod()) {
PutStatus("Unable to reload modules. Access Denied.");
return;
}

if (sType.Equals("global")) {
eType = ModuleTypeGlobal;
} else if (sType.Equals("user")) {
eType = ModuleTypeUser;
} else {
sMod = sType;
sArgs = sLine.Token(2, true);
sType = "default";
}

if (sMod.empty()) {
PutStatus("Usage: ReloadMod <module> [args]");
PutStatus("Usage: ReloadMod [type] <module> [args]");
return;
}

Expand All @@ -658,22 +712,27 @@ void CClient::UserCommand(CString& sLine) {
return;
}

if (sType.Equals("default")) {
eType = ModInfo.DefaultType();
}

if (eType == ModuleTypeGlobal && !m_pUser->IsAdmin()) {
PutStatus("Unable to reload global module [" + sMod + "] Access Denied.");
return;
}

CString sModRet;

switch (ModInfo.GetType()) {
switch (eType) {
case ModuleTypeGlobal:
if (!m_pUser->IsAdmin()) {
PutStatus("Unable to reload modules. Access Denied.");
return;
}
CZNC::Get().GetModules().ReloadModule(sMod, sArgs, NULL, sModRet);
break;
case ModuleTypeUser:
m_pUser->GetModules().ReloadModule(sMod, sArgs, m_pUser, sModRet);
break;
default:
sModRet = "Unable to reload module [" + sMod + "] Unknown module type";
break;

}

PutStatus(sModRet);
Expand Down Expand Up @@ -1143,17 +1202,17 @@ void CClient::HelpUser() {
if (!m_pUser->DenyLoadMod()) {
Table.AddRow();
Table.SetCell("Command", "LoadMod");
Table.SetCell("Arguments", "<module>");
Table.SetCell("Arguments", "[type] <module>");
Table.SetCell("Description", "Load a module");

Table.AddRow();
Table.SetCell("Command", "UnloadMod");
Table.SetCell("Arguments", "<module>");
Table.SetCell("Arguments", "[type] <module>");
Table.SetCell("Description", "Unload a module");

Table.AddRow();
Table.SetCell("Command", "ReloadMod");
Table.SetCell("Arguments", "<module>");
Table.SetCell("Arguments", "[type] <module>");
Table.SetCell("Description", "Reload a module");

if (m_pUser->IsAdmin()) {
Expand Down
8 changes: 4 additions & 4 deletions Modules.cpp
Expand Up @@ -830,9 +830,9 @@ bool CModules::LoadModule(const CString& sModule, const CString& sArgs, EModuleT
return false;
}

if (!Info.SupportsModule(eType)) {
if (!Info.SupportsType(eType)) {
dlclose(p);
sRetMsg = "Module [ + sModule + ] does not support module type.";
sRetMsg = "Module [" + sModule + "] does not support module type.";
return false;
}

Expand All @@ -858,7 +858,7 @@ bool CModules::LoadModule(const CString& sModule, const CString& sArgs, EModuleT
}

pModule->SetDescription(Info.GetDescription());
pModule->SetType(Info.GetType());
pModule->SetType(eType);
pModule->SetArgs(sArgs);
pModule->SetModPath(CDir::ChangeDir(CZNC::Get().GetCurPath(), sModPath));
push_back(pModule);
Expand Down Expand Up @@ -1007,7 +1007,7 @@ void CModules::GetAvailableMods(set<CModInfo>& ssMods, EModuleType eType) {

CString sIgnoreRetMsg;
if (GetModPathInfo(ModInfo, sName, sPath, sIgnoreRetMsg)) {
if (ModInfo.GetType() == eType) {
if (ModInfo.SupportsType(eType)) {
ssMods.insert(ModInfo);
}
}
Expand Down
18 changes: 12 additions & 6 deletions Modules.h
Expand Up @@ -70,7 +70,7 @@ template<class M> CModule* TModLoadGlobal(ModHandle p,
if (dCoreVersion != VERSION) \
return false; \
Info.SetDescription(DESCRIPTION); \
Info.SetType(TYPE); \
Info.AddType(TYPE); \
LOADER; \
TModInfo<CLASS>(Info); \
return true; \
Expand Down Expand Up @@ -195,16 +195,23 @@ class CModInfo {
return (GetName() < Info.GetName());
}

bool SupportsModule(EModuleType eType) {
return eType == m_eType;
bool SupportsType(EModuleType eType) {
return m_seType.find(eType) != m_seType.end();
}

void AddType(EModuleType eType) {
m_seType.insert(eType);
}

EModuleType DefaultType() {
return *m_seType.begin();
}

// Getters
const CString& GetName() const { return m_sName; }
const CString& GetPath() const { return m_sPath; }
const CString& GetDescription() const { return m_sDescription; }
const CString& GetWikiPage() const { return m_sWikiPage; }
EModuleType GetType() const { return m_eType; }
ModLoader GetLoader() const { return m_fLoader; }
GlobalModLoader GetGlobalLoader() const { return m_fGlobalLoader; }
// !Getters
Expand All @@ -214,13 +221,12 @@ class CModInfo {
void SetPath(const CString& s) { m_sPath = s; }
void SetDescription(const CString& s) { m_sDescription = s; }
void SetWikiPage(const CString& s) { m_sWikiPage = s; }
void SetType(EModuleType eType) { m_eType = eType; }
void SetLoader(ModLoader fLoader) { m_fLoader = fLoader; }
void SetGlobalLoader(GlobalModLoader fGlobalLoader) { m_fGlobalLoader = fGlobalLoader; }
// !Setters
private:
protected:
EModuleType m_eType;
set<EModuleType> m_seType;
CString m_sName;
CString m_sPath;
CString m_sDescription;
Expand Down
4 changes: 2 additions & 2 deletions modules/modperl.cpp
Expand Up @@ -148,7 +148,7 @@ class CModPerl: public CModule {
case Perl_Loaded:
result = HALT;
if (4 == ret) {
ModInfo.SetType(ModuleTypeUser);
ModInfo.AddType(ModuleTypeUser);
ModInfo.SetDescription(PString(ST(2)));
ModInfo.SetName(sModule);
ModInfo.SetPath(PString(ST(1)));
Expand Down Expand Up @@ -203,7 +203,7 @@ class CModPerl: public CModule {
PUSH_STR(sName);
PCALL("ZNC::Core::ModInfoByPath");
if (!SvTRUE(ERRSV) && ret == 2) {
ModInfo.SetType(ModuleTypeUser);
ModInfo.AddType(ModuleTypeUser);
ModInfo.SetDescription(PString(ST(0)));
ModInfo.SetName(sName);
ModInfo.SetPath(sPath);
Expand Down
7 changes: 4 additions & 3 deletions modules/modpython.cpp
Expand Up @@ -135,10 +135,11 @@ class CModPython: public CModule {
bSuccess = false;
return HALT;
}
PyObject* pyRes = PyObject_CallFunction(pyFunc, const_cast<char*>("ssNNN"),
PyObject* pyRes = PyObject_CallFunction(pyFunc, const_cast<char*>("ssiNNN"),
sModName.c_str(),
sArgs.c_str(),
(eType == ModuleTypeUser ? SWIG_NewInstanceObj(GetUser(), SWIG_TypeQuery("CUser*"), 0) : NULL),
(int)eType,
(eType == ModuleTypeUser ? SWIG_NewInstanceObj(GetUser(), SWIG_TypeQuery("CUser*"), 0) : Py_None),
CPyRetString::wrap(sRetMsg),
SWIG_NewInstanceObj(reinterpret_cast<CModule*>(this), SWIG_TypeQuery("CModule*"), 0));
if (!pyRes) {
Expand Down Expand Up @@ -279,7 +280,7 @@ class CModPython: public CModule {
return;
}
Py_CLEAR(pyRes);
if (x && ModInfo.GetType() == eType) {
if (x && ModInfo.SupportsType(eType)) {
ssMods.insert(ModInfo);
ssAlready.insert(sName);
}
Expand Down

0 comments on commit 274d3eb

Please sign in to comment.