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 for (LookupIter i = ELines.begin(); i != ELines.end(); i++)
79 u->exempt = e->Matches(u);
85 XLineLookup* XLineManager::GetAll(const std::string &type)
87 ContainerIter n = lookup_lines.find(type);
89 if (n == lookup_lines.end())
95 std::vector<std::string> XLineManager::GetAllTypes()
97 std::vector<std::string> items;
98 for (ContainerIter x = lookup_lines.begin(); x != lookup_lines.end(); ++x)
99 items.push_back(x->first);
103 IdentHostPair XLineManager::IdentSplit(const std::string &ident_and_host)
105 IdentHostPair n = std::make_pair<std::string,std::string>("*","*");
106 std::string::size_type x = ident_and_host.find('@');
107 if (x != std::string::npos)
109 n.second = ident_and_host.substr(x + 1,ident_and_host.length());
110 n.first = ident_and_host.substr(0, x);
111 if (!n.first.length())
113 if (!n.second.length())
114 n.second.assign("*");
118 n.second = ident_and_host;
126 bool XLineManager::AddLine(XLine* line, User* user)
128 /*IdentHostPair ih = IdentSplit(hostmask);*/
130 if (DelLine(line->Displayable(), line->type, user, true))
133 /*ELine* item = new ELine(ServerInstance, ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());*/
134 pending_lines.push_back(line);
135 lookup_lines[line->type][line->Displayable()] = line;
138 FOREACH_MOD(I_OnAddLine,OnAddLine(user, line));
143 // deletes a line, returns true if the line existed and was removed
145 bool XLineManager::DelLine(const char* hostmask, const std::string &type, User* user, bool simulate)
147 ContainerIter x = lookup_lines.find(type);
149 if (x == lookup_lines.end())
152 LookupIter y = x->second.find(hostmask);
154 if (y == x->second.end())
160 FOREACH_MOD(I_OnDelLine,OnDelLine(user, y->second));
164 std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), y->second);
165 if (pptr != pending_lines.end())
166 pending_lines.erase(pptr);
177 /* remove exempt from everyone and force recheck after deleting eline */
178 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
180 User* u = (User*)(*u2);
184 ServerInstance->XLines->CheckELines();
187 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
189 XLine* XLineManager::MatchesLine(const std::string &type, User* user)
191 ContainerIter x = lookup_lines.find(type);
193 if (x == lookup_lines.end())
196 const time_t current = ServerInstance->Time();
198 for (LookupIter i = x->second.begin(); i != x->second.end(); i++)
200 if (i->second->Matches(user))
202 if (current > i->second->expiry)
204 /* Expire the line, return nothing */
215 XLine* XLineManager::MatchesLine(const std::string &type, const std::string &pattern)
217 ContainerIter x = lookup_lines.find(type);
219 if (x == lookup_lines.end())
222 const time_t current = ServerInstance->Time();
224 for (LookupIter i = x->second.begin(); i != x->second.end(); i++)
226 if (i->second->Matches(pattern))
228 if (current > i->second->expiry)
230 /* Expire the line, return nothing */
241 // removes lines that have expired
242 void XLineManager::ExpireLine(ContainerIter container, LookupIter item)
244 item->second->DisplayExpiry();
245 item->second->Unset();
247 /* TODO: Can we skip this loop by having a 'pending' field in the XLine class, which is set when a line
248 * is pending, cleared when it is no longer pending, so we skip over this loop if its not pending?
251 std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), item->second);
252 if (pptr != pending_lines.end())
253 pending_lines.erase(pptr);
256 container->second.erase(item);
260 // applies lines, removing clients and changing nicks etc as applicable
261 void XLineManager::ApplyLines()
263 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
265 User* u = (User*)(*u2);
267 for (std::vector<XLine *>::iterator i = pending_lines.begin(); i != pending_lines.end(); i++)
275 pending_lines.clear();
285 void XLineManager::InvokeStats(const std::string &type, int numeric, User* user, string_list &results)
287 std::string sn = ServerInstance->Config->ServerName;
289 ContainerIter n = lookup_lines.find(type);
291 if (n != lookup_lines.end())
293 XLineLookup& list = n->second;
294 for (LookupIter i = list.begin(); i != list.end(); i++)
295 results.push_back(sn+" "+ConvToStr(numeric)+" "+user->nick+" :"+i->second->Displayable()+" "+
296 ConvToStr(i->second->set_time)+" "+ConvToStr(i->second->duration)+" "+std::string(i->second->source)+" :"+(i->second->reason));
301 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance)
303 GFact = new GLineFactory(Instance);
304 EFact = new ELineFactory(Instance);
305 KFact = new KLineFactory(Instance);
306 QFact = new QLineFactory(Instance);
307 ZFact = new ZLineFactory(Instance);
309 RegisterFactory(GFact);
310 RegisterFactory(EFact);
311 RegisterFactory(KFact);
312 RegisterFactory(QFact);
313 RegisterFactory(ZFact);
316 XLineManager::~XLineManager()
318 UnregisterFactory(GFact);
319 UnregisterFactory(EFact);
320 UnregisterFactory(KFact);
321 UnregisterFactory(QFact);
322 UnregisterFactory(ZFact);
331 void XLine::Apply(User* u)
335 void XLine::DefaultApply(User* u, const std::string &line)
338 snprintf(reason, MAXBUF, "%s-Lined: %s", line.c_str(), this->reason);
339 if (*ServerInstance->Config->MoronBanner)
340 u->WriteServ("NOTICE %s :*** %s", u->nick, ServerInstance->Config->MoronBanner);
341 if (ServerInstance->Config->HideBans)
342 User::QuitUser(ServerInstance, u, line + "-Lined", reason);
344 User::QuitUser(ServerInstance, u, reason);
347 bool KLine::Matches(User *u)
352 if ((match(u->ident, this->identmask)))
354 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
363 void KLine::Apply(User* u)
365 DefaultApply(u, "K");
368 bool GLine::Matches(User *u)
373 if ((match(u->ident, this->identmask)))
375 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
384 void GLine::Apply(User* u)
386 DefaultApply(u, "G");
389 bool ELine::Matches(User *u)
394 if ((match(u->ident, this->identmask)))
396 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
405 bool ZLine::Matches(User *u)
410 if (match(u->GetIPString(), this->ipaddr, true))
416 void ZLine::Apply(User* u)
418 DefaultApply(u, "Z");
422 bool QLine::Matches(User *u)
427 if (match(u->nick, this->nick))
433 void QLine::Apply(User* u)
435 /* Can we force the user to their uid here instead? */
436 DefaultApply(u, "Q");
440 bool ZLine::Matches(const std::string &str)
442 if (match(str.c_str(), this->ipaddr, true))
448 bool QLine::Matches(const std::string &str)
450 if (match(str.c_str(), this->nick))
456 bool ELine::Matches(const std::string &str)
458 return ((match(str.c_str(), matchtext.c_str(), true)));
461 bool KLine::Matches(const std::string &str)
463 return ((match(str.c_str(), matchtext.c_str(), true)));
466 bool GLine::Matches(const std::string &str)
468 return ((match(str.c_str(), matchtext.c_str(), true)));
473 /* When adding one eline, only check the one eline */
474 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
476 User* u = (User*)(*u2);
477 if (this->Matches(u))
482 void ELine::DisplayExpiry()
484 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed E-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
487 void QLine::DisplayExpiry()
489 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Q-Line %s (set by %s %d seconds ago)",this->nick,this->source,this->duration);
492 void ZLine::DisplayExpiry()
494 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Z-Line %s (set by %s %d seconds ago)",this->ipaddr,this->source,this->duration);
497 void KLine::DisplayExpiry()
499 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed K-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
502 void GLine::DisplayExpiry()
504 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
507 const char* ELine::Displayable()
509 return matchtext.c_str();
512 const char* KLine::Displayable()
514 return matchtext.c_str();
517 const char* GLine::Displayable()
519 return matchtext.c_str();
522 const char* ZLine::Displayable()
527 const char* QLine::Displayable()
532 bool XLineManager::RegisterFactory(XLineFactory* xlf)
534 XLineFactMap::iterator n = line_factory.find(xlf->GetType());
536 if (n != line_factory.end())
539 line_factory[xlf->GetType()] = xlf;
544 bool XLineManager::UnregisterFactory(XLineFactory* xlf)
546 XLineFactMap::iterator n = line_factory.find(xlf->GetType());
548 if (n == line_factory.end())
551 line_factory.erase(n);
556 XLineFactory* XLineManager::GetFactory(const std::string &type)
558 XLineFactMap::iterator n = line_factory.find(type);
560 if (n != line_factory.end())