]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/xline.h
Switch <stdint.h> test to use a test file too.
[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() = 0;
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 void DisplayExpiry();
181
182         virtual const char* Displayable();
183
184         virtual bool IsBurstable();
185
186         /** Ident mask (ident part only)
187          */
188         std::string identmask;
189         /** Host mask (host part only)
190          */
191         std::string hostmask;
192
193         std::string matchtext;
194 };
195
196 /** GLine class
197  */
198 class CoreExport GLine : public XLine
199 {
200   public:
201         /** Create a G-Line.
202          * @param s_time The set time
203          * @param d The duration of the xline
204          * @param src The sender of the xline
205          * @param re The reason of the xline
206          * @param ident Ident to match
207          * @param host Host to match
208          */
209         GLine(time_t s_time, long d, std::string src, std::string re, std::string ident, std::string host)
210                 : XLine(s_time, d, src, re, "G"), identmask(ident), hostmask(host)
211         {
212                 matchtext = this->identmask;
213                 matchtext.append("@").append(this->hostmask);
214         }
215
216         /** Destructor
217          */
218         ~GLine()
219         {
220         }
221
222         virtual bool Matches(User *u);
223
224         virtual bool Matches(const std::string &str);
225
226         virtual void Apply(User* u);
227
228         virtual void DisplayExpiry();
229
230         virtual const char* Displayable();
231
232         /** Ident mask (ident part only)
233          */
234         std::string identmask;
235         /** Host mask (host part only)
236          */
237         std::string hostmask;
238
239         std::string matchtext;
240 };
241
242 /** ELine class
243  */
244 class CoreExport ELine : public XLine
245 {
246   public:
247         /** Create an E-Line.
248          * @param s_time The set time
249          * @param d The duration of the xline
250          * @param src The sender of the xline
251          * @param re The reason of the xline
252          * @param ident Ident to match
253          * @param host Host to match
254          */
255         ELine(time_t s_time, long d, std::string src, std::string re, std::string ident, std::string host)
256                 : XLine(s_time, d, src, re, "E"), identmask(ident), hostmask(host)
257         {
258                 matchtext = this->identmask;
259                 matchtext.append("@").append(this->hostmask);
260         }
261
262         ~ELine()
263         {
264         }
265
266         virtual bool Matches(User *u);
267
268         virtual bool Matches(const std::string &str);
269
270         virtual void Unset();
271
272         virtual void DisplayExpiry();
273
274         virtual void OnAdd();
275
276         virtual const char* Displayable();
277
278         /** Ident mask (ident part only)
279          */
280         std::string identmask;
281         /** Host mask (host part only)
282          */
283         std::string hostmask;
284
285         std::string matchtext;
286 };
287
288 /** ZLine class
289  */
290 class CoreExport ZLine : public XLine
291 {
292   public:
293         /** Create a Z-Line.
294          * @param s_time The set time
295          * @param d The duration of the xline
296          * @param src The sender of the xline
297          * @param re The reason of the xline
298          * @param ip IP to match
299          */
300         ZLine(time_t s_time, long d, std::string src, std::string re, std::string ip)
301                 : XLine(s_time, d, src, re, "Z"), ipaddr(ip)
302         {
303         }
304
305         /** Destructor
306          */
307         ~ZLine()
308         {
309         }
310
311         virtual bool Matches(User *u);
312
313         virtual bool Matches(const std::string &str);
314
315         virtual void Apply(User* u);
316
317         virtual void DisplayExpiry();
318
319         virtual const char* Displayable();
320
321         /** IP mask (no ident part)
322          */
323         std::string ipaddr;
324 };
325
326 /** QLine class
327  */
328 class CoreExport QLine : public XLine
329 {
330   public:
331         /** Create a G-Line.
332          * @param s_time The set time
333          * @param d The duration of the xline
334          * @param src The sender of the xline
335          * @param re The reason of the xline
336          * @param nickname Nickname to match
337          */
338         QLine(time_t s_time, long d, std::string src, std::string re, std::string nickname)
339                 : XLine(s_time, d, src, re, "Q"), nick(nickname)
340         {
341         }
342
343         /** Destructor
344          */
345         ~QLine()
346         {
347         }
348         virtual bool Matches(User *u);
349
350         virtual bool Matches(const std::string &str);
351
352         virtual void Apply(User* u);
353
354         virtual void DisplayExpiry();
355
356         virtual const char* Displayable();
357
358         /** Nickname mask
359          */
360         std::string nick;
361 };
362
363 /** XLineFactory is used to generate an XLine pointer, given just the
364  * pattern, timing information and type of line to create. This is used
365  * for example in the spanningtree module which will call an XLineFactory
366  * to create a new XLine when it is inbound on a server link, so that it
367  * does not have to know the specifics of the internals of an XLine class
368  * and/or how to call its constructor.
369  */
370 class CoreExport XLineFactory
371 {
372  protected:
373
374         std::string type;
375
376  public:
377
378         /** Create an XLine factory
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 /** XLineManager is a class used to manage glines, klines, elines, zlines and qlines,
406  * or any other line created by a module. It also manages XLineFactory classes which
407  * can generate a specialized XLine for use by another module.
408  */
409 class CoreExport XLineManager
410 {
411  protected:
412         /** Used to hold XLines which have not yet been applied.
413          */
414         std::vector<XLine *> pending_lines;
415
416         /** Current xline factories
417          */
418         XLineFactMap line_factory;
419
420         /** Container of all lines, this is a map of maps which
421          * allows for fast lookup for add/remove of a line, and
422          * the shortest possible timed O(n) for checking a user
423          * against a line.
424          */
425         XLineContainer lookup_lines;
426
427  public:
428
429         /** Constructor
430          */
431         XLineManager();
432
433         /** Destructor
434          */
435         ~XLineManager();
436
437         /** Split an ident and host into two seperate strings.
438          * This allows for faster matching.
439          */
440         IdentHostPair IdentSplit(const std::string &ident_and_host);
441
442         /** Checks what users match e:lines and sets their ban exempt flag accordingly.
443          */
444         void CheckELines();
445
446         /** Get all lines of a certain type to an XLineLookup (std::map<std::string, XLine*>).
447          * NOTE: When this function runs any expired items are removed from the list before it
448          * is returned to the caller.
449          * @param type The type to look up
450          * @return A list of all XLines of the given type.
451          */
452         XLineLookup* GetAll(const std::string &type);
453
454         /** Remove all lines of a certain type.
455          */
456         void DelAll(const std::string &type);
457
458         /** Return all known types of line currently stored by the XLineManager.
459          * @return A vector containing all known line types currently stored in the main list.
460          */
461         std::vector<std::string> GetAllTypes();
462
463         /** Add a new XLine
464          * @param line The line to be added
465          * @param user The user adding the line or NULL for the local server
466          * @return True if the line was added successfully
467          */
468         bool AddLine(XLine* line, User* user);
469
470         /** Delete an XLine
471          * @param hostmask The xline-specific string identifying the line, e.g. "*@foo"
472          * @param type The type of xline
473          * @param user The user removing the line or NULL if its the local server
474          * @param simulate If this is true, don't actually remove the line, just return
475          * @return True if the line was deleted successfully
476          */
477         bool DelLine(const char* hostmask, const std::string &type, User* user, bool simulate = false);
478
479         /** Registers an xline factory.
480          * An xline factory is a class which when given a particular xline type,
481          * will generate a new XLine specialized to that type. For example if you
482          * pass the XLineFactory that handles glines some data it will return a
483          * pointer to a GLine, polymorphically represented as XLine. This is used where
484          * you do not know the full details of the item you wish to create, e.g. in a
485          * server protocol module like m_spanningtree, when you receive xlines from other
486          * servers.
487          * @param xlf XLineFactory pointer to register
488          */
489         bool RegisterFactory(XLineFactory* xlf);
490
491         /** Unregisters an xline factory.
492          * You must do this when your module unloads.
493          * @param xlf XLineFactory pointer to unregister
494          */
495         bool UnregisterFactory(XLineFactory* xlf);
496
497         /** Get the XLineFactory for a specific type.
498          * Returns NULL if there is no known handler for this xline type.
499          * @param type The type of XLine you require the XLineFactory for
500          */
501         XLineFactory* GetFactory(const std::string &type);
502
503         /** Check if a user matches an XLine
504          * @param type The type of line to look up
505          * @param user The user to match against (what is checked is specific to the xline type)
506          * @return The reason for the line if there is a match, or NULL if there is no match
507          */
508         XLine* MatchesLine(const std::string &type, User* user);
509
510         /** Check if a pattern matches an XLine
511          * @param type The type of line to look up
512          * @param pattern A pattern string specific to the xline type
513          * @return The matching XLine if there is a match, or NULL if there is no match
514          */
515         XLine* MatchesLine(const std::string &type, const std::string &pattern);
516
517         /** Expire a line given two iterators which identify it in the main map.
518          * @param container Iterator to the first level of entries the map
519          * @param item Iterator to the second level of entries in the map
520          */
521         void ExpireLine(ContainerIter container, LookupIter item);
522
523         /** Apply any new lines that are pending to be applied.
524          * This will only apply lines in the pending_lines list, to save on
525          * CPU time.
526          */
527         void ApplyLines();
528
529         /** Handle /STATS for a given type.
530          * NOTE: Any items in the list for this particular line type which have expired
531          * will be expired and removed before the list is displayed.
532          * @param type The type of stats to show
533          * @param numeric The numeric to give to each result line
534          * @param user The username making the query
535          * @param results The string_list to receive the results
536          */
537         void InvokeStats(const std::string &type, int numeric, User* user, string_list &results);
538 };
539
540 #endif