1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
21 /** XLine is the base class for ban lines such as G lines and K lines.
22 * Modules may derive from this, and their xlines will automatically be
23 * handled as expected by any protocol modules (e.g. m_spanningtree will
24 * propogate them using AddLine). The process of translating a type+pattern
25 * to a known line type is done by means of an XLineFactory object (see
28 class CoreExport XLine : public classbase
32 /** Default 'apply' action. Quits the user.
33 * @param u User to apply the line against
34 * @param line The line typed, used for display purposes in the quit message
35 * @param bancache If true, the user will be added to the bancache if they match. Else not.
37 void DefaultApply(User* u, const std::string &line, bool bancache);
42 * @param s_time The set time
43 * @param d The duration of the xline
44 * @param src The sender of the xline
45 * @param re The reason of the xline
46 * @param t The line type, should be set by the derived class constructor
48 XLine(time_t s_time, long d, std::string src, std::string re, const std::string &t)
49 : set_time(s_time), duration(d), source(src), reason(re), type(t)
51 expiry = set_time + duration;
60 /** Change creation time of an xline. Updates expiry
61 * to be after the creation time
63 virtual void SetCreateTime(time_t created)
66 expiry = created + duration;
69 /** Returns true whether or not the given user is covered by this line.
70 * @param u The user to match against. The mechanics of the match
71 * are defined by the derived class.
72 * @return True if there is a match.
74 virtual bool Matches(User *u) = 0;
76 /** Returns true whether or not the given string is covered by this line.
77 * @param str The string to match against. The details of what must be
78 * in this string and the mechanics of the match are defined by the
80 * @return True if there is a match
82 virtual bool Matches(const std::string &str) = 0;
84 /** Apply a line against a user. The mechanics of what occurs when
85 * the line is applied are specific to the derived class.
86 * @param u The user to apply against
88 virtual void Apply(User* u);
90 /** Called when the line is unset either via expiry or
91 * via explicit removal.
93 virtual void Unset() { }
95 /** Called when the expiry message is to be displayed for the
96 * line. Usually a line in the form 'expiring Xline blah, set by...'
97 * see the DisplayExpiry methods of GLine, ELine etc.
99 virtual void DisplayExpiry() = 0;
101 /** Returns the displayable form of the pattern for this xline,
102 * e.g. '*@foo' or '*baz*'. This must always return the full pattern
103 * in a form which can be used to construct an entire derived xline,
104 * even if it is stored differently internally (e.g. GLine stores the
105 * ident and host parts seperately but will still return ident@host
106 * for its Displayable() method)
108 virtual const char* Displayable() = 0;
110 /** Called when the xline has just been added.
112 virtual void OnAdd() { }
114 /** The time the line was added.
118 /** The duration of the ban, or 0 if permenant
122 /** Source of the ban. This can be a servername or an oper nickname
126 /** Reason for the ban
130 /** Expiry time. Does not contain useful data if the duration is 0.
134 /** "Q", "K", etc. Set only by derived classes constructor to the
135 * type of line this is.
137 const std::string type;
139 virtual bool IsBurstable();
144 class CoreExport KLine : public XLine
149 * @param s_time The set time
150 * @param d The duration of the xline
151 * @param src The sender of the xline
152 * @param re The reason of the xline
153 * @param ident Ident to match
154 * @param host Host to match
156 KLine(time_t s_time, long d, std::string src, std::string re, std::string ident, std::string host)
157 : XLine(s_time, d, src, re, "K"), identmask(ident), hostmask(host)
159 matchtext = this->identmask;
160 matchtext.append("@").append(this->hostmask);
169 virtual bool Matches(User *u);
171 virtual bool Matches(const std::string &str);
173 virtual void Apply(User* u);
175 virtual void DisplayExpiry();
177 virtual const char* Displayable();
179 virtual bool IsBurstable();
181 /** Ident mask (ident part only)
183 std::string identmask;
184 /** Host mask (host part only)
186 std::string hostmask;
188 std::string matchtext;
193 class CoreExport GLine : public XLine
197 * @param s_time The set time
198 * @param d The duration of the xline
199 * @param src The sender of the xline
200 * @param re The reason of the xline
201 * @param ident Ident to match
202 * @param host Host to match
204 GLine(time_t s_time, long d, std::string src, std::string re, std::string ident, std::string host)
205 : XLine(s_time, d, src, re, "G"), identmask(ident), hostmask(host)
207 matchtext = this->identmask;
208 matchtext.append("@").append(this->hostmask);
217 virtual bool Matches(User *u);
219 virtual bool Matches(const std::string &str);
221 virtual void Apply(User* u);
223 virtual void DisplayExpiry();
225 virtual const char* Displayable();
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 virtual bool Matches(User *u);
263 virtual bool Matches(const std::string &str);
265 virtual void Unset();
267 virtual void DisplayExpiry();
269 virtual void OnAdd();
271 virtual const char* Displayable();
273 /** Ident mask (ident part only)
275 std::string identmask;
276 /** Host mask (host part only)
278 std::string hostmask;
280 std::string matchtext;
285 class CoreExport ZLine : public XLine
289 * @param s_time The set time
290 * @param d The duration of the xline
291 * @param src The sender of the xline
292 * @param re The reason of the xline
293 * @param ip IP to match
295 ZLine(time_t s_time, long d, std::string src, std::string re, std::string ip)
296 : XLine(s_time, d, src, re, "Z"), ipaddr(ip)
306 virtual bool Matches(User *u);
308 virtual bool Matches(const std::string &str);
310 virtual void Apply(User* u);
312 virtual void DisplayExpiry();
314 virtual const char* Displayable();
316 /** IP mask (no ident part)
323 class CoreExport QLine : public XLine
327 * @param s_time The set time
328 * @param d The duration of the xline
329 * @param src The sender of the xline
330 * @param re The reason of the xline
331 * @param nickname Nickname to match
333 QLine(time_t s_time, long d, std::string src, std::string re, std::string nickname)
334 : XLine(s_time, d, src, re, "Q"), nick(nickname)
343 virtual bool Matches(User *u);
345 virtual bool Matches(const std::string &str);
347 virtual void Apply(User* u);
349 virtual void DisplayExpiry();
351 virtual const char* Displayable();
358 /** Contains an ident and host split into two strings
360 typedef std::pair<std::string, std::string> IdentHostPair;
362 /** XLineFactory is used to generate an XLine pointer, given just the
363 * pattern, timing information and type of line to create. This is used
364 * for example in the spanningtree module which will call an XLineFactory
365 * to create a new XLine when it is inbound on a server link, so that it
366 * does not have to know the specifics of the internals of an XLine class
367 * and/or how to call its constructor.
369 class CoreExport XLineFactory : public classbase
377 /** Create an XLine factory
378 * @param Instance creator
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 /* Required forward declarations
415 /** A map of xline factories
417 typedef std::map<std::string, XLineFactory*> XLineFactMap;
419 /** A map of XLines indexed by string
421 typedef std::map<irc::string, XLine *> XLineLookup;
423 /** A map of XLineLookup maps indexed by string
425 typedef std::map<std::string, XLineLookup > XLineContainer;
427 /** An iterator in an XLineContainer
429 typedef XLineContainer::iterator ContainerIter;
431 /** An interator in an XLineLookup
433 typedef XLineLookup::iterator LookupIter;
435 /** XLineManager is a class used to manage glines, klines, elines, zlines and qlines,
436 * or any other line created by a module. It also manages XLineFactory classes which
437 * can generate a specialized XLine for use by another module.
439 class CoreExport XLineManager : public classbase
442 /** Used to hold XLines which have not yet been applied.
444 std::vector<XLine *> pending_lines;
446 /** Current xline factories
448 XLineFactMap line_factory;
450 /** Core xline factories for G/E/K/Q/Z lines
451 * (These generate GLine, ELine, KLine, QLine and ZLine
460 /** Container of all lines, this is a map of maps which
461 * allows for fast lookup for add/remove of a line, and
462 * the shortest possible timed O(n) for checking a user
465 XLineContainer lookup_lines;
470 * @param Instance A pointer to the creator object
478 /** Split an ident and host into two seperate strings.
479 * This allows for faster matching.
481 IdentHostPair IdentSplit(const std::string &ident_and_host);
483 /** Checks what users match e:lines and sets their ban exempt flag accordingly.
487 /** Get all lines of a certain type to an XLineLookup (std::map<std::string, XLine*>).
488 * NOTE: When this function runs any expired items are removed from the list before it
489 * is returned to the caller.
490 * @param The type to look up
491 * @return A list of all XLines of the given type.
493 XLineLookup* GetAll(const std::string &type);
495 /** Remove all lines of a certain type.
497 void DelAll(const std::string &type);
499 /** Return all known types of line currently stored by the XLineManager.
500 * @return A vector containing all known line types currently stored in the main list.
502 std::vector<std::string> GetAllTypes();
505 * @param line The line to be added
506 * @param user The user adding the line or NULL for the local server
507 * @return True if the line was added successfully
509 bool AddLine(XLine* line, User* user);
512 * @param hostmask The xline-specific string identifying the line, e.g. "*@foo"
513 * @param type The type of xline
514 * @param user The user removing the line or NULL if its the local server
515 * @param simulate If this is true, don't actually remove the line, just return
516 * @return True if the line was deleted successfully
518 bool DelLine(const char* hostmask, const std::string &type, User* user, bool simulate = false);
520 /** Registers an xline factory.
521 * An xline factory is a class which when given a particular xline type,
522 * will generate a new XLine specialized to that type. For example if you
523 * pass the XLineFactory that handles glines some data it will return a
524 * pointer to a GLine, polymorphically represented as XLine. This is used where
525 * you do not know the full details of the item you wish to create, e.g. in a
526 * server protocol module like m_spanningtree, when you receive xlines from other
528 * @param xlf XLineFactory pointer to register
530 bool RegisterFactory(XLineFactory* xlf);
532 /** Unregisters an xline factory.
533 * You must do this when your module unloads.
534 * @param xlf XLineFactory pointer to unregister
536 bool UnregisterFactory(XLineFactory* xlf);
538 /** Get the XLineFactory for a specific type.
539 * Returns NULL if there is no known handler for this xline type.
540 * @param type The type of XLine you require the XLineFactory for
542 XLineFactory* GetFactory(const std::string &type);
544 /** Check if a user matches an XLine
545 * @param type The type of line to look up
546 * @param user The user to match against (what is checked is specific to the xline type)
547 * @return The reason for the line if there is a match, or NULL if there is no match
549 XLine* MatchesLine(const std::string &type, User* user);
551 /** Check if a pattern matches an XLine
552 * @param type The type of line to look up
553 * @param pattern A pattern string specific to the xline type
554 * @return The matching XLine if there is a match, or NULL if there is no match
556 XLine* MatchesLine(const std::string &type, const std::string &pattern);
558 /** Expire a line given two iterators which identify it in the main map.
559 * @param container Iterator to the first level of entries the map
560 * @param item Iterator to the second level of entries in the map
562 void ExpireLine(ContainerIter container, LookupIter item);
564 /** Apply any new lines that are pending to be applied.
565 * This will only apply lines in the pending_lines list, to save on
570 /** Handle /STATS for a given type.
571 * NOTE: Any items in the list for this particular line type which have expired
572 * will be expired and removed before the list is displayed.
573 * @param numeric The numeric to give to each result line
574 * @param user The username making the query
575 * @param results The string_list to receive the results
577 void InvokeStats(const std::string &type, int numeric, User* user, string_list &results);
580 /** An XLineFactory specialized to generate GLine* pointers
582 class CoreExport GLineFactory : public XLineFactory
585 GLineFactory() : XLineFactory("G") { }
589 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
591 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
592 return new GLine(set_time, duration, source, reason, ih.first, ih.second);
596 /** An XLineFactory specialized to generate ELine* pointers
598 class CoreExport ELineFactory : public XLineFactory
601 ELineFactory() : XLineFactory("E") { }
603 /** Generate an ELine
605 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
607 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
608 return new ELine(set_time, duration, source, reason, ih.first, ih.second);
612 /** An XLineFactory specialized to generate KLine* pointers
614 class CoreExport KLineFactory : public XLineFactory
617 KLineFactory() : XLineFactory("K") { }
621 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
623 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
624 return new KLine(set_time, duration, source, reason, ih.first, ih.second);
628 /** An XLineFactory specialized to generate QLine* pointers
630 class CoreExport QLineFactory : public XLineFactory
633 QLineFactory() : XLineFactory("Q") { }
637 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
639 return new QLine(set_time, duration, source, reason, xline_specific_mask);
643 /** An XLineFactory specialized to generate ZLine* pointers
645 class CoreExport ZLineFactory : public XLineFactory
648 ZLineFactory() : XLineFactory("Z") { }
652 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
654 return new ZLine(set_time, duration, source, reason, xline_specific_mask);