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 * ---------------------------------------------------
17 // include the common header files
25 const int APPLY_GLINES = 1;
26 const int APPLY_KLINES = 2;
27 const int APPLY_QLINES = 4;
28 const int APPLY_ZLINES = 8;
29 const int APPLY_PERM_ONLY = 16;
30 const int APPLY_ALL = APPLY_GLINES | APPLY_KLINES | APPLY_QLINES | APPLY_ZLINES;
32 /** XLine is the base class for ban lines such as G lines and K lines.
34 class CoreExport XLine : public classbase
38 InspIRCd* ServerInstance;
39 void DefaultApply(User* u, char 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
49 XLine(InspIRCd* Instance, time_t s_time, long d, const char* src, const char* re, const char t)
50 : ServerInstance(Instance), set_time(s_time), duration(d), type(t)
54 expiry = set_time + duration;
65 virtual void SetCreateTime(time_t created)
68 expiry = created + duration;
71 /** Returns true whether or not the given user is covered by this line.
73 virtual bool Matches(User *u) = 0;
75 /** Returns true wether or not the given string exactly matches the gline
76 * (no wildcard use in this method) -- used for removal of a line
78 virtual bool MatchesLiteral(const std::string &str) = 0;
80 virtual bool Matches(const std::string &str) = 0;
82 virtual void Apply(User* u);
84 virtual void Unset() { }
86 virtual void DisplayExpiry() = 0;
88 virtual const char* Displayable() = 0;
90 virtual void OnAdd() { }
92 /** The time the line was added.
96 /** The duration of the ban, or 0 if permenant
100 /** Source of the ban. This can be a servername or an oper nickname
104 /** Reason for the ban
112 /** Q, K, etc. Don't change this. Constructors set it.
119 class CoreExport KLine : public XLine
123 * @param s_time The set time
124 * @param d The duration of the xline
125 * @param src The sender of the xline
126 * @param re The reason of the xline
127 * @param ident Ident to match
128 * @param host Host to match
130 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')
132 identmask = strdup(ident);
133 hostmask = strdup(host);
134 matchtext = this->identmask;
135 matchtext.append("@").append(this->hostmask);
146 virtual bool Matches(User *u);
148 virtual bool Matches(const std::string &str);
150 virtual bool MatchesLiteral(const std::string &str);
152 virtual void Apply(User* u);
154 virtual void DisplayExpiry();
156 virtual const char* Displayable();
165 std::string matchtext;
170 class CoreExport GLine : public XLine
174 * @param s_time The set time
175 * @param d The duration of the xline
176 * @param src The sender of the xline
177 * @param re The reason of the xline
178 * @param ident Ident to match
179 * @param host Host to match
181 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')
183 identmask = strdup(ident);
184 hostmask = strdup(host);
185 matchtext = this->identmask;
186 matchtext.append("@").append(this->hostmask);
197 virtual bool Matches(User *u);
199 virtual bool Matches(const std::string &str);
201 virtual bool MatchesLiteral(const std::string &str);
203 virtual void Apply(User* u);
205 virtual void DisplayExpiry();
207 virtual const char* Displayable();
216 std::string matchtext;
221 class CoreExport ELine : public XLine
224 /** Create an E-Line.
225 * @param s_time The set time
226 * @param d The duration of the xline
227 * @param src The sender of the xline
228 * @param re The reason of the xline
229 * @param ident Ident to match
230 * @param host Host to match
232 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')
234 identmask = strdup(ident);
235 hostmask = strdup(host);
236 matchtext = this->identmask;
237 matchtext.append("@").append(this->hostmask);
246 virtual bool Matches(User *u);
248 virtual bool Matches(const std::string &str);
250 virtual bool MatchesLiteral(const std::string &str);
252 virtual void Unset();
254 virtual void DisplayExpiry();
256 virtual void OnAdd();
258 virtual const char* Displayable();
267 std::string matchtext;
272 class CoreExport ZLine : public XLine
276 * @param s_time The set time
277 * @param d The duration of the xline
278 * @param src The sender of the xline
279 * @param re The reason of the xline
280 * @param ip IP to match
282 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')
294 virtual bool Matches(User *u);
296 virtual bool Matches(const std::string &str);
298 virtual bool MatchesLiteral(const std::string &str);
300 virtual void Apply(User* u);
302 virtual void DisplayExpiry();
304 virtual const char* Displayable();
313 class CoreExport QLine : public XLine
317 * @param s_time The set time
318 * @param d The duration of the xline
319 * @param src The sender of the xline
320 * @param re The reason of the xline
321 * @param nickname Nickname to match
323 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')
325 nick = strdup(nickname);
335 virtual bool Matches(User *u);
337 virtual bool Matches(const std::string &str);
339 virtual bool MatchesLiteral(const std::string &str);
341 virtual void Apply(User* u);
343 virtual void DisplayExpiry();
345 virtual const char* Displayable();
352 /** Contains an ident and host split into two strings
354 typedef std::pair<std::string, std::string> IdentHostPair;
357 class CoreExport XLineFactory
361 InspIRCd* ServerInstance;
366 XLineFactory(InspIRCd* Instance, const char t) : ServerInstance(Instance), type(t) { }
368 virtual const char GetType() { return type; }
370 virtual XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask) = 0;
372 virtual ~XLineFactory() { }
375 /* Required forward declarations
386 /** XLineManager is a class used to manage glines, klines, elines, zlines and qlines.
388 class CoreExport XLineManager
391 /** The owner/creator of this class
393 InspIRCd* ServerInstance;
395 /** This functor is used by the std::sort() function to keep all lines in order
397 static bool XSortComparison (const XLine *one, const XLine *two);
399 /** Used to hold XLines which have not yet been applied.
401 std::vector<XLine *> pending_lines;
403 std::vector<XLine *> active_lines;
405 std::map<char, XLineFactory*> line_factory;
413 unsigned int PermLines;
417 std::map<char, std::map<std::string, XLine *> > lookup_lines;
420 * @param Instance A pointer to the creator object
422 XLineManager(InspIRCd* Instance);
426 /** Split an ident and host into two seperate strings.
427 * This allows for faster matching.
429 IdentHostPair IdentSplit(const std::string &ident_and_host);
431 /** Checks what users match a given list of ELines and sets their ban exempt flag accordingly.
432 * @param ELines List of E:Lines to check.
434 void CheckELines(std::map<std::string, XLine *> &ELines);
437 * @param line The line to be added
438 * @param user The user adding the line or NULL for the local server
439 * @return True if the line was added successfully
441 bool AddLine(XLine* line, User* user);
444 * @param hostmask The xline-specific string identifying the line, e.g. "*@foo"
445 * @param type The type of xline
446 * @param user The user removing the line or NULL if its the local server
447 * @param simulate If this is true, don't actually remove the line, just return
448 * @return True if the line was deleted successfully
450 bool DelLine(const char* hostmask, char type, User* user, bool simulate = false);
452 /** Registers an xline factory.
453 * An xline factory is a class which when given a particular xline type,
454 * will generate a new XLine specialized to that type. For example if you
455 * pass the XLineFactory that handles glines some data it will return a
456 * pointer to a GLine, polymorphically represented as XLine. This is used where
457 * you do not know the full details of the item you wish to create, e.g. in a
458 * server protocol module like m_spanningtree, when you receive xlines from other
461 bool RegisterFactory(XLineFactory* xlf);
463 /** Unregisters an xline factory
465 bool UnregisterFactory(XLineFactory* xlf);
467 /** Get the XLineFactory for a specific type.
468 * Returns NULL if there is no known handler for this xline type
470 XLineFactory* GetFactory(const char type);
472 /** Check if a nickname matches a QLine
473 * @return nick The nick to check against
474 * @return The reason for the line if there is a match, or NULL if there is no match
476 QLine* matches_qline(const char* nick);
478 /** Check if a hostname matches a GLine
479 * @param user The user to check against
480 * @return The reason for the line if there is a match, or NULL if there is no match
482 GLine* matches_gline(User* user);
484 /** Check if a user's IP matches a ZLine
485 * @param user The user to check against
486 * @return The reason for the line if there is a match, or NULL if there is no match
488 ZLine* matches_zline(User *user);
490 /** Check if a hostname matches a KLine
491 * @param user The user to check against
492 * @return The reason for the line if there is a match, or NULL if there is no match
494 KLine* matches_kline(User* user);
496 /** Check if a hostname matches a ELine
497 * @param user The user to check against
498 * @return The reason for the line if there is a match, or NULL if there is no match
500 ELine* matches_exception(User* user);
502 /** Expire any lines that should be expired.
506 /** Apply any new lines that are pending to be applied
510 /** Handle /STATS for a given type.
511 * @param numeric The numeric to give to each result line
512 * @param user The username making the query
513 * @param results The string_list to receive the results
515 void InvokeStats(const char type, int numeric, User* user, string_list &results);
518 * @param user The username making the query
519 * @param results The string_list to receive the results
521 void stats_e(User* user, string_list &results);
523 /** Change creation time of a GLine
524 * @param host The hostname to change
525 * @param create_Time The new creation time
527 void gline_set_creation_time(const char* host, time_t create_time);
529 /** Change creation time of a QLine
530 * @param nick The nickmask to change
531 * @param create_Time The new creation time
533 void qline_set_creation_time(const char* nick, time_t create_time);
535 /** Change creation time of a ZLine
536 * @param ip The ipmask to change
537 * @param create_Time The new creation time
539 void zline_set_creation_time(const char* ip, time_t create_time);
541 /** Change creation time of a ELine
542 * @param host The hostname to change
543 * @param create_Time The new creation time
545 void eline_set_creation_time(const char* host, time_t create_time);
548 class CoreExport GLineFactory : public XLineFactory
551 GLineFactory(InspIRCd* Instance) : XLineFactory(Instance, 'G') { }
553 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
555 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
556 return new GLine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
560 class CoreExport ELineFactory : public XLineFactory
563 ELineFactory(InspIRCd* Instance) : XLineFactory(Instance, 'E') { }
565 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
567 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
568 return new ELine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
572 class CoreExport KLineFactory : public XLineFactory
575 KLineFactory(InspIRCd* Instance) : XLineFactory(Instance, 'K') { }
577 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
579 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
580 return new KLine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
584 class CoreExport QLineFactory : public XLineFactory
587 QLineFactory(InspIRCd* Instance) : XLineFactory(Instance, 'Q') { }
589 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
591 return new QLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
595 class CoreExport ZLineFactory : public XLineFactory
598 ZLineFactory(InspIRCd* Instance) : XLineFactory(Instance, 'Z') { }
600 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
602 return new ZLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);