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/>.
26 /** XLine is the base class for ban lines such as G lines and K lines.
27 * Modules may derive from this, and their xlines will automatically be
28 * handled as expected by any protocol modules (e.g. m_spanningtree will
29 * propogate them using AddLine). The process of translating a type+pattern
30 * to a known line type is done by means of an XLineFactory object (see
33 class CoreExport XLine : public classbase
37 /** Default 'apply' action. Quits the user.
38 * @param u User to apply the line against
39 * @param line The line typed, used for display purposes in the quit message
40 * @param bancache If true, the user will be added to the bancache if they match. Else not.
42 void DefaultApply(User* u, const std::string &line, bool bancache);
47 * @param s_time The set time
48 * @param d The duration of the xline
49 * @param src The sender of the xline
50 * @param re The reason of the xline
51 * @param t The line type, should be set by the derived class constructor
53 XLine(time_t s_time, long d, std::string src, std::string re, const std::string &t)
54 : set_time(s_time), duration(d), source(src), reason(re), type(t)
56 expiry = set_time + duration;
65 /** Change creation time of an xline. Updates expiry
66 * to be after the creation time
68 virtual void SetCreateTime(time_t created)
71 expiry = created + duration;
74 /** Returns true whether or not the given user is covered by this line.
75 * @param u The user to match against. The mechanics of the match
76 * are defined by the derived class.
77 * @return True if there is a match.
79 virtual bool Matches(User *u) = 0;
81 /** Returns true whether or not the given string is covered by this line.
82 * @param str The string to match against. The details of what must be
83 * in this string and the mechanics of the match are defined by the
85 * @return True if there is a match
87 virtual bool Matches(const std::string &str) = 0;
89 /** Apply a line against a user. The mechanics of what occurs when
90 * the line is applied are specific to the derived class.
91 * @param u The user to apply against
93 virtual void Apply(User* u);
95 /** Called when the line is unset either via expiry or
96 * via explicit removal.
98 virtual void Unset() { }
100 /** Called when the expiry message is to be displayed for the
101 * line. Usually a line in the form 'expiring Xline blah, set by...'
102 * see the DisplayExpiry methods of GLine, ELine etc.
104 virtual void DisplayExpiry() = 0;
106 /** Returns the displayable form of the pattern for this xline,
107 * e.g. '*\@foo' or '*baz*'. This must always return the full pattern
108 * in a form which can be used to construct an entire derived xline,
109 * even if it is stored differently internally (e.g. GLine stores the
110 * ident and host parts seperately but will still return ident\@host
111 * for its Displayable() method)
113 virtual const char* Displayable() = 0;
115 /** Called when the xline has just been added.
117 virtual void OnAdd() { }
119 /** The time the line was added.
123 /** The duration of the ban, or 0 if permenant
127 /** Source of the ban. This can be a servername or an oper nickname
131 /** Reason for the ban
135 /** Expiry time. Does not contain useful data if the duration is 0.
139 /** "Q", "K", etc. Set only by derived classes constructor to the
140 * type of line this is.
142 const std::string type;
144 virtual bool IsBurstable();
149 class CoreExport KLine : public XLine
154 * @param s_time The set time
155 * @param d The duration of the xline
156 * @param src The sender of the xline
157 * @param re The reason of the xline
158 * @param ident Ident to match
159 * @param host Host to match
161 KLine(time_t s_time, long d, std::string src, std::string re, std::string ident, std::string host)
162 : XLine(s_time, d, src, re, "K"), identmask(ident), hostmask(host)
164 matchtext = this->identmask;
165 matchtext.append("@").append(this->hostmask);
174 virtual bool Matches(User *u);
176 virtual bool Matches(const std::string &str);
178 virtual void Apply(User* u);
180 virtual void DisplayExpiry();
182 virtual const char* Displayable();
184 virtual bool IsBurstable();
186 /** Ident mask (ident part only)
188 std::string identmask;
189 /** Host mask (host part only)
191 std::string hostmask;
193 std::string matchtext;
198 class CoreExport GLine : public XLine
202 * @param s_time The set time
203 * @param d The duration of the xline
204 * @param src The sender of the xline
205 * @param re The reason of the xline
206 * @param ident Ident to match
207 * @param host Host to match
209 GLine(time_t s_time, long d, std::string src, std::string re, std::string ident, std::string host)
210 : XLine(s_time, d, src, re, "G"), identmask(ident), hostmask(host)
212 matchtext = this->identmask;
213 matchtext.append("@").append(this->hostmask);
222 virtual bool Matches(User *u);
224 virtual bool Matches(const std::string &str);
226 virtual void Apply(User* u);
228 virtual void DisplayExpiry();
230 virtual const char* Displayable();
232 /** Ident mask (ident part only)
234 std::string identmask;
235 /** Host mask (host part only)
237 std::string hostmask;
239 std::string matchtext;
244 class CoreExport ELine : public XLine
247 /** Create an E-Line.
248 * @param s_time The set time
249 * @param d The duration of the xline
250 * @param src The sender of the xline
251 * @param re The reason of the xline
252 * @param ident Ident to match
253 * @param host Host to match
255 ELine(time_t s_time, long d, std::string src, std::string re, std::string ident, std::string host)
256 : XLine(s_time, d, src, re, "E"), identmask(ident), hostmask(host)
258 matchtext = this->identmask;
259 matchtext.append("@").append(this->hostmask);
266 virtual bool Matches(User *u);
268 virtual bool Matches(const std::string &str);
270 virtual void Unset();
272 virtual void DisplayExpiry();
274 virtual void OnAdd();
276 virtual const char* Displayable();
278 /** Ident mask (ident part only)
280 std::string identmask;
281 /** Host mask (host part only)
283 std::string hostmask;
285 std::string matchtext;
290 class CoreExport ZLine : public XLine
294 * @param s_time The set time
295 * @param d The duration of the xline
296 * @param src The sender of the xline
297 * @param re The reason of the xline
298 * @param ip IP to match
300 ZLine(time_t s_time, long d, std::string src, std::string re, std::string ip)
301 : XLine(s_time, d, src, re, "Z"), ipaddr(ip)
311 virtual bool Matches(User *u);
313 virtual bool Matches(const std::string &str);
315 virtual void Apply(User* u);
317 virtual void DisplayExpiry();
319 virtual const char* Displayable();
321 /** IP mask (no ident part)
328 class CoreExport QLine : public XLine
332 * @param s_time The set time
333 * @param d The duration of the xline
334 * @param src The sender of the xline
335 * @param re The reason of the xline
336 * @param nickname Nickname to match
338 QLine(time_t s_time, long d, std::string src, std::string re, std::string nickname)
339 : XLine(s_time, d, src, re, "Q"), nick(nickname)
348 virtual bool Matches(User *u);
350 virtual bool Matches(const std::string &str);
352 virtual void Apply(User* u);
354 virtual void DisplayExpiry();
356 virtual const char* Displayable();
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, long duration, std::string source, std::string reason, 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 glines, klines, elines, zlines and qlines,
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 seperate 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 user The user removing the line or NULL if its the local server
474 * @param simulate If this is true, don't actually remove the line, just return
475 * @return True if the line was deleted successfully
477 bool DelLine(const char* hostmask, const std::string &type, User* user, bool simulate = false);
479 /** Registers an xline factory.
480 * An xline factory is a class which when given a particular xline type,
481 * will generate a new XLine specialized to that type. For example if you
482 * pass the XLineFactory that handles glines some data it will return a
483 * pointer to a GLine, polymorphically represented as XLine. This is used where
484 * you do not know the full details of the item you wish to create, e.g. in a
485 * server protocol module like m_spanningtree, when you receive xlines from other
487 * @param xlf XLineFactory pointer to register
489 bool RegisterFactory(XLineFactory* xlf);
491 /** Unregisters an xline factory.
492 * You must do this when your module unloads.
493 * @param xlf XLineFactory pointer to unregister
495 bool UnregisterFactory(XLineFactory* xlf);
497 /** Get the XLineFactory for a specific type.
498 * Returns NULL if there is no known handler for this xline type.
499 * @param type The type of XLine you require the XLineFactory for
501 XLineFactory* GetFactory(const std::string &type);
503 /** Check if a user matches an XLine
504 * @param type The type of line to look up
505 * @param user The user to match against (what is checked is specific to the xline type)
506 * @return The reason for the line if there is a match, or NULL if there is no match
508 XLine* MatchesLine(const std::string &type, User* user);
510 /** Check if a pattern matches an XLine
511 * @param type The type of line to look up
512 * @param pattern A pattern string specific to the xline type
513 * @return The matching XLine if there is a match, or NULL if there is no match
515 XLine* MatchesLine(const std::string &type, const std::string &pattern);
517 /** Expire a line given two iterators which identify it in the main map.
518 * @param container Iterator to the first level of entries the map
519 * @param item Iterator to the second level of entries in the map
521 void ExpireLine(ContainerIter container, LookupIter item);
523 /** Apply any new lines that are pending to be applied.
524 * This will only apply lines in the pending_lines list, to save on
529 /** Handle /STATS for a given type.
530 * NOTE: Any items in the list for this particular line type which have expired
531 * will be expired and removed before the list is displayed.
532 * @param type The type of stats to show
533 * @param numeric The numeric to give to each result line
534 * @param user The username making the query
535 * @param results The string_list to receive the results
537 void InvokeStats(const std::string &type, int numeric, User* user, string_list &results);