]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cban.cpp
Windows support. Tested and working to compile on freebsd and linux. Next step is...
[user/henk/code/inspircd.git] / src / modules / m_cban.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include <algorithm>
15 #include "users.h"
16 #include "channels.h"
17 #include "modules.h"
18 #include "configreader.h"
19 #include "inspircd.h"
20
21 /* $ModDesc: Gives /cban, aka C:lines. Think Q:lines, for channels. */
22
23 /** Holds a CBAN item
24  */
25 class CBan : public classbase
26 {
27 public:
28         irc::string chname;
29         std::string set_by;
30         time_t set_on;
31         long length;
32         std::string reason;
33
34         CBan()
35         {
36         }
37
38         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)
39         {
40         }
41 };
42
43 bool CBanComp(const CBan &ban1, const CBan &ban2);
44
45 typedef std::vector<CBan> cbanlist;
46
47 /* cbans is declared here, as our type is right above. Don't try move it. */
48 cbanlist cbans;
49
50 /** Handle /CBAN
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         CmdResult 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                                 return CMD_FAILURE;
111                         }
112                 }
113
114                 /* we want this routed! */
115                 return CMD_SUCCESS;
116         }
117 };
118
119 bool CBanComp(const CBan &ban1, const CBan &ban2)
120 {
121         return ((ban1.set_on + ban1.length) < (ban2.set_on + ban2.length));
122 }
123
124 class ModuleCBan : public Module
125 {
126         cmd_cban* mycommand;
127         
128
129  public:
130         ModuleCBan(InspIRCd* Me) : Module(Me)
131         {
132                 
133                 mycommand = new cmd_cban(Me);
134                 ServerInstance->AddCommand(mycommand);
135         }
136
137         void Implements(char* List)
138         {
139                 List[I_OnUserPreJoin] = List[I_OnSyncOtherMetaData] = List[I_OnDecodeMetaData] = List[I_OnStats] = 1;
140         }
141         
142         virtual int OnStats(char symbol, userrec* user, string_list &results)
143         {
144                 ExpireBans();
145         
146                 if(symbol == 'C')
147                 {
148                         for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
149                         {
150                                 unsigned long remaining = (iter->set_on + iter->length) - ServerInstance->Time();
151                                 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);
152                         }
153                 }
154                 
155                 return 0;
156         }
157
158         virtual int OnUserPreJoin(userrec *user, chanrec *chan, const char *cname, std::string &privs)
159         {
160                 ExpireBans();
161         
162                 /* check cbans in here, and apply as necessary. */
163                 for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
164                 {
165                         if(iter->chname == cname && !user->modes[UM_OPERATOR])
166                         {
167                                 // Channel is banned.
168                                 user->WriteServ( "384 %s %s :Cannot join channel, CBANed (%s)", user->nick, cname, iter->reason.c_str());
169                                 ServerInstance->WriteOpers("*** %s tried to join %s which is CBANed (%s)", user->nick, cname, iter->reason.c_str());
170                                 return 1;
171                         }
172                 }
173                 return 0;
174         }
175         
176         virtual void OnSyncOtherMetaData(Module* proto, void* opaque, bool displayable)
177         {
178                 for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
179                 {
180                         proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "cban", EncodeCBan(*iter));
181                 }
182         }
183         
184         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
185         {
186                 if((target_type == TYPE_OTHER) && (extname == "cban"))
187                 {
188                         cbans.push_back(DecodeCBan(extdata));
189                         std::sort(cbans.begin(), cbans.end(), CBanComp);
190                 }
191         }
192
193         virtual ~ModuleCBan()
194         {
195         }
196         
197         virtual Version GetVersion()
198         {
199                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
200         }
201
202         std::string EncodeCBan(const CBan &ban)
203         {
204                 std::ostringstream stream;      
205                 stream << ban.chname << " " << ban.set_by << " " << ban.set_on << " " << ban.length << " " << ban.reason;
206                 return stream.str();    
207         }
208
209         CBan DecodeCBan(const std::string &data)
210         {
211                 CBan res;
212                 std::istringstream stream(data);
213                 stream >> res.chname;
214                 stream >> res.set_by;
215                 stream >> res.set_on;
216                 stream >> res.length;
217                 res.reason = stream.str();
218         
219                 return res;
220         }
221
222         void ExpireBans()
223         {
224                 bool go_again = true;
225
226                 while (go_again)
227                 {
228                         go_again = false;
229         
230                         for (cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
231                         {
232                                 /* 0 == permanent, don't mess with them! -- w00t */
233                                 if (iter->length != 0)
234                                 {
235                                         if (iter->set_on + iter->length <= ServerInstance->Time())
236                                         {
237                                                 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);
238                                                 cbans.erase(iter);
239                                                 go_again = true;
240                                         }
241                                 }
242         
243                                 if (go_again == true)
244                                         break;
245                         }
246                 }
247         }
248 };
249
250 class ModuleCBanFactory : public ModuleFactory
251 {
252  public:
253         ModuleCBanFactory()
254         {
255         }
256         
257         ~ModuleCBanFactory()
258         {
259         }
260         
261         virtual Module * CreateModule(InspIRCd* Me)
262         {
263                 return new ModuleCBan(Me);
264         }
265         
266 };
267
268
269 extern "C" DllExport void * init_module( void )
270 {
271         return new ModuleCBanFactory;
272 }