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