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