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());*/
123 pending_lines.push_back(line);
124 lookup_lines[line->type][line->Displayable()] = line;
130 FOREACH_MOD(I_OnAddLine,OnAddLine(user, line));
135 // deletes a line, returns true if the line existed and was removed
137 bool XLineManager::DelLine(const char* hostmask, char type, User* user, bool simulate)
139 std::map<char, std::map<std::string, XLine*> >::iterator x = lookup_lines.find(type);
141 if (x == lookup_lines.end())
144 std::map<std::string, XLine*>::iterator y = lookup_lines[type].find(hostmask);
146 if (y == lookup_lines[type].end())
152 FOREACH_MOD(I_OnDelLine,OnDelLine(user, y->second));
156 lookup_lines[type].erase(hostmask);
158 std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), y->second);
159 if (pptr != pending_lines.end())
160 pending_lines.erase(pptr);
162 if (y->second->duration)
166 lookup_lines[type].erase(y);
174 /* remove exempt from everyone and force recheck after deleting eline */
175 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
177 User* u = (User*)(*u2);
181 if (ServerInstance->XLines->lookup_lines.find('E') != ServerInstance->XLines->lookup_lines.end())
182 ServerInstance->XLines->CheckELines(ServerInstance->XLines->lookup_lines['E']);
185 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
187 XLine* XLineManager::MatchesLine(const char type, User* user)
189 std::map<char, std::map<std::string, XLine*> >::iterator x = lookup_lines.find(type);
191 if (x == lookup_lines.end())
195 for (std::map<std::string, XLine*>::iterator i = x->second.begin(); i != x->second.end(); i++)
197 if (i->second->Matches(user))
203 XLine* XLineManager::MatchesLine(const char type, const std::string &pattern)
205 std::map<char, std::map<std::string, XLine*> >::iterator x = lookup_lines.find(type);
207 if (x == lookup_lines.end())
210 for (std::map<std::string, XLine*>::iterator i = x->second.begin(); i != x->second.end(); i++)
212 if (i->second->Matches(pattern))
220 // removes lines that have expired
221 void XLineManager::expire_lines()
223 /* time_t current = ServerInstance->Time();
225 std::vector<XLine*>::iterator start = active_lines.begin() + PermLines;
227 while ((start < active_lines.end()) && (current > (*start)->expiry))
229 (*start)->DisplayExpiry();
232 if (lookup_lines.find((*start)->type) != lookup_lines.end())
233 lookup_lines[(*start)->type].erase((*start)->Displayable());
235 std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), *start);
236 if (pptr != pending_lines.end())
237 pending_lines.erase(pptr);
239 if (!(*start)->duration)
243 active_lines.erase(start);
248 // applies lines, removing clients and changing nicks etc as applicable
249 void XLineManager::ApplyLines()
251 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
253 User* u = (User*)(*u2);
255 for (std::vector<XLine *>::iterator i = pending_lines.begin(); i != pending_lines.end(); i++)
263 pending_lines.clear();
273 void XLineManager::InvokeStats(const char type, int numeric, User* user, string_list &results)
275 std::string sn = ServerInstance->Config->ServerName;
277 std::map<const char, std::map<std::string, XLine*> >::iterator n = lookup_lines.find(type);
279 if (n != lookup_lines.end())
281 std::map<std::string, XLine*>& list = n->second;
282 for (std::map<std::string, XLine*>::iterator i = list.begin(); i != list.end(); i++)
283 results.push_back(sn+" "+ConvToStr(numeric)+" "+user->nick+" :"+i->second->Displayable()+" "+
284 ConvToStr(i->second->set_time)+" "+ConvToStr(i->second->duration)+" "+std::string(i->second->source)+" :"+(i->second->reason));
289 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance), PermLines(0)
291 GFact = new GLineFactory(Instance);
292 EFact = new ELineFactory(Instance);
293 KFact = new KLineFactory(Instance);
294 QFact = new QLineFactory(Instance);
295 ZFact = new ZLineFactory(Instance);
297 RegisterFactory(GFact);
298 RegisterFactory(EFact);
299 RegisterFactory(KFact);
300 RegisterFactory(QFact);
301 RegisterFactory(ZFact);
304 XLineManager::~XLineManager()
306 UnregisterFactory(GFact);
307 UnregisterFactory(EFact);
308 UnregisterFactory(KFact);
309 UnregisterFactory(QFact);
310 UnregisterFactory(ZFact);
319 void XLine::Apply(User* u)
323 void XLine::DefaultApply(User* u, char line)
326 snprintf(reason, MAXBUF, "%c-Lined: %s", line, this->reason);
327 if (*ServerInstance->Config->MoronBanner)
328 u->WriteServ("NOTICE %s :*** %s", u->nick, ServerInstance->Config->MoronBanner);
329 if (ServerInstance->Config->HideBans)
330 User::QuitUser(ServerInstance, u, line + std::string("-Lined"), reason);
332 User::QuitUser(ServerInstance, u, reason);
335 bool KLine::Matches(User *u)
340 if ((match(u->ident, this->identmask)))
342 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
351 void KLine::Apply(User* u)
353 DefaultApply(u, 'K');
356 bool GLine::Matches(User *u)
361 if ((match(u->ident, this->identmask)))
363 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
372 void GLine::Apply(User* u)
374 DefaultApply(u, 'G');
377 bool ELine::Matches(User *u)
382 if ((match(u->ident, this->identmask)))
384 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
393 bool ZLine::Matches(User *u)
398 if (match(u->GetIPString(), this->ipaddr, true))
404 void ZLine::Apply(User* u)
406 DefaultApply(u, 'Z');
410 bool QLine::Matches(User *u)
415 if (match(u->nick, this->nick))
421 void QLine::Apply(User* u)
423 /* Can we force the user to their uid here instead? */
424 DefaultApply(u, 'Q');
428 bool ZLine::Matches(const std::string &str)
430 if (match(str.c_str(), this->ipaddr, true))
436 bool QLine::Matches(const std::string &str)
438 if (match(str.c_str(), this->nick))
444 bool ELine::Matches(const std::string &str)
446 return ((match(str.c_str(), matchtext.c_str(), true)));
449 bool KLine::Matches(const std::string &str)
451 return ((match(str.c_str(), matchtext.c_str(), true)));
454 bool GLine::Matches(const std::string &str)
456 return ((match(str.c_str(), matchtext.c_str(), true)));
459 bool ELine::MatchesLiteral(const std::string &str)
461 return (assign(str) == matchtext);
464 bool ZLine::MatchesLiteral(const std::string &str)
466 return (assign(str) == this->ipaddr);
469 bool GLine::MatchesLiteral(const std::string &str)
471 return (assign(str) == matchtext);
474 bool KLine::MatchesLiteral(const std::string &str)
476 return (assign(str) == matchtext);
479 bool QLine::MatchesLiteral(const std::string &str)
481 return (assign(str) == this->nick);
486 /* When adding one eline, only check the one eline */
487 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
489 User* u = (User*)(*u2);
490 if (this->Matches(u))
495 void ELine::DisplayExpiry()
497 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed E-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
500 void QLine::DisplayExpiry()
502 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Q-Line %s (set by %s %d seconds ago)",this->nick,this->source,this->duration);
505 void ZLine::DisplayExpiry()
507 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Z-Line %s (set by %s %d seconds ago)",this->ipaddr,this->source,this->duration);
510 void KLine::DisplayExpiry()
512 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed K-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
515 void GLine::DisplayExpiry()
517 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
520 const char* ELine::Displayable()
522 return matchtext.c_str();
525 const char* KLine::Displayable()
527 return matchtext.c_str();
530 const char* GLine::Displayable()
532 return matchtext.c_str();
535 const char* ZLine::Displayable()
540 const char* QLine::Displayable()
545 bool XLineManager::RegisterFactory(XLineFactory* xlf)
547 std::map<char, XLineFactory*>::iterator n = line_factory.find(xlf->GetType());
549 if (n != line_factory.end())
552 line_factory[xlf->GetType()] = xlf;
557 bool XLineManager::UnregisterFactory(XLineFactory* xlf)
559 std::map<char, XLineFactory*>::iterator n = line_factory.find(xlf->GetType());
561 if (n == line_factory.end())
564 line_factory.erase(n);
569 XLineFactory* XLineManager::GetFactory(const char type)
571 std::map<char, XLineFactory*>::iterator n = line_factory.find(type);
573 if (n != line_factory.end())