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
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, std::string src, std::string re, const std::string &t)
52 : ServerInstance(Instance), set_time(s_time), duration(d), source(src), reason(re), type(t)
54 expiry = set_time + duration;
63 /** Change creation time of an xline. Updates expiry
64 * to be after the creation time
66 virtual void SetCreateTime(time_t created)
69 expiry = created + duration;
72 /** Returns true whether or not the given user is covered by this line.
73 * @param u The user to match against. The mechanics of the match
74 * are defined by the derived class.
75 * @return True if there is a match.
77 virtual bool Matches(User *u) = 0;
79 /** Returns true whether or not the given string is covered by this line.
80 * @param str The string to match against. The details of what must be
81 * in this string and the mechanics of the match are defined by the
83 * @return True if there is a match
85 virtual bool Matches(const std::string &str) = 0;
87 /** Apply a line against a user. The mechanics of what occurs when
88 * the line is applied are specific to the derived class.
89 * @param u The user to apply against
91 virtual void Apply(User* u);
93 /** Called when the line is unset either via expiry or
94 * via explicit removal.
96 virtual void Unset() { }
98 /** Called when the expiry message is to be displayed for the
99 * line. Usually a line in the form 'expiring Xline blah, set by...'
100 * see the DisplayExpiry methods of GLine, ELine etc.
102 virtual void DisplayExpiry() = 0;
104 /** Returns the displayable form of the pattern for this xline,
105 * e.g. '*@foo' or '*baz*'. This must always return the full pattern
106 * in a form which can be used to construct an entire derived xline,
107 * even if it is stored differently internally (e.g. GLine stores the
108 * ident and host parts seperately but will still return ident@host
109 * for its Displayable() method)
111 virtual const char* Displayable() = 0;
113 /** Called when the xline has just been added.
115 virtual void OnAdd() { }
117 /** The time the line was added.
121 /** The duration of the ban, or 0 if permenant
125 /** Source of the ban. This can be a servername or an oper nickname
129 /** Reason for the ban
133 /** Expiry time. Does not contain useful data if the duration is 0.
137 /** "Q", "K", etc. Set only by derived classes constructor to the
138 * type of line this is.
140 const std::string type;
142 virtual bool IsBurstable();
147 class CoreExport KLine : public XLine
152 * @param s_time The set time
153 * @param d The duration of the xline
154 * @param src The sender of the xline
155 * @param re The reason of the xline
156 * @param ident Ident to match
157 * @param host Host to match
159 KLine(InspIRCd* Instance, time_t s_time, long d, std::string src, std::string re, std::string ident, std::string host)
160 : XLine(Instance, s_time, d, src, re, "K"), identmask(ident), hostmask(host)
162 matchtext = this->identmask;
163 matchtext.append("@").append(this->hostmask);
172 virtual bool Matches(User *u);
174 virtual bool Matches(const std::string &str);
176 virtual void Apply(User* u);
178 virtual void DisplayExpiry();
180 virtual const char* Displayable();
182 virtual bool IsBurstable();
184 /** Ident mask (ident part only)
186 std::string identmask;
187 /** Host mask (host part only)
189 std::string hostmask;
191 std::string matchtext;
196 class CoreExport GLine : public XLine
200 * @param s_time The set time
201 * @param d The duration of the xline
202 * @param src The sender of the xline
203 * @param re The reason of the xline
204 * @param ident Ident to match
205 * @param host Host to match
207 GLine(InspIRCd* Instance, time_t s_time, long d, std::string src, std::string re, std::string ident, std::string host)
208 : XLine(Instance, s_time, d, src, re, "G"), identmask(ident), hostmask(host)
210 matchtext = this->identmask;
211 matchtext.append("@").append(this->hostmask);
220 virtual bool Matches(User *u);
222 virtual bool Matches(const std::string &str);
224 virtual void Apply(User* u);
226 virtual void DisplayExpiry();
228 virtual const char* Displayable();
230 /** Ident mask (ident part only)
232 std::string identmask;
233 /** Host mask (host part only)
235 std::string hostmask;
237 std::string matchtext;
242 class CoreExport ELine : public XLine
245 /** Create an E-Line.
246 * @param s_time The set time
247 * @param d The duration of the xline
248 * @param src The sender of the xline
249 * @param re The reason of the xline
250 * @param ident Ident to match
251 * @param host Host to match
253 ELine(InspIRCd* Instance, time_t s_time, long d, std::string src, std::string re, std::string ident, std::string host)
254 : XLine(Instance, s_time, d, src, re, "E"), identmask(ident), hostmask(host)
256 matchtext = this->identmask;
257 matchtext.append("@").append(this->hostmask);
264 virtual bool Matches(User *u);
266 virtual bool Matches(const std::string &str);
268 virtual void Unset();
270 virtual void DisplayExpiry();
272 virtual void OnAdd();
274 virtual const char* Displayable();
276 /** Ident mask (ident part only)
278 std::string identmask;
279 /** Host mask (host part only)
281 std::string hostmask;
283 std::string matchtext;
288 class CoreExport ZLine : public XLine
292 * @param s_time The set time
293 * @param d The duration of the xline
294 * @param src The sender of the xline
295 * @param re The reason of the xline
296 * @param ip IP to match
298 ZLine(InspIRCd* Instance, time_t s_time, long d, std::string src, std::string re, std::string ip)
299 : XLine(Instance, s_time, d, src, re, "Z"), ipaddr(ip)
309 virtual bool Matches(User *u);
311 virtual bool Matches(const std::string &str);
313 virtual void Apply(User* u);
315 virtual void DisplayExpiry();
317 virtual const char* Displayable();
319 /** IP mask (no ident part)
326 class CoreExport QLine : public XLine
330 * @param s_time The set time
331 * @param d The duration of the xline
332 * @param src The sender of the xline
333 * @param re The reason of the xline
334 * @param nickname Nickname to match
336 QLine(InspIRCd* Instance, time_t s_time, long d, std::string src, std::string re, std::string nickname)
337 : XLine(Instance, s_time, d, src, re, "Q"), nick(nickname)
346 virtual bool Matches(User *u);
348 virtual bool Matches(const std::string &str);
350 virtual void Apply(User* u);
352 virtual void DisplayExpiry();
354 virtual const char* Displayable();
361 /** Contains an ident and host split into two strings
363 typedef std::pair<std::string, std::string> IdentHostPair;
365 /** XLineFactory is used to generate an XLine pointer, given just the
366 * pattern, timing information and type of line to create. This is used
367 * for example in the spanningtree module which will call an XLineFactory
368 * to create a new XLine when it is inbound on a server link, so that it
369 * does not have to know the specifics of the internals of an XLine class
370 * and/or how to call its constructor.
372 class CoreExport XLineFactory : public classbase
376 InspIRCd* ServerInstance;
381 /** Create an XLine factory
382 * @param Instance creator
383 * @param t Type of XLine this factory generates
385 XLineFactory(InspIRCd* Instance, const std::string &t) : ServerInstance(Instance), type(t) { }
387 /** Return the type of XLine this factory generates
388 * @return The type of XLine this factory generates
390 virtual const std::string& GetType() { return type; }
392 /** Generate a specialized XLine*.
393 * @param set_time Time this line was created
394 * @param duration Duration of the line
395 * @param source The sender of the line, nickname or server
396 * @param reason The reason for the line
397 * @param xline_specific_mask The mask string for the line, specific to the XLine type being created.
398 * @return A specialized XLine class of the given type for this factory.
400 virtual XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask) = 0;
402 virtual bool AutoApplyToUserList(XLine* x) { return true; }
406 virtual ~XLineFactory() { }
409 /* Required forward declarations
420 /** A map of xline factories
422 typedef std::map<std::string, XLineFactory*> XLineFactMap;
424 /** A map of XLines indexed by string
426 typedef std::map<irc::string, XLine *> XLineLookup;
428 /** A map of XLineLookup maps indexed by string
430 typedef std::map<std::string, XLineLookup > XLineContainer;
432 /** An iterator in an XLineContainer
434 typedef XLineContainer::iterator ContainerIter;
436 /** An interator in an XLineLookup
438 typedef XLineLookup::iterator LookupIter;
440 /** XLineManager is a class used to manage glines, klines, elines, zlines and qlines,
441 * or any other line created by a module. It also manages XLineFactory classes which
442 * can generate a specialized XLine for use by another module.
444 class CoreExport XLineManager : public classbase
447 /** The owner/creator of this class
449 InspIRCd* ServerInstance;
451 /** Used to hold XLines which have not yet been applied.
453 std::vector<XLine *> pending_lines;
455 /** Current xline factories
457 XLineFactMap line_factory;
459 /** Core xline factories for G/E/K/Q/Z lines
460 * (These generate GLine, ELine, KLine, QLine and ZLine
469 /** Container of all lines, this is a map of maps which
470 * allows for fast lookup for add/remove of a line, and
471 * the shortest possible timed O(n) for checking a user
474 XLineContainer lookup_lines;
479 * @param Instance A pointer to the creator object
481 XLineManager(InspIRCd* Instance);
487 /** Split an ident and host into two seperate strings.
488 * This allows for faster matching.
490 IdentHostPair IdentSplit(const std::string &ident_and_host);
492 /** Checks what users match e:lines and sets their ban exempt flag accordingly.
496 /** Get all lines of a certain type to an XLineLookup (std::map<std::string, XLine*>).
497 * NOTE: When this function runs any expired items are removed from the list before it
498 * is returned to the caller.
499 * @param The type to look up
500 * @return A list of all XLines of the given type.
502 XLineLookup* GetAll(const std::string &type);
504 /** Remove all lines of a certain type.
506 void DelAll(const std::string &type);
508 /** Return all known types of line currently stored by the XLineManager.
509 * @return A vector containing all known line types currently stored in the main list.
511 std::vector<std::string> GetAllTypes();
514 * @param line The line to be added
515 * @param user The user adding the line or NULL for the local server
516 * @return True if the line was added successfully
518 bool AddLine(XLine* line, User* user);
521 * @param hostmask The xline-specific string identifying the line, e.g. "*@foo"
522 * @param type The type of xline
523 * @param user The user removing the line or NULL if its the local server
524 * @param simulate If this is true, don't actually remove the line, just return
525 * @return True if the line was deleted successfully
527 bool DelLine(const char* hostmask, const std::string &type, User* user, bool simulate = false);
529 /** Registers an xline factory.
530 * An xline factory is a class which when given a particular xline type,
531 * will generate a new XLine specialized to that type. For example if you
532 * pass the XLineFactory that handles glines some data it will return a
533 * pointer to a GLine, polymorphically represented as XLine. This is used where
534 * you do not know the full details of the item you wish to create, e.g. in a
535 * server protocol module like m_spanningtree, when you receive xlines from other
537 * @param xlf XLineFactory pointer to register
539 bool RegisterFactory(XLineFactory* xlf);
541 /** Unregisters an xline factory.
542 * You must do this when your module unloads.
543 * @param xlf XLineFactory pointer to unregister
545 bool UnregisterFactory(XLineFactory* xlf);
547 /** Get the XLineFactory for a specific type.
548 * Returns NULL if there is no known handler for this xline type.
549 * @param type The type of XLine you require the XLineFactory for
551 XLineFactory* GetFactory(const std::string &type);
553 /** Check if a user matches an XLine
554 * @param type The type of line to look up
555 * @param user The user to match against (what is checked is specific to the xline type)
556 * @return The reason for the line if there is a match, or NULL if there is no match
558 XLine* MatchesLine(const std::string &type, User* user);
560 /** Check if a pattern matches an XLine
561 * @param type The type of line to look up
562 * @param pattern A pattern string specific to the xline type
563 * @return The matching XLine if there is a match, or NULL if there is no match
565 XLine* MatchesLine(const std::string &type, const std::string &pattern);
567 /** Expire a line given two iterators which identify it in the main map.
568 * @param container Iterator to the first level of entries the map
569 * @param item Iterator to the second level of entries in the map
571 void ExpireLine(ContainerIter container, LookupIter item);
573 /** Apply any new lines that are pending to be applied.
574 * This will only apply lines in the pending_lines list, to save on
579 /** Handle /STATS for a given type.
580 * NOTE: Any items in the list for this particular line type which have expired
581 * will be expired and removed before the list is displayed.
582 * @param numeric The numeric to give to each result line
583 * @param user The username making the query
584 * @param results The string_list to receive the results
586 void InvokeStats(const std::string &type, int numeric, User* user, string_list &results);
589 /** An XLineFactory specialized to generate GLine* pointers
591 class CoreExport GLineFactory : public XLineFactory
594 GLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "G") { }
598 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
600 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
601 return new GLine(ServerInstance, set_time, duration, source, reason, ih.first, ih.second);
605 /** An XLineFactory specialized to generate ELine* pointers
607 class CoreExport ELineFactory : public XLineFactory
610 ELineFactory(InspIRCd* Instance) : XLineFactory(Instance, "E") { }
612 /** Generate an ELine
614 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
616 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
617 return new ELine(ServerInstance, set_time, duration, source, reason, ih.first, ih.second);
621 /** An XLineFactory specialized to generate KLine* pointers
623 class CoreExport KLineFactory : public XLineFactory
626 KLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "K") { }
630 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
632 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
633 return new KLine(ServerInstance, set_time, duration, source, reason, ih.first, ih.second);
637 /** An XLineFactory specialized to generate QLine* pointers
639 class CoreExport QLineFactory : public XLineFactory
642 QLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "Q") { }
646 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
648 return new QLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
652 /** An XLineFactory specialized to generate ZLine* pointers
654 class CoreExport ZLineFactory : public XLineFactory
657 ZLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "Z") { }
661 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
663 return new ZLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);