1 #define MODNAME cmd_all
8 typedef std::map<std::string, AllModuleList*> modmap;
9 static std::vector<AllCommandList::fn>* cmdlist = NULL;
10 static modmap* modlist = NULL;
12 AllCommandList::AllCommandList(fn cmd)
15 cmdlist = new std::vector<AllCommandList::fn>();
16 cmdlist->push_back(cmd);
19 AllModuleList::AllModuleList(AllModuleList::fn mod, const std::string& Name) : init(mod), name(Name)
22 modlist = new modmap();
23 modlist->insert(std::make_pair(Name, this));
26 class AllModule : public Module
28 std::vector<Command*> cmds;
36 cmds.reserve(cmdlist->size());
37 for(std::vector<AllCommandList::fn>::iterator i = cmdlist->begin(); i != cmdlist->end(); ++i)
39 Command* c = (*i)(this);
41 ServerInstance->AddCommand(c);
46 this->AllModule::~AllModule();
53 for(std::vector<Command*>::iterator i = cmds.begin(); i != cmds.end(); ++i)
59 return Version("All commands", VF_VENDOR|VF_CORE);
63 MODULE_INIT(AllModule)
65 bool ModuleManager::Load(const std::string& name, bool defer)
67 modmap::iterator it = modlist->find(name);
68 if (it == modlist->end())
73 mod = (*it->second->init)();
74 mod->ModuleSourceFile = name;
75 mod->ModuleDLLManager = NULL;
79 ServerInstance->Logs->Log("MODULE", DEFAULT,"New module introduced: %s", name.c_str());
87 catch (CoreException& modexcept)
91 ServerInstance->Logs->Log("MODULE", DEFAULT, "Unable to load " + name + ": " + modexcept.GetReason());
94 FOREACH_MOD(I_OnLoadModule,OnLoadModule(mod));
95 /* We give every module a chance to re-prioritize when we introduce a new one,
96 * not just the one thats loading, as the new module could affect the preference
99 for(int tries = 0; tries < 20; tries++)
101 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
102 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
103 n->second->Prioritize();
105 if (prioritizationState == PRIO_STATE_LAST)
108 ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected while loading " + name);
111 ServerInstance->BuildISupport();
116 struct UnloadAction : public HandlerBase0<void>
119 UnloadAction(Module* m) : mod(m) {}
122 ServerInstance->Modules->DoSafeUnload(mod);
123 ServerInstance->GlobalCulls.Apply();
124 ServerInstance->GlobalCulls.AddItem(this);
128 struct ReloadAction : public HandlerBase0<void>
131 HandlerBase1<void, bool>* const callback;
132 ReloadAction(Module* m, HandlerBase1<void, bool>* c)
133 : mod(m), callback(c) {}
136 std::string name = mod->ModuleSourceFile;
137 ServerInstance->Modules->DoSafeUnload(mod);
138 ServerInstance->GlobalCulls.Apply();
139 bool rv = ServerInstance->Modules->Load(name.c_str());
141 ServerInstance->GlobalCulls.AddItem(this);
146 bool ModuleManager::Unload(Module* mod)
150 ServerInstance->AtomicActions.AddAction(new UnloadAction(mod));
154 void ModuleManager::Reload(Module* mod, HandlerBase1<void, bool>* callback)
157 ServerInstance->AtomicActions.AddAction(new ReloadAction(mod, callback));
159 callback->Call(false);
162 void ModuleManager::LoadAll()
164 Load("cmd_all", true);
165 Load("cmd_whowas.so", true);
167 ConfigTagList tags = ServerInstance->Config->ConfTags("module");
168 for(ConfigIter i = tags.first; i != tags.second; ++i)
170 ConfigTag* tag = i->second;
171 std::string name = tag->getString("name");
172 printf_c("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",name.c_str());
174 if (!this->Load(name, true))
176 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
177 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
178 ServerInstance->Exit(EXIT_STATUS_MODULE);
182 for(std::map<std::string, Module*>::iterator i = Modules.begin(); i != Modules.end(); i++)
184 Module* mod = i->second;
189 catch (CoreException& modexcept)
191 LastModuleError = "Unable to initialize " + mod->ModuleSourceFile + ": " + modexcept.GetReason();
192 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
193 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", LastModuleError.c_str());
194 ServerInstance->Exit(EXIT_STATUS_MODULE);
198 /* We give every module a chance to re-prioritize when we introduce a new one,
199 * not just the one thats loading, as the new module could affect the preference
202 for(int tries = 0; tries < 20; tries++)
204 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
205 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
206 n->second->Prioritize();
208 if (prioritizationState == PRIO_STATE_LAST)
212 ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected");
213 ServerInstance->Exit(EXIT_STATUS_MODULE);