1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/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
33 InspIRCd* ServerInstance;
35 /** Default 'apply' action. Quits the user.
36 * @param u User to apply the line against
37 * @param line The line typed, used for display purposes in the quit message
38 * @param bancache If true, the user will be added to the bancache if they match. Else not.
40 void DefaultApply(User* u, const std::string &line, bool bancache);
45 * @param s_time The set time
46 * @param d The duration of the xline
47 * @param src The sender of the xline
48 * @param re The reason of the xline
49 * @param t The line type, should be set by the derived class constructor
51 XLine(InspIRCd* Instance, time_t s_time, long d, const char* src, const char* re, const std::string &t)
52 : ServerInstance(Instance), set_time(s_time), duration(d), type(t)
56 expiry = set_time + duration;
67 /** Change creation time of an xline. Updates expiry
68 * to be after the creation time
70 virtual void SetCreateTime(time_t created)
73 expiry = created + duration;
76 /** Returns true whether or not the given user is covered by this line.
77 * @param u The user to match against. The mechanics of the match
78 * are defined by the derived class.
79 * @return True if there is a match.
81 virtual bool Matches(User *u) = 0;
83 /** Returns true whether or not the given string is covered by this line.
84 * @param str The string to match against. The details of what must be
85 * in this string and the mechanics of the match are defined by the
87 * @return True if there is a match
89 virtual bool Matches(const std::string &str) = 0;
91 /** Apply a line against a user. The mechanics of what occurs when
92 * the line is applied are specific to the derived class.
93 * @param u The user to apply against
95 virtual void Apply(User* u);
97 /** Called when the line is unset either via expiry or
98 * via explicit removal.
100 virtual void Unset() { }
102 /** Called when the expiry message is to be displayed for the
103 * line. Usually a line in the form 'expiring Xline blah, set by...'
104 * see the DisplayExpiry methods of GLine, ELine etc.
106 virtual void DisplayExpiry() = 0;
108 /** Returns the displayable form of the pattern for this xline,
109 * e.g. '*@foo' or '*baz*'. This must always return the full pattern
110 * in a form which can be used to construct an entire derived xline,
111 * even if it is stored differently internally (e.g. GLine stores the
112 * ident and host parts seperately but will still return ident@host
113 * for its Displayable() method)
115 virtual const char* Displayable() = 0;
117 /** Called when the xline has just been added.
119 virtual void OnAdd() { }
121 /** The time the line was added.
125 /** The duration of the ban, or 0 if permenant
129 /** Source of the ban. This can be a servername or an oper nickname
133 /** Reason for the ban
137 /** Expiry time. Does not contain useful data if the duration is 0.
141 /** "Q", "K", etc. Set only by derived classes constructor to the
142 * type of line this is.
144 const std::string type;
146 virtual bool IsBurstable();
151 class CoreExport KLine : public XLine
156 * @param s_time The set time
157 * @param d The duration of the xline
158 * @param src The sender of the xline
159 * @param re The reason of the xline
160 * @param ident Ident to match
161 * @param host Host to match
163 KLine(InspIRCd* Instance, time_t s_time, long d, const char* src, const char* re, const char* ident, const char* host) : XLine(Instance, s_time, d, src, re, "K")
165 identmask = strdup(ident);
166 hostmask = strdup(host);
167 matchtext = this->identmask;
168 matchtext.append("@").append(this->hostmask);
179 virtual bool Matches(User *u);
181 virtual bool Matches(const std::string &str);
183 virtual void Apply(User* u);
185 virtual void DisplayExpiry();
187 virtual const char* Displayable();
189 virtual bool IsBurstable();
191 /** Ident mask (ident part only)
194 /** Host mask (host part only)
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(InspIRCd* Instance, time_t s_time, long d, const char* src, const char* re, const char* ident, const char* host) : XLine(Instance, s_time, d, src, re, "G")
216 identmask = strdup(ident);
217 hostmask = strdup(host);
218 matchtext = this->identmask;
219 matchtext.append("@").append(this->hostmask);
230 virtual bool Matches(User *u);
232 virtual bool Matches(const std::string &str);
234 virtual void Apply(User* u);
236 virtual void DisplayExpiry();
238 virtual const char* Displayable();
240 /** Ident mask (ident part only)
243 /** Host mask (host part only)
247 std::string matchtext;
252 class CoreExport ELine : public XLine
255 /** Create an E-Line.
256 * @param s_time The set time
257 * @param d The duration of the xline
258 * @param src The sender of the xline
259 * @param re The reason of the xline
260 * @param ident Ident to match
261 * @param host Host to match
263 ELine(InspIRCd* Instance, time_t s_time, long d, const char* src, const char* re, const char* ident, const char* host) : XLine(Instance, s_time, d, src, re, "E")
265 identmask = strdup(ident);
266 hostmask = strdup(host);
267 matchtext = this->identmask;
268 matchtext.append("@").append(this->hostmask);
277 virtual bool Matches(User *u);
279 virtual bool Matches(const std::string &str);
281 virtual void Unset();
283 virtual void DisplayExpiry();
285 virtual void OnAdd();
287 virtual const char* Displayable();
289 /** Ident mask (ident part only)
292 /** Host mask (host part only)
296 std::string matchtext;
301 class CoreExport ZLine : public XLine
305 * @param s_time The set time
306 * @param d The duration of the xline
307 * @param src The sender of the xline
308 * @param re The reason of the xline
309 * @param ip IP to match
311 ZLine(InspIRCd* Instance, time_t s_time, long d, const char* src, const char* re, const char* ip) : XLine(Instance, s_time, d, src, re, "Z")
323 virtual bool Matches(User *u);
325 virtual bool Matches(const std::string &str);
327 virtual void Apply(User* u);
329 virtual void DisplayExpiry();
331 virtual const char* Displayable();
333 /** IP mask (no ident part)
340 class CoreExport QLine : public XLine
344 * @param s_time The set time
345 * @param d The duration of the xline
346 * @param src The sender of the xline
347 * @param re The reason of the xline
348 * @param nickname Nickname to match
350 QLine(InspIRCd* Instance, time_t s_time, long d, const char* src, const char* re, const char* nickname) : XLine(Instance, s_time, d, src, re, "Q")
352 nick = strdup(nickname);
362 virtual bool Matches(User *u);
364 virtual bool Matches(const std::string &str);
366 virtual void Apply(User* u);
368 virtual void DisplayExpiry();
370 virtual const char* Displayable();
377 /** Contains an ident and host split into two strings
379 typedef std::pair<std::string, std::string> IdentHostPair;
381 /** XLineFactory is used to generate an XLine pointer, given just the
382 * pattern, timing information and type of line to create. This is used
383 * for example in the spanningtree module which will call an XLineFactory
384 * to create a new XLine when it is inbound on a server link, so that it
385 * does not have to know the specifics of the internals of an XLine class
386 * and/or how to call its constructor.
388 class CoreExport XLineFactory : public classbase
392 InspIRCd* ServerInstance;
397 /** Create an XLine factory
398 * @param Instance creator
399 * @param t Type of XLine this factory generates
401 XLineFactory(InspIRCd* Instance, const std::string &t) : ServerInstance(Instance), type(t) { }
403 /** Return the type of XLine this factory generates
404 * @return The type of XLine this factory generates
406 virtual const std::string& GetType() { return type; }
408 /** Generate a specialized XLine*.
409 * @param set_time Time this line was created
410 * @param duration Duration of the line
411 * @param source The sender of the line, nickname or server
412 * @param reason The reason for the line
413 * @param xline_specific_mask The mask string for the line, specific to the XLine type being created.
414 * @return A specialized XLine class of the given type for this factory.
416 virtual XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask) = 0;
418 virtual bool AutoApplyToUserList(XLine* x) { return true; }
422 virtual ~XLineFactory() { }
425 /* Required forward declarations
436 /** A map of xline factories
438 typedef std::map<std::string, XLineFactory*> XLineFactMap;
440 /** A map of XLines indexed by string
442 typedef std::map<irc::string, XLine *> XLineLookup;
444 /** A map of XLineLookup maps indexed by string
446 typedef std::map<std::string, XLineLookup > XLineContainer;
448 /** An iterator in an XLineContainer
450 typedef XLineContainer::iterator ContainerIter;
452 /** An interator in an XLineLookup
454 typedef XLineLookup::iterator LookupIter;
456 /** XLineManager is a class used to manage glines, klines, elines, zlines and qlines,
457 * or any other line created by a module. It also manages XLineFactory classes which
458 * can generate a specialized XLine for use by another module.
460 class CoreExport XLineManager : public classbase
463 /** The owner/creator of this class
465 InspIRCd* ServerInstance;
467 /** Used to hold XLines which have not yet been applied.
469 std::vector<XLine *> pending_lines;
471 /** Current xline factories
473 XLineFactMap line_factory;
475 /** Core xline factories for G/E/K/Q/Z lines
476 * (These generate GLine, ELine, KLine, QLine and ZLine
485 /** Container of all lines, this is a map of maps which
486 * allows for fast lookup for add/remove of a line, and
487 * the shortest possible timed O(n) for checking a user
490 XLineContainer lookup_lines;
495 * @param Instance A pointer to the creator object
497 XLineManager(InspIRCd* Instance);
503 /** Split an ident and host into two seperate strings.
504 * This allows for faster matching.
506 IdentHostPair IdentSplit(const std::string &ident_and_host);
508 /** Checks what users match e:lines and sets their ban exempt flag accordingly.
512 /** Get all lines of a certain type to an XLineLookup (std::map<std::string, XLine*>).
513 * NOTE: When this function runs any expired items are removed from the list before it
514 * is returned to the caller.
515 * @param The type to look up
516 * @return A list of all XLines of the given type.
518 XLineLookup* GetAll(const std::string &type);
520 /** Remove all lines of a certain type.
522 void DelAll(const std::string &type);
524 /** Return all known types of line currently stored by the XLineManager.
525 * @return A vector containing all known line types currently stored in the main list.
527 std::vector<std::string> GetAllTypes();
530 * @param line The line to be added
531 * @param user The user adding the line or NULL for the local server
532 * @return True if the line was added successfully
534 bool AddLine(XLine* line, User* user);
537 * @param hostmask The xline-specific string identifying the line, e.g. "*@foo"
538 * @param type The type of xline
539 * @param user The user removing the line or NULL if its the local server
540 * @param simulate If this is true, don't actually remove the line, just return
541 * @return True if the line was deleted successfully
543 bool DelLine(const char* hostmask, const std::string &type, User* user, bool simulate = false);
545 /** Registers an xline factory.
546 * An xline factory is a class which when given a particular xline type,
547 * will generate a new XLine specialized to that type. For example if you
548 * pass the XLineFactory that handles glines some data it will return a
549 * pointer to a GLine, polymorphically represented as XLine. This is used where
550 * you do not know the full details of the item you wish to create, e.g. in a
551 * server protocol module like m_spanningtree, when you receive xlines from other
553 * @param xlf XLineFactory pointer to register
555 bool RegisterFactory(XLineFactory* xlf);
557 /** Unregisters an xline factory.
558 * You must do this when your module unloads.
559 * @param xlf XLineFactory pointer to unregister
561 bool UnregisterFactory(XLineFactory* xlf);
563 /** Get the XLineFactory for a specific type.
564 * Returns NULL if there is no known handler for this xline type.
565 * @param type The type of XLine you require the XLineFactory for
567 XLineFactory* GetFactory(const std::string &type);
569 /** Check if a user matches an XLine
570 * @param type The type of line to look up
571 * @param user The user to match against (what is checked is specific to the xline type)
572 * @return The reason for the line if there is a match, or NULL if there is no match
574 XLine* MatchesLine(const std::string &type, User* user);
576 /** Check if a pattern matches an XLine
577 * @param type The type of line to look up
578 * @param pattern A pattern string specific to the xline type
579 * @return The matching XLine if there is a match, or NULL if there is no match
581 XLine* MatchesLine(const std::string &type, const std::string &pattern);
583 /** Expire a line given two iterators which identify it in the main map.
584 * @param container Iterator to the first level of entries the map
585 * @param item Iterator to the second level of entries in the map
587 void ExpireLine(ContainerIter container, LookupIter item);
589 /** Apply any new lines that are pending to be applied.
590 * This will only apply lines in the pending_lines list, to save on
595 /** Handle /STATS for a given type.
596 * NOTE: Any items in the list for this particular line type which have expired
597 * will be expired and removed before the list is displayed.
598 * @param numeric The numeric to give to each result line
599 * @param user The username making the query
600 * @param results The string_list to receive the results
602 void InvokeStats(const std::string &type, int numeric, User* user, string_list &results);
605 /** An XLineFactory specialized to generate GLine* pointers
607 class CoreExport GLineFactory : public XLineFactory
610 GLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "G") { }
614 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
616 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
617 return new GLine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
621 /** An XLineFactory specialized to generate ELine* pointers
623 class CoreExport ELineFactory : public XLineFactory
626 ELineFactory(InspIRCd* Instance) : XLineFactory(Instance, "E") { }
628 /** Generate an ELine
630 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
632 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
633 return new ELine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
637 /** An XLineFactory specialized to generate KLine* pointers
639 class CoreExport KLineFactory : public XLineFactory
642 KLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "K") { }
646 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
648 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
649 return new KLine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
653 /** An XLineFactory specialized to generate QLine* pointers
655 class CoreExport QLineFactory : public XLineFactory
658 QLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "Q") { }
662 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
664 return new QLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
668 /** An XLineFactory specialized to generate ZLine* pointers
670 class CoreExport ZLineFactory : public XLineFactory
673 ZLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "Z") { }
677 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
679 return new ZLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);