1 #define MODNAME AllModule
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("AllModule", true);
166 ConfigTagList tags = ServerInstance->Config->ConfTags("module");
167 for(ConfigIter i = tags.first; i != tags.second; ++i)
169 ConfigTag* tag = i->second;
170 std::string name = tag->getString("name");
171 printf_c("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",name.c_str());
173 if (!this->Load(name, true))
175 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
176 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
177 ServerInstance->Exit(EXIT_STATUS_MODULE);
181 for(std::map<std::string, Module*>::iterator i = Modules.begin(); i != Modules.end(); i++)
183 Module* mod = i->second;
188 catch (CoreException& modexcept)
190 LastModuleError = "Unable to initialize " + mod->ModuleSourceFile + ": " + modexcept.GetReason();
191 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
192 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", LastModuleError.c_str());
193 ServerInstance->Exit(EXIT_STATUS_MODULE);
197 /* We give every module a chance to re-prioritize when we introduce a new one,
198 * not just the one thats loading, as the new module could affect the preference
201 for(int tries = 0; tries < 20; tries++)
203 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
204 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
205 n->second->Prioritize();
207 if (prioritizationState == PRIO_STATE_LAST)
211 ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected");
212 ServerInstance->Exit(EXIT_STATUS_MODULE);
217 void ModuleManager::UnloadAll()