]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modmanager_static.cpp
Replace OnRehash() with ReadConfig() that is called on boot, on module load and on...
[user/henk/code/inspircd.git] / src / modmanager_static.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *
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.
9  *
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
13  * details.
14  *
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/>.
17  */
18
19
20 #define MODNAME cmd_all
21
22 #include "inspircd.h"
23 #include "exitcodes.h"
24 #include <iostream>
25
26 #ifdef PURE_STATIC
27
28 typedef std::map<std::string, AllModuleList*> modmap;
29 static std::vector<AllCommandList::fn>* cmdlist = NULL;
30 static modmap* modlist = NULL;
31
32 AllCommandList::AllCommandList(fn cmd)
33 {
34         if (!cmdlist)
35                 cmdlist = new std::vector<AllCommandList::fn>();
36         cmdlist->push_back(cmd);
37 }
38
39 AllModuleList::AllModuleList(AllModuleList::fn mod, const std::string& Name) : init(mod), name(Name)
40 {
41         if (!modlist)
42                 modlist = new modmap();
43         modlist->insert(std::make_pair(Name, this));
44 }
45
46 class AllModule : public Module
47 {
48         std::vector<Command*> cmds;
49  public:
50         AllModule()
51         {
52                 if (!cmdlist)
53                         return;
54                 try
55                 {
56                         cmds.reserve(cmdlist->size());
57                         for(std::vector<AllCommandList::fn>::iterator i = cmdlist->begin(); i != cmdlist->end(); ++i)
58                         {
59                                 Command* c = (*i)(this);
60                                 cmds.push_back(c);
61                                 ServerInstance->Modules->AddService(*c);
62                         }
63                 }
64                 catch (...)
65                 {
66                         this->AllModule::~AllModule();
67                         throw;
68                 }
69         }
70
71         ~AllModule()
72         {
73                 for(std::vector<Command*>::iterator i = cmds.begin(); i != cmds.end(); ++i)
74                         delete *i;
75         }
76
77         Version GetVersion()
78         {
79                 return Version("All commands", VF_VENDOR|VF_CORE);
80         }
81 };
82
83 MODULE_INIT(AllModule)
84
85 bool ModuleManager::Load(const std::string& name, bool defer)
86 {
87         modmap::iterator it = modlist->find(name);
88         if (it == modlist->end())
89                 return false;
90         Module* mod = NULL;
91         try
92         {
93                 mod = (*it->second->init)();
94                 mod->ModuleSourceFile = name;
95                 mod->ModuleDLLManager = NULL;
96                 mod->dying = false;
97                 Modules[name] = mod;
98                 if (defer)
99                 {
100                         ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "New module introduced: %s", name.c_str());
101                         return true;
102                 }
103                 else
104                 {
105                         ConfigStatus confstatus;
106
107                         AttachAll(mod);
108                         mod->init();
109                         mod->ReadConfig(confstatus);
110                 }
111         }
112         catch (CoreException& modexcept)
113         {
114                 if (mod)
115                         DoSafeUnload(mod);
116                 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "Unable to load " + name + ": " + modexcept.GetReason());
117                 return false;
118         }
119         FOREACH_MOD(OnLoadModule, (mod));
120         /* We give every module a chance to re-prioritize when we introduce a new one,
121          * not just the one thats loading, as the new module could affect the preference
122          * of others
123          */
124         for(int tries = 0; tries < 20; tries++)
125         {
126                 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
127                 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
128                         n->second->Prioritize();
129
130                 if (prioritizationState == PRIO_STATE_LAST)
131                         break;
132                 if (tries == 19)
133                         ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "Hook priority dependency loop detected while loading " + name);
134         }
135
136         ServerInstance->ISupport.Build();
137         return true;
138 }
139
140 namespace {
141         struct UnloadAction : public HandlerBase0<void>
142         {
143                 Module* const mod;
144                 UnloadAction(Module* m) : mod(m) {}
145                 void Call()
146                 {
147                         ServerInstance->Modules->DoSafeUnload(mod);
148                         ServerInstance->GlobalCulls.Apply();
149                         ServerInstance->GlobalCulls.AddItem(this);
150                 }
151         };
152
153         struct ReloadAction : public HandlerBase0<void>
154         {
155                 Module* const mod;
156                 HandlerBase1<void, bool>* const callback;
157                 ReloadAction(Module* m, HandlerBase1<void, bool>* c)
158                         : mod(m), callback(c) {}
159                 void Call()
160                 {
161                         std::string name = mod->ModuleSourceFile;
162                         ServerInstance->Modules->DoSafeUnload(mod);
163                         ServerInstance->GlobalCulls.Apply();
164                         bool rv = ServerInstance->Modules->Load(name.c_str());
165                         callback->Call(rv);
166                         ServerInstance->GlobalCulls.AddItem(this);
167                 }
168         };
169 }
170
171 bool ModuleManager::Unload(Module* mod)
172 {
173         if (!CanUnload(mod))
174                 return false;
175         ServerInstance->AtomicActions.AddAction(new UnloadAction(mod));
176         return true;
177 }
178
179 void ModuleManager::Reload(Module* mod, HandlerBase1<void, bool>* callback)
180 {
181         if (CanUnload(mod))
182                 ServerInstance->AtomicActions.AddAction(new ReloadAction(mod, callback));
183         else
184                 callback->Call(false);
185 }
186
187 void ModuleManager::LoadAll()
188 {
189         Load("cmd_all", true);
190         Load("cmd_whowas.so", true);
191         Load("cmd_lusers.so", true);
192         Load("cmd_privmsg.so", true);
193
194         ConfigTagList tags = ServerInstance->Config->ConfTags("module");
195         for(ConfigIter i = tags.first; i != tags.second; ++i)
196         {
197                 ConfigTag* tag = i->second;
198                 std::string name = tag->getString("name");
199                 std::cout << "[" << con_green << "*" << con_reset << "] Loading module:\t" << con_green << name << con_reset << std::endl;
200
201                 if (!this->Load(name, true))
202                 {
203                         ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, this->LastError());
204                         std::cout << std::endl << "[" << con_red << "*" << con_reset << "] " << this->LastError() << std::endl << std::endl;
205                         ServerInstance->Exit(EXIT_STATUS_MODULE);
206                 }
207         }
208
209         ConfigStatus confstatus;
210
211         for(std::map<std::string, Module*>::iterator i = Modules.begin(); i != Modules.end(); i++)
212         {
213                 Module* mod = i->second;
214                 try
215                 {
216                         AttachAll(mod);
217                         mod->init();
218                         mod->ReadConfig(confstatus);
219                 }
220                 catch (CoreException& modexcept)
221                 {
222                         LastModuleError = "Unable to initialize " + mod->ModuleSourceFile + ": " + modexcept.GetReason();
223                         ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
224                         std::cout << std::endl << "[" << con_red << "*" << con_reset << "] " << LastModuleError << std::endl << std::endl;
225                         ServerInstance->Exit(EXIT_STATUS_MODULE);
226                 }
227         }
228
229         /* We give every module a chance to re-prioritize when we introduce a new one,
230          * not just the one thats loading, as the new module could affect the preference
231          * of others
232          */
233         for(int tries = 0; tries < 20; tries++)
234         {
235                 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
236                 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
237                         n->second->Prioritize();
238
239                 if (prioritizationState == PRIO_STATE_LAST)
240                         break;
241                 if (tries == 19)
242                 {
243                         ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "Hook priority dependency loop detected");
244                         ServerInstance->Exit(EXIT_STATUS_MODULE);
245                 }
246         }
247 }
248
249 #endif