2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2004-2007 Craig Edwards <craigedwards@brainbox.cc>
6 * Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
7 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
9 * This file is part of InspIRCd. InspIRCd is free software: you can
10 * redistribute it and/or modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation, version 2.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 /** XLine is the base class for ban lines such as G lines and K lines.
26 * Modules may derive from this, and their xlines will automatically be
27 * handled as expected by any protocol modules (e.g. m_spanningtree will
28 * propogate them using AddLine). The process of translating a type+pattern
29 * to a known line type is done by means of an XLineFactory object (see
32 class CoreExport XLine : public classbase
36 /** Default 'apply' action. Quits the user.
37 * @param u User to apply the line against
38 * @param line The line typed, used for display purposes in the quit message
39 * @param bancache If true, the user will be added to the bancache if they match. Else not.
41 void DefaultApply(User* u, const std::string &line, bool bancache);
46 * @param s_time The set time
47 * @param d The duration of the xline
48 * @param src The sender of the xline
49 * @param re The reason of the xline
50 * @param t The line type, should be set by the derived class constructor
52 XLine(time_t s_time, long d, std::string src, std::string re, const std::string &t)
60 expiry = set_time + duration;
69 /** Change creation time of an xline. Updates expiry
70 * to be after the creation time
72 virtual void SetCreateTime(time_t created)
75 expiry = created + duration;
78 /** Returns true whether or not the given user is covered by this line.
79 * @param u The user to match against. The mechanics of the match
80 * are defined by the derived class.
81 * @return True if there is a match.
83 virtual bool Matches(User *u) = 0;
85 /** Returns true whether or not the given string is covered by this line.
86 * @param str The string to match against. The details of what must be
87 * in this string and the mechanics of the match are defined by the
89 * @return True if there is a match
91 virtual bool Matches(const std::string &str) = 0;
93 /** Apply a line against a user. The mechanics of what occurs when
94 * the line is applied are specific to the derived class.
95 * @param u The user to apply against
97 virtual void Apply(User* u);
99 /** Called when the line is unset either via expiry or
100 * via explicit removal.
102 virtual void Unset() { }
104 /** Called when the expiry message is to be displayed for the
105 * line. Usually a line in the form 'expiring Xline blah, set by...'
106 * see the DisplayExpiry methods of GLine, ELine etc.
108 virtual void DisplayExpiry();
110 /** Returns the displayable form of the pattern for this xline,
111 * e.g. '*\@foo' or '*baz*'. This must always return the full pattern
112 * in a form which can be used to construct an entire derived xline,
113 * even if it is stored differently internally (e.g. GLine stores the
114 * ident and host parts seperately but will still return ident\@host
115 * for its Displayable() method).
117 virtual const std::string& Displayable() = 0;
119 /** Called when the xline has just been added.
121 virtual void OnAdd() { }
123 /** The time the line was added.
127 /** The duration of the ban, or 0 if permenant
131 /** Source of the ban. This can be a servername or an oper nickname
135 /** Reason for the ban
139 /** Expiry time. Does not contain useful data if the duration is 0.
143 /** "Q", "K", etc. Set only by derived classes constructor to the
144 * type of line this is.
146 const std::string type;
148 // Whether this XLine was loaded from the server config.
151 virtual bool IsBurstable();
156 class CoreExport KLine : public XLine
161 * @param s_time The set time
162 * @param d The duration of the xline
163 * @param src The sender of the xline
164 * @param re The reason of the xline
165 * @param ident Ident to match
166 * @param host Host to match
168 KLine(time_t s_time, long d, std::string src, std::string re, std::string ident, std::string host)
169 : XLine(s_time, d, src, re, "K"), identmask(ident), hostmask(host)
171 matchtext = this->identmask;
172 matchtext.append("@").append(this->hostmask);
181 bool Matches(User* u) CXX11_OVERRIDE;
183 bool Matches(const std::string& str) CXX11_OVERRIDE;
185 void Apply(User* u) CXX11_OVERRIDE;
187 const std::string& Displayable() CXX11_OVERRIDE;
189 bool IsBurstable() CXX11_OVERRIDE;
191 /** Ident mask (ident part only)
193 std::string identmask;
194 /** Host mask (host part only)
196 std::string hostmask;
198 std::string matchtext;
203 class CoreExport GLine : public XLine
207 * @param s_time The set time
208 * @param d The duration of the xline
209 * @param src The sender of the xline
210 * @param re The reason of the xline
211 * @param ident Ident to match
212 * @param host Host to match
214 GLine(time_t s_time, long d, std::string src, std::string re, std::string ident, std::string host)
215 : XLine(s_time, d, src, re, "G"), identmask(ident), hostmask(host)
217 matchtext = this->identmask;
218 matchtext.append("@").append(this->hostmask);
227 bool Matches(User* u) CXX11_OVERRIDE;
229 bool Matches(const std::string& str) CXX11_OVERRIDE;
231 void Apply(User* u) CXX11_OVERRIDE;
233 const std::string& Displayable() CXX11_OVERRIDE;
235 /** Ident mask (ident part only)
237 std::string identmask;
238 /** Host mask (host part only)
240 std::string hostmask;
242 std::string matchtext;
247 class CoreExport ELine : public XLine
250 /** Create an E-Line.
251 * @param s_time The set time
252 * @param d The duration of the xline
253 * @param src The sender of the xline
254 * @param re The reason of the xline
255 * @param ident Ident to match
256 * @param host Host to match
258 ELine(time_t s_time, long d, std::string src, std::string re, std::string ident, std::string host)
259 : XLine(s_time, d, src, re, "E"), identmask(ident), hostmask(host)
261 matchtext = this->identmask;
262 matchtext.append("@").append(this->hostmask);
269 bool Matches(User* u) CXX11_OVERRIDE;
271 bool Matches(const std::string& str) CXX11_OVERRIDE;
273 void Unset() CXX11_OVERRIDE;
275 void OnAdd() CXX11_OVERRIDE;
277 const std::string& Displayable() CXX11_OVERRIDE;
279 /** Ident mask (ident part only)
281 std::string identmask;
282 /** Host mask (host part only)
284 std::string hostmask;
286 std::string matchtext;
291 class CoreExport ZLine : public XLine
295 * @param s_time The set time
296 * @param d The duration of the xline
297 * @param src The sender of the xline
298 * @param re The reason of the xline
299 * @param ip IP to match
301 ZLine(time_t s_time, long d, std::string src, std::string re, std::string ip)
302 : XLine(s_time, d, src, re, "Z"), ipaddr(ip)
312 bool Matches(User* u) CXX11_OVERRIDE;
314 bool Matches(const std::string& str) CXX11_OVERRIDE;
316 void Apply(User* u) CXX11_OVERRIDE;
318 const std::string& Displayable() CXX11_OVERRIDE;
320 /** IP mask (no ident part)
327 class CoreExport QLine : public XLine
331 * @param s_time The set time
332 * @param d The duration of the xline
333 * @param src The sender of the xline
334 * @param re The reason of the xline
335 * @param nickname Nickname to match
337 QLine(time_t s_time, long d, std::string src, std::string re, std::string nickname)
338 : XLine(s_time, d, src, re, "Q"), nick(nickname)
347 bool Matches(User* u) CXX11_OVERRIDE;
349 bool Matches(const std::string& str) CXX11_OVERRIDE;
351 void Apply(User* u) CXX11_OVERRIDE;
353 const std::string& Displayable() CXX11_OVERRIDE;
360 /** XLineFactory is used to generate an XLine pointer, given just the
361 * pattern, timing information and type of line to create. This is used
362 * for example in the spanningtree module which will call an XLineFactory
363 * to create a new XLine when it is inbound on a server link, so that it
364 * does not have to know the specifics of the internals of an XLine class
365 * and/or how to call its constructor.
367 class CoreExport XLineFactory
375 /** Create an XLine factory
376 * @param t Type of XLine this factory generates
378 XLineFactory(const std::string &t) : type(t) { }
380 /** Return the type of XLine this factory generates
381 * @return The type of XLine this factory generates
383 virtual const std::string& GetType() { return type; }
385 /** Generate a specialized XLine*.
386 * @param set_time Time this line was created
387 * @param duration Duration of the line
388 * @param source The sender of the line, nickname or server
389 * @param reason The reason for the line
390 * @param xline_specific_mask The mask string for the line, specific to the XLine type being created.
391 * @return A specialized XLine class of the given type for this factory.
393 virtual XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask) = 0;
395 virtual bool AutoApplyToUserList(XLine* x) { return true; }
399 virtual ~XLineFactory() { }
402 /** XLineManager is a class used to manage glines, klines, elines, zlines and qlines,
403 * or any other line created by a module. It also manages XLineFactory classes which
404 * can generate a specialized XLine for use by another module.
406 class CoreExport XLineManager
409 /** Used to hold XLines which have not yet been applied.
411 std::vector<XLine *> pending_lines;
413 /** Current xline factories
415 XLineFactMap line_factory;
417 /** Container of all lines, this is a map of maps which
418 * allows for fast lookup for add/remove of a line, and
419 * the shortest possible timed O(n) for checking a user
422 XLineContainer lookup_lines;
434 /** Split an ident and host into two seperate strings.
435 * This allows for faster matching.
437 IdentHostPair IdentSplit(const std::string &ident_and_host);
439 /** Checks what users match e:lines and sets their ban exempt flag accordingly.
443 /** Get all lines of a certain type to an XLineLookup (std::map<std::string, XLine*>).
444 * NOTE: When this function runs any expired items are removed from the list before it
445 * is returned to the caller.
446 * @param type The type to look up
447 * @return A list of all XLines of the given type.
449 XLineLookup* GetAll(const std::string &type);
451 /** Remove all lines of a certain type.
453 void DelAll(const std::string &type);
455 /** Return all known types of line currently stored by the XLineManager.
456 * @return A vector containing all known line types currently stored in the main list.
458 std::vector<std::string> GetAllTypes();
461 * @param line The line to be added
462 * @param user The user adding the line or NULL for the local server
463 * @return True if the line was added successfully
465 bool AddLine(XLine* line, User* user);
468 * @param hostmask The xline-specific string identifying the line, e.g. "*@foo"
469 * @param type The type of xline
470 * @param user The user removing the line or NULL if its the local server
471 * @param simulate If this is true, don't actually remove the line, just return
472 * @return True if the line was deleted successfully
474 bool DelLine(const char* hostmask, const std::string &type, User* user, bool simulate = false);
476 /** Registers an xline factory.
477 * An xline factory is a class which when given a particular xline type,
478 * will generate a new XLine specialized to that type. For example if you
479 * pass the XLineFactory that handles glines some data it will return a
480 * pointer to a GLine, polymorphically represented as XLine. This is used where
481 * you do not know the full details of the item you wish to create, e.g. in a
482 * server protocol module like m_spanningtree, when you receive xlines from other
484 * @param xlf XLineFactory pointer to register
486 bool RegisterFactory(XLineFactory* xlf);
488 /** Unregisters an xline factory.
489 * You must do this when your module unloads.
490 * @param xlf XLineFactory pointer to unregister
492 bool UnregisterFactory(XLineFactory* xlf);
494 /** Get the XLineFactory for a specific type.
495 * Returns NULL if there is no known handler for this xline type.
496 * @param type The type of XLine you require the XLineFactory for
498 XLineFactory* GetFactory(const std::string &type);
500 /** Check if a user matches an XLine
501 * @param type The type of line to look up
502 * @param user The user to match against (what is checked is specific to the xline type)
503 * @return The reason for the line if there is a match, or NULL if there is no match
505 XLine* MatchesLine(const std::string &type, User* user);
507 /** Check if a pattern matches an XLine
508 * @param type The type of line to look up
509 * @param pattern A pattern string specific to the xline type
510 * @return The matching XLine if there is a match, or NULL if there is no match
512 XLine* MatchesLine(const std::string &type, const std::string &pattern);
514 /** Expire a line given two iterators which identify it in the main map.
515 * @param container Iterator to the first level of entries the map
516 * @param item Iterator to the second level of entries in the map
518 void ExpireLine(ContainerIter container, LookupIter item);
520 /** Apply any new lines that are pending to be applied.
521 * This will only apply lines in the pending_lines list, to save on
526 /** Handle /STATS for a given type.
527 * NOTE: Any items in the list for this particular line type which have expired
528 * will be expired and removed before the list is displayed.
529 * @param type The type of stats to show
530 * @param numeric The numeric to give to each result line
531 * @param stats Stats context
533 void InvokeStats(const std::string& type, unsigned int numeric, Stats::Context& stats);
535 /** Clears any XLines which were added by the server configuration. */
536 void ClearConfigLines();