]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_svshold.cpp
Convert WriteNumeric() calls to pass the parameters of the numeric as method parameters
[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
26 namespace
27 {
28         bool silent;
29 }
30
31 /** Holds a SVSHold item
32  */
33 class SVSHold : public XLine
34 {
35 public:
36         std::string nickname;
37
38         SVSHold(time_t s_time, long d, const std::string& src, const std::string& re, const std::string& nick)
39                 : XLine(s_time, d, src, re, "SVSHOLD")
40         {
41                 this->nickname = nick;
42         }
43
44         bool Matches(User *u)
45         {
46                 if (u->nick == nickname)
47                         return true;
48                 return false;
49         }
50
51         bool Matches(const std::string &s)
52         {
53                 return InspIRCd::Match(s, nickname);
54         }
55
56         void DisplayExpiry()
57         {
58                 if (!silent)
59                 {
60                         ServerInstance->SNO->WriteToSnoMask('x', "Removing expired SVSHOLD %s (set by %s %ld seconds ago)",
61                                 nickname.c_str(), source.c_str(), (long)(ServerInstance->Time() - set_time));
62                 }
63         }
64
65         const std::string& Displayable()
66         {
67                 return nickname;
68         }
69 };
70
71 /** An XLineFactory specialized to generate SVSHOLD pointers
72  */
73 class SVSHoldFactory : public XLineFactory
74 {
75  public:
76         SVSHoldFactory() : XLineFactory("SVSHOLD") { }
77
78         /** Generate a shun
79         */
80         XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
81         {
82                 return new SVSHold(set_time, duration, source, reason, xline_specific_mask);
83         }
84
85         bool AutoApplyToUserList(XLine *x)
86         {
87                 return false;
88         }
89 };
90
91 /** Handle /SVSHold
92  */
93 class CommandSvshold : public Command
94 {
95  public:
96         CommandSvshold(Module* Creator) : Command(Creator, "SVSHOLD", 1)
97         {
98                 flags_needed = 'o'; this->syntax = "<nickname> [<duration> :<reason>]";
99         }
100
101         CmdResult Handle(const std::vector<std::string> &parameters, User *user)
102         {
103                 /* syntax: svshold nickname time :reason goes here */
104                 /* 'time' is a human-readable timestring, like 2d3h2s. */
105
106                 if (!user->server->IsULine())
107                 {
108                         /* don't allow SVSHOLD from non-ulined clients */
109                         return CMD_FAILURE;
110                 }
111
112                 if (parameters.size() == 1)
113                 {
114                         if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "SVSHOLD", user))
115                         {
116                                 if (!silent)
117                                         ServerInstance->SNO->WriteToSnoMask('x',"%s removed SVSHOLD on %s",user->nick.c_str(),parameters[0].c_str());
118                         }
119                         else
120                         {
121                                 user->WriteNotice("*** SVSHOLD " + parameters[0] + " not found in list, try /stats S.");
122                         }
123                 }
124                 else
125                 {
126                         if (parameters.size() < 3)
127                                 return CMD_FAILURE;
128
129                         unsigned long duration = InspIRCd::Duration(parameters[1]);
130                         SVSHold* r = new SVSHold(ServerInstance->Time(), duration, user->nick.c_str(), parameters[2].c_str(), parameters[0].c_str());
131
132                         if (ServerInstance->XLines->AddLine(r, user))
133                         {
134                                 if (silent)
135                                         return CMD_SUCCESS;
136
137                                 if (!duration)
138                                 {
139                                         ServerInstance->SNO->WriteGlobalSno('x', "%s added permanent SVSHOLD for %s: %s", user->nick.c_str(), parameters[0].c_str(), parameters[2].c_str());
140                                 }
141                                 else
142                                 {
143                                         time_t c_requires_crap = duration + ServerInstance->Time();
144                                         std::string timestr = InspIRCd::TimeString(c_requires_crap);
145                                         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());
146                                 }
147                         }
148                         else
149                         {
150                                 delete r;
151                                 return CMD_FAILURE;
152                         }
153                 }
154
155                 return CMD_SUCCESS;
156         }
157
158         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
159         {
160                 return ROUTE_BROADCAST;
161         }
162 };
163
164 class ModuleSVSHold : public Module
165 {
166         CommandSvshold cmd;
167         SVSHoldFactory s;
168
169
170  public:
171         ModuleSVSHold() : cmd(this)
172         {
173         }
174
175         void init() CXX11_OVERRIDE
176         {
177                 ServerInstance->XLines->RegisterFactory(&s);
178         }
179
180         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
181         {
182                 ConfigTag* tag = ServerInstance->Config->ConfValue("svshold");
183                 silent = tag->getBool("silent", true);
184         }
185
186         ModResult OnStats(char symbol, User* user, string_list &out) CXX11_OVERRIDE
187         {
188                 if(symbol != 'S')
189                         return MOD_RES_PASSTHRU;
190
191                 ServerInstance->XLines->InvokeStats("SVSHOLD", 210, user, out);
192                 return MOD_RES_DENY;
193         }
194
195         ModResult OnUserPreNick(LocalUser* user, const std::string& newnick) CXX11_OVERRIDE
196         {
197                 XLine *rl = ServerInstance->XLines->MatchesLine("SVSHOLD", newnick);
198
199                 if (rl)
200                 {
201                         user->WriteNumeric(ERR_ERRONEUSNICKNAME, newnick, InspIRCd::Format("Services reserved nickname: %s", rl->reason.c_str()));
202                         return MOD_RES_DENY;
203                 }
204
205                 return MOD_RES_PASSTHRU;
206         }
207
208         ~ModuleSVSHold()
209         {
210                 ServerInstance->XLines->DelAll("SVSHOLD");
211                 ServerInstance->XLines->UnregisterFactory(&s);
212         }
213
214         Version GetVersion() CXX11_OVERRIDE
215         {
216                 return Version("Implements SVSHOLD. Like Q:Lines, but can only be added/removed by Services.", VF_COMMON | VF_VENDOR);
217         }
218 };
219
220 MODULE_INIT(ModuleSVSHold)