]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_svshold.cpp
a38776d8db7a501a7a8f1fefe20a19cb4cccfce9
[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         irc::string nickname;
34
35         SVSHold(time_t s_time, long d, std::string src, std::string re, std::string nick)
36                 : XLine(s_time, d, src, re, "SVSHOLD")
37         {
38                 this->nickname = nick.c_str();
39         }
40
41         ~SVSHold()
42         {
43         }
44
45         bool Matches(User *u)
46         {
47                 if (u->nick == nickname)
48                         return true;
49                 return false;
50         }
51
52         bool Matches(const std::string &s)
53         {
54                 if (nickname == s)
55                         return true;
56                 return false;
57         }
58
59         void DisplayExpiry()
60         {
61                 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired SVSHOLD %s (set by %s %ld seconds ago)",
62                         this->nickname.c_str(), this->source.c_str(), (long int)(ServerInstance->Time() - this->set_time));
63         }
64
65         const char* Displayable()
66         {
67                 return nickname.c_str();
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
86 /** Handle /SVSHold
87  */
88 class CommandSvshold : public Command
89 {
90  public:
91         CommandSvshold(Module* Creator) : Command(Creator, "SVSHOLD", 1)
92         {
93                 flags_needed = 'o'; this->syntax = "<nickname> [<duration> :<reason>]";
94                 TRANSLATE4(TR_TEXT, TR_TEXT, TR_TEXT, TR_END);
95         }
96
97         CmdResult Handle(const std::vector<std::string> &parameters, User *user)
98         {
99                 /* syntax: svshold nickname time :reason goes here */
100                 /* 'time' is a human-readable timestring, like 2d3h2s. */
101
102                 if (!ServerInstance->ULine(user->server))
103                 {
104                         /* don't allow SVSHOLD from non-ulined clients */
105                         return CMD_FAILURE;
106                 }
107
108                 if (parameters.size() == 1)
109                 {
110                         if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "SVSHOLD", user))
111                         {
112                                 ServerInstance->SNO->WriteToSnoMask('x',"%s removed SVSHOLD on %s",user->nick.c_str(),parameters[0].c_str());
113                         }
114                         else
115                         {
116                                 user->WriteServ("NOTICE %s :*** SVSHOLD %s not found in list, try /stats S.",user->nick.c_str(),parameters[0].c_str());
117                         }
118
119                         return CMD_SUCCESS;
120                 }
121                 else if (parameters.size() >= 2)
122                 {
123                         // Adding - XXX todo make this respect <insane> tag perhaps..
124                         long duration = ServerInstance->Duration(parameters[1]);
125                         SVSHold *r = NULL;
126
127                         try
128                         {
129                                 r = new SVSHold(ServerInstance->Time(), duration, user->nick.c_str(), parameters[2].c_str(), parameters[0].c_str());
130                         }
131                         catch (...)
132                         {
133                                 ; // Do nothing.
134                         }
135
136                         if (r)
137                         {
138                                 if (ServerInstance->XLines->AddLine(r, user))
139                                 {
140                                         if (!duration)
141                                         {
142                                                 ServerInstance->SNO->WriteGlobalSno('x', "%s added permanent SVSHOLD for %s: %s", user->nick.c_str(), parameters[0].c_str(), parameters[2].c_str());
143                                         }
144                                         else
145                                         {
146                                                 time_t c_requires_crap = duration + ServerInstance->Time();
147                                                 ServerInstance->SNO->WriteGlobalSno('x', "%s added timed SVSHOLD for %s, expires on %s: %s", user->nick.c_str(), parameters[0].c_str(), ServerInstance->TimeString(c_requires_crap).c_str(), parameters[2].c_str());
148                                         }
149
150                                         ServerInstance->XLines->ApplyLines();
151                                 }
152                                 else
153                                 {
154                                         delete r;
155                                 }
156                         }
157                 }
158
159                 return CMD_SUCCESS;
160         }
161
162         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
163         {
164                 return ROUTE_BROADCAST;
165         }
166 };
167
168 class ModuleSVSHold : public Module
169 {
170         CommandSvshold cmd;
171         SVSHoldFactory s;
172
173
174  public:
175         ModuleSVSHold() : cmd(this)
176         {
177                 ServerInstance->XLines->RegisterFactory(&s);
178                 ServerInstance->AddCommand(&cmd);
179                 Implementation eventlist[] = { I_OnUserPreNick, I_OnStats };
180                 ServerInstance->Modules->Attach(eventlist, this, 2);
181         }
182
183
184         virtual ModResult OnStats(char symbol, User* user, string_list &out)
185         {
186                 if(symbol != 'S')
187                         return MOD_RES_PASSTHRU;
188
189                 ServerInstance->XLines->InvokeStats("SVSHOLD", 210, user, out);
190                 return MOD_RES_DENY;
191         }
192
193         virtual ModResult OnUserPreNick(User *user, const std::string &newnick)
194         {
195                 XLine *rl = ServerInstance->XLines->MatchesLine("SVSHOLD", newnick);
196
197                 if (rl)
198                 {
199                         user->WriteServ( "432 %s %s :Services reserved nickname: %s", user->nick.c_str(), newnick.c_str(), rl->reason.c_str());
200                         return MOD_RES_DENY;
201                 }
202
203                 return MOD_RES_PASSTHRU;
204         }
205
206         virtual ~ModuleSVSHold()
207         {
208                 ServerInstance->XLines->DelAll("SVSHOLD");
209                 ServerInstance->XLines->UnregisterFactory(&s);
210         }
211
212         virtual Version GetVersion()
213         {
214                 return Version("Implements SVSHOLD. Like Q:Lines, but can only be added/removed by Services.", VF_COMMON | VF_VENDOR);
215         }
216 };
217
218 MODULE_INIT(ModuleSVSHold)