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)
131 /*IdentHostPair ih = IdentSplit(hostmask);*/
133 if (DelLine(line->Displayable(), line->type, 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(item);
139 sort(active_lines.begin(), active_lines.end(), XLineManager::XSortComparison);
140 lookup_lines[line->type][hostmask] = item;
143 // XXX we really only need to check one line (the new one) - this is a bit wasteful!
144 // we should really create a temporary var here and pass that instead.
145 // hmm, perhaps we can merge this with line "application" somehow.. and just force a recheck on DelELine?
146 /*this->CheckELines(lookup_lines['E']);
150 /*bool XLineManager::AddZLine(long duration, const char* source, const char* reason, const char* ipaddr)
152 if (strchr(ipaddr,'@'))
154 while (*ipaddr != '@')
159 // deletes a g:line, returns true if the line existed and was removed
161 bool XLineManager::DelLine(const char* hostmask, char type, bool simulate)
163 IdentHostPair ih = IdentSplit(hostmask);
164 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
166 if ((*i)->type == type)
168 if ((*i)->MatchesLiteral(hostmask))
174 active_lines.erase(i);
175 if (lookup_lines.find(type) != lookup_lines.end())
176 lookup_lines[type].erase(hostmask);
177 /* XXX: Should erase from pending lines here */
190 /* remove exempt from everyone and force recheck after deleting eline */
191 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
193 User* u = (User*)(*u2);
197 if (ServerInstance->XLines->lookup_lines.find('E') != ServerInstance->XLines->lookup_lines.end())
198 ServerInstance->XLines->CheckELines(ServerInstance->XLines->lookup_lines['E']);
201 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
203 QLine* XLineManager::matches_qline(const char* nick)
205 if (lookup_lines.find('Q') == lookup_lines.end())
208 if (lookup_lines.find('Q') != lookup_lines.end() && lookup_lines['Q'].empty())
211 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
212 if ((*i)->type == 'Q' && (*i)->Matches(nick))
217 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
219 GLine* XLineManager::matches_gline(User* user)
221 if (lookup_lines.find('G') == lookup_lines.end())
224 if (lookup_lines.find('G') != lookup_lines.end() && lookup_lines['G'].empty())
227 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
228 if ((*i)->type == 'G' && (*i)->Matches(user))
234 ELine* XLineManager::matches_exception(User* user)
236 if (lookup_lines.find('E') == lookup_lines.end())
239 if (lookup_lines.find('E') != lookup_lines.end() && lookup_lines['E'].empty())
242 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
244 if ((*i)->type == 'E' && (*i)->Matches(user))
251 void XLineManager::gline_set_creation_time(const char* host, time_t create_time)
253 /*for (std::vector<XLine*>::iterator i = glines.begin(); i != glines.end(); i++)
255 if (!strcasecmp(host,(*i)->hostmask))
257 (*i)->set_time = create_time;
258 (*i)->expiry = create_time + (*i)->duration;
266 void XLineManager::eline_set_creation_time(const char* host, time_t create_time)
268 /*for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
270 if (!strcasecmp(host,(*i)->hostmask))
272 (*i)->set_time = create_time;
273 (*i)->expiry = create_time + (*i)->duration;
281 void XLineManager::qline_set_creation_time(const char* nick, time_t create_time)
283 /*for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
285 if (!strcasecmp(nick,(*i)->nick))
287 (*i)->set_time = create_time;
288 (*i)->expiry = create_time + (*i)->duration;
296 void XLineManager::zline_set_creation_time(const char* ip, time_t create_time)
298 /*for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
300 if (!strcasecmp(ip,(*i)->ipaddr))
302 (*i)->set_time = create_time;
303 (*i)->expiry = create_time + (*i)->duration;
311 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
313 ZLine* XLineManager::matches_zline(User *u)
315 if (lookup_lines.find('Z') == lookup_lines.end())
318 if (lookup_lines.find('Z') != lookup_lines.end() && lookup_lines['Z'].empty())
321 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
322 if ((*i)->type == 'Z' && (*i)->Matches(u))
327 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
329 KLine* XLineManager::matches_kline(User* user)
331 if (lookup_lines.find('K') == lookup_lines.end())
334 if (lookup_lines.find('K') != lookup_lines.end() && lookup_lines['K'].empty())
337 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
338 if ((*i)->Matches(user))
344 bool XLineManager::XSortComparison(const XLine *one, const XLine *two)
346 // account for permanent lines
347 if (one->expiry == 0)
351 return (one->expiry) < (two->expiry);
354 // removes lines that have expired
355 void XLineManager::expire_lines()
357 time_t current = ServerInstance->Time();
359 /* Because we now store all our XLines in sorted order using ((*i)->duration + (*i)->set_time) as a key, this
360 * means that to expire the XLines we just need to do a while, picking off the top few until there are
361 * none left at the head of the queue that are after the current time.
364 while ((active_lines.size()) && (current > (*active_lines.begin())->expiry) && ((*active_lines.begin())->duration != 0))
366 std::vector<XLine*>::iterator i = active_lines.begin();
367 (*i)->DisplayExpiry();
369 active_lines.erase(i);
374 // applies lines, removing clients and changing nicks etc as applicable
375 void XLineManager::ApplyLines()
377 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
379 User* u = (User*)(*u2);
381 for (std::vector<XLine *>::iterator i = pending_lines.begin(); i != pending_lines.end(); i++)
389 pending_lines.clear();
392 void XLineManager::stats_k(User* user, string_list &results)
394 /*std::string sn = ServerInstance->Config->ServerName;
395 for (std::vector<KLine*>::iterator i = klines.begin(); i != klines.end(); i++)
396 results.push_back(sn+" 216 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);*/
399 void XLineManager::stats_g(User* user, string_list &results)
401 /*std::string sn = ServerInstance->Config->ServerName;
402 for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
403 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);*/
406 void XLineManager::stats_q(User* user, string_list &results)
408 /*std::string sn = ServerInstance->Config->ServerName;
409 for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
410 results.push_back(sn+" 217 "+user->nick+" :"+(*i)->nick+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);*/
413 void XLineManager::stats_z(User* user, string_list &results)
415 /*std::string sn = ServerInstance->Config->ServerName;
416 for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
417 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->ipaddr+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);*/
420 void XLineManager::stats_e(User* user, string_list &results)
422 /*std::string sn = ServerInstance->Config->ServerName;
423 for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
424 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);*/
427 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance)
431 void XLine::Apply(User* u)
435 void XLine::DefaultApply(User* u, char line)
438 snprintf(reason, MAXBUF, "%c-Lined: %s", line, this->reason);
439 if (*ServerInstance->Config->MoronBanner)
440 u->WriteServ("NOTICE %s :*** %s", u->nick, ServerInstance->Config->MoronBanner);
441 if (ServerInstance->Config->HideBans)
442 User::QuitUser(ServerInstance, u, line + std::string("-Lined"), reason);
444 User::QuitUser(ServerInstance, u, reason);
447 bool KLine::Matches(User *u)
452 if ((match(u->ident, this->identmask)))
454 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
463 void KLine::Apply(User* u)
465 DefaultApply(u, 'K');
468 bool GLine::Matches(User *u)
473 if ((match(u->ident, this->identmask)))
475 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
484 void GLine::Apply(User* u)
486 DefaultApply(u, 'G');
489 bool ELine::Matches(User *u)
494 if ((match(u->ident, this->identmask)))
496 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
505 bool ZLine::Matches(User *u)
510 if (match(u->GetIPString(), this->ipaddr, true))
516 void ZLine::Apply(User* u)
518 DefaultApply(u, 'Z');
522 bool QLine::Matches(User *u)
527 if (match(u->nick, this->nick))
533 void QLine::Apply(User* u)
535 /* Can we force the user to their uid here instead? */
536 DefaultApply(u, 'Q');
540 bool ZLine::Matches(const std::string &str)
542 if (match(str.c_str(), this->ipaddr, true))
548 bool QLine::Matches(const std::string &str)
550 if (match(str.c_str(), this->nick))
556 bool ELine::Matches(const std::string &str)
558 return ((match(str.c_str(), matchtext.c_str(), true)));
561 bool KLine::Matches(const std::string &str)
563 return ((match(str.c_str(), matchtext.c_str(), true)));
566 bool GLine::Matches(const std::string &str)
568 return ((match(str.c_str(), matchtext.c_str(), true)));
571 bool ELine::MatchesLiteral(const std::string &str)
573 return (assign(str) == matchtext);
576 bool ZLine::MatchesLiteral(const std::string &str)
578 return (assign(str) == this->ipmask);
581 bool GLine::MatchesLiteral(const std::string &str)
583 return (assign(str) == matchtext);
586 bool KLine::MatchesLiteral(const std::string &str)
588 return (assign(str) == matchtext);
591 bool QLine::MatchesLiteral(const std::string &str)
593 return (assign(str) == this->nickmask);
598 ServerInstance->XLines->CheckELines(ServerInstance->XLines->lookup_lines['E']);
601 void ELine::DisplayExpiry()
603 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed E-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
606 void QLine::DisplayExpiry()
608 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Q-Line %s (set by %s %d seconds ago)",this->nick,this->source,this->duration);
611 void ZLine::DisplayExpiry()
613 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Z-Line %s (set by %s %d seconds ago)",this->ipaddr,this->source,this->duration);
616 void KLine::DisplayExpiry()
618 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed K-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
621 void GLine::DisplayExpiry()
623 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
626 const char* ELine::Displayable()
628 return matchtext.c_str();
631 const char* KLine::Displayable()
633 return matchtext.c_str();
636 const char* GLine::Displayable()
638 return matchtext.c_str();
641 const char* ZLine::Displayable()
646 const char* QLine::Displayable()