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