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