]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_svshold.cpp
Fix bug found by giggsey (even though he doesnt realise he just found one)
[user/henk/code/inspircd.git] / src / modules / m_svshold.cpp
1 /*      +------------------------------------+
2  *      | Inspire Internet Relay Chat Daemon |
3  *      +------------------------------------+
4  *
5  * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                    E-mail:
7  *              <brain@chatspike.net>
8  *              <Craig@chatspike.net>
9  *                <omster@gmail.com>
10  * 
11  * Written by Craig Edwards, Craig McLure, and others.
12  * This program is free but copyrighted software; see
13  * the file COPYING for details.
14  *
15  * ---------------------------------------------------
16  */
17
18 #include <algorithm>
19 #include "users.h"
20 #include "channels.h"
21 #include "modules.h"
22 #include "configreader.h"
23 #include "inspircd.h"
24
25 /* $ModDesc: Implements SVSHOLD. Like Q:Lines, but can only be added/removed by Services. */
26
27 /** Holds a SVSHold item
28  */
29 class SVSHold : public classbase
30 {
31 public:
32         std::string nickname;
33         std::string set_by;
34         time_t set_on;
35         long length;
36         std::string reason;
37
38         SVSHold()
39         {
40         }
41
42         SVSHold(std::string nn, std::string sb, time_t so, long ln, std::string rs) : nickname(nn), set_by(sb), set_on(so), length(ln), reason(rs)
43         {
44         }
45 };
46
47 bool SVSHoldComp(const SVSHold &ban1, const SVSHold &ban2);
48
49 typedef std::vector<SVSHold> SVSHoldlist;
50
51 /* SVSHolds is declared here, as our type is right above. Don't try move it. */
52 SVSHoldlist SVSHolds;
53
54 /** Handle /SVSHold
55  */
56 class cmd_svshold : public command_t
57 {
58  public:
59         cmd_svshold(InspIRCd* Me) : command_t(Me, "SVSHOLD", 'o', 1)
60         {
61                 this->source = "m_svshold.so";
62                 this->
63                 syntax = "<nickname> [<duration> :<reason>]";
64         }
65
66         CmdResult Handle(const char** parameters, int pcnt, userrec *user)
67         {
68                 /* syntax: svshold nickname time :reason goes here */
69                 /* 'time' is a human-readable timestring, like 2d3h2s. */
70
71                 if (pcnt == 1)
72                 {
73                         /* form: svshold nickname removes a hold. */
74                         for (SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
75                         {
76                                 if (parameters[0] == iter->nickname)
77                                 {
78                                         unsigned long remaining = (iter->set_on + iter->length) - ServerInstance->Time();
79                                         user->WriteServ( "386 %s %s :Removed SVSHOLD with %lu seconds left before expiry (%s)", user->nick, iter->nickname.c_str(), remaining, iter->reason.c_str());
80                                         SVSHolds.erase(iter);
81                                         break;
82                                 }
83                         }
84                 }
85                 else if (pcnt >= 2)
86                 {
87                         /* full form to add a SVSHold */
88                         if (ServerInstance->IsNick(parameters[0]))
89                         {
90                                 // parameters[0] = w00t
91                                 // parameters[1] = 1h3m2s
92                                 // parameters[2] = Registered nickname
93                                 long length = ServerInstance->Duration(parameters[1]);
94                                 std::string reason = (pcnt > 2) ? parameters[2] : "No reason supplied";
95                                 
96                                 SVSHolds.push_back(SVSHold(parameters[0], user->nick, ServerInstance->Time(), length, reason));
97                                         
98                                 std::sort(SVSHolds.begin(), SVSHolds.end(), SVSHoldComp);
99                                 
100                                 if(length > 0)
101                                 {
102                                         user->WriteServ( "385 %s %s :Added %lu second SVSHOLD (%s)", user->nick, parameters[0], length, reason.c_str());
103                                         ServerInstance->WriteOpers("*** %s added %lu second SVSHOLD on %s (%s)", user->nick, length, parameters[0], reason.c_str());
104                                 }
105                                 else
106                                 {
107                                         user->WriteServ( "385 %s %s :Added permanent SVSHOLD on %s (%s)", user->nick, parameters[0], reason.c_str());
108                                         ServerInstance->WriteOpers("*** %s added permanent SVSHOLD on %s (%s)", user->nick, parameters[0], reason.c_str());
109                                 }
110                         }
111                         else
112                         {
113                                 /* as this is primarily a Services command, do not provide an error */
114                                 return CMD_FAILURE;
115                         }
116                 }
117
118                 return CMD_SUCCESS;
119         }
120 };
121
122 bool SVSHoldComp(const SVSHold &ban1, const SVSHold &ban2)
123 {
124         return ((ban1.set_on + ban1.length) < (ban2.set_on + ban2.length));
125 }
126
127 class ModuleSVSHold : public Module
128 {
129         cmd_svshold *mycommand;
130         
131
132  public:
133         ModuleSVSHold(InspIRCd* Me) : Module::Module(Me)
134         {
135                 mycommand = new cmd_svshold(Me);
136                 ServerInstance->AddCommand(mycommand);
137         }
138
139         void Implements(char* List)
140         {
141                 List[I_OnUserPreNick] = List[I_OnSyncOtherMetaData] = List[I_OnDecodeMetaData] = List[I_OnStats] = 1;
142         }
143         
144         virtual int OnStats(char symbol, userrec* user, string_list &results)
145         {
146                 ExpireBans();
147         
148                 if(symbol == 'S')
149                 {
150                         for(SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
151                         {
152                                 unsigned long remaining = (iter->set_on + iter->length) - ServerInstance->Time();
153                                 results.push_back(std::string(ServerInstance->Config->ServerName)+" 210 "+user->nick+" "+iter->nickname.c_str()+" "+iter->set_by+" "+ConvToStr(iter->set_on)+" "+ConvToStr(iter->length)+" "+ConvToStr(remaining)+" :"+iter->reason);
154                         }
155                 }
156                 
157                 return 0;
158         }
159
160         virtual int OnUserPreNick(userrec *user, const std::string &newnick)
161         {
162                 ExpireBans();
163         
164                 /* check SVSHolds in here, and apply as necessary. */
165                 for(SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
166                 {
167                         if (iter->nickname == newnick)
168                         {
169                                 // nope, boned.
170                                 user->WriteServ( "432 %s %s :Reserved nickname: %s", user->nick, newnick.c_str(), iter->reason.c_str());
171                                 return 1;
172                         }
173                 }
174                 return 0;
175         }
176         
177         virtual void OnSyncOtherMetaData(Module* proto, void* opaque)
178         {
179                 for(SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
180                 {
181                         proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "SVSHold", EncodeSVSHold(*iter));
182                 }
183         }
184         
185         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
186         {
187                 if((target_type == TYPE_OTHER) && (extname == "SVSHold"))
188                 {
189                         SVSHolds.push_back(DecodeSVSHold(extdata));
190                         std::sort(SVSHolds.begin(), SVSHolds.end(), SVSHoldComp);
191                 }
192         }
193
194         virtual ~ModuleSVSHold()
195         {
196         }
197         
198         virtual Version GetVersion()
199         {
200                 return Version(1,0,0,1,VF_VENDOR);
201         }
202
203         std::string EncodeSVSHold(const SVSHold &ban)
204         {
205                 std::ostringstream stream;      
206                 stream << ban.nickname << " " << ban.set_by << " " << ban.set_on << " " << ban.length << " " << ban.reason;
207                 return stream.str();    
208         }
209
210         SVSHold DecodeSVSHold(const std::string &data)
211         {
212                 SVSHold res;
213                 std::istringstream stream(data);
214                 stream >> res.nickname;
215                 stream >> res.set_by;
216                 stream >> res.set_on;
217                 stream >> res.length;
218                 res.reason = stream.str();
219         
220                 return res;
221         }
222
223         void ExpireBans()
224         {
225                 bool go_again = true;
226
227                 while (go_again)
228                 {
229                         go_again = false;
230         
231                         for (SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
232                         {
233                                 /* 0 == permanent, don't mess with them! -- w00t */
234                                 if (iter->length != 0)
235                                 {
236                                         if (iter->set_on + iter->length <= ServerInstance->Time())
237                                         {
238                                                 ServerInstance->Log(DEBUG, "m_svshold.so: hold on %s expired, removing...", iter->nickname.c_str());
239                                                 ServerInstance->WriteOpers("*** %li second SVSHOLD on %s (%s) set %u seconds ago expired", iter->length, iter->nickname.c_str(), iter->reason.c_str(), ServerInstance->Time() - iter->set_on);
240                                                 SVSHolds.erase(iter);
241                                                 go_again = true;
242                                         }
243                                 }
244         
245                                 if (go_again == true)
246                                         break;
247                         }
248                 }
249         }
250 };
251
252 class ModuleSVSHoldFactory : public ModuleFactory
253 {
254  public:
255         ModuleSVSHoldFactory()
256         {
257         }
258         
259         ~ModuleSVSHoldFactory()
260         {
261         }
262         
263         virtual Module * CreateModule(InspIRCd* Me)
264         {
265                 return new ModuleSVSHold(Me);
266         }
267         
268 };
269
270
271 extern "C" void * init_module( void )
272 {
273         return new ModuleSVSHoldFactory;
274 }