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