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 newmod->dying = false;
70 Modules[filename] = newmod;
71 std::string version = newhandle->GetVersion();
74 ServerInstance->Logs->Log("MODULE", DEFAULT,"New module introduced: %s (Module version %s)",
75 filename.c_str(), version.c_str());
81 Version v = newmod->GetVersion();
82 ServerInstance->Logs->Log("MODULE", DEFAULT,"New module introduced: %s (Module version %s)%s",
83 filename.c_str(), version.c_str(), (!(v.Flags & VF_VENDOR) ? " [3rd Party]" : " [Vendor]"));
88 LastModuleError = "Unable to load " + filename + ": " + newhandle->LastError();
89 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
94 catch (CoreException& modexcept)
96 // failure in module constructor
100 ServerInstance->GlobalCulls.AddItem(newhandle);
104 LastModuleError = "Unable to load " + filename + ": " + modexcept.GetReason();
105 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
113 FOREACH_MOD(I_OnLoadModule,OnLoadModule(newmod));
114 /* We give every module a chance to re-prioritize when we introduce a new one,
115 * not just the one thats loading, as the new module could affect the preference
118 for(int tries = 0; tries < 20; tries++)
120 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
121 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
122 n->second->Prioritize();
124 if (prioritizationState == PRIO_STATE_LAST)
127 ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected while loading " + filename);
130 ServerInstance->BuildISupport();
135 struct UnloadAction : public HandlerBase0<void>
138 UnloadAction(Module* m) : mod(m) {}
141 DLLManager* dll = mod->ModuleDLLManager;
142 ServerInstance->Modules->DoSafeUnload(mod);
143 ServerInstance->GlobalCulls.Apply();
145 ServerInstance->GlobalCulls.AddItem(this);
149 struct ReloadAction : public HandlerBase0<void>
152 HandlerBase1<void, bool>* const callback;
153 ReloadAction(Module* m, HandlerBase1<void, bool>* c)
154 : mod(m), callback(c) {}
157 DLLManager* dll = mod->ModuleDLLManager;
158 std::string name = mod->ModuleSourceFile;
159 ServerInstance->Modules->DoSafeUnload(mod);
160 ServerInstance->GlobalCulls.Apply();
162 bool rv = ServerInstance->Modules->Load(name.c_str());
165 ServerInstance->GlobalCulls.AddItem(this);
170 bool ModuleManager::Unload(Module* mod)
174 ServerInstance->AtomicActions.AddAction(new UnloadAction(mod));
178 void ModuleManager::Reload(Module* mod, HandlerBase1<void, bool>* callback)
181 ServerInstance->AtomicActions.AddAction(new ReloadAction(mod, callback));
183 callback->Call(false);
186 /* We must load the modules AFTER initializing the socket engine, now */
187 void ModuleManager::LoadAll()
191 std::cout << std::endl << "Loading core commands";
194 DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
197 dirent* entry = NULL;
198 while (0 != (entry = readdir(library)))
200 if (InspIRCd::Match(entry->d_name, "cmd_*.so", ascii_case_insensitive_map))
205 if (!Load(entry->d_name, true))
207 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
208 std::cout << std::endl << "[" << con_red << "*" << con_reset << "] " << this->LastError() << std::endl << std::endl;
209 ServerInstance->Exit(EXIT_STATUS_MODULE);
214 std::cout << std::endl;
217 ConfigTagList tags = ServerInstance->Config->ConfTags("module");
218 for(ConfigIter i = tags.first; i != tags.second; ++i)
220 ConfigTag* tag = i->second;
221 std::string name = tag->getString("name");
222 std::cout << "[" << con_green << "*" << con_reset << "] Loading module:\t" << con_green << name << con_reset << std::endl;
224 if (!this->Load(name, true))
226 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
227 std::cout << std::endl << "[" << con_red << "*" << con_reset << "] " << this->LastError() << std::endl << std::endl;
228 ServerInstance->Exit(EXIT_STATUS_MODULE);
232 for(std::map<std::string, Module*>::iterator i = Modules.begin(); i != Modules.end(); i++)
234 Module* mod = i->second;
237 ServerInstance->Logs->Log("MODULE", DEBUG, "Initializing %s", i->first.c_str());
240 catch (CoreException& modexcept)
242 LastModuleError = "Unable to initialize " + mod->ModuleSourceFile + ": " + modexcept.GetReason();
243 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
244 std::cout << std::endl << "[" << con_red << "*" << con_reset << "] " << LastModuleError << std::endl << std::endl;
245 ServerInstance->Exit(EXIT_STATUS_MODULE);
249 /* We give every module a chance to re-prioritize when we introduce a new one,
250 * not just the one thats loading, as the new module could affect the preference
253 for(int tries = 0; tries < 20; tries++)
255 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
256 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
257 n->second->Prioritize();
259 if (prioritizationState == PRIO_STATE_LAST)
263 ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected");
264 ServerInstance->Exit(EXIT_STATUS_MODULE);