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