]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cban.cpp
None of the modules use an extern InspIRCd* any more
[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
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 bool CBanComp(const CBan &ban1, const CBan &ban2)
130 {
131         return ((ban1.set_on + ban1.length) < (ban2.set_on + ban2.length));
132 }
133
134 class ModuleCBan : public Module
135 {
136         cmd_cban* mycommand;
137         
138
139  public:
140         ModuleCBan(InspIRCd* Me) : Module::Module(Me)
141         {
142                 
143                 mycommand = new cmd_cban(Me);
144                 ServerInstance->AddCommand(mycommand);
145         }
146
147         void Implements(char* List)
148         {
149                 List[I_OnUserPreJoin] = List[I_OnSyncOtherMetaData] = List[I_OnDecodeMetaData] = List[I_OnStats] = 1;
150         }
151         
152         virtual int OnStats(char symbol, userrec* user, string_list &results)
153         {
154                 ExpireBans();
155         
156                 if(symbol == 'C')
157                 {
158                         for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
159                         {
160                                 unsigned long remaining = (iter->set_on + iter->length) - TIME;
161                                 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);
162                         }
163                 }
164                 
165                 return 0;
166         }
167
168         virtual int OnUserPreJoin(userrec *user, chanrec *chan, const char *cname)
169         {
170                 ExpireBans();
171         
172                 /* check cbans in here, and apply as necessary. */
173                 for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
174                 {
175                         if(iter->chname == cname && !user->modes[UM_OPERATOR])
176                         {
177                                 // Channel is banned.
178                                 user->WriteServ( "384 %s %s :Cannot join channel, CBANed (%s)", user->nick, cname, iter->reason.c_str());
179                                 ServerInstance->WriteOpers("*** %s tried to join %s which is CBANed (%s)", user->nick, cname, iter->reason.c_str());
180                                 return 1;
181                         }
182                 }
183                 return 0;
184         }
185         
186         virtual void OnSyncOtherMetaData(Module* proto, void* opaque)
187         {
188                 for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
189                 {
190                         proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "cban", EncodeCBan(*iter));
191                 }
192         }
193         
194         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
195         {
196                 if((target_type == TYPE_OTHER) && (extname == "cban"))
197                 {
198                         cbans.push_back(DecodeCBan(extdata));
199                         std::sort(cbans.begin(), cbans.end(), CBanComp);
200                 }
201         }
202
203         virtual ~ModuleCBan()
204         {
205         }
206         
207         virtual Version GetVersion()
208         {
209                 return Version(1,0,0,1,VF_VENDOR);
210         }
211
212         std::string EncodeCBan(const CBan &ban)
213         {
214                 std::ostringstream stream;      
215                 stream << ban.chname << " " << ban.set_by << " " << ban.set_on << " " << ban.length << " " << ban.reason;
216                 return stream.str();    
217         }
218
219         CBan DecodeCBan(const std::string &data)
220         {
221                 CBan res;
222                 std::istringstream stream(data);
223                 stream >> res.chname;
224                 stream >> res.set_by;
225                 stream >> res.set_on;
226                 stream >> res.length;
227                 res.reason = stream.str();
228         
229                 return res;
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                                                 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);
249                                                 cbans.erase(iter);
250                                                 go_again = true;
251                                         }
252                                 }
253         
254                                 if (go_again == true)
255                                         break;
256                         }
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 }