]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modmanager_static.cpp
Move already sent id rollover handling and static LocalUser::already_sent_id into...
[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& inputname, bool defer)
84 {
85         const std::string name = ExpandModName(inputname);
86         modmap::iterator it = modlist->find(name);
87         if (it == modlist->end())
88                 return false;
89         Module* mod = NULL;
90
91         ServiceList newservices;
92         if (!defer)
93                 this->NewServices = &newservices;
94
95         try
96         {
97                 mod = (*it->second->init)();
98                 mod->ModuleSourceFile = name;
99                 mod->ModuleDLLManager = NULL;
100                 mod->dying = false;
101                 Modules[name] = mod;
102                 this->NewServices = NULL;
103                 if (defer)
104                 {
105                         ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "New module introduced: %s", name.c_str());
106                         return true;
107                 }
108                 else
109                 {
110                         ConfigStatus confstatus;
111
112                         AttachAll(mod);
113                         AddServices(newservices);
114                         mod->init();
115                         mod->ReadConfig(confstatus);
116                 }
117         }
118         catch (CoreException& modexcept)
119         {
120                 this->NewServices = NULL;
121
122                 if (mod)
123                         DoSafeUnload(mod);
124                 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "Unable to load " + name + ": " + modexcept.GetReason());
125                 return false;
126         }
127
128         FOREACH_MOD(OnLoadModule, (mod));
129         PrioritizeHooks();
130         ServerInstance->ISupport.Build();
131         return true;
132 }
133
134 void ModuleManager::LoadCoreModules(std::map<std::string, ServiceList>& servicemap)
135 {
136         for (modmap::const_iterator i = modlist->begin(); i != modlist->end(); ++i)
137         {
138                 const std::string modname = i->first;
139                 if (modname[0] == 'c')
140                 {
141                         this->NewServices = &servicemap[modname];
142                         Load(modname, true);
143                 }
144         }
145         this->NewServices = NULL;
146 }
147
148 #endif