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