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