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(std::map<std::string, XLine *> &ELines)
65 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
67 User* u = (User*)(*u2);
69 for (std::map<std::string, XLine *>::iterator i = ELines.begin(); i != ELines.end(); i++)
72 u->exempt = e->Matches(u);
77 // this should probably be moved to configreader, but atm it relies on CheckELines above.
78 bool DoneELine(ServerConfig* conf, const char* tag)
80 for (std::vector<User*>::const_iterator u2 = conf->GetInstance()->local_users.begin(); u2 != conf->GetInstance()->local_users.end(); u2++)
82 User* u = (User*)(*u2);
86 conf->GetInstance()->XLines->CheckELines(conf->GetInstance()->XLines->lookup_lines['E']);
91 IdentHostPair XLineManager::IdentSplit(const std::string &ident_and_host)
93 IdentHostPair n = std::make_pair<std::string,std::string>("*","*");
94 std::string::size_type x = ident_and_host.find('@');
95 if (x != std::string::npos)
97 n.second = ident_and_host.substr(x + 1,ident_and_host.length());
98 n.first = ident_and_host.substr(0, x);
99 if (!n.first.length())
101 if (!n.second.length())
102 n.second.assign("*");
106 n.second = ident_and_host;
114 /*bool XLineManager::AddELine(long duration, const char* source, const char* reason, const char* hostmask)*/
115 bool XLineManager::AddLine(XLine* line, User* user)
117 /*IdentHostPair ih = IdentSplit(hostmask);*/
119 if (DelLine(line->Displayable(), line->type, user, true))
122 /*ELine* item = new ELine(ServerInstance, ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());*/
124 active_lines.push_back(line);
125 sort(active_lines.begin(), active_lines.end(), XLineManager::XSortComparison);
126 pending_lines.push_back(line);
127 lookup_lines[line->type][line->Displayable()] = line;
133 FOREACH_MOD(I_OnAddLine,OnAddLine(user, line));
138 /*bool XLineManager::AddZLine(long duration, const char* source, const char* reason, const char* ipaddr)
140 if (strchr(ipaddr,'@'))
142 while (*ipaddr != '@')
147 // deletes a g:line, returns true if the line existed and was removed
149 bool XLineManager::DelLine(const char* hostmask, char type, User* user, bool simulate)
151 IdentHostPair ih = IdentSplit(hostmask);
152 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
154 if ((*i)->type == type)
156 if ((*i)->MatchesLiteral(hostmask))
162 if (lookup_lines.find(type) != lookup_lines.end())
163 lookup_lines[type].erase(hostmask);
165 FOREACH_MOD(I_OnDelLine,OnDelLine(user, *i));
167 std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), *i);
168 if (pptr != pending_lines.end())
169 pending_lines.erase(pptr);
175 active_lines.erase(i);
188 /* remove exempt from everyone and force recheck after deleting eline */
189 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
191 User* u = (User*)(*u2);
195 if (ServerInstance->XLines->lookup_lines.find('E') != ServerInstance->XLines->lookup_lines.end())
196 ServerInstance->XLines->CheckELines(ServerInstance->XLines->lookup_lines['E']);
199 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
201 QLine* XLineManager::matches_qline(const char* nick)
203 if (lookup_lines.find('Q') == lookup_lines.end())
206 if (lookup_lines.find('Q') != lookup_lines.end() && lookup_lines['Q'].empty())
209 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
210 if ((*i)->type == 'Q' && (*i)->Matches(nick))
215 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
217 GLine* XLineManager::matches_gline(User* user)
219 if (lookup_lines.find('G') == lookup_lines.end())
222 if (lookup_lines.find('G') != lookup_lines.end() && lookup_lines['G'].empty())
225 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
226 if ((*i)->type == 'G' && (*i)->Matches(user))
232 ELine* XLineManager::matches_exception(User* user)
234 if (lookup_lines.find('E') == lookup_lines.end())
237 if (lookup_lines.find('E') != lookup_lines.end() && lookup_lines['E'].empty())
240 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
242 if ((*i)->type == 'E' && (*i)->Matches(user))
248 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
250 ZLine* XLineManager::matches_zline(User *u)
252 if (lookup_lines.find('Z') == lookup_lines.end())
255 if (lookup_lines.find('Z') != lookup_lines.end() && lookup_lines['Z'].empty())
258 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
259 if ((*i)->type == 'Z' && (*i)->Matches(u))
264 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
266 KLine* XLineManager::matches_kline(User* user)
268 if (lookup_lines.find('K') == lookup_lines.end())
271 if (lookup_lines.find('K') != lookup_lines.end() && lookup_lines['K'].empty())
274 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
275 if ((*i)->Matches(user))
281 bool XLineManager::XSortComparison(const XLine *one, const XLine *two)
283 return (one->expiry) < (two->expiry);
286 // removes lines that have expired
287 void XLineManager::expire_lines()
289 time_t current = ServerInstance->Time();
291 /* Because we now store all our XLines in sorted order using ((*i)->duration + (*i)->set_time) as a key, this
292 * means that to expire the XLines we just need to do a while, picking off the top few until there are
293 * none left at the head of the queue that are after the current time. We use PermLines as an offset into the
294 * vector past the first item with a duration 0.
297 std::vector<XLine*>::iterator start = active_lines.begin() + PermLines;
299 while ((start < active_lines.end()) && (current > (*start)->expiry))
301 (*start)->DisplayExpiry();
304 if (lookup_lines.find((*start)->type) != lookup_lines.end())
305 lookup_lines[(*start)->type].erase((*start)->Displayable());
307 std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), *start);
308 if (pptr != pending_lines.end())
309 pending_lines.erase(pptr);
311 if (!(*start)->duration)
315 active_lines.erase(start);
319 // applies lines, removing clients and changing nicks etc as applicable
320 void XLineManager::ApplyLines()
322 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
324 User* u = (User*)(*u2);
326 for (std::vector<XLine *>::iterator i = pending_lines.begin(); i != pending_lines.end(); i++)
334 pending_lines.clear();
344 void XLineManager::InvokeStats(const char type, int numeric, User* user, string_list &results)
346 std::string sn = ServerInstance->Config->ServerName;
348 std::map<const char, std::map<std::string, XLine*> >::iterator n = lookup_lines.find(type);
350 if (n != lookup_lines.end())
352 std::map<std::string, XLine*>& list = n->second;
353 for (std::map<std::string, XLine*>::iterator i = list.begin(); i != list.end(); i++)
354 results.push_back(sn+" "+ConvToStr(numeric)+" "+user->nick+" :"+i->second->Displayable()+" "+
355 ConvToStr(i->second->set_time)+" "+ConvToStr(i->second->duration)+" "+std::string(i->second->source)+" :"+(i->second->reason));
360 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance), PermLines(0)
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 void XLine::DefaultApply(User* u, char line)
397 snprintf(reason, MAXBUF, "%c-Lined: %s", line, this->reason);
398 if (*ServerInstance->Config->MoronBanner)
399 u->WriteServ("NOTICE %s :*** %s", u->nick, ServerInstance->Config->MoronBanner);
400 if (ServerInstance->Config->HideBans)
401 User::QuitUser(ServerInstance, u, line + std::string("-Lined"), reason);
403 User::QuitUser(ServerInstance, u, reason);
406 bool KLine::Matches(User *u)
411 if ((match(u->ident, this->identmask)))
413 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
422 void KLine::Apply(User* u)
424 DefaultApply(u, 'K');
427 bool GLine::Matches(User *u)
432 if ((match(u->ident, this->identmask)))
434 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
443 void GLine::Apply(User* u)
445 DefaultApply(u, 'G');
448 bool ELine::Matches(User *u)
453 if ((match(u->ident, this->identmask)))
455 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
464 bool ZLine::Matches(User *u)
469 if (match(u->GetIPString(), this->ipaddr, true))
475 void ZLine::Apply(User* u)
477 DefaultApply(u, 'Z');
481 bool QLine::Matches(User *u)
486 if (match(u->nick, this->nick))
492 void QLine::Apply(User* u)
494 /* Can we force the user to their uid here instead? */
495 DefaultApply(u, 'Q');
499 bool ZLine::Matches(const std::string &str)
501 if (match(str.c_str(), this->ipaddr, true))
507 bool QLine::Matches(const std::string &str)
509 if (match(str.c_str(), this->nick))
515 bool ELine::Matches(const std::string &str)
517 return ((match(str.c_str(), matchtext.c_str(), true)));
520 bool KLine::Matches(const std::string &str)
522 return ((match(str.c_str(), matchtext.c_str(), true)));
525 bool GLine::Matches(const std::string &str)
527 return ((match(str.c_str(), matchtext.c_str(), true)));
530 bool ELine::MatchesLiteral(const std::string &str)
532 return (assign(str) == matchtext);
535 bool ZLine::MatchesLiteral(const std::string &str)
537 return (assign(str) == this->ipaddr);
540 bool GLine::MatchesLiteral(const std::string &str)
542 return (assign(str) == matchtext);
545 bool KLine::MatchesLiteral(const std::string &str)
547 return (assign(str) == matchtext);
550 bool QLine::MatchesLiteral(const std::string &str)
552 return (assign(str) == this->nick);
557 /* When adding one eline, only check the one eline */
558 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
560 User* u = (User*)(*u2);
561 if (this->Matches(u))
566 void ELine::DisplayExpiry()
568 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed E-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
571 void QLine::DisplayExpiry()
573 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Q-Line %s (set by %s %d seconds ago)",this->nick,this->source,this->duration);
576 void ZLine::DisplayExpiry()
578 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Z-Line %s (set by %s %d seconds ago)",this->ipaddr,this->source,this->duration);
581 void KLine::DisplayExpiry()
583 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed K-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
586 void GLine::DisplayExpiry()
588 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
591 const char* ELine::Displayable()
593 return matchtext.c_str();
596 const char* KLine::Displayable()
598 return matchtext.c_str();
601 const char* GLine::Displayable()
603 return matchtext.c_str();
606 const char* ZLine::Displayable()
611 const char* QLine::Displayable()
616 bool XLineManager::RegisterFactory(XLineFactory* xlf)
618 std::map<char, XLineFactory*>::iterator n = line_factory.find(xlf->GetType());
620 if (n != line_factory.end())
623 line_factory[xlf->GetType()] = xlf;
628 bool XLineManager::UnregisterFactory(XLineFactory* xlf)
630 std::map<char, XLineFactory*>::iterator n = line_factory.find(xlf->GetType());
632 if (n == line_factory.end())
635 line_factory.erase(n);
640 XLineFactory* XLineManager::GetFactory(const char type)
642 std::map<char, XLineFactory*>::iterator n = line_factory.find(type);
644 if (n != line_factory.end())