]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/xline.h
Move lookup_lines to private, theres no need for it to be public any more
[user/henk/code/inspircd.git] / include / xline.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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 the common header files
18
19 #include <string>
20 #include <deque>
21 #include <vector>
22 #include "users.h"
23 #include "channels.h"
24
25 const int APPLY_GLINES          = 1;
26 const int APPLY_KLINES          = 2;
27 const int APPLY_QLINES          = 4;
28 const int APPLY_ZLINES          = 8;
29 const int APPLY_PERM_ONLY       = 16;
30 const int APPLY_ALL             = APPLY_GLINES | APPLY_KLINES | APPLY_QLINES | APPLY_ZLINES;
31
32 /** XLine is the base class for ban lines such as G lines and K lines.
33  */
34 class CoreExport XLine : public classbase
35 {
36  protected:
37
38         InspIRCd* ServerInstance;
39         void DefaultApply(User* u, const std::string &line);
40
41  public:
42
43         /** Create an XLine.
44          * @param s_time The set time
45          * @param d The duration of the xline
46          * @param src The sender of the xline
47          * @param re The reason of the xline
48          */
49         XLine(InspIRCd* Instance, time_t s_time, long d, const char* src, const char* re, const std::string &t)
50                 : ServerInstance(Instance), set_time(s_time), duration(d), type(t)
51         {
52                 source = strdup(src);
53                 reason = strdup(re);
54                 expiry = set_time + duration;
55         }
56
57         /** Destructor
58          */
59         virtual ~XLine()
60         {
61                 free(reason);
62                 free(source);
63         }
64
65         virtual void SetCreateTime(time_t created)
66         {
67                 set_time = created;
68                 expiry = created + duration;
69         }
70
71         /** Returns true whether or not the given user is covered by this line.
72          */
73         virtual bool Matches(User *u) = 0;
74
75         virtual bool Matches(const std::string &str) = 0;
76
77         virtual void Apply(User* u);
78
79         virtual void Unset() { }
80
81         virtual void DisplayExpiry() = 0;
82
83         virtual const char* Displayable() = 0;
84
85         virtual void OnAdd() { }
86
87         /** The time the line was added.
88          */
89         time_t set_time;
90         
91         /** The duration of the ban, or 0 if permenant
92          */
93         long duration;
94         
95         /** Source of the ban. This can be a servername or an oper nickname
96          */
97         char* source;
98         
99         /** Reason for the ban
100          */
101         char* reason;
102
103         /** Expiry time
104          */
105         time_t expiry;
106
107         /** Q, K, etc. Don't change this. Constructors set it.
108          */
109         const std::string type;
110 };
111
112 /** KLine class
113  */
114 class CoreExport KLine : public XLine
115 {
116   public:
117         /** Create a K-Line.
118          * @param s_time The set time
119          * @param d The duration of the xline
120          * @param src The sender of the xline
121          * @param re The reason of the xline
122          * @param ident Ident to match
123          * @param host Host to match
124          */
125         KLine(InspIRCd* Instance, time_t s_time, long d, const char* src, const char* re, const char* ident, const char* host) : XLine(Instance, s_time, d, src, re, "K")
126         {
127                 identmask = strdup(ident);
128                 hostmask = strdup(host);
129                 matchtext = this->identmask;
130                 matchtext.append("@").append(this->hostmask);
131         }
132
133         /** Destructor
134          */
135         ~KLine()
136         {
137                 free(identmask);
138                 free(hostmask);
139         }
140
141         virtual bool Matches(User *u);
142
143         virtual bool Matches(const std::string &str);
144
145         virtual void Apply(User* u);
146
147         virtual void DisplayExpiry();
148
149         virtual const char* Displayable();
150
151         /** Ident mask
152          */
153         char* identmask;
154         /** Host mask
155          */
156         char* hostmask;
157
158         std::string matchtext;
159 };
160
161 /** GLine class
162  */
163 class CoreExport GLine : public XLine
164 {
165   public:
166         /** Create a G-Line.
167          * @param s_time The set time
168          * @param d The duration of the xline
169          * @param src The sender of the xline
170          * @param re The reason of the xline
171          * @param ident Ident to match
172          * @param host Host to match
173          */
174         GLine(InspIRCd* Instance, time_t s_time, long d, const char* src, const char* re, const char* ident, const char* host) : XLine(Instance, s_time, d, src, re, "G")
175         {
176                 identmask = strdup(ident);
177                 hostmask = strdup(host);
178                 matchtext = this->identmask;
179                 matchtext.append("@").append(this->hostmask);
180         }
181
182         /** Destructor
183          */
184         ~GLine()
185         {
186                 free(identmask);
187                 free(hostmask);
188         }
189
190         virtual bool Matches(User *u);
191
192         virtual bool Matches(const std::string &str);
193
194         virtual void Apply(User* u);
195
196         virtual void DisplayExpiry();
197
198         virtual const char* Displayable();
199
200         /** Ident mask
201          */
202         char* identmask;
203         /** Host mask
204          */
205         char* hostmask;
206
207         std::string matchtext;
208 };
209
210 /** ELine class
211  */
212 class CoreExport ELine : public XLine
213 {
214   public:
215         /** Create an E-Line.
216          * @param s_time The set time
217          * @param d The duration of the xline
218          * @param src The sender of the xline
219          * @param re The reason of the xline
220          * @param ident Ident to match
221          * @param host Host to match
222          */
223         ELine(InspIRCd* Instance, time_t s_time, long d, const char* src, const char* re, const char* ident, const char* host) : XLine(Instance, s_time, d, src, re, "E")
224         {
225                 identmask = strdup(ident);
226                 hostmask = strdup(host);
227                 matchtext = this->identmask;
228                 matchtext.append("@").append(this->hostmask);
229         }
230
231         ~ELine()
232         {
233                 free(identmask);
234                 free(hostmask);
235         }
236
237         virtual bool Matches(User *u);
238
239         virtual bool Matches(const std::string &str);
240
241         virtual void Unset();
242
243         virtual void DisplayExpiry();
244
245         virtual void OnAdd();
246
247         virtual const char* Displayable();
248
249         /** Ident mask
250          */
251         char* identmask;
252         /** Host mask
253          */
254         char* hostmask;
255
256         std::string matchtext;
257 };
258
259 /** ZLine class
260  */
261 class CoreExport ZLine : public XLine
262 {
263   public:
264         /** Create a Z-Line.
265          * @param s_time The set time
266          * @param d The duration of the xline
267          * @param src The sender of the xline
268          * @param re The reason of the xline
269          * @param ip IP to match
270          */
271         ZLine(InspIRCd* Instance, time_t s_time, long d, const char* src, const char* re, const char* ip) : XLine(Instance, s_time, d, src, re, "Z")
272         {
273                 ipaddr = strdup(ip);
274         }
275
276         /** Destructor
277          */
278         ~ZLine()
279         {
280                 free(ipaddr);
281         }
282
283         virtual bool Matches(User *u);
284
285         virtual bool Matches(const std::string &str);
286
287         virtual void Apply(User* u);
288
289         virtual void DisplayExpiry();
290
291         virtual const char* Displayable();
292
293         /** IP mask
294          */
295         char* ipaddr;
296 };
297
298 /** QLine class
299  */
300 class CoreExport QLine : public XLine
301 {
302   public:
303         /** Create a G-Line.
304          * @param s_time The set time
305          * @param d The duration of the xline
306          * @param src The sender of the xline
307          * @param re The reason of the xline
308          * @param nickname Nickname to match
309          */
310         QLine(InspIRCd* Instance, time_t s_time, long d, const char* src, const char* re, const char* nickname) : XLine(Instance, s_time, d, src, re, "Q")
311         {
312                 nick = strdup(nickname);
313         }
314
315         /** Destructor
316          */
317         ~QLine()
318         {
319                 free(nick);
320
321         }
322         virtual bool Matches(User *u);
323
324         virtual bool Matches(const std::string &str);
325
326         virtual void Apply(User* u);
327
328         virtual void DisplayExpiry();
329
330         virtual const char* Displayable();
331
332         /** Nickname mask
333          */
334         char* nick;
335 };
336
337 /** Contains an ident and host split into two strings
338  */
339 typedef std::pair<std::string, std::string> IdentHostPair;
340
341
342 class CoreExport XLineFactory
343 {
344  protected:
345
346         InspIRCd* ServerInstance;
347         std::string type;
348
349  public:
350
351         XLineFactory(InspIRCd* Instance, const std::string &t) : ServerInstance(Instance), type(t) { }
352         
353         virtual const std::string& GetType() { return type; }
354
355         virtual XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask) = 0;
356
357         virtual ~XLineFactory() { }
358 };
359
360 /* Required forward declarations
361  */
362 class ServerConfig;
363 class InspIRCd;
364
365 class GLineFactory;
366 class ELineFactory;
367 class QLineFactory;
368 class ZLineFactory;
369 class KLineFactory;
370
371 typedef std::map<std::string, XLineFactory*> XLineFactMap;
372 typedef std::map<std::string, XLine *> XLineLookup;
373 typedef std::map<std::string, XLineLookup > XLineContainer;
374 typedef XLineContainer::iterator ContainerIter;
375 typedef XLineLookup::iterator LookupIter;
376
377 /** XLineManager is a class used to manage glines, klines, elines, zlines and qlines.
378  */
379 class CoreExport XLineManager
380 {
381  protected:
382         /** The owner/creator of this class
383          */
384         InspIRCd* ServerInstance;
385
386         /** Used to hold XLines which have not yet been applied.
387          */
388         std::vector<XLine *> pending_lines;
389
390         XLineFactMap line_factory;
391
392         GLineFactory* GFact;
393         ELineFactory* EFact;
394         KLineFactory* KFact;
395         QLineFactory* QFact;
396         ZLineFactory* ZFact;
397
398         XLineContainer lookup_lines;
399
400  public:
401
402         /** Constructor
403          * @param Instance A pointer to the creator object
404          */
405         XLineManager(InspIRCd* Instance);
406
407         ~XLineManager();
408
409         /** Split an ident and host into two seperate strings.
410          * This allows for faster matching.
411          */
412         IdentHostPair IdentSplit(const std::string &ident_and_host);
413
414         /** Checks what users match e:lines and sets their ban exempt flag accordingly.
415          */
416         void CheckELines();
417
418         /** Add a new GLine
419          * @param line The line to be added
420          * @param user The user adding the line or NULL for the local server
421          * @return True if the line was added successfully
422          */
423         bool AddLine(XLine* line, User* user);
424
425         /** Delete a GLine
426          * @param hostmask The xline-specific string identifying the line, e.g. "*@foo"
427          * @param type The type of xline
428          * @param user The user removing the line or NULL if its the local server
429          * @param simulate If this is true, don't actually remove the line, just return
430          * @return True if the line was deleted successfully
431          */
432         bool DelLine(const char* hostmask, const std::string &type, User* user, bool simulate = false);
433
434         /** Registers an xline factory.
435          * An xline factory is a class which when given a particular xline type,
436          * will generate a new XLine specialized to that type. For example if you
437          * pass the XLineFactory that handles glines some data it will return a
438          * pointer to a GLine, polymorphically represented as XLine. This is used where
439          * you do not know the full details of the item you wish to create, e.g. in a 
440          * server protocol module like m_spanningtree, when you receive xlines from other
441          * servers.
442          */
443         bool RegisterFactory(XLineFactory* xlf);
444
445         /** Unregisters an xline factory
446          */
447         bool UnregisterFactory(XLineFactory* xlf);
448
449         /** Get the XLineFactory for a specific type.
450          * Returns NULL if there is no known handler for this xline type
451          */
452         XLineFactory* GetFactory(const std::string &type);
453
454         /** Check if a user matches an XLine
455          * @param type The type of line to look up
456          * @param user The user to match against (what is checked is specific to the xline type)
457          * @return The reason for the line if there is a match, or NULL if there is no match
458          */
459         XLine* MatchesLine(const std::string &type, User* user);
460
461         /** Check if a pattern matches an XLine
462          * @param type The type of line to look up
463          * @param pattern A pattern string specific to the xline type
464          * @return The matching XLine if there is a match, or NULL if there is no match
465          */
466         XLine* MatchesLine(const std::string &type, const std::string &pattern);
467
468         /** Expire a line given two iterators which identify it
469          */
470         void ExpireLine(ContainerIter container, LookupIter item);
471
472         /** Apply any new lines that are pending to be applied
473          */
474         void ApplyLines();
475
476         /** Handle /STATS for a given type.
477          * @param numeric The numeric to give to each result line
478          * @param user The username making the query
479          * @param results The string_list to receive the results
480          */
481         void InvokeStats(const std::string &type, int numeric, User* user, string_list &results);
482 };
483
484 class CoreExport GLineFactory : public XLineFactory
485 {
486  public:
487         GLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "G") { }
488
489         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
490         {
491                 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
492                 return new GLine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
493         }
494 };
495
496 class CoreExport ELineFactory : public XLineFactory
497 {
498  public:
499         ELineFactory(InspIRCd* Instance) : XLineFactory(Instance, "E") { }
500
501         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
502         {
503                 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
504                 return new ELine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
505         }
506 };
507
508 class CoreExport KLineFactory : public XLineFactory
509 {
510  public:
511         KLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "K") { }
512
513         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
514         {
515                 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
516                 return new KLine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
517         }
518 };
519
520 class CoreExport QLineFactory : public XLineFactory
521 {
522  public:
523         QLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "Q") { }
524
525         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
526         {
527                 return new QLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
528         }
529 };
530
531 class CoreExport ZLineFactory : public XLineFactory
532 {
533  public:
534         ZLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "Z") { }
535
536         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
537         {
538                 return new ZLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
539         }
540 };
541
542 #endif
543