-bool ModuleManager::Load(const char* filename)
-{
- /* Do we have a glob pattern in the filename?
- * The user wants to load multiple modules which
- * match the pattern.
- */
- if (strchr(filename,'*') || (strchr(filename,'?')))
- {
- int n_match = 0;
- DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
- if (library)
- {
- /* Try and locate and load all modules matching the pattern */
- dirent* entry = NULL;
- while (0 != (entry = readdir(library)))
- {
- if (InspIRCd::Match(entry->d_name, filename, ascii_case_insensitive_map))
- {
- if (!this->Load(entry->d_name))
- n_match++;
- }
- }
- closedir(library);
- }
- /* Loadmodule will now return false if any one of the modules failed
- * to load (but wont abort when it encounters a bad one) and when 1 or
- * more modules were actually loaded.
- */
- return (n_match > 0 ? false : true);
- }
-
- char modfile[MAXBUF];
- snprintf(modfile,MAXBUF,"%s/%s",ServerInstance->Config->ModPath.c_str(),filename);
- std::string filename_str = filename;
-
- if (!ServerConfig::FileExists(modfile))
- {
- LastModuleError = "Module file could not be found: " + filename_str;
- ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
- return false;
- }
-
- if (Modules.find(filename_str) != Modules.end())
- {
- LastModuleError = "Module " + filename_str + " is already loaded, cannot load a module twice!";
- ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
- return false;
- }
-
- Module* newmod = NULL;
- DLLFactory* newhandle = NULL;
-
- try
- {
- /* This will throw a CoreException if there's a problem loading
- * the module file or getting a pointer to the init_module symbol.
- */
- newhandle = new DLLFactory(modfile, "init_module");
- if (newhandle->init_func)
- newmod = newhandle->init_func();
-
- if (newmod)
- {
- newmod->ModuleSourceFile = filename_str;
- newmod->ModuleDLLFactory = newhandle;
- Version v = newmod->GetVersion();
-
- ServerInstance->Logs->Log("MODULE", DEFAULT,"New module introduced: %s (Module version %s)%s", filename, v.version.c_str(), (!(v.Flags & VF_VENDOR) ? " [3rd Party]" : " [Vendor]"));
-
- Modules[filename_str] = newmod;
- }
- else
- {
- delete newhandle;
- LastModuleError = "Unable to load " + filename_str + ": Probably missing init_module() entrypoint, but dlsym() didn't notice a problem";
- ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
- return false;
- }
- }
- /** XXX: Is there anything we can do about this mess? -- Brain
- * Yeah, don't use exceptions without RAII. -- Daniel
- */
- catch (LoadModuleException& modexcept)
- {
- DetachAll(newmod);
- if (newmod)
- delete newmod;
- if (newhandle)
- delete newhandle;
- LastModuleError = "Unable to load " + filename_str + ": Error when loading: " + modexcept.GetReason();
- ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
- return false;
- }
- catch (FindSymbolException& modexcept)