Skip to content

Commit

Permalink
Create EModuleType, creating SetType which replaces SetGlobal
Browse files Browse the repository at this point in the history
  • Loading branch information
kylef committed Aug 21, 2011
1 parent 091a287 commit a4b1559
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 96 deletions.
56 changes: 31 additions & 25 deletions ClientCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
20 changes: 10 additions & 10 deletions Modules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand Down
34 changes: 19 additions & 15 deletions Modules.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class CModInfo;
#endif
#endif

typedef enum {
ModuleTypeGlobal,
ModuleTypeUser
} EModuleType;

typedef void* ModHandle;

template<class M> void TModInfo(CModInfo& Info) {}
Expand All @@ -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; \
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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; }
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
};

Expand All @@ -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:
};

Expand Down
2 changes: 1 addition & 1 deletion Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions WebModules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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);
}

Expand Down
8 changes: 4 additions & 4 deletions modules/modperl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
Expand Down
Loading

0 comments on commit a4b1559

Please sign in to comment.