2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
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
36 class CoreExport XLine : public classbase
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.
45 void DefaultApply(User* u, const std::string &line, bool bancache);
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
56 XLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& t)
64 expiry = set_time + duration;
73 /** Change creation time of an xline. Updates expiry
74 * to be after the creation time.
76 virtual void SetCreateTime(time_t created)
79 expiry = created + duration;
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.
87 virtual bool Matches(User *u) = 0;
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
93 * @return True if there is a match
95 virtual bool Matches(const std::string &str) = 0;
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
101 virtual void Apply(User* u);
103 /** Called when the line is unset either via expiry or
104 * via explicit removal.
106 virtual void Unset() { }
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.
112 virtual void DisplayExpiry();
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).
121 virtual const std::string& Displayable() = 0;
123 /** Called when the xline has just been added.
125 virtual void OnAdd() { }
127 /** The time the line was added.
131 /** The duration of the ban, or 0 if permenant
133 unsigned long duration;
135 /** Source of the ban. This can be a servername or an oper nickname
139 /** Reason for the ban
143 /** Expiry time. Does not contain useful data if the duration is 0.
147 /** "Q", "K", etc. Set only by derived classes constructor to the
148 * type of line this is.
150 const std::string type;
152 // Whether this XLine was loaded from the server config.
155 virtual bool IsBurstable();
160 class CoreExport KLine : public XLine
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
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)
175 matchtext = this->identmask;
176 matchtext.append("@").append(this->hostmask);
185 bool Matches(User* u) CXX11_OVERRIDE;
187 bool Matches(const std::string& str) CXX11_OVERRIDE;
189 void Apply(User* u) CXX11_OVERRIDE;
191 const std::string& Displayable() CXX11_OVERRIDE;
193 bool IsBurstable() CXX11_OVERRIDE;
195 /** Ident mask (ident part only)
197 std::string identmask;
198 /** Host mask (host part only)
200 std::string hostmask;
202 std::string matchtext;
207 class CoreExport GLine : public XLine
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
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)
221 matchtext = this->identmask;
222 matchtext.append("@").append(this->hostmask);
231 bool Matches(User* u) CXX11_OVERRIDE;
233 bool Matches(const std::string& str) CXX11_OVERRIDE;
235 void Apply(User* u) CXX11_OVERRIDE;
237 const std::string& Displayable() CXX11_OVERRIDE;
239 /** Ident mask (ident part only)
241 std::string identmask;
242 /** Host mask (host part only)
244 std::string hostmask;
246 std::string matchtext;
251 class CoreExport ELine : public XLine
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
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)
265 matchtext = this->identmask;
266 matchtext.append("@").append(this->hostmask);
273 bool Matches(User* u) CXX11_OVERRIDE;
275 bool Matches(const std::string& str) CXX11_OVERRIDE;
277 void Unset() CXX11_OVERRIDE;
279 void OnAdd() CXX11_OVERRIDE;
281 const std::string& Displayable() CXX11_OVERRIDE;
283 /** Ident mask (ident part only)
285 std::string identmask;
286 /** Host mask (host part only)
288 std::string hostmask;
290 std::string matchtext;
295 class CoreExport ZLine : public XLine
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
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)
316 bool Matches(User* u) CXX11_OVERRIDE;
318 bool Matches(const std::string& str) CXX11_OVERRIDE;
320 void Apply(User* u) CXX11_OVERRIDE;
322 const std::string& Displayable() CXX11_OVERRIDE;
324 /** IP mask (no ident part)
331 class CoreExport QLine : public XLine
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
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)
351 bool Matches(User* u) CXX11_OVERRIDE;
353 bool Matches(const std::string& str) CXX11_OVERRIDE;
355 void Apply(User* u) CXX11_OVERRIDE;
357 const std::string& Displayable() CXX11_OVERRIDE;
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.
371 class CoreExport XLineFactory
379 /** Create an XLine factory
380 * @param t Type of XLine this factory generates
382 XLineFactory(const std::string &t) : type(t) { }
384 /** Return the type of XLine this factory generates
385 * @return The type of XLine this factory generates
387 virtual const std::string& GetType() { return type; }
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.
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;
399 virtual bool AutoApplyToUserList(XLine* x) { return true; }
403 virtual ~XLineFactory() { }
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.
410 class CoreExport XLineManager
413 /** Used to hold XLines which have not yet been applied.
415 std::vector<XLine *> pending_lines;
417 /** Current xline factories
419 XLineFactMap line_factory;
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
426 XLineContainer lookup_lines;
438 /** Split an ident and host into two seperate strings.
439 * This allows for faster matching.
441 IdentHostPair IdentSplit(const std::string &ident_and_host);
443 /** Checks what users match E-lines and sets their ban exempt flag accordingly.
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.
453 XLineLookup* GetAll(const std::string &type);
455 /** Remove all lines of a certain type.
457 void DelAll(const std::string &type);
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.
462 std::vector<std::string> GetAllTypes();
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
469 bool AddLine(XLine* line, User* user);
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
479 bool DelLine(const char* hostmask, const std::string& type, std::string& reason, User* user, bool simulate = false);
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
489 * @param xlf XLineFactory pointer to register
491 bool RegisterFactory(XLineFactory* xlf);
493 /** Unregisters an xline factory.
494 * You must do this when your module unloads.
495 * @param xlf XLineFactory pointer to unregister
497 bool UnregisterFactory(XLineFactory* xlf);
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
503 XLineFactory* GetFactory(const std::string &type);
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
510 XLine* MatchesLine(const std::string &type, User* user);
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
517 XLine* MatchesLine(const std::string &type, const std::string &pattern);
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.
524 void ExpireLine(ContainerIter container, LookupIter item, bool silent = false);
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
532 /** Handle /STATS for a given type.
533 * NOTE: Any items in the list for this particular line type which have expired
534 * will be expired and removed before the list is displayed.
535 * @param type The type of stats to show
536 * @param numeric The numeric to give to each result line
537 * @param stats Stats context
539 void InvokeStats(const std::string& type, unsigned int numeric, Stats::Context& stats);
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);