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