]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modulemanager.cpp
8cb7a040175bdef246bcfe57a94e693e0de44ab1
[user/henk/code/inspircd.git] / src / modulemanager.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 #include "inspircd.h"
21 #include "exitcodes.h"
22 #include <iostream>
23
24 bool ModuleManager::Load(const std::string& modname, bool defer)
25 {
26         /* Don't allow people to specify paths for modules, it doesn't work as expected */
27         if (modname.find('/') != std::string::npos)
28         {
29                 LastModuleError = "You can't load modules with a path: " + modname;
30                 return false;
31         }
32
33         const std::string filename = ExpandModName(modname);
34         const std::string moduleFile = ServerInstance->Config->Paths.PrependModule(filename);
35
36         if (!FileSystem::FileExists(moduleFile))
37         {
38                 LastModuleError = "Module file could not be found: " + filename;
39                 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
40                 return false;
41         }
42
43         if (Modules.find(filename) != Modules.end())
44         {
45                 LastModuleError = "Module " + filename + " is already loaded, cannot load a module twice!";
46                 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
47                 return false;
48         }
49
50         Module* newmod = NULL;
51         DLLManager* newhandle = new DLLManager(moduleFile.c_str());
52         ServiceList newservices;
53         if (!defer)
54                 this->NewServices = &newservices;
55
56         try
57         {
58                 newmod = newhandle->CallInit();
59                 this->NewServices = NULL;
60
61                 if (newmod)
62                 {
63                         newmod->ModuleSourceFile = filename;
64                         newmod->ModuleDLLManager = newhandle;
65                         Modules[filename] = newmod;
66                         std::string version = newhandle->GetVersion();
67                         if (version.empty())
68                                 version.assign("unknown");
69                         if (defer)
70                         {
71                                 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "New module introduced: %s (Module version %s)",
72                                         filename.c_str(), version.c_str());
73                         }
74                         else
75                         {
76                                 ConfigStatus confstatus;
77
78                                 AttachAll(newmod);
79                                 AddServices(newservices);
80                                 newmod->init();
81                                 newmod->ReadConfig(confstatus);
82
83                                 Version v = newmod->GetVersion();
84                                 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "New module introduced: %s (Module version %s)%s",
85                                         filename.c_str(), version.c_str(), (!(v.Flags & VF_VENDOR) ? " [3rd Party]" : " [Vendor]"));
86                         }
87                 }
88                 else
89                 {
90                         LastModuleError = "Unable to load " + filename + ": " + newhandle->LastError();
91                         ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
92                         delete newhandle;
93                         return false;
94                 }
95         }
96         catch (CoreException& modexcept)
97         {
98                 this->NewServices = NULL;
99
100                 // failure in module constructor
101                 if (newmod)
102                 {
103                         DoSafeUnload(newmod);
104                         ServerInstance->GlobalCulls.AddItem(newhandle);
105                 }
106                 else
107                         delete newhandle;
108                 LastModuleError = "Unable to load " + filename + ": " + modexcept.GetReason();
109                 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
110                 return false;
111         }
112
113         if (defer)
114                 return true;
115
116         FOREACH_MOD(OnLoadModule, (newmod));
117         PrioritizeHooks();
118         ServerInstance->ISupport.Build();
119         return true;
120 }
121
122 /* We must load the modules AFTER initializing the socket engine, now */
123 void ModuleManager::LoadCoreModules(std::map<std::string, ServiceList>& servicemap)
124 {
125         std::cout << "Loading core modules " << std::flush;
126
127         std::vector<std::string> files;
128         if (!FileSystem::GetFileList(ServerInstance->Config->Paths.Module, files, "core_*.so"))
129         {
130                 std::cout << "failed!" << std::endl;
131                 ServerInstance->Exit(EXIT_STATUS_MODULE);
132         }
133
134         for (std::vector<std::string>::const_iterator iter = files.begin(); iter != files.end(); ++iter)
135         {
136                 std::cout << "." << std::flush;
137
138                 const std::string& name = *iter;
139                 this->NewServices = &servicemap[name];
140
141                 if (!Load(name, true))
142                 {
143                         ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, this->LastError());
144                         std::cout << std::endl << "[" << con_red << "*" << con_reset << "] " << this->LastError() << std::endl << std::endl;
145                         ServerInstance->Exit(EXIT_STATUS_MODULE);
146                 }
147         }
148
149         std::cout << std::endl;
150 }