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 * ---------------------------------------------------
14 /* $Core: libIRCDxline */
21 * This is now version 3 of the XLine subsystem, let's see if we can get it as nice and
22 * efficient as we can this time so we can close this file and never ever touch it again ..
25 * Version 1 stored all line types in one list (one for g, one for z, etc). This was fine,
26 * but both version 1 and 2 suck at applying lines efficiently. That is, every time a new line
27 * was added, it iterated every existing line for every existing user. Ow. Expiry was also
28 * expensive, as the lists were NOT sorted.
30 * Version 2 moved permanent lines into a seperate list from non-permanent to help optimize
31 * matching speed, but matched in the same way.
32 * Expiry was also sped up by sorting the list by expiry (meaning just remove the items at the
33 * head of the list that are outdated.)
35 * This was fine and good, but it looked less than ideal in code, and matching was still slower
36 * than it could have been, something which we address here.
39 * All lines are (as in v1) stored together -- no seperation of perm and non-perm. They are stored in
40 * a map of maps (first map is line type, second map is for quick lookup on add/delete/etc).
42 * Expiry is *no longer* performed on a timer, and no longer uses a sorted list of any variety. This
43 * is now done by only checking for expiry when a line is accessed, meaning that expiry is no longer
44 * a resource intensive problem.
46 * Application no longer tries to apply every single line on every single user - instead, now only lines
47 * added since the previous application are applied. This keeps S2S ADDLINE during burst nice and fast,
48 * while at the same time not slowing things the fuck down when we try adding a ban with lots of preexisting
52 bool XLine::Matches(User *u)
58 * Checks what users match a given vector of ELines and sets their ban exempt flag accordingly.
60 void XLineManager::CheckELines()
62 ContainerIter n = lookup_lines.find("E");
64 if (n == lookup_lines.end())
67 XLineLookup& ELines = n->second;
72 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
74 User* u = (User*)(*u2);
76 /* This uses safe iteration to ensure that if a line expires here, it doenst trash the iterator */
79 for (LookupIter i = ELines.begin(); i != ELines.end(); )
85 u->exempt = e->Matches(u);
93 XLineLookup* XLineManager::GetAll(const std::string &type)
95 ContainerIter n = lookup_lines.find(type);
97 if (n == lookup_lines.end())
101 const time_t current = ServerInstance->Time();
103 /* Expire any dead ones, before sending */
104 for (LookupIter x = n->second.begin(); x != n->second.end(); )
108 if (x->second->duration && current > x->second->expiry)
118 std::vector<std::string> XLineManager::GetAllTypes()
120 std::vector<std::string> items;
121 for (ContainerIter x = lookup_lines.begin(); x != lookup_lines.end(); ++x)
122 items.push_back(x->first);
126 IdentHostPair XLineManager::IdentSplit(const std::string &ident_and_host)
128 IdentHostPair n = std::make_pair<std::string,std::string>("*","*");
129 std::string::size_type x = ident_and_host.find('@');
130 if (x != std::string::npos)
132 n.second = ident_and_host.substr(x + 1,ident_and_host.length());
133 n.first = ident_and_host.substr(0, x);
134 if (!n.first.length())
136 if (!n.second.length())
137 n.second.assign("*");
141 n.second = ident_and_host;
149 bool XLineManager::AddLine(XLine* line, User* user)
151 /*IdentHostPair ih = IdentSplit(hostmask);*/
153 if (DelLine(line->Displayable(), line->type, user, true))
156 /*ELine* item = new ELine(ServerInstance, ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());*/
157 pending_lines.push_back(line);
158 lookup_lines[line->type][line->Displayable()] = line;
161 FOREACH_MOD(I_OnAddLine,OnAddLine(user, line));
166 // deletes a line, returns true if the line existed and was removed
168 bool XLineManager::DelLine(const char* hostmask, const std::string &type, User* user, bool simulate)
170 ContainerIter x = lookup_lines.find(type);
172 if (x == lookup_lines.end())
175 LookupIter y = x->second.find(hostmask);
177 if (y == x->second.end())
183 FOREACH_MOD(I_OnDelLine,OnDelLine(user, y->second));
187 std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), y->second);
188 if (pptr != pending_lines.end())
189 pending_lines.erase(pptr);
200 /* remove exempt from everyone and force recheck after deleting eline */
201 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
203 User* u = (User*)(*u2);
207 ServerInstance->XLines->CheckELines();
210 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
212 XLine* XLineManager::MatchesLine(const std::string &type, User* user)
214 ContainerIter x = lookup_lines.find(type);
216 if (x == lookup_lines.end())
219 const time_t current = ServerInstance->Time();
223 for (LookupIter i = x->second.begin(); i != x->second.end(); )
228 if (i->second->Matches(user))
230 if (i->second->duration && current > i->second->expiry)
232 /* Expire the line, return nothing */
234 /* Continue, there may be another that matches
249 XLine* XLineManager::MatchesLine(const std::string &type, const std::string &pattern)
251 ContainerIter x = lookup_lines.find(type);
253 if (x == lookup_lines.end())
256 const time_t current = ServerInstance->Time();
260 for (LookupIter i = x->second.begin(); i != x->second.end(); )
265 if (i->second->Matches(pattern))
267 if (i->second->duration && current > i->second->expiry)
269 /* Expire the line, return nothing */
284 // removes lines that have expired
285 void XLineManager::ExpireLine(ContainerIter container, LookupIter item)
287 item->second->DisplayExpiry();
288 item->second->Unset();
290 /* TODO: Can we skip this loop by having a 'pending' field in the XLine class, which is set when a line
291 * is pending, cleared when it is no longer pending, so we skip over this loop if its not pending?
294 std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), item->second);
295 if (pptr != pending_lines.end())
296 pending_lines.erase(pptr);
299 container->second.erase(item);
303 // applies lines, removing clients and changing nicks etc as applicable
304 void XLineManager::ApplyLines()
306 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
308 User* u = (User*)(*u2);
310 for (std::vector<XLine *>::iterator i = pending_lines.begin(); i != pending_lines.end(); i++)
318 pending_lines.clear();
321 void XLineManager::InvokeStats(const std::string &type, int numeric, User* user, string_list &results)
323 std::string sn = ServerInstance->Config->ServerName;
325 ContainerIter n = lookup_lines.find(type);
327 time_t current = ServerInstance->Time();
331 if (n != lookup_lines.end())
333 XLineLookup& list = n->second;
334 for (LookupIter i = list.begin(); i != list.end(); )
339 if (i->second->duration && current > i->second->expiry)
344 results.push_back(sn+" "+ConvToStr(numeric)+" "+user->nick+" :"+i->second->Displayable()+" "+
345 ConvToStr(i->second->set_time)+" "+ConvToStr(i->second->duration)+" "+std::string(i->second->source)+" :"+(i->second->reason));
352 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance)
354 GFact = new GLineFactory(Instance);
355 EFact = new ELineFactory(Instance);
356 KFact = new KLineFactory(Instance);
357 QFact = new QLineFactory(Instance);
358 ZFact = new ZLineFactory(Instance);
360 RegisterFactory(GFact);
361 RegisterFactory(EFact);
362 RegisterFactory(KFact);
363 RegisterFactory(QFact);
364 RegisterFactory(ZFact);
367 XLineManager::~XLineManager()
369 UnregisterFactory(GFact);
370 UnregisterFactory(EFact);
371 UnregisterFactory(KFact);
372 UnregisterFactory(QFact);
373 UnregisterFactory(ZFact);
382 void XLine::Apply(User* u)
386 void XLine::DefaultApply(User* u, const std::string &line)
389 snprintf(reason, MAXBUF, "%s-Lined: %s", line.c_str(), this->reason);
390 if (*ServerInstance->Config->MoronBanner)
391 u->WriteServ("NOTICE %s :*** %s", u->nick, ServerInstance->Config->MoronBanner);
392 if (ServerInstance->Config->HideBans)
393 User::QuitUser(ServerInstance, u, line + "-Lined", reason);
395 User::QuitUser(ServerInstance, u, reason);
398 bool KLine::Matches(User *u)
403 if ((match(u->ident, this->identmask)))
405 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
414 void KLine::Apply(User* u)
416 DefaultApply(u, "K");
419 bool GLine::Matches(User *u)
424 if ((match(u->ident, this->identmask)))
426 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
435 void GLine::Apply(User* u)
437 DefaultApply(u, "G");
440 bool ELine::Matches(User *u)
445 if ((match(u->ident, this->identmask)))
447 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
456 bool ZLine::Matches(User *u)
461 if (match(u->GetIPString(), this->ipaddr, true))
467 void ZLine::Apply(User* u)
469 DefaultApply(u, "Z");
473 bool QLine::Matches(User *u)
478 if (match(u->nick, this->nick))
484 void QLine::Apply(User* u)
486 /* Force to uuid on apply of qline, no need to disconnect any more :) */
487 u->ForceNickChange(u->uuid);
491 bool ZLine::Matches(const std::string &str)
493 if (match(str.c_str(), this->ipaddr, true))
499 bool QLine::Matches(const std::string &str)
501 if (match(str.c_str(), this->nick))
507 bool ELine::Matches(const std::string &str)
509 return ((match(str.c_str(), matchtext.c_str(), true)));
512 bool KLine::Matches(const std::string &str)
514 return ((match(str.c_str(), matchtext.c_str(), true)));
517 bool GLine::Matches(const std::string &str)
519 return ((match(str.c_str(), matchtext.c_str(), true)));
524 /* When adding one eline, only check the one eline */
525 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
527 User* u = (User*)(*u2);
528 if (this->Matches(u))
533 void ELine::DisplayExpiry()
535 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed E-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
538 void QLine::DisplayExpiry()
540 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Q-Line %s (set by %s %d seconds ago)",this->nick,this->source,this->duration);
543 void ZLine::DisplayExpiry()
545 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Z-Line %s (set by %s %d seconds ago)",this->ipaddr,this->source,this->duration);
548 void KLine::DisplayExpiry()
550 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed K-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
553 void GLine::DisplayExpiry()
555 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
558 const char* ELine::Displayable()
560 return matchtext.c_str();
563 const char* KLine::Displayable()
565 return matchtext.c_str();
568 const char* GLine::Displayable()
570 return matchtext.c_str();
573 const char* ZLine::Displayable()
578 const char* QLine::Displayable()
583 bool XLineManager::RegisterFactory(XLineFactory* xlf)
585 XLineFactMap::iterator n = line_factory.find(xlf->GetType());
587 if (n != line_factory.end())
590 line_factory[xlf->GetType()] = xlf;
595 bool XLineManager::UnregisterFactory(XLineFactory* xlf)
597 XLineFactMap::iterator n = line_factory.find(xlf->GetType());
599 if (n == line_factory.end())
602 line_factory.erase(n);
607 XLineFactory* XLineManager::GetFactory(const std::string &type)
609 XLineFactMap::iterator n = line_factory.find(type);
611 if (n != line_factory.end())