Permalink
Browse files

Create EModuleType, creating SetType which replaces SetGlobal

  • Loading branch information...
Kyle Fuller
Kyle Fuller committed Aug 8, 2011
1 parent 091a287 commit a4b155995cbfe70a1fb9bfca6f956db132dde264
Showing with 86 additions and 96 deletions.
  1. +31 −25 ClientCommand.cpp
  2. +10 −10 Modules.cpp
  3. +19 −15 Modules.h
  4. +1 −1 Socket.cpp
  5. +2 −2 WebModules.cpp
  6. +4 −4 modules/modperl.cpp
  7. +6 −13 modules/modpython.cpp
  8. +3 −2 modules/modpython/znc.py
  9. +9 −23 modules/webadmin.cpp
  10. +1 −1 znc.cpp
View
@@ -517,7 +517,7 @@ void CClient::UserCommand(CString& sLine) {
if (m_pUser->IsAdmin()) {
set<CModInfo> ssGlobalMods;
- CZNC::Get().GetModules().GetAvailableMods(ssGlobalMods, true);
+ CZNC::Get().GetModules().GetAvailableMods(ssGlobalMods, ModuleTypeGlobal);
if (ssGlobalMods.empty()) {
PutStatus("No global modules available.");
@@ -585,20 +585,23 @@ void CClient::UserCommand(CString& sLine) {
return;
}
- bool bGlobal = ModInfo.IsGlobal();
-
- if (bGlobal && !m_pUser->IsAdmin()) {
- PutStatus("Unable to load global module [" + sMod + "] Access Denied.");
- return;
- }
-
CString sModRet;
- bool b;
-
- if (bGlobal) {
- b = CZNC::Get().GetModules().LoadModule(sMod, sArgs, NULL, sModRet);
- } else {
- b = m_pUser->GetModules().LoadModule(sMod, sArgs, m_pUser, sModRet);
+ bool b = false;
+
+ switch (ModInfo.GetType()) {
+ case ModuleTypeGlobal:
+ if (m_pUser->IsAdmin()) {
+ b = CZNC::Get().GetModules().LoadModule(sMod, sArgs, NULL, sModRet);
+ } else {
+ sModRet = "Unable to load global module [" + sMod + "] Access Denied.";
+ }
+ break;
+ case ModuleTypeUser:
+ b = m_pUser->GetModules().LoadModule(sMod, sArgs, m_pUser, sModRet);
+ break;
+ default:
+ sModRet = "Unable to load module [" + sMod + "] Unknown module type";
+ break;
}
if (b)
@@ -655,19 +658,22 @@ void CClient::UserCommand(CString& sLine) {
return;
}
- bool bGlobal = ModInfo.IsGlobal();
-
- if (bGlobal && !m_pUser->IsAdmin()) {
- PutStatus("Unable to reload global module [" + sMod + "] Access Denied.");
- return;
- }
-
CString sModRet;
- if (bGlobal) {
- CZNC::Get().GetModules().ReloadModule(sMod, sArgs, NULL, sModRet);
- } else {
- m_pUser->GetModules().ReloadModule(sMod, sArgs, m_pUser, sModRet);
+ switch (ModInfo.GetType()) {
+ 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);
View
@@ -105,7 +105,7 @@ const CString& CTimer::GetDescription() const { return m_sDescription; }
CModule::CModule(ModHandle pDLL, CUser* pUser, const CString& sModName, const CString& sDataDir) {
- m_bGlobal = false;
+ m_eType = ModuleTypeUser;
m_pDLL = pDLL;
m_pManager = &(CZNC::Get().GetManager());;
m_pUser = pUser;
@@ -588,7 +588,7 @@ CModule::EModRet CGlobalModule::OnModuleUnloading(CModule* pModule, bool& bSucce
}
CModule::EModRet CGlobalModule::OnGetModInfo(CModInfo& ModInfo, const CString& sModule,
bool& bSuccess, CString& sRetMsg) { return CONTINUE; }
-void CGlobalModule::OnGetAvailableMods(set<CModInfo>& ssMods, bool bGlobal) {}
+void CGlobalModule::OnGetAvailableMods(set<CModInfo>& ssMods, EModuleType eType) {}
CModules::CModules() {
@@ -788,8 +788,8 @@ bool CGlobalModules::OnGetModInfo(CModInfo& ModInfo, const CString& sModule,
GLOBALMODHALTCHK(OnGetModInfo(ModInfo, sModule, bSuccess, sRetMsg));
}
-bool CGlobalModules::OnGetAvailableMods(set<CModInfo>& ssMods, bool bGlobal) {
- GLOBALMODCALL(OnGetAvailableMods(ssMods, bGlobal));
+bool CGlobalModules::OnGetAvailableMods(set<CModInfo>& ssMods, EModuleType eType) {
+ GLOBALMODCALL(OnGetAvailableMods(ssMods, eType));
return false;
}
@@ -835,10 +835,10 @@ bool CModules::LoadModule(const CString& sModule, const CString& sArgs, CUser* p
return false;
}
- if ((pUser == NULL) != Info.IsGlobal()) {
+ if ((pUser == NULL) != (Info.GetType() == ModuleTypeGlobal)) {
dlclose(p);
sRetMsg = "Module [" + sModule + "] is ";
- sRetMsg += Info.IsGlobal() ? "" : "not ";
+ sRetMsg += (Info.GetType() == ModuleTypeGlobal) ? "" : "not ";
sRetMsg += "a global module.";
return false;
}
@@ -852,7 +852,7 @@ bool CModules::LoadModule(const CString& sModule, const CString& sArgs, CUser* p
}
pModule->SetDescription(Info.GetDescription());
- pModule->SetGlobal(Info.IsGlobal());
+ pModule->SetType(Info.GetType());
pModule->SetArgs(sArgs);
pModule->SetModPath(CDir::ChangeDir(CZNC::Get().GetCurPath(), sModPath));
push_back(pModule);
@@ -970,7 +970,7 @@ bool CModules::GetModPathInfo(CModInfo& ModInfo, const CString& sModule, const C
return true;
}
-void CModules::GetAvailableMods(set<CModInfo>& ssMods, bool bGlobal) {
+void CModules::GetAvailableMods(set<CModInfo>& ssMods, EModuleType eType) {
ssMods.clear();
unsigned int a = 0;
@@ -991,14 +991,14 @@ void CModules::GetAvailableMods(set<CModInfo>& ssMods, bool bGlobal) {
CString sIgnoreRetMsg;
if (GetModPathInfo(ModInfo, sName, sPath, sIgnoreRetMsg)) {
- if (ModInfo.IsGlobal() == bGlobal) {
+ if (ModInfo.GetType() == eType) {
ssMods.insert(ModInfo);
}
}
}
}
- GLOBALMODULECALL(OnGetAvailableMods(ssMods, bGlobal), NULL, NULL, NOTHING);
+ GLOBALMODULECALL(OnGetAvailableMods(ssMods, eType), NULL, NULL, NOTHING);
}
bool CModules::FindModPath(const CString& sModule, CString& sModPath,
View
@@ -40,6 +40,11 @@ class CModInfo;
#endif
#endif
+typedef enum {
+ ModuleTypeGlobal,
+ ModuleTypeUser
+} EModuleType;
+
typedef void* ModHandle;
template<class M> void TModInfo(CModInfo& Info) {}
@@ -59,14 +64,14 @@ template<class M> CGlobalModule* TModLoadGlobal(ModHandle p,
# define MODULE_EXPORT
#endif
-#define MODCOMMONDEFS(CLASS, DESCRIPTION, GLOBAL, LOADER) \
+#define MODCOMMONDEFS(CLASS, DESCRIPTION, TYPE, LOADER) \
extern "C" { \
MODULE_EXPORT bool ZNCModInfo(double dCoreVersion, CModInfo& Info); \
bool ZNCModInfo(double dCoreVersion, CModInfo& Info) { \
if (dCoreVersion != VERSION) \
return false; \
Info.SetDescription(DESCRIPTION); \
- Info.SetGlobal(GLOBAL); \
+ Info.SetType(TYPE); \
LOADER; \
TModInfo<CLASS>(Info); \
return true; \
@@ -101,7 +106,7 @@ template<class M> CGlobalModule* TModLoadGlobal(ModHandle p,
* @see For global modules you need GLOBALMODULEDEFS.
*/
#define MODULEDEFS(CLASS, DESCRIPTION) \
- MODCOMMONDEFS(CLASS, DESCRIPTION, false, Info.SetLoader(TModLoad<CLASS>))
+ MODCOMMONDEFS(CLASS, DESCRIPTION, ModuleTypeUser, Info.SetLoader(TModLoad<CLASS>))
// !User Module Macros
// Global Module Macros
@@ -112,7 +117,7 @@ template<class M> CGlobalModule* TModLoadGlobal(ModHandle p,
/** This works exactly like MODULEDEFS, but for global modules. */
#define GLOBALMODULEDEFS(CLASS, DESCRIPTION) \
- MODCOMMONDEFS(CLASS, DESCRIPTION, true, Info.SetGlobalLoader(TModLoadGlobal<CLASS>))
+ MODCOMMONDEFS(CLASS, DESCRIPTION, ModuleTypeGlobal, Info.SetGlobalLoader(TModLoadGlobal<CLASS>))
// !Global Module Macros
// Forward Declarations
@@ -179,8 +184,7 @@ class CModInfo {
m_fGlobalLoader = NULL;
m_fLoader = NULL;
}
- CModInfo(const CString& sName, const CString& sPath, bool bGlobal) {
- m_bGlobal = bGlobal;
+ CModInfo(const CString& sName, const CString& sPath, EModuleType eType) {
m_sName = sName;
m_sPath = sPath;
m_fGlobalLoader = NULL;
@@ -197,7 +201,7 @@ class CModInfo {
const CString& GetPath() const { return m_sPath; }
const CString& GetDescription() const { return m_sDescription; }
const CString& GetWikiPage() const { return m_sWikiPage; }
- bool IsGlobal() const { return m_bGlobal; }
+ EModuleType GetType() const { return m_eType; }
ModLoader GetLoader() const { return m_fLoader; }
GlobalModLoader GetGlobalLoader() const { return m_fGlobalLoader; }
// !Getters
@@ -207,13 +211,13 @@ 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 SetGlobal(bool b) { m_bGlobal = b; }
+ void SetType(EModuleType eType) { m_eType = eType; }
void SetLoader(ModLoader fLoader) { m_fLoader = fLoader; }
void SetGlobalLoader(GlobalModLoader fGlobalLoader) { m_fGlobalLoader = fGlobalLoader; }
// !Setters
private:
protected:
- bool m_bGlobal;
+ EModuleType m_eType;
CString m_sName;
CString m_sPath;
CString m_sDescription;
@@ -845,14 +849,14 @@ class CModule {
const CString& GetSavePath() const;
// Setters
- void SetGlobal(bool b) { m_bGlobal = b; }
+ void SetType(EModuleType eType) { m_eType = eType; }
void SetDescription(const CString& s) { m_sDescription = s; }
void SetModPath(const CString& s) { m_sModPath = s; }
void SetArgs(const CString& s) { m_sArgs = s; }
// !Setters
// Getters
- bool IsGlobal() const { return m_bGlobal; }
+ EModuleType GetType() const { return m_eType; }
const CString& GetDescription() const { return m_sDescription; }
const CString& GetArgs() const { return m_sArgs; }
const CString& GetModPath() const { return m_sModPath; }
@@ -871,7 +875,7 @@ class CModule {
// !Getters
protected:
- bool m_bGlobal;
+ EModuleType m_eType;
CString m_sDescription;
set<CTimer*> m_sTimers;
set<CSocket*> m_sSockets;
@@ -974,7 +978,7 @@ class CModules : public vector<CModule*> {
static bool GetModInfo(CModInfo& ModInfo, const CString& sModule, CString &sRetMsg);
static bool GetModPathInfo(CModInfo& ModInfo, const CString& sModule, const CString& sModPath, CString &sRetMsg);
- static void GetAvailableMods(set<CModInfo>& ssMods, bool bGlobal = false);
+ static void GetAvailableMods(set<CModInfo>& ssMods, EModuleType eType = ModuleTypeUser);
// This returns the path to the .so and to the data dir
// which is where static data (webadmin skins) are saved
@@ -1097,7 +1101,7 @@ class CGlobalModule : public CModule {
* @param ssMods put new modules here.
* @param bGlobal true if global modules are needed.
*/
- virtual void OnGetAvailableMods(set<CModInfo>& ssMods, bool bGlobal);
+ virtual void OnGetAvailableMods(set<CModInfo>& ssMods, EModuleType eType);
private:
};
@@ -1120,7 +1124,7 @@ class CGlobalModules : public CModules {
bool OnModuleUnloading(CModule* pModule, bool& bSuccess, CString& sRetMsg);
bool OnGetModInfo(CModInfo& ModInfo, const CString& sModule,
bool& bSuccess, CString& sRetMsg);
- bool OnGetAvailableMods(set<CModInfo>& ssMods, bool bGlobal);
+ bool OnGetAvailableMods(set<CModInfo>& ssMods, EModuleType eType);
private:
};
View
@@ -59,7 +59,7 @@ CSocket::~CSocket() {
m_pModule->UnlinkSocket(this);
}
- if (pUser && m_pModule && !m_pModule->IsGlobal()) {
+ if (pUser && m_pModule && (m_pModule->GetType() != ModuleTypeGlobal)) {
pUser->AddBytesWritten(GetBytesWritten());
pUser->AddBytesRead(GetBytesRead());
} else {
View
@@ -639,7 +639,7 @@ CWebSock::EPageReqResult CWebSock::OnPageRequestInternal(const CString& sURI, CS
} else if (pModule->WebRequiresAdmin() && !GetSession()->IsAdmin()) {
PrintErrorPage(403, "Forbidden", "You need to be an admin to access this module");
return PAGE_DONE;
- } else if (!pModule->IsGlobal() && pModule->GetUser() != GetSession()->GetUser()) {
+ } else if (pModule->GetType() != ModuleTypeGlobal && pModule->GetUser() != GetSession()->GetUser()) {
PrintErrorPage(403, "Forbidden", "You must login as " + pModule->GetUser()->GetUserName() + " in order to view this page");
return PAGE_DONE;
} else if (pModule->OnWebPreRequest(*this, m_sPage)) {
@@ -659,7 +659,7 @@ CWebSock::EPageReqResult CWebSock::OnPageRequestInternal(const CString& sURI, CS
}
}
- if (pModule && !pModule->IsGlobal() && (!IsLoggedIn() || pModule->GetUser() != GetSession()->GetUser())) {
+ if (pModule && pModule->GetType() != ModuleTypeGlobal && (!IsLoggedIn() || pModule->GetUser() != GetSession()->GetUser())) {
AddModLoop("UserModLoop", *pModule);
}
View
@@ -148,7 +148,7 @@ class CModPerl: public CGlobalModule {
case Perl_Loaded:
result = HALT;
if (4 == ret) {
- ModInfo.SetGlobal(false);
+ ModInfo.SetType(ModuleTypeUser);
ModInfo.SetDescription(PString(ST(2)));
ModInfo.SetName(sModule);
ModInfo.SetPath(PString(ST(1)));
@@ -178,8 +178,8 @@ class CModPerl: public CGlobalModule {
return result;
}
- virtual void OnGetAvailableMods(set<CModInfo>& ssMods, bool bGlobal) {
- if (bGlobal) {
+ virtual void OnGetAvailableMods(set<CModInfo>& ssMods, EModuleType eType) {
+ if (eType != ModuleTypeUser) {
return;
}
@@ -203,7 +203,7 @@ class CModPerl: public CGlobalModule {
PUSH_STR(sName);
PCALL("ZNC::Core::ModInfoByPath");
if (!SvTRUE(ERRSV) && ret == 2) {
- ModInfo.SetGlobal(false);
+ ModInfo.SetType(ModuleTypeUser);
ModInfo.SetDescription(PString(ST(0)));
ModInfo.SetName(sName);
ModInfo.SetPath(sPath);
Oops, something went wrong.

0 comments on commit a4b1559

Please sign in to comment.