1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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 * ---------------------------------------------------
14 /* $Core: libIRCDxline */
22 * This is now version 3 of the XLine subsystem, let's see if we can get it as nice and
23 * efficient as we can this time so we can close this file and never ever touch it again ..
26 * Version 1 stored all line types in one list (one for g, one for z, etc). This was fine,
27 * but both version 1 and 2 suck at applying lines efficiently. That is, every time a new line
28 * was added, it iterated every existing line for every existing user. Ow. Expiry was also
29 * expensive, as the lists were NOT sorted.
31 * Version 2 moved permanent lines into a seperate list from non-permanent to help optimize
32 * matching speed, but matched in the same way.
33 * Expiry was also sped up by sorting the list by expiry (meaning just remove the items at the
34 * head of the list that are outdated.)
36 * This was fine and good, but it looked less than ideal in code, and matching was still slower
37 * than it could have been, something which we address here.
40 * All lines are (as in v1) stored together -- no seperation of perm and non-perm. They are stored in
41 * a map of maps (first map is line type, second map is for quick lookup on add/delete/etc).
43 * Expiry is *no longer* performed on a timer, and no longer uses a sorted list of any variety. This
44 * is now done by only checking for expiry when a line is accessed, meaning that expiry is no longer
45 * a resource intensive problem.
47 * Application no longer tries to apply every single line on every single user - instead, now only lines
48 * added since the previous application are applied. This keeps S2S ADDLINE during burst nice and fast,
49 * while at the same time not slowing things the fuck down when we try adding a ban with lots of preexisting
53 bool XLine::Matches(User *u)
59 * Checks what users match a given vector of ELines and sets their ban exempt flag accordingly.
61 void XLineManager::CheckELines()
63 ContainerIter n = lookup_lines.find("E");
65 if (n == lookup_lines.end())
68 XLineLookup& ELines = n->second;
73 for (std::vector<User*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
75 User* u = (User*)(*u2);
77 /* This uses safe iteration to ensure that if a line expires here, it doenst trash the iterator */
80 for (LookupIter i = ELines.begin(); i != ELines.end(); )
86 u->exempt = e->Matches(u);
94 XLineLookup* XLineManager::GetAll(const std::string &type)
96 ContainerIter n = lookup_lines.find(type);
98 if (n == lookup_lines.end())
102 const time_t current = ServerInstance->Time();
104 /* Expire any dead ones, before sending */
105 for (LookupIter x = n->second.begin(); x != n->second.end(); )
109 if (x->second->duration && current > x->second->expiry)
119 std::vector<std::string> XLineManager::GetAllTypes()
121 std::vector<std::string> items;
122 for (ContainerIter x = lookup_lines.begin(); x != lookup_lines.end(); ++x)
123 items.push_back(x->first);
127 IdentHostPair XLineManager::IdentSplit(const std::string &ident_and_host)
129 IdentHostPair n = std::make_pair<std::string,std::string>("*","*");
130 std::string::size_type x = ident_and_host.find('@');
131 if (x != std::string::npos)
133 n.second = ident_and_host.substr(x + 1,ident_and_host.length());
134 n.first = ident_and_host.substr(0, x);
135 if (!n.first.length())
137 if (!n.second.length())
138 n.second.assign("*");
143 n.second = ident_and_host;
151 bool XLineManager::AddLine(XLine* line, User* user)
153 /*IdentHostPair ih = IdentSplit(hostmask);*/
155 ServerInstance->BanCache->RemoveEntries(line->type, false); // XXX perhaps remove ELines here?
157 if (DelLine(line->Displayable(), line->type, user, true))
160 /*ELine* item = new ELine(ServerInstance, ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());*/
161 pending_lines.push_back(line);
162 lookup_lines[line->type][line->Displayable()] = line;
165 FOREACH_MOD(I_OnAddLine,OnAddLine(user, line));
170 // deletes a line, returns true if the line existed and was removed
172 bool XLineManager::DelLine(const char* hostmask, const std::string &type, User* user, bool simulate)
174 ContainerIter x = lookup_lines.find(type);
176 if (x == lookup_lines.end())
179 LookupIter y = x->second.find(hostmask);
181 if (y == x->second.end())
187 ServerInstance->BanCache->RemoveEntries(y->second->type, true);
189 FOREACH_MOD(I_OnDelLine,OnDelLine(user, y->second));
193 std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), y->second);
194 if (pptr != pending_lines.end())
195 pending_lines.erase(pptr);
206 /* remove exempt from everyone and force recheck after deleting eline */
207 for (std::vector<User*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
209 User* u = (User*)(*u2);
213 ServerInstance->XLines->CheckELines();
216 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
218 XLine* XLineManager::MatchesLine(const std::string &type, User* user)
220 ContainerIter x = lookup_lines.find(type);
222 if (x == lookup_lines.end())
225 const time_t current = ServerInstance->Time();
229 for (LookupIter i = x->second.begin(); i != x->second.end(); )
234 if (i->second->Matches(user))
236 if (i->second->duration && current > i->second->expiry)
238 /* Expire the line, return nothing */
240 /* Continue, there may be another that matches
255 XLine* XLineManager::MatchesLine(const std::string &type, const std::string &pattern)
257 ContainerIter x = lookup_lines.find(type);
259 if (x == lookup_lines.end())
262 const time_t current = ServerInstance->Time();
266 for (LookupIter i = x->second.begin(); i != x->second.end(); )
271 if (i->second->Matches(pattern))
273 if (i->second->duration && current > i->second->expiry)
275 /* Expire the line, return nothing */
290 // removes lines that have expired
291 void XLineManager::ExpireLine(ContainerIter container, LookupIter item)
293 FOREACH_MOD(I_OnExpireLine, OnExpireLine(item->second));
295 item->second->DisplayExpiry();
296 item->second->Unset();
298 /* TODO: Can we skip this loop by having a 'pending' field in the XLine class, which is set when a line
299 * is pending, cleared when it is no longer pending, so we skip over this loop if its not pending?
302 std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), item->second);
303 if (pptr != pending_lines.end())
304 pending_lines.erase(pptr);
307 container->second.erase(item);
311 // applies lines, removing clients and changing nicks etc as applicable
312 void XLineManager::ApplyLines()
314 for (std::vector<User*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
316 User* u = (User*)(*u2);
318 for (std::vector<XLine *>::iterator i = pending_lines.begin(); i != pending_lines.end(); i++)
326 pending_lines.clear();
329 void XLineManager::InvokeStats(const std::string &type, int numeric, User* user, string_list &results)
331 std::string sn = ServerInstance->Config->ServerName;
333 ContainerIter n = lookup_lines.find(type);
335 time_t current = ServerInstance->Time();
339 if (n != lookup_lines.end())
341 XLineLookup& list = n->second;
342 for (LookupIter i = list.begin(); i != list.end(); )
347 if (i->second->duration && current > i->second->expiry)
352 results.push_back(sn+" "+ConvToStr(numeric)+" "+user->nick+" :"+i->second->Displayable()+" "+
353 ConvToStr(i->second->set_time)+" "+ConvToStr(i->second->duration)+" "+std::string(i->second->source)+" :"+(i->second->reason));
360 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance)
362 GFact = new GLineFactory(Instance);
363 EFact = new ELineFactory(Instance);
364 KFact = new KLineFactory(Instance);
365 QFact = new QLineFactory(Instance);
366 ZFact = new ZLineFactory(Instance);
368 RegisterFactory(GFact);
369 RegisterFactory(EFact);
370 RegisterFactory(KFact);
371 RegisterFactory(QFact);
372 RegisterFactory(ZFact);
375 XLineManager::~XLineManager()
377 UnregisterFactory(GFact);
378 UnregisterFactory(EFact);
379 UnregisterFactory(KFact);
380 UnregisterFactory(QFact);
381 UnregisterFactory(ZFact);
390 void XLine::Apply(User* u)
394 bool XLine::IsBurstable()
399 void XLine::DefaultApply(User* u, const std::string &line, bool bancache)
401 char sreason[MAXBUF];
402 snprintf(sreason, MAXBUF, "%s-Lined: %s", line.c_str(), this->reason);
403 if (*ServerInstance->Config->MoronBanner)
404 u->WriteServ("NOTICE %s :*** %s", u->nick, ServerInstance->Config->MoronBanner);
405 if (ServerInstance->Config->HideBans)
406 User::QuitUser(ServerInstance, u, line + "-Lined", sreason);
408 User::QuitUser(ServerInstance, u, sreason);
413 ServerInstance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Adding positive hit (") + line + ") for " + u->GetIPString());
414 if (this->duration > 0)
415 ServerInstance->BanCache->AddHit(u->GetIPString(), this->type, line + "-Lined: " + this->reason, this->duration);
417 ServerInstance->BanCache->AddHit(u->GetIPString(), this->type, line + "-Lined: " + this->reason);
421 bool KLine::Matches(User *u)
426 if ((match(u->ident, this->identmask)))
428 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
437 void KLine::Apply(User* u)
439 DefaultApply(u, "K", (strcmp(this->identmask, "*") == 0) ? true : false);
442 bool GLine::Matches(User *u)
447 if ((match(u->ident, this->identmask)))
449 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
458 void GLine::Apply(User* u)
460 DefaultApply(u, "G", (strcmp(this->identmask, "*") == 0) ? true : false);
463 bool ELine::Matches(User *u)
468 if ((match(u->ident, this->identmask)))
470 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
479 bool ZLine::Matches(User *u)
484 if (match(u->GetIPString(), this->ipaddr, true))
490 void ZLine::Apply(User* u)
492 DefaultApply(u, "Z", true);
496 bool QLine::Matches(User *u)
501 if (match(u->nick, this->nick))
507 void QLine::Apply(User* u)
509 /* Force to uuid on apply of qline, no need to disconnect any more :) */
510 u->ForceNickChange(u->uuid);
514 bool ZLine::Matches(const std::string &str)
516 if (match(str.c_str(), this->ipaddr, true))
522 bool QLine::Matches(const std::string &str)
524 if (match(str.c_str(), this->nick))
530 bool ELine::Matches(const std::string &str)
532 return ((match(str.c_str(), matchtext.c_str(), true)));
535 bool KLine::Matches(const std::string &str)
537 return ((match(str.c_str(), matchtext.c_str(), true)));
540 bool GLine::Matches(const std::string &str)
542 return ((match(str.c_str(), matchtext.c_str(), true)));
547 /* When adding one eline, only check the one eline */
548 for (std::vector<User*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
550 User* u = (User*)(*u2);
551 if (this->Matches(u))
556 void ELine::DisplayExpiry()
558 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed E-Line %s@%s (set by %s %ld seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
561 void QLine::DisplayExpiry()
563 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Q-Line %s (set by %s %ld seconds ago)",this->nick,this->source,this->duration);
566 void ZLine::DisplayExpiry()
568 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Z-Line %s (set by %s %ld seconds ago)",this->ipaddr,this->source,this->duration);
571 void KLine::DisplayExpiry()
573 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed K-Line %s@%s (set by %s %ld seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
576 void GLine::DisplayExpiry()
578 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s@%s (set by %s %ld seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
581 const char* ELine::Displayable()
583 return matchtext.c_str();
586 const char* KLine::Displayable()
588 return matchtext.c_str();
591 const char* GLine::Displayable()
593 return matchtext.c_str();
596 const char* ZLine::Displayable()
601 const char* QLine::Displayable()
606 bool KLine::IsBurstable()
611 bool XLineManager::RegisterFactory(XLineFactory* xlf)
613 XLineFactMap::iterator n = line_factory.find(xlf->GetType());
615 if (n != line_factory.end())
618 line_factory[xlf->GetType()] = xlf;
623 bool XLineManager::UnregisterFactory(XLineFactory* xlf)
625 XLineFactMap::iterator n = line_factory.find(xlf->GetType());
627 if (n == line_factory.end())
630 line_factory.erase(n);
635 XLineFactory* XLineManager::GetFactory(const std::string &type)
637 XLineFactMap::iterator n = line_factory.find(type);
639 if (n == line_factory.end())