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