1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 #include "socketengine.h"
18 #include "command_parse.h"
20 #include "exitcodes.h"
28 bool ModuleManager::Load(const std::string& filename, bool defer)
30 /* Don't allow people to specify paths for modules, it doesn't work as expected */
31 if (filename.find('/') != std::string::npos)
33 /* Do we have a glob pattern in the filename?
34 * The user wants to load multiple modules which
37 if (strchr(filename.c_str(),'*') || (strchr(filename.c_str(),'?')))
40 DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
43 /* Try and locate and load all modules matching the pattern */
45 while (0 != (entry = readdir(library)))
47 if (entry->d_name[0] == '.')
49 if (InspIRCd::Match(entry->d_name, filename.c_str(), ascii_case_insensitive_map))
51 if (!this->Load(entry->d_name, defer))
57 /* Loadmodule will now return false if any one of the modules failed
58 * to load (but wont abort when it encounters a bad one) and when 1 or
59 * more modules were actually loaded.
61 return (n_match > 0 ? false : true);
65 snprintf(modfile,MAXBUF,"%s/%s",ServerInstance->Config->ModPath.c_str(),filename.c_str());
67 if (!ServerConfig::FileExists(modfile))
69 LastModuleError = "Module file could not be found: " + filename;
70 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
74 if (Modules.find(filename) != Modules.end())
76 LastModuleError = "Module " + filename + " is already loaded, cannot load a module twice!";
77 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
81 Module* newmod = NULL;
82 DLLManager* newhandle = new DLLManager(modfile);
86 newmod = newhandle->CallInit();
90 newmod->ModuleSourceFile = filename;
91 newmod->ModuleDLLManager = newhandle;
92 Modules[filename] = newmod;
95 ServerInstance->Logs->Log("MODULE", DEFAULT,"New module introduced: %s", filename.c_str());
101 Version v = newmod->GetVersion();
102 ServerInstance->Logs->Log("MODULE", DEFAULT,"New module introduced: %s (Module version %s)%s",
103 filename.c_str(), newhandle->GetVersion().c_str(), (!(v.Flags & VF_VENDOR) ? " [3rd Party]" : " [Vendor]"));
108 LastModuleError = "Unable to load " + filename + ": " + newhandle->LastError();
109 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
114 catch (CoreException& modexcept)
116 // failure in module constructor
118 DoSafeUnload(newmod);
121 LastModuleError = "Unable to load " + filename + ": " + modexcept.GetReason();
122 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
130 FOREACH_MOD(I_OnLoadModule,OnLoadModule(newmod));
131 /* We give every module a chance to re-prioritize when we introduce a new one,
132 * not just the one thats loading, as the new module could affect the preference
135 for(int tries = 0; tries < 20; tries++)
137 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
138 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
139 n->second->Prioritize();
141 if (prioritizationState == PRIO_STATE_LAST)
144 ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected while loading " + filename);
147 ServerInstance->BuildISupport();
152 struct UnloadAction : public HandlerBase0<void>
155 UnloadAction(Module* m) : mod(m) {}
158 DLLManager* dll = mod->ModuleDLLManager;
159 ServerInstance->Modules->DoSafeUnload(mod);
160 ServerInstance->GlobalCulls.Apply();
162 ServerInstance->GlobalCulls.AddItem(this);
166 struct ReloadAction : public HandlerBase0<void>
169 HandlerBase1<void, bool>* const callback;
170 ReloadAction(Module* m, HandlerBase1<void, bool>* c)
171 : mod(m), callback(c) {}
174 DLLManager* dll = mod->ModuleDLLManager;
175 std::string name = mod->ModuleSourceFile;
176 ServerInstance->Modules->DoSafeUnload(mod);
177 ServerInstance->GlobalCulls.Apply();
179 bool rv = ServerInstance->Modules->Load(name.c_str());
182 ServerInstance->GlobalCulls.AddItem(this);
187 bool ModuleManager::Unload(Module* mod)
191 ServerInstance->AtomicActions.AddAction(new UnloadAction(mod));
195 void ModuleManager::Reload(Module* mod, HandlerBase1<void, bool>* callback)
198 ServerInstance->AtomicActions.AddAction(new ReloadAction(mod, callback));
200 callback->Call(false);
203 /* We must load the modules AFTER initializing the socket engine, now */
204 void ModuleManager::LoadAll()
208 printf("\nLoading core commands");
211 DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
214 dirent* entry = NULL;
215 while (0 != (entry = readdir(library)))
217 if (InspIRCd::Match(entry->d_name, "cmd_*.so", ascii_case_insensitive_map))
222 if (!Load(entry->d_name, true))
224 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
225 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
226 ServerInstance->Exit(EXIT_STATUS_MODULE);
234 ConfigTagList tags = ServerInstance->Config->ConfTags("module");
235 for(ConfigIter i = tags.first; i != tags.second; ++i)
237 ConfigTag* tag = i->second;
238 std::string name = tag->getString("name");
239 printf_c("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",name.c_str());
241 if (!this->Load(name, true))
243 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
244 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
245 ServerInstance->Exit(EXIT_STATUS_MODULE);
249 for(std::map<std::string, Module*>::iterator i = Modules.begin(); i != Modules.end(); i++)
251 Module* mod = i->second;
256 catch (CoreException& modexcept)
258 LastModuleError = "Unable to initialize " + mod->ModuleSourceFile + ": " + modexcept.GetReason();
259 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
260 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", LastModuleError.c_str());
261 ServerInstance->Exit(EXIT_STATUS_MODULE);
265 /* We give every module a chance to re-prioritize when we introduce a new one,
266 * not just the one thats loading, as the new module could affect the preference
269 for(int tries = 0; tries < 20; tries++)
271 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
272 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
273 n->second->Prioritize();
275 if (prioritizationState == PRIO_STATE_LAST)
279 ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected");
280 ServerInstance->Exit(EXIT_STATUS_MODULE);