]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/xline.h
Update all wiki links to point to the new wiki. This was done automatically with...
[user/henk/code/inspircd.git] / include / xline.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/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 : public classbase
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         virtual bool AutoApplyToUserList(XLine* x) { return true; }
419
420         /** Destructor
421          */
422         virtual ~XLineFactory() { }
423 };
424
425 /* Required forward declarations
426  */
427 class ServerConfig;
428 class InspIRCd;
429
430 class GLineFactory;
431 class ELineFactory;
432 class QLineFactory;
433 class ZLineFactory;
434 class KLineFactory;
435
436 /** A map of xline factories
437  */
438 typedef std::map<std::string, XLineFactory*> XLineFactMap;
439
440 /** A map of XLines indexed by string
441  */
442 typedef std::map<irc::string, XLine *> XLineLookup;
443
444 /** A map of XLineLookup maps indexed by string
445  */
446 typedef std::map<std::string, XLineLookup > XLineContainer;
447
448 /** An iterator in an XLineContainer
449  */
450 typedef XLineContainer::iterator ContainerIter;
451
452 /** An interator in an XLineLookup
453  */
454 typedef XLineLookup::iterator LookupIter;
455
456 /** XLineManager is a class used to manage glines, klines, elines, zlines and qlines,
457  * or any other line created by a module. It also manages XLineFactory classes which
458  * can generate a specialized XLine for use by another module.
459  */
460 class CoreExport XLineManager : public classbase
461 {
462  protected:
463         /** The owner/creator of this class
464          */
465         InspIRCd* ServerInstance;
466
467         /** Used to hold XLines which have not yet been applied.
468          */
469         std::vector<XLine *> pending_lines;
470
471         /** Current xline factories
472          */
473         XLineFactMap line_factory;
474
475         /** Core xline factories for G/E/K/Q/Z lines
476          * (These generate GLine, ELine, KLine, QLine and ZLine
477          * respectively)
478          */
479         GLineFactory* GFact;
480         ELineFactory* EFact;
481         KLineFactory* KFact;
482         QLineFactory* QFact;
483         ZLineFactory* ZFact;
484
485         /** Container of all lines, this is a map of maps which
486          * allows for fast lookup for add/remove of a line, and
487          * the shortest possible timed O(n) for checking a user
488          * against a line.
489          */
490         XLineContainer lookup_lines;
491
492  public:
493
494         /** Constructor
495          * @param Instance A pointer to the creator object
496          */
497         XLineManager(InspIRCd* Instance);
498
499         /** Destructor
500          */
501         ~XLineManager();
502
503         /** Split an ident and host into two seperate strings.
504          * This allows for faster matching.
505          */
506         IdentHostPair IdentSplit(const std::string &ident_and_host);
507
508         /** Checks what users match e:lines and sets their ban exempt flag accordingly.
509          */
510         void CheckELines();
511
512         /** Get all lines of a certain type to an XLineLookup (std::map<std::string, XLine*>).
513          * NOTE: When this function runs any expired items are removed from the list before it
514          * is returned to the caller.
515          * @param The type to look up
516          * @return A list of all XLines of the given type.
517          */
518         XLineLookup* GetAll(const std::string &type);
519
520         /** Remove all lines of a certain type.
521          */
522         void DelAll(const std::string &type);
523
524         /** Return all known types of line currently stored by the XLineManager.
525          * @return A vector containing all known line types currently stored in the main list.
526          */
527         std::vector<std::string> GetAllTypes();
528
529         /** Add a new XLine
530          * @param line The line to be added
531          * @param user The user adding the line or NULL for the local server
532          * @return True if the line was added successfully
533          */
534         bool AddLine(XLine* line, User* user);
535
536         /** Delete an XLine
537          * @param hostmask The xline-specific string identifying the line, e.g. "*@foo"
538          * @param type The type of xline
539          * @param user The user removing the line or NULL if its the local server
540          * @param simulate If this is true, don't actually remove the line, just return
541          * @return True if the line was deleted successfully
542          */
543         bool DelLine(const char* hostmask, const std::string &type, User* user, bool simulate = false);
544
545         /** Registers an xline factory.
546          * An xline factory is a class which when given a particular xline type,
547          * will generate a new XLine specialized to that type. For example if you
548          * pass the XLineFactory that handles glines some data it will return a
549          * pointer to a GLine, polymorphically represented as XLine. This is used where
550          * you do not know the full details of the item you wish to create, e.g. in a
551          * server protocol module like m_spanningtree, when you receive xlines from other
552          * servers.
553          * @param xlf XLineFactory pointer to register
554          */
555         bool RegisterFactory(XLineFactory* xlf);
556
557         /** Unregisters an xline factory.
558          * You must do this when your module unloads.
559          * @param xlf XLineFactory pointer to unregister
560          */
561         bool UnregisterFactory(XLineFactory* xlf);
562
563         /** Get the XLineFactory for a specific type.
564          * Returns NULL if there is no known handler for this xline type.
565          * @param type The type of XLine you require the XLineFactory for
566          */
567         XLineFactory* GetFactory(const std::string &type);
568
569         /** Check if a user matches an XLine
570          * @param type The type of line to look up
571          * @param user The user to match against (what is checked is specific to the xline type)
572          * @return The reason for the line if there is a match, or NULL if there is no match
573          */
574         XLine* MatchesLine(const std::string &type, User* user);
575
576         /** Check if a pattern matches an XLine
577          * @param type The type of line to look up
578          * @param pattern A pattern string specific to the xline type
579          * @return The matching XLine if there is a match, or NULL if there is no match
580          */
581         XLine* MatchesLine(const std::string &type, const std::string &pattern);
582
583         /** Expire a line given two iterators which identify it in the main map.
584          * @param container Iterator to the first level of entries the map
585          * @param item Iterator to the second level of entries in the map
586          */
587         void ExpireLine(ContainerIter container, LookupIter item);
588
589         /** Apply any new lines that are pending to be applied.
590          * This will only apply lines in the pending_lines list, to save on
591          * CPU time.
592          */
593         void ApplyLines();
594
595         /** Handle /STATS for a given type.
596          * NOTE: Any items in the list for this particular line type which have expired
597          * will be expired and removed before the list is displayed.
598          * @param numeric The numeric to give to each result line
599          * @param user The username making the query
600          * @param results The string_list to receive the results
601          */
602         void InvokeStats(const std::string &type, int numeric, User* user, string_list &results);
603 };
604
605 /** An XLineFactory specialized to generate GLine* pointers
606  */
607 class CoreExport GLineFactory : public XLineFactory
608 {
609  public:
610         GLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "G") { }
611
612         /** Generate a GLine
613          */
614         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
615         {
616                 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
617                 return new GLine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
618         }
619 };
620
621 /** An XLineFactory specialized to generate ELine* pointers
622  */
623 class CoreExport ELineFactory : public XLineFactory
624 {
625  public:
626         ELineFactory(InspIRCd* Instance) : XLineFactory(Instance, "E") { }
627
628         /** Generate an ELine
629          */
630         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
631         {
632                 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
633                 return new ELine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
634         }
635 };
636
637 /** An XLineFactory specialized to generate KLine* pointers
638  */
639 class CoreExport KLineFactory : public XLineFactory
640 {
641  public:
642         KLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "K") { }
643
644         /** Generate a KLine
645          */
646         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
647         {
648                 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
649                 return new KLine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
650         }
651 };
652
653 /** An XLineFactory specialized to generate QLine* pointers
654  */
655 class CoreExport QLineFactory : public XLineFactory
656 {
657  public:
658         QLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "Q") { }
659
660         /** Generate a QLine
661          */
662         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
663         {
664                 return new QLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
665         }
666 };
667
668 /** An XLineFactory specialized to generate ZLine* pointers
669  */
670 class CoreExport ZLineFactory : public XLineFactory
671 {
672  public:
673         ZLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "Z") { }
674
675         /** Generate a ZLine
676          */
677         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
678         {
679                 return new ZLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
680         }
681 };
682
683 #endif