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