1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 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 * ---------------------------------------------------
17 /** XLine is the base class for ban lines such as G lines and K lines.
18 * Modules may derive from this, and their xlines will automatically be
19 * handled as expected by any protocol modules (e.g. m_spanningtree will
20 * propogate them using AddLine). The process of translating a type+pattern
21 * to a known line type is done by means of an XLineFactory object (see
24 class CoreExport XLine : public classbase
28 /** Default 'apply' action. Quits the user.
29 * @param u User to apply the line against
30 * @param line The line typed, used for display purposes in the quit message
31 * @param bancache If true, the user will be added to the bancache if they match. Else not.
33 void DefaultApply(User* u, const std::string &line, bool bancache);
38 * @param s_time The set time
39 * @param d The duration of the xline
40 * @param src The sender of the xline
41 * @param re The reason of the xline
42 * @param t The line type, should be set by the derived class constructor
44 XLine(time_t s_time, long d, std::string src, std::string re, const std::string &t)
45 : set_time(s_time), duration(d), source(src), reason(re), type(t)
47 expiry = set_time + duration;
56 /** Change creation time of an xline. Updates expiry
57 * to be after the creation time
59 virtual void SetCreateTime(time_t created)
62 expiry = created + duration;
65 /** Returns true whether or not the given user is covered by this line.
66 * @param u The user to match against. The mechanics of the match
67 * are defined by the derived class.
68 * @return True if there is a match.
70 virtual bool Matches(User *u) = 0;
72 /** Returns true whether or not the given string is covered by this line.
73 * @param str The string to match against. The details of what must be
74 * in this string and the mechanics of the match are defined by the
76 * @return True if there is a match
78 virtual bool Matches(const std::string &str) = 0;
80 /** Apply a line against a user. The mechanics of what occurs when
81 * the line is applied are specific to the derived class.
82 * @param u The user to apply against
84 virtual void Apply(User* u);
86 /** Called when the line is unset either via expiry or
87 * via explicit removal.
89 virtual void Unset() { }
91 /** Called when the expiry message is to be displayed for the
92 * line. Usually a line in the form 'expiring Xline blah, set by...'
93 * see the DisplayExpiry methods of GLine, ELine etc.
95 virtual void DisplayExpiry() = 0;
97 /** Returns the displayable form of the pattern for this xline,
98 * e.g. '*@foo' or '*baz*'. This must always return the full pattern
99 * in a form which can be used to construct an entire derived xline,
100 * even if it is stored differently internally (e.g. GLine stores the
101 * ident and host parts seperately but will still return ident@host
102 * for its Displayable() method)
104 virtual const char* Displayable() = 0;
106 /** Called when the xline has just been added.
108 virtual void OnAdd() { }
110 /** The time the line was added.
114 /** The duration of the ban, or 0 if permenant
118 /** Source of the ban. This can be a servername or an oper nickname
122 /** Reason for the ban
126 /** Expiry time. Does not contain useful data if the duration is 0.
130 /** "Q", "K", etc. Set only by derived classes constructor to the
131 * type of line this is.
133 const std::string type;
135 virtual bool IsBurstable();
140 class CoreExport KLine : public XLine
145 * @param s_time The set time
146 * @param d The duration of the xline
147 * @param src The sender of the xline
148 * @param re The reason of the xline
149 * @param ident Ident to match
150 * @param host Host to match
152 KLine(time_t s_time, long d, std::string src, std::string re, std::string ident, std::string host)
153 : XLine(s_time, d, src, re, "K"), identmask(ident), hostmask(host)
155 matchtext = this->identmask;
156 matchtext.append("@").append(this->hostmask);
165 virtual bool Matches(User *u);
167 virtual bool Matches(const std::string &str);
169 virtual void Apply(User* u);
171 virtual void DisplayExpiry();
173 virtual const char* Displayable();
175 virtual bool IsBurstable();
177 /** Ident mask (ident part only)
179 std::string identmask;
180 /** Host mask (host part only)
182 std::string hostmask;
184 std::string matchtext;
189 class CoreExport GLine : public XLine
193 * @param s_time The set time
194 * @param d The duration of the xline
195 * @param src The sender of the xline
196 * @param re The reason of the xline
197 * @param ident Ident to match
198 * @param host Host to match
200 GLine(time_t s_time, long d, std::string src, std::string re, std::string ident, std::string host)
201 : XLine(s_time, d, src, re, "G"), identmask(ident), hostmask(host)
203 matchtext = this->identmask;
204 matchtext.append("@").append(this->hostmask);
213 virtual bool Matches(User *u);
215 virtual bool Matches(const std::string &str);
217 virtual void Apply(User* u);
219 virtual void DisplayExpiry();
221 virtual const char* Displayable();
223 /** Ident mask (ident part only)
225 std::string identmask;
226 /** Host mask (host part only)
228 std::string hostmask;
230 std::string matchtext;
235 class CoreExport ELine : public XLine
238 /** Create an E-Line.
239 * @param s_time The set time
240 * @param d The duration of the xline
241 * @param src The sender of the xline
242 * @param re The reason of the xline
243 * @param ident Ident to match
244 * @param host Host to match
246 ELine(time_t s_time, long d, std::string src, std::string re, std::string ident, std::string host)
247 : XLine(s_time, d, src, re, "E"), identmask(ident), hostmask(host)
249 matchtext = this->identmask;
250 matchtext.append("@").append(this->hostmask);
257 virtual bool Matches(User *u);
259 virtual bool Matches(const std::string &str);
261 virtual void Unset();
263 virtual void DisplayExpiry();
265 virtual void OnAdd();
267 virtual const char* Displayable();
269 /** Ident mask (ident part only)
271 std::string identmask;
272 /** Host mask (host part only)
274 std::string hostmask;
276 std::string matchtext;
281 class CoreExport ZLine : public XLine
285 * @param s_time The set time
286 * @param d The duration of the xline
287 * @param src The sender of the xline
288 * @param re The reason of the xline
289 * @param ip IP to match
291 ZLine(time_t s_time, long d, std::string src, std::string re, std::string ip)
292 : XLine(s_time, d, src, re, "Z"), ipaddr(ip)
302 virtual bool Matches(User *u);
304 virtual bool Matches(const std::string &str);
306 virtual void Apply(User* u);
308 virtual void DisplayExpiry();
310 virtual const char* Displayable();
312 /** IP mask (no ident part)
319 class CoreExport QLine : public XLine
323 * @param s_time The set time
324 * @param d The duration of the xline
325 * @param src The sender of the xline
326 * @param re The reason of the xline
327 * @param nickname Nickname to match
329 QLine(time_t s_time, long d, std::string src, std::string re, std::string nickname)
330 : XLine(s_time, d, src, re, "Q"), nick(nickname)
339 virtual bool Matches(User *u);
341 virtual bool Matches(const std::string &str);
343 virtual void Apply(User* u);
345 virtual void DisplayExpiry();
347 virtual const char* Displayable();
354 /** XLineFactory is used to generate an XLine pointer, given just the
355 * pattern, timing information and type of line to create. This is used
356 * for example in the spanningtree module which will call an XLineFactory
357 * to create a new XLine when it is inbound on a server link, so that it
358 * does not have to know the specifics of the internals of an XLine class
359 * and/or how to call its constructor.
361 class CoreExport XLineFactory
369 /** Create an XLine factory
370 * @param Instance creator
371 * @param t Type of XLine this factory generates
373 XLineFactory(const std::string &t) : type(t) { }
375 /** Return the type of XLine this factory generates
376 * @return The type of XLine this factory generates
378 virtual const std::string& GetType() { return type; }
380 /** Generate a specialized XLine*.
381 * @param set_time Time this line was created
382 * @param duration Duration of the line
383 * @param source The sender of the line, nickname or server
384 * @param reason The reason for the line
385 * @param xline_specific_mask The mask string for the line, specific to the XLine type being created.
386 * @return A specialized XLine class of the given type for this factory.
388 virtual XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask) = 0;
390 virtual bool AutoApplyToUserList(XLine* x) { return true; }
394 virtual ~XLineFactory() { }
397 /** XLineManager is a class used to manage glines, klines, elines, zlines and qlines,
398 * or any other line created by a module. It also manages XLineFactory classes which
399 * can generate a specialized XLine for use by another module.
401 class CoreExport XLineManager
404 /** Used to hold XLines which have not yet been applied.
406 std::vector<XLine *> pending_lines;
408 /** Current xline factories
410 XLineFactMap line_factory;
412 /** Container of all lines, this is a map of maps which
413 * allows for fast lookup for add/remove of a line, and
414 * the shortest possible timed O(n) for checking a user
417 XLineContainer lookup_lines;
422 * @param Instance A pointer to the creator object
430 /** Split an ident and host into two seperate strings.
431 * This allows for faster matching.
433 IdentHostPair IdentSplit(const std::string &ident_and_host);
435 /** Checks what users match e:lines and sets their ban exempt flag accordingly.
439 /** Get all lines of a certain type to an XLineLookup (std::map<std::string, XLine*>).
440 * NOTE: When this function runs any expired items are removed from the list before it
441 * is returned to the caller.
442 * @param The type to look up
443 * @return A list of all XLines of the given type.
445 XLineLookup* GetAll(const std::string &type);
447 /** Remove all lines of a certain type.
449 void DelAll(const std::string &type);
451 /** Return all known types of line currently stored by the XLineManager.
452 * @return A vector containing all known line types currently stored in the main list.
454 std::vector<std::string> GetAllTypes();
457 * @param line The line to be added
458 * @param user The user adding the line or NULL for the local server
459 * @return True if the line was added successfully
461 bool AddLine(XLine* line, User* user);
464 * @param hostmask The xline-specific string identifying the line, e.g. "*@foo"
465 * @param type The type of xline
466 * @param user The user removing the line or NULL if its the local server
467 * @param simulate If this is true, don't actually remove the line, just return
468 * @return True if the line was deleted successfully
470 bool DelLine(const char* hostmask, const std::string &type, User* user, bool simulate = false);
472 /** Registers an xline factory.
473 * An xline factory is a class which when given a particular xline type,
474 * will generate a new XLine specialized to that type. For example if you
475 * pass the XLineFactory that handles glines some data it will return a
476 * pointer to a GLine, polymorphically represented as XLine. This is used where
477 * you do not know the full details of the item you wish to create, e.g. in a
478 * server protocol module like m_spanningtree, when you receive xlines from other
480 * @param xlf XLineFactory pointer to register
482 bool RegisterFactory(XLineFactory* xlf);
484 /** Unregisters an xline factory.
485 * You must do this when your module unloads.
486 * @param xlf XLineFactory pointer to unregister
488 bool UnregisterFactory(XLineFactory* xlf);
490 /** Get the XLineFactory for a specific type.
491 * Returns NULL if there is no known handler for this xline type.
492 * @param type The type of XLine you require the XLineFactory for
494 XLineFactory* GetFactory(const std::string &type);
496 /** Check if a user matches an XLine
497 * @param type The type of line to look up
498 * @param user The user to match against (what is checked is specific to the xline type)
499 * @return The reason for the line if there is a match, or NULL if there is no match
501 XLine* MatchesLine(const std::string &type, User* user);
503 /** Check if a pattern matches an XLine
504 * @param type The type of line to look up
505 * @param pattern A pattern string specific to the xline type
506 * @return The matching XLine if there is a match, or NULL if there is no match
508 XLine* MatchesLine(const std::string &type, const std::string &pattern);
510 /** Expire a line given two iterators which identify it in the main map.
511 * @param container Iterator to the first level of entries the map
512 * @param item Iterator to the second level of entries in the map
514 void ExpireLine(ContainerIter container, LookupIter item);
516 /** Apply any new lines that are pending to be applied.
517 * This will only apply lines in the pending_lines list, to save on
522 /** Handle /STATS for a given type.
523 * NOTE: Any items in the list for this particular line type which have expired
524 * will be expired and removed before the list is displayed.
525 * @param numeric The numeric to give to each result line
526 * @param user The username making the query
527 * @param results The string_list to receive the results
529 void InvokeStats(const std::string &type, int numeric, User* user, string_list &results);