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