]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_globalload.cpp
Merge commit 'refs/merge-requests/5' of git://gitorious.org/inspircd/inspircd into...
[user/henk/code/inspircd.git] / src / modules / m_globalload.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $ModDesc: Allows global loading of a module. */
15
16 #include "inspircd.h"
17
18 /** Handle /GLOADMODULE
19  */
20 class CommandGloadmodule : public Command
21 {
22  public:
23         CommandGloadmodule(Module* Creator) : Command(Creator,"GLOADMODULE", 1)
24         {
25                 flags_needed = 'o';
26                 syntax = "<modulename> [servermask]";
27                 TRANSLATE3(TR_TEXT, TR_TEXT, TR_END);
28         }
29
30         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
31         {
32                 std::string servername = parameters.size() > 1 ? parameters[1] : "*";
33
34                 if (InspIRCd::Match(ServerInstance->Config->ServerName.c_str(), servername))
35                 {
36                         if (ServerInstance->Modules->Load(parameters[0].c_str()))
37                         {
38                                 ServerInstance->SNO->WriteToSnoMask('a', "NEW MODULE '%s' GLOBALLY LOADED BY '%s'",parameters[0].c_str(), user->nick.c_str());
39                                 user->WriteNumeric(975, "%s %s :Module successfully loaded.",user->nick.c_str(), parameters[0].c_str());
40                         }
41                         else
42                         {
43                                 user->WriteNumeric(974, "%s %s :%s",user->nick.c_str(), parameters[0].c_str(), ServerInstance->Modules->LastError().c_str());
44                         }
45                 }
46                 else
47                         ServerInstance->SNO->WriteToSnoMask('a', "MODULE '%s' GLOBAL LOAD BY '%s' (not loaded here)",parameters[0].c_str(), user->nick.c_str());
48
49                 return CMD_SUCCESS;
50         }
51
52         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
53         {
54                 return ROUTE_BROADCAST;
55         }
56 };
57
58 /** Handle /GUNLOADMODULE
59  */
60 class CommandGunloadmodule : public Command
61 {
62  public:
63         CommandGunloadmodule(Module* Creator) : Command(Creator,"GUNLOADMODULE", 1)
64         {
65                 flags_needed = 'o';
66                 syntax = "<modulename> [servermask]";
67         }
68
69         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
70         {
71                 std::string servername = parameters.size() > 1 ? parameters[1] : "*";
72
73                 if (InspIRCd::Match(ServerInstance->Config->ServerName.c_str(), servername))
74                 {
75                         Module* m = ServerInstance->Modules->Find(parameters[0]);
76                         if (m && ServerInstance->Modules->Unload(m))
77                         {
78                                 ServerInstance->SNO->WriteToSnoMask('a', "MODULE '%s' GLOBALLY UNLOADED BY '%s'",parameters[0].c_str(), user->nick.c_str());
79                                 user->SendText(":%s 973 %s %s :Module successfully unloaded.",
80                                         ServerInstance->Config->ServerName.c_str(), user->nick.c_str(), parameters[0].c_str());
81                         }
82                         else
83                         {
84                                 user->WriteNumeric(972, "%s %s :%s",user->nick.c_str(), parameters[0].c_str(), ServerInstance->Modules->LastError().c_str());
85                         }
86                 }
87                 else
88                         ServerInstance->SNO->WriteToSnoMask('a', "MODULE '%s' GLOBAL UNLOAD BY '%s' (not unloaded here)",parameters[0].c_str(), user->nick.c_str());
89
90                 return CMD_SUCCESS;
91         }
92
93         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
94         {
95                 return ROUTE_BROADCAST;
96         }
97 };
98
99 class GReloadModuleWorker : public HandlerBase1<void, bool>
100 {
101  public:
102         const std::string nick;
103         const std::string name;
104         const std::string uid;
105         GReloadModuleWorker(const std::string& usernick, const std::string& uuid, const std::string& modn)
106                 : nick(usernick), name(modn), uid(uuid) {}
107         void Call(bool result)
108         {
109                 ServerInstance->SNO->WriteToSnoMask('a', "MODULE '%s' GLOBALLY RELOADED BY '%s'%s", name.c_str(), nick.c_str(), result ? "" : " (failed here)");
110                 User* user = ServerInstance->FindNick(uid);
111                 if (user)
112                         user->WriteNumeric(975, "%s %s :Module %ssuccessfully reloaded.",
113                                 user->nick.c_str(), name.c_str(), result ? "" : "un");
114                 ServerInstance->GlobalCulls.AddItem(this);
115         }
116 };
117
118 /** Handle /GRELOADMODULE
119  */
120 class CommandGreloadmodule : public Command
121 {
122  public:
123         CommandGreloadmodule(Module* Creator) : Command(Creator, "GRELOADMODULE", 1)
124         {
125                 flags_needed = 'o'; syntax = "<modulename> [servermask]";
126         }
127
128         CmdResult Handle(const std::vector<std::string> &parameters, User *user)
129         {
130                 std::string servername = parameters.size() > 1 ? parameters[1] : "*";
131
132                 if (InspIRCd::Match(ServerInstance->Config->ServerName.c_str(), servername))
133                 {
134                         Module* m = ServerInstance->Modules->Find(parameters[0]);
135                         if (m)
136                                 ServerInstance->Modules->Reload(m, new GReloadModuleWorker(user->nick, user->uuid, parameters[0]));
137                         else
138                         {
139                                 user->WriteNumeric(975, "%s %s :Could not find module by that name", user->nick.c_str(), parameters[0].c_str());
140                                 return CMD_FAILURE;
141                         }
142                 }
143                 else
144                         ServerInstance->SNO->WriteToSnoMask('a', "MODULE '%s' GLOBAL RELOAD BY '%s' (not reloaded here)",parameters[0].c_str(), user->nick.c_str());
145
146                 return CMD_SUCCESS;
147         }
148
149         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
150         {
151                 return ROUTE_BROADCAST;
152         }
153 };
154
155 class ModuleGlobalLoad : public Module
156 {
157         CommandGloadmodule cmd1;
158         CommandGunloadmodule cmd2;
159         CommandGreloadmodule cmd3;
160
161  public:
162         ModuleGlobalLoad()
163                 : cmd1(this), cmd2(this), cmd3(this)
164         {
165                 ServerInstance->AddCommand(&cmd1);
166                 ServerInstance->AddCommand(&cmd2);
167                 ServerInstance->AddCommand(&cmd3);
168         }
169
170         ~ModuleGlobalLoad()
171         {
172         }
173
174         Version GetVersion()
175         {
176                 return Version("Allows global loading of a module.", VF_COMMON | VF_VENDOR);
177         }
178 };
179
180 MODULE_INIT(ModuleGlobalLoad)
181