1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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;
420 virtual ~XLineFactory() { }
423 /* Required forward declarations
434 /** A map of xline factories
436 typedef std::map<std::string, XLineFactory*> XLineFactMap;
438 /** A map of XLines indexed by string
440 typedef std::map<std::string, XLine *> XLineLookup;
442 /** A map of XLineLookup maps indexed by string
444 typedef std::map<std::string, XLineLookup > XLineContainer;
446 /** An iterator in an XLineContainer
448 typedef XLineContainer::iterator ContainerIter;
450 /** An interator in an XLineLookup
452 typedef XLineLookup::iterator LookupIter;
454 /** XLineManager is a class used to manage glines, klines, elines, zlines and qlines,
455 * or any other line created by a module. It also manages XLineFactory classes which
456 * can generate a specialized XLine for use by another module.
458 class CoreExport XLineManager : public classbase
461 /** The owner/creator of this class
463 InspIRCd* ServerInstance;
465 /** Used to hold XLines which have not yet been applied.
467 std::vector<XLine *> pending_lines;
469 /** Current xline factories
471 XLineFactMap line_factory;
473 /** Core xline factories for G/E/K/Q/Z lines
474 * (These generate GLine, ELine, KLine, QLine and ZLine
483 /** Container of all lines, this is a map of maps which
484 * allows for fast lookup for add/remove of a line, and
485 * the shortest possible timed O(n) for checking a user
488 XLineContainer lookup_lines;
493 * @param Instance A pointer to the creator object
495 XLineManager(InspIRCd* Instance);
501 /** Split an ident and host into two seperate strings.
502 * This allows for faster matching.
504 IdentHostPair IdentSplit(const std::string &ident_and_host);
506 /** Checks what users match e:lines and sets their ban exempt flag accordingly.
510 /** Get all lines of a certain type to an XLineLookup (std::map<std::string, XLine*>).
511 * NOTE: When this function runs any expired items are removed from the list before it
512 * is returned to the caller.
513 * @param The type to look up
514 * @return A list of all XLines of the given type.
516 XLineLookup* GetAll(const std::string &type);
518 /** Return all known types of line currently stored by the XLineManager.
519 * @return A vector containing all known line types currently stored in the main list.
521 std::vector<std::string> GetAllTypes();
524 * @param line The line to be added
525 * @param user The user adding the line or NULL for the local server
526 * @return True if the line was added successfully
528 bool AddLine(XLine* line, User* user);
531 * @param hostmask The xline-specific string identifying the line, e.g. "*@foo"
532 * @param type The type of xline
533 * @param user The user removing the line or NULL if its the local server
534 * @param simulate If this is true, don't actually remove the line, just return
535 * @return True if the line was deleted successfully
537 bool DelLine(const char* hostmask, const std::string &type, User* user, bool simulate = false);
539 /** Registers an xline factory.
540 * An xline factory is a class which when given a particular xline type,
541 * will generate a new XLine specialized to that type. For example if you
542 * pass the XLineFactory that handles glines some data it will return a
543 * pointer to a GLine, polymorphically represented as XLine. This is used where
544 * you do not know the full details of the item you wish to create, e.g. in a
545 * server protocol module like m_spanningtree, when you receive xlines from other
547 * @param xlf XLineFactory pointer to register
549 bool RegisterFactory(XLineFactory* xlf);
551 /** Unregisters an xline factory.
552 * You must do this when your module unloads.
553 * @param xlf XLineFactory pointer to unregister
555 bool UnregisterFactory(XLineFactory* xlf);
557 /** Get the XLineFactory for a specific type.
558 * Returns NULL if there is no known handler for this xline type.
559 * @param type The type of XLine you require the XLineFactory for
561 XLineFactory* GetFactory(const std::string &type);
563 /** Check if a user matches an XLine
564 * @param type The type of line to look up
565 * @param user The user to match against (what is checked is specific to the xline type)
566 * @return The reason for the line if there is a match, or NULL if there is no match
568 XLine* MatchesLine(const std::string &type, User* user);
570 /** Check if a pattern matches an XLine
571 * @param type The type of line to look up
572 * @param pattern A pattern string specific to the xline type
573 * @return The matching XLine if there is a match, or NULL if there is no match
575 XLine* MatchesLine(const std::string &type, const std::string &pattern);
577 /** Expire a line given two iterators which identify it in the main map.
578 * @param container Iterator to the first level of entries the map
579 * @param item Iterator to the second level of entries in the map
581 void ExpireLine(ContainerIter container, LookupIter item);
583 /** Apply any new lines that are pending to be applied.
584 * This will only apply lines in the pending_lines list, to save on
589 /** Handle /STATS for a given type.
590 * NOTE: Any items in the list for this particular line type which have expired
591 * will be expired and removed before the list is displayed.
592 * @param numeric The numeric to give to each result line
593 * @param user The username making the query
594 * @param results The string_list to receive the results
596 void InvokeStats(const std::string &type, int numeric, User* user, string_list &results);
599 /** An XLineFactory specialized to generate GLine* pointers
601 class CoreExport GLineFactory : public XLineFactory
604 GLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "G") { }
608 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
610 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
611 return new GLine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
615 /** An XLineFactory specialized to generate ELine* pointers
617 class CoreExport ELineFactory : public XLineFactory
620 ELineFactory(InspIRCd* Instance) : XLineFactory(Instance, "E") { }
622 /** Generate an ELine
624 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
626 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
627 return new ELine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
631 /** An XLineFactory specialized to generate KLine* pointers
633 class CoreExport KLineFactory : public XLineFactory
636 KLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "K") { }
640 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
642 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
643 return new KLine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
647 /** An XLineFactory specialized to generate QLine* pointers
649 class CoreExport QLineFactory : public XLineFactory
652 QLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "Q") { }
656 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
658 return new QLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
662 /** An XLineFactory specialized to generate ZLine* pointers
664 class CoreExport ZLineFactory : public XLineFactory
667 ZLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "Z") { }
671 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
673 return new ZLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);