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;
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(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")
163 identmask = strdup(ident);
164 hostmask = strdup(host);
165 matchtext = this->identmask;
166 matchtext.append("@").append(this->hostmask);
177 virtual bool Matches(User *u);
179 virtual bool Matches(const std::string &str);
181 virtual void Apply(User* u);
183 virtual void DisplayExpiry();
185 virtual const char* Displayable();
187 /** Ident mask (ident part only)
190 /** Host mask (host part only)
194 std::string matchtext;
199 class CoreExport GLine : public XLine
203 * @param s_time The set time
204 * @param d The duration of the xline
205 * @param src The sender of the xline
206 * @param re The reason of the xline
207 * @param ident Ident to match
208 * @param host Host to match
210 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")
212 identmask = strdup(ident);
213 hostmask = strdup(host);
214 matchtext = this->identmask;
215 matchtext.append("@").append(this->hostmask);
226 virtual bool Matches(User *u);
228 virtual bool Matches(const std::string &str);
230 virtual void Apply(User* u);
232 virtual void DisplayExpiry();
234 virtual const char* Displayable();
236 /** Ident mask (ident part only)
239 /** Host mask (host part only)
243 std::string matchtext;
248 class CoreExport ELine : public XLine
251 /** Create an E-Line.
252 * @param s_time The set time
253 * @param d The duration of the xline
254 * @param src The sender of the xline
255 * @param re The reason of the xline
256 * @param ident Ident to match
257 * @param host Host to match
259 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")
261 identmask = strdup(ident);
262 hostmask = strdup(host);
263 matchtext = this->identmask;
264 matchtext.append("@").append(this->hostmask);
273 virtual bool Matches(User *u);
275 virtual bool Matches(const std::string &str);
277 virtual void Unset();
279 virtual void DisplayExpiry();
281 virtual void OnAdd();
283 virtual const char* Displayable();
285 /** Ident mask (ident part only)
288 /** Host mask (host part only)
292 std::string matchtext;
297 class CoreExport ZLine : public XLine
301 * @param s_time The set time
302 * @param d The duration of the xline
303 * @param src The sender of the xline
304 * @param re The reason of the xline
305 * @param ip IP to match
307 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")
319 virtual bool Matches(User *u);
321 virtual bool Matches(const std::string &str);
323 virtual void Apply(User* u);
325 virtual void DisplayExpiry();
327 virtual const char* Displayable();
329 /** IP mask (no ident part)
336 class CoreExport QLine : public XLine
340 * @param s_time The set time
341 * @param d The duration of the xline
342 * @param src The sender of the xline
343 * @param re The reason of the xline
344 * @param nickname Nickname to match
346 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")
348 nick = strdup(nickname);
358 virtual bool Matches(User *u);
360 virtual bool Matches(const std::string &str);
362 virtual void Apply(User* u);
364 virtual void DisplayExpiry();
366 virtual const char* Displayable();
373 /** Contains an ident and host split into two strings
375 typedef std::pair<std::string, std::string> IdentHostPair;
377 /** XLineFactory is used to generate an XLine pointer, given just the
378 * pattern, timing information and type of line to create. This is used
379 * for example in the spanningtree module which will call an XLineFactory
380 * to create a new XLine when it is inbound on a server link, so that it
381 * does not have to know the specifics of the internals of an XLine class
382 * and/or how to call its constructor.
384 class CoreExport XLineFactory
388 InspIRCd* ServerInstance;
393 /** Create an XLine factory
394 * @param Instance creator
395 * @param t Type of XLine this factory generates
397 XLineFactory(InspIRCd* Instance, const std::string &t) : ServerInstance(Instance), type(t) { }
399 /** Return the type of XLine this factory generates
400 * @return The type of XLine this factory generates
402 virtual const std::string& GetType() { return type; }
404 /** Generate a specialized XLine*.
405 * @param set_time Time this line was created
406 * @param duration Duration of the line
407 * @param source The sender of the line, nickname or server
408 * @param reason The reason for the line
409 * @param xline_specific_mask The mask string for the line, specific to the XLine type being created.
410 * @return A specialized XLine class of the given type for this factory.
412 virtual XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask) = 0;
416 virtual ~XLineFactory() { }
419 /* Required forward declarations
430 /** A map of xline factories
432 typedef std::map<std::string, XLineFactory*> XLineFactMap;
434 /** A map of XLines indexed by string
436 typedef std::map<std::string, XLine *> XLineLookup;
438 /** A map of XLineLookup maps indexed by string
440 typedef std::map<std::string, XLineLookup > XLineContainer;
442 /** An iterator in an XLineContainer
444 typedef XLineContainer::iterator ContainerIter;
446 /** An interator in an XLineLookup
448 typedef XLineLookup::iterator LookupIter;
450 /** XLineManager is a class used to manage glines, klines, elines, zlines and qlines,
451 * or any other line created by a module. It also manages XLineFactory classes which
452 * can generate a specialized XLine for use by another module.
454 class CoreExport XLineManager
457 /** The owner/creator of this class
459 InspIRCd* ServerInstance;
461 /** Used to hold XLines which have not yet been applied.
463 std::vector<XLine *> pending_lines;
465 /** Current xline factories
467 XLineFactMap line_factory;
469 /** Core xline factories for G/E/K/Q/Z lines
470 * (These generate GLine, ELine, KLine, QLine and ZLine
479 /** Container of all lines, this is a map of maps which
480 * allows for fast lookup for add/remove of a line, and
481 * the shortest possible timed O(n) for checking a user
484 XLineContainer lookup_lines;
489 * @param Instance A pointer to the creator object
491 XLineManager(InspIRCd* Instance);
497 /** Split an ident and host into two seperate strings.
498 * This allows for faster matching.
500 IdentHostPair IdentSplit(const std::string &ident_and_host);
502 /** Checks what users match e:lines and sets their ban exempt flag accordingly.
506 /** Get all lines of a certain type to an XLineLookup (std::map<std::string, XLine*>).
507 * NOTE: When this function runs any expired items are removed from the list before it
508 * is returned to the caller.
509 * @param The type to look up
510 * @return A list of all XLines of the given type.
512 XLineLookup* GetAll(const std::string &type);
514 /** Return all known types of line currently stored by the XLineManager.
515 * @return A vector containing all known line types currently stored in the main list.
517 std::vector<std::string> GetAllTypes();
520 * @param line The line to be added
521 * @param user The user adding the line or NULL for the local server
522 * @return True if the line was added successfully
524 bool AddLine(XLine* line, User* user);
527 * @param hostmask The xline-specific string identifying the line, e.g. "*@foo"
528 * @param type The type of xline
529 * @param user The user removing the line or NULL if its the local server
530 * @param simulate If this is true, don't actually remove the line, just return
531 * @return True if the line was deleted successfully
533 bool DelLine(const char* hostmask, const std::string &type, User* user, bool simulate = false);
535 /** Registers an xline factory.
536 * An xline factory is a class which when given a particular xline type,
537 * will generate a new XLine specialized to that type. For example if you
538 * pass the XLineFactory that handles glines some data it will return a
539 * pointer to a GLine, polymorphically represented as XLine. This is used where
540 * you do not know the full details of the item you wish to create, e.g. in a
541 * server protocol module like m_spanningtree, when you receive xlines from other
543 * @param xlf XLineFactory pointer to register
545 bool RegisterFactory(XLineFactory* xlf);
547 /** Unregisters an xline factory.
548 * You must do this when your module unloads.
549 * @param xlf XLineFactory pointer to unregister
551 bool UnregisterFactory(XLineFactory* xlf);
553 /** Get the XLineFactory for a specific type.
554 * Returns NULL if there is no known handler for this xline type.
555 * @param type The type of XLine you require the XLineFactory for
557 XLineFactory* GetFactory(const std::string &type);
559 /** Check if a user matches an XLine
560 * @param type The type of line to look up
561 * @param user The user to match against (what is checked is specific to the xline type)
562 * @return The reason for the line if there is a match, or NULL if there is no match
564 XLine* MatchesLine(const std::string &type, User* user);
566 /** Check if a pattern matches an XLine
567 * @param type The type of line to look up
568 * @param pattern A pattern string specific to the xline type
569 * @return The matching XLine if there is a match, or NULL if there is no match
571 XLine* MatchesLine(const std::string &type, const std::string &pattern);
573 /** Expire a line given two iterators which identify it in the main map.
574 * @param container Iterator to the first level of entries the map
575 * @param item Iterator to the second level of entries in the map
577 void ExpireLine(ContainerIter container, LookupIter item);
579 /** Apply any new lines that are pending to be applied.
580 * This will only apply lines in the pending_lines list, to save on
585 /** Handle /STATS for a given type.
586 * NOTE: Any items in the list for this particular line type which have expired
587 * will be expired and removed before the list is displayed.
588 * @param numeric The numeric to give to each result line
589 * @param user The username making the query
590 * @param results The string_list to receive the results
592 void InvokeStats(const std::string &type, int numeric, User* user, string_list &results);
595 /** An XLineFactory specialized to generate GLine* pointers
597 class CoreExport GLineFactory : public XLineFactory
600 GLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "G") { }
604 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
606 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
607 return new GLine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
611 /** An XLineFactory specialized to generate ELine* pointers
613 class CoreExport ELineFactory : public XLineFactory
616 ELineFactory(InspIRCd* Instance) : XLineFactory(Instance, "E") { }
618 /** Generate an ELine
620 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
622 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
623 return new ELine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
627 /** An XLineFactory specialized to generate KLine* pointers
629 class CoreExport KLineFactory : public XLineFactory
632 KLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "K") { }
636 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
638 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
639 return new KLine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
643 /** An XLineFactory specialized to generate QLine* pointers
645 class CoreExport QLineFactory : public XLineFactory
648 QLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "Q") { }
652 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
654 return new QLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
658 /** An XLineFactory specialized to generate ZLine* pointers
660 class CoreExport ZLineFactory : public XLineFactory
663 ZLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "Z") { }
667 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
669 return new ZLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);