]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modmanager_static.cpp
Merge insp20
[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                         }
62                 }
63                 catch (...)
64                 {
65                         this->AllModule::~AllModule();
66                         throw;
67                 }
68         }
69
70         ~AllModule()
71         {
72                 stdalgo::delete_all(cmds);
73         }
74
75         Version GetVersion()
76         {
77                 return Version("All commands", VF_VENDOR|VF_CORE);
78         }
79 };
80
81 MODULE_INIT(AllModule)
82
83 bool ModuleManager::Load(const std::string& name, bool defer)
84 {
85         modmap::iterator it = modlist->find(name);
86         if (it == modlist->end())
87                 return false;
88         Module* mod = NULL;
89
90         ServiceList newservices;
91         if (!defer)
92                 this->NewServices = &newservices;
93
94         try
95         {
96                 mod = (*it->second->init)();
97                 mod->ModuleSourceFile = name;
98                 mod->ModuleDLLManager = NULL;
99                 mod->dying = false;
100                 Modules[name] = mod;
101                 this->NewServices = NULL;
102                 if (defer)
103                 {
104                         ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "New module introduced: %s", name.c_str());
105                         return true;
106                 }
107                 else
108                 {
109                         ConfigStatus confstatus;
110
111                         AttachAll(mod);
112                         AddServices(newservices);
113                         mod->init();
114                         mod->ReadConfig(confstatus);
115                 }
116         }
117         catch (CoreException& modexcept)
118         {
119                 this->NewServices = NULL;
120
121                 if (mod)
122                         DoSafeUnload(mod);
123                 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "Unable to load " + name + ": " + modexcept.GetReason());
124                 return false;
125         }
126
127         FOREACH_MOD(OnLoadModule, (mod));
128         PrioritizeHooks();
129         ServerInstance->ISupport.Build();
130         return true;
131 }
132
133 void ModuleManager::LoadCoreModules(std::map<std::string, ServiceList>& servicemap)
134 {
135         for (modmap::const_iterator i = modlist->begin(); i != modlist->end(); ++i)
136         {
137                 const std::string modname = i->first;
138                 if (modname[0] == 'c')
139                 {
140                         this->NewServices = &servicemap[modname];
141                         Load(modname, true);
142                 }
143         }
144         this->NewServices = NULL;
145 }
146
147 #endif