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 ServerInstance->XLines->CheckELines();
169 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
171 XLine* XLineManager::MatchesLine(const std::string &type, User* user)
173 ContainerIter x = lookup_lines.find(type);
175 if (x == lookup_lines.end())
178 const time_t current = ServerInstance->Time();
180 for (LookupIter i = x->second.begin(); i != x->second.end(); i++)
182 if (i->second->Matches(user))
184 if (current > i->second->expiry)
186 /* Expire the line, return nothing */
197 XLine* XLineManager::MatchesLine(const std::string &type, const std::string &pattern)
199 ContainerIter x = lookup_lines.find(type);
201 if (x == lookup_lines.end())
204 const time_t current = ServerInstance->Time();
206 for (LookupIter i = x->second.begin(); i != x->second.end(); i++)
208 if (i->second->Matches(pattern))
210 if (current > i->second->expiry)
212 /* Expire the line, return nothing */
223 // removes lines that have expired
224 void XLineManager::ExpireLine(ContainerIter container, LookupIter item)
226 item->second->DisplayExpiry();
227 item->second->Unset();
229 /* TODO: Can we skip this loop by having a 'pending' field in the XLine class, which is set when a line
230 * is pending, cleared when it is no longer pending, so we skip over this loop if its not pending?
233 std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), item->second);
234 if (pptr != pending_lines.end())
235 pending_lines.erase(pptr);
238 container->second.erase(item);
242 // applies lines, removing clients and changing nicks etc as applicable
243 void XLineManager::ApplyLines()
245 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
247 User* u = (User*)(*u2);
249 for (std::vector<XLine *>::iterator i = pending_lines.begin(); i != pending_lines.end(); i++)
257 pending_lines.clear();
267 void XLineManager::InvokeStats(const std::string &type, int numeric, User* user, string_list &results)
269 std::string sn = ServerInstance->Config->ServerName;
271 ContainerIter n = lookup_lines.find(type);
273 if (n != lookup_lines.end())
275 XLineLookup& list = n->second;
276 for (LookupIter i = list.begin(); i != list.end(); i++)
277 results.push_back(sn+" "+ConvToStr(numeric)+" "+user->nick+" :"+i->second->Displayable()+" "+
278 ConvToStr(i->second->set_time)+" "+ConvToStr(i->second->duration)+" "+std::string(i->second->source)+" :"+(i->second->reason));
283 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance)
285 GFact = new GLineFactory(Instance);
286 EFact = new ELineFactory(Instance);
287 KFact = new KLineFactory(Instance);
288 QFact = new QLineFactory(Instance);
289 ZFact = new ZLineFactory(Instance);
291 RegisterFactory(GFact);
292 RegisterFactory(EFact);
293 RegisterFactory(KFact);
294 RegisterFactory(QFact);
295 RegisterFactory(ZFact);
298 XLineManager::~XLineManager()
300 UnregisterFactory(GFact);
301 UnregisterFactory(EFact);
302 UnregisterFactory(KFact);
303 UnregisterFactory(QFact);
304 UnregisterFactory(ZFact);
313 void XLine::Apply(User* u)
317 void XLine::DefaultApply(User* u, const std::string &line)
320 snprintf(reason, MAXBUF, "%s-Lined: %s", line.c_str(), this->reason);
321 if (*ServerInstance->Config->MoronBanner)
322 u->WriteServ("NOTICE %s :*** %s", u->nick, ServerInstance->Config->MoronBanner);
323 if (ServerInstance->Config->HideBans)
324 User::QuitUser(ServerInstance, u, line + "-Lined", reason);
326 User::QuitUser(ServerInstance, u, reason);
329 bool KLine::Matches(User *u)
334 if ((match(u->ident, this->identmask)))
336 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
345 void KLine::Apply(User* u)
347 DefaultApply(u, "K");
350 bool GLine::Matches(User *u)
355 if ((match(u->ident, this->identmask)))
357 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
366 void GLine::Apply(User* u)
368 DefaultApply(u, "G");
371 bool ELine::Matches(User *u)
376 if ((match(u->ident, this->identmask)))
378 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
387 bool ZLine::Matches(User *u)
392 if (match(u->GetIPString(), this->ipaddr, true))
398 void ZLine::Apply(User* u)
400 DefaultApply(u, "Z");
404 bool QLine::Matches(User *u)
409 if (match(u->nick, this->nick))
415 void QLine::Apply(User* u)
417 /* Can we force the user to their uid here instead? */
418 DefaultApply(u, "Q");
422 bool ZLine::Matches(const std::string &str)
424 if (match(str.c_str(), this->ipaddr, true))
430 bool QLine::Matches(const std::string &str)
432 if (match(str.c_str(), this->nick))
438 bool ELine::Matches(const std::string &str)
440 return ((match(str.c_str(), matchtext.c_str(), true)));
443 bool KLine::Matches(const std::string &str)
445 return ((match(str.c_str(), matchtext.c_str(), true)));
448 bool GLine::Matches(const std::string &str)
450 return ((match(str.c_str(), matchtext.c_str(), true)));
455 /* When adding one eline, only check the one eline */
456 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
458 User* u = (User*)(*u2);
459 if (this->Matches(u))
464 void ELine::DisplayExpiry()
466 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed E-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
469 void QLine::DisplayExpiry()
471 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Q-Line %s (set by %s %d seconds ago)",this->nick,this->source,this->duration);
474 void ZLine::DisplayExpiry()
476 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Z-Line %s (set by %s %d seconds ago)",this->ipaddr,this->source,this->duration);
479 void KLine::DisplayExpiry()
481 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed K-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
484 void GLine::DisplayExpiry()
486 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
489 const char* ELine::Displayable()
491 return matchtext.c_str();
494 const char* KLine::Displayable()
496 return matchtext.c_str();
499 const char* GLine::Displayable()
501 return matchtext.c_str();
504 const char* ZLine::Displayable()
509 const char* QLine::Displayable()
514 bool XLineManager::RegisterFactory(XLineFactory* xlf)
516 XLineFactMap::iterator n = line_factory.find(xlf->GetType());
518 if (n != line_factory.end())
521 line_factory[xlf->GetType()] = xlf;
526 bool XLineManager::UnregisterFactory(XLineFactory* xlf)
528 XLineFactMap::iterator n = line_factory.find(xlf->GetType());
530 if (n == line_factory.end())
533 line_factory.erase(n);
538 XLineFactory* XLineManager::GetFactory(const std::string &type)
540 XLineFactMap::iterator n = line_factory.find(type);
542 if (n != line_factory.end())