]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modulemanager.cpp
Merge extras/m_privdeaf into m_deaf and update documentation.
[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 #ifndef _WIN32
25 #include <dirent.h>
26 #endif
27
28 bool ModuleManager::Load(const std::string& modname, bool defer)
29 {
30         /* Don't allow people to specify paths for modules, it doesn't work as expected */
31         if (modname.find('/') != std::string::npos)
32         {
33                 LastModuleError = "You can't load modules with a path: " + modname;
34                 return false;
35         }
36
37         const std::string filename = ExpandModName(modname);
38         const std::string moduleFile = ServerInstance->Config->Paths.PrependModule(filename);
39
40         if (!FileSystem::FileExists(moduleFile))
41         {
42                 LastModuleError = "Module file could not be found: " + filename;
43                 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
44                 return false;
45         }
46
47         if (Modules.find(filename) != Modules.end())
48         {
49                 LastModuleError = "Module " + filename + " is already loaded, cannot load a module twice!";
50                 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
51                 return false;
52         }
53
54         Module* newmod = NULL;
55         DLLManager* newhandle = new DLLManager(moduleFile.c_str());
56         ServiceList newservices;
57         if (!defer)
58                 this->NewServices = &newservices;
59
60         try
61         {
62                 newmod = newhandle->CallInit();
63                 this->NewServices = NULL;
64
65                 if (newmod)
66                 {
67                         newmod->ModuleSourceFile = filename;
68                         newmod->ModuleDLLManager = newhandle;
69                         Modules[filename] = newmod;
70                         std::string version = newhandle->GetVersion();
71                         if (version.empty())
72                                 version.assign("unknown");
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" << std::flush;
130
131         DIR* library = opendir(ServerInstance->Config->Paths.Module.c_str());
132         if (library)
133         {
134                 dirent* entry = NULL;
135                 while (0 != (entry = readdir(library)))
136                 {
137                         if (InspIRCd::Match(entry->d_name, "core_*.so", ascii_case_insensitive_map))
138                         {
139                                 std::cout << "." << std::flush;
140
141                                 this->NewServices = &servicemap[entry->d_name];
142
143                                 if (!Load(entry->d_name, true))
144                                 {
145                                         ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, this->LastError());
146                                         std::cout << std::endl << "[" << con_red << "*" << con_reset << "] " << this->LastError() << std::endl << std::endl;
147                                         ServerInstance->Exit(EXIT_STATUS_MODULE);
148                                 }
149                         }
150                 }
151                 closedir(library);
152                 std::cout << std::endl;
153         }
154 }