]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/xline.h
Use ircd-hybrid's numerics for the "pending invites" list.
[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) 2013, 2016 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2012-2013, 2017-2018 Sadie Powell <sadie@witchery.services>
7  *   Copyright (C) 2012, 2018-2019 Robby <robby@chatbelgie.be>
8  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
9  *   Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
10  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
11  *   Copyright (C) 2006-2008, 2010 Craig Edwards <brain@inspircd.org>
12  *
13  * This file is part of InspIRCd.  InspIRCd is free software: you can
14  * redistribute it and/or modify it under the terms of the GNU General Public
15  * License as published by the Free Software Foundation, version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  */
25
26
27 #pragma once
28
29 /** XLine is the base class for ban lines such as G-lines and K-lines.
30  * Modules may derive from this, and their xlines will automatically be
31  * handled as expected by any protocol modules (e.g. m_spanningtree will
32  * propogate them using AddLine). The process of translating a type+pattern
33  * to a known line type is done by means of an XLineFactory object (see
34  * below).
35  */
36 class CoreExport XLine : public classbase
37 {
38  protected:
39
40         /** Default 'apply' action. Quits the user.
41          * @param u User to apply the line against
42          * @param line The line typed, used for display purposes in the quit message
43          * @param bancache If true, the user will be added to the bancache if they match. Else not.
44          */
45         void DefaultApply(User* u, const std::string &line, bool bancache);
46
47  public:
48
49         /** Create an XLine.
50          * @param s_time The set time
51          * @param d The duration of the xline
52          * @param src The sender of the xline
53          * @param re The reason of the xline
54          * @param t The line type, should be set by the derived class constructor
55          */
56         XLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& t)
57                 : set_time(s_time)
58                 , duration(d)
59                 , source(src)
60                 , reason(re)
61                 , type(t)
62                 , from_config(false)
63         {
64                 expiry = set_time + duration;
65         }
66
67         /** Destructor
68          */
69         virtual ~XLine()
70         {
71         }
72
73         /** Change creation time of an xline. Updates expiry
74          * to be after the creation time.
75          */
76         virtual void SetCreateTime(time_t created)
77         {
78                 set_time = created;
79                 expiry = created + duration;
80         }
81
82         /** Returns true whether or not the given user is covered by this line.
83          * @param u The user to match against. The mechanics of the match
84          * are defined by the derived class.
85          * @return True if there is a match.
86          */
87         virtual bool Matches(User *u) = 0;
88
89         /** Returns true whether or not the given string is covered by this line.
90          * @param str The string to match against. The details of what must be
91          * in this string and the mechanics of the match are defined by the
92          * derived class.
93          * @return True if there is a match
94          */
95         virtual bool Matches(const std::string &str) = 0;
96
97         /** Apply a line against a user. The mechanics of what occurs when
98          * the line is applied are specific to the derived class.
99          * @param u The user to apply against
100          */
101         virtual void Apply(User* u);
102
103         /** Called when the line is unset either via expiry or
104          * via explicit removal.
105          */
106         virtual void Unset() { }
107
108         /** Called when the expiry message is to be displayed for the
109          * line. Usually a line in the form 'expiring X-line blah, set by...'
110          * see the DisplayExpiry methods of GLine, ELine etc.
111          */
112         virtual void DisplayExpiry();
113
114         /** Returns the displayable form of the pattern for this xline,
115          * e.g. '*\@foo' or '*baz*'. This must always return the full pattern
116          * in a form which can be used to construct an entire derived xline,
117          * even if it is stored differently internally (e.g. GLine stores the
118          * ident and host parts separately but will still return ident\@host
119          * for its Displayable() method).
120          */
121         virtual const std::string& Displayable() = 0;
122
123         /** Called when the xline has just been added.
124          */
125         virtual void OnAdd() { }
126
127         /** The time the line was added.
128          */
129         time_t set_time;
130
131         /** The duration of the ban, or 0 if permenant
132          */
133         unsigned long duration;
134
135         /** Source of the ban. This can be a servername or an oper nickname
136          */
137         std::string source;
138
139         /** Reason for the ban
140          */
141         std::string reason;
142
143         /** Expiry time. Does not contain useful data if the duration is 0.
144          */
145         time_t expiry;
146
147         /** "Q", "K", etc. Set only by derived classes constructor to the
148          * type of line this is.
149          */
150         const std::string type;
151
152         // Whether this XLine was loaded from the server config.
153         bool from_config;
154
155         virtual bool IsBurstable();
156 };
157
158 /** KLine class
159  */
160 class CoreExport KLine : public XLine
161 {
162   public:
163
164         /** Create a K-line.
165          * @param s_time The set time
166          * @param d The duration of the xline
167          * @param src The sender of the xline
168          * @param re The reason of the xline
169          * @param ident Ident to match
170          * @param host Host to match
171          */
172         KLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& ident, const std::string& host)
173                 : XLine(s_time, d, src, re, "K"), identmask(ident), hostmask(host)
174         {
175                 matchtext = this->identmask;
176                 matchtext.append("@").append(this->hostmask);
177         }
178
179         /** Destructor
180          */
181         ~KLine()
182         {
183         }
184
185         bool Matches(User* u) CXX11_OVERRIDE;
186
187         bool Matches(const std::string& str) CXX11_OVERRIDE;
188
189         void Apply(User* u) CXX11_OVERRIDE;
190
191         const std::string& Displayable() CXX11_OVERRIDE;
192
193         bool IsBurstable() CXX11_OVERRIDE;
194
195         /** Ident mask (ident part only)
196          */
197         std::string identmask;
198         /** Host mask (host part only)
199          */
200         std::string hostmask;
201
202         std::string matchtext;
203 };
204
205 /** GLine class
206  */
207 class CoreExport GLine : public XLine
208 {
209   public:
210         /** Create a G-line.
211          * @param s_time The set time
212          * @param d The duration of the xline
213          * @param src The sender of the xline
214          * @param re The reason of the xline
215          * @param ident Ident to match
216          * @param host Host to match
217          */
218         GLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& ident, const std::string& host)
219                 : XLine(s_time, d, src, re, "G"), identmask(ident), hostmask(host)
220         {
221                 matchtext = this->identmask;
222                 matchtext.append("@").append(this->hostmask);
223         }
224
225         /** Destructor
226          */
227         ~GLine()
228         {
229         }
230
231         bool Matches(User* u) CXX11_OVERRIDE;
232
233         bool Matches(const std::string& str) CXX11_OVERRIDE;
234
235         void Apply(User* u)  CXX11_OVERRIDE;
236
237         const std::string& Displayable() CXX11_OVERRIDE;
238
239         /** Ident mask (ident part only)
240          */
241         std::string identmask;
242         /** Host mask (host part only)
243          */
244         std::string hostmask;
245
246         std::string matchtext;
247 };
248
249 /** ELine class
250  */
251 class CoreExport ELine : public XLine
252 {
253   public:
254         /** Create an E-line.
255          * @param s_time The set time
256          * @param d The duration of the xline
257          * @param src The sender of the xline
258          * @param re The reason of the xline
259          * @param ident Ident to match
260          * @param host Host to match
261          */
262         ELine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& ident, const std::string& host)
263                 : XLine(s_time, d, src, re, "E"), identmask(ident), hostmask(host)
264         {
265                 matchtext = this->identmask;
266                 matchtext.append("@").append(this->hostmask);
267         }
268
269         ~ELine()
270         {
271         }
272
273         bool Matches(User* u) CXX11_OVERRIDE;
274
275         bool Matches(const std::string& str) CXX11_OVERRIDE;
276
277         void Unset() CXX11_OVERRIDE;
278
279         void OnAdd() CXX11_OVERRIDE;
280
281         const std::string& Displayable() CXX11_OVERRIDE;
282
283         /** Ident mask (ident part only)
284          */
285         std::string identmask;
286         /** Host mask (host part only)
287          */
288         std::string hostmask;
289
290         std::string matchtext;
291 };
292
293 /** ZLine class
294  */
295 class CoreExport ZLine : public XLine
296 {
297   public:
298         /** Create a Z-line.
299          * @param s_time The set time
300          * @param d The duration of the xline
301          * @param src The sender of the xline
302          * @param re The reason of the xline
303          * @param ip IP to match
304          */
305         ZLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& ip)
306                 : XLine(s_time, d, src, re, "Z"), ipaddr(ip)
307         {
308         }
309
310         /** Destructor
311          */
312         ~ZLine()
313         {
314         }
315
316         bool Matches(User* u) CXX11_OVERRIDE;
317
318         bool Matches(const std::string& str) CXX11_OVERRIDE;
319
320         void Apply(User* u) CXX11_OVERRIDE;
321
322         const std::string& Displayable() CXX11_OVERRIDE;
323
324         /** IP mask (no ident part)
325          */
326         std::string ipaddr;
327 };
328
329 /** QLine class
330  */
331 class CoreExport QLine : public XLine
332 {
333   public:
334         /** Create a Q-line.
335          * @param s_time The set time
336          * @param d The duration of the xline
337          * @param src The sender of the xline
338          * @param re The reason of the xline
339          * @param nickname Nickname to match
340          */
341         QLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& nickname)
342                 : XLine(s_time, d, src, re, "Q"), nick(nickname)
343         {
344         }
345
346         /** Destructor
347          */
348         ~QLine()
349         {
350         }
351         bool Matches(User* u) CXX11_OVERRIDE;
352
353         bool Matches(const std::string& str) CXX11_OVERRIDE;
354
355         void Apply(User* u) CXX11_OVERRIDE;
356
357         const std::string& Displayable() CXX11_OVERRIDE;
358
359         /** Nickname mask
360          */
361         std::string nick;
362 };
363
364 /** XLineFactory is used to generate an XLine pointer, given just the
365  * pattern, timing information and type of line to create. This is used
366  * for example in the spanningtree module which will call an XLineFactory
367  * to create a new XLine when it is inbound on a server link, so that it
368  * does not have to know the specifics of the internals of an XLine class
369  * and/or how to call its constructor.
370  */
371 class CoreExport XLineFactory
372 {
373  protected:
374
375         std::string type;
376
377  public:
378
379         /** Create an XLine factory
380          * @param t Type of XLine this factory generates
381          */
382         XLineFactory(const std::string &t) : type(t) { }
383
384         /** Return the type of XLine this factory generates
385          * @return The type of XLine this factory generates
386          */
387         virtual const std::string& GetType() { return type; }
388
389         /** Generate a specialized XLine*.
390          * @param set_time Time this line was created
391          * @param duration Duration of the line
392          * @param source The sender of the line, nickname or server
393          * @param reason The reason for the line
394          * @param xline_specific_mask The mask string for the line, specific to the XLine type being created.
395          * @return A specialized XLine class of the given type for this factory.
396          */
397         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;
398
399         virtual bool AutoApplyToUserList(XLine* x) { return true; }
400
401         /** Destructor
402          */
403         virtual ~XLineFactory() { }
404 };
405
406 /** XLineManager is a class used to manage G-lines, K-lines, E-lines, Z-lines and Q-lines,
407  * or any other line created by a module. It also manages XLineFactory classes which
408  * can generate a specialized XLine for use by another module.
409  */
410 class CoreExport XLineManager
411 {
412  protected:
413         /** Used to hold XLines which have not yet been applied.
414          */
415         std::vector<XLine *> pending_lines;
416
417         /** Current xline factories
418          */
419         XLineFactMap line_factory;
420
421         /** Container of all lines, this is a map of maps which
422          * allows for fast lookup for add/remove of a line, and
423          * the shortest possible timed O(n) for checking a user
424          * against a line.
425          */
426         XLineContainer lookup_lines;
427
428  public:
429
430         /** Constructor
431          */
432         XLineManager();
433
434         /** Destructor
435          */
436         ~XLineManager();
437
438         /** Split an ident and host into two seperate strings.
439          * This allows for faster matching.
440          */
441         IdentHostPair IdentSplit(const std::string &ident_and_host);
442
443         /** Checks what users match E-lines and sets their ban exempt flag accordingly.
444          */
445         void CheckELines();
446
447         /** Get all lines of a certain type to an XLineLookup (std::map<std::string, XLine*>).
448          * NOTE: When this function runs any expired items are removed from the list before it
449          * is returned to the caller.
450          * @param type The type to look up
451          * @return A list of all XLines of the given type.
452          */
453         XLineLookup* GetAll(const std::string &type);
454
455         /** Remove all lines of a certain type.
456          */
457         void DelAll(const std::string &type);
458
459         /** Return all known types of line currently stored by the XLineManager.
460          * @return A vector containing all known line types currently stored in the main list.
461          */
462         std::vector<std::string> GetAllTypes();
463
464         /** Add a new XLine
465          * @param line The line to be added
466          * @param user The user adding the line or NULL for the local server
467          * @return True if the line was added successfully
468          */
469         bool AddLine(XLine* line, User* user);
470
471         /** Delete an XLine
472          * @param hostmask The xline-specific string identifying the line, e.g. "*@foo"
473          * @param type The type of xline
474          * @param reason The xline reason, if it is being removed successfully
475          * @param user The user removing the line or NULL if its the local server
476          * @param simulate If this is true, don't actually remove the line, just return
477          * @return True if the line was deleted successfully
478          */
479         bool DelLine(const char* hostmask, const std::string& type, std::string& reason, User* user, bool simulate = false);
480
481         /** Registers an xline factory.
482          * An xline factory is a class which when given a particular xline type,
483          * will generate a new XLine specialized to that type. For example if you
484          * pass the XLineFactory that handles G-lines some data it will return a
485          * pointer to a GLine, polymorphically represented as XLine. This is used where
486          * you do not know the full details of the item you wish to create, e.g. in a
487          * server protocol module like m_spanningtree, when you receive xlines from other
488          * servers.
489          * @param xlf XLineFactory pointer to register
490          */
491         bool RegisterFactory(XLineFactory* xlf);
492
493         /** Unregisters an xline factory.
494          * You must do this when your module unloads.
495          * @param xlf XLineFactory pointer to unregister
496          */
497         bool UnregisterFactory(XLineFactory* xlf);
498
499         /** Get the XLineFactory for a specific type.
500          * Returns NULL if there is no known handler for this xline type.
501          * @param type The type of XLine you require the XLineFactory for
502          */
503         XLineFactory* GetFactory(const std::string &type);
504
505         /** Check if a user matches an XLine
506          * @param type The type of line to look up
507          * @param user The user to match against (what is checked is specific to the xline type)
508          * @return The reason for the line if there is a match, or NULL if there is no match
509          */
510         XLine* MatchesLine(const std::string &type, User* user);
511
512         /** Check if a pattern matches an XLine
513          * @param type The type of line to look up
514          * @param pattern A pattern string specific to the xline type
515          * @return The matching XLine if there is a match, or NULL if there is no match
516          */
517         XLine* MatchesLine(const std::string &type, const std::string &pattern);
518
519         /** Expire a line given two iterators which identify it in the main map.
520          * @param container Iterator to the first level of entries the map
521          * @param item Iterator to the second level of entries in the map
522          * @param silent If true, doesn't send an expiry SNOTICE.
523          */
524         void ExpireLine(ContainerIter container, LookupIter item, bool silent = false);
525
526         /** Apply any new lines that are pending to be applied.
527          * This will only apply lines in the pending_lines list, to save on
528          * CPU time.
529          */
530         void ApplyLines();
531
532         /** DEPRECATED: use the `bool InvokeStats(const std::string&, Stats::Context&)` overload instead. */
533         DEPRECATED_METHOD(void InvokeStats(const std::string& type, unsigned int numeric, Stats::Context& stats));
534
535         /** Generates a /STATS response for the given X-line type.
536          * @param type The type of X-line to look up.
537          * @param context The stats context to respond with.
538          * @return True if a response was sent; otherwise, false.
539          */
540         bool InvokeStats(const std::string& type, Stats::Context& context);
541
542         /** Expire X-lines which were added by the server configuration and have been removed. */
543         void ExpireRemovedConfigLines(const std::string& type, const insp::flat_set<std::string>& configlines);
544 };