]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cban.cpp
IRC case sensitivity bug, if only we could use ASCII......
[user/henk/code/inspircd.git] / src / modules / m_cban.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 <vector>
20 #include <string>
21 #include <sstream>
22 #include "users.h"
23 #include "channels.h"
24 #include "modules.h"
25 #include "helperfuncs.h"
26 #include "hashcomp.h"
27
28 /* $ModDesc: Gives /cban, aka C:lines. Think Q:lines, for channels. */
29
30 class CBan
31 {
32 public:
33         irc::string chname;
34         std::string set_by;
35         time_t set_on;
36         long length;
37         std::string reason;
38
39         CBan()
40         {
41         }
42
43         CBan(irc::string cn, std::string sb, time_t so, long ln, std::string rs) : chname(cn), set_by(sb), set_on(so), length(ln), reason(rs)
44         {
45         }
46 };
47
48 std::string EncodeCBan(const CBan &ban);
49 CBan DecodeCBan(const std::string &data);
50 bool CBanComp(const CBan &ban1, const CBan &ban2);
51 void ExpireBans();
52 bool IsValidChan(const char* cname);
53
54 extern time_t TIME;
55 typedef std::vector<CBan> cbanlist;
56
57 /* cbans is declared here, as our type is right above. Don't try move it. */
58 cbanlist cbans;
59
60 class cmd_cban : public command_t
61 {
62  private:
63         Server *Srv;
64
65  public:
66         cmd_cban(Server* Me) : command_t("CBAN", 'o', 1)
67         {
68                 this->source = "m_cban.so";
69                 this->Srv = Me;
70         }
71
72         void Handle(char **parameters, int pcnt, userrec *user)
73         {
74                 /* syntax: CBAN #channel time :reason goes here */
75                 /* 'time' is a human-readable timestring, like 2d3h2s. */
76                 
77                 ExpireBans();
78
79                 if(pcnt == 1)
80                 {
81                         /* form: CBAN #channel removes a CBAN */
82                         for (cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
83                         {
84                                 if (parameters[0] == iter->chname)
85                                 {
86                                         unsigned long remaining = (iter->set_on + iter->length) - TIME;
87                                         WriteServ(user->fd, "386 %s %s :Removed CBAN with %lu seconds left before expiry (%s)", user->nick, iter->chname.c_str(), remaining, iter->reason.c_str());
88                                         cbans.erase(iter);
89                                         break;
90                                 }
91                         }
92                 }
93                 else if (pcnt >= 2)
94                 {
95                         /* full form to add a CBAN */
96                         if(IsValidChan(parameters[0]))
97                         {
98                                 // parameters[0] = #channel
99                                 // parameters[1] = 1h3m2s
100                                 // parameters[2] = Tortoise abuser
101                                 long length = Srv->CalcDuration(parameters[1]);
102                                 std::string reason = (pcnt > 2) ? parameters[2] : "No reason supplied";
103                                 
104                                 cbans.push_back(CBan(parameters[0], user->nick, TIME, length, reason));
105                                         
106                                 std::sort(cbans.begin(), cbans.end(), CBanComp);
107                                 
108                                 if(length > 0)
109                                 {
110                                         WriteServ(user->fd, "385 %s %s :Added %lu second channel ban (%s)", user->nick, parameters[0], length, reason.c_str());
111                                         WriteOpers("*** %s added %lu second channel ban on %s (%s)", user->nick, length, parameters[0], reason.c_str());
112                                 }
113                                 else
114                                 {
115                                         WriteServ(user->fd, "385 %s %s :Added permenant channel ban (%s)", user->nick, parameters[0], reason.c_str());
116                                         WriteOpers("*** %s added permenant channel ban on %s (%s)", user->nick, parameters[0], reason.c_str());
117                                 }
118                         }
119                         else
120                         {
121                                 WriteServ(user->fd, "403 %s %s :No such channel", user->nick, parameters[0]);
122                         }
123                 }
124         }
125 };
126
127 class ModuleCBan : public Module
128 {
129         cmd_cban* mycommand;
130         Server* Srv;
131
132  public:
133         ModuleCBan(Server* Me) : Module::Module(Me)
134         {
135                 Srv = Me;
136                 mycommand = new cmd_cban(Srv);
137                 Srv->AddCommand(mycommand);
138         }
139
140         void Implements(char* List)
141         {
142                 List[I_OnUserPreJoin] = List[I_OnSyncOtherMetaData] = List[I_OnDecodeMetaData] = List[I_OnStats] = 1;
143         }
144         
145         virtual int OnStats(char symbol, userrec* user)
146         {
147                 ExpireBans();
148         
149                 if(symbol == 'C')
150                 {
151                         for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
152                         {
153                                 unsigned long remaining = (iter->set_on + iter->length) - TIME;
154                                 WriteServ(user->fd, "210 %s %s %s %lu %lu %lu :%s", user->nick, iter->chname.c_str(), iter->set_by.c_str(), iter->set_on, iter->length, remaining, iter->reason.c_str());
155                         }
156                 }
157                 
158                 return 0;
159         }
160
161         virtual int OnUserPreJoin(userrec *user, chanrec *chan, const char *cname)
162         {
163                 ExpireBans();
164         
165                 /* check cbans in here, and apply as necessary. */
166                 for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
167                 {
168                         if(iter->chname == cname && !strchr(user->modes, 'o'))
169                         {
170                                 // Channel is banned.
171                                 WriteServ(user->fd, "384 %s %s :Cannot join channel, CBANed (%s)", user->nick, cname, iter->reason.c_str());
172                                 WriteOpers("*** %s tried to join %s which is CBANed (%s)", user->nick, cname, iter->reason.c_str());
173                                 return 1;
174                         }
175                 }
176                 return 0;
177         }
178         
179         virtual void OnSyncOtherMetaData(Module* proto, void* opaque)
180         {
181                 for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
182                 {
183                         proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "cban", EncodeCBan(*iter));
184                 }
185         }
186         
187         virtual void OnDecodeMetaData(int target_type, void* target, std::string extname, std::string extdata)
188         {
189                 if((target_type == TYPE_OTHER) && (extname == "cban"))
190                 {
191                         cbans.push_back(DecodeCBan(extdata));
192                         std::sort(cbans.begin(), cbans.end(), CBanComp);
193                 }
194         }
195
196         virtual ~ModuleCBan()
197         {
198         }
199         
200         virtual Version GetVersion()
201         {
202                 return Version(1,0,0,0,VF_VENDOR);
203         }
204 };
205
206 std::string EncodeCBan(const CBan &ban)
207 {
208         std::ostringstream stream;      
209         stream << ban.chname.c_str() << " " << ban.set_by << " " << ban.set_on << " " << ban.length << " " << ban.reason;
210         return stream.str();    
211 }
212
213 CBan DecodeCBan(const std::string &data)
214 {
215         CBan res;
216         std::istringstream stream;
217         // XXX - Change this...we shouldn't need tempname, need an overloaded iostream operator on irc::string?
218         std::string tempname;
219         stream >> tempname;
220         stream >> res.set_by;
221         stream >> res.set_on;
222         stream >> res.length;
223         res.reason = stream.str();
224         res.chname = tempname.c_str();
225         
226         return res;
227 }
228
229 bool CBanComp(const CBan &ban1, const CBan &ban2)
230 {
231         return ((ban1.set_on + ban1.length) < (ban2.set_on + ban2.length));
232 }
233
234 void ExpireBans()
235 {
236         while(cbans.size() && ((cbans.begin()->set_on + cbans.begin()->length) <= TIME))
237         {
238                 cbanlist::iterator iter = cbans.begin();
239                 
240                 log(DEBUG, "m_cban.so: Ban on %s expired, removing...", iter->chname.c_str());
241                 WriteOpers("*** %li second CBAN on %s (%s) set %u seconds ago expired", iter->length, iter->chname.c_str(), iter->reason.c_str(), TIME - iter->set_on);
242                 cbans.erase(iter);
243         }
244 }
245
246 bool IsValidChan(const char* cname)
247 {
248         if(!cname)
249                 return false;
250                 
251         if(cname[0] != '#')
252                 return false;
253                 
254         for(unsigned int i = 0; i < strlen(cname); i++)
255                 if((cname[i] == ' ') || (cname[i] == '\7') || (cname[i] == ','))
256                         return false;
257                         
258         return true;
259 }
260
261 class ModuleCBanFactory : public ModuleFactory
262 {
263  public:
264         ModuleCBanFactory()
265         {
266         }
267         
268         ~ModuleCBanFactory()
269         {
270         }
271         
272         virtual Module * CreateModule(Server* Me)
273         {
274                 return new ModuleCBan(Me);
275         }
276         
277 };
278
279
280 extern "C" void * init_module( void )
281 {
282         return new ModuleCBanFactory;
283 }