]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/xline.h
Add XLine::SetCreateTime that removes gline_set_creation_time and friends
[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         /** This functor is used by the std::sort() function to keep all lines in order
396          */
397         static bool XSortComparison (const XLine *one, const XLine *two);
398
399         /** Used to hold XLines which have not yet been applied.
400          */
401         std::vector<XLine *> pending_lines;
402
403         std::vector<XLine *> active_lines;
404
405         std::map<char, XLineFactory*> line_factory;
406
407         GLineFactory* GFact;
408         ELineFactory* EFact;
409         KLineFactory* KFact;
410         QLineFactory* QFact;
411         ZLineFactory* ZFact;
412
413         unsigned int PermLines;
414
415  public:
416
417         std::map<char, std::map<std::string, XLine *> > lookup_lines;
418
419         /** Constructor
420          * @param Instance A pointer to the creator object
421          */
422         XLineManager(InspIRCd* Instance);
423
424         ~XLineManager();
425
426         /** Split an ident and host into two seperate strings.
427          * This allows for faster matching.
428          */
429         IdentHostPair IdentSplit(const std::string &ident_and_host);
430
431         /** Checks what users match a given list of ELines and sets their ban exempt flag accordingly.
432          * @param ELines List of E:Lines to check.
433          */
434         void CheckELines(std::map<std::string, XLine *> &ELines);
435
436         /** Add a new GLine
437          * @param line The line to be added
438          * @param user The user adding the line or NULL for the local server
439          * @return True if the line was added successfully
440          */
441         bool AddLine(XLine* line, User* user);
442
443         /** Delete a GLine
444          * @param hostmask The xline-specific string identifying the line, e.g. "*@foo"
445          * @param type The type of xline
446          * @param user The user removing the line or NULL if its the local server
447          * @param simulate If this is true, don't actually remove the line, just return
448          * @return True if the line was deleted successfully
449          */
450         bool DelLine(const char* hostmask, char type, User* user, bool simulate = false);
451
452         /** Registers an xline factory.
453          * An xline factory is a class which when given a particular xline type,
454          * will generate a new XLine specialized to that type. For example if you
455          * pass the XLineFactory that handles glines some data it will return a
456          * pointer to a GLine, polymorphically represented as XLine. This is used where
457          * you do not know the full details of the item you wish to create, e.g. in a 
458          * server protocol module like m_spanningtree, when you receive xlines from other
459          * servers.
460          */
461         bool RegisterFactory(XLineFactory* xlf);
462
463         /** Unregisters an xline factory
464          */
465         bool UnregisterFactory(XLineFactory* xlf);
466
467         /** Get the XLineFactory for a specific type.
468          * Returns NULL if there is no known handler for this xline type
469          */
470         XLineFactory* GetFactory(const char type);
471
472         /** Check if a nickname matches a QLine
473          * @return nick The nick to check against
474          * @return The reason for the line if there is a match, or NULL if there is no match
475          */
476         QLine* matches_qline(const char* nick);
477
478         /** Check if a hostname matches a GLine
479          * @param user The user to check against
480          * @return The reason for the line if there is a match, or NULL if there is no match
481          */
482         GLine* matches_gline(User* user);
483
484         /** Check if a user's IP matches a ZLine
485          * @param user The user to check against
486          * @return The reason for the line if there is a match, or NULL if there is no match
487          */
488         ZLine* matches_zline(User *user);
489
490         /** Check if a hostname matches a KLine
491          * @param user The user to check against
492          * @return The reason for the line if there is a match, or NULL if there is no match
493          */
494         KLine* matches_kline(User* user);
495
496         /** Check if a hostname matches a ELine
497          * @param user The user to check against
498          * @return The reason for the line if there is a match, or NULL if there is no match
499          */
500         ELine* matches_exception(User* user);
501
502         /** Expire any lines that should be expired.
503          */
504         void expire_lines();
505
506         /** Apply any new lines that are pending to be applied
507          */
508         void ApplyLines();
509
510         /** Handle /STATS for a given type.
511          * @param numeric The numeric to give to each result line
512          * @param user The username making the query
513          * @param results The string_list to receive the results
514          */
515         void InvokeStats(const char type, int numeric, User* user, string_list &results);
516
517         /** Handle /STATS E
518          * @param user The username making the query
519          * @param results The string_list to receive the results
520          */
521         void stats_e(User* user, string_list &results);
522
523         /** Change creation time of a GLine
524          * @param host The hostname to change
525          * @param create_Time The new creation time
526          */
527         void gline_set_creation_time(const char* host, time_t create_time);
528
529         /** Change creation time of a QLine
530          * @param nick The nickmask to change
531          * @param create_Time The new creation time
532          */
533         void qline_set_creation_time(const char* nick, time_t create_time);
534
535         /** Change creation time of a ZLine
536          * @param ip The ipmask to change
537          * @param create_Time The new creation time
538          */
539         void zline_set_creation_time(const char* ip, time_t create_time);
540
541         /** Change creation time of a ELine
542          * @param host The hostname to change
543          * @param create_Time The new creation time
544          */
545         void eline_set_creation_time(const char* host, time_t create_time);
546 };
547
548 class CoreExport GLineFactory : public XLineFactory
549 {
550  public:
551         GLineFactory(InspIRCd* Instance) : XLineFactory(Instance, 'G') { }
552
553         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
554         {
555                 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
556                 return new GLine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
557         }
558 };
559
560 class CoreExport ELineFactory : public XLineFactory
561 {
562  public:
563         ELineFactory(InspIRCd* Instance) : XLineFactory(Instance, 'E') { }
564
565         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
566         {
567                 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
568                 return new ELine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
569         }
570 };
571
572 class CoreExport KLineFactory : public XLineFactory
573 {
574  public:
575         KLineFactory(InspIRCd* Instance) : XLineFactory(Instance, 'K') { }
576
577         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
578         {
579                 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
580                 return new KLine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
581         }
582 };
583
584 class CoreExport QLineFactory : public XLineFactory
585 {
586  public:
587         QLineFactory(InspIRCd* Instance) : XLineFactory(Instance, 'Q') { }
588
589         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
590         {
591                 return new QLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
592         }
593 };
594
595 class CoreExport ZLineFactory : public XLineFactory
596 {
597  public:
598         ZLineFactory(InspIRCd* Instance) : XLineFactory(Instance, 'Z') { }
599
600         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
601         {
602                 return new ZLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
603         }
604 };
605
606 #endif