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