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