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();
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 const char* Displayable();
182 virtual bool IsBurstable();
184 /** Ident mask (ident part only)
186 std::string identmask;
187 /** Host mask (host part only)
189 std::string hostmask;
191 std::string matchtext;
196 class CoreExport GLine : public XLine
200 * @param s_time The set time
201 * @param d The duration of the xline
202 * @param src The sender of the xline
203 * @param re The reason of the xline
204 * @param ident Ident to match
205 * @param host Host to match
207 GLine(time_t s_time, long d, std::string src, std::string re, std::string ident, std::string host)
208 : XLine(s_time, d, src, re, "G"), identmask(ident), hostmask(host)
210 matchtext = this->identmask;
211 matchtext.append("@").append(this->hostmask);
220 virtual bool Matches(User *u);
222 virtual bool Matches(const std::string &str);
224 virtual void Apply(User* u);
226 virtual const char* Displayable();
228 /** Ident mask (ident part only)
230 std::string identmask;
231 /** Host mask (host part only)
233 std::string hostmask;
235 std::string matchtext;
240 class CoreExport ELine : public XLine
243 /** Create an E-Line.
244 * @param s_time The set time
245 * @param d The duration of the xline
246 * @param src The sender of the xline
247 * @param re The reason of the xline
248 * @param ident Ident to match
249 * @param host Host to match
251 ELine(time_t s_time, long d, std::string src, std::string re, std::string ident, std::string host)
252 : XLine(s_time, d, src, re, "E"), identmask(ident), hostmask(host)
254 matchtext = this->identmask;
255 matchtext.append("@").append(this->hostmask);
262 virtual bool Matches(User *u);
264 virtual bool Matches(const std::string &str);
266 virtual void Unset();
268 virtual void OnAdd();
270 virtual const char* Displayable();
272 /** Ident mask (ident part only)
274 std::string identmask;
275 /** Host mask (host part only)
277 std::string hostmask;
279 std::string matchtext;
284 class CoreExport ZLine : public XLine
288 * @param s_time The set time
289 * @param d The duration of the xline
290 * @param src The sender of the xline
291 * @param re The reason of the xline
292 * @param ip IP to match
294 ZLine(time_t s_time, long d, std::string src, std::string re, std::string ip)
295 : XLine(s_time, d, src, re, "Z"), ipaddr(ip)
305 virtual bool Matches(User *u);
307 virtual bool Matches(const std::string &str);
309 virtual void Apply(User* u);
311 virtual const char* Displayable();
313 /** IP mask (no ident part)
320 class CoreExport QLine : public XLine
324 * @param s_time The set time
325 * @param d The duration of the xline
326 * @param src The sender of the xline
327 * @param re The reason of the xline
328 * @param nickname Nickname to match
330 QLine(time_t s_time, long d, std::string src, std::string re, std::string nickname)
331 : XLine(s_time, d, src, re, "Q"), nick(nickname)
340 virtual bool Matches(User *u);
342 virtual bool Matches(const std::string &str);
344 virtual void Apply(User* u);
346 virtual const char* Displayable();
353 /** XLineFactory is used to generate an XLine pointer, given just the
354 * pattern, timing information and type of line to create. This is used
355 * for example in the spanningtree module which will call an XLineFactory
356 * to create a new XLine when it is inbound on a server link, so that it
357 * does not have to know the specifics of the internals of an XLine class
358 * and/or how to call its constructor.
360 class CoreExport XLineFactory
368 /** Create an XLine factory
369 * @param t Type of XLine this factory generates
371 XLineFactory(const std::string &t) : type(t) { }
373 /** Return the type of XLine this factory generates
374 * @return The type of XLine this factory generates
376 virtual const std::string& GetType() { return type; }
378 /** Generate a specialized XLine*.
379 * @param set_time Time this line was created
380 * @param duration Duration of the line
381 * @param source The sender of the line, nickname or server
382 * @param reason The reason for the line
383 * @param xline_specific_mask The mask string for the line, specific to the XLine type being created.
384 * @return A specialized XLine class of the given type for this factory.
386 virtual XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask) = 0;
388 virtual bool AutoApplyToUserList(XLine* x) { return true; }
392 virtual ~XLineFactory() { }
395 /** XLineManager is a class used to manage glines, klines, elines, zlines and qlines,
396 * or any other line created by a module. It also manages XLineFactory classes which
397 * can generate a specialized XLine for use by another module.
399 class CoreExport XLineManager
402 /** Used to hold XLines which have not yet been applied.
404 std::vector<XLine *> pending_lines;
406 /** Current xline factories
408 XLineFactMap line_factory;
410 /** Container of all lines, this is a map of maps which
411 * allows for fast lookup for add/remove of a line, and
412 * the shortest possible timed O(n) for checking a user
415 XLineContainer lookup_lines;
427 /** Split an ident and host into two seperate strings.
428 * This allows for faster matching.
430 IdentHostPair IdentSplit(const std::string &ident_and_host);
432 /** Checks what users match e:lines and sets their ban exempt flag accordingly.
436 /** Get all lines of a certain type to an XLineLookup (std::map<std::string, XLine*>).
437 * NOTE: When this function runs any expired items are removed from the list before it
438 * is returned to the caller.
439 * @param type The type to look up
440 * @return A list of all XLines of the given type.
442 XLineLookup* GetAll(const std::string &type);
444 /** Remove all lines of a certain type.
446 void DelAll(const std::string &type);
448 /** Return all known types of line currently stored by the XLineManager.
449 * @return A vector containing all known line types currently stored in the main list.
451 std::vector<std::string> GetAllTypes();
454 * @param line The line to be added
455 * @param user The user adding the line or NULL for the local server
456 * @return True if the line was added successfully
458 bool AddLine(XLine* line, User* user);
461 * @param hostmask The xline-specific string identifying the line, e.g. "*@foo"
462 * @param type The type of xline
463 * @param user The user removing the line or NULL if its the local server
464 * @param simulate If this is true, don't actually remove the line, just return
465 * @return True if the line was deleted successfully
467 bool DelLine(const char* hostmask, const std::string &type, User* user, bool simulate = false);
469 /** Registers an xline factory.
470 * An xline factory is a class which when given a particular xline type,
471 * will generate a new XLine specialized to that type. For example if you
472 * pass the XLineFactory that handles glines some data it will return a
473 * pointer to a GLine, polymorphically represented as XLine. This is used where
474 * you do not know the full details of the item you wish to create, e.g. in a
475 * server protocol module like m_spanningtree, when you receive xlines from other
477 * @param xlf XLineFactory pointer to register
479 bool RegisterFactory(XLineFactory* xlf);
481 /** Unregisters an xline factory.
482 * You must do this when your module unloads.
483 * @param xlf XLineFactory pointer to unregister
485 bool UnregisterFactory(XLineFactory* xlf);
487 /** Get the XLineFactory for a specific type.
488 * Returns NULL if there is no known handler for this xline type.
489 * @param type The type of XLine you require the XLineFactory for
491 XLineFactory* GetFactory(const std::string &type);
493 /** Check if a user matches an XLine
494 * @param type The type of line to look up
495 * @param user The user to match against (what is checked is specific to the xline type)
496 * @return The reason for the line if there is a match, or NULL if there is no match
498 XLine* MatchesLine(const std::string &type, User* user);
500 /** Check if a pattern matches an XLine
501 * @param type The type of line to look up
502 * @param pattern A pattern string specific to the xline type
503 * @return The matching XLine if there is a match, or NULL if there is no match
505 XLine* MatchesLine(const std::string &type, const std::string &pattern);
507 /** Expire a line given two iterators which identify it in the main map.
508 * @param container Iterator to the first level of entries the map
509 * @param item Iterator to the second level of entries in the map
511 void ExpireLine(ContainerIter container, LookupIter item);
513 /** Apply any new lines that are pending to be applied.
514 * This will only apply lines in the pending_lines list, to save on
519 /** Handle /STATS for a given type.
520 * NOTE: Any items in the list for this particular line type which have expired
521 * will be expired and removed before the list is displayed.
522 * @param type The type of stats to show
523 * @param numeric The numeric to give to each result line
524 * @param user The username making the query
525 * @param results The string_list to receive the results
527 void InvokeStats(const std::string &type, int numeric, User* user, string_list &results);