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