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"
26 #include "exitcodes.h"
35 bool ModuleManager::Load(const std::string& filename, bool defer)
37 /* Don't allow people to specify paths for modules, it doesn't work as expected */
38 if (filename.find('/') != std::string::npos)
40 LastModuleError = "You can't load modules with a path: " + filename;
45 snprintf(modfile,MAXBUF,"%s/%s",ServerInstance->Config->ModPath.c_str(),filename.c_str());
47 if (!ServerConfig::FileExists(modfile))
49 LastModuleError = "Module file could not be found: " + filename;
50 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
54 if (Modules.find(filename) != Modules.end())
56 LastModuleError = "Module " + filename + " is already loaded, cannot load a module twice!";
57 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
61 Module* newmod = NULL;
62 DLLManager* newhandle = new DLLManager(modfile);
66 newmod = newhandle->CallInit();
70 newmod->ModuleSourceFile = filename;
71 newmod->ModuleDLLManager = newhandle;
72 newmod->dying = false;
73 Modules[filename] = newmod;
74 std::string version = newhandle->GetVersion();
77 ServerInstance->Logs->Log("MODULE", DEFAULT,"New module introduced: %s (Module version %s)",
78 filename.c_str(), version.c_str());
84 Version v = newmod->GetVersion();
85 ServerInstance->Logs->Log("MODULE", DEFAULT,"New module introduced: %s (Module version %s)%s",
86 filename.c_str(), version.c_str(), (!(v.Flags & VF_VENDOR) ? " [3rd Party]" : " [Vendor]"));
91 LastModuleError = "Unable to load " + filename + ": " + newhandle->LastError();
92 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
97 catch (CoreException& modexcept)
99 // failure in module constructor
102 DoSafeUnload(newmod);
103 ServerInstance->GlobalCulls.AddItem(newhandle);
107 LastModuleError = "Unable to load " + filename + ": " + modexcept.GetReason();
108 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
116 FOREACH_MOD(I_OnLoadModule,OnLoadModule(newmod));
117 /* We give every module a chance to re-prioritize when we introduce a new one,
118 * not just the one thats loading, as the new module could affect the preference
121 for(int tries = 0; tries < 20; tries++)
123 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
124 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
125 n->second->Prioritize();
127 if (prioritizationState == PRIO_STATE_LAST)
130 ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected while loading " + filename);
133 ServerInstance->BuildISupport();
138 struct UnloadAction : public HandlerBase0<void>
141 UnloadAction(Module* m) : mod(m) {}
144 DLLManager* dll = mod->ModuleDLLManager;
145 ServerInstance->Modules->DoSafeUnload(mod);
146 ServerInstance->GlobalCulls.Apply();
148 ServerInstance->GlobalCulls.AddItem(this);
152 struct ReloadAction : public HandlerBase0<void>
155 HandlerBase1<void, bool>* const callback;
156 ReloadAction(Module* m, HandlerBase1<void, bool>* c)
157 : mod(m), callback(c) {}
160 DLLManager* dll = mod->ModuleDLLManager;
161 std::string name = mod->ModuleSourceFile;
162 ServerInstance->Modules->DoSafeUnload(mod);
163 ServerInstance->GlobalCulls.Apply();
165 bool rv = ServerInstance->Modules->Load(name.c_str());
168 ServerInstance->GlobalCulls.AddItem(this);
173 bool ModuleManager::Unload(Module* mod)
177 ServerInstance->AtomicActions.AddAction(new UnloadAction(mod));
181 void ModuleManager::Reload(Module* mod, HandlerBase1<void, bool>* callback)
184 ServerInstance->AtomicActions.AddAction(new ReloadAction(mod, callback));
186 callback->Call(false);
189 /* We must load the modules AFTER initializing the socket engine, now */
190 void ModuleManager::LoadAll()
194 std::cout << std::endl << "Loading core commands";
197 DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
200 dirent* entry = NULL;
201 while (0 != (entry = readdir(library)))
203 if (InspIRCd::Match(entry->d_name, "cmd_*.so", ascii_case_insensitive_map))
208 if (!Load(entry->d_name, true))
210 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
211 std::cout << std::endl << "[" << con_red << "*" << con_reset << "] " << this->LastError() << std::endl << std::endl;
212 ServerInstance->Exit(EXIT_STATUS_MODULE);
217 std::cout << std::endl;
220 ConfigTagList tags = ServerInstance->Config->ConfTags("module");
221 for(ConfigIter i = tags.first; i != tags.second; ++i)
223 ConfigTag* tag = i->second;
224 std::string name = tag->getString("name");
225 std::cout << "[" << con_green << "*" << con_reset << "] Loading module:\t" << con_green << name << con_reset << std::endl;
227 if (!this->Load(name, true))
229 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
230 std::cout << std::endl << "[" << con_red << "*" << con_reset << "] " << this->LastError() << std::endl << std::endl;
231 ServerInstance->Exit(EXIT_STATUS_MODULE);
235 for(std::map<std::string, Module*>::iterator i = Modules.begin(); i != Modules.end(); i++)
237 Module* mod = i->second;
240 ServerInstance->Logs->Log("MODULE", DEBUG, "Initializing %s", i->first.c_str());
243 catch (CoreException& modexcept)
245 LastModuleError = "Unable to initialize " + mod->ModuleSourceFile + ": " + modexcept.GetReason();
246 ServerInstance->Logs->Log("MODULE", 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", DEFAULT, "Hook priority dependency loop detected");
267 ServerInstance->Exit(EXIT_STATUS_MODULE);