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