]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_globalload.cpp
Replace hardcoded mode letters passed to IsModeSet() and GetModeParameter() with...
[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 /* $ModDesc: Allows global loading of a module. */
26
27 #include "inspircd.h"
28
29 /** Handle /GLOADMODULE
30  */
31 class CommandGloadmodule : public Command
32 {
33  public:
34         CommandGloadmodule(Module* Creator) : Command(Creator,"GLOADMODULE", 1)
35         {
36                 flags_needed = 'o';
37                 syntax = "<modulename> [servermask]";
38         }
39
40         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
41         {
42                 std::string servername = parameters.size() > 1 ? parameters[1] : "*";
43
44                 if (InspIRCd::Match(ServerInstance->Config->ServerName.c_str(), servername))
45                 {
46                         if (ServerInstance->Modules->Load(parameters[0].c_str()))
47                         {
48                                 ServerInstance->SNO->WriteToSnoMask('a', "NEW MODULE '%s' GLOBALLY LOADED BY '%s'",parameters[0].c_str(), user->nick.c_str());
49                                 user->WriteNumeric(975, "%s %s :Module successfully loaded.",user->nick.c_str(), parameters[0].c_str());
50                         }
51                         else
52                         {
53                                 user->WriteNumeric(974, "%s %s :%s",user->nick.c_str(), parameters[0].c_str(), ServerInstance->Modules->LastError().c_str());
54                         }
55                 }
56                 else
57                         ServerInstance->SNO->WriteToSnoMask('a', "MODULE '%s' GLOBAL LOAD BY '%s' (not loaded here)",parameters[0].c_str(), user->nick.c_str());
58
59                 return CMD_SUCCESS;
60         }
61
62         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
63         {
64                 return ROUTE_BROADCAST;
65         }
66 };
67
68 /** Handle /GUNLOADMODULE
69  */
70 class CommandGunloadmodule : public Command
71 {
72  public:
73         CommandGunloadmodule(Module* Creator) : Command(Creator,"GUNLOADMODULE", 1)
74         {
75                 flags_needed = 'o';
76                 syntax = "<modulename> [servermask]";
77         }
78
79         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
80         {
81                 if (!ServerInstance->Config->ConfValue("security")->getBool("allowcoreunload") &&
82                         InspIRCd::Match(parameters[0], "cmd_*.so", ascii_case_insensitive_map))
83                 {
84                         user->WriteNumeric(972, "%s %s :You cannot unload core commands!", user->nick.c_str(), parameters[0].c_str());
85                         return CMD_FAILURE;
86                 }
87
88                 std::string servername = parameters.size() > 1 ? parameters[1] : "*";
89
90                 if (InspIRCd::Match(ServerInstance->Config->ServerName.c_str(), servername))
91                 {
92                         Module* m = ServerInstance->Modules->Find(parameters[0]);
93                         if (m)
94                         {
95                                 if (ServerInstance->Modules->Unload(m))
96                                 {
97                                         ServerInstance->SNO->WriteToSnoMask('a', "MODULE '%s' GLOBALLY UNLOADED BY '%s'",parameters[0].c_str(), user->nick.c_str());
98                                         user->SendText(":%s 973 %s %s :Module successfully unloaded.",
99                                                 ServerInstance->Config->ServerName.c_str(), user->nick.c_str(), parameters[0].c_str());
100                                 }
101                                 else
102                                 {
103                                         user->WriteNumeric(972, "%s %s :%s",user->nick.c_str(), parameters[0].c_str(), ServerInstance->Modules->LastError().c_str());
104                                 }
105                         }
106                         else
107                                 user->SendText(":%s 972 %s %s :No such module", ServerInstance->Config->ServerName.c_str(), user->nick.c_str(), parameters[0].c_str());
108                 }
109                 else
110                         ServerInstance->SNO->WriteToSnoMask('a', "MODULE '%s' GLOBAL UNLOAD BY '%s' (not unloaded here)",parameters[0].c_str(), user->nick.c_str());
111
112                 return CMD_SUCCESS;
113         }
114
115         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
116         {
117                 return ROUTE_BROADCAST;
118         }
119 };
120
121 class GReloadModuleWorker : public HandlerBase1<void, bool>
122 {
123  public:
124         const std::string nick;
125         const std::string name;
126         const std::string uid;
127         GReloadModuleWorker(const std::string& usernick, const std::string& uuid, const std::string& modn)
128                 : nick(usernick), name(modn), uid(uuid) {}
129         void Call(bool result)
130         {
131                 ServerInstance->SNO->WriteToSnoMask('a', "MODULE '%s' GLOBALLY RELOADED BY '%s'%s", name.c_str(), nick.c_str(), result ? "" : " (failed here)");
132                 User* user = ServerInstance->FindNick(uid);
133                 if (user)
134                         user->WriteNumeric(975, "%s %s :Module %ssuccessfully reloaded.",
135                                 user->nick.c_str(), name.c_str(), result ? "" : "un");
136                 ServerInstance->GlobalCulls.AddItem(this);
137         }
138 };
139
140 /** Handle /GRELOADMODULE
141  */
142 class CommandGreloadmodule : public Command
143 {
144  public:
145         CommandGreloadmodule(Module* Creator) : Command(Creator, "GRELOADMODULE", 1)
146         {
147                 flags_needed = 'o'; syntax = "<modulename> [servermask]";
148         }
149
150         CmdResult Handle(const std::vector<std::string> &parameters, User *user)
151         {
152                 std::string servername = parameters.size() > 1 ? parameters[1] : "*";
153
154                 if (InspIRCd::Match(ServerInstance->Config->ServerName.c_str(), servername))
155                 {
156                         Module* m = ServerInstance->Modules->Find(parameters[0]);
157                         if (m)
158                                 ServerInstance->Modules->Reload(m, new GReloadModuleWorker(user->nick, user->uuid, parameters[0]));
159                         else
160                         {
161                                 user->WriteNumeric(975, "%s %s :Could not find module by that name", user->nick.c_str(), parameters[0].c_str());
162                                 return CMD_FAILURE;
163                         }
164                 }
165                 else
166                         ServerInstance->SNO->WriteToSnoMask('a', "MODULE '%s' GLOBAL RELOAD BY '%s' (not reloaded here)",parameters[0].c_str(), user->nick.c_str());
167
168                 return CMD_SUCCESS;
169         }
170
171         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
172         {
173                 return ROUTE_BROADCAST;
174         }
175 };
176
177 class ModuleGlobalLoad : public Module
178 {
179         CommandGloadmodule cmd1;
180         CommandGunloadmodule cmd2;
181         CommandGreloadmodule cmd3;
182
183  public:
184         ModuleGlobalLoad()
185                 : cmd1(this), cmd2(this), cmd3(this)
186         {
187         }
188
189         void init() CXX11_OVERRIDE
190         {
191                 ServerInstance->Modules->AddService(cmd1);
192                 ServerInstance->Modules->AddService(cmd2);
193                 ServerInstance->Modules->AddService(cmd3);
194         }
195
196         Version GetVersion() CXX11_OVERRIDE
197         {
198                 return Version("Allows global loading of a module.", VF_COMMON | VF_VENDOR);
199         }
200 };
201
202 MODULE_INIT(ModuleGlobalLoad)