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)
53 : set_time(s_time), duration(d), source(src), reason(re), type(t)
55 expiry = set_time + duration;
64 /** Change creation time of an xline. Updates expiry
65 * to be after the creation time
67 virtual void SetCreateTime(time_t created)
70 expiry = created + duration;
73 /** Returns true whether or not the given user is covered by this line.
74 * @param u The user to match against. The mechanics of the match
75 * are defined by the derived class.
76 * @return True if there is a match.
78 virtual bool Matches(User *u) = 0;
80 /** Returns true whether or not the given string is covered by this line.
81 * @param str The string to match against. The details of what must be
82 * in this string and the mechanics of the match are defined by the
84 * @return True if there is a match
86 virtual bool Matches(const std::string &str) = 0;
88 /** Apply a line against a user. The mechanics of what occurs when
89 * the line is applied are specific to the derived class.
90 * @param u The user to apply against
92 virtual void Apply(User* u);
94 /** Called when the line is unset either via expiry or
95 * via explicit removal.
97 virtual void Unset() { }
99 /** Called when the expiry message is to be displayed for the
100 * line. Usually a line in the form 'expiring Xline blah, set by...'
101 * see the DisplayExpiry methods of GLine, ELine etc.
103 virtual void DisplayExpiry();
105 /** Returns the displayable form of the pattern for this xline,
106 * e.g. '*\@foo' or '*baz*'. This must always return the full pattern
107 * in a form which can be used to construct an entire derived xline,
108 * even if it is stored differently internally (e.g. GLine stores the
109 * ident and host parts seperately but will still return ident\@host
110 * for its Displayable() method).
112 virtual const std::string& Displayable() = 0;
114 /** Called when the xline has just been added.
116 virtual void OnAdd() { }
118 /** The time the line was added.
122 /** The duration of the ban, or 0 if permenant
126 /** Source of the ban. This can be a servername or an oper nickname
130 /** Reason for the ban
134 /** Expiry time. Does not contain useful data if the duration is 0.
138 /** "Q", "K", etc. Set only by derived classes constructor to the
139 * type of line this is.
141 const std::string type;
143 virtual bool IsBurstable();
148 class CoreExport KLine : public XLine
153 * @param s_time The set time
154 * @param d The duration of the xline
155 * @param src The sender of the xline
156 * @param re The reason of the xline
157 * @param ident Ident to match
158 * @param host Host to match
160 KLine(time_t s_time, long d, std::string src, std::string re, std::string ident, std::string host)
161 : XLine(s_time, d, src, re, "K"), identmask(ident), hostmask(host)
163 matchtext = this->identmask;
164 matchtext.append("@").append(this->hostmask);
173 bool Matches(User* u) CXX11_OVERRIDE;
175 bool Matches(const std::string& str) CXX11_OVERRIDE;
177 void Apply(User* u) CXX11_OVERRIDE;
179 const std::string& Displayable() CXX11_OVERRIDE;
181 bool IsBurstable() CXX11_OVERRIDE;
183 /** Ident mask (ident part only)
185 std::string identmask;
186 /** Host mask (host part only)
188 std::string hostmask;
190 std::string matchtext;
195 class CoreExport GLine : public XLine
199 * @param s_time The set time
200 * @param d The duration of the xline
201 * @param src The sender of the xline
202 * @param re The reason of the xline
203 * @param ident Ident to match
204 * @param host Host to match
206 GLine(time_t s_time, long d, std::string src, std::string re, std::string ident, std::string host)
207 : XLine(s_time, d, src, re, "G"), identmask(ident), hostmask(host)
209 matchtext = this->identmask;
210 matchtext.append("@").append(this->hostmask);
219 bool Matches(User* u) CXX11_OVERRIDE;
221 bool Matches(const std::string& str) CXX11_OVERRIDE;
223 void Apply(User* u) CXX11_OVERRIDE;
225 const std::string& Displayable() CXX11_OVERRIDE;
227 /** Ident mask (ident part only)
229 std::string identmask;
230 /** Host mask (host part only)
232 std::string hostmask;
234 std::string matchtext;
239 class CoreExport ELine : public XLine
242 /** Create an E-Line.
243 * @param s_time The set time
244 * @param d The duration of the xline
245 * @param src The sender of the xline
246 * @param re The reason of the xline
247 * @param ident Ident to match
248 * @param host Host to match
250 ELine(time_t s_time, long d, std::string src, std::string re, std::string ident, std::string host)
251 : XLine(s_time, d, src, re, "E"), identmask(ident), hostmask(host)
253 matchtext = this->identmask;
254 matchtext.append("@").append(this->hostmask);
261 bool Matches(User* u) CXX11_OVERRIDE;
263 bool Matches(const std::string& str) CXX11_OVERRIDE;
265 void Unset() CXX11_OVERRIDE;
267 void OnAdd() CXX11_OVERRIDE;
269 const std::string& Displayable() CXX11_OVERRIDE;
271 /** Ident mask (ident part only)
273 std::string identmask;
274 /** Host mask (host part only)
276 std::string hostmask;
278 std::string matchtext;
283 class CoreExport ZLine : public XLine
287 * @param s_time The set time
288 * @param d The duration of the xline
289 * @param src The sender of the xline
290 * @param re The reason of the xline
291 * @param ip IP to match
293 ZLine(time_t s_time, long d, std::string src, std::string re, std::string ip)
294 : XLine(s_time, d, src, re, "Z"), ipaddr(ip)
304 bool Matches(User* u) CXX11_OVERRIDE;
306 bool Matches(const std::string& str) CXX11_OVERRIDE;
308 void Apply(User* u) CXX11_OVERRIDE;
310 const std::string& Displayable() CXX11_OVERRIDE;
312 /** IP mask (no ident part)
319 class CoreExport QLine : public XLine
323 * @param s_time The set time
324 * @param d The duration of the xline
325 * @param src The sender of the xline
326 * @param re The reason of the xline
327 * @param nickname Nickname to match
329 QLine(time_t s_time, long d, std::string src, std::string re, std::string nickname)
330 : XLine(s_time, d, src, re, "Q"), nick(nickname)
339 bool Matches(User* u) CXX11_OVERRIDE;
341 bool Matches(const std::string& str) CXX11_OVERRIDE;
343 void Apply(User* u) CXX11_OVERRIDE;
345 const std::string& Displayable() CXX11_OVERRIDE;
352 /** XLineFactory is used to generate an XLine pointer, given just the
353 * pattern, timing information and type of line to create. This is used
354 * for example in the spanningtree module which will call an XLineFactory
355 * to create a new XLine when it is inbound on a server link, so that it
356 * does not have to know the specifics of the internals of an XLine class
357 * and/or how to call its constructor.
359 class CoreExport XLineFactory
367 /** Create an XLine factory
368 * @param t Type of XLine this factory generates
370 XLineFactory(const std::string &t) : type(t) { }
372 /** Return the type of XLine this factory generates
373 * @return The type of XLine this factory generates
375 virtual const std::string& GetType() { return type; }
377 /** Generate a specialized XLine*.
378 * @param set_time Time this line was created
379 * @param duration Duration of the line
380 * @param source The sender of the line, nickname or server
381 * @param reason The reason for the line
382 * @param xline_specific_mask The mask string for the line, specific to the XLine type being created.
383 * @return A specialized XLine class of the given type for this factory.
385 virtual XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask) = 0;
387 virtual bool AutoApplyToUserList(XLine* x) { return true; }
391 virtual ~XLineFactory() { }
394 /** XLineManager is a class used to manage glines, klines, elines, zlines and qlines,
395 * or any other line created by a module. It also manages XLineFactory classes which
396 * can generate a specialized XLine for use by another module.
398 class CoreExport XLineManager
401 /** Used to hold XLines which have not yet been applied.
403 std::vector<XLine *> pending_lines;
405 /** Current xline factories
407 XLineFactMap line_factory;
409 /** Container of all lines, this is a map of maps which
410 * allows for fast lookup for add/remove of a line, and
411 * the shortest possible timed O(n) for checking a user
414 XLineContainer lookup_lines;
426 /** Split an ident and host into two seperate strings.
427 * This allows for faster matching.
429 IdentHostPair IdentSplit(const std::string &ident_and_host);
431 /** Checks what users match e:lines and sets their ban exempt flag accordingly.
435 /** Get all lines of a certain type to an XLineLookup (std::map<std::string, XLine*>).
436 * NOTE: When this function runs any expired items are removed from the list before it
437 * is returned to the caller.
438 * @param type The type to look up
439 * @return A list of all XLines of the given type.
441 XLineLookup* GetAll(const std::string &type);
443 /** Remove all lines of a certain type.
445 void DelAll(const std::string &type);
447 /** Return all known types of line currently stored by the XLineManager.
448 * @return A vector containing all known line types currently stored in the main list.
450 std::vector<std::string> GetAllTypes();
453 * @param line The line to be added
454 * @param user The user adding the line or NULL for the local server
455 * @return True if the line was added successfully
457 bool AddLine(XLine* line, User* user);
460 * @param hostmask The xline-specific string identifying the line, e.g. "*@foo"
461 * @param type The type of xline
462 * @param user The user removing the line or NULL if its the local server
463 * @param simulate If this is true, don't actually remove the line, just return
464 * @return True if the line was deleted successfully
466 bool DelLine(const char* hostmask, const std::string &type, User* user, bool simulate = false);
468 /** Registers an xline factory.
469 * An xline factory is a class which when given a particular xline type,
470 * will generate a new XLine specialized to that type. For example if you
471 * pass the XLineFactory that handles glines some data it will return a
472 * pointer to a GLine, polymorphically represented as XLine. This is used where
473 * you do not know the full details of the item you wish to create, e.g. in a
474 * server protocol module like m_spanningtree, when you receive xlines from other
476 * @param xlf XLineFactory pointer to register
478 bool RegisterFactory(XLineFactory* xlf);
480 /** Unregisters an xline factory.
481 * You must do this when your module unloads.
482 * @param xlf XLineFactory pointer to unregister
484 bool UnregisterFactory(XLineFactory* xlf);
486 /** Get the XLineFactory for a specific type.
487 * Returns NULL if there is no known handler for this xline type.
488 * @param type The type of XLine you require the XLineFactory for
490 XLineFactory* GetFactory(const std::string &type);
492 /** Check if a user matches an XLine
493 * @param type The type of line to look up
494 * @param user The user to match against (what is checked is specific to the xline type)
495 * @return The reason for the line if there is a match, or NULL if there is no match
497 XLine* MatchesLine(const std::string &type, User* user);
499 /** Check if a pattern matches an XLine
500 * @param type The type of line to look up
501 * @param pattern A pattern string specific to the xline type
502 * @return The matching XLine if there is a match, or NULL if there is no match
504 XLine* MatchesLine(const std::string &type, const std::string &pattern);
506 /** Expire a line given two iterators which identify it in the main map.
507 * @param container Iterator to the first level of entries the map
508 * @param item Iterator to the second level of entries in the map
510 void ExpireLine(ContainerIter container, LookupIter item);
512 /** Apply any new lines that are pending to be applied.
513 * This will only apply lines in the pending_lines list, to save on
518 /** Handle /STATS for a given type.
519 * NOTE: Any items in the list for this particular line type which have expired
520 * will be expired and removed before the list is displayed.
521 * @param type The type of stats to show
522 * @param numeric The numeric to give to each result line
523 * @param stats Stats context
525 void InvokeStats(const std::string& type, unsigned int numeric, Stats::Context& stats);