2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
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
35 class CoreExport XLine : public classbase
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.
44 void DefaultApply(User* u, const std::string &line, bool bancache);
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
55 XLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& t)
63 expiry = set_time + duration;
72 /** Change creation time of an xline. Updates expiry
73 * to be after the creation time.
75 virtual void SetCreateTime(time_t created)
78 expiry = created + duration;
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.
86 virtual bool Matches(User *u) = 0;
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
92 * @return True if there is a match
94 virtual bool Matches(const std::string &str) = 0;
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
100 virtual void Apply(User* u);
102 /** Called when the line is unset either via expiry or
103 * via explicit removal.
105 virtual void Unset() { }
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.
111 virtual void DisplayExpiry();
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).
120 virtual const std::string& Displayable() = 0;
122 /** Called when the xline has just been added.
124 virtual void OnAdd() { }
126 /** The time the line was added.
130 /** The duration of the ban, or 0 if permanent
132 unsigned long duration;
134 /** Source of the ban. This can be a servername or an oper nickname
138 /** Reason for the ban
142 /** Expiry time. Does not contain useful data if the duration is 0.
146 /** "Q", "K", etc. Set only by derived classes constructor to the
147 * type of line this is.
149 const std::string type;
151 // Whether this XLine was loaded from the server config.
154 virtual bool IsBurstable();
159 class CoreExport KLine : public XLine
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
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)
174 matchtext = this->identmask;
175 matchtext.append("@").append(this->hostmask);
184 bool Matches(User* u) CXX11_OVERRIDE;
186 bool Matches(const std::string& str) CXX11_OVERRIDE;
188 void Apply(User* u) CXX11_OVERRIDE;
190 const std::string& Displayable() CXX11_OVERRIDE;
192 bool IsBurstable() CXX11_OVERRIDE;
194 /** Ident mask (ident part only)
196 std::string identmask;
197 /** Host mask (host part only)
199 std::string hostmask;
201 std::string matchtext;
206 class CoreExport GLine : public XLine
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
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)
220 matchtext = this->identmask;
221 matchtext.append("@").append(this->hostmask);
230 bool Matches(User* u) CXX11_OVERRIDE;
232 bool Matches(const std::string& str) CXX11_OVERRIDE;
234 void Apply(User* u) CXX11_OVERRIDE;
236 const std::string& Displayable() CXX11_OVERRIDE;
238 /** Ident mask (ident part only)
240 std::string identmask;
241 /** Host mask (host part only)
243 std::string hostmask;
245 std::string matchtext;
250 class CoreExport ELine : public XLine
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
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)
264 matchtext = this->identmask;
265 matchtext.append("@").append(this->hostmask);
272 bool Matches(User* u) CXX11_OVERRIDE;
274 bool Matches(const std::string& str) CXX11_OVERRIDE;
276 void Unset() CXX11_OVERRIDE;
278 void OnAdd() CXX11_OVERRIDE;
280 const std::string& Displayable() CXX11_OVERRIDE;
282 /** Ident mask (ident part only)
284 std::string identmask;
285 /** Host mask (host part only)
287 std::string hostmask;
289 std::string matchtext;
294 class CoreExport ZLine : public XLine
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
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)
315 bool Matches(User* u) CXX11_OVERRIDE;
317 bool Matches(const std::string& str) CXX11_OVERRIDE;
319 void Apply(User* u) CXX11_OVERRIDE;
321 const std::string& Displayable() CXX11_OVERRIDE;
323 /** IP mask (no ident part)
330 class CoreExport QLine : public XLine
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
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)
350 bool Matches(User* u) CXX11_OVERRIDE;
352 bool Matches(const std::string& str) CXX11_OVERRIDE;
354 void Apply(User* u) CXX11_OVERRIDE;
356 const std::string& Displayable() CXX11_OVERRIDE;
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.
370 class CoreExport XLineFactory
378 /** Create an XLine factory
379 * @param t Type of XLine this factory generates
381 XLineFactory(const std::string &t) : type(t) { }
383 /** Return the type of XLine this factory generates
384 * @return The type of XLine this factory generates
386 virtual const std::string& GetType() { return type; }
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.
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;
398 virtual bool AutoApplyToUserList(XLine* x) { return true; }
402 virtual ~XLineFactory() { }
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.
409 class CoreExport XLineManager
412 /** Used to hold XLines which have not yet been applied.
414 std::vector<XLine *> pending_lines;
416 /** Current xline factories
418 XLineFactMap line_factory;
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
425 XLineContainer lookup_lines;
437 /** Split an ident and host into two separate strings.
438 * This allows for faster matching.
440 IdentHostPair IdentSplit(const std::string &ident_and_host);
442 /** Checks what users match E-lines and sets their ban exempt flag accordingly.
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.
452 XLineLookup* GetAll(const std::string &type);
454 /** Remove all lines of a certain type.
456 void DelAll(const std::string &type);
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.
461 std::vector<std::string> GetAllTypes();
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
468 bool AddLine(XLine* line, User* user);
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
478 bool DelLine(const char* hostmask, const std::string& type, std::string& reason, User* user, bool simulate = false);
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
488 * @param xlf XLineFactory pointer to register
490 bool RegisterFactory(XLineFactory* xlf);
492 /** Unregisters an xline factory.
493 * You must do this when your module unloads.
494 * @param xlf XLineFactory pointer to unregister
496 bool UnregisterFactory(XLineFactory* xlf);
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
502 XLineFactory* GetFactory(const std::string &type);
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
509 XLine* MatchesLine(const std::string &type, User* user);
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
516 XLine* MatchesLine(const std::string &type, const std::string &pattern);
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.
523 void ExpireLine(ContainerIter container, LookupIter item, bool silent = false);
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
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));
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.
539 bool InvokeStats(const std::string& type, Stats::Context& context);
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);