]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/xline.h
Fix (?) stats chars
[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 const char* Displayable() = 0;
83
84         virtual void OnAdd() { }
85
86         /** The time the line was added.
87          */
88         time_t set_time;
89         
90         /** The duration of the ban, or 0 if permenant
91          */
92         long duration;
93         
94         /** Source of the ban. This can be a servername or an oper nickname
95          */
96         char* source;
97         
98         /** Reason for the ban
99          */
100         char* reason;
101
102         /** Expiry time
103          */
104         time_t expiry;
105
106         /** Q, K, etc. Don't change this. Constructors set it.
107          */
108         const char type;
109 };
110
111 /** KLine class
112  */
113 class CoreExport KLine : public XLine
114 {
115   public:
116         /** Create a K-Line.
117          * @param s_time The set time
118          * @param d The duration of the xline
119          * @param src The sender of the xline
120          * @param re The reason of the xline
121          * @param ident Ident to match
122          * @param host Host to match
123          */
124         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')
125         {
126                 identmask = strdup(ident);
127                 hostmask = strdup(host);
128                 matchtext = this->identmask;
129                 matchtext.append("@").append(this->hostmask);
130         }
131
132         /** Destructor
133          */
134         ~KLine()
135         {
136                 free(identmask);
137                 free(hostmask);
138         }
139
140         virtual bool Matches(User *u);
141
142         virtual bool Matches(const std::string &str);
143
144         virtual bool MatchesLiteral(const std::string &str);
145
146         virtual void Apply(User* u);
147
148         virtual void DisplayExpiry();
149
150         virtual const char* Displayable();
151
152         /** Ident mask
153          */
154         char* identmask;
155         /** Host mask
156          */
157         char* hostmask;
158
159         std::string matchtext;
160 };
161
162 /** GLine class
163  */
164 class CoreExport GLine : public XLine
165 {
166   public:
167         /** Create a G-Line.
168          * @param s_time The set time
169          * @param d The duration of the xline
170          * @param src The sender of the xline
171          * @param re The reason of the xline
172          * @param ident Ident to match
173          * @param host Host to match
174          */
175         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')
176         {
177                 identmask = strdup(ident);
178                 hostmask = strdup(host);
179                 matchtext = this->identmask;
180                 matchtext.append("@").append(this->hostmask);
181         }
182
183         /** Destructor
184          */
185         ~GLine()
186         {
187                 free(identmask);
188                 free(hostmask);
189         }
190
191         virtual bool Matches(User *u);
192
193         virtual bool Matches(const std::string &str);
194
195         virtual bool MatchesLiteral(const std::string &str);
196
197         virtual void Apply(User* u);
198
199         virtual void DisplayExpiry();
200
201         virtual const char* Displayable();
202
203         /** Ident mask
204          */
205         char* identmask;
206         /** Host mask
207          */
208         char* hostmask;
209
210         std::string matchtext;
211 };
212
213 /** ELine class
214  */
215 class CoreExport ELine : public XLine
216 {
217   public:
218         /** Create an E-Line.
219          * @param s_time The set time
220          * @param d The duration of the xline
221          * @param src The sender of the xline
222          * @param re The reason of the xline
223          * @param ident Ident to match
224          * @param host Host to match
225          */
226         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')
227         {
228                 identmask = strdup(ident);
229                 hostmask = strdup(host);
230                 matchtext = this->identmask;
231                 matchtext.append("@").append(this->hostmask);
232         }
233
234         ~ELine()
235         {
236                 free(identmask);
237                 free(hostmask);
238         }
239
240         virtual bool Matches(User *u);
241
242         virtual bool Matches(const std::string &str);
243
244         virtual bool MatchesLiteral(const std::string &str);
245
246         virtual void Unset();
247
248         virtual void DisplayExpiry();
249
250         virtual void OnAdd();
251
252         virtual const char* Displayable();
253
254         /** Ident mask
255          */
256         char* identmask;
257         /** Host mask
258          */
259         char* hostmask;
260
261         std::string matchtext;
262 };
263
264 /** ZLine class
265  */
266 class CoreExport ZLine : public XLine
267 {
268   public:
269         /** Create a Z-Line.
270          * @param s_time The set time
271          * @param d The duration of the xline
272          * @param src The sender of the xline
273          * @param re The reason of the xline
274          * @param ip IP to match
275          */
276         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')
277         {
278                 ipaddr = strdup(ip);
279         }
280
281         /** Destructor
282          */
283         ~ZLine()
284         {
285                 free(ipaddr);
286         }
287
288         virtual bool Matches(User *u);
289
290         virtual bool Matches(const std::string &str);
291
292         virtual bool MatchesLiteral(const std::string &str);
293
294         virtual void Apply(User* u);
295
296         virtual void DisplayExpiry();
297
298         virtual const char* Displayable();
299
300         /** IP mask
301          */
302         char* ipaddr;
303 };
304
305 /** QLine class
306  */
307 class CoreExport QLine : public XLine
308 {
309   public:
310         /** Create a G-Line.
311          * @param s_time The set time
312          * @param d The duration of the xline
313          * @param src The sender of the xline
314          * @param re The reason of the xline
315          * @param nickname Nickname to match
316          */
317         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')
318         {
319                 nick = strdup(nickname);
320         }
321
322         /** Destructor
323          */
324         ~QLine()
325         {
326                 free(nick);
327
328         }
329         virtual bool Matches(User *u);
330
331         virtual bool Matches(const std::string &str);
332
333         virtual bool MatchesLiteral(const std::string &str);
334
335         virtual void Apply(User* u);
336
337         virtual void DisplayExpiry();
338
339         virtual const char* Displayable();
340
341         /** Nickname mask
342          */
343         char* nick;
344 };
345
346 /** Contains an ident and host split into two strings
347  */
348 typedef std::pair<std::string, std::string> IdentHostPair;
349
350
351 class CoreExport XLineFactory
352 {
353  protected:
354
355         InspIRCd* ServerInstance;
356         const char type;
357
358  public:
359
360         XLineFactory(InspIRCd* Instance, const char t) : ServerInstance(Instance), type(t) { }
361         
362         virtual const char GetType() { return type; }
363
364         virtual XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask) = 0;
365
366         virtual ~XLineFactory() { }
367 };
368
369 /* Required forward declarations
370  */
371 class ServerConfig;
372 class InspIRCd;
373
374 class GLineFactory;
375 class ELineFactory;
376 class QLineFactory;
377 class ZLineFactory;
378 class KLineFactory;
379
380 /** XLineManager is a class used to manage glines, klines, elines, zlines and qlines.
381  */
382 class CoreExport XLineManager
383 {
384  protected:
385         /** The owner/creator of this class
386          */
387         InspIRCd* ServerInstance;
388
389         /** This functor is used by the std::sort() function to keep all lines in order
390          */
391         static bool XSortComparison (const XLine *one, const XLine *two);
392
393         /** Used to hold XLines which have not yet been applied.
394          */
395         std::vector<XLine *> pending_lines;
396
397         std::vector<XLine *> active_lines;
398
399         std::map<char, XLineFactory*> line_factory;
400
401         GLineFactory* GFact;
402         ELineFactory* EFact;
403         KLineFactory* KFact;
404         QLineFactory* QFact;
405         ZLineFactory* ZFact;
406
407         unsigned int PermLines;
408
409  public:
410
411         std::map<char, std::map<std::string, XLine *> > lookup_lines;
412
413         /** Constructor
414          * @param Instance A pointer to the creator object
415          */
416         XLineManager(InspIRCd* Instance);
417
418         ~XLineManager();
419
420         /** Split an ident and host into two seperate strings.
421          * This allows for faster matching.
422          */
423         IdentHostPair IdentSplit(const std::string &ident_and_host);
424
425         /** Checks what users match a given list of ELines and sets their ban exempt flag accordingly.
426          * @param ELines List of E:Lines to check.
427          */
428         void CheckELines(std::map<std::string, XLine *> &ELines);
429
430         /** Add a new GLine
431          * @param line The line to be added
432          * @param user The user adding the line or NULL for the local server
433          * @return True if the line was added successfully
434          */
435         bool AddLine(XLine* line, User* user);
436
437         /** Delete a GLine
438          * @param hostmask The xline-specific string identifying the line, e.g. "*@foo"
439          * @param type The type of xline
440          * @param user The user removing the line or NULL if its the local server
441          * @param simulate If this is true, don't actually remove the line, just return
442          * @return True if the line was deleted successfully
443          */
444         bool DelLine(const char* hostmask, char type, User* user, bool simulate = false);
445
446         /** Registers an xline factory.
447          * An xline factory is a class which when given a particular xline type,
448          * will generate a new XLine specialized to that type. For example if you
449          * pass the XLineFactory that handles glines some data it will return a
450          * pointer to a GLine, polymorphically represented as XLine. This is used where
451          * you do not know the full details of the item you wish to create, e.g. in a 
452          * server protocol module like m_spanningtree, when you receive xlines from other
453          * servers.
454          */
455         bool RegisterFactory(XLineFactory* xlf);
456
457         /** Unregisters an xline factory
458          */
459         bool UnregisterFactory(XLineFactory* xlf);
460
461         /** Get the XLineFactory for a specific type.
462          * Returns NULL if there is no known handler for this xline type
463          */
464         XLineFactory* GetFactory(const char type);
465
466         /** Check if a nickname matches a QLine
467          * @return nick The nick to check against
468          * @return The reason for the line if there is a match, or NULL if there is no match
469          */
470         QLine* matches_qline(const char* nick);
471
472         /** Check if a hostname matches a GLine
473          * @param user The user to check against
474          * @return The reason for the line if there is a match, or NULL if there is no match
475          */
476         GLine* matches_gline(User* user);
477
478         /** Check if a user's IP matches a ZLine
479          * @param user The user to check against
480          * @return The reason for the line if there is a match, or NULL if there is no match
481          */
482         ZLine* matches_zline(User *user);
483
484         /** Check if a hostname matches a KLine
485          * @param user The user to check against
486          * @return The reason for the line if there is a match, or NULL if there is no match
487          */
488         KLine* matches_kline(User* user);
489
490         /** Check if a hostname matches a ELine
491          * @param user The user to check against
492          * @return The reason for the line if there is a match, or NULL if there is no match
493          */
494         ELine* matches_exception(User* user);
495
496         /** Expire any lines that should be expired.
497          */
498         void expire_lines();
499
500         /** Apply any new lines that are pending to be applied
501          */
502         void ApplyLines();
503
504         /** Handle /STATS for a given type.
505          * @param numeric The numeric to give to each result line
506          * @param user The username making the query
507          * @param results The string_list to receive the results
508          */
509         void InvokeStats(const char type, int numeric, 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 class CoreExport GLineFactory : public XLineFactory
543 {
544  public:
545         GLineFactory(InspIRCd* Instance) : XLineFactory(Instance, 'G') { }
546
547         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
548         {
549                 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
550                 return new GLine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
551         }
552 };
553
554 class CoreExport ELineFactory : public XLineFactory
555 {
556  public:
557         ELineFactory(InspIRCd* Instance) : XLineFactory(Instance, 'E') { }
558
559         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
560         {
561                 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
562                 return new ELine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
563         }
564 };
565
566 class CoreExport KLineFactory : public XLineFactory
567 {
568  public:
569         KLineFactory(InspIRCd* Instance) : XLineFactory(Instance, 'K') { }
570
571         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
572         {
573                 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
574                 return new KLine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
575         }
576 };
577
578 class CoreExport QLineFactory : public XLineFactory
579 {
580  public:
581         QLineFactory(InspIRCd* Instance) : XLineFactory(Instance, 'Q') { }
582
583         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
584         {
585                 return new QLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
586         }
587 };
588
589 class CoreExport ZLineFactory : public XLineFactory
590 {
591  public:
592         ZLineFactory(InspIRCd* Instance) : XLineFactory(Instance, 'Z') { }
593
594         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
595         {
596                 return new ZLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
597         }
598 };
599
600 #endif