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);
74 conf->GetInstance()->Log(DEBUG,"Read Z line (badip tag): ipmask=%s reason=%s",ipmask,reason);
78 bool DoQLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
80 const char* reason = values[0].GetString();
81 const char* nick = values[1].GetString();
83 conf->GetInstance()->XLines->add_qline(0,"<Config>",reason,nick);
84 conf->GetInstance()->Log(DEBUG,"Read Q line (badnick tag): nick=%s reason=%s",nick,reason);
88 bool DoKLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
90 const char* reason = values[0].GetString();
91 const char* host = values[1].GetString();
93 conf->GetInstance()->XLines->add_kline(0,"<Config>",reason,host);
94 conf->GetInstance()->Log(DEBUG,"Read K line (badhost tag): host=%s reason=%s",host,reason);
98 bool DoELine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
100 const char* reason = values[0].GetString();
101 const char* host = values[1].GetString();
103 conf->GetInstance()->XLines->add_eline(0,"<Config>",reason,host);
104 conf->GetInstance()->Log(DEBUG,"Read E line (exception tag): host=%s reason=%s",host,reason);
108 IdentHostPair XLineManager::IdentSplit(const std::string &ident_and_host)
110 IdentHostPair n = std::make_pair<std::string,std::string>("*","*");
111 std::string::size_type x = ident_and_host.find('@');
112 if (x != std::string::npos)
114 n.second = ident_and_host.substr(x + 1,ident_and_host.length());
115 n.first = ident_and_host.substr(0, x);
116 if (!n.first.length())
118 if (!n.second.length())
123 n.second = ident_and_host;
131 bool XLineManager::add_gline(long duration, const char* source,const char* reason,const char* hostmask)
133 IdentHostPair ih = IdentSplit(hostmask);
135 bool ret = del_gline(hostmask);
137 GLine* item = new GLine(ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());
141 glines.push_back(item);
142 sort(glines.begin(), glines.end(),XLineManager::GSortComparison);
146 pglines.push_back(item);
152 // adds an e:line (exception to bans)
154 bool XLineManager::add_eline(long duration, const char* source, const char* reason, const char* hostmask)
156 IdentHostPair ih = IdentSplit(hostmask);
158 bool ret = del_eline(hostmask);
160 ELine* item = new ELine(ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());
164 elines.push_back(item);
165 sort(elines.begin(), elines.end(),XLineManager::ESortComparison);
169 pelines.push_back(item);
176 bool XLineManager::add_qline(long duration, const char* source, const char* reason, const char* nickname)
178 bool ret = del_qline(nickname);
179 QLine* item = new QLine(ServerInstance->Time(), duration, source, reason, nickname);
183 qlines.push_back(item);
184 sort(qlines.begin(), qlines.end(),XLineManager::QSortComparison);
188 pqlines.push_back(item);
195 bool XLineManager::add_zline(long duration, const char* source, const char* reason, const char* ipaddr)
197 bool ret = del_zline(ipaddr);
199 if (strchr(ipaddr,'@'))
201 while (*ipaddr != '@')
206 ZLine* item = new ZLine(ServerInstance->Time(), duration, source, reason, ipaddr);
210 zlines.push_back(item);
211 sort(zlines.begin(), zlines.end(),XLineManager::ZSortComparison);
215 pzlines.push_back(item);
222 bool XLineManager::add_kline(long duration, const char* source, const char* reason, const char* hostmask)
224 IdentHostPair ih = IdentSplit(hostmask);
226 bool ret = del_kline(hostmask);
228 KLine* item = new KLine(ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());
232 klines.push_back(item);
233 sort(klines.begin(), klines.end(),XLineManager::KSortComparison);
237 pklines.push_back(item);
242 // deletes a g:line, returns true if the line existed and was removed
244 bool XLineManager::del_gline(const char* hostmask)
246 IdentHostPair ih = IdentSplit(hostmask);
247 for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
249 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
256 for (std::vector<GLine*>::iterator i = pglines.begin(); i != pglines.end(); i++)
258 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
268 // deletes a e:line, returns true if the line existed and was removed
270 bool XLineManager::del_eline(const char* hostmask)
272 IdentHostPair ih = IdentSplit(hostmask);
273 for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
275 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
282 for (std::vector<ELine*>::iterator i = pelines.begin(); i != pelines.end(); i++)
284 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
294 // deletes a q:line, returns true if the line existed and was removed
296 bool XLineManager::del_qline(const char* nickname)
298 for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
300 if (!strcasecmp(nickname,(*i)->nick))
307 for (std::vector<QLine*>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
309 if (!strcasecmp(nickname,(*i)->nick))
319 // deletes a z:line, returns true if the line existed and was removed
321 bool XLineManager::del_zline(const char* ipaddr)
323 for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
325 if (!strcasecmp(ipaddr,(*i)->ipaddr))
332 for (std::vector<ZLine*>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
334 if (!strcasecmp(ipaddr,(*i)->ipaddr))
344 // deletes a k:line, returns true if the line existed and was removed
346 bool XLineManager::del_kline(const char* hostmask)
348 IdentHostPair ih = IdentSplit(hostmask);
349 for (std::vector<KLine*>::iterator i = klines.begin(); i != klines.end(); i++)
351 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
358 for (std::vector<KLine*>::iterator i = pklines.begin(); i != pklines.end(); i++)
360 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
370 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
372 QLine* XLineManager::matches_qline(const char* nick, bool permonly)
374 if ((qlines.empty()) && (pqlines.empty()))
378 for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
379 if (match(nick,(*i)->nick))
382 for (std::vector<QLine*>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
383 if (match(nick,(*i)->nick))
388 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
390 GLine* XLineManager::matches_gline(userrec* user, bool permonly)
392 if ((glines.empty()) && (pglines.empty()))
396 for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
398 if ((match(user->ident,(*i)->identmask)))
400 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
407 for (std::vector<GLine*>::iterator i = pglines.begin(); i != pglines.end(); i++)
409 if ((match(user->ident,(*i)->identmask)))
411 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
420 ELine* XLineManager::matches_exception(userrec* user, bool permonly)
422 if ((elines.empty()) && (pelines.empty()))
425 snprintf(host2,MAXBUF,"*@%s",user->host);
428 for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
430 if ((match(user->ident,(*i)->identmask)))
432 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
439 for (std::vector<ELine*>::iterator i = pelines.begin(); i != pelines.end(); i++)
441 if ((match(user->ident,(*i)->identmask)))
443 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
453 void XLineManager::gline_set_creation_time(const char* host, time_t create_time)
455 for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
457 if (!strcasecmp(host,(*i)->hostmask))
459 (*i)->set_time = create_time;
460 (*i)->expiry = create_time + (*i)->duration;
464 for (std::vector<GLine*>::iterator i = pglines.begin(); i != pglines.end(); i++)
466 if (!strcasecmp(host,(*i)->hostmask))
468 (*i)->set_time = create_time;
475 void XLineManager::eline_set_creation_time(const char* host, time_t create_time)
477 for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
479 if (!strcasecmp(host,(*i)->hostmask))
481 (*i)->set_time = create_time;
482 (*i)->expiry = create_time + (*i)->duration;
486 for (std::vector<ELine*>::iterator i = pelines.begin(); i != pelines.end(); i++)
488 if (!strcasecmp(host,(*i)->hostmask))
490 (*i)->set_time = create_time;
497 void XLineManager::qline_set_creation_time(const char* nick, time_t create_time)
499 for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
501 if (!strcasecmp(nick,(*i)->nick))
503 (*i)->set_time = create_time;
504 (*i)->expiry = create_time + (*i)->duration;
508 for (std::vector<QLine*>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
510 if (!strcasecmp(nick,(*i)->nick))
512 (*i)->set_time = create_time;
519 void XLineManager::zline_set_creation_time(const char* ip, time_t create_time)
521 for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
523 if (!strcasecmp(ip,(*i)->ipaddr))
525 (*i)->set_time = create_time;
526 (*i)->expiry = create_time + (*i)->duration;
530 for (std::vector<ZLine*>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
532 if (!strcasecmp(ip,(*i)->ipaddr))
534 (*i)->set_time = create_time;
541 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
543 ZLine* XLineManager::matches_zline(const char* ipaddr, bool permonly)
545 if ((zlines.empty()) && (pzlines.empty()))
549 for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
550 if (match(ipaddr,(*i)->ipaddr, true))
553 for (std::vector<ZLine*>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
554 if (match(ipaddr,(*i)->ipaddr, true))
559 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
561 KLine* XLineManager::matches_kline(userrec* user, bool permonly)
563 if ((klines.empty()) && (pklines.empty()))
567 for (std::vector<KLine*>::iterator i = klines.begin(); i != klines.end(); i++)
569 if ((match(user->ident,(*i)->identmask)))
571 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
578 for (std::vector<KLine*>::iterator i = pklines.begin(); i != pklines.end(); i++)
580 if ((match(user->ident,(*i)->identmask)))
582 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
591 bool XLineManager::GSortComparison ( const GLine* one, const GLine* two )
593 return (one->expiry) < (two->expiry);
596 bool XLineManager::ESortComparison ( const ELine* one, const ELine* two )
598 return (one->expiry) < (two->expiry);
601 bool XLineManager::ZSortComparison ( const ZLine* one, const ZLine* two )
603 return (one->expiry) < (two->expiry);
606 bool XLineManager::KSortComparison ( const KLine* one, const KLine* two )
608 return (one->expiry) < (two->expiry);
611 bool XLineManager::QSortComparison ( const QLine* one, const QLine* two )
613 return (one->expiry) < (two->expiry);
616 // removes lines that have expired
618 void XLineManager::expire_lines()
620 time_t current = ServerInstance->Time();
622 /* Because we now store all our XLines in sorted order using ((*i)->duration + (*i)->set_time) as a key, this
623 * means that to expire the XLines we just need to do a while, picking off the top few until there are
624 * none left at the head of the queue that are after the current time.
627 while ((glines.size()) && (current > (*glines.begin())->expiry))
629 std::vector<GLine*>::iterator i = glines.begin();
630 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s@%s (set by %s %d seconds ago)",(*i)->identmask,(*i)->hostmask,(*i)->source,(*i)->duration);
634 while ((elines.size()) && (current > (*elines.begin())->expiry))
636 std::vector<ELine*>::iterator i = elines.begin();
637 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed E-Line %s@%s (set by %s %d seconds ago)",(*i)->identmask,(*i)->hostmask,(*i)->source,(*i)->duration);
641 while ((zlines.size()) && (current > (*zlines.begin())->expiry))
643 std::vector<ZLine*>::iterator i = zlines.begin();
644 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Z-Line %s (set by %s %d seconds ago)",(*i)->ipaddr,(*i)->source,(*i)->duration);
648 while ((klines.size()) && (current > (*klines.begin())->expiry))
650 std::vector<KLine*>::iterator i = klines.begin();
651 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed K-Line %s@%s (set by %s %d seconds ago)",(*i)->identmask,(*i)->hostmask,(*i)->source,(*i)->duration);
655 while ((qlines.size()) && (current > (*qlines.begin())->expiry))
657 std::vector<QLine*>::iterator i = qlines.begin();
658 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Q-Line %s (set by %s %d seconds ago)",(*i)->nick,(*i)->source,(*i)->duration);
664 // applies lines, removing clients and changing nicks etc as applicable
666 void XLineManager::apply_lines(const int What)
671 if (What & APPLY_PERM_ONLY)
675 if ((!pglines.size()) && (!pklines.size()) && (!pzlines.size()) && (!pqlines.size()))
678 CullList* Goners = new CullList(ServerInstance);
680 for (std::vector<userrec*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
682 userrec* u = (userrec*)(*u2);
684 if (elines.size() || pelines.size())
685 if (matches_exception(u))
688 if ((What & APPLY_GLINES) && pglines.size())
690 if ((check = matches_gline(u,true)))
692 snprintf(reason,MAXBUF,"G-Lined: %s",check->reason);
693 Goners->AddItem(u,reason);
697 if ((What & APPLY_KLINES) && pklines.size())
699 if ((check = matches_kline(u,true)))
701 snprintf(reason,MAXBUF,"K-Lined: %s",check->reason);
702 Goners->AddItem(u,reason);
706 if ((What & APPLY_QLINES) && pqlines.size())
708 if ((check = matches_qline(u->nick,true)))
710 snprintf(reason,MAXBUF,"Q-Lined: %s",check->reason);
711 Goners->AddItem(u,reason);
715 if ((What & APPLY_ZLINES) && pzlines.size())
717 if ((check = matches_zline(u->GetIPString(),true)))
719 snprintf(reason,MAXBUF,"Z-Lined: %s",check->reason);
720 Goners->AddItem(u,reason);
732 if ((!glines.size()) && (!klines.size()) && (!zlines.size()) && (!qlines.size()) &&
733 (!pglines.size()) && (!pklines.size()) && (!pzlines.size()) && (!pqlines.size()))
736 CullList* Goners = new CullList(ServerInstance);
738 for (std::vector<userrec*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
740 userrec* u = (userrec*)(*u2);
742 if (elines.size() || pelines.size())
744 // ignore people matching exempts
745 if (matches_exception(u))
748 if ((What & APPLY_GLINES) && (glines.size() || pglines.size()))
750 if ((check = matches_gline(u)))
752 snprintf(reason,MAXBUF,"G-Lined: %s",check->reason);
753 Goners->AddItem(u,reason);
756 if ((What & APPLY_KLINES) && (klines.size() || pklines.size()))
758 if ((check = matches_kline(u)))
760 snprintf(reason,MAXBUF,"K-Lined: %s",check->reason);
761 Goners->AddItem(u,reason);
764 if ((What & APPLY_QLINES) && (qlines.size() || pqlines.size()))
766 if ((check = matches_qline(u->nick)))
768 snprintf(reason,MAXBUF,"Q-Lined: %s",check->reason);
769 Goners->AddItem(u,reason);
772 if ((What & APPLY_ZLINES) && (zlines.size() || pzlines.size()))
774 if ((check = matches_zline(u->GetIPString())))
776 snprintf(reason,MAXBUF,"Z-Lined: %s",check->reason);
777 Goners->AddItem(u,reason);
787 void XLineManager::stats_k(userrec* user, string_list &results)
789 std::string sn = ServerInstance->Config->ServerName;
790 for (std::vector<KLine*>::iterator i = klines.begin(); i != klines.end(); i++)
791 results.push_back(sn+" 216 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
792 for (std::vector<KLine*>::iterator i = pklines.begin(); i != pklines.end(); i++)
793 results.push_back(sn+" 216 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
796 void XLineManager::stats_g(userrec* user, string_list &results)
798 std::string sn = ServerInstance->Config->ServerName;
799 for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
800 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
801 for (std::vector<GLine*>::iterator i = pglines.begin(); i != pglines.end(); i++)
802 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
805 void XLineManager::stats_q(userrec* user, string_list &results)
807 std::string sn = ServerInstance->Config->ServerName;
808 for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
809 results.push_back(sn+" 217 "+user->nick+" :"+(*i)->nick+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
810 for (std::vector<QLine*>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
811 results.push_back(sn+" 217 "+user->nick+" :"+(*i)->nick+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
814 void XLineManager::stats_z(userrec* user, string_list &results)
816 std::string sn = ServerInstance->Config->ServerName;
817 for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
818 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->ipaddr+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
819 for (std::vector<ZLine*>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
820 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->ipaddr+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
823 void XLineManager::stats_e(userrec* user, string_list &results)
825 std::string sn = ServerInstance->Config->ServerName;
826 for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
827 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
828 for (std::vector<ELine*>::iterator i = pelines.begin(); i != pelines.end(); i++)
829 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
832 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance)