]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_svshold.cpp
Textual improvements and fixes such as typos, casing, etc. (#1612)
[user/henk/code/inspircd.git] / src / modules / m_svshold.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2006-2008 Robin Burchell <robin+git@viroteck.net>
5  *   Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
6  *   Copyright (C) 2006-2008 Dennis Friis <peavey@inspircd.org>
7  *   Copyright (C) 2006 Craig Edwards <craigedwards@brainbox.cc>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "inspircd.h"
24 #include "xline.h"
25 #include "modules/stats.h"
26
27 namespace
28 {
29         bool silent;
30 }
31
32 /** Holds a SVSHold item
33  */
34 class SVSHold : public XLine
35 {
36 public:
37         std::string nickname;
38
39         SVSHold(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& nick)
40                 : XLine(s_time, d, src, re, "SVSHOLD")
41         {
42                 this->nickname = nick;
43         }
44
45         bool Matches(User* u) CXX11_OVERRIDE
46         {
47                 if (u->nick == nickname)
48                         return true;
49                 return false;
50         }
51
52         bool Matches(const std::string& s) CXX11_OVERRIDE
53         {
54                 return InspIRCd::Match(s, nickname);
55         }
56
57         void DisplayExpiry() CXX11_OVERRIDE
58         {
59                 if (!silent)
60                 {
61                         ServerInstance->SNO->WriteToSnoMask('x', "Removing expired SVSHOLD %s (set by %s %s ago): %s",
62                                 nickname.c_str(), source.c_str(), InspIRCd::DurationString(ServerInstance->Time() - set_time).c_str(), reason.c_str());
63                 }
64         }
65
66         const std::string& Displayable() CXX11_OVERRIDE
67         {
68                 return nickname;
69         }
70 };
71
72 /** An XLineFactory specialized to generate SVSHOLD pointers
73  */
74 class SVSHoldFactory : public XLineFactory
75 {
76  public:
77         SVSHoldFactory() : XLineFactory("SVSHOLD") { }
78
79         /** Generate an SVSHOLD
80         */
81         XLine* Generate(time_t set_time, unsigned long duration, const std::string& source, const std::string& reason, const std::string& xline_specific_mask) CXX11_OVERRIDE
82         {
83                 return new SVSHold(set_time, duration, source, reason, xline_specific_mask);
84         }
85
86         bool AutoApplyToUserList(XLine* x) CXX11_OVERRIDE
87         {
88                 return false;
89         }
90 };
91
92 /** Handle /SVSHold
93  */
94 class CommandSvshold : public Command
95 {
96  public:
97         CommandSvshold(Module* Creator) : Command(Creator, "SVSHOLD", 1)
98         {
99                 flags_needed = 'o'; this->syntax = "<nick> [<duration> :<reason>]";
100         }
101
102         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
103         {
104                 /* syntax: svshold nickname time :reason goes here */
105                 /* 'time' is a human-readable timestring, like 2d3h2s. */
106
107                 if (!user->server->IsULine())
108                 {
109                         /* don't allow SVSHOLD from non-ulined clients */
110                         return CMD_FAILURE;
111                 }
112
113                 if (parameters.size() == 1)
114                 {
115                         std::string reason;
116
117                         if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "SVSHOLD", reason, user))
118                         {
119                                 if (!silent)
120                                         ServerInstance->SNO->WriteToSnoMask('x', "%s removed SVSHOLD on %s: %s", user->nick.c_str(), parameters[0].c_str(), reason.c_str());
121                         }
122                         else
123                         {
124                                 user->WriteNotice("*** SVSHOLD " + parameters[0] + " not found on the list.");
125                         }
126                 }
127                 else
128                 {
129                         if (parameters.size() < 3)
130                                 return CMD_FAILURE;
131
132                         unsigned long duration;
133                         if (!InspIRCd::Duration(parameters[1], duration))
134                         {
135                                 user->WriteNotice("*** Invalid duration for SVSHOLD.");
136                                 return CMD_FAILURE;
137                         }
138                         SVSHold* r = new SVSHold(ServerInstance->Time(), duration, user->nick.c_str(), parameters[2].c_str(), parameters[0].c_str());
139
140                         if (ServerInstance->XLines->AddLine(r, user))
141                         {
142                                 if (silent)
143                                         return CMD_SUCCESS;
144
145                                 if (!duration)
146                                 {
147                                         ServerInstance->SNO->WriteGlobalSno('x', "%s added permanent SVSHOLD for %s: %s", user->nick.c_str(), parameters[0].c_str(), parameters[2].c_str());
148                                 }
149                                 else
150                                 {
151                                         ServerInstance->SNO->WriteGlobalSno('x', "%s added timed SVSHOLD for %s, expires in %s (on %s): %s",
152                                                 user->nick.c_str(), parameters[0].c_str(), InspIRCd::DurationString(duration).c_str(),
153                                                 InspIRCd::TimeString(ServerInstance->Time() + duration).c_str(), parameters[2].c_str());
154                                 }
155                         }
156                         else
157                         {
158                                 delete r;
159                                 return CMD_FAILURE;
160                         }
161                 }
162
163                 return CMD_SUCCESS;
164         }
165
166         RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
167         {
168                 return ROUTE_BROADCAST;
169         }
170 };
171
172 class ModuleSVSHold : public Module, public Stats::EventListener
173 {
174         CommandSvshold cmd;
175         SVSHoldFactory s;
176
177
178  public:
179         ModuleSVSHold()
180                 : Stats::EventListener(this)
181                 , cmd(this)
182         {
183         }
184
185         void init() CXX11_OVERRIDE
186         {
187                 ServerInstance->XLines->RegisterFactory(&s);
188         }
189
190         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
191         {
192                 ConfigTag* tag = ServerInstance->Config->ConfValue("svshold");
193                 silent = tag->getBool("silent", true);
194         }
195
196         ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE
197         {
198                 if (stats.GetSymbol() != 'S')
199                         return MOD_RES_PASSTHRU;
200
201                 ServerInstance->XLines->InvokeStats("SVSHOLD", 210, stats);
202                 return MOD_RES_DENY;
203         }
204
205         ModResult OnUserPreNick(LocalUser* user, const std::string& newnick) CXX11_OVERRIDE
206         {
207                 XLine *rl = ServerInstance->XLines->MatchesLine("SVSHOLD", newnick);
208
209                 if (rl)
210                 {
211                         user->WriteNumeric(ERR_ERRONEUSNICKNAME, newnick, InspIRCd::Format("Services reserved nickname: %s", rl->reason.c_str()));
212                         return MOD_RES_DENY;
213                 }
214
215                 return MOD_RES_PASSTHRU;
216         }
217
218         ~ModuleSVSHold()
219         {
220                 ServerInstance->XLines->DelAll("SVSHOLD");
221                 ServerInstance->XLines->UnregisterFactory(&s);
222         }
223
224         Version GetVersion() CXX11_OVERRIDE
225         {
226                 return Version("Implements SVSHOLD, like Q-lines, but can only be added/removed by Services", VF_COMMON | VF_VENDOR);
227         }
228 };
229
230 MODULE_INIT(ModuleSVSHold)