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"
28 typedef std::map<std::string, AllModuleList*> modmap;
29 static std::vector<AllCommandList::fn>* cmdlist = NULL;
30 static modmap* modlist = NULL;
32 AllCommandList::AllCommandList(fn cmd)
35 cmdlist = new std::vector<AllCommandList::fn>();
36 cmdlist->push_back(cmd);
39 AllModuleList::AllModuleList(AllModuleList::fn mod, const std::string& Name) : init(mod), name(Name)
42 modlist = new modmap();
43 modlist->insert(std::make_pair(Name, this));
46 class AllModule : public Module
48 std::vector<Command*> cmds;
56 cmds.reserve(cmdlist->size());
57 for(std::vector<AllCommandList::fn>::iterator i = cmdlist->begin(); i != cmdlist->end(); ++i)
59 Command* c = (*i)(this);
61 ServerInstance->AddCommand(c);
66 this->AllModule::~AllModule();
73 for(std::vector<Command*>::iterator i = cmds.begin(); i != cmds.end(); ++i)
79 return Version("All commands", VF_VENDOR|VF_CORE);
83 MODULE_INIT(AllModule)
85 bool ModuleManager::Load(const std::string& name, bool defer)
87 modmap::iterator it = modlist->find(name);
88 if (it == modlist->end())
93 mod = (*it->second->init)();
94 mod->ModuleSourceFile = name;
95 mod->ModuleDLLManager = NULL;
100 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT,"New module introduced: %s", name.c_str());
108 catch (CoreException& modexcept)
112 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "Unable to load " + name + ": " + modexcept.GetReason());
115 FOREACH_MOD(I_OnLoadModule,OnLoadModule(mod));
116 /* We give every module a chance to re-prioritize when we introduce a new one,
117 * not just the one thats loading, as the new module could affect the preference
120 for(int tries = 0; tries < 20; tries++)
122 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
123 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
124 n->second->Prioritize();
126 if (prioritizationState == PRIO_STATE_LAST)
129 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "Hook priority dependency loop detected while loading " + name);
132 ServerInstance->ISupport.Build();
137 struct UnloadAction : public HandlerBase0<void>
140 UnloadAction(Module* m) : mod(m) {}
143 ServerInstance->Modules->DoSafeUnload(mod);
144 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 std::string name = mod->ModuleSourceFile;
158 ServerInstance->Modules->DoSafeUnload(mod);
159 ServerInstance->GlobalCulls.Apply();
160 bool rv = ServerInstance->Modules->Load(name.c_str());
162 ServerInstance->GlobalCulls.AddItem(this);
167 bool ModuleManager::Unload(Module* mod)
171 ServerInstance->AtomicActions.AddAction(new UnloadAction(mod));
175 void ModuleManager::Reload(Module* mod, HandlerBase1<void, bool>* callback)
178 ServerInstance->AtomicActions.AddAction(new ReloadAction(mod, callback));
180 callback->Call(false);
183 void ModuleManager::LoadAll()
185 Load("cmd_all", true);
186 Load("cmd_whowas.so", true);
187 Load("cmd_lusers.so", true);
189 ConfigTagList tags = ServerInstance->Config->ConfTags("module");
190 for(ConfigIter i = tags.first; i != tags.second; ++i)
192 ConfigTag* tag = i->second;
193 std::string name = tag->getString("name");
194 std::cout << "[" << con_green << "*" << con_reset << "] Loading module:\t" << con_green << name << con_reset << std::endl;
196 if (!this->Load(name, true))
198 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, this->LastError());
199 std::cout << std::endl << "[" << con_red << "*" << con_reset << "] " << this->LastError() << std::endl << std::endl;
200 ServerInstance->Exit(EXIT_STATUS_MODULE);
204 for(std::map<std::string, Module*>::iterator i = Modules.begin(); i != Modules.end(); i++)
206 Module* mod = i->second;
211 catch (CoreException& modexcept)
213 LastModuleError = "Unable to initialize " + mod->ModuleSourceFile + ": " + modexcept.GetReason();
214 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
215 std::cout << std::endl << "[" << con_red << "*" << con_reset << "] " << LastModuleError << std::endl << std::endl;
216 ServerInstance->Exit(EXIT_STATUS_MODULE);
220 /* We give every module a chance to re-prioritize when we introduce a new one,
221 * not just the one thats loading, as the new module could affect the preference
224 for(int tries = 0; tries < 20; tries++)
226 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
227 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
228 n->second->Prioritize();
230 if (prioritizationState == PRIO_STATE_LAST)
234 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "Hook priority dependency loop detected");
235 ServerInstance->Exit(EXIT_STATUS_MODULE);