]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_svshold.cpp
Improve X-line text consistency.
[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 %ld seconds ago): %s",
62                                 nickname.c_str(), source.c_str(), (long)(ServerInstance->Time() - set_time), 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 = "<nickname> [<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                         if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "SVSHOLD", user))
116                         {
117                                 if (!silent)
118                                         ServerInstance->SNO->WriteToSnoMask('x',"%s removed SVSHOLD on %s",user->nick.c_str(),parameters[0].c_str());
119                         }
120                         else
121                         {
122                                 user->WriteNotice("*** SVSHOLD " + parameters[0] + " not found in list, try /stats S.");
123                         }
124                 }
125                 else
126                 {
127                         if (parameters.size() < 3)
128                                 return CMD_FAILURE;
129
130                         unsigned long duration;
131                         if (!InspIRCd::Duration(parameters[1], duration))
132                         {
133                                 user->WriteNotice("*** Invalid duration for SVSHOLD.");
134                                 return CMD_FAILURE;
135                         }
136                         SVSHold* r = new SVSHold(ServerInstance->Time(), duration, user->nick.c_str(), parameters[2].c_str(), parameters[0].c_str());
137
138                         if (ServerInstance->XLines->AddLine(r, user))
139                         {
140                                 if (silent)
141                                         return CMD_SUCCESS;
142
143                                 if (!duration)
144                                 {
145                                         ServerInstance->SNO->WriteGlobalSno('x', "%s added permanent SVSHOLD for %s: %s", user->nick.c_str(), parameters[0].c_str(), parameters[2].c_str());
146                                 }
147                                 else
148                                 {
149                                         time_t c_requires_crap = duration + ServerInstance->Time();
150                                         std::string timestr = InspIRCd::TimeString(c_requires_crap);
151                                         ServerInstance->SNO->WriteGlobalSno('x', "%s added timed SVSHOLD for %s, expires on %s: %s", user->nick.c_str(), parameters[0].c_str(), timestr.c_str(), parameters[2].c_str());
152                                 }
153                         }
154                         else
155                         {
156                                 delete r;
157                                 return CMD_FAILURE;
158                         }
159                 }
160
161                 return CMD_SUCCESS;
162         }
163
164         RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
165         {
166                 return ROUTE_BROADCAST;
167         }
168 };
169
170 class ModuleSVSHold : public Module, public Stats::EventListener
171 {
172         CommandSvshold cmd;
173         SVSHoldFactory s;
174
175
176  public:
177         ModuleSVSHold()
178                 : Stats::EventListener(this)
179                 , cmd(this)
180         {
181         }
182
183         void init() CXX11_OVERRIDE
184         {
185                 ServerInstance->XLines->RegisterFactory(&s);
186         }
187
188         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
189         {
190                 ConfigTag* tag = ServerInstance->Config->ConfValue("svshold");
191                 silent = tag->getBool("silent", true);
192         }
193
194         ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE
195         {
196                 if (stats.GetSymbol() != 'S')
197                         return MOD_RES_PASSTHRU;
198
199                 ServerInstance->XLines->InvokeStats("SVSHOLD", 210, stats);
200                 return MOD_RES_DENY;
201         }
202
203         ModResult OnUserPreNick(LocalUser* user, const std::string& newnick) CXX11_OVERRIDE
204         {
205                 XLine *rl = ServerInstance->XLines->MatchesLine("SVSHOLD", newnick);
206
207                 if (rl)
208                 {
209                         user->WriteNumeric(ERR_ERRONEUSNICKNAME, newnick, InspIRCd::Format("Services reserved nickname: %s", rl->reason.c_str()));
210                         return MOD_RES_DENY;
211                 }
212
213                 return MOD_RES_PASSTHRU;
214         }
215
216         ~ModuleSVSHold()
217         {
218                 ServerInstance->XLines->DelAll("SVSHOLD");
219                 ServerInstance->XLines->UnregisterFactory(&s);
220         }
221
222         Version GetVersion() CXX11_OVERRIDE
223         {
224                 return Version("Implements SVSHOLD. Like Q-lines, but can only be added/removed by Services.", VF_COMMON | VF_VENDOR);
225         }
226 };
227
228 MODULE_INIT(ModuleSVSHold)