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, const std::string &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 std::string &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 virtual bool Matches(const std::string &str) = 0;
77 virtual void Apply(User* u);
79 virtual void Unset() { }
81 virtual void DisplayExpiry() = 0;
83 virtual const char* Displayable() = 0;
85 virtual void OnAdd() { }
87 /** The time the line was added.
91 /** The duration of the ban, or 0 if permenant
95 /** Source of the ban. This can be a servername or an oper nickname
99 /** Reason for the ban
107 /** Q, K, etc. Don't change this. Constructors set it.
109 const std::string type;
114 class CoreExport KLine : public XLine
118 * @param s_time The set time
119 * @param d The duration of the xline
120 * @param src The sender of the xline
121 * @param re The reason of the xline
122 * @param ident Ident to match
123 * @param host Host to match
125 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")
127 identmask = strdup(ident);
128 hostmask = strdup(host);
129 matchtext = this->identmask;
130 matchtext.append("@").append(this->hostmask);
141 virtual bool Matches(User *u);
143 virtual bool Matches(const std::string &str);
145 virtual void Apply(User* u);
147 virtual void DisplayExpiry();
149 virtual const char* Displayable();
158 std::string matchtext;
163 class CoreExport GLine : public XLine
167 * @param s_time The set time
168 * @param d The duration of the xline
169 * @param src The sender of the xline
170 * @param re The reason of the xline
171 * @param ident Ident to match
172 * @param host Host to match
174 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")
176 identmask = strdup(ident);
177 hostmask = strdup(host);
178 matchtext = this->identmask;
179 matchtext.append("@").append(this->hostmask);
190 virtual bool Matches(User *u);
192 virtual bool Matches(const std::string &str);
194 virtual void Apply(User* u);
196 virtual void DisplayExpiry();
198 virtual const char* Displayable();
207 std::string matchtext;
212 class CoreExport ELine : public XLine
215 /** Create an E-Line.
216 * @param s_time The set time
217 * @param d The duration of the xline
218 * @param src The sender of the xline
219 * @param re The reason of the xline
220 * @param ident Ident to match
221 * @param host Host to match
223 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")
225 identmask = strdup(ident);
226 hostmask = strdup(host);
227 matchtext = this->identmask;
228 matchtext.append("@").append(this->hostmask);
237 virtual bool Matches(User *u);
239 virtual bool Matches(const std::string &str);
241 virtual void Unset();
243 virtual void DisplayExpiry();
245 virtual void OnAdd();
247 virtual const char* Displayable();
256 std::string matchtext;
261 class CoreExport ZLine : public XLine
265 * @param s_time The set time
266 * @param d The duration of the xline
267 * @param src The sender of the xline
268 * @param re The reason of the xline
269 * @param ip IP to match
271 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")
283 virtual bool Matches(User *u);
285 virtual bool Matches(const std::string &str);
287 virtual void Apply(User* u);
289 virtual void DisplayExpiry();
291 virtual const char* Displayable();
300 class CoreExport QLine : public XLine
304 * @param s_time The set time
305 * @param d The duration of the xline
306 * @param src The sender of the xline
307 * @param re The reason of the xline
308 * @param nickname Nickname to match
310 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")
312 nick = strdup(nickname);
322 virtual bool Matches(User *u);
324 virtual bool Matches(const std::string &str);
326 virtual void Apply(User* u);
328 virtual void DisplayExpiry();
330 virtual const char* Displayable();
337 /** Contains an ident and host split into two strings
339 typedef std::pair<std::string, std::string> IdentHostPair;
342 class CoreExport XLineFactory
346 InspIRCd* ServerInstance;
351 XLineFactory(InspIRCd* Instance, const std::string &t) : ServerInstance(Instance), type(t) { }
353 virtual const std::string& GetType() { return type; }
355 virtual XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask) = 0;
357 virtual ~XLineFactory() { }
360 /* Required forward declarations
371 typedef std::map<std::string, XLineFactory*> XLineFactMap;
372 typedef std::map<std::string, XLine *> XLineLookup;
373 typedef std::map<std::string, XLineLookup > XLineContainer;
374 typedef XLineContainer::iterator ContainerIter;
375 typedef XLineLookup::iterator LookupIter;
377 /** XLineManager is a class used to manage glines, klines, elines, zlines and qlines.
379 class CoreExport XLineManager
382 /** The owner/creator of this class
384 InspIRCd* ServerInstance;
386 /** Used to hold XLines which have not yet been applied.
388 std::vector<XLine *> pending_lines;
390 XLineFactMap line_factory;
398 XLineContainer lookup_lines;
403 * @param Instance A pointer to the creator object
405 XLineManager(InspIRCd* Instance);
409 /** Split an ident and host into two seperate strings.
410 * This allows for faster matching.
412 IdentHostPair IdentSplit(const std::string &ident_and_host);
414 /** Checks what users match e:lines and sets their ban exempt flag accordingly.
419 * @param line The line to be added
420 * @param user The user adding the line or NULL for the local server
421 * @return True if the line was added successfully
423 bool AddLine(XLine* line, User* user);
426 * @param hostmask The xline-specific string identifying the line, e.g. "*@foo"
427 * @param type The type of xline
428 * @param user The user removing the line or NULL if its the local server
429 * @param simulate If this is true, don't actually remove the line, just return
430 * @return True if the line was deleted successfully
432 bool DelLine(const char* hostmask, const std::string &type, User* user, bool simulate = false);
434 /** Registers an xline factory.
435 * An xline factory is a class which when given a particular xline type,
436 * will generate a new XLine specialized to that type. For example if you
437 * pass the XLineFactory that handles glines some data it will return a
438 * pointer to a GLine, polymorphically represented as XLine. This is used where
439 * you do not know the full details of the item you wish to create, e.g. in a
440 * server protocol module like m_spanningtree, when you receive xlines from other
443 bool RegisterFactory(XLineFactory* xlf);
445 /** Unregisters an xline factory
447 bool UnregisterFactory(XLineFactory* xlf);
449 /** Get the XLineFactory for a specific type.
450 * Returns NULL if there is no known handler for this xline type
452 XLineFactory* GetFactory(const std::string &type);
454 /** Check if a user matches an XLine
455 * @param type The type of line to look up
456 * @param user The user to match against (what is checked is specific to the xline type)
457 * @return The reason for the line if there is a match, or NULL if there is no match
459 XLine* MatchesLine(const std::string &type, User* user);
461 /** Check if a pattern matches an XLine
462 * @param type The type of line to look up
463 * @param pattern A pattern string specific to the xline type
464 * @return The matching XLine if there is a match, or NULL if there is no match
466 XLine* MatchesLine(const std::string &type, const std::string &pattern);
468 /** Expire a line given two iterators which identify it
470 void ExpireLine(ContainerIter container, LookupIter item);
472 /** Apply any new lines that are pending to be applied
476 /** Handle /STATS for a given type.
477 * @param numeric The numeric to give to each result line
478 * @param user The username making the query
479 * @param results The string_list to receive the results
481 void InvokeStats(const std::string &type, int numeric, User* user, string_list &results);
484 class CoreExport GLineFactory : public XLineFactory
487 GLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "G") { }
489 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
491 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
492 return new GLine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
496 class CoreExport ELineFactory : public XLineFactory
499 ELineFactory(InspIRCd* Instance) : XLineFactory(Instance, "E") { }
501 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
503 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
504 return new ELine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
508 class CoreExport KLineFactory : public XLineFactory
511 KLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "K") { }
513 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
515 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
516 return new KLine(ServerInstance, set_time, duration, source, reason, ih.first.c_str(), ih.second.c_str());
520 class CoreExport QLineFactory : public XLineFactory
523 QLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "Q") { }
525 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
527 return new QLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
531 class CoreExport ZLineFactory : public XLineFactory
534 ZLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "Z") { }
536 XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
538 return new ZLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);