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 * ---------------------------------------------------
18 /** An XLineFactory specialized to generate GLine* pointers
20 class GLineFactory : public XLineFactory
23 GLineFactory() : XLineFactory("G") { }
27 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
29 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
30 return new GLine(set_time, duration, source, reason, ih.first, ih.second);
34 /** An XLineFactory specialized to generate ELine* pointers
36 class ELineFactory : public XLineFactory
39 ELineFactory() : XLineFactory("E") { }
43 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
45 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
46 return new ELine(set_time, duration, source, reason, ih.first, ih.second);
50 /** An XLineFactory specialized to generate KLine* pointers
52 class KLineFactory : public XLineFactory
55 KLineFactory() : XLineFactory("K") { }
59 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
61 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
62 return new KLine(set_time, duration, source, reason, ih.first, ih.second);
66 /** An XLineFactory specialized to generate QLine* pointers
68 class QLineFactory : public XLineFactory
71 QLineFactory() : XLineFactory("Q") { }
75 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
77 return new QLine(set_time, duration, source, reason, xline_specific_mask);
81 /** An XLineFactory specialized to generate ZLine* pointers
83 class ZLineFactory : public XLineFactory
86 ZLineFactory() : XLineFactory("Z") { }
90 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
92 return new ZLine(set_time, duration, source, reason, xline_specific_mask);
98 * This is now version 3 of the XLine subsystem, let's see if we can get it as nice and
99 * efficient as we can this time so we can close this file and never ever touch it again ..
102 * Version 1 stored all line types in one list (one for g, one for z, etc). This was fine,
103 * but both version 1 and 2 suck at applying lines efficiently. That is, every time a new line
104 * was added, it iterated every existing line for every existing user. Ow. Expiry was also
105 * expensive, as the lists were NOT sorted.
107 * Version 2 moved permanent lines into a seperate list from non-permanent to help optimize
108 * matching speed, but matched in the same way.
109 * Expiry was also sped up by sorting the list by expiry (meaning just remove the items at the
110 * head of the list that are outdated.)
112 * This was fine and good, but it looked less than ideal in code, and matching was still slower
113 * than it could have been, something which we address here.
116 * All lines are (as in v1) stored together -- no seperation of perm and non-perm. They are stored in
117 * a map of maps (first map is line type, second map is for quick lookup on add/delete/etc).
119 * Expiry is *no longer* performed on a timer, and no longer uses a sorted list of any variety. This
120 * is now done by only checking for expiry when a line is accessed, meaning that expiry is no longer
121 * a resource intensive problem.
123 * Application no longer tries to apply every single line on every single user - instead, now only lines
124 * added since the previous application are applied. This keeps S2S ADDLINE during burst nice and fast,
125 * while at the same time not slowing things the fuck down when we try adding a ban with lots of preexisting
129 bool XLine::Matches(User *u)
135 * Checks what users match a given vector of ELines and sets their ban exempt flag accordingly.
137 void XLineManager::CheckELines()
139 ContainerIter n = lookup_lines.find("E");
141 if (n == lookup_lines.end())
144 XLineLookup& ELines = n->second;
149 for (std::vector<LocalUser*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
151 User* u = (User*)(*u2);
153 /* This uses safe iteration to ensure that if a line expires here, it doenst trash the iterator */
156 for (LookupIter i = ELines.begin(); i != ELines.end(); )
161 XLine *e = i->second;
162 u->exempt = e->Matches(u);
170 XLineLookup* XLineManager::GetAll(const std::string &type)
172 ContainerIter n = lookup_lines.find(type);
174 if (n == lookup_lines.end())
178 const time_t current = ServerInstance->Time();
180 /* Expire any dead ones, before sending */
181 for (LookupIter x = n->second.begin(); x != n->second.end(); )
185 if (x->second->duration && current > x->second->expiry)
195 void XLineManager::DelAll(const std::string &type)
197 ContainerIter n = lookup_lines.find(type);
199 if (n == lookup_lines.end())
204 /* Delete all of a given type (this should probably use DelLine, but oh well) */
205 while ((x = n->second.begin()) != n->second.end())
211 std::vector<std::string> XLineManager::GetAllTypes()
213 std::vector<std::string> items;
214 for (ContainerIter x = lookup_lines.begin(); x != lookup_lines.end(); ++x)
215 items.push_back(x->first);
219 IdentHostPair XLineManager::IdentSplit(const std::string &ident_and_host)
221 IdentHostPair n = std::make_pair<std::string,std::string>("*","*");
222 std::string::size_type x = ident_and_host.find('@');
223 if (x != std::string::npos)
225 n.second = ident_and_host.substr(x + 1,ident_and_host.length());
226 n.first = ident_and_host.substr(0, x);
227 if (!n.first.length())
229 if (!n.second.length())
230 n.second.assign("*");
235 n.second = ident_and_host;
243 bool XLineManager::AddLine(XLine* line, User* user)
245 ServerInstance->BanCache->RemoveEntries(line->type, false); // XXX perhaps remove ELines here?
247 if (line->duration && ServerInstance->Time() > line->expiry)
248 return false; // Don't apply expired XLines.
250 /* Don't apply duplicate xlines */
251 ContainerIter x = lookup_lines.find(line->type);
252 if (x != lookup_lines.end())
254 LookupIter i = x->second.find(line->Displayable());
255 if (i != x->second.end())
261 /*ELine* item = new ELine(ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());*/
262 XLineFactory* xlf = GetFactory(line->type);
266 if (xlf->AutoApplyToUserList(line))
267 pending_lines.push_back(line);
269 lookup_lines[line->type][line->Displayable()] = line;
272 FOREACH_MOD(I_OnAddLine,OnAddLine(user, line));
277 // deletes a line, returns true if the line existed and was removed
279 bool XLineManager::DelLine(const char* hostmask, const std::string &type, User* user, bool simulate)
281 ContainerIter x = lookup_lines.find(type);
283 if (x == lookup_lines.end())
286 LookupIter y = x->second.find(hostmask);
288 if (y == x->second.end())
294 ServerInstance->BanCache->RemoveEntries(y->second->type, true);
296 FOREACH_MOD(I_OnDelLine,OnDelLine(user, y->second));
300 std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), y->second);
301 if (pptr != pending_lines.end())
302 pending_lines.erase(pptr);
313 /* remove exempt from everyone and force recheck after deleting eline */
314 for (std::vector<LocalUser*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
316 User* u = (User*)(*u2);
320 ServerInstance->XLines->CheckELines();
323 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
325 XLine* XLineManager::MatchesLine(const std::string &type, User* user)
327 ContainerIter x = lookup_lines.find(type);
329 if (x == lookup_lines.end())
332 const time_t current = ServerInstance->Time();
336 for (LookupIter i = x->second.begin(); i != x->second.end(); )
341 if (i->second->duration && current > i->second->expiry)
343 /* Expire the line, proceed to next one */
349 if (i->second->Matches(user))
359 XLine* XLineManager::MatchesLine(const std::string &type, const std::string &pattern)
361 ContainerIter x = lookup_lines.find(type);
363 if (x == lookup_lines.end())
366 const time_t current = ServerInstance->Time();
370 for (LookupIter i = x->second.begin(); i != x->second.end(); )
375 if (i->second->Matches(pattern))
377 if (i->second->duration && current > i->second->expiry)
379 /* Expire the line, return nothing */
394 // removes lines that have expired
395 void XLineManager::ExpireLine(ContainerIter container, LookupIter item)
397 FOREACH_MOD(I_OnExpireLine, OnExpireLine(item->second));
399 item->second->DisplayExpiry();
400 item->second->Unset();
402 /* TODO: Can we skip this loop by having a 'pending' field in the XLine class, which is set when a line
403 * is pending, cleared when it is no longer pending, so we skip over this loop if its not pending?
406 std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), item->second);
407 if (pptr != pending_lines.end())
408 pending_lines.erase(pptr);
411 container->second.erase(item);
415 // applies lines, removing clients and changing nicks etc as applicable
416 void XLineManager::ApplyLines()
418 std::vector<LocalUser*>::reverse_iterator u2 = ServerInstance->Users->local_users.rbegin();
419 while (u2 != ServerInstance->Users->local_users.rend())
423 // Don't ban people who are exempt.
427 for (std::vector<XLine *>::iterator i = pending_lines.begin(); i != pending_lines.end(); i++)
435 pending_lines.clear();
438 void XLineManager::InvokeStats(const std::string &type, int numeric, User* user, string_list &results)
440 std::string sn = ServerInstance->Config->ServerName;
442 ContainerIter n = lookup_lines.find(type);
444 time_t current = ServerInstance->Time();
448 if (n != lookup_lines.end())
450 XLineLookup& list = n->second;
451 for (LookupIter i = list.begin(); i != list.end(); )
456 if (i->second->duration && current > i->second->expiry)
461 results.push_back(sn+" "+ConvToStr(numeric)+" "+user->nick+" :"+i->second->Displayable()+" "+
462 ConvToStr(i->second->set_time)+" "+ConvToStr(i->second->duration)+" "+std::string(i->second->source)+" :"+(i->second->reason));
469 XLineManager::XLineManager()
478 GFact = new GLineFactory;
479 EFact = new ELineFactory;
480 KFact = new KLineFactory;
481 QFact = new QLineFactory;
482 ZFact = new ZLineFactory;
484 RegisterFactory(GFact);
485 RegisterFactory(EFact);
486 RegisterFactory(KFact);
487 RegisterFactory(QFact);
488 RegisterFactory(ZFact);
491 XLineManager::~XLineManager()
493 const char gekqz[] = "GEKQZ";
494 for(unsigned int i=0; i < sizeof(gekqz); i++)
496 XLineFactory* xlf = GetFactory(std::string(1, gekqz[i]));
498 UnregisterFactory(xlf);
502 // Delete all existing XLines
503 for (XLineContainer::iterator i = lookup_lines.begin(); i != lookup_lines.end(); i++)
505 for (XLineLookup::iterator j = i->second.begin(); j != i->second.end(); j++)
511 lookup_lines.clear();
515 void XLine::Apply(User* u)
519 bool XLine::IsBurstable()
524 void XLine::DefaultApply(User* u, const std::string &line, bool bancache)
526 char sreason[MAXBUF];
527 snprintf(sreason, MAXBUF, "%s-Lined: %s", line.c_str(), this->reason.c_str());
528 if (!ServerInstance->Config->MoronBanner.empty())
529 u->WriteServ("NOTICE %s :*** %s", u->nick.c_str(), ServerInstance->Config->MoronBanner.c_str());
531 if (ServerInstance->Config->HideBans)
532 ServerInstance->Users->QuitUser(u, line + "-Lined", sreason);
534 ServerInstance->Users->QuitUser(u, sreason);
539 ServerInstance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Adding positive hit (") + line + ") for " + u->GetIPString());
540 if (this->duration > 0)
541 ServerInstance->BanCache->AddHit(u->GetIPString(), this->type, line + "-Lined: " + this->reason, this->duration);
543 ServerInstance->BanCache->AddHit(u->GetIPString(), this->type, line + "-Lined: " + this->reason);
547 bool KLine::Matches(User *u)
552 if (InspIRCd::Match(u->ident, this->identmask, ascii_case_insensitive_map))
554 if (InspIRCd::MatchCIDR(u->host, this->hostmask, ascii_case_insensitive_map) ||
555 InspIRCd::MatchCIDR(u->GetIPString(), this->hostmask, ascii_case_insensitive_map))
564 void KLine::Apply(User* u)
566 DefaultApply(u, "K", (this->identmask == "*") ? true : false);
569 bool GLine::Matches(User *u)
574 if (InspIRCd::Match(u->ident, this->identmask, ascii_case_insensitive_map))
576 if (InspIRCd::MatchCIDR(u->host, this->hostmask, ascii_case_insensitive_map) ||
577 InspIRCd::MatchCIDR(u->GetIPString(), this->hostmask, ascii_case_insensitive_map))
586 void GLine::Apply(User* u)
588 DefaultApply(u, "G", (this->identmask == "*") ? true : false);
591 bool ELine::Matches(User *u)
596 if (InspIRCd::Match(u->ident, this->identmask, ascii_case_insensitive_map))
598 if (InspIRCd::MatchCIDR(u->host, this->hostmask, ascii_case_insensitive_map) ||
599 InspIRCd::MatchCIDR(u->GetIPString(), this->hostmask, ascii_case_insensitive_map))
608 bool ZLine::Matches(User *u)
613 if (InspIRCd::MatchCIDR(u->GetIPString(), this->ipaddr))
619 void ZLine::Apply(User* u)
621 DefaultApply(u, "Z", true);
625 bool QLine::Matches(User *u)
627 if (InspIRCd::Match(u->nick, this->nick))
633 void QLine::Apply(User* u)
635 /* Force to uuid on apply of qline, no need to disconnect any more :) */
636 u->ForceNickChange(u->uuid.c_str());
640 bool ZLine::Matches(const std::string &str)
642 if (InspIRCd::MatchCIDR(str, this->ipaddr))
648 bool QLine::Matches(const std::string &str)
650 if (InspIRCd::Match(str, this->nick))
656 bool ELine::Matches(const std::string &str)
658 return (InspIRCd::MatchCIDR(str, matchtext));
661 bool KLine::Matches(const std::string &str)
663 return (InspIRCd::MatchCIDR(str.c_str(), matchtext));
666 bool GLine::Matches(const std::string &str)
668 return (InspIRCd::MatchCIDR(str, matchtext));
673 /* When adding one eline, only check the one eline */
674 for (std::vector<LocalUser*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
676 User* u = (User*)(*u2);
677 if (this->Matches(u))
682 void ELine::DisplayExpiry()
684 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired E-Line %s@%s (set by %s %ld seconds ago)",
685 identmask.c_str(),hostmask.c_str(),source.c_str(),(long)(ServerInstance->Time() - this->set_time));
688 void QLine::DisplayExpiry()
690 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired Q-Line %s (set by %s %ld seconds ago)",
691 nick.c_str(),source.c_str(),(long)(ServerInstance->Time() - this->set_time));
694 void ZLine::DisplayExpiry()
696 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired Z-Line %s (set by %s %ld seconds ago)",
697 ipaddr.c_str(),source.c_str(),(long)(ServerInstance->Time() - this->set_time));
700 void KLine::DisplayExpiry()
702 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired K-Line %s@%s (set by %s %ld seconds ago)",
703 identmask.c_str(),hostmask.c_str(),source.c_str(),(long)(ServerInstance->Time() - this->set_time));
706 void GLine::DisplayExpiry()
708 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired G-Line %s@%s (set by %s %ld seconds ago)",
709 identmask.c_str(),hostmask.c_str(),source.c_str(),(long)(ServerInstance->Time() - this->set_time));
712 const char* ELine::Displayable()
714 return matchtext.c_str();
717 const char* KLine::Displayable()
719 return matchtext.c_str();
722 const char* GLine::Displayable()
724 return matchtext.c_str();
727 const char* ZLine::Displayable()
729 return ipaddr.c_str();
732 const char* QLine::Displayable()
737 bool KLine::IsBurstable()
742 bool XLineManager::RegisterFactory(XLineFactory* xlf)
744 XLineFactMap::iterator n = line_factory.find(xlf->GetType());
746 if (n != line_factory.end())
749 line_factory[xlf->GetType()] = xlf;
754 bool XLineManager::UnregisterFactory(XLineFactory* xlf)
756 XLineFactMap::iterator n = line_factory.find(xlf->GetType());
758 if (n == line_factory.end())
761 line_factory.erase(n);
766 XLineFactory* XLineManager::GetFactory(const std::string &type)
768 XLineFactMap::iterator n = line_factory.find(type);
770 if (n == line_factory.end())