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