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 * ---------------------------------------------------
19 #include "cull_list.h"
21 /* Version two, now with optimized expiry!
23 * Because the old way was horrendously slow, the new way of expiring xlines is very
24 * very efficient. I have improved the efficiency of the algorithm in two ways:
26 * (1) There are now two lists of items for each linetype. One list holds temporary
27 * items, and the other list holds permanent items (ones which will expire).
28 * Items which are on the permanent list are NEVER checked at all by the
29 * expire_lines() function.
30 * (2) The temporary xline lists are always kept in strict numerical order, keyed by
31 * current time + duration. This means that the line which is due to expire the
32 * soonest is always pointed at by vector::begin(), so a simple while loop can
33 * very efficiently, very quickly and above all SAFELY pick off the first few
34 * items in the vector which need zapping.
39 bool InitXLine(ServerConfig* conf, const char* tag)
44 bool DoneZLine(ServerConfig* conf, const char* tag)
46 conf->GetInstance()->XLines->apply_lines(APPLY_ZLINES|APPLY_PERM_ONLY);
50 bool DoneQLine(ServerConfig* conf, const char* tag)
52 conf->GetInstance()->XLines->apply_lines(APPLY_QLINES|APPLY_PERM_ONLY);
56 bool DoneKLine(ServerConfig* conf, const char* tag)
58 conf->GetInstance()->XLines->apply_lines(APPLY_KLINES|APPLY_PERM_ONLY);
62 bool DoneELine(ServerConfig* conf, const char* tag)
64 /* Yes, this is supposed to do nothing, we dont 'apply' these */
68 bool DoZLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
70 const char* reason = values[0].GetString();
71 const char* ipmask = values[1].GetString();
73 conf->GetInstance()->XLines->add_zline(0,"<Config>",reason,ipmask);
77 bool DoQLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
79 const char* reason = values[0].GetString();
80 const char* nick = values[1].GetString();
82 conf->GetInstance()->XLines->add_qline(0,"<Config>",reason,nick);
86 bool DoKLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
88 const char* reason = values[0].GetString();
89 const char* host = values[1].GetString();
91 conf->GetInstance()->XLines->add_kline(0,"<Config>",reason,host);
95 bool DoELine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
97 const char* reason = values[0].GetString();
98 const char* host = values[1].GetString();
100 conf->GetInstance()->XLines->add_eline(0,"<Config>",reason,host);
104 IdentHostPair XLineManager::IdentSplit(const std::string &ident_and_host)
106 IdentHostPair n = std::make_pair<std::string,std::string>("*","*");
107 std::string::size_type x = ident_and_host.find('@');
108 if (x != std::string::npos)
110 n.second = ident_and_host.substr(x + 1,ident_and_host.length());
111 n.first = ident_and_host.substr(0, x);
112 if (!n.first.length())
114 if (!n.second.length())
119 n.second = ident_and_host;
127 bool XLineManager::add_gline(long duration, const char* source,const char* reason,const char* hostmask)
129 IdentHostPair ih = IdentSplit(hostmask);
131 bool ret = del_gline(hostmask);
133 GLine* item = new GLine(ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());
137 glines.push_back(item);
138 sort(glines.begin(), glines.end(),XLineManager::GSortComparison);
142 pglines.push_back(item);
148 // adds an e:line (exception to bans)
150 bool XLineManager::add_eline(long duration, const char* source, const char* reason, const char* hostmask)
152 IdentHostPair ih = IdentSplit(hostmask);
154 bool ret = del_eline(hostmask);
156 ELine* item = new ELine(ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());
160 elines.push_back(item);
161 sort(elines.begin(), elines.end(),XLineManager::ESortComparison);
165 pelines.push_back(item);
172 bool XLineManager::add_qline(long duration, const char* source, const char* reason, const char* nickname)
174 bool ret = del_qline(nickname);
175 QLine* item = new QLine(ServerInstance->Time(), duration, source, reason, nickname);
179 qlines.push_back(item);
180 sort(qlines.begin(), qlines.end(),XLineManager::QSortComparison);
184 pqlines.push_back(item);
191 bool XLineManager::add_zline(long duration, const char* source, const char* reason, const char* ipaddr)
193 bool ret = del_zline(ipaddr);
195 if (strchr(ipaddr,'@'))
197 while (*ipaddr != '@')
202 ZLine* item = new ZLine(ServerInstance->Time(), duration, source, reason, ipaddr);
206 zlines.push_back(item);
207 sort(zlines.begin(), zlines.end(),XLineManager::ZSortComparison);
211 pzlines.push_back(item);
218 bool XLineManager::add_kline(long duration, const char* source, const char* reason, const char* hostmask)
220 IdentHostPair ih = IdentSplit(hostmask);
222 bool ret = del_kline(hostmask);
224 KLine* item = new KLine(ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());
228 klines.push_back(item);
229 sort(klines.begin(), klines.end(),XLineManager::KSortComparison);
233 pklines.push_back(item);
238 // deletes a g:line, returns true if the line existed and was removed
240 bool XLineManager::del_gline(const char* hostmask)
242 IdentHostPair ih = IdentSplit(hostmask);
243 for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
245 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
252 for (std::vector<GLine*>::iterator i = pglines.begin(); i != pglines.end(); i++)
254 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
264 // deletes a e:line, returns true if the line existed and was removed
266 bool XLineManager::del_eline(const char* hostmask)
268 IdentHostPair ih = IdentSplit(hostmask);
269 for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
271 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
278 for (std::vector<ELine*>::iterator i = pelines.begin(); i != pelines.end(); i++)
280 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
290 // deletes a q:line, returns true if the line existed and was removed
292 bool XLineManager::del_qline(const char* nickname)
294 for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
296 if (!strcasecmp(nickname,(*i)->nick))
303 for (std::vector<QLine*>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
305 if (!strcasecmp(nickname,(*i)->nick))
315 // deletes a z:line, returns true if the line existed and was removed
317 bool XLineManager::del_zline(const char* ipaddr)
319 for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
321 if (!strcasecmp(ipaddr,(*i)->ipaddr))
328 for (std::vector<ZLine*>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
330 if (!strcasecmp(ipaddr,(*i)->ipaddr))
340 // deletes a k:line, returns true if the line existed and was removed
342 bool XLineManager::del_kline(const char* hostmask)
344 IdentHostPair ih = IdentSplit(hostmask);
345 for (std::vector<KLine*>::iterator i = klines.begin(); i != klines.end(); i++)
347 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
354 for (std::vector<KLine*>::iterator i = pklines.begin(); i != pklines.end(); i++)
356 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
366 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
368 QLine* XLineManager::matches_qline(const char* nick, bool permonly)
370 if ((qlines.empty()) && (pqlines.empty()))
374 for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
375 if (match(nick,(*i)->nick))
378 for (std::vector<QLine*>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
379 if (match(nick,(*i)->nick))
384 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
386 GLine* XLineManager::matches_gline(userrec* user, bool permonly)
388 if ((glines.empty()) && (pglines.empty()))
392 for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
394 if ((match(user->ident,(*i)->identmask)))
396 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
403 for (std::vector<GLine*>::iterator i = pglines.begin(); i != pglines.end(); i++)
405 if ((match(user->ident,(*i)->identmask)))
407 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
416 ELine* XLineManager::matches_exception(userrec* user, bool permonly)
418 if ((elines.empty()) && (pelines.empty()))
421 snprintf(host2,MAXBUF,"*@%s",user->host);
424 for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
426 if ((match(user->ident,(*i)->identmask)))
428 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
435 for (std::vector<ELine*>::iterator i = pelines.begin(); i != pelines.end(); i++)
437 if ((match(user->ident,(*i)->identmask)))
439 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
449 void XLineManager::gline_set_creation_time(const char* host, time_t create_time)
451 for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
453 if (!strcasecmp(host,(*i)->hostmask))
455 (*i)->set_time = create_time;
456 (*i)->expiry = create_time + (*i)->duration;
460 for (std::vector<GLine*>::iterator i = pglines.begin(); i != pglines.end(); i++)
462 if (!strcasecmp(host,(*i)->hostmask))
464 (*i)->set_time = create_time;
471 void XLineManager::eline_set_creation_time(const char* host, time_t create_time)
473 for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
475 if (!strcasecmp(host,(*i)->hostmask))
477 (*i)->set_time = create_time;
478 (*i)->expiry = create_time + (*i)->duration;
482 for (std::vector<ELine*>::iterator i = pelines.begin(); i != pelines.end(); i++)
484 if (!strcasecmp(host,(*i)->hostmask))
486 (*i)->set_time = create_time;
493 void XLineManager::qline_set_creation_time(const char* nick, time_t create_time)
495 for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
497 if (!strcasecmp(nick,(*i)->nick))
499 (*i)->set_time = create_time;
500 (*i)->expiry = create_time + (*i)->duration;
504 for (std::vector<QLine*>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
506 if (!strcasecmp(nick,(*i)->nick))
508 (*i)->set_time = create_time;
515 void XLineManager::zline_set_creation_time(const char* ip, time_t create_time)
517 for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
519 if (!strcasecmp(ip,(*i)->ipaddr))
521 (*i)->set_time = create_time;
522 (*i)->expiry = create_time + (*i)->duration;
526 for (std::vector<ZLine*>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
528 if (!strcasecmp(ip,(*i)->ipaddr))
530 (*i)->set_time = create_time;
537 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
539 ZLine* XLineManager::matches_zline(const char* ipaddr, bool permonly)
541 if ((zlines.empty()) && (pzlines.empty()))
545 for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
546 if (match(ipaddr,(*i)->ipaddr, true))
549 for (std::vector<ZLine*>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
550 if (match(ipaddr,(*i)->ipaddr, true))
555 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
557 KLine* XLineManager::matches_kline(userrec* user, bool permonly)
559 if ((klines.empty()) && (pklines.empty()))
563 for (std::vector<KLine*>::iterator i = klines.begin(); i != klines.end(); i++)
565 if ((match(user->ident,(*i)->identmask)))
567 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
574 for (std::vector<KLine*>::iterator i = pklines.begin(); i != pklines.end(); i++)
576 if ((match(user->ident,(*i)->identmask)))
578 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
587 bool XLineManager::GSortComparison ( const GLine* one, const GLine* two )
589 return (one->expiry) < (two->expiry);
592 bool XLineManager::ESortComparison ( const ELine* one, const ELine* two )
594 return (one->expiry) < (two->expiry);
597 bool XLineManager::ZSortComparison ( const ZLine* one, const ZLine* two )
599 return (one->expiry) < (two->expiry);
602 bool XLineManager::KSortComparison ( const KLine* one, const KLine* two )
604 return (one->expiry) < (two->expiry);
607 bool XLineManager::QSortComparison ( const QLine* one, const QLine* two )
609 return (one->expiry) < (two->expiry);
612 // removes lines that have expired
614 void XLineManager::expire_lines()
616 time_t current = ServerInstance->Time();
618 /* Because we now store all our XLines in sorted order using ((*i)->duration + (*i)->set_time) as a key, this
619 * means that to expire the XLines we just need to do a while, picking off the top few until there are
620 * none left at the head of the queue that are after the current time.
623 while ((glines.size()) && (current > (*glines.begin())->expiry))
625 std::vector<GLine*>::iterator i = glines.begin();
626 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s@%s (set by %s %d seconds ago)",(*i)->identmask,(*i)->hostmask,(*i)->source,(*i)->duration);
630 while ((elines.size()) && (current > (*elines.begin())->expiry))
632 std::vector<ELine*>::iterator i = elines.begin();
633 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed E-Line %s@%s (set by %s %d seconds ago)",(*i)->identmask,(*i)->hostmask,(*i)->source,(*i)->duration);
637 while ((zlines.size()) && (current > (*zlines.begin())->expiry))
639 std::vector<ZLine*>::iterator i = zlines.begin();
640 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Z-Line %s (set by %s %d seconds ago)",(*i)->ipaddr,(*i)->source,(*i)->duration);
644 while ((klines.size()) && (current > (*klines.begin())->expiry))
646 std::vector<KLine*>::iterator i = klines.begin();
647 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed K-Line %s@%s (set by %s %d seconds ago)",(*i)->identmask,(*i)->hostmask,(*i)->source,(*i)->duration);
651 while ((qlines.size()) && (current > (*qlines.begin())->expiry))
653 std::vector<QLine*>::iterator i = qlines.begin();
654 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Q-Line %s (set by %s %d seconds ago)",(*i)->nick,(*i)->source,(*i)->duration);
660 // applies lines, removing clients and changing nicks etc as applicable
662 void XLineManager::apply_lines(const int What)
667 if (What & APPLY_PERM_ONLY)
671 if ((!pglines.size()) && (!pklines.size()) && (!pzlines.size()) && (!pqlines.size()))
674 CullList* Goners = new CullList(ServerInstance);
676 for (std::vector<userrec*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
678 userrec* u = (userrec*)(*u2);
680 if (elines.size() || pelines.size())
681 if (matches_exception(u))
684 if ((What & APPLY_GLINES) && pglines.size())
686 if ((check = matches_gline(u,true)))
688 snprintf(reason,MAXBUF,"G-Lined: %s",check->reason);
689 Goners->AddItem(u,reason);
693 if ((What & APPLY_KLINES) && pklines.size())
695 if ((check = matches_kline(u,true)))
697 snprintf(reason,MAXBUF,"K-Lined: %s",check->reason);
698 Goners->AddItem(u,reason);
702 if ((What & APPLY_QLINES) && pqlines.size())
704 if ((check = matches_qline(u->nick,true)))
706 snprintf(reason,MAXBUF,"Q-Lined: %s",check->reason);
707 Goners->AddItem(u,reason);
711 if ((What & APPLY_ZLINES) && pzlines.size())
713 if ((check = matches_zline(u->GetIPString(),true)))
715 snprintf(reason,MAXBUF,"Z-Lined: %s",check->reason);
716 Goners->AddItem(u,reason);
728 if ((!glines.size()) && (!klines.size()) && (!zlines.size()) && (!qlines.size()) &&
729 (!pglines.size()) && (!pklines.size()) && (!pzlines.size()) && (!pqlines.size()))
732 CullList* Goners = new CullList(ServerInstance);
734 for (std::vector<userrec*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
736 userrec* u = (userrec*)(*u2);
738 if (elines.size() || pelines.size())
740 // ignore people matching exempts
741 if (matches_exception(u))
744 if ((What & APPLY_GLINES) && (glines.size() || pglines.size()))
746 if ((check = matches_gline(u)))
748 snprintf(reason,MAXBUF,"G-Lined: %s",check->reason);
749 Goners->AddItem(u,reason);
752 if ((What & APPLY_KLINES) && (klines.size() || pklines.size()))
754 if ((check = matches_kline(u)))
756 snprintf(reason,MAXBUF,"K-Lined: %s",check->reason);
757 Goners->AddItem(u,reason);
760 if ((What & APPLY_QLINES) && (qlines.size() || pqlines.size()))
762 if ((check = matches_qline(u->nick)))
764 snprintf(reason,MAXBUF,"Q-Lined: %s",check->reason);
765 Goners->AddItem(u,reason);
768 if ((What & APPLY_ZLINES) && (zlines.size() || pzlines.size()))
770 if ((check = matches_zline(u->GetIPString())))
772 snprintf(reason,MAXBUF,"Z-Lined: %s",check->reason);
773 Goners->AddItem(u,reason);
783 void XLineManager::stats_k(userrec* user, string_list &results)
785 std::string sn = ServerInstance->Config->ServerName;
786 for (std::vector<KLine*>::iterator i = klines.begin(); i != klines.end(); i++)
787 results.push_back(sn+" 216 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
788 for (std::vector<KLine*>::iterator i = pklines.begin(); i != pklines.end(); i++)
789 results.push_back(sn+" 216 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
792 void XLineManager::stats_g(userrec* user, string_list &results)
794 std::string sn = ServerInstance->Config->ServerName;
795 for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
796 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
797 for (std::vector<GLine*>::iterator i = pglines.begin(); i != pglines.end(); i++)
798 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
801 void XLineManager::stats_q(userrec* user, string_list &results)
803 std::string sn = ServerInstance->Config->ServerName;
804 for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
805 results.push_back(sn+" 217 "+user->nick+" :"+(*i)->nick+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
806 for (std::vector<QLine*>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
807 results.push_back(sn+" 217 "+user->nick+" :"+(*i)->nick+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
810 void XLineManager::stats_z(userrec* user, string_list &results)
812 std::string sn = ServerInstance->Config->ServerName;
813 for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
814 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->ipaddr+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
815 for (std::vector<ZLine*>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
816 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->ipaddr+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
819 void XLineManager::stats_e(userrec* user, string_list &results)
821 std::string sn = ServerInstance->Config->ServerName;
822 for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
823 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
824 for (std::vector<ELine*>::iterator i = pelines.begin(); i != pelines.end(); i++)
825 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
828 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance)