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