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