2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
6 * This file is part of InspIRCd. InspIRCd is free software: you can
7 * redistribute it and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "socketengine.h"
24 #include "command_parse.h"
25 #include "exitcodes.h"
34 bool ModuleManager::Load(const std::string& filename, bool defer)
36 /* Don't allow people to specify paths for modules, it doesn't work as expected */
37 if (filename.find('/') != std::string::npos)
40 const std::string moduleFile = ServerInstance->Config->Paths.PrependModule(filename);
42 if (!ServerConfig::FileExists(moduleFile.c_str()))
44 LastModuleError = "Module file could not be found: " + filename;
45 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
49 if (Modules.find(filename) != Modules.end())
51 LastModuleError = "Module " + filename + " is already loaded, cannot load a module twice!";
52 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
56 Module* newmod = NULL;
57 DLLManager* newhandle = new DLLManager(moduleFile.c_str());
61 newmod = newhandle->CallInit();
65 newmod->ModuleSourceFile = filename;
66 newmod->ModuleDLLManager = newhandle;
67 newmod->dying = false;
68 Modules[filename] = newmod;
69 std::string version = newhandle->GetVersion();
72 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "New module introduced: %s (Module version %s)",
73 filename.c_str(), version.c_str());
77 ConfigStatus confstatus;
81 newmod->ReadConfig(confstatus);
83 Version v = newmod->GetVersion();
84 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "New module introduced: %s (Module version %s)%s",
85 filename.c_str(), version.c_str(), (!(v.Flags & VF_VENDOR) ? " [3rd Party]" : " [Vendor]"));
90 LastModuleError = "Unable to load " + filename + ": " + newhandle->LastError();
91 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
96 catch (CoreException& modexcept)
98 // failure in module constructor
101 DoSafeUnload(newmod);
102 ServerInstance->GlobalCulls.AddItem(newhandle);
106 LastModuleError = "Unable to load " + filename + ": " + modexcept.GetReason();
107 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
114 FOREACH_MOD(OnLoadModule, (newmod));
115 /* We give every module a chance to re-prioritize when we introduce a new one,
116 * not just the one thats loading, as the new module could affect the preference
119 for(int tries = 0; tries < 20; tries++)
121 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
122 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
123 n->second->Prioritize();
125 if (prioritizationState == PRIO_STATE_LAST)
128 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "Hook priority dependency loop detected while loading " + filename);
131 ServerInstance->ISupport.Build();
136 struct UnloadAction : public HandlerBase0<void>
139 UnloadAction(Module* m) : mod(m) {}
142 DLLManager* dll = mod->ModuleDLLManager;
143 ServerInstance->Modules->DoSafeUnload(mod);
144 ServerInstance->GlobalCulls.Apply();
146 ServerInstance->GlobalCulls.AddItem(this);
150 struct ReloadAction : public HandlerBase0<void>
153 HandlerBase1<void, bool>* const callback;
154 ReloadAction(Module* m, HandlerBase1<void, bool>* c)
155 : mod(m), callback(c) {}
158 DLLManager* dll = mod->ModuleDLLManager;
159 std::string name = mod->ModuleSourceFile;
160 ServerInstance->Modules->DoSafeUnload(mod);
161 ServerInstance->GlobalCulls.Apply();
163 bool rv = ServerInstance->Modules->Load(name.c_str());
166 ServerInstance->GlobalCulls.AddItem(this);
171 bool ModuleManager::Unload(Module* mod)
175 ServerInstance->AtomicActions.AddAction(new UnloadAction(mod));
179 void ModuleManager::Reload(Module* mod, HandlerBase1<void, bool>* callback)
182 ServerInstance->AtomicActions.AddAction(new ReloadAction(mod, callback));
184 callback->Call(false);
187 /* We must load the modules AFTER initializing the socket engine, now */
188 void ModuleManager::LoadAll()
190 std::cout << std::endl << "Loading core commands";
193 DIR* library = opendir(ServerInstance->Config->Paths.Module.c_str());
196 dirent* entry = NULL;
197 while (0 != (entry = readdir(library)))
199 if (InspIRCd::Match(entry->d_name, "cmd_*.so", ascii_case_insensitive_map))
204 if (!Load(entry->d_name, true))
206 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, this->LastError());
207 std::cout << std::endl << "[" << con_red << "*" << con_reset << "] " << this->LastError() << std::endl << std::endl;
208 ServerInstance->Exit(EXIT_STATUS_MODULE);
213 std::cout << std::endl;
216 ConfigTagList tags = ServerInstance->Config->ConfTags("module");
217 for(ConfigIter i = tags.first; i != tags.second; ++i)
219 ConfigTag* tag = i->second;
220 std::string name = tag->getString("name");
221 std::cout << "[" << con_green << "*" << con_reset << "] Loading module:\t" << con_green << name << con_reset << std::endl;
223 if (!this->Load(name, true))
225 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, this->LastError());
226 std::cout << std::endl << "[" << con_red << "*" << con_reset << "] " << this->LastError() << std::endl << std::endl;
227 ServerInstance->Exit(EXIT_STATUS_MODULE);
231 ConfigStatus confstatus;
233 for(std::map<std::string, Module*>::iterator i = Modules.begin(); i != Modules.end(); i++)
235 Module* mod = i->second;
238 ServerInstance->Logs->Log("MODULE", LOG_DEBUG, "Initializing %s", i->first.c_str());
241 mod->ReadConfig(confstatus);
243 catch (CoreException& modexcept)
245 LastModuleError = "Unable to initialize " + mod->ModuleSourceFile + ": " + modexcept.GetReason();
246 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
247 std::cout << std::endl << "[" << con_red << "*" << con_reset << "] " << LastModuleError << std::endl << std::endl;
248 ServerInstance->Exit(EXIT_STATUS_MODULE);
252 /* We give every module a chance to re-prioritize when we introduce a new one,
253 * not just the one thats loading, as the new module could affect the preference
256 for(int tries = 0; tries < 20; tries++)
258 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
259 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
260 n->second->Prioritize();
262 if (prioritizationState == PRIO_STATE_LAST)
266 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "Hook priority dependency loop detected");
267 ServerInstance->Exit(EXIT_STATUS_MODULE);