]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/xline.h
Update copyright headers.
[user/henk/code/inspircd.git] / include / xline.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019 Matt Schatz <genius3000@g3k.solutions>
5  *   Copyright (C) 2012-2013, 2017-2018, 2020 Sadie Powell <sadie@witchery.services>
6  *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
8  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
9  *   Copyright (C) 2006-2008 Robin Burchell <robin+git@viroteck.net>
10  *   Copyright (C) 2004, 2006-2008, 2010 Craig Edwards <brain@inspircd.org>
11  *
12  * This file is part of InspIRCd.  InspIRCd is free software: you can
13  * redistribute it and/or modify it under the terms of the GNU General Public
14  * License as published by the Free Software Foundation, version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25
26 #pragma once
27
28 /** XLine is the base class for ban lines such as G-lines and K-lines.
29  * Modules may derive from this, and their xlines will automatically be
30  * handled as expected by any protocol modules (e.g. m_spanningtree will
31  * propagate them using AddLine). The process of translating a type+pattern
32  * to a known line type is done by means of an XLineFactory object (see
33  * below).
34  */
35 class CoreExport XLine : public classbase
36 {
37  protected:
38
39         /** Default 'apply' action. Quits the user.
40          * @param u User to apply the line against
41          * @param line The line typed, used for display purposes in the quit message
42          * @param bancache If true, the user will be added to the bancache if they match. Else not.
43          */
44         void DefaultApply(User* u, const std::string &line, bool bancache);
45
46  public:
47
48         /** Create an XLine.
49          * @param s_time The set time
50          * @param d The duration of the xline
51          * @param src The sender of the xline
52          * @param re The reason of the xline
53          * @param t The line type, should be set by the derived class constructor
54          */
55         XLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& t)
56                 : set_time(s_time)
57                 , duration(d)
58                 , source(src)
59                 , reason(re)
60                 , type(t)
61                 , from_config(false)
62         {
63                 expiry = set_time + duration;
64         }
65
66         /** Destructor
67          */
68         virtual ~XLine()
69         {
70         }
71
72         /** Change creation time of an xline. Updates expiry
73          * to be after the creation time.
74          */
75         virtual void SetCreateTime(time_t created)
76         {
77                 set_time = created;
78                 expiry = created + duration;
79         }
80
81         /** Returns true whether or not the given user is covered by this line.
82          * @param u The user to match against. The mechanics of the match
83          * are defined by the derived class.
84          * @return True if there is a match.
85          */
86         virtual bool Matches(User *u) = 0;
87
88         /** Returns true whether or not the given string is covered by this line.
89          * @param str The string to match against. The details of what must be
90          * in this string and the mechanics of the match are defined by the
91          * derived class.
92          * @return True if there is a match
93          */
94         virtual bool Matches(const std::string &str) = 0;
95
96         /** Apply a line against a user. The mechanics of what occurs when
97          * the line is applied are specific to the derived class.
98          * @param u The user to apply against
99          */
100         virtual void Apply(User* u);
101
102         /** Called when the line is unset either via expiry or
103          * via explicit removal.
104          */
105         virtual void Unset() { }
106
107         /** Called when the expiry message is to be displayed for the
108          * line. Usually a line in the form 'expiring X-line blah, set by...'
109          * see the DisplayExpiry methods of GLine, ELine etc.
110          */
111         virtual void DisplayExpiry();
112
113         /** Returns the displayable form of the pattern for this xline,
114          * e.g. '*\@foo' or '*baz*'. This must always return the full pattern
115          * in a form which can be used to construct an entire derived xline,
116          * even if it is stored differently internally (e.g. GLine stores the
117          * ident and host parts separately but will still return ident\@host
118          * for its Displayable() method).
119          */
120         virtual const std::string& Displayable() = 0;
121
122         /** Called when the xline has just been added.
123          */
124         virtual void OnAdd() { }
125
126         /** The time the line was added.
127          */
128         time_t set_time;
129
130         /** The duration of the ban, or 0 if permanent
131          */
132         unsigned long duration;
133
134         /** Source of the ban. This can be a servername or an oper nickname
135          */
136         std::string source;
137
138         /** Reason for the ban
139          */
140         std::string reason;
141
142         /** Expiry time. Does not contain useful data if the duration is 0.
143          */
144         time_t expiry;
145
146         /** "Q", "K", etc. Set only by derived classes constructor to the
147          * type of line this is.
148          */
149         const std::string type;
150
151         // Whether this XLine was loaded from the server config.
152         bool from_config;
153
154         virtual bool IsBurstable();
155 };
156
157 /** KLine class
158  */
159 class CoreExport KLine : public XLine
160 {
161   public:
162
163         /** Create a K-line.
164          * @param s_time The set time
165          * @param d The duration of the xline
166          * @param src The sender of the xline
167          * @param re The reason of the xline
168          * @param ident Ident to match
169          * @param host Host to match
170          */
171         KLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& ident, const std::string& host)
172                 : XLine(s_time, d, src, re, "K"), identmask(ident), hostmask(host)
173         {
174                 matchtext = this->identmask;
175                 matchtext.append("@").append(this->hostmask);
176         }
177
178         /** Destructor
179          */
180         ~KLine()
181         {
182         }
183
184         bool Matches(User* u) CXX11_OVERRIDE;
185
186         bool Matches(const std::string& str) CXX11_OVERRIDE;
187
188         void Apply(User* u) CXX11_OVERRIDE;
189
190         const std::string& Displayable() CXX11_OVERRIDE;
191
192         bool IsBurstable() CXX11_OVERRIDE;
193
194         /** Ident mask (ident part only)
195          */
196         std::string identmask;
197         /** Host mask (host part only)
198          */
199         std::string hostmask;
200
201         std::string matchtext;
202 };
203
204 /** GLine class
205  */
206 class CoreExport GLine : public XLine
207 {
208   public:
209         /** Create a G-line.
210          * @param s_time The set time
211          * @param d The duration of the xline
212          * @param src The sender of the xline
213          * @param re The reason of the xline
214          * @param ident Ident to match
215          * @param host Host to match
216          */
217         GLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& ident, const std::string& host)
218                 : XLine(s_time, d, src, re, "G"), identmask(ident), hostmask(host)
219         {
220                 matchtext = this->identmask;
221                 matchtext.append("@").append(this->hostmask);
222         }
223
224         /** Destructor
225          */
226         ~GLine()
227         {
228         }
229
230         bool Matches(User* u) CXX11_OVERRIDE;
231
232         bool Matches(const std::string& str) CXX11_OVERRIDE;
233
234         void Apply(User* u)  CXX11_OVERRIDE;
235
236         const std::string& Displayable() CXX11_OVERRIDE;
237
238         /** Ident mask (ident part only)
239          */
240         std::string identmask;
241         /** Host mask (host part only)
242          */
243         std::string hostmask;
244
245         std::string matchtext;
246 };
247
248 /** ELine class
249  */
250 class CoreExport ELine : public XLine
251 {
252   public:
253         /** Create an E-line.
254          * @param s_time The set time
255          * @param d The duration of the xline
256          * @param src The sender of the xline
257          * @param re The reason of the xline
258          * @param ident Ident to match
259          * @param host Host to match
260          */
261         ELine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& ident, const std::string& host)
262                 : XLine(s_time, d, src, re, "E"), identmask(ident), hostmask(host)
263         {
264                 matchtext = this->identmask;
265                 matchtext.append("@").append(this->hostmask);
266         }
267
268         ~ELine()
269         {
270         }
271
272         bool Matches(User* u) CXX11_OVERRIDE;
273
274         bool Matches(const std::string& str) CXX11_OVERRIDE;
275
276         void Unset() CXX11_OVERRIDE;
277
278         void OnAdd() CXX11_OVERRIDE;
279
280         const std::string& Displayable() CXX11_OVERRIDE;
281
282         /** Ident mask (ident part only)
283          */
284         std::string identmask;
285         /** Host mask (host part only)
286          */
287         std::string hostmask;
288
289         std::string matchtext;
290 };
291
292 /** ZLine class
293  */
294 class CoreExport ZLine : public XLine
295 {
296   public:
297         /** Create a Z-line.
298          * @param s_time The set time
299          * @param d The duration of the xline
300          * @param src The sender of the xline
301          * @param re The reason of the xline
302          * @param ip IP to match
303          */
304         ZLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& ip)
305                 : XLine(s_time, d, src, re, "Z"), ipaddr(ip)
306         {
307         }
308
309         /** Destructor
310          */
311         ~ZLine()
312         {
313         }
314
315         bool Matches(User* u) CXX11_OVERRIDE;
316
317         bool Matches(const std::string& str) CXX11_OVERRIDE;
318
319         void Apply(User* u) CXX11_OVERRIDE;
320
321         const std::string& Displayable() CXX11_OVERRIDE;
322
323         /** IP mask (no ident part)
324          */
325         std::string ipaddr;
326 };
327
328 /** QLine class
329  */
330 class CoreExport QLine : public XLine
331 {
332   public:
333         /** Create a Q-line.
334          * @param s_time The set time
335          * @param d The duration of the xline
336          * @param src The sender of the xline
337          * @param re The reason of the xline
338          * @param nickname Nickname to match
339          */
340         QLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& nickname)
341                 : XLine(s_time, d, src, re, "Q"), nick(nickname)
342         {
343         }
344
345         /** Destructor
346          */
347         ~QLine()
348         {
349         }
350         bool Matches(User* u) CXX11_OVERRIDE;
351
352         bool Matches(const std::string& str) CXX11_OVERRIDE;
353
354         void Apply(User* u) CXX11_OVERRIDE;
355
356         const std::string& Displayable() CXX11_OVERRIDE;
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, unsigned long duration, const std::string& source, const std::string& reason, const 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 G-lines, K-lines, E-lines, Z-lines and Q-lines,
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 separate 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 reason The xline reason, if it is being removed successfully
474          * @param user The user removing the line or NULL if its the local server
475          * @param simulate If this is true, don't actually remove the line, just return
476          * @return True if the line was deleted successfully
477          */
478         bool DelLine(const char* hostmask, const std::string& type, std::string& reason, User* user, bool simulate = false);
479
480         /** Registers an xline factory.
481          * An xline factory is a class which when given a particular xline type,
482          * will generate a new XLine specialized to that type. For example if you
483          * pass the XLineFactory that handles G-lines some data it will return a
484          * pointer to a GLine, polymorphically represented as XLine. This is used where
485          * you do not know the full details of the item you wish to create, e.g. in a
486          * server protocol module like m_spanningtree, when you receive xlines from other
487          * servers.
488          * @param xlf XLineFactory pointer to register
489          */
490         bool RegisterFactory(XLineFactory* xlf);
491
492         /** Unregisters an xline factory.
493          * You must do this when your module unloads.
494          * @param xlf XLineFactory pointer to unregister
495          */
496         bool UnregisterFactory(XLineFactory* xlf);
497
498         /** Get the XLineFactory for a specific type.
499          * Returns NULL if there is no known handler for this xline type.
500          * @param type The type of XLine you require the XLineFactory for
501          */
502         XLineFactory* GetFactory(const std::string &type);
503
504         /** Check if a user matches an XLine
505          * @param type The type of line to look up
506          * @param user The user to match against (what is checked is specific to the xline type)
507          * @return The reason for the line if there is a match, or NULL if there is no match
508          */
509         XLine* MatchesLine(const std::string &type, User* user);
510
511         /** Check if a pattern matches an XLine
512          * @param type The type of line to look up
513          * @param pattern A pattern string specific to the xline type
514          * @return The matching XLine if there is a match, or NULL if there is no match
515          */
516         XLine* MatchesLine(const std::string &type, const std::string &pattern);
517
518         /** Expire a line given two iterators which identify it in the main map.
519          * @param container Iterator to the first level of entries the map
520          * @param item Iterator to the second level of entries in the map
521          * @param silent If true, doesn't send an expiry SNOTICE.
522          */
523         void ExpireLine(ContainerIter container, LookupIter item, bool silent = false);
524
525         /** Apply any new lines that are pending to be applied.
526          * This will only apply lines in the pending_lines list, to save on
527          * CPU time.
528          */
529         void ApplyLines();
530
531         /** DEPRECATED: use the `bool InvokeStats(const std::string&, Stats::Context&)` overload instead. */
532         DEPRECATED_METHOD(void InvokeStats(const std::string& type, unsigned int numeric, Stats::Context& stats));
533
534         /** Generates a /STATS response for the given X-line type.
535          * @param type The type of X-line to look up.
536          * @param context The stats context to respond with.
537          * @return True if a response was sent; otherwise, false.
538          */
539         bool InvokeStats(const std::string& type, Stats::Context& context);
540
541         /** Expire X-lines which were added by the server configuration and have been removed. */
542         void ExpireRemovedConfigLines(const std::string& type, const insp::flat_set<std::string>& configlines);
543 };