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. Expiry will
40 * still use a sorted list, and we'll just ignore anything permanent.
42 * Application will be by a list of lines 'pending' application, meaning only the newly added lines
43 * will be gone over. Much faster.
45 * More of course is to come.
48 /* Version two, now with optimized expiry!
50 * Because the old way was horrendously slow, the new way of expiring xlines is very
51 * very efficient. I have improved the efficiency of the algorithm in two ways:
53 * (1) There are now two lists of items for each linetype. One list holds temporary
54 * items, and the other list holds permanent items (ones which will expire).
55 * Items which are on the permanent list are NEVER checked at all by the
56 * expire_lines() function.
57 * (2) The temporary xline lists are always kept in strict numerical order, keyed by
58 * current time + duration. This means that the line which is due to expire the
59 * soonest is always pointed at by vector::begin(), so a simple while loop can
60 * very efficiently, very quickly and above all SAFELY pick off the first few
61 * items in the vector which need zapping.
66 bool XLine::Matches(User *u)
72 * Checks what users match a given vector of ELines and sets their ban exempt flag accordingly.
74 void XLineManager::CheckELines(std::map<std::string, XLine *> &ELines)
79 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
81 User* u = (User*)(*u2);
83 for (std::map<std::string, XLine *>::iterator i = ELines.begin(); i != ELines.end(); i++)
86 u->exempt = e->Matches(u);
91 // this should probably be moved to configreader, but atm it relies on CheckELines above.
92 bool DoneELine(ServerConfig* conf, const char* tag)
94 for (std::vector<User*>::const_iterator u2 = conf->GetInstance()->local_users.begin(); u2 != conf->GetInstance()->local_users.end(); u2++)
96 User* u = (User*)(*u2);
100 conf->GetInstance()->XLines->CheckELines(conf->GetInstance()->XLines->lookup_lines['E']);
105 IdentHostPair XLineManager::IdentSplit(const std::string &ident_and_host)
107 IdentHostPair n = std::make_pair<std::string,std::string>("*","*");
108 std::string::size_type x = ident_and_host.find('@');
109 if (x != std::string::npos)
111 n.second = ident_and_host.substr(x + 1,ident_and_host.length());
112 n.first = ident_and_host.substr(0, x);
113 if (!n.first.length())
115 if (!n.second.length())
116 n.second.assign("*");
120 n.second = ident_and_host;
128 /*bool XLineManager::AddELine(long duration, const char* source, const char* reason, const char* hostmask)*/
129 bool XLineManager::AddLine(XLine* line, User* user)
131 /*IdentHostPair ih = IdentSplit(hostmask);*/
133 if (DelLine(line->Displayable(), line->type, user, true))
136 /*ELine* item = new ELine(ServerInstance, ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());*/
138 active_lines.push_back(line);
139 sort(active_lines.begin(), active_lines.end(), XLineManager::XSortComparison);
140 pending_lines.push_back(line);
141 lookup_lines[line->type][line->Displayable()] = line;
147 FOREACH_MOD(I_OnAddLine,OnAddLine(user, line));
152 /*bool XLineManager::AddZLine(long duration, const char* source, const char* reason, const char* ipaddr)
154 if (strchr(ipaddr,'@'))
156 while (*ipaddr != '@')
161 // deletes a g:line, returns true if the line existed and was removed
163 bool XLineManager::DelLine(const char* hostmask, char type, User* user, bool simulate)
165 IdentHostPair ih = IdentSplit(hostmask);
166 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
168 if ((*i)->type == type)
170 if ((*i)->MatchesLiteral(hostmask))
176 if (lookup_lines.find(type) != lookup_lines.end())
177 lookup_lines[type].erase(hostmask);
179 FOREACH_MOD(I_OnDelLine,OnDelLine(user, *i));
181 std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), *i);
182 if (pptr != pending_lines.end())
183 pending_lines.erase(pptr);
189 active_lines.erase(i);
202 /* remove exempt from everyone and force recheck after deleting eline */
203 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
205 User* u = (User*)(*u2);
209 if (ServerInstance->XLines->lookup_lines.find('E') != ServerInstance->XLines->lookup_lines.end())
210 ServerInstance->XLines->CheckELines(ServerInstance->XLines->lookup_lines['E']);
213 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
215 QLine* XLineManager::matches_qline(const char* nick)
217 if (lookup_lines.find('Q') == lookup_lines.end())
220 if (lookup_lines.find('Q') != lookup_lines.end() && lookup_lines['Q'].empty())
223 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
224 if ((*i)->type == 'Q' && (*i)->Matches(nick))
229 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
231 GLine* XLineManager::matches_gline(User* user)
233 if (lookup_lines.find('G') == lookup_lines.end())
236 if (lookup_lines.find('G') != lookup_lines.end() && lookup_lines['G'].empty())
239 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
240 if ((*i)->type == 'G' && (*i)->Matches(user))
246 ELine* XLineManager::matches_exception(User* user)
248 if (lookup_lines.find('E') == lookup_lines.end())
251 if (lookup_lines.find('E') != lookup_lines.end() && lookup_lines['E'].empty())
254 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
256 if ((*i)->type == 'E' && (*i)->Matches(user))
263 void XLineManager::gline_set_creation_time(const char* host, time_t create_time)
265 /*for (std::vector<XLine*>::iterator i = glines.begin(); i != glines.end(); i++)
267 if (!strcasecmp(host,(*i)->hostmask))
269 (*i)->set_time = create_time;
270 (*i)->expiry = create_time + (*i)->duration;
278 void XLineManager::eline_set_creation_time(const char* host, time_t create_time)
280 /*for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
282 if (!strcasecmp(host,(*i)->hostmask))
284 (*i)->set_time = create_time;
285 (*i)->expiry = create_time + (*i)->duration;
293 void XLineManager::qline_set_creation_time(const char* nick, time_t create_time)
295 /*for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
297 if (!strcasecmp(nick,(*i)->nick))
299 (*i)->set_time = create_time;
300 (*i)->expiry = create_time + (*i)->duration;
308 void XLineManager::zline_set_creation_time(const char* ip, time_t create_time)
310 /*for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
312 if (!strcasecmp(ip,(*i)->ipaddr))
314 (*i)->set_time = create_time;
315 (*i)->expiry = create_time + (*i)->duration;
323 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
325 ZLine* XLineManager::matches_zline(User *u)
327 if (lookup_lines.find('Z') == lookup_lines.end())
330 if (lookup_lines.find('Z') != lookup_lines.end() && lookup_lines['Z'].empty())
333 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
334 if ((*i)->type == 'Z' && (*i)->Matches(u))
339 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
341 KLine* XLineManager::matches_kline(User* user)
343 if (lookup_lines.find('K') == lookup_lines.end())
346 if (lookup_lines.find('K') != lookup_lines.end() && lookup_lines['K'].empty())
349 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
350 if ((*i)->Matches(user))
356 bool XLineManager::XSortComparison(const XLine *one, const XLine *two)
358 return (one->expiry) < (two->expiry);
361 // removes lines that have expired
362 void XLineManager::expire_lines()
364 time_t current = ServerInstance->Time();
366 /* Because we now store all our XLines in sorted order using ((*i)->duration + (*i)->set_time) as a key, this
367 * means that to expire the XLines we just need to do a while, picking off the top few until there are
368 * none left at the head of the queue that are after the current time. We use PermLines as an offset into the
369 * vector past the first item with a duration 0.
372 std::vector<XLine*>::iterator start = active_lines.begin() + PermLines;
374 while ((start < active_lines.end()) && (current > (*start)->expiry))
376 (*start)->DisplayExpiry();
379 if (lookup_lines.find((*start)->type) != lookup_lines.end())
380 lookup_lines[(*start)->type].erase((*start)->Displayable());
382 std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), *start);
383 if (pptr != pending_lines.end())
384 pending_lines.erase(pptr);
386 if (!(*start)->duration)
390 active_lines.erase(start);
394 // applies lines, removing clients and changing nicks etc as applicable
395 void XLineManager::ApplyLines()
397 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
399 User* u = (User*)(*u2);
401 for (std::vector<XLine *>::iterator i = pending_lines.begin(); i != pending_lines.end(); i++)
409 pending_lines.clear();
412 void XLineManager::stats_k(User* user, string_list &results)
414 /*std::string sn = ServerInstance->Config->ServerName;
415 for (std::vector<KLine*>::iterator i = klines.begin(); i != klines.end(); i++)
416 results.push_back(sn+" 216 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);*/
419 void XLineManager::stats_g(User* user, string_list &results)
421 /*std::string sn = ServerInstance->Config->ServerName;
422 for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
423 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);*/
426 void XLineManager::stats_q(User* user, string_list &results)
428 /*std::string sn = ServerInstance->Config->ServerName;
429 for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
430 results.push_back(sn+" 217 "+user->nick+" :"+(*i)->nick+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);*/
433 void XLineManager::stats_z(User* user, string_list &results)
435 /*std::string sn = ServerInstance->Config->ServerName;
436 for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
437 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->ipaddr+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);*/
440 void XLineManager::stats_e(User* user, string_list &results)
442 /*std::string sn = ServerInstance->Config->ServerName;
443 for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
444 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);*/
447 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance), PermLines(0)
449 GFact = new GLineFactory(Instance);
450 EFact = new ELineFactory(Instance);
451 KFact = new KLineFactory(Instance);
452 QFact = new QLineFactory(Instance);
453 ZFact = new ZLineFactory(Instance);
455 RegisterFactory(GFact);
456 RegisterFactory(EFact);
457 RegisterFactory(KFact);
458 RegisterFactory(QFact);
459 RegisterFactory(ZFact);
462 XLineManager::~XLineManager()
464 UnregisterFactory(GFact);
465 UnregisterFactory(EFact);
466 UnregisterFactory(KFact);
467 UnregisterFactory(QFact);
468 UnregisterFactory(ZFact);
477 void XLine::Apply(User* u)
481 void XLine::DefaultApply(User* u, char line)
484 snprintf(reason, MAXBUF, "%c-Lined: %s", line, this->reason);
485 if (*ServerInstance->Config->MoronBanner)
486 u->WriteServ("NOTICE %s :*** %s", u->nick, ServerInstance->Config->MoronBanner);
487 if (ServerInstance->Config->HideBans)
488 User::QuitUser(ServerInstance, u, line + std::string("-Lined"), reason);
490 User::QuitUser(ServerInstance, u, reason);
493 bool KLine::Matches(User *u)
498 if ((match(u->ident, this->identmask)))
500 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
509 void KLine::Apply(User* u)
511 DefaultApply(u, 'K');
514 bool GLine::Matches(User *u)
519 if ((match(u->ident, this->identmask)))
521 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
530 void GLine::Apply(User* u)
532 DefaultApply(u, 'G');
535 bool ELine::Matches(User *u)
540 if ((match(u->ident, this->identmask)))
542 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
551 bool ZLine::Matches(User *u)
556 if (match(u->GetIPString(), this->ipaddr, true))
562 void ZLine::Apply(User* u)
564 DefaultApply(u, 'Z');
568 bool QLine::Matches(User *u)
573 if (match(u->nick, this->nick))
579 void QLine::Apply(User* u)
581 /* Can we force the user to their uid here instead? */
582 DefaultApply(u, 'Q');
586 bool ZLine::Matches(const std::string &str)
588 if (match(str.c_str(), this->ipaddr, true))
594 bool QLine::Matches(const std::string &str)
596 if (match(str.c_str(), this->nick))
602 bool ELine::Matches(const std::string &str)
604 return ((match(str.c_str(), matchtext.c_str(), true)));
607 bool KLine::Matches(const std::string &str)
609 return ((match(str.c_str(), matchtext.c_str(), true)));
612 bool GLine::Matches(const std::string &str)
614 return ((match(str.c_str(), matchtext.c_str(), true)));
617 bool ELine::MatchesLiteral(const std::string &str)
619 return (assign(str) == matchtext);
622 bool ZLine::MatchesLiteral(const std::string &str)
624 return (assign(str) == this->ipaddr);
627 bool GLine::MatchesLiteral(const std::string &str)
629 return (assign(str) == matchtext);
632 bool KLine::MatchesLiteral(const std::string &str)
634 return (assign(str) == matchtext);
637 bool QLine::MatchesLiteral(const std::string &str)
639 return (assign(str) == this->nick);
644 /* When adding one eline, only check the one eline */
645 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
647 User* u = (User*)(*u2);
648 if (this->Matches(u))
653 void ELine::DisplayExpiry()
655 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed E-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
658 void QLine::DisplayExpiry()
660 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Q-Line %s (set by %s %d seconds ago)",this->nick,this->source,this->duration);
663 void ZLine::DisplayExpiry()
665 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Z-Line %s (set by %s %d seconds ago)",this->ipaddr,this->source,this->duration);
668 void KLine::DisplayExpiry()
670 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed K-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
673 void GLine::DisplayExpiry()
675 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
678 const char* ELine::Displayable()
680 return matchtext.c_str();
683 const char* KLine::Displayable()
685 return matchtext.c_str();
688 const char* GLine::Displayable()
690 return matchtext.c_str();
693 const char* ZLine::Displayable()
698 const char* QLine::Displayable()
703 bool XLineManager::RegisterFactory(XLineFactory* xlf)
705 std::map<char, XLineFactory*>::iterator n = line_factory.find(xlf->GetType());
707 if (n != line_factory.end())
710 line_factory[xlf->GetType()] = xlf;
715 bool XLineManager::UnregisterFactory(XLineFactory* xlf)
717 std::map<char, XLineFactory*>::iterator n = line_factory.find(xlf->GetType());
719 if (n == line_factory.end())
722 line_factory.erase(n);
727 XLineFactory* XLineManager::GetFactory(const char type)
729 std::map<char, XLineFactory*>::iterator n = line_factory.find(type);
731 if (n != line_factory.end())