]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/xline.h
This commit is untested and breaks a lot of xline stuff. work in progress.
[user/henk/code/inspircd.git] / include / xline.h
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 #ifndef __XLINE_H
15 #define __XLINE_H
16
17 // include the common header files
18
19 #include <string>
20 #include <deque>
21 #include <vector>
22 #include "users.h"
23 #include "channels.h"
24
25 const int APPLY_GLINES          = 1;
26 const int APPLY_KLINES          = 2;
27 const int APPLY_QLINES          = 4;
28 const int APPLY_ZLINES          = 8;
29 const int APPLY_PERM_ONLY       = 16;
30 const int APPLY_ALL             = APPLY_GLINES | APPLY_KLINES | APPLY_QLINES | APPLY_ZLINES;
31
32 /** XLine is the base class for ban lines such as G lines and K lines.
33  */
34 class CoreExport XLine : public classbase
35 {
36  protected:
37
38         InspIRCd* ServerInstance;
39         void DefaultApply(User* u, char line);
40
41  public:
42
43         /** Create an XLine.
44          * @param s_time The set time
45          * @param d The duration of the xline
46          * @param src The sender of the xline
47          * @param re The reason of the xline
48          */
49         XLine(InspIRCd* Instance, time_t s_time, long d, const char* src, const char* re)
50                 : ServerInstance(Instance), set_time(s_time), duration(d)
51         {
52                 source = strdup(src);
53                 reason = strdup(re);
54                 expiry = set_time + duration;
55         }
56
57         /** Destructor
58          */
59         virtual ~XLine()
60         {
61                 free(reason);
62                 free(source);
63         }
64
65         /** Returns true whether or not the given user is covered by this line.
66          */
67         virtual bool Matches(User *u) = 0;
68
69         virtual bool Matches(const std::string &str);
70
71         virtual void Apply(User* u);
72
73         virtual void Unset() { };
74
75         virtual void DisplayExpiry() = 0;
76
77         /** The time the line was added.
78          */
79         time_t set_time;
80         
81         /** The duration of the ban, or 0 if permenant
82          */
83         long duration;
84         
85         /** Source of the ban. This can be a servername or an oper nickname
86          */
87         char* source;
88         
89         /** Reason for the ban
90          */
91         char* reason;
92
93         /** Expiry time
94          */
95         time_t expiry;
96
97         /** Q, K, etc. Don't change this. Constructors set it.
98          */
99         char type;
100 };
101
102 /** KLine class
103  */
104 class CoreExport KLine : public XLine
105 {
106   public:
107         /** Create a K-Line.
108          * @param s_time The set time
109          * @param d The duration of the xline
110          * @param src The sender of the xline
111          * @param re The reason of the xline
112          * @param ident Ident to match
113          * @param host Host to match
114          */
115         KLine(InspIRCd* Instance, time_t s_time, long d, const char* src, const char* re, const char* ident, const char* host) : XLine(Instance, s_time, d, src, re)
116         {
117                 identmask = strdup(ident);
118                 hostmask = strdup(host);
119                 type = 'K';
120         }
121
122         /** Destructor
123          */
124         ~KLine()
125         {
126                 free(identmask);
127                 free(hostmask);
128         }
129
130         virtual bool Matches(User *u);
131
132         virtual void Apply(User* u);
133
134         virtual void DisplayExpiry();
135
136         /** Ident mask
137          */
138         char* identmask;
139         /** Host mask
140          */
141         char* hostmask;
142 };
143
144 /** GLine class
145  */
146 class CoreExport GLine : public XLine
147 {
148   public:
149         /** Create a G-Line.
150          * @param s_time The set time
151          * @param d The duration of the xline
152          * @param src The sender of the xline
153          * @param re The reason of the xline
154          * @param ident Ident to match
155          * @param host Host to match
156          */
157         GLine(InspIRCd* Instance, time_t s_time, long d, const char* src, const char* re, const char* ident, const char* host) : XLine(Instance, s_time, d, src, re)
158         {
159                 identmask = strdup(ident);
160                 hostmask = strdup(host);
161                 type = 'G';
162         }
163
164         /** Destructor
165          */
166         ~GLine()
167         {
168                 free(identmask);
169                 free(hostmask);
170         }
171
172         virtual bool Matches(User *u);
173
174         virtual void Apply(User* u);
175
176         virtual void DisplayExpiry();
177
178         /** Ident mask
179          */
180         char* identmask;
181         /** Host mask
182          */
183         char* hostmask;
184 };
185
186 /** ELine class
187  */
188 class CoreExport ELine : public XLine
189 {
190   public:
191         /** Create an E-Line.
192          * @param s_time The set time
193          * @param d The duration of the xline
194          * @param src The sender of the xline
195          * @param re The reason of the xline
196          * @param ident Ident to match
197          * @param host Host to match
198          */
199         ELine(InspIRCd* Instance, time_t s_time, long d, const char* src, const char* re, const char* ident, const char* host) : XLine(Instance, s_time, d, src, re)
200         {
201                 identmask = strdup(ident);
202                 hostmask = strdup(host);
203                 type = 'E';
204         }
205
206         ~ELine()
207         {
208                 free(identmask);
209                 free(hostmask);
210         }
211
212         virtual bool Matches(User *u);
213
214         virtual void Unset();
215
216         virtual void DisplayExpiry();
217
218         /** Ident mask
219          */
220         char* identmask;
221         /** Host mask
222          */
223         char* hostmask;
224 };
225
226 /** ZLine class
227  */
228 class CoreExport ZLine : public XLine
229 {
230   public:
231         /** Create a Z-Line.
232          * @param s_time The set time
233          * @param d The duration of the xline
234          * @param src The sender of the xline
235          * @param re The reason of the xline
236          * @param ip IP to match
237          */
238         ZLine(InspIRCd* Instance, time_t s_time, long d, const char* src, const char* re, const char* ip) : XLine(Instance, s_time, d, src, re)
239         {
240                 ipaddr = strdup(ip);
241                 type = 'Z';
242         }
243
244         /** Destructor
245          */
246         ~ZLine()
247         {
248                 free(ipaddr);
249         }
250
251         virtual bool Matches(User *u);
252
253         virtual bool Matches(const std::string &str);
254
255         virtual void Apply(User* u);
256
257         virtual void DisplayExpiry();
258
259         /** IP mask
260          */
261         char* ipaddr;
262 };
263
264 /** QLine class
265  */
266 class CoreExport QLine : public XLine
267 {
268   public:
269         /** Create a G-Line.
270          * @param s_time The set time
271          * @param d The duration of the xline
272          * @param src The sender of the xline
273          * @param re The reason of the xline
274          * @param nickname Nickname to match
275          */
276         QLine(InspIRCd* Instance, time_t s_time, long d, const char* src, const char* re, const char* nickname) : XLine(Instance, s_time, d, src, re)
277         {
278                 nick = strdup(nickname);
279                 type = 'Q';
280         }
281
282         /** Destructor
283          */
284         ~QLine()
285         {
286                 free(nick);
287
288         }
289         virtual bool Matches(User *u);
290
291         virtual bool Matches(const std::string &str);
292
293         virtual void Apply(User* u);
294
295         virtual void DisplayExpiry();
296
297         /** Nickname mask
298          */
299         char* nick;
300 };
301
302 /* Required forward declarations
303  */
304 class ServerConfig;
305 class InspIRCd;
306
307 /** Initialize x line
308  */
309 bool InitXLine(ServerConfig* conf, const char* tag);
310
311 /** Done adding zlines from the config
312  */
313 bool DoneZLine(ServerConfig* conf, const char* tag);
314 /** Done adding qlines from the config
315  */
316 bool DoneQLine(ServerConfig* conf, const char* tag);
317 /** Done adding klines from the config
318  */
319 bool DoneKLine(ServerConfig* conf, const char* tag);
320 /** Done adding elines from the config
321  */
322 bool DoneELine(ServerConfig* conf, const char* tag);
323
324 /** Add a config-defined zline
325  */
326 bool DoZLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
327 /** Add a config-defined qline
328  */
329 bool DoQLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
330 /** Add a config-defined kline
331  */
332 bool DoKLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
333 /** Add a config-defined eline
334  */
335 bool DoELine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
336
337 /** Contains an ident and host split into two strings
338  */
339 typedef std::pair<std::string, std::string> IdentHostPair;
340
341 /** XLineManager is a class used to manage glines, klines, elines, zlines and qlines.
342  */
343 class CoreExport XLineManager
344 {
345  protected:
346         /** The owner/creator of this class
347          */
348         InspIRCd* ServerInstance;
349
350         /** This functor is used by the std::sort() function to keep all lines in order
351          */
352         static bool XSortComparison (const XLine *one, const XLine *two);
353
354         /** Used to hold XLines which have not yet been applied.
355          */
356         std::vector<XLine *> pending_lines;
357
358         std::vector<XLine *> active_lines;
359
360  public:
361
362         std::map<std::string, ELine *> elines;
363
364         /** Constructor
365          * @param Instance A pointer to the creator object
366          */
367         XLineManager(InspIRCd* Instance);
368
369         /** Split an ident and host into two seperate strings.
370          * This allows for faster matching.
371          */
372         IdentHostPair IdentSplit(const std::string &ident_and_host);
373
374         /** Add a new GLine
375          * @param duration The duration of the line
376          * @param source The source of the line
377          * @param reason The reason for the line
378          * @param hostmask The hostmask
379          * @return True if the line was added successfully
380          */
381         bool AddGLine(long duration, const char* source, const char* reason, const char* hostmask);
382
383         /** Add a new QLine
384          * @param duration The duration of the line
385          * @param source The source of the line
386          * @param reason The reason for the line
387          * @param nickname The nickmask
388          * @return True if the line was added successfully
389          */
390         bool AddQLine(long duration, const char* source, const char* reason, const char* nickname);
391
392         /** Add a new ZLine
393          * @param duration The duration of the line
394          * @param source The source of the line
395          * @param reason The reason for the line
396          * @param ipaddr The IP mask
397          * @return True if the line was added successfully
398          */
399         bool AddZLine(long duration, const char* source, const char* reason, const char* ipaddr);
400
401         /** Add a new KLine
402          * @param duration The duration of the line
403          * @param source The source of the line
404          * @param reason The reason for the line
405          * @param hostmask The hostmask
406          * @return True if the line was added successfully
407          */
408         bool AddKLine(long duration, const char* source, const char* reason, const char* hostmask);
409
410         /** Add a new ELine
411          * @param duration The duration of the line
412          * @param source The source of the line
413          * @param reason The reason for the line
414          * @param hostmask The hostmask
415          * @return True if the line was added successfully
416          */
417         bool AddELine(long duration, const char* source, const char* reason, const char* hostmask);
418
419         /** Delete a GLine
420          * @param hostmask The host to remove
421          * @param type Type of line to remove
422          * @param simulate If this is true, don't actually remove the line, just return
423          * @return True if the line was deleted successfully
424          */
425         bool DelLine(const char* hostmask, char type, bool simulate = false);
426
427         /** Check if a nickname matches a QLine
428          * @return nick The nick to check against
429          * @return The reason for the line if there is a match, or NULL if there is no match
430          */
431         QLine* matches_qline(const char* nick);
432
433         /** Check if a hostname matches a GLine
434          * @param user The user to check against
435          * @return The reason for the line if there is a match, or NULL if there is no match
436          */
437         GLine* matches_gline(User* user);
438
439         /** Check if a user's IP matches a ZLine
440          * @param user The user to check against
441          * @return The reason for the line if there is a match, or NULL if there is no match
442          */
443         ZLine* matches_zline(User *user);
444
445         /** Check if a hostname matches a KLine
446          * @param user The user to check against
447          * @return The reason for the line if there is a match, or NULL if there is no match
448          */
449         KLine* matches_kline(User* user);
450
451         /** Check if a hostname matches a ELine
452          * @param user The user to check against
453          * @return The reason for the line if there is a match, or NULL if there is no match
454          */
455         ELine* matches_exception(User* user);
456
457         /** Expire any lines that should be expired.
458          */
459         void expire_lines();
460
461         /** Apply any new lines that are pending to be applied
462          */
463         void ApplyLines();
464
465         /** Handle /STATS K
466          * @param user The username making the query
467          * @param results The string_list to receive the results
468          */
469         void stats_k(User* user, string_list &results);
470
471         /** Handle /STATS G
472          * @param user The username making the query
473          * @param results The string_list to receive the results
474          */
475         void stats_g(User* user, string_list &results);
476
477         /** Handle /STATS Q
478          * @param user The username making the query
479          * @param results The string_list to receive the results
480          */
481         void stats_q(User* user, string_list &results);
482
483         /** Handle /STATS Z
484          * @param user The username making the query
485          * @param results The string_list to receive the results
486          */
487         void stats_z(User* user, string_list &results);
488
489         /** Handle /STATS E
490          * @param user The username making the query
491          * @param results The string_list to receive the results
492          */
493         void stats_e(User* user, string_list &results);
494
495         /** Change creation time of a GLine
496          * @param host The hostname to change
497          * @param create_Time The new creation time
498          */
499         void gline_set_creation_time(const char* host, time_t create_time);
500
501         /** Change creation time of a QLine
502          * @param nick The nickmask to change
503          * @param create_Time The new creation time
504          */
505         void qline_set_creation_time(const char* nick, time_t create_time);
506
507         /** Change creation time of a ZLine
508          * @param ip The ipmask to change
509          * @param create_Time The new creation time
510          */
511         void zline_set_creation_time(const char* ip, time_t create_time);
512
513         /** Change creation time of a ELine
514          * @param host The hostname to change
515          * @param create_Time The new creation time
516          */
517         void eline_set_creation_time(const char* host, time_t create_time);
518 };
519
520 #endif
521