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