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)
35 snprintf(modfile,MAXBUF,"%s/%s",ServerInstance->Config->ModPath.c_str(),filename.c_str());
37 if (!ServerConfig::FileExists(modfile))
39 LastModuleError = "Module file could not be found: " + filename;
40 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
44 if (Modules.find(filename) != Modules.end())
46 LastModuleError = "Module " + filename + " is already loaded, cannot load a module twice!";
47 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
51 Module* newmod = NULL;
52 DLLManager* newhandle = new DLLManager(modfile);
56 newmod = newhandle->CallInit();
60 newmod->ModuleSourceFile = filename;
61 newmod->ModuleDLLManager = newhandle;
62 Modules[filename] = newmod;
65 ServerInstance->Logs->Log("MODULE", DEFAULT,"New module introduced: %s (Module version %s)",
66 filename.c_str(), newhandle->GetVersion().c_str());
72 Version v = newmod->GetVersion();
73 ServerInstance->Logs->Log("MODULE", DEFAULT,"New module introduced: %s (Module version %s)%s",
74 filename.c_str(), newhandle->GetVersion().c_str(), (!(v.Flags & VF_VENDOR) ? " [3rd Party]" : " [Vendor]"));
79 LastModuleError = "Unable to load " + filename + ": " + newhandle->LastError();
80 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
85 catch (CoreException& modexcept)
87 // failure in module constructor
92 LastModuleError = "Unable to load " + filename + ": " + modexcept.GetReason();
93 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
101 FOREACH_MOD(I_OnLoadModule,OnLoadModule(newmod));
102 /* We give every module a chance to re-prioritize when we introduce a new one,
103 * not just the one thats loading, as the new module could affect the preference
106 for(int tries = 0; tries < 20; tries++)
108 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
109 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
110 n->second->Prioritize();
112 if (prioritizationState == PRIO_STATE_LAST)
115 ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected while loading " + filename);
118 ServerInstance->BuildISupport();
123 struct UnloadAction : public HandlerBase0<void>
126 UnloadAction(Module* m) : mod(m) {}
129 DLLManager* dll = mod->ModuleDLLManager;
130 ServerInstance->Modules->DoSafeUnload(mod);
131 ServerInstance->GlobalCulls.Apply();
133 ServerInstance->GlobalCulls.AddItem(this);
137 struct ReloadAction : public HandlerBase0<void>
140 HandlerBase1<void, bool>* const callback;
141 ReloadAction(Module* m, HandlerBase1<void, bool>* c)
142 : mod(m), callback(c) {}
145 DLLManager* dll = mod->ModuleDLLManager;
146 std::string name = mod->ModuleSourceFile;
147 ServerInstance->Modules->DoSafeUnload(mod);
148 ServerInstance->GlobalCulls.Apply();
150 bool rv = ServerInstance->Modules->Load(name.c_str());
153 ServerInstance->GlobalCulls.AddItem(this);
158 bool ModuleManager::Unload(Module* mod)
162 ServerInstance->AtomicActions.AddAction(new UnloadAction(mod));
166 void ModuleManager::Reload(Module* mod, HandlerBase1<void, bool>* callback)
169 ServerInstance->AtomicActions.AddAction(new ReloadAction(mod, callback));
171 callback->Call(false);
174 /* We must load the modules AFTER initializing the socket engine, now */
175 void ModuleManager::LoadAll()
179 printf("\nLoading core commands");
182 DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
185 dirent* entry = NULL;
186 while (0 != (entry = readdir(library)))
188 if (InspIRCd::Match(entry->d_name, "cmd_*.so", ascii_case_insensitive_map))
193 if (!Load(entry->d_name, true))
195 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
196 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
197 ServerInstance->Exit(EXIT_STATUS_MODULE);
205 ConfigTagList tags = ServerInstance->Config->ConfTags("module");
206 for(ConfigIter i = tags.first; i != tags.second; ++i)
208 ConfigTag* tag = i->second;
209 std::string name = tag->getString("name");
210 printf_c("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",name.c_str());
212 if (!this->Load(name, true))
214 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
215 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
216 ServerInstance->Exit(EXIT_STATUS_MODULE);
220 for(std::map<std::string, Module*>::iterator i = Modules.begin(); i != Modules.end(); i++)
222 Module* mod = i->second;
225 ServerInstance->Logs->Log("MODULE", DEBUG, "Initializing %s", i->first.c_str());
228 catch (CoreException& modexcept)
230 LastModuleError = "Unable to initialize " + mod->ModuleSourceFile + ": " + modexcept.GetReason();
231 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
232 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", LastModuleError.c_str());
233 ServerInstance->Exit(EXIT_STATUS_MODULE);
237 /* We give every module a chance to re-prioritize when we introduce a new one,
238 * not just the one thats loading, as the new module could affect the preference
241 for(int tries = 0; tries < 20; tries++)
243 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
244 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
245 n->second->Prioritize();
247 if (prioritizationState == PRIO_STATE_LAST)
251 ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected");
252 ServerInstance->Exit(EXIT_STATUS_MODULE);