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