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)
42 snprintf(modfile,MAXBUF,"%s/%s",ServerInstance->Config->ModPath.c_str(),filename.c_str());
44 if (!ServerConfig::FileExists(modfile))
46 LastModuleError = "Module file could not be found: " + filename;
47 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
51 if (Modules.find(filename) != Modules.end())
53 LastModuleError = "Module " + filename + " is already loaded, cannot load a module twice!";
54 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
58 Module* newmod = NULL;
59 DLLManager* newhandle = new DLLManager(modfile);
63 newmod = newhandle->CallInit();
67 newmod->ModuleSourceFile = filename;
68 newmod->ModuleDLLManager = newhandle;
69 Modules[filename] = newmod;
70 std::string version = newhandle->GetVersion();
73 ServerInstance->Logs->Log("MODULE", DEFAULT,"New module introduced: %s (Module version %s)",
74 filename.c_str(), version.c_str());
80 Version v = newmod->GetVersion();
81 ServerInstance->Logs->Log("MODULE", DEFAULT,"New module introduced: %s (Module version %s)%s",
82 filename.c_str(), version.c_str(), (!(v.Flags & VF_VENDOR) ? " [3rd Party]" : " [Vendor]"));
87 LastModuleError = "Unable to load " + filename + ": " + newhandle->LastError();
88 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
93 catch (CoreException& modexcept)
95 // failure in module constructor
100 LastModuleError = "Unable to load " + filename + ": " + modexcept.GetReason();
101 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
109 FOREACH_MOD(I_OnLoadModule,OnLoadModule(newmod));
110 /* We give every module a chance to re-prioritize when we introduce a new one,
111 * not just the one thats loading, as the new module could affect the preference
114 for(int tries = 0; tries < 20; tries++)
116 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
117 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
118 n->second->Prioritize();
120 if (prioritizationState == PRIO_STATE_LAST)
123 ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected while loading " + filename);
126 ServerInstance->BuildISupport();
131 struct UnloadAction : public HandlerBase0<void>
134 UnloadAction(Module* m) : mod(m) {}
137 DLLManager* dll = mod->ModuleDLLManager;
138 ServerInstance->Modules->DoSafeUnload(mod);
139 ServerInstance->GlobalCulls.Apply();
141 ServerInstance->GlobalCulls.AddItem(this);
145 struct ReloadAction : public HandlerBase0<void>
148 HandlerBase1<void, bool>* const callback;
149 ReloadAction(Module* m, HandlerBase1<void, bool>* c)
150 : mod(m), callback(c) {}
153 DLLManager* dll = mod->ModuleDLLManager;
154 std::string name = mod->ModuleSourceFile;
155 ServerInstance->Modules->DoSafeUnload(mod);
156 ServerInstance->GlobalCulls.Apply();
158 bool rv = ServerInstance->Modules->Load(name.c_str());
161 ServerInstance->GlobalCulls.AddItem(this);
166 bool ModuleManager::Unload(Module* mod)
170 ServerInstance->AtomicActions.AddAction(new UnloadAction(mod));
174 void ModuleManager::Reload(Module* mod, HandlerBase1<void, bool>* callback)
177 ServerInstance->AtomicActions.AddAction(new ReloadAction(mod, callback));
179 callback->Call(false);
182 /* We must load the modules AFTER initializing the socket engine, now */
183 void ModuleManager::LoadAll()
187 std::cout << std::endl << "Loading core commands";
190 DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
193 dirent* entry = NULL;
194 while (0 != (entry = readdir(library)))
196 if (InspIRCd::Match(entry->d_name, "cmd_*.so", ascii_case_insensitive_map))
201 if (!Load(entry->d_name, true))
203 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
204 std::cout << std::endl << "[" << con_red << "*" << con_reset << "]" << this->LastError() << std::endl << std::endl;
205 ServerInstance->Exit(EXIT_STATUS_MODULE);
210 std::cout << std::endl;
213 ConfigTagList tags = ServerInstance->Config->ConfTags("module");
214 for(ConfigIter i = tags.first; i != tags.second; ++i)
216 ConfigTag* tag = i->second;
217 std::string name = tag->getString("name");
218 std::cout << "[" << con_green << "*" << con_reset << "] Loading module:\t" << con_green << name << con_reset << std::endl;
220 if (!this->Load(name, true))
222 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
223 std::cout << std::endl << "[" << con_red << "*" << con_reset << "]" << this->LastError() << std::endl << std::endl;
224 ServerInstance->Exit(EXIT_STATUS_MODULE);
228 for(std::map<std::string, Module*>::iterator i = Modules.begin(); i != Modules.end(); i++)
230 Module* mod = i->second;
233 ServerInstance->Logs->Log("MODULE", DEBUG, "Initializing %s", i->first.c_str());
236 catch (CoreException& modexcept)
238 LastModuleError = "Unable to initialize " + mod->ModuleSourceFile + ": " + modexcept.GetReason();
239 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
240 std::cout << std::endl << "[" << con_red << "*" << con_reset << "]" << LastModuleError << std::endl << std::endl;
241 ServerInstance->Exit(EXIT_STATUS_MODULE);
245 /* We give every module a chance to re-prioritize when we introduce a new one,
246 * not just the one thats loading, as the new module could affect the preference
249 for(int tries = 0; tries < 20; tries++)
251 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
252 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
253 n->second->Prioritize();
255 if (prioritizationState == PRIO_STATE_LAST)
259 ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected");
260 ServerInstance->Exit(EXIT_STATUS_MODULE);