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