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