2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
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.
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
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/>.
21 #include "exitcodes.h"
28 #ifndef INSPIRCD_STATIC
30 bool ModuleManager::Load(const std::string& modname, bool defer)
32 /* Don't allow people to specify paths for modules, it doesn't work as expected */
33 if (modname.find('/') != std::string::npos)
35 LastModuleError = "You can't load modules with a path: " + modname;
39 const std::string filename = ExpandModName(modname);
40 const std::string moduleFile = ServerInstance->Config->Paths.PrependModule(filename);
42 if (!FileSystem::FileExists(moduleFile))
44 LastModuleError = "Module file could not be found: " + filename;
45 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
49 if (Modules.find(filename) != Modules.end())
51 LastModuleError = "Module " + filename + " is already loaded, cannot load a module twice!";
52 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
56 Module* newmod = NULL;
57 DLLManager* newhandle = new DLLManager(moduleFile.c_str());
58 ServiceList newservices;
60 this->NewServices = &newservices;
64 newmod = newhandle->CallInit();
65 this->NewServices = NULL;
69 newmod->ModuleSourceFile = filename;
70 newmod->ModuleDLLManager = newhandle;
71 newmod->dying = false;
72 Modules[filename] = newmod;
73 std::string version = newhandle->GetVersion();
75 version.assign("unknown");
78 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "New module introduced: %s (Module version %s)",
79 filename.c_str(), version.c_str());
83 ConfigStatus confstatus;
86 AddServices(newservices);
88 newmod->ReadConfig(confstatus);
90 Version v = newmod->GetVersion();
91 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "New module introduced: %s (Module version %s)%s",
92 filename.c_str(), version.c_str(), (!(v.Flags & VF_VENDOR) ? " [3rd Party]" : " [Vendor]"));
97 LastModuleError = "Unable to load " + filename + ": " + newhandle->LastError();
98 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
103 catch (CoreException& modexcept)
105 this->NewServices = NULL;
107 // failure in module constructor
110 DoSafeUnload(newmod);
111 ServerInstance->GlobalCulls.AddItem(newhandle);
115 LastModuleError = "Unable to load " + filename + ": " + modexcept.GetReason();
116 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
123 FOREACH_MOD(OnLoadModule, (newmod));
125 ServerInstance->ISupport.Build();
129 /* We must load the modules AFTER initializing the socket engine, now */
130 void ModuleManager::LoadCoreModules(std::map<std::string, ServiceList>& servicemap)
132 std::cout << std::endl << "Loading core commands";
135 DIR* library = opendir(ServerInstance->Config->Paths.Module.c_str());
138 dirent* entry = NULL;
139 while (0 != (entry = readdir(library)))
141 if (InspIRCd::Match(entry->d_name, "core_*.so", ascii_case_insensitive_map))
146 this->NewServices = &servicemap[entry->d_name];
148 if (!Load(entry->d_name, true))
150 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, this->LastError());
151 std::cout << std::endl << "[" << con_red << "*" << con_reset << "] " << this->LastError() << std::endl << std::endl;
152 ServerInstance->Exit(EXIT_STATUS_MODULE);
157 std::cout << std::endl;