]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/xline.h
I gots a better fix :p
[user/henk/code/inspircd.git] / include / xline.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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 <string>
18 //#include <deque>
19 //#include <vector>
20
21 /** XLine is the base class for ban lines such as G lines and K lines.
22  * Modules may derive from this, and their xlines will automatically be
23  * handled as expected by any protocol modules (e.g. m_spanningtree will
24  * propogate them using AddLine). The process of translating a type+pattern
25  * to a known line type is done by means of an XLineFactory object (see
26  * below).
27  */
28 class CoreExport XLine : public classbase
29 {
30  protected:
31
32         /** Creator */
33         InspIRCd* ServerInstance;
34
35         /** Default 'apply' action. Quits the user.
36          * @param u User to apply the line against
37          * @param line The line typed, used for display purposes in the quit message
38          * @param bancache If true, the user will be added to the bancache if they match. Else not.
39          */
40         void DefaultApply(User* u, const std::string &line, bool bancache);
41
42  public:
43
44         /** Create an XLine.
45          * @param s_time The set time
46          * @param d The duration of the xline
47          * @param src The sender of the xline
48          * @param re The reason of the xline
49          * @param t The line type, should be set by the derived class constructor
50          */
51         XLine(InspIRCd* Instance, time_t s_time, long d, const char* src, const char* re, const std::string &t)
52                 : ServerInstance(Instance), set_time(s_time), duration(d), type(t)
53         {
54                 source = strdup(src);
55                 reason = strdup(re);
56                 expiry = set_time + duration;
57         }
58
59         /** Destructor
60          */
61         virtual ~XLine()
62         {
63                 free(reason);
64                 free(source);
65         }
66
67         /** Change creation time of an xline. Updates expiry
68          * to be after the creation time
69          */
70         virtual void SetCreateTime(time_t created)
71         {
72                 set_time = created;
73                 expiry = created + duration;
74         }
75
76         /** Returns true whether or not the given user is covered by this line.
77          * @param u The user to match against. The mechanics of the match
78          * are defined by the derived class.
79          * @return True if there is a match.
80          */
81         virtual bool Matches(User *u) = 0;
82
83         /** Returns true whether or not the given string is covered by this line.
84          * @param str The string to match against. The details of what must be
85          * in this string and the mechanics of the match are defined by the
86          * derived class.
87          * @return True if there is a match
88          */
89         virtual bool Matches(const std::string &str) = 0;
90
91         /** Apply a line against a user. The mechanics of what occurs when
92          * the line is applied are specific to the derived class.
93          * @param u The user to apply against
94          */
95         virtual void Apply(User* u);
96
97         /** Called when the line is unset either via expiry or
98          * via explicit removal.
99          */
100         virtual void Unset() { }
101
102         /** Called when the expiry message is to be displayed for the
103          * line. Usually a line in the form 'expiring Xline blah, set by...'
104          * see the DisplayExpiry methods of GLine, ELine etc.
105          */
106         virtual void DisplayExpiry() = 0;
107
108         /** Returns the displayable form of the pattern for this xline,
109          * e.g. '*@foo' or '*baz*'. This must always return the full pattern
110          * in a form which can be used to construct an entire derived xline,
111          * even if it is stored differently internally (e.g. GLine stores the
112          * ident and host parts seperately but will still return ident@host
113          * for its Displayable() method)
114          */
115         virtual const char* Displayable() = 0;
116
117         /** Called when the xline has just been added.
118          */
119         virtual void OnAdd() { }
120
121         /** The time the line was added.
122          */
123         time_t set_time;
124         
125         /** The duration of the ban, or 0 if permenant
126          */
127         long duration;
128         
129         /** Source of the ban. This can be a servername or an oper nickname
130          */
131         char* source;
132         
133         /** Reason for the ban
134          */
135         char* reason;
136
137         /** Expiry time. Does not contain useful data if the duration is 0.
138          */
139         time_t expiry;
140
141         /** "Q", "K", etc. Set only by derived classes constructor to the
142          * type of line this is.
143          */
144         const std::string type;
145
146         virtual bool IsBurstable();
147 };
148
149 /** KLine class
150  */
151 class CoreExport KLine : public XLine
152 {
153   public:
154
155         /** Create a K-Line.
156          * @param s_time The set time
157          * @param d The duration of the xline
158          * @param src The sender of the xline
159          * @param re The reason of the xline
160          * @param ident Ident to match
161          * @param host Host to match
162          */
163         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")
164         {
165                 identmask = strdup(ident);
166                 hostmask = strdup(host);
167                 matchtext = this->identmask;
168                 matchtext.append("@").append(this->hostmask);
169         }
170
171         /** Destructor
172          */
173         ~KLine()
174         {
175                 free(identmask);
176                 free(hostmask);
177         }
178
179         virtual bool Matches(User *u);
180
181         virtual bool Matches(const std::string &str);
182
183         virtual void Apply(User* u);
184
185         virtual void DisplayExpiry();
186
187         virtual const char* Displayable();
188
189         virtual bool IsBurstable();
190
191         /** Ident mask (ident part only)
192          */
193         char* identmask;
194         /** Host mask (host part only)
195          */
196         char* hostmask;
197
198         std::string matchtext;
199 };
200
201 /** GLine class
202  */
203 class CoreExport GLine : public XLine
204 {
205   public:
206         /** Create a G-Line.
207          * @param s_time The set time
208          * @param d The duration of the xline
209          * @param src The sender of the xline
210          * @param re The reason of the xline
211          * @param ident Ident to match
212          * @param host Host to match
213          */
214         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")
215         {
216                 identmask = strdup(ident);
217                 hostmask = strdup(host);
218                 matchtext = this->identmask;
219                 matchtext.append("@").append(this->hostmask);
220         }
221
222         /** Destructor
223          */
224         ~GLine()
225         {
226                 free(identmask);
227                 free(hostmask);
228         }
229
230         virtual bool Matches(User *u);
231
232         virtual bool Matches(const std::string &str);
233
234         virtual void Apply(User* u);
235
236         virtual void DisplayExpiry();
237
238         virtual const char* Displayable();
239
240         /** Ident mask (ident part only)
241          */
242         char* identmask;
243         /** Host mask (host part only)
244          */
245         char* hostmask;
246
247         std::string matchtext;
248 };
249
250 /** ELine class
251  */
252 class CoreExport ELine : public XLine
253 {
254   public:
255         /** Create an E-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 ident Ident to match
261          * @param host Host to match
262          */
263         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")
264         {
265                 identmask = strdup(ident);
266                 hostmask = strdup(host);
267                 matchtext = this->identmask;
268                 matchtext.append("@").append(this->hostmask);
269         }
270
271         ~ELine()
272         {
273                 free(identmask);
274                 free(hostmask);
275         }
276
277         virtual bool Matches(User *u);
278
279         virtual bool Matches(const std::string &str);
280
281         virtual void Unset();
282
283         virtual void DisplayExpiry();
284
285         virtual void OnAdd();
286
287         virtual const char* Displayable();
288
289         /** Ident mask (ident part only)
290          */
291         char* identmask;
292         /** Host mask (host part only)
293          */
294         char* hostmask;
295
296         std::string matchtext;
297 };
298
299 /** ZLine class
300  */
301 class CoreExport ZLine : public XLine
302 {
303   public:
304         /** Create a Z-Line.
305          * @param s_time The set time
306          * @param d The duration of the xline
307          * @param src The sender of the xline
308          * @param re The reason of the xline
309          * @param ip IP to match
310          */
311         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")
312         {
313                 ipaddr = strdup(ip);
314         }
315
316         /** Destructor
317          */
318         ~ZLine()
319         {
320                 free(ipaddr);
321         }
322
323         virtual bool Matches(User *u);
324
325         virtual bool Matches(const std::string &str);
326
327         virtual void Apply(User* u);
328
329         virtual void DisplayExpiry();
330
331         virtual const char* Displayable();
332
333         /** IP mask (no ident part)
334          */
335         char* ipaddr;
336 };
337
338 /** QLine class
339  */
340 class CoreExport QLine : public XLine
341 {
342   public:
343         /** Create a G-Line.
344          * @param s_time The set time
345          * @param d The duration of the xline
346          * @param src The sender of the xline
347          * @param re The reason of the xline
348          * @param nickname Nickname to match
349          */
350         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")
351         {
352                 nick = strdup(nickname);
353         }
354
355         /** Destructor
356          */
357         ~QLine()
358         {
359                 free(nick);
360
361         }
362         virtual bool Matches(User *u);
363
364         virtual bool Matches(const std::string &str);
365
366         virtual void Apply(User* u);
367
368         virtual void DisplayExpiry();
369
370         virtual const char* Displayable();
371
372         /** Nickname mask
373          */
374         char* nick;
375 };
376
377 /** Contains an ident and host split into two strings
378  */
379 typedef std::pair<std::string, std::string> IdentHostPair;
380
381 /** XLineFactory is used to generate an XLine pointer, given just the 
382  * pattern, timing information and type of line to create. This is used
383  * for example in the spanningtree module which will call an XLineFactory
384  * to create a new XLine when it is inbound on a server link, so that it
385  * does not have to know the specifics of the internals of an XLine class
386  * and/or how to call its constructor.
387  */
388 class CoreExport XLineFactory
389 {
390  protected:
391
392         InspIRCd* ServerInstance;
393         std::string type;
394
395  public:
396
397         /** Create an XLine factory
398          * @param Instance creator
399          * @param t Type of XLine this factory generates
400          */
401         XLineFactory(InspIRCd* Instance, const std::string &t) : ServerInstance(Instance), type(t) { }
402         
403         /** Return the type of XLine this factory generates
404          * @return The type of XLine this factory generates
405          */
406         virtual const std::string& GetType() { return type; }
407
408         /** Generate a specialized XLine*.
409          * @param set_time Time this line was created
410          * @param duration Duration of the line
411          * @param source The sender of the line, nickname or server
412          * @param reason The reason for the line
413          * @param xline_specific_mask The mask string for the line, specific to the XLine type being created.
414          * @return A specialized XLine class of the given type for this factory.
415          */
416         virtual XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask) = 0;
417
418         /** Destructor
419          */
420         virtual ~XLineFactory() { }
421 };
422
423 /* Required forward declarations
424  */
425 class ServerConfig;
426 class InspIRCd;
427
428 class GLineFactory;
429 class ELineFactory;
430 class QLineFactory;
431 class ZLineFactory;
432 class KLineFactory;
433
434 /** A map of xline factories
435  */
436 typedef std::map<std::string, XLineFactory*> XLineFactMap;
437
438 /** A map of XLines indexed by string
439  */
440 typedef std::map<std::string, XLine *> XLineLookup;
441
442 /** A map of XLineLookup maps indexed by string
443  */
444 typedef std::map<std::string, XLineLookup > XLineContainer;
445
446 /** An iterator in an XLineContainer
447  */
448 typedef XLineContainer::iterator ContainerIter;
449
450 /** An interator in an XLineLookup
451  */
452 typedef XLineLookup::iterator LookupIter;
453
454 /** XLineManager is a class used to manage glines, klines, elines, zlines and qlines,
455  * or any other line created by a module. It also manages XLineFactory classes which
456  * can generate a specialized XLine for use by another module.
457  */
458 class CoreExport XLineManager
459 {
460  protected:
461         /** The owner/creator of this class
462          */
463         InspIRCd* ServerInstance;
464
465         /** Used to hold XLines which have not yet been applied.
466          */
467         std::vector<XLine *> pending_lines;
468
469         /** Current xline factories
470          */
471         XLineFactMap line_factory;
472
473         /** Core xline factories for G/E/K/Q/Z lines
474          * (These generate GLine, ELine, KLine, QLine and ZLine
475          * respectively)
476          */
477         GLineFactory* GFact;
478         ELineFactory* EFact;
479         KLineFactory* KFact;
480         QLineFactory* QFact;
481         ZLineFactory* ZFact;
482
483         /** Container of all lines, this is a map of maps which
484          * allows for fast lookup for add/remove of a line, and
485          * the shortest possible timed O(n) for checking a user
486          * against a line.
487          */
488         XLineContainer lookup_lines;
489
490  public:
491
492         /** Constructor
493          * @param Instance A pointer to the creator object
494          */
495         XLineManager(InspIRCd* Instance);
496
497         /** Destructor
498          */
499         ~XLineManager();
500
501         /** Split an ident and host into two seperate strings.
502          * This allows for faster matching.
503          */
504         IdentHostPair IdentSplit(const std::string &ident_and_host);
505
506         /** Checks what users match e:lines and sets their ban exempt flag accordingly.
507          */
508         void CheckELines();
509
510         /** Get all lines of a certain type to an XLineLookup (std::map<std::string, XLine*>).
511          * NOTE: When this function runs any expired items are removed from the list before it
512          * is returned to the caller.
513          * @param The type to look up
514          * @return A list of all XLines of the given type.
515          */
516         XLineLookup* GetAll(const std::string &type);
517
518         /** Return all known types of line currently stored by the XLineManager.
519          * @return A vector containing all known line types currently stored in the main list.
520          */
521         std::vector<std::string> GetAllTypes();
522
523         /** Add a new XLine
524          * @param line The line to be added
525          * @param user The user adding the line or NULL for the local server
526          * @return True if the line was added successfully
527          */
528         bool AddLine(XLine* line, User* user);
529
530         /** Delete an XLine
531          * @param hostmask The xline-specific string identifying the line, e.g. "*@foo"
532          * @param type The type of xline
533          * @param user The user removing the line or NULL if its the local server
534          * @param simulate If this is true, don't actually remove the line, just return
535          * @return True if the line was deleted successfully
536          */
537         bool DelLine(const char* hostmask, const std::string &type, User* user, bool simulate = false);
538
539         /** Registers an xline factory.
540          * An xline factory is a class which when given a particular xline type,
541          * will generate a new XLine specialized to that type. For example if you
542          * pass the XLineFactory that handles glines some data it will return a
543          * pointer to a GLine, polymorphically represented as XLine. This is used where
544          * you do not know the full details of the item you wish to create, e.g. in a 
545          * server protocol module like m_spanningtree, when you receive xlines from other
546          * servers.
547          * @param xlf XLineFactory pointer to register
548          */
549         bool RegisterFactory(XLineFactory* xlf);
550
551         /** Unregisters an xline factory.
552          * You must do this when your module unloads.
553          * @param xlf XLineFactory pointer to unregister
554          */
555         bool UnregisterFactory(XLineFactory* xlf);
556
557         /** Get the XLineFactory for a specific type.
558          * Returns NULL if there is no known handler for this xline type.
559          * @param type The type of XLine you require the XLineFactory for
560          */
561         XLineFactory* GetFactory(const std::string &type);
562
563         /** Check if a user matches an XLine
564          * @param type The type of line to look up
565          * @param user The user to match against (what is checked is specific to the xline type)
566          * @return The reason for the line if there is a match, or NULL if there is no match
567          */
568         XLine* MatchesLine(const std::string &type, User* user);
569
570         /** Check if a pattern matches an XLine
571          * @param type The type of line to look up
572          * @param pattern A pattern string specific to the xline type
573          * @return The matching XLine if there is a match, or NULL if there is no match
574          */
575         XLine* MatchesLine(const std::string &type, const std::string &pattern);
576
577         /** Expire a line given two iterators which identify it in the main map.
578          * @param container Iterator to the first level of entries the map
579          * @param item Iterator to the second level of entries in the map
580          */
581         void ExpireLine(ContainerIter container, LookupIter item);
582
583         /** Apply any new lines that are pending to be applied.
584          * This will only apply lines in the pending_lines list, to save on
585          * CPU time.
586          */
587         void ApplyLines();
588
589         /** Handle /STATS for a given type.
590          * NOTE: Any items in the list for this particular line type which have expired
591          * will be expired and removed before the list is displayed.
592          * @param numeric The numeric to give to each result line
593          * @param user The username making the query
594          * @param results The string_list to receive the results
595          */
596         void InvokeStats(const std::string &type, int numeric, User* user, string_list &results);
597 };
598
599 /** An XLineFactory specialized to generate GLine* pointers
600  */
601 class CoreExport GLineFactory : public XLineFactory
602 {
603  public:
604         GLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "G") { }
605
606         /** Generate a GLine
607          */
608         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
609         {
610                 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
611                 return new GLine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
612         }
613 };
614
615 /** An XLineFactory specialized to generate ELine* pointers
616  */
617 class CoreExport ELineFactory : public XLineFactory
618 {
619  public:
620         ELineFactory(InspIRCd* Instance) : XLineFactory(Instance, "E") { }
621
622         /** Generate an ELine
623          */
624         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
625         {
626                 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
627                 return new ELine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
628         }
629 };
630
631 /** An XLineFactory specialized to generate KLine* pointers
632  */
633 class CoreExport KLineFactory : public XLineFactory
634 {
635  public:
636         KLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "K") { }
637
638         /** Generate a KLine
639          */
640         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
641         {
642                 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
643                 return new KLine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
644         }
645 };
646
647 /** An XLineFactory specialized to generate QLine* pointers
648  */
649 class CoreExport QLineFactory : public XLineFactory
650 {
651  public:
652         QLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "Q") { }
653
654         /** Generate a QLine
655          */
656         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
657         {
658                 return new QLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
659         }
660 };
661
662 /** An XLineFactory specialized to generate ZLine* pointers
663  */
664 class CoreExport ZLineFactory : public XLineFactory
665 {
666  public:
667         ZLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "Z") { }
668
669         /** Generate a ZLine
670          */
671         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
672         {
673                 return new ZLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
674         }
675 };
676
677 #endif
678