1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 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
39 void DefaultApply(User* u, const std::string &line);
44 * @param s_time The set time
45 * @param d The duration of the xline
46 * @param src The sender of the xline
47 * @param re The reason of the xline
48 * @param t The line type, should be set by the derived class constructor
50 XLine(InspIRCd* Instance, time_t s_time, long d, const char* src, const char* re, const std::string &t)
51 : ServerInstance(Instance), set_time(s_time), duration(d), type(t)
55 expiry = set_time + duration;
66 /** Change creation time of an xline. Updates expiry
67 * to be after the creation time
69 virtual void SetCreateTime(time_t created)
72 expiry = created + duration;
75 /** Returns true whether or not the given user is covered by this line.
76 * @param u The user to match against. The mechanics of the match
77 * are defined by the derived class.
78 * @return True if there is a match.
80 virtual bool Matches(User *u) = 0;
82 /** Returns true whether or not the given string is covered by this line.
83 * @param str The string to match against. The details of what must be
84 * in this string and the mechanics of the match are defined by the
86 * @return True if there is a match
88 virtual bool Matches(const std::string &str) = 0;
90 /** Apply a line against a user. The mechanics of what occurs when
91 * the line is applied are specific to the derived class.
92 * @param u The user to apply against
94 virtual void Apply(User* u);
96 /** Called when the line is unset either via expiry or
97 * via explicit removal.
99 virtual void Unset() { }
101 /** Called when the expiry message is to be displayed for the
102 * line. Usually a line in the form 'expiring Xline blah, set by...'
103 * see the DisplayExpiry methods of GLine, ELine etc.
105 virtual void DisplayExpiry() = 0;
107 /** Returns the displayable form of the pattern for this xline,
108 * e.g. '*@foo' or '*baz*'. This must always return the full pattern
109 * in a form which can be used to construct an entire derived xline,
110 * even if it is stored differently internally (e.g. GLine stores the
111 * ident and host parts seperately but will still return ident@host
112 * for its Displayable() method)
114 virtual const char* Displayable() = 0;
116 /** Called when the xline has just been added.
118 virtual void OnAdd() { }
120 /** The time the line was added.
124 /** The duration of the ban, or 0 if permenant
128 /** Source of the ban. This can be a servername or an oper nickname
132 /** Reason for the ban
136 /** Expiry time. Does not contain useful data if the duration is 0.
140 /** "Q", "K", etc. Set only by derived classes constructor to the
141 * type of line this is.
143 const std::string type;
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(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")
162 identmask = strdup(ident);
163 hostmask = strdup(host);
164 matchtext = this->identmask;
165 matchtext.append("@").append(this->hostmask);
176 virtual bool Matches(User *u);
178 virtual bool Matches(const std::string &str);
180 virtual void Apply(User* u);
182 virtual void DisplayExpiry();
184 virtual const char* Displayable();
186 /** Ident mask (ident part only)
189 /** Host mask (host part only)
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(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")
211 identmask = strdup(ident);
212 hostmask = strdup(host);
213 matchtext = this->identmask;
214 matchtext.append("@").append(this->hostmask);
225 virtual bool Matches(User *u);
227 virtual bool Matches(const std::string &str);
229 virtual void Apply(User* u);
231 virtual void DisplayExpiry();
233 virtual const char* Displayable();
235 /** Ident mask (ident part only)
238 /** Host mask (host part only)
242 std::string matchtext;
247 class CoreExport ELine : public XLine
250 /** Create an E-Line.
251 * @param s_time The set time
252 * @param d The duration of the xline
253 * @param src The sender of the xline
254 * @param re The reason of the xline
255 * @param ident Ident to match
256 * @param host Host to match
258 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")
260 identmask = strdup(ident);
261 hostmask = strdup(host);
262 matchtext = this->identmask;
263 matchtext.append("@").append(this->hostmask);
272 virtual bool Matches(User *u);
274 virtual bool Matches(const std::string &str);
276 virtual void Unset();
278 virtual void DisplayExpiry();
280 virtual void OnAdd();
282 virtual const char* Displayable();
284 /** Ident mask (ident part only)
287 /** Host mask (host part only)
291 std::string matchtext;
296 class CoreExport ZLine : public XLine
300 * @param s_time The set time
301 * @param d The duration of the xline
302 * @param src The sender of the xline
303 * @param re The reason of the xline
304 * @param ip IP to match
306 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")
318 virtual bool Matches(User *u);
320 virtual bool Matches(const std::string &str);
322 virtual void Apply(User* u);
324 virtual void DisplayExpiry();
326 virtual const char* Displayable();
328 /** IP mask (no ident part)
335 class CoreExport QLine : public XLine
339 * @param s_time The set time
340 * @param d The duration of the xline
341 * @param src The sender of the xline
342 * @param re The reason of the xline
343 * @param nickname Nickname to match
345 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")
347 nick = strdup(nickname);
357 virtual bool Matches(User *u);
359 virtual bool Matches(const std::string &str);
361 virtual void Apply(User* u);
363 virtual void DisplayExpiry();
365 virtual const char* Displayable();
372 /** Contains an ident and host split into two strings
374 typedef std::pair<std::string, std::string> IdentHostPair;
376 /** XLineFactory is used to generate an XLine pointer, given just the
377 * pattern, timing information and type of line to create. This is used
378 * for example in the spanningtree module which will call an XLineFactory
379 * to create a new XLine when it is inbound on a server link, so that it
380 * does not have to know the specifics of the internals of an XLine class
381 * and/or how to call its constructor.
383 class CoreExport XLineFactory
387 InspIRCd* ServerInstance;
392 /** Create an XLine factory
393 * @param Instance creator
394 * @param t Type of XLine this factory generates
396 XLineFactory(InspIRCd* Instance, const std::string &t) : ServerInstance(Instance), type(t) { }
398 /** Return the type of XLine this factory generates
399 * @return The type of XLine this factory generates
401 virtual const std::string& GetType() { return type; }
403 /** Generate a specialized XLine*.
404 * @param set_time Time this line was created
405 * @param duration Duration of the line
406 * @param source The sender of the line, nickname or server
407 * @param reason The reason for the line
408 * @param xline_specific_mask The mask string for the line, specific to the XLine type being created.
409 * @return A specialized XLine class of the given type for this factory.
411 virtual XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask) = 0;
415 virtual ~XLineFactory() { }
418 /* Required forward declarations
429 /** A map of xline factories
431 typedef std::map<std::string, XLineFactory*> XLineFactMap;
433 /** A map of XLines indexed by string
435 typedef std::map<std::string, XLine *> XLineLookup;
437 /** A map of XLineLookup maps indexed by string
439 typedef std::map<std::string, XLineLookup > XLineContainer;
441 /** An iterator in an XLineContainer
443 typedef XLineContainer::iterator ContainerIter;
445 /** An interator in an XLineLookup
447 typedef XLineLookup::iterator LookupIter;
449 /** XLineManager is a class used to manage glines, klines, elines, zlines and qlines,
450 * or any other line created by a module. It also manages XLineFactory classes which
451 * can generate a specialized XLine for use by another module.
453 class CoreExport XLineManager
456 /** The owner/creator of this class
458 InspIRCd* ServerInstance;
460 /** Used to hold XLines which have not yet been applied.
462 std::vector<XLine *> pending_lines;
464 /** Current xline factories
466 XLineFactMap line_factory;
468 /** Core xline factories for G/E/K/Q/Z lines
469 * (These generate GLine, ELine, KLine, QLine and ZLine
478 /** Container of all lines, this is a map of maps which
479 * allows for fast lookup for add/remove of a line, and
480 * the shortest possible timed O(n) for checking a user
483 XLineContainer lookup_lines;
488 * @param Instance A pointer to the creator object
490 XLineManager(InspIRCd* Instance);
496 /** Split an ident and host into two seperate strings.
497 * This allows for faster matching.
499 IdentHostPair IdentSplit(const std::string &ident_and_host);
501 /** Checks what users match e:lines and sets their ban exempt flag accordingly.
505 /** Get all lines of a certain type to an XLineLookup (std::map<std::string, XLine*>).
506 * NOTE: When this function runs any expired items are removed from the list before it
507 * is returned to the caller.
508 * @param The type to look up
509 * @return A list of all XLines of the given type.
511 XLineLookup* GetAll(const std::string &type);
513 /** Return all known types of line currently stored by the XLineManager.
514 * @return A vector containing all known line types currently stored in the main list.
516 std::vector<std::string> GetAllTypes();
519 * @param line The line to be added
520 * @param user The user adding the line or NULL for the local server
521 * @return True if the line was added successfully
523 bool AddLine(XLine* line, User* user);
526 * @param hostmask The xline-specific string identifying the line, e.g. "*@foo"
527 * @param type The type of xline
528 * @param user The user removing the line or NULL if its the local server
529 * @param simulate If this is true, don't actually remove the line, just return
530 * @return True if the line was deleted successfully
532 bool DelLine(const char* hostmask, const std::string &type, User* user, bool simulate = false);
534 /** Registers an xline factory.
535 * An xline factory is a class which when given a particular xline type,
536 * will generate a new XLine specialized to that type. For example if you
537 * pass the XLineFactory that handles glines some data it will return a
538 * pointer to a GLine, polymorphically represented as XLine. This is used where
539 * you do not know the full details of the item you wish to create, e.g. in a
540 * server protocol module like m_spanningtree, when you receive xlines from other
542 * @param xlf XLineFactory pointer to register
544 bool RegisterFactory(XLineFactory* xlf);
546 /** Unregisters an xline factory.
547 * You must do this when your module unloads.
548 * @param xlf XLineFactory pointer to unregister
550 bool UnregisterFactory(XLineFactory* xlf);
552 /** Get the XLineFactory for a specific type.
553 * Returns NULL if there is no known handler for this xline type.
554 * @param type The type of XLine you require the XLineFactory for
556 XLineFactory* GetFactory(const std::string &type);
558 /** Check if a user matches an XLine
559 * @param type The type of line to look up
560 * @param user The user to match against (what is checked is specific to the xline type)
561 * @return The reason for the line if there is a match, or NULL if there is no match
563 XLine* MatchesLine(const std::string &type, User* user);
565 /** Check if a pattern matches an XLine
566 * @param type The type of line to look up
567 * @param pattern A pattern string specific to the xline type
568 * @return The matching XLine if there is a match, or NULL if there is no match
570 XLine* MatchesLine(const std::string &type, const std::string &pattern);
572 /** Expire a line given two iterators which identify it in the main map.
573 * @param container Iterator to the first level of entries the map
574 * @param item Iterator to the second level of entries in the map
576 void ExpireLine(ContainerIter container, LookupIter item);
578 /** Apply any new lines that are pending to be applied.
579 * This will only apply lines in the pending_lines list, to save on
584 /** Handle /STATS for a given type.
585 * NOTE: Any items in the list for this particular line type which have expired
586 * will be expired and removed before the list is displayed.
587 * @param numeric The numeric to give to each result line
588 * @param user The username making the query
589 * @param results The string_list to receive the results
591 void InvokeStats(const std::string &type, int numeric, User* user, string_list &results);
594 /** An XLineFactory specialized to generate GLine* pointers
596 class CoreExport GLineFactory : public XLineFactory
599 GLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "G") { }
603 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
605 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
606 return new GLine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
610 /** An XLineFactory specialized to generate ELine* pointers
612 class CoreExport ELineFactory : public XLineFactory
615 ELineFactory(InspIRCd* Instance) : XLineFactory(Instance, "E") { }
617 /** Generate an ELine
619 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
621 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
622 return new ELine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
626 /** An XLineFactory specialized to generate KLine* pointers
628 class CoreExport KLineFactory : public XLineFactory
631 KLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "K") { }
635 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
637 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
638 return new KLine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
642 /** An XLineFactory specialized to generate QLine* pointers
644 class CoreExport QLineFactory : public XLineFactory
647 QLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "Q") { }
651 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
653 return new QLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
657 /** An XLineFactory specialized to generate ZLine* pointers
659 class CoreExport ZLineFactory : public XLineFactory
662 ZLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "Z") { }
666 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
668 return new ZLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);