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