1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
24 #include "cull_list.h"
26 /* Version two, now with optimized expiry!
28 * Because the old way was horrendously slow, the new way of expiring xlines is very
29 * very efficient. I have improved the efficiency of the algorithm in two ways:
31 * (1) There are now two lists of items for each linetype. One list holds temporary
32 * items, and the other list holds permanent items (ones which will expire).
33 * Items which are on the permanent list are NEVER checked at all by the
34 * expire_lines() function.
35 * (2) The temporary xline lists are always kept in strict numerical order, keyed by
36 * current time + duration. This means that the line which is due to expire the
37 * soonest is always pointed at by vector::begin(), so a simple while loop can
38 * very efficiently, very quickly and above all SAFELY pick off the first few
39 * items in the vector which need zapping.
44 bool InitXLine(ServerConfig* conf, const char* tag)
49 bool DoneXLine(ServerConfig* conf, const char* tag)
51 conf->GetInstance()->XLines->apply_lines(APPLY_ALL);
55 bool DoZLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
57 const char* reason = values[0].GetString();
58 const char* ipmask = values[1].GetString();
60 conf->GetInstance()->XLines->add_zline(0,"<Config>",reason,ipmask);
61 conf->GetInstance()->Log(DEBUG,"Read Z line (badip tag): ipmask=%s reason=%s",ipmask,reason);
65 bool DoQLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
67 const char* reason = values[0].GetString();
68 const char* nick = values[1].GetString();
70 conf->GetInstance()->XLines->add_qline(0,"<Config>",reason,nick);
71 conf->GetInstance()->Log(DEBUG,"Read Q line (badnick tag): nick=%s reason=%s",nick,reason);
75 bool DoKLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
77 const char* reason = values[0].GetString();
78 const char* host = values[1].GetString();
80 conf->GetInstance()->XLines->add_kline(0,"<Config>",reason,host);
81 conf->GetInstance()->Log(DEBUG,"Read K line (badhost tag): host=%s reason=%s",host,reason);
85 bool DoELine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
87 const char* reason = values[0].GetString();
88 const char* host = values[1].GetString();
90 conf->GetInstance()->XLines->add_eline(0,"<Config>",reason,host);
91 conf->GetInstance()->Log(DEBUG,"Read E line (exception tag): host=%s reason=%s",host,reason);
95 IdentHostPair XLineManager::IdentSplit(const std::string &ident_and_host)
97 IdentHostPair n = std::make_pair<std::string,std::string>("*","*");
98 std::string::size_type x = ident_and_host.find('@');
99 if (x != std::string::npos)
101 n.second = ident_and_host.substr(x + 1,ident_and_host.length());
102 n.first = ident_and_host.substr(0, x);
103 if (!n.first.length())
105 if (!n.second.length())
110 n.second = ident_and_host;
118 bool XLineManager::add_gline(long duration, const char* source,const char* reason,const char* hostmask)
120 IdentHostPair ih = IdentSplit(hostmask);
122 bool ret = del_gline(hostmask);
124 GLine* item = new GLine(ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());
128 glines.push_back(item);
129 sort(glines.begin(), glines.end(),XLineManager::GSortComparison);
133 pglines.push_back(item);
139 // adds an e:line (exception to bans)
141 bool XLineManager::add_eline(long duration, const char* source, const char* reason, const char* hostmask)
143 IdentHostPair ih = IdentSplit(hostmask);
145 bool ret = del_eline(hostmask);
147 ELine* item = new ELine(ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());
151 elines.push_back(item);
152 sort(elines.begin(), elines.end(),XLineManager::ESortComparison);
156 pelines.push_back(item);
163 bool XLineManager::add_qline(long duration, const char* source, const char* reason, const char* nickname)
165 bool ret = del_qline(nickname);
166 QLine* item = new QLine(ServerInstance->Time(), duration, source, reason, nickname);
170 qlines.push_back(item);
171 sort(qlines.begin(), qlines.end(),XLineManager::QSortComparison);
175 pqlines.push_back(item);
182 bool XLineManager::add_zline(long duration, const char* source, const char* reason, const char* ipaddr)
184 bool ret = del_zline(ipaddr);
186 if (strchr(ipaddr,'@'))
188 while (*ipaddr != '@')
193 ZLine* item = new ZLine(ServerInstance->Time(), duration, source, reason, ipaddr);
197 zlines.push_back(item);
198 sort(zlines.begin(), zlines.end(),XLineManager::ZSortComparison);
202 pzlines.push_back(item);
209 bool XLineManager::add_kline(long duration, const char* source, const char* reason, const char* hostmask)
211 IdentHostPair ih = IdentSplit(hostmask);
213 bool ret = del_kline(hostmask);
215 KLine* item = new KLine(ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());
219 klines.push_back(item);
220 sort(klines.begin(), klines.end(),XLineManager::KSortComparison);
224 pklines.push_back(item);
229 // deletes a g:line, returns true if the line existed and was removed
231 bool XLineManager::del_gline(const char* hostmask)
233 IdentHostPair ih = IdentSplit(hostmask);
234 for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
236 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
243 for (std::vector<GLine*>::iterator i = pglines.begin(); i != pglines.end(); i++)
245 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
255 // deletes a e:line, returns true if the line existed and was removed
257 bool XLineManager::del_eline(const char* hostmask)
259 IdentHostPair ih = IdentSplit(hostmask);
260 for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
262 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
269 for (std::vector<ELine*>::iterator i = pelines.begin(); i != pelines.end(); i++)
271 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
281 // deletes a q:line, returns true if the line existed and was removed
283 bool XLineManager::del_qline(const char* nickname)
285 for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
287 if (!strcasecmp(nickname,(*i)->nick))
294 for (std::vector<QLine*>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
296 if (!strcasecmp(nickname,(*i)->nick))
306 // deletes a z:line, returns true if the line existed and was removed
308 bool XLineManager::del_zline(const char* ipaddr)
310 for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
312 if (!strcasecmp(ipaddr,(*i)->ipaddr))
319 for (std::vector<ZLine*>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
321 if (!strcasecmp(ipaddr,(*i)->ipaddr))
331 // deletes a k:line, returns true if the line existed and was removed
333 bool XLineManager::del_kline(const char* hostmask)
335 IdentHostPair ih = IdentSplit(hostmask);
336 for (std::vector<KLine*>::iterator i = klines.begin(); i != klines.end(); i++)
338 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
345 for (std::vector<KLine*>::iterator i = pklines.begin(); i != pklines.end(); i++)
347 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
357 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
359 QLine* XLineManager::matches_qline(const char* nick)
361 if ((qlines.empty()) && (pqlines.empty()))
363 for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
364 if (match(nick,(*i)->nick))
366 for (std::vector<QLine*>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
367 if (match(nick,(*i)->nick))
372 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
374 GLine* XLineManager::matches_gline(userrec* user)
376 if ((glines.empty()) && (pglines.empty()))
378 for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
380 if ((match(user->ident,(*i)->identmask)))
382 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
388 for (std::vector<GLine*>::iterator i = pglines.begin(); i != pglines.end(); i++)
390 if ((match(user->ident,(*i)->identmask)))
392 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
401 ELine* XLineManager::matches_exception(userrec* user)
403 if ((elines.empty()) && (pelines.empty()))
406 snprintf(host2,MAXBUF,"*@%s",user->host);
407 for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
409 if ((match(user->ident,(*i)->identmask)))
411 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
417 for (std::vector<ELine*>::iterator i = pelines.begin(); i != pelines.end(); i++)
419 if ((match(user->ident,(*i)->identmask)))
421 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
431 void XLineManager::gline_set_creation_time(const char* host, time_t create_time)
433 for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
435 if (!strcasecmp(host,(*i)->hostmask))
437 (*i)->set_time = create_time;
438 (*i)->expiry = create_time + (*i)->duration;
442 for (std::vector<GLine*>::iterator i = pglines.begin(); i != pglines.end(); i++)
444 if (!strcasecmp(host,(*i)->hostmask))
446 (*i)->set_time = create_time;
453 void XLineManager::eline_set_creation_time(const char* host, time_t create_time)
455 for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.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<ELine*>::iterator i = pelines.begin(); i != pelines.end(); i++)
466 if (!strcasecmp(host,(*i)->hostmask))
468 (*i)->set_time = create_time;
475 void XLineManager::qline_set_creation_time(const char* nick, time_t create_time)
477 for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
479 if (!strcasecmp(nick,(*i)->nick))
481 (*i)->set_time = create_time;
482 (*i)->expiry = create_time + (*i)->duration;
486 for (std::vector<QLine*>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
488 if (!strcasecmp(nick,(*i)->nick))
490 (*i)->set_time = create_time;
497 void XLineManager::zline_set_creation_time(const char* ip, time_t create_time)
499 for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
501 if (!strcasecmp(ip,(*i)->ipaddr))
503 (*i)->set_time = create_time;
504 (*i)->expiry = create_time + (*i)->duration;
508 for (std::vector<ZLine*>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
510 if (!strcasecmp(ip,(*i)->ipaddr))
512 (*i)->set_time = create_time;
519 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
521 ZLine* XLineManager::matches_zline(const char* ipaddr)
523 if ((zlines.empty()) && (pzlines.empty()))
525 for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
526 if (match(ipaddr,(*i)->ipaddr, true))
528 for (std::vector<ZLine*>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
529 if (match(ipaddr,(*i)->ipaddr, true))
534 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
536 KLine* XLineManager::matches_kline(userrec* user)
538 if ((klines.empty()) && (pklines.empty()))
540 for (std::vector<KLine*>::iterator i = klines.begin(); i != klines.end(); i++)
542 if ((match(user->ident,(*i)->identmask)))
544 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
550 for (std::vector<KLine*>::iterator i = pklines.begin(); i != pklines.end(); i++)
552 if ((match(user->ident,(*i)->identmask)))
554 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
563 bool XLineManager::GSortComparison ( const GLine* one, const GLine* two )
565 return (one->expiry) < (two->expiry);
568 bool XLineManager::ESortComparison ( const ELine* one, const ELine* two )
570 return (one->expiry) < (two->expiry);
573 bool XLineManager::ZSortComparison ( const ZLine* one, const ZLine* two )
575 return (one->expiry) < (two->expiry);
578 bool XLineManager::KSortComparison ( const KLine* one, const KLine* two )
580 return (one->expiry) < (two->expiry);
583 bool XLineManager::QSortComparison ( const QLine* one, const QLine* two )
585 return (one->expiry) < (two->expiry);
588 // removes lines that have expired
590 void XLineManager::expire_lines()
592 time_t current = ServerInstance->Time();
594 /* Because we now store all our XLines in sorted order using ((*i)->duration + (*i)->set_time) as a key, this
595 * means that to expire the XLines we just need to do a while, picking off the top few until there are
596 * none left at the head of the queue that are after the current time.
599 while ((glines.size()) && (current > (*glines.begin())->expiry))
601 std::vector<GLine*>::iterator i = glines.begin();
602 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s@%s (set by %s %d seconds ago)",(*i)->identmask,(*i)->hostmask,(*i)->source,(*i)->duration);
606 while ((elines.size()) && (current > (*elines.begin())->expiry))
608 std::vector<ELine*>::iterator i = elines.begin();
609 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed E-Line %s@%s (set by %s %d seconds ago)",(*i)->identmask,(*i)->hostmask,(*i)->source,(*i)->duration);
613 while ((zlines.size()) && (current > (*zlines.begin())->expiry))
615 std::vector<ZLine*>::iterator i = zlines.begin();
616 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Z-Line %s (set by %s %d seconds ago)",(*i)->ipaddr,(*i)->source,(*i)->duration);
620 while ((klines.size()) && (current > (*klines.begin())->expiry))
622 std::vector<KLine*>::iterator i = klines.begin();
623 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed K-Line %s@%s (set by %s %d seconds ago)",(*i)->identmask,(*i)->hostmask,(*i)->source,(*i)->duration);
627 while ((qlines.size()) && (current > (*qlines.begin())->expiry))
629 std::vector<QLine*>::iterator i = qlines.begin();
630 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Q-Line %s (set by %s %d seconds ago)",(*i)->nick,(*i)->source,(*i)->duration);
636 // applies lines, removing clients and changing nicks etc as applicable
638 void XLineManager::apply_lines(const int What)
642 if ((!glines.size()) && (!klines.size()) && (!zlines.size()) && (!qlines.size()) &&
643 (!pglines.size()) && (!pklines.size()) && (!pzlines.size()) && (!pqlines.size()))
646 CullList* Goners = new CullList(ServerInstance);
648 for (std::vector<userrec*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
650 userrec* u = (userrec*)(*u2);
652 if (elines.size() || pelines.size())
654 // ignore people matching exempts
655 if (matches_exception(u))
658 if ((What & APPLY_GLINES) && (glines.size() || pglines.size()))
660 if ((check = matches_gline(u)))
662 snprintf(reason,MAXBUF,"G-Lined: %s",check->reason);
663 Goners->AddItem(u,reason);
666 if ((What & APPLY_KLINES) && (klines.size() || pklines.size()))
668 if ((check = matches_kline(u)))
670 snprintf(reason,MAXBUF,"K-Lined: %s",check->reason);
671 Goners->AddItem(u,reason);
674 if ((What & APPLY_QLINES) && (qlines.size() || pqlines.size()))
676 if ((check = matches_qline(u->nick)))
678 snprintf(reason,MAXBUF,"Q-Lined: %s",check->reason);
679 Goners->AddItem(u,reason);
682 if ((What & APPLY_ZLINES) && (zlines.size() || pzlines.size()))
684 if ((check = matches_zline(u->GetIPString())))
686 snprintf(reason,MAXBUF,"Z-Lined: %s",check->reason);
687 Goners->AddItem(u,reason);
696 void XLineManager::stats_k(userrec* user, string_list &results)
698 std::string sn = ServerInstance->Config->ServerName;
699 for (std::vector<KLine*>::iterator i = klines.begin(); i != klines.end(); i++)
700 results.push_back(sn+" 216 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
701 for (std::vector<KLine*>::iterator i = pklines.begin(); i != pklines.end(); i++)
702 results.push_back(sn+" 216 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
705 void XLineManager::stats_g(userrec* user, string_list &results)
707 std::string sn = ServerInstance->Config->ServerName;
708 for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
709 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
710 for (std::vector<GLine*>::iterator i = pglines.begin(); i != pglines.end(); i++)
711 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
714 void XLineManager::stats_q(userrec* user, string_list &results)
716 std::string sn = ServerInstance->Config->ServerName;
717 for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
718 results.push_back(sn+" 217 "+user->nick+" :"+(*i)->nick+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
719 for (std::vector<QLine*>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
720 results.push_back(sn+" 217 "+user->nick+" :"+(*i)->nick+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
723 void XLineManager::stats_z(userrec* user, string_list &results)
725 std::string sn = ServerInstance->Config->ServerName;
726 for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
727 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->ipaddr+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
728 for (std::vector<ZLine*>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
729 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->ipaddr+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
732 void XLineManager::stats_e(userrec* user, string_list &results)
734 std::string sn = ServerInstance->Config->ServerName;
735 for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
736 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
737 for (std::vector<ELine*>::iterator i = pelines.begin(); i != pelines.end(); i++)
738 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
741 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance)