]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cban.cpp
More conversions
[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 "inspircd.h"
15 #include <algorithm>
16 #include "users.h"
17 #include "channels.h"
18 #include "modules.h"
19 #include "configreader.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->syntax = "<channel> [<duration> :<reason>]";
59         }
60
61         CmdResult Handle(const char** parameters, int pcnt, userrec *user)
62         {
63                 /* syntax: CBAN #channel time :reason goes here */
64                 /* 'time' is a human-readable timestring, like 2d3h2s. */
65
66                 if(pcnt == 1)
67                 {
68                         /* form: CBAN #channel removes a CBAN */
69                         for (cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
70                         {
71                                 if (parameters[0] == iter->chname)
72                                 {
73                                         long remaining = iter->length + ServerInstance->Time();
74                                         user->WriteServ("386 %s %s :Removed CBAN due to expire at %s (%s)", user->nick, iter->chname.c_str(), ServerInstance->TimeString(remaining).c_str(), iter->reason.c_str());
75                                         cbans.erase(iter);
76                                         break;
77                                 }
78                         }
79                 }
80                 else if (pcnt >= 2)
81                 {
82                         /* full form to add a CBAN */
83                         if (ServerInstance->IsChannel(parameters[0]))
84                         {
85                                 // parameters[0] = #channel
86                                 // parameters[1] = 1h3m2s
87                                 // parameters[2] = Tortoise abuser
88                                 long length = ServerInstance->Duration(parameters[1]);
89                                 std::string reason = (pcnt > 2) ? parameters[2] : "No reason supplied";
90                                 
91                                 cbans.push_back(CBan(parameters[0], user->nick, ServerInstance->Time(), length, reason));
92                                         
93                                 std::sort(cbans.begin(), cbans.end(), CBanComp);
94                                 
95                                 if(length > 0)
96                                 {
97                                         user->WriteServ("385 %s %s :Added %lu second channel ban (%s)", user->nick, parameters[0], length, reason.c_str());
98                                         ServerInstance->WriteOpers("*** %s added %lu second channel ban on %s (%s)", user->nick, length, parameters[0], reason.c_str());
99                                 }
100                                 else
101                                 {
102                                         user->WriteServ("385 %s %s :Added permanent channel ban (%s)", user->nick, parameters[0], reason.c_str());
103                                         ServerInstance->WriteOpers("*** %s added permanent channel ban on %s (%s)", user->nick, parameters[0], reason.c_str());
104                                 }
105                         }
106                         else
107                         {
108                                 user->WriteServ("403 %s %s :Invalid channel name", user->nick, parameters[0]);
109                                 return CMD_FAILURE;
110                         }
111                 }
112
113                 /* we want this routed! */
114                 return CMD_SUCCESS;
115         }
116 };
117
118 bool CBanComp(const CBan &ban1, const CBan &ban2)
119 {
120         return ((ban1.set_on + ban1.length) < (ban2.set_on + ban2.length));
121 }
122
123 class ModuleCBan : public Module
124 {
125         cmd_cban* mycommand;
126         
127
128  public:
129         ModuleCBan(InspIRCd* Me) : Module(Me)
130         {
131                 
132                 mycommand = new cmd_cban(Me);
133                 ServerInstance->AddCommand(mycommand);
134         }
135
136         void Implements(char* List)
137         {
138                 List[I_OnUserPreJoin] = List[I_OnSyncOtherMetaData] = List[I_OnDecodeMetaData] = List[I_OnStats] = 1;
139         }
140         
141         virtual int OnStats(char symbol, userrec* user, string_list &results)
142         {
143                 ExpireBans();
144         
145                 if(symbol == 'C')
146                 {
147                         for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
148                         {
149                                 unsigned long remaining = (iter->set_on + iter->length) - ServerInstance->Time();
150                                 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);
151                         }
152                 }
153                 
154                 return 0;
155         }
156
157         virtual int OnUserPreJoin(userrec *user, chanrec *chan, const char *cname, std::string &privs)
158         {
159                 ExpireBans();
160         
161                 /* check cbans in here, and apply as necessary. */
162                 for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
163                 {
164                         if(iter->chname == cname && !user->modes[UM_OPERATOR])
165                         {
166                                 // Channel is banned.
167                                 user->WriteServ( "384 %s %s :Cannot join channel, CBANed (%s)", user->nick, cname, iter->reason.c_str());
168                                 ServerInstance->WriteOpers("*** %s tried to join %s which is CBANed (%s)", user->nick, cname, iter->reason.c_str());
169                                 return 1;
170                         }
171                 }
172                 return 0;
173         }
174         
175         virtual void OnSyncOtherMetaData(Module* proto, void* opaque, bool displayable)
176         {
177                 for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
178                 {
179                         proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "cban", EncodeCBan(*iter));
180                 }
181         }
182         
183         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
184         {
185                 if((target_type == TYPE_OTHER) && (extname == "cban"))
186                 {
187                         cbans.push_back(DecodeCBan(extdata));
188                         std::sort(cbans.begin(), cbans.end(), CBanComp);
189                 }
190         }
191
192         virtual ~ModuleCBan()
193         {
194         }
195         
196         virtual Version GetVersion()
197         {
198                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
199         }
200
201         std::string EncodeCBan(const CBan &ban)
202         {
203                 std::ostringstream stream;      
204                 stream << ban.chname << " " << ban.set_by << " " << ban.set_on << " " << ban.length << " :" << ban.reason;
205                 return stream.str();
206         }
207
208         CBan DecodeCBan(const std::string &data)
209         {
210                 CBan res;
211                 int set_on;
212                 irc::tokenstream tokens(data);
213                 tokens.GetToken(res.chname);
214                 tokens.GetToken(res.set_by);
215                 tokens.GetToken(set_on);
216                 res.set_on = set_on;
217                 tokens.GetToken(res.length);
218                 tokens.GetToken(res.reason);
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 on %s expired", iter->length, iter->chname.c_str(), iter->reason.c_str(), ServerInstance->TimeString(iter->set_on).c_str());
238                                                 cbans.erase(iter);
239                                                 go_again = true;
240                                         }
241                                 }
242         
243                                 if (go_again == true)
244                                         break;
245                         }
246                 }
247         }
248 };
249
250 MODULE_INIT(ModuleCBan);
251