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