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/>.
20 #define MODNAME cmd_all
23 #include "exitcodes.h"
27 typedef std::map<std::string, AllModuleList*> modmap;
28 static std::vector<AllCommandList::fn>* cmdlist = NULL;
29 static modmap* modlist = NULL;
31 AllCommandList::AllCommandList(fn cmd)
34 cmdlist = new std::vector<AllCommandList::fn>();
35 cmdlist->push_back(cmd);
38 AllModuleList::AllModuleList(AllModuleList::fn mod, const std::string& Name) : init(mod), name(Name)
41 modlist = new modmap();
42 modlist->insert(std::make_pair(Name, this));
45 class AllModule : public Module
47 std::vector<Command*> cmds;
55 cmds.reserve(cmdlist->size());
56 for(std::vector<AllCommandList::fn>::iterator i = cmdlist->begin(); i != cmdlist->end(); ++i)
58 Command* c = (*i)(this);
60 ServerInstance->AddCommand(c);
65 this->AllModule::~AllModule();
72 for(std::vector<Command*>::iterator i = cmds.begin(); i != cmds.end(); ++i)
78 return Version("All commands", VF_VENDOR|VF_CORE);
82 MODULE_INIT(AllModule)
84 bool ModuleManager::Load(const std::string& name, bool defer)
86 modmap::iterator it = modlist->find(name);
87 if (it == modlist->end())
92 mod = (*it->second->init)();
93 mod->ModuleSourceFile = name;
94 mod->ModuleDLLManager = NULL;
98 ServerInstance->Logs->Log("MODULE", DEFAULT,"New module introduced: %s", name.c_str());
106 catch (CoreException& modexcept)
110 ServerInstance->Logs->Log("MODULE", DEFAULT, "Unable to load " + name + ": " + modexcept.GetReason());
113 FOREACH_MOD(I_OnLoadModule,OnLoadModule(mod));
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 " + name);
130 ServerInstance->BuildISupport();
135 struct UnloadAction : public HandlerBase0<void>
138 UnloadAction(Module* m) : mod(m) {}
141 ServerInstance->Modules->DoSafeUnload(mod);
142 ServerInstance->GlobalCulls.Apply();
143 ServerInstance->GlobalCulls.AddItem(this);
147 struct ReloadAction : public HandlerBase0<void>
150 HandlerBase1<void, bool>* const callback;
151 ReloadAction(Module* m, HandlerBase1<void, bool>* c)
152 : mod(m), callback(c) {}
155 std::string name = mod->ModuleSourceFile;
156 ServerInstance->Modules->DoSafeUnload(mod);
157 ServerInstance->GlobalCulls.Apply();
158 bool rv = ServerInstance->Modules->Load(name.c_str());
160 ServerInstance->GlobalCulls.AddItem(this);
165 bool ModuleManager::Unload(Module* mod)
169 ServerInstance->AtomicActions.AddAction(new UnloadAction(mod));
173 void ModuleManager::Reload(Module* mod, HandlerBase1<void, bool>* callback)
176 ServerInstance->AtomicActions.AddAction(new ReloadAction(mod, callback));
178 callback->Call(false);
181 void ModuleManager::LoadAll()
183 Load("cmd_all", true);
184 Load("cmd_whowas.so", true);
186 ConfigTagList tags = ServerInstance->Config->ConfTags("module");
187 for(ConfigIter i = tags.first; i != tags.second; ++i)
189 ConfigTag* tag = i->second;
190 std::string name = tag->getString("name");
191 printf_c("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",name.c_str());
193 if (!this->Load(name, true))
195 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
196 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
197 ServerInstance->Exit(EXIT_STATUS_MODULE);
201 for(std::map<std::string, Module*>::iterator i = Modules.begin(); i != Modules.end(); i++)
203 Module* mod = i->second;
208 catch (CoreException& modexcept)
210 LastModuleError = "Unable to initialize " + mod->ModuleSourceFile + ": " + modexcept.GetReason();
211 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
212 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", LastModuleError.c_str());
213 ServerInstance->Exit(EXIT_STATUS_MODULE);
217 /* We give every module a chance to re-prioritize when we introduce a new one,
218 * not just the one thats loading, as the new module could affect the preference
221 for(int tries = 0; tries < 20; tries++)
223 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
224 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
225 n->second->Prioritize();
227 if (prioritizationState == PRIO_STATE_LAST)
231 ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected");
232 ServerInstance->Exit(EXIT_STATUS_MODULE);