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"
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)
41 snprintf(modfile,MAXBUF,"%s/%s",ServerInstance->Config->ModPath.c_str(),filename.c_str());
43 if (!ServerConfig::FileExists(modfile))
45 LastModuleError = "Module file could not be found: " + filename;
46 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
50 if (Modules.find(filename) != Modules.end())
52 LastModuleError = "Module " + filename + " is already loaded, cannot load a module twice!";
53 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
57 Module* newmod = NULL;
58 DLLManager* newhandle = new DLLManager(modfile);
62 newmod = newhandle->CallInit();
66 newmod->ModuleSourceFile = filename;
67 newmod->ModuleDLLManager = newhandle;
68 Modules[filename] = newmod;
71 ServerInstance->Logs->Log("MODULE", DEFAULT,"New module introduced: %s (Module version %s)",
72 filename.c_str(), newhandle->GetVersion().c_str());
78 Version v = newmod->GetVersion();
79 ServerInstance->Logs->Log("MODULE", DEFAULT,"New module introduced: %s (Module version %s)%s",
80 filename.c_str(), newhandle->GetVersion().c_str(), (!(v.Flags & VF_VENDOR) ? " [3rd Party]" : " [Vendor]"));
85 LastModuleError = "Unable to load " + filename + ": " + newhandle->LastError();
86 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
91 catch (CoreException& modexcept)
93 // failure in module constructor
98 LastModuleError = "Unable to load " + filename + ": " + modexcept.GetReason();
99 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
107 FOREACH_MOD(I_OnLoadModule,OnLoadModule(newmod));
108 /* We give every module a chance to re-prioritize when we introduce a new one,
109 * not just the one thats loading, as the new module could affect the preference
112 for(int tries = 0; tries < 20; tries++)
114 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
115 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
116 n->second->Prioritize();
118 if (prioritizationState == PRIO_STATE_LAST)
121 ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected while loading " + filename);
124 ServerInstance->BuildISupport();
129 struct UnloadAction : public HandlerBase0<void>
132 UnloadAction(Module* m) : mod(m) {}
135 DLLManager* dll = mod->ModuleDLLManager;
136 ServerInstance->Modules->DoSafeUnload(mod);
137 ServerInstance->GlobalCulls.Apply();
139 ServerInstance->GlobalCulls.AddItem(this);
143 struct ReloadAction : public HandlerBase0<void>
146 HandlerBase1<void, bool>* const callback;
147 ReloadAction(Module* m, HandlerBase1<void, bool>* c)
148 : mod(m), callback(c) {}
151 DLLManager* dll = mod->ModuleDLLManager;
152 std::string name = mod->ModuleSourceFile;
153 ServerInstance->Modules->DoSafeUnload(mod);
154 ServerInstance->GlobalCulls.Apply();
156 bool rv = ServerInstance->Modules->Load(name.c_str());
159 ServerInstance->GlobalCulls.AddItem(this);
164 bool ModuleManager::Unload(Module* mod)
168 ServerInstance->AtomicActions.AddAction(new UnloadAction(mod));
172 void ModuleManager::Reload(Module* mod, HandlerBase1<void, bool>* callback)
175 ServerInstance->AtomicActions.AddAction(new ReloadAction(mod, callback));
177 callback->Call(false);
180 /* We must load the modules AFTER initializing the socket engine, now */
181 void ModuleManager::LoadAll()
185 printf("\nLoading core commands");
188 DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
191 dirent* entry = NULL;
192 while (0 != (entry = readdir(library)))
194 if (InspIRCd::Match(entry->d_name, "cmd_*.so", ascii_case_insensitive_map))
199 if (!Load(entry->d_name, true))
201 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
202 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
203 ServerInstance->Exit(EXIT_STATUS_MODULE);
211 ConfigTagList tags = ServerInstance->Config->ConfTags("module");
212 for(ConfigIter i = tags.first; i != tags.second; ++i)
214 ConfigTag* tag = i->second;
215 std::string name = tag->getString("name");
216 printf_c("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",name.c_str());
218 if (!this->Load(name, true))
220 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
221 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
222 ServerInstance->Exit(EXIT_STATUS_MODULE);
226 for(std::map<std::string, Module*>::iterator i = Modules.begin(); i != Modules.end(); i++)
228 Module* mod = i->second;
231 ServerInstance->Logs->Log("MODULE", DEBUG, "Initializing %s", i->first.c_str());
234 catch (CoreException& modexcept)
236 LastModuleError = "Unable to initialize " + mod->ModuleSourceFile + ": " + modexcept.GetReason();
237 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
238 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", LastModuleError.c_str());
239 ServerInstance->Exit(EXIT_STATUS_MODULE);
243 /* We give every module a chance to re-prioritize when we introduce a new one,
244 * not just the one thats loading, as the new module could affect the preference
247 for(int tries = 0; tries < 20; tries++)
249 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
250 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
251 n->second->Prioritize();
253 if (prioritizationState == PRIO_STATE_LAST)
257 ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected");
258 ServerInstance->Exit(EXIT_STATUS_MODULE);