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 = ServerInstance->XLines->lookup_lines.find("E");
64 if (n == ServerInstance->XLines->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 IdentHostPair XLineManager::IdentSplit(const std::string &ident_and_host)
87 IdentHostPair n = std::make_pair<std::string,std::string>("*","*");
88 std::string::size_type x = ident_and_host.find('@');
89 if (x != std::string::npos)
91 n.second = ident_and_host.substr(x + 1,ident_and_host.length());
92 n.first = ident_and_host.substr(0, x);
93 if (!n.first.length())
95 if (!n.second.length())
100 n.second = ident_and_host;
108 bool XLineManager::AddLine(XLine* line, User* user)
110 /*IdentHostPair ih = IdentSplit(hostmask);*/
112 if (DelLine(line->Displayable(), line->type, user, true))
115 /*ELine* item = new ELine(ServerInstance, ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());*/
116 pending_lines.push_back(line);
117 lookup_lines[line->type][line->Displayable()] = line;
120 FOREACH_MOD(I_OnAddLine,OnAddLine(user, line));
125 // deletes a line, returns true if the line existed and was removed
127 bool XLineManager::DelLine(const char* hostmask, const std::string &type, User* user, bool simulate)
129 ContainerIter x = lookup_lines.find(type);
131 if (x == lookup_lines.end())
134 LookupIter y = x->second.find(hostmask);
136 if (y == x->second.end())
142 FOREACH_MOD(I_OnDelLine,OnDelLine(user, y->second));
146 std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), y->second);
147 if (pptr != pending_lines.end())
148 pending_lines.erase(pptr);
159 /* remove exempt from everyone and force recheck after deleting eline */
160 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
162 User* u = (User*)(*u2);
166 ContainerIter x = ServerInstance->XLines->lookup_lines.find("E");
167 if (x != ServerInstance->XLines->lookup_lines.end())
168 ServerInstance->XLines->CheckELines();
171 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
173 XLine* XLineManager::MatchesLine(const std::string &type, User* user)
175 ContainerIter x = lookup_lines.find(type);
177 if (x == lookup_lines.end())
180 const time_t current = ServerInstance->Time();
182 for (LookupIter i = x->second.begin(); i != x->second.end(); i++)
184 if (i->second->Matches(user))
186 if (current > i->second->expiry)
188 /* Expire the line, return nothing */
199 XLine* XLineManager::MatchesLine(const std::string &type, const std::string &pattern)
201 ContainerIter x = lookup_lines.find(type);
203 if (x == lookup_lines.end())
206 const time_t current = ServerInstance->Time();
208 for (LookupIter i = x->second.begin(); i != x->second.end(); i++)
210 if (i->second->Matches(pattern))
212 if (current > i->second->expiry)
214 /* Expire the line, return nothing */
225 // removes lines that have expired
226 void XLineManager::ExpireLine(ContainerIter container, LookupIter item)
228 item->second->DisplayExpiry();
229 item->second->Unset();
231 /* TODO: Can we skip this loop by having a 'pending' field in the XLine class, which is set when a line
232 * is pending, cleared when it is no longer pending, so we skip over this loop if its not pending?
235 std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), item->second);
236 if (pptr != pending_lines.end())
237 pending_lines.erase(pptr);
240 container->second.erase(item);
244 // applies lines, removing clients and changing nicks etc as applicable
245 void XLineManager::ApplyLines()
247 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
249 User* u = (User*)(*u2);
251 for (std::vector<XLine *>::iterator i = pending_lines.begin(); i != pending_lines.end(); i++)
259 pending_lines.clear();
269 void XLineManager::InvokeStats(const std::string &type, int numeric, User* user, string_list &results)
271 std::string sn = ServerInstance->Config->ServerName;
273 ContainerIter n = lookup_lines.find(type);
275 if (n != lookup_lines.end())
277 XLineLookup& list = n->second;
278 for (LookupIter i = list.begin(); i != list.end(); i++)
279 results.push_back(sn+" "+ConvToStr(numeric)+" "+user->nick+" :"+i->second->Displayable()+" "+
280 ConvToStr(i->second->set_time)+" "+ConvToStr(i->second->duration)+" "+std::string(i->second->source)+" :"+(i->second->reason));
285 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance)
287 GFact = new GLineFactory(Instance);
288 EFact = new ELineFactory(Instance);
289 KFact = new KLineFactory(Instance);
290 QFact = new QLineFactory(Instance);
291 ZFact = new ZLineFactory(Instance);
293 RegisterFactory(GFact);
294 RegisterFactory(EFact);
295 RegisterFactory(KFact);
296 RegisterFactory(QFact);
297 RegisterFactory(ZFact);
300 XLineManager::~XLineManager()
302 UnregisterFactory(GFact);
303 UnregisterFactory(EFact);
304 UnregisterFactory(KFact);
305 UnregisterFactory(QFact);
306 UnregisterFactory(ZFact);
315 void XLine::Apply(User* u)
319 void XLine::DefaultApply(User* u, const std::string &line)
322 snprintf(reason, MAXBUF, "%s-Lined: %s", line.c_str(), this->reason);
323 if (*ServerInstance->Config->MoronBanner)
324 u->WriteServ("NOTICE %s :*** %s", u->nick, ServerInstance->Config->MoronBanner);
325 if (ServerInstance->Config->HideBans)
326 User::QuitUser(ServerInstance, u, line + "-Lined", reason);
328 User::QuitUser(ServerInstance, u, reason);
331 bool KLine::Matches(User *u)
336 if ((match(u->ident, this->identmask)))
338 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
347 void KLine::Apply(User* u)
349 DefaultApply(u, "K");
352 bool GLine::Matches(User *u)
357 if ((match(u->ident, this->identmask)))
359 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
368 void GLine::Apply(User* u)
370 DefaultApply(u, "G");
373 bool ELine::Matches(User *u)
378 if ((match(u->ident, this->identmask)))
380 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
389 bool ZLine::Matches(User *u)
394 if (match(u->GetIPString(), this->ipaddr, true))
400 void ZLine::Apply(User* u)
402 DefaultApply(u, "Z");
406 bool QLine::Matches(User *u)
411 if (match(u->nick, this->nick))
417 void QLine::Apply(User* u)
419 /* Can we force the user to their uid here instead? */
420 DefaultApply(u, "Q");
424 bool ZLine::Matches(const std::string &str)
426 if (match(str.c_str(), this->ipaddr, true))
432 bool QLine::Matches(const std::string &str)
434 if (match(str.c_str(), this->nick))
440 bool ELine::Matches(const std::string &str)
442 return ((match(str.c_str(), matchtext.c_str(), true)));
445 bool KLine::Matches(const std::string &str)
447 return ((match(str.c_str(), matchtext.c_str(), true)));
450 bool GLine::Matches(const std::string &str)
452 return ((match(str.c_str(), matchtext.c_str(), true)));
457 /* When adding one eline, only check the one eline */
458 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
460 User* u = (User*)(*u2);
461 if (this->Matches(u))
466 void ELine::DisplayExpiry()
468 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed E-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
471 void QLine::DisplayExpiry()
473 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Q-Line %s (set by %s %d seconds ago)",this->nick,this->source,this->duration);
476 void ZLine::DisplayExpiry()
478 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Z-Line %s (set by %s %d seconds ago)",this->ipaddr,this->source,this->duration);
481 void KLine::DisplayExpiry()
483 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed K-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
486 void GLine::DisplayExpiry()
488 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
491 const char* ELine::Displayable()
493 return matchtext.c_str();
496 const char* KLine::Displayable()
498 return matchtext.c_str();
501 const char* GLine::Displayable()
503 return matchtext.c_str();
506 const char* ZLine::Displayable()
511 const char* QLine::Displayable()
516 bool XLineManager::RegisterFactory(XLineFactory* xlf)
518 XLineFactMap::iterator n = line_factory.find(xlf->GetType());
520 if (n != line_factory.end())
523 line_factory[xlf->GetType()] = xlf;
528 bool XLineManager::UnregisterFactory(XLineFactory* xlf)
530 XLineFactMap::iterator n = line_factory.find(xlf->GetType());
532 if (n == line_factory.end())
535 line_factory.erase(n);
540 XLineFactory* XLineManager::GetFactory(const std::string &type)
542 XLineFactMap::iterator n = line_factory.find(type);
544 if (n != line_factory.end())