Change a way for modules to provide description.

Now there're only 2 functions which modules export:
ZNCModVersion() and ZNCModInfo().

ZNCModInfo() returns CModInfo instance, which contains
description, globality, function for loading the module. It needs to be
deleted afterwise.
This commit is contained in:
Alexey Sokolov
2011-06-11 17:19:36 +07:00
parent 4dbd088fd7
commit ad9f1f8ab9
2 changed files with 82 additions and 105 deletions
+35 -70
View File
@@ -816,16 +816,15 @@ bool CModules::LoadModule(const CString& sModule, const CString& sArgs, CUser* p
GLOBALMODULECALL(OnModuleLoading(sModule, sArgs, bSuccess, sRetMsg), pUser, NULL, return bSuccess);
CString sModPath, sDataPath;
CString sDesc;
bool bVersionMismatch;
bool bIsGlobal;
CModInfo* Info;
if (!FindModPath(sModule, sModPath, sDataPath)) {
sRetMsg = "Unable to find module [" + sModule + "]";
return false;
}
ModHandle p = OpenModule(sModule, sModPath, bVersionMismatch, bIsGlobal, sDesc, sRetMsg);
ModHandle p = OpenModule(sModule, sModPath, bVersionMismatch, Info, sRetMsg);
if (!p)
return false;
@@ -836,10 +835,11 @@ bool CModules::LoadModule(const CString& sModule, const CString& sArgs, CUser* p
return false;
}
if ((pUser == NULL) != bIsGlobal) {
if ((pUser == NULL) != Info->IsGlobal()) {
delete Info;
dlclose(p);
sRetMsg = "Module [" + sModule + "] is ";
sRetMsg += (bIsGlobal) ? "" : "not ";
sRetMsg += Info->IsGlobal() ? "" : "not ";
sRetMsg += "a global module.";
return false;
}
@@ -847,37 +847,19 @@ bool CModules::LoadModule(const CString& sModule, const CString& sArgs, CUser* p
CModule* pModule = NULL;
if (pUser) {
typedef CModule* (*fp)(ModHandle, CUser* pUser,
const CString& sModName, const CString& sDataPath);
fp Load = (fp) dlsym(p, "ZNCModLoad");
if (!Load) {
dlclose(p);
sRetMsg = "Could not find ZNCModLoad() in module [" + sModule + "]";
return false;
}
pModule = Load(p, pUser, sModule, sDataPath);
pModule = Info->GetLoader()(p, pUser, sModule, sDataPath);
} else {
typedef CModule* (*fp)(ModHandle, const CString& sModName,
const CString& sDataPath);
fp Load = (fp) dlsym(p, "ZNCModLoad");
if (!Load) {
dlclose(p);
sRetMsg = "Could not find ZNCModLoad() in module [" + sModule + "]";
return false;
}
pModule = Load(p, sModule, sDataPath);
pModule = Info->GetGlobalLoader()(p, sModule, sDataPath);
}
pModule->SetDescription(sDesc);
pModule->SetGlobal(bIsGlobal);
pModule->SetDescription(Info->GetDescription());
pModule->SetGlobal(Info->IsGlobal());
pModule->SetArgs(sArgs);
pModule->SetModPath(CDir::ChangeDir(CZNC::Get().GetCurPath(), sModPath));
push_back(pModule);
delete Info;
bool bLoaded;
try {
bLoaded = pModule->OnLoad(sArgs, sRetMsg);
@@ -923,27 +905,19 @@ bool CModules::UnloadModule(const CString& sModule, CString& sRetMsg) {
ModHandle p = pModule->GetDLL();
if (p) {
typedef void (*fp)(CModule*);
fp Unload = (fp)dlsym(p, "ZNCModUnload");
delete pModule;
if (Unload) {
Unload(pModule);
for (iterator it = begin(); it != end(); ++it) {
if (*it == pModule) {
erase(it);
break;
}
for (iterator it = begin(); it != end(); ++it) {
if (*it == pModule) {
erase(it);
break;
}
dlclose(p);
sRetMsg = "Module [" + sMod + "] unloaded";
return true;
} else {
sRetMsg = "Unable to unload module [" + sMod + "] could not find ZNCModUnload()";
return false;
}
dlclose(p);
sRetMsg = "Module [" + sMod + "] unloaded";
return true;
}
sRetMsg = "Unable to unload module [" + sMod + "]";
@@ -980,17 +954,18 @@ bool CModules::GetModInfo(CModInfo& ModInfo, const CString& sModule, CString& sR
}
bool CModules::GetModPathInfo(CModInfo& ModInfo, const CString& sModule, const CString& sModPath, CString& sRetMsg) {
CString sDesc;
bool bVersionMismatch;
bool bIsGlobal;
CModInfo* Info;
ModHandle p = OpenModule(sModule, sModPath, bVersionMismatch, bIsGlobal, sDesc, sRetMsg);
ModHandle p = OpenModule(sModule, sModPath, bVersionMismatch, Info, sRetMsg);
if (!p)
return false;
ModInfo.SetGlobal(bIsGlobal);
ModInfo.SetDescription(sDesc);
if (Info) {
ModInfo.SetGlobal(Info->IsGlobal());
ModInfo.SetDescription(Info->GetDescription());
}
ModInfo.SetName(sModule);
ModInfo.SetPath(sModPath);
@@ -998,6 +973,7 @@ bool CModules::GetModPathInfo(CModInfo& ModInfo, const CString& sModule, const C
ModInfo.SetDescription("--- Version mismatch, recompile this module. ---");
}
delete Info;
dlclose(p);
return true;
@@ -1082,11 +1058,10 @@ CModules::ModDirList CModules::GetModDirs() {
}
ModHandle CModules::OpenModule(const CString& sModule, const CString& sModPath, bool &bVersionMismatch,
bool &bIsGlobal, CString& sDesc, CString& sRetMsg) {
CModInfo*& Info, CString& sRetMsg) {
// Some sane defaults in case anything errors out below
bVersionMismatch = false;
bIsGlobal = false;
sDesc.clear();
Info = NULL;
sRetMsg.clear();
for (unsigned int a = 0; a < sModule.length(); a++) {
@@ -1124,21 +1099,12 @@ ModHandle CModules::OpenModule(const CString& sModule, const CString& sModPath,
return NULL;
}
typedef bool (*bFP)();
bFP IsGlobal = (bFP) dlsym(p, "ZNCModGlobal");
typedef CModInfo* (*InfoFP)();
InfoFP ZNCModInfo = (InfoFP) dlsym(p, "ZNCModInfo");
if (!IsGlobal) {
if (!ZNCModInfo) {
dlclose(p);
sRetMsg = "Could not find ZNCModGlobal() in module [" + sModule + "]";
return NULL;
}
typedef const char *(*sFP)();
sFP GetDesc = (sFP) dlsym(p, "ZNCModDescription");
if (!GetDesc) {
dlclose(p);
sRetMsg = "Could not find ZNCModDescription() in module [" + sModule + "]";
sRetMsg = "Could not find ZNCModInfo() in module [" + sModule + "]";
return NULL;
}
@@ -1146,10 +1112,9 @@ ModHandle CModules::OpenModule(const CString& sModule, const CString& sModPath,
bVersionMismatch = true;
sRetMsg = "Version mismatch, recompile this module.";
} else {
Info = ZNCModInfo();
sRetMsg = "";
bVersionMismatch = false;
bIsGlobal = IsGlobal();
sDesc = GetDesc();
}
return p;