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 /** Returns true whether or not the given user is covered by this line.
67 virtual bool Matches(User *u) = 0;
69 /** Returns true wether or not the given string exactly matches the gline
70 * (no wildcard use in this method) -- used for removal of a line
72 virtual bool MatchesLiteral(const std::string &str) = 0;
74 virtual bool Matches(const std::string &str) = 0;
76 virtual void Apply(User* u);
78 virtual void Unset() { }
80 virtual void DisplayExpiry() = 0;
82 virtual const char* Displayable() = 0;
84 virtual void OnAdd() { }
86 /** The time the line was added.
90 /** The duration of the ban, or 0 if permenant
94 /** Source of the ban. This can be a servername or an oper nickname
98 /** Reason for the ban
106 /** Q, K, etc. Don't change this. Constructors set it.
113 class CoreExport KLine : public XLine
117 * @param s_time The set time
118 * @param d The duration of the xline
119 * @param src The sender of the xline
120 * @param re The reason of the xline
121 * @param ident Ident to match
122 * @param host Host to match
124 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')
126 identmask = strdup(ident);
127 hostmask = strdup(host);
128 matchtext = this->identmask;
129 matchtext.append("@").append(this->hostmask);
140 virtual bool Matches(User *u);
142 virtual bool Matches(const std::string &str);
144 virtual bool MatchesLiteral(const std::string &str);
146 virtual void Apply(User* u);
148 virtual void DisplayExpiry();
150 virtual const char* Displayable();
159 std::string matchtext;
164 class CoreExport GLine : public XLine
168 * @param s_time The set time
169 * @param d The duration of the xline
170 * @param src The sender of the xline
171 * @param re The reason of the xline
172 * @param ident Ident to match
173 * @param host Host to match
175 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')
177 identmask = strdup(ident);
178 hostmask = strdup(host);
179 matchtext = this->identmask;
180 matchtext.append("@").append(this->hostmask);
191 virtual bool Matches(User *u);
193 virtual bool Matches(const std::string &str);
195 virtual bool MatchesLiteral(const std::string &str);
197 virtual void Apply(User* u);
199 virtual void DisplayExpiry();
201 virtual const char* Displayable();
210 std::string matchtext;
215 class CoreExport ELine : public XLine
218 /** Create an E-Line.
219 * @param s_time The set time
220 * @param d The duration of the xline
221 * @param src The sender of the xline
222 * @param re The reason of the xline
223 * @param ident Ident to match
224 * @param host Host to match
226 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')
228 identmask = strdup(ident);
229 hostmask = strdup(host);
230 matchtext = this->identmask;
231 matchtext.append("@").append(this->hostmask);
240 virtual bool Matches(User *u);
242 virtual bool Matches(const std::string &str);
244 virtual bool MatchesLiteral(const std::string &str);
246 virtual void Unset();
248 virtual void DisplayExpiry();
250 virtual void OnAdd();
252 virtual const char* Displayable();
261 std::string matchtext;
266 class CoreExport ZLine : public XLine
270 * @param s_time The set time
271 * @param d The duration of the xline
272 * @param src The sender of the xline
273 * @param re The reason of the xline
274 * @param ip IP to match
276 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')
288 virtual bool Matches(User *u);
290 virtual bool Matches(const std::string &str);
292 virtual bool MatchesLiteral(const std::string &str);
294 virtual void Apply(User* u);
296 virtual void DisplayExpiry();
298 virtual const char* Displayable();
307 class CoreExport QLine : public XLine
311 * @param s_time The set time
312 * @param d The duration of the xline
313 * @param src The sender of the xline
314 * @param re The reason of the xline
315 * @param nickname Nickname to match
317 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')
319 nick = strdup(nickname);
329 virtual bool Matches(User *u);
331 virtual bool Matches(const std::string &str);
333 virtual bool MatchesLiteral(const std::string &str);
335 virtual void Apply(User* u);
337 virtual void DisplayExpiry();
339 virtual const char* Displayable();
346 /** Contains an ident and host split into two strings
348 typedef std::pair<std::string, std::string> IdentHostPair;
351 class CoreExport XLineFactory
355 InspIRCd* ServerInstance;
360 XLineFactory(InspIRCd* Instance, const char t) : ServerInstance(Instance), type(t) { }
362 virtual const char GetType() { return type; }
364 virtual XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask) = 0;
366 virtual ~XLineFactory() { }
369 /* Required forward declarations
380 /** XLineManager is a class used to manage glines, klines, elines, zlines and qlines.
382 class CoreExport XLineManager
385 /** The owner/creator of this class
387 InspIRCd* ServerInstance;
389 /** This functor is used by the std::sort() function to keep all lines in order
391 static bool XSortComparison (const XLine *one, const XLine *two);
393 /** Used to hold XLines which have not yet been applied.
395 std::vector<XLine *> pending_lines;
397 std::vector<XLine *> active_lines;
399 std::map<char, XLineFactory*> line_factory;
407 unsigned int PermLines;
411 std::map<char, std::map<std::string, XLine *> > lookup_lines;
414 * @param Instance A pointer to the creator object
416 XLineManager(InspIRCd* Instance);
420 /** Split an ident and host into two seperate strings.
421 * This allows for faster matching.
423 IdentHostPair IdentSplit(const std::string &ident_and_host);
425 /** Checks what users match a given list of ELines and sets their ban exempt flag accordingly.
426 * @param ELines List of E:Lines to check.
428 void CheckELines(std::map<std::string, XLine *> &ELines);
431 * @param line The line to be added
432 * @param user The user adding the line or NULL for the local server
433 * @return True if the line was added successfully
435 bool AddLine(XLine* line, User* user);
438 * @param hostmask The xline-specific string identifying the line, e.g. "*@foo"
439 * @param type The type of xline
440 * @param user The user removing the line or NULL if its the local server
441 * @param simulate If this is true, don't actually remove the line, just return
442 * @return True if the line was deleted successfully
444 bool DelLine(const char* hostmask, char type, User* user, bool simulate = false);
446 /** Registers an xline factory.
447 * An xline factory is a class which when given a particular xline type,
448 * will generate a new XLine specialized to that type. For example if you
449 * pass the XLineFactory that handles glines some data it will return a
450 * pointer to a GLine, polymorphically represented as XLine. This is used where
451 * you do not know the full details of the item you wish to create, e.g. in a
452 * server protocol module like m_spanningtree, when you receive xlines from other
455 bool RegisterFactory(XLineFactory* xlf);
457 /** Unregisters an xline factory
459 bool UnregisterFactory(XLineFactory* xlf);
461 /** Get the XLineFactory for a specific type.
462 * Returns NULL if there is no known handler for this xline type
464 XLineFactory* GetFactory(const char type);
466 /** Check if a nickname matches a QLine
467 * @return nick The nick to check against
468 * @return The reason for the line if there is a match, or NULL if there is no match
470 QLine* matches_qline(const char* nick);
472 /** Check if a hostname matches a GLine
473 * @param user The user to check against
474 * @return The reason for the line if there is a match, or NULL if there is no match
476 GLine* matches_gline(User* user);
478 /** Check if a user's IP matches a ZLine
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 ZLine* matches_zline(User *user);
484 /** Check if a hostname matches a KLine
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 KLine* matches_kline(User* user);
490 /** Check if a hostname matches a ELine
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 ELine* matches_exception(User* user);
496 /** Expire any lines that should be expired.
500 /** Apply any new lines that are pending to be applied
505 * @param user The username making the query
506 * @param results The string_list to receive the results
508 void stats_k(User* user, string_list &results);
511 * @param user The username making the query
512 * @param results The string_list to receive the results
514 void stats_g(User* user, string_list &results);
517 * @param user The username making the query
518 * @param results The string_list to receive the results
520 void stats_q(User* user, string_list &results);
523 * @param user The username making the query
524 * @param results The string_list to receive the results
526 void stats_z(User* user, string_list &results);
529 * @param user The username making the query
530 * @param results The string_list to receive the results
532 void stats_e(User* user, string_list &results);
534 /** Change creation time of a GLine
535 * @param host The hostname to change
536 * @param create_Time The new creation time
538 void gline_set_creation_time(const char* host, time_t create_time);
540 /** Change creation time of a QLine
541 * @param nick The nickmask to change
542 * @param create_Time The new creation time
544 void qline_set_creation_time(const char* nick, time_t create_time);
546 /** Change creation time of a ZLine
547 * @param ip The ipmask to change
548 * @param create_Time The new creation time
550 void zline_set_creation_time(const char* ip, time_t create_time);
552 /** Change creation time of a ELine
553 * @param host The hostname to change
554 * @param create_Time The new creation time
556 void eline_set_creation_time(const char* host, time_t create_time);
559 class CoreExport GLineFactory : public XLineFactory
562 GLineFactory(InspIRCd* Instance) : XLineFactory(Instance, 'G') { }
564 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
566 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
567 return new GLine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
571 class CoreExport ELineFactory : public XLineFactory
574 ELineFactory(InspIRCd* Instance) : XLineFactory(Instance, 'E') { }
576 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
578 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
579 return new ELine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
583 class CoreExport KLineFactory : public XLineFactory
586 KLineFactory(InspIRCd* Instance) : XLineFactory(Instance, 'K') { }
588 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
590 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
591 return new KLine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
595 class CoreExport QLineFactory : public XLineFactory
598 QLineFactory(InspIRCd* Instance) : XLineFactory(Instance, 'Q') { }
600 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
602 return new QLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
606 class CoreExport ZLineFactory : public XLineFactory
609 ZLineFactory(InspIRCd* Instance) : XLineFactory(Instance, 'Z') { }
611 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
613 return new ZLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);