]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_globalload.cpp
m_override Deduplicate code
[user/henk/code/inspircd.git] / src / modules / m_globalload.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2011 Jackmcbarn <jackmcbarn@jackmcbarn.no-ip.org>
5  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
6  *   Copyright (C) 2006-2008 Craig Edwards <craigedwards@brainbox.cc>
7  *   Copyright (C) 2007-2008 Dennis Friis <peavey@inspircd.org>
8  *   Copyright (C) 2006-2007 Robin Burchell <robin+git@viroteck.net>
9  *   Copyright (C) 2006 John Brooks <john.brooks@dereferenced.net>
10  *
11  * This file is part of InspIRCd.  InspIRCd is free software: you can
12  * redistribute it and/or modify it under the terms of the GNU General Public
13  * License as published by the Free Software Foundation, version 2.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  */
23
24
25 #include "inspircd.h"
26
27 /** Handle /GLOADMODULE
28  */
29 class CommandGloadmodule : public Command
30 {
31  public:
32         CommandGloadmodule(Module* Creator) : Command(Creator,"GLOADMODULE", 1)
33         {
34                 flags_needed = 'o';
35                 syntax = "<modulename> [servermask]";
36         }
37
38         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
39         {
40                 std::string servername = parameters.size() > 1 ? parameters[1] : "*";
41
42                 if (InspIRCd::Match(ServerInstance->Config->ServerName.c_str(), servername))
43                 {
44                         if (ServerInstance->Modules->Load(parameters[0].c_str()))
45                         {
46                                 ServerInstance->SNO->WriteToSnoMask('a', "NEW MODULE '%s' GLOBALLY LOADED BY '%s'",parameters[0].c_str(), user->nick.c_str());
47                                 user->WriteNumeric(RPL_LOADEDMODULE, "%s :Module successfully loaded.", parameters[0].c_str());
48                         }
49                         else
50                         {
51                                 user->WriteNumeric(ERR_CANTLOADMODULE, "%s :%s", parameters[0].c_str(), ServerInstance->Modules->LastError().c_str());
52                         }
53                 }
54                 else
55                         ServerInstance->SNO->WriteToSnoMask('a', "MODULE '%s' GLOBAL LOAD BY '%s' (not loaded here)",parameters[0].c_str(), user->nick.c_str());
56
57                 return CMD_SUCCESS;
58         }
59
60         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
61         {
62                 return ROUTE_BROADCAST;
63         }
64 };
65
66 /** Handle /GUNLOADMODULE
67  */
68 class CommandGunloadmodule : public Command
69 {
70  public:
71         CommandGunloadmodule(Module* Creator) : Command(Creator,"GUNLOADMODULE", 1)
72         {
73                 flags_needed = 'o';
74                 syntax = "<modulename> [servermask]";
75         }
76
77         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
78         {
79                 if (!ServerInstance->Config->ConfValue("security")->getBool("allowcoreunload") &&
80                         InspIRCd::Match(parameters[0], "core_*.so", ascii_case_insensitive_map))
81                 {
82                         user->WriteNumeric(ERR_CANTUNLOADMODULE, "%s :You cannot unload core commands!", parameters[0].c_str());
83                         return CMD_FAILURE;
84                 }
85
86                 std::string servername = parameters.size() > 1 ? parameters[1] : "*";
87
88                 if (InspIRCd::Match(ServerInstance->Config->ServerName.c_str(), servername))
89                 {
90                         Module* m = ServerInstance->Modules->Find(parameters[0]);
91                         if (m)
92                         {
93                                 if (ServerInstance->Modules->Unload(m))
94                                 {
95                                         ServerInstance->SNO->WriteToSnoMask('a', "MODULE '%s' GLOBALLY UNLOADED BY '%s'",parameters[0].c_str(), user->nick.c_str());
96                                         user->SendText(":%s 973 %s %s :Module successfully unloaded.",
97                                                 ServerInstance->Config->ServerName.c_str(), user->nick.c_str(), parameters[0].c_str());
98                                 }
99                                 else
100                                 {
101                                         user->WriteNumeric(ERR_CANTUNLOADMODULE, "%s :%s", parameters[0].c_str(), ServerInstance->Modules->LastError().c_str());
102                                 }
103                         }
104                         else
105                                 user->SendText(":%s %03d %s %s :No such module", ServerInstance->Config->ServerName.c_str(), ERR_CANTUNLOADMODULE, user->nick.c_str(), parameters[0].c_str());
106                 }
107                 else
108                         ServerInstance->SNO->WriteToSnoMask('a', "MODULE '%s' GLOBAL UNLOAD BY '%s' (not unloaded here)",parameters[0].c_str(), user->nick.c_str());
109
110                 return CMD_SUCCESS;
111         }
112
113         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
114         {
115                 return ROUTE_BROADCAST;
116         }
117 };
118
119 class GReloadModuleWorker : public HandlerBase1<void, bool>
120 {
121  public:
122         const std::string nick;
123         const std::string name;
124         const std::string uid;
125         GReloadModuleWorker(const std::string& usernick, const std::string& uuid, const std::string& modn)
126                 : nick(usernick), name(modn), uid(uuid) {}
127         void Call(bool result)
128         {
129                 ServerInstance->SNO->WriteToSnoMask('a', "MODULE '%s' GLOBALLY RELOADED BY '%s'%s", name.c_str(), nick.c_str(), result ? "" : " (failed here)");
130                 User* user = ServerInstance->FindNick(uid);
131                 if (user)
132                         user->WriteNumeric(RPL_LOADEDMODULE, "%s :Module %ssuccessfully reloaded.",
133                                 name.c_str(), result ? "" : "un");
134                 ServerInstance->GlobalCulls.AddItem(this);
135         }
136 };
137
138 /** Handle /GRELOADMODULE
139  */
140 class CommandGreloadmodule : public Command
141 {
142  public:
143         CommandGreloadmodule(Module* Creator) : Command(Creator, "GRELOADMODULE", 1)
144         {
145                 flags_needed = 'o'; syntax = "<modulename> [servermask]";
146         }
147
148         CmdResult Handle(const std::vector<std::string> &parameters, User *user)
149         {
150                 std::string servername = parameters.size() > 1 ? parameters[1] : "*";
151
152                 if (InspIRCd::Match(ServerInstance->Config->ServerName.c_str(), servername))
153                 {
154                         Module* m = ServerInstance->Modules->Find(parameters[0]);
155                         if (m)
156                                 ServerInstance->Modules->Reload(m, new GReloadModuleWorker(user->nick, user->uuid, parameters[0]));
157                         else
158                         {
159                                 user->WriteNumeric(RPL_LOADEDMODULE, "%s :Could not find module by that name", parameters[0].c_str());
160                                 return CMD_FAILURE;
161                         }
162                 }
163                 else
164                         ServerInstance->SNO->WriteToSnoMask('a', "MODULE '%s' GLOBAL RELOAD BY '%s' (not reloaded here)",parameters[0].c_str(), user->nick.c_str());
165
166                 return CMD_SUCCESS;
167         }
168
169         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
170         {
171                 return ROUTE_BROADCAST;
172         }
173 };
174
175 class ModuleGlobalLoad : public Module
176 {
177         CommandGloadmodule cmd1;
178         CommandGunloadmodule cmd2;
179         CommandGreloadmodule cmd3;
180
181  public:
182         ModuleGlobalLoad()
183                 : cmd1(this), cmd2(this), cmd3(this)
184         {
185         }
186
187         Version GetVersion() CXX11_OVERRIDE
188         {
189                 return Version("Allows global loading of a module.", VF_COMMON | VF_VENDOR);
190         }
191 };
192
193 MODULE_INIT(ModuleGlobalLoad)