]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modmanager_static.cpp
7d6c89258852a6493fabda7965e64efc4024872b
[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
120         FOREACH_MOD(OnLoadModule, (mod));
121         PrioritizeHooks();
122         ServerInstance->ISupport.Build();
123         return true;
124 }
125
126 void ModuleManager::LoadCoreModules()
127 {
128         Load("cmd_all.so", true);
129         Load("cmd_whowas.so", true);
130         Load("cmd_lusers.so", true);
131         Load("cmd_privmsg.so", true);
132 }
133
134 #endif