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 (Module version %s)",
96 filename.c_str(), newhandle->GetVersion().c_str());
102 Version v = newmod->GetVersion();
103 ServerInstance->Logs->Log("MODULE", DEFAULT,"New module introduced: %s (Module version %s)%s",
104 filename.c_str(), newhandle->GetVersion().c_str(), (!(v.Flags & VF_VENDOR) ? " [3rd Party]" : " [Vendor]"));
109 LastModuleError = "Unable to load " + filename + ": " + newhandle->LastError();
110 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
115 catch (CoreException& modexcept)
117 // failure in module constructor
119 DoSafeUnload(newmod);
122 LastModuleError = "Unable to load " + filename + ": " + modexcept.GetReason();
123 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
131 FOREACH_MOD(I_OnLoadModule,OnLoadModule(newmod));
132 /* We give every module a chance to re-prioritize when we introduce a new one,
133 * not just the one thats loading, as the new module could affect the preference
136 for(int tries = 0; tries < 20; tries++)
138 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
139 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
140 n->second->Prioritize();
142 if (prioritizationState == PRIO_STATE_LAST)
145 ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected while loading " + filename);
148 ServerInstance->BuildISupport();
153 struct UnloadAction : public HandlerBase0<void>
156 UnloadAction(Module* m) : mod(m) {}
159 DLLManager* dll = mod->ModuleDLLManager;
160 ServerInstance->Modules->DoSafeUnload(mod);
161 ServerInstance->GlobalCulls.Apply();
163 ServerInstance->GlobalCulls.AddItem(this);
167 struct ReloadAction : public HandlerBase0<void>
170 HandlerBase1<void, bool>* const callback;
171 ReloadAction(Module* m, HandlerBase1<void, bool>* c)
172 : mod(m), callback(c) {}
175 DLLManager* dll = mod->ModuleDLLManager;
176 std::string name = mod->ModuleSourceFile;
177 ServerInstance->Modules->DoSafeUnload(mod);
178 ServerInstance->GlobalCulls.Apply();
180 bool rv = ServerInstance->Modules->Load(name.c_str());
183 ServerInstance->GlobalCulls.AddItem(this);
188 bool ModuleManager::Unload(Module* mod)
192 ServerInstance->AtomicActions.AddAction(new UnloadAction(mod));
196 void ModuleManager::Reload(Module* mod, HandlerBase1<void, bool>* callback)
199 ServerInstance->AtomicActions.AddAction(new ReloadAction(mod, callback));
201 callback->Call(false);
204 /* We must load the modules AFTER initializing the socket engine, now */
205 void ModuleManager::LoadAll()
209 printf("\nLoading core commands");
212 DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
215 dirent* entry = NULL;
216 while (0 != (entry = readdir(library)))
218 if (InspIRCd::Match(entry->d_name, "cmd_*.so", ascii_case_insensitive_map))
223 if (!Load(entry->d_name, true))
225 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
226 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
227 ServerInstance->Exit(EXIT_STATUS_MODULE);
235 ConfigTagList tags = ServerInstance->Config->ConfTags("module");
236 for(ConfigIter i = tags.first; i != tags.second; ++i)
238 ConfigTag* tag = i->second;
239 std::string name = tag->getString("name");
240 printf_c("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",name.c_str());
242 if (!this->Load(name, true))
244 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
245 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
246 ServerInstance->Exit(EXIT_STATUS_MODULE);
250 for(std::map<std::string, Module*>::iterator i = Modules.begin(); i != Modules.end(); i++)
252 Module* mod = i->second;
255 ServerInstance->Logs->Log("MODULE", DEBUG, "Initializing %s", i->first.c_str());
258 catch (CoreException& modexcept)
260 LastModuleError = "Unable to initialize " + mod->ModuleSourceFile + ": " + modexcept.GetReason();
261 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
262 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", LastModuleError.c_str());
263 ServerInstance->Exit(EXIT_STATUS_MODULE);
267 /* We give every module a chance to re-prioritize when we introduce a new one,
268 * not just the one thats loading, as the new module could affect the preference
271 for(int tries = 0; tries < 20; tries++)
273 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
274 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
275 n->second->Prioritize();
277 if (prioritizationState == PRIO_STATE_LAST)
281 ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected");
282 ServerInstance->Exit(EXIT_STATUS_MODULE);