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