]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cban.cpp
f43f26bb8002b124527c9ae6b9871450a7b29598
[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         void Implements(char* List)
135         {
136                 List[I_OnUserPreJoin] = List[I_OnSyncOtherMetaData] = List[I_OnDecodeMetaData] = List[I_OnStats] = 1;
137         }
138         
139         virtual int OnStats(char symbol, User* user, string_list &results)
140         {
141                 ExpireBans();
142         
143                 if(symbol == 'C')
144                 {
145                         for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
146                         {
147                                 unsigned long remaining = (iter->set_on + iter->length) - ServerInstance->Time();
148                                 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);
149                         }
150                 }
151                 
152                 return 0;
153         }
154
155         virtual int OnUserPreJoin(User *user, Channel *chan, const char *cname, std::string &privs)
156         {
157                 ExpireBans();
158         
159                 /* check cbans in here, and apply as necessary. */
160                 for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
161                 {
162                         if(iter->chname == cname && !user->modes[UM_OPERATOR])
163                         {
164                                 // Channel is banned.
165                                 user->WriteServ( "384 %s %s :Cannot join channel, CBANed (%s)", user->nick, cname, iter->reason.c_str());
166                                 ServerInstance->WriteOpers("*** %s tried to join %s which is CBANed (%s)", user->nick, cname, iter->reason.c_str());
167                                 return 1;
168                         }
169                 }
170                 return 0;
171         }
172         
173         virtual void OnSyncOtherMetaData(Module* proto, void* opaque, bool displayable)
174         {
175                 for(cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
176                 {
177                         proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "cban", EncodeCBan(*iter));
178                 }
179         }
180         
181         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
182         {
183                 if((target_type == TYPE_OTHER) && (extname == "cban"))
184                 {
185                         cbans.push_back(DecodeCBan(extdata));
186                         std::sort(cbans.begin(), cbans.end(), CBanComp);
187                 }
188         }
189
190         virtual ~ModuleCBan()
191         {
192         }
193         
194         virtual Version GetVersion()
195         {
196                 return Version(1, 1, 0, 1, VF_COMMON | VF_VENDOR, API_VERSION);
197         }
198
199         std::string EncodeCBan(const CBan &ban)
200         {
201                 std::ostringstream stream;      
202                 stream << ban.chname << " " << ban.set_by << " " << ban.set_on << " " << ban.length << " :" << ban.reason;
203                 return stream.str();
204         }
205
206         CBan DecodeCBan(const std::string &data)
207         {
208                 CBan res;
209                 int set_on;
210                 irc::tokenstream tokens(data);
211                 tokens.GetToken(res.chname);
212                 tokens.GetToken(res.set_by);
213                 tokens.GetToken(set_on);
214                 res.set_on = set_on;
215                 tokens.GetToken(res.length);
216                 tokens.GetToken(res.reason);
217                 return res;
218         }
219
220         void ExpireBans()
221         {
222                 bool go_again = true;
223
224                 while (go_again)
225                 {
226                         go_again = false;
227         
228                         for (cbanlist::iterator iter = cbans.begin(); iter != cbans.end(); iter++)
229                         {
230                                 /* 0 == permanent, don't mess with them! -- w00t */
231                                 if (iter->length != 0)
232                                 {
233                                         if (iter->set_on + iter->length <= ServerInstance->Time())
234                                         {
235                                                 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());
236                                                 cbans.erase(iter);
237                                                 go_again = true;
238                                         }
239                                 }
240         
241                                 if (go_again == true)
242                                         break;
243                         }
244                 }
245         }
246 };
247
248 MODULE_INIT(ModuleCBan)
249