]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_svshold.cpp
e99adcaece54ce9d53583abeebb986224860fb4e
[user/henk/code/inspircd.git] / src / modules / m_svshold.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "xline.h"
16
17 /* $ModDesc: Implements SVSHOLD. Like Q:Lines, but can only be added/removed by Services. */
18
19 /** Holds a SVSHold item
20  */
21 class SVSHold : public XLine
22 {
23 public:
24         irc::string nickname;
25
26         SVSHold(InspIRCd* Instance, time_t s_time, long d, std::string src, std::string re, std::string nick)
27                 : XLine(Instance, s_time, d, src, re, "SVSHOLD")
28         {
29                 this->nickname = nick.c_str();
30         }
31
32         ~SVSHold()
33         {
34         }
35
36         bool Matches(User *u)
37         {
38                 if (u->nick == nickname)
39                         return true;
40                 return false;
41         }
42
43         bool Matches(const std::string &s)
44         {
45                 if (nickname == s)
46                         return true;
47                 return false;
48         }
49
50         void DisplayExpiry()
51         {
52                 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired SVSHOLD %s (set by %s %ld seconds ago)",
53                         this->nickname.c_str(), this->source.c_str(), (long int)(ServerInstance->Time() - this->set_time));
54         }
55
56         const char* Displayable()
57         {
58                 return nickname.c_str();
59         }
60 };
61
62 /** An XLineFactory specialized to generate SVSHOLD pointers
63  */
64 class SVSHoldFactory : public XLineFactory
65 {
66  public:
67         SVSHoldFactory(InspIRCd* Instance) : XLineFactory(Instance, "SVSHOLD") { }
68
69         /** Generate a shun
70         */
71         XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
72         {
73                 return new SVSHold(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
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                 TRANSLATE4(TR_NICK, TR_TEXT, TR_TEXT, TR_END);
86         }
87
88         CmdResult Handle(const std::vector<std::string> &parameters, User *user)
89         {
90                 /* syntax: svshold nickname time :reason goes here */
91                 /* 'time' is a human-readable timestring, like 2d3h2s. */
92
93                 if (!ServerInstance->ULine(user->server))
94                 {
95                         /* don't allow SVSHOLD from non-ulined clients */
96                         return CMD_FAILURE;
97                 }
98
99                 if (parameters.size() == 1)
100                 {
101                         if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "SVSHOLD", user))
102                         {
103                                 ServerInstance->SNO->WriteToSnoMask('x',"%s removed SVSHOLD on %s",user->nick.c_str(),parameters[0].c_str());
104                         }
105                         else
106                         {
107                                 user->WriteServ("NOTICE %s :*** SVSHOLD %s not found in list, try /stats S.",user->nick.c_str(),parameters[0].c_str());
108                         }
109
110                         return CMD_SUCCESS;
111                 }
112                 else if (parameters.size() >= 2)
113                 {
114                         // Adding - XXX todo make this respect <insane> tag perhaps..
115                         long duration = ServerInstance->Duration(parameters[1]);
116                         SVSHold *r = NULL;
117
118                         try
119                         {
120                                 r = new SVSHold(ServerInstance, ServerInstance->Time(), duration, user->nick.c_str(), parameters[2].c_str(), parameters[0].c_str());
121                         }
122                         catch (...)
123                         {
124                                 ; // Do nothing.
125                         }
126
127                         if (r)
128                         {
129                                 if (ServerInstance->XLines->AddLine(r, user))
130                                 {
131                                         if (!duration)
132                                         {
133                                                 ServerInstance->SNO->WriteGlobalSno('x', "%s added permanent SVSHOLD for %s: %s", user->nick.c_str(), parameters[0].c_str(), parameters[2].c_str());
134                                         }
135                                         else
136                                         {
137                                                 time_t c_requires_crap = duration + ServerInstance->Time();
138                                                 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());
139                                         }
140
141                                         ServerInstance->XLines->ApplyLines();
142                                 }
143                                 else
144                                 {
145                                         delete r;
146                                 }
147                         }
148                 }
149
150                 return CMD_SUCCESS;
151         }
152
153         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
154         {
155                 return ROUTE_BROADCAST;
156         }
157 };
158
159 class ModuleSVSHold : public Module
160 {
161         CommandSvshold cmd;
162         SVSHoldFactory s;
163
164
165  public:
166         ModuleSVSHold(InspIRCd* Me) : Module(Me), cmd(this), s(Me)
167         {
168                 ServerInstance->XLines->RegisterFactory(&s);
169                 ServerInstance->AddCommand(&cmd);
170                 Implementation eventlist[] = { I_OnUserPreNick, I_OnStats };
171                 ServerInstance->Modules->Attach(eventlist, this, 2);
172         }
173
174
175         virtual ModResult OnStats(char symbol, User* user, string_list &out)
176         {
177                 if(symbol != 'S')
178                         return MOD_RES_PASSTHRU;
179
180                 ServerInstance->XLines->InvokeStats("SVSHOLD", 210, user, out);
181                 return MOD_RES_DENY;
182         }
183
184         virtual ModResult OnUserPreNick(User *user, const std::string &newnick)
185         {
186                 XLine *rl = ServerInstance->XLines->MatchesLine("SVSHOLD", newnick);
187
188                 if (rl)
189                 {
190                         user->WriteServ( "432 %s %s :Services reserved nickname: %s", user->nick.c_str(), newnick.c_str(), rl->reason.c_str());
191                         return MOD_RES_DENY;
192                 }
193
194                 return MOD_RES_PASSTHRU;
195         }
196
197         virtual ~ModuleSVSHold()
198         {
199                 ServerInstance->XLines->DelAll("SVSHOLD");
200                 ServerInstance->XLines->UnregisterFactory(&s);
201         }
202
203         virtual Version GetVersion()
204         {
205                 return Version("Implements SVSHOLD. Like Q:Lines, but can only be added/removed by Services.", VF_COMMON | VF_VENDOR, API_VERSION);
206         }
207 };
208
209 MODULE_INIT(ModuleSVSHold)