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