]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/xline.h
Clear up header insanity
[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
147 /** KLine class
148  */
149 class CoreExport KLine : public XLine
150 {
151   public:
152
153         /** Create a K-Line.
154          * @param s_time The set time
155          * @param d The duration of the xline
156          * @param src The sender of the xline
157          * @param re The reason of the xline
158          * @param ident Ident to match
159          * @param host Host to match
160          */
161         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")
162         {
163                 identmask = strdup(ident);
164                 hostmask = strdup(host);
165                 matchtext = this->identmask;
166                 matchtext.append("@").append(this->hostmask);
167         }
168
169         /** Destructor
170          */
171         ~KLine()
172         {
173                 free(identmask);
174                 free(hostmask);
175         }
176
177         virtual bool Matches(User *u);
178
179         virtual bool Matches(const std::string &str);
180
181         virtual void Apply(User* u);
182
183         virtual void DisplayExpiry();
184
185         virtual const char* Displayable();
186
187         /** Ident mask (ident part only)
188          */
189         char* identmask;
190         /** Host mask (host part only)
191          */
192         char* hostmask;
193
194         std::string matchtext;
195 };
196
197 /** GLine class
198  */
199 class CoreExport GLine : public XLine
200 {
201   public:
202         /** Create a G-Line.
203          * @param s_time The set time
204          * @param d The duration of the xline
205          * @param src The sender of the xline
206          * @param re The reason of the xline
207          * @param ident Ident to match
208          * @param host Host to match
209          */
210         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")
211         {
212                 identmask = strdup(ident);
213                 hostmask = strdup(host);
214                 matchtext = this->identmask;
215                 matchtext.append("@").append(this->hostmask);
216         }
217
218         /** Destructor
219          */
220         ~GLine()
221         {
222                 free(identmask);
223                 free(hostmask);
224         }
225
226         virtual bool Matches(User *u);
227
228         virtual bool Matches(const std::string &str);
229
230         virtual void Apply(User* u);
231
232         virtual void DisplayExpiry();
233
234         virtual const char* Displayable();
235
236         /** Ident mask (ident part only)
237          */
238         char* identmask;
239         /** Host mask (host part only)
240          */
241         char* hostmask;
242
243         std::string matchtext;
244 };
245
246 /** ELine class
247  */
248 class CoreExport ELine : public XLine
249 {
250   public:
251         /** Create an E-Line.
252          * @param s_time The set time
253          * @param d The duration of the xline
254          * @param src The sender of the xline
255          * @param re The reason of the xline
256          * @param ident Ident to match
257          * @param host Host to match
258          */
259         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")
260         {
261                 identmask = strdup(ident);
262                 hostmask = strdup(host);
263                 matchtext = this->identmask;
264                 matchtext.append("@").append(this->hostmask);
265         }
266
267         ~ELine()
268         {
269                 free(identmask);
270                 free(hostmask);
271         }
272
273         virtual bool Matches(User *u);
274
275         virtual bool Matches(const std::string &str);
276
277         virtual void Unset();
278
279         virtual void DisplayExpiry();
280
281         virtual void OnAdd();
282
283         virtual const char* Displayable();
284
285         /** Ident mask (ident part only)
286          */
287         char* identmask;
288         /** Host mask (host part only)
289          */
290         char* hostmask;
291
292         std::string matchtext;
293 };
294
295 /** ZLine class
296  */
297 class CoreExport ZLine : public XLine
298 {
299   public:
300         /** Create a Z-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 ip IP to match
306          */
307         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")
308         {
309                 ipaddr = strdup(ip);
310         }
311
312         /** Destructor
313          */
314         ~ZLine()
315         {
316                 free(ipaddr);
317         }
318
319         virtual bool Matches(User *u);
320
321         virtual bool Matches(const std::string &str);
322
323         virtual void Apply(User* u);
324
325         virtual void DisplayExpiry();
326
327         virtual const char* Displayable();
328
329         /** IP mask (no ident part)
330          */
331         char* ipaddr;
332 };
333
334 /** QLine class
335  */
336 class CoreExport QLine : public XLine
337 {
338   public:
339         /** Create a G-Line.
340          * @param s_time The set time
341          * @param d The duration of the xline
342          * @param src The sender of the xline
343          * @param re The reason of the xline
344          * @param nickname Nickname to match
345          */
346         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")
347         {
348                 nick = strdup(nickname);
349         }
350
351         /** Destructor
352          */
353         ~QLine()
354         {
355                 free(nick);
356
357         }
358         virtual bool Matches(User *u);
359
360         virtual bool Matches(const std::string &str);
361
362         virtual void Apply(User* u);
363
364         virtual void DisplayExpiry();
365
366         virtual const char* Displayable();
367
368         /** Nickname mask
369          */
370         char* nick;
371 };
372
373 /** Contains an ident and host split into two strings
374  */
375 typedef std::pair<std::string, std::string> IdentHostPair;
376
377 /** XLineFactory is used to generate an XLine pointer, given just the 
378  * pattern, timing information and type of line to create. This is used
379  * for example in the spanningtree module which will call an XLineFactory
380  * to create a new XLine when it is inbound on a server link, so that it
381  * does not have to know the specifics of the internals of an XLine class
382  * and/or how to call its constructor.
383  */
384 class CoreExport XLineFactory
385 {
386  protected:
387
388         InspIRCd* ServerInstance;
389         std::string type;
390
391  public:
392
393         /** Create an XLine factory
394          * @param Instance creator
395          * @param t Type of XLine this factory generates
396          */
397         XLineFactory(InspIRCd* Instance, const std::string &t) : ServerInstance(Instance), type(t) { }
398         
399         /** Return the type of XLine this factory generates
400          * @return The type of XLine this factory generates
401          */
402         virtual const std::string& GetType() { return type; }
403
404         /** Generate a specialized XLine*.
405          * @param set_time Time this line was created
406          * @param duration Duration of the line
407          * @param source The sender of the line, nickname or server
408          * @param reason The reason for the line
409          * @param xline_specific_mask The mask string for the line, specific to the XLine type being created.
410          * @return A specialized XLine class of the given type for this factory.
411          */
412         virtual XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask) = 0;
413
414         /** Destructor
415          */
416         virtual ~XLineFactory() { }
417 };
418
419 /* Required forward declarations
420  */
421 class ServerConfig;
422 class InspIRCd;
423
424 class GLineFactory;
425 class ELineFactory;
426 class QLineFactory;
427 class ZLineFactory;
428 class KLineFactory;
429
430 /** A map of xline factories
431  */
432 typedef std::map<std::string, XLineFactory*> XLineFactMap;
433
434 /** A map of XLines indexed by string
435  */
436 typedef std::map<std::string, XLine *> XLineLookup;
437
438 /** A map of XLineLookup maps indexed by string
439  */
440 typedef std::map<std::string, XLineLookup > XLineContainer;
441
442 /** An iterator in an XLineContainer
443  */
444 typedef XLineContainer::iterator ContainerIter;
445
446 /** An interator in an XLineLookup
447  */
448 typedef XLineLookup::iterator LookupIter;
449
450 /** XLineManager is a class used to manage glines, klines, elines, zlines and qlines,
451  * or any other line created by a module. It also manages XLineFactory classes which
452  * can generate a specialized XLine for use by another module.
453  */
454 class CoreExport XLineManager
455 {
456  protected:
457         /** The owner/creator of this class
458          */
459         InspIRCd* ServerInstance;
460
461         /** Used to hold XLines which have not yet been applied.
462          */
463         std::vector<XLine *> pending_lines;
464
465         /** Current xline factories
466          */
467         XLineFactMap line_factory;
468
469         /** Core xline factories for G/E/K/Q/Z lines
470          * (These generate GLine, ELine, KLine, QLine and ZLine
471          * respectively)
472          */
473         GLineFactory* GFact;
474         ELineFactory* EFact;
475         KLineFactory* KFact;
476         QLineFactory* QFact;
477         ZLineFactory* ZFact;
478
479         /** Container of all lines, this is a map of maps which
480          * allows for fast lookup for add/remove of a line, and
481          * the shortest possible timed O(n) for checking a user
482          * against a line.
483          */
484         XLineContainer lookup_lines;
485
486  public:
487
488         /** Constructor
489          * @param Instance A pointer to the creator object
490          */
491         XLineManager(InspIRCd* Instance);
492
493         /** Destructor
494          */
495         ~XLineManager();
496
497         /** Split an ident and host into two seperate strings.
498          * This allows for faster matching.
499          */
500         IdentHostPair IdentSplit(const std::string &ident_and_host);
501
502         /** Checks what users match e:lines and sets their ban exempt flag accordingly.
503          */
504         void CheckELines();
505
506         /** Get all lines of a certain type to an XLineLookup (std::map<std::string, XLine*>).
507          * NOTE: When this function runs any expired items are removed from the list before it
508          * is returned to the caller.
509          * @param The type to look up
510          * @return A list of all XLines of the given type.
511          */
512         XLineLookup* GetAll(const std::string &type);
513
514         /** Return all known types of line currently stored by the XLineManager.
515          * @return A vector containing all known line types currently stored in the main list.
516          */
517         std::vector<std::string> GetAllTypes();
518
519         /** Add a new XLine
520          * @param line The line to be added
521          * @param user The user adding the line or NULL for the local server
522          * @return True if the line was added successfully
523          */
524         bool AddLine(XLine* line, User* user);
525
526         /** Delete an XLine
527          * @param hostmask The xline-specific string identifying the line, e.g. "*@foo"
528          * @param type The type of xline
529          * @param user The user removing the line or NULL if its the local server
530          * @param simulate If this is true, don't actually remove the line, just return
531          * @return True if the line was deleted successfully
532          */
533         bool DelLine(const char* hostmask, const std::string &type, User* user, bool simulate = false);
534
535         /** Registers an xline factory.
536          * An xline factory is a class which when given a particular xline type,
537          * will generate a new XLine specialized to that type. For example if you
538          * pass the XLineFactory that handles glines some data it will return a
539          * pointer to a GLine, polymorphically represented as XLine. This is used where
540          * you do not know the full details of the item you wish to create, e.g. in a 
541          * server protocol module like m_spanningtree, when you receive xlines from other
542          * servers.
543          * @param xlf XLineFactory pointer to register
544          */
545         bool RegisterFactory(XLineFactory* xlf);
546
547         /** Unregisters an xline factory.
548          * You must do this when your module unloads.
549          * @param xlf XLineFactory pointer to unregister
550          */
551         bool UnregisterFactory(XLineFactory* xlf);
552
553         /** Get the XLineFactory for a specific type.
554          * Returns NULL if there is no known handler for this xline type.
555          * @param type The type of XLine you require the XLineFactory for
556          */
557         XLineFactory* GetFactory(const std::string &type);
558
559         /** Check if a user matches an XLine
560          * @param type The type of line to look up
561          * @param user The user to match against (what is checked is specific to the xline type)
562          * @return The reason for the line if there is a match, or NULL if there is no match
563          */
564         XLine* MatchesLine(const std::string &type, User* user);
565
566         /** Check if a pattern matches an XLine
567          * @param type The type of line to look up
568          * @param pattern A pattern string specific to the xline type
569          * @return The matching XLine if there is a match, or NULL if there is no match
570          */
571         XLine* MatchesLine(const std::string &type, const std::string &pattern);
572
573         /** Expire a line given two iterators which identify it in the main map.
574          * @param container Iterator to the first level of entries the map
575          * @param item Iterator to the second level of entries in the map
576          */
577         void ExpireLine(ContainerIter container, LookupIter item);
578
579         /** Apply any new lines that are pending to be applied.
580          * This will only apply lines in the pending_lines list, to save on
581          * CPU time.
582          */
583         void ApplyLines();
584
585         /** Handle /STATS for a given type.
586          * NOTE: Any items in the list for this particular line type which have expired
587          * will be expired and removed before the list is displayed.
588          * @param numeric The numeric to give to each result line
589          * @param user The username making the query
590          * @param results The string_list to receive the results
591          */
592         void InvokeStats(const std::string &type, int numeric, User* user, string_list &results);
593 };
594
595 /** An XLineFactory specialized to generate GLine* pointers
596  */
597 class CoreExport GLineFactory : public XLineFactory
598 {
599  public:
600         GLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "G") { }
601
602         /** Generate a GLine
603          */
604         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
605         {
606                 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
607                 return new GLine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
608         }
609 };
610
611 /** An XLineFactory specialized to generate ELine* pointers
612  */
613 class CoreExport ELineFactory : public XLineFactory
614 {
615  public:
616         ELineFactory(InspIRCd* Instance) : XLineFactory(Instance, "E") { }
617
618         /** Generate an ELine
619          */
620         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
621         {
622                 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
623                 return new ELine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
624         }
625 };
626
627 /** An XLineFactory specialized to generate KLine* pointers
628  */
629 class CoreExport KLineFactory : public XLineFactory
630 {
631  public:
632         KLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "K") { }
633
634         /** Generate a KLine
635          */
636         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
637         {
638                 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
639                 return new KLine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
640         }
641 };
642
643 /** An XLineFactory specialized to generate QLine* pointers
644  */
645 class CoreExport QLineFactory : public XLineFactory
646 {
647  public:
648         QLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "Q") { }
649
650         /** Generate a QLine
651          */
652         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
653         {
654                 return new QLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
655         }
656 };
657
658 /** An XLineFactory specialized to generate ZLine* pointers
659  */
660 class CoreExport ZLineFactory : public XLineFactory
661 {
662  public:
663         ZLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "Z") { }
664
665         /** Generate a ZLine
666          */
667         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
668         {
669                 return new ZLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
670         }
671 };
672
673 #endif
674