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, void** values, int* types)
57 char* reason = (char*)values[0];
58 char* ipmask = (char*)values[1];
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, void** values, int* types)
67 char* reason = (char*)values[0];
68 char* nick = (char*)values[1];
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, void** values, int* types)
77 char* reason = (char*)values[0];
78 char* host = (char*)values[1];
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, void** values, int* types)
87 char* reason = (char*)values[0];
88 char* host = (char*)values[1];
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;
441 for (std::vector<GLine*>::iterator i = pglines.begin(); i != pglines.end(); i++)
443 if (!strcasecmp(host,(*i)->hostmask))
445 (*i)->set_time = create_time;
452 void XLineManager::eline_set_creation_time(const char* host, time_t create_time)
454 for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
456 if (!strcasecmp(host,(*i)->hostmask))
458 (*i)->set_time = create_time;
462 for (std::vector<ELine*>::iterator i = pelines.begin(); i != pelines.end(); i++)
464 if (!strcasecmp(host,(*i)->hostmask))
466 (*i)->set_time = create_time;
473 void XLineManager::qline_set_creation_time(const char* nick, time_t create_time)
475 for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
477 if (!strcasecmp(nick,(*i)->nick))
479 (*i)->set_time = create_time;
483 for (std::vector<QLine*>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
485 if (!strcasecmp(nick,(*i)->nick))
487 (*i)->set_time = create_time;
494 void XLineManager::zline_set_creation_time(const char* ip, time_t create_time)
496 for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
498 if (!strcasecmp(ip,(*i)->ipaddr))
500 (*i)->set_time = create_time;
504 for (std::vector<ZLine*>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
506 if (!strcasecmp(ip,(*i)->ipaddr))
508 (*i)->set_time = create_time;
515 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
517 ZLine* XLineManager::matches_zline(const char* ipaddr)
519 if ((zlines.empty()) && (pzlines.empty()))
521 for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
522 if (match(ipaddr,(*i)->ipaddr, true))
524 for (std::vector<ZLine*>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
525 if (match(ipaddr,(*i)->ipaddr, true))
530 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
532 KLine* XLineManager::matches_kline(userrec* user)
534 if ((klines.empty()) && (pklines.empty()))
536 for (std::vector<KLine*>::iterator i = klines.begin(); i != klines.end(); i++)
538 if ((match(user->ident,(*i)->identmask)))
540 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
546 for (std::vector<KLine*>::iterator i = pklines.begin(); i != pklines.end(); i++)
548 if ((match(user->ident,(*i)->identmask)))
550 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
559 bool XLineManager::GSortComparison ( const GLine* one, const GLine* two )
561 return (one->expiry) < (two->expiry);
564 bool XLineManager::ESortComparison ( const ELine* one, const ELine* two )
566 return (one->expiry) < (two->expiry);
569 bool XLineManager::ZSortComparison ( const ZLine* one, const ZLine* two )
571 return (one->expiry) < (two->expiry);
574 bool XLineManager::KSortComparison ( const KLine* one, const KLine* two )
576 return (one->expiry) < (two->expiry);
579 bool XLineManager::QSortComparison ( const QLine* one, const QLine* two )
581 return (one->expiry) < (two->expiry);
584 // removes lines that have expired
586 void XLineManager::expire_lines()
588 time_t current = ServerInstance->Time();
590 /* Because we now store all our XLines in sorted order using ((*i)->duration + (*i)->set_time) as a key, this
591 * means that to expire the XLines we just need to do a while, picking off the top few until there are
592 * none left at the head of the queue that are after the current time.
595 while ((glines.size()) && (current > (*glines.begin())->expiry))
597 std::vector<GLine*>::iterator i = glines.begin();
598 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s (set by %s %d seconds ago)",(*i)->hostmask,(*i)->source,(*i)->duration);
602 while ((elines.size()) && (current > (*elines.begin())->expiry))
604 std::vector<ELine*>::iterator i = elines.begin();
605 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed E-Line %s (set by %s %d seconds ago)",(*i)->hostmask,(*i)->source,(*i)->duration);
609 while ((zlines.size()) && (current > (*zlines.begin())->expiry))
611 std::vector<ZLine*>::iterator i = zlines.begin();
612 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Z-Line %s (set by %s %d seconds ago)",(*i)->ipaddr,(*i)->source,(*i)->duration);
616 while ((klines.size()) && (current > (*klines.begin())->expiry))
618 std::vector<KLine*>::iterator i = klines.begin();
619 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed K-Line %s (set by %s %d seconds ago)",(*i)->hostmask,(*i)->source,(*i)->duration);
623 while ((qlines.size()) && (current > (*qlines.begin())->expiry))
625 std::vector<QLine*>::iterator i = qlines.begin();
626 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Q-Line %s (set by %s %d seconds ago)",(*i)->nick,(*i)->source,(*i)->duration);
632 // applies lines, removing clients and changing nicks etc as applicable
634 void XLineManager::apply_lines(const int What)
638 if ((!glines.size()) && (!klines.size()) && (!zlines.size()) && (!qlines.size()) &&
639 (!pglines.size()) && (!pklines.size()) && (!pzlines.size()) && (!pqlines.size()))
642 CullList* Goners = new CullList(ServerInstance);
644 for (std::vector<userrec*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
646 userrec* u = (userrec*)(*u2);
648 if (elines.size() || pelines.size())
650 // ignore people matching exempts
651 if (matches_exception(u))
654 if ((What & APPLY_GLINES) && (glines.size() || pglines.size()))
656 if ((check = matches_gline(u)))
658 snprintf(reason,MAXBUF,"G-Lined: %s",check->reason);
659 Goners->AddItem(u,reason);
662 if ((What & APPLY_KLINES) && (klines.size() || pklines.size()))
664 if ((check = matches_kline(u)))
666 snprintf(reason,MAXBUF,"K-Lined: %s",check->reason);
667 Goners->AddItem(u,reason);
670 if ((What & APPLY_QLINES) && (qlines.size() || pqlines.size()))
672 if ((check = matches_qline(u->nick)))
674 snprintf(reason,MAXBUF,"Q-Lined: %s",check->reason);
675 Goners->AddItem(u,reason);
678 if ((What & APPLY_ZLINES) && (zlines.size() || pzlines.size()))
680 if ((check = matches_zline(u->GetIPString())))
682 snprintf(reason,MAXBUF,"Z-Lined: %s",check->reason);
683 Goners->AddItem(u,reason);
692 void XLineManager::stats_k(userrec* user, string_list &results)
694 std::string sn = ServerInstance->Config->ServerName;
695 for (std::vector<KLine*>::iterator i = klines.begin(); i != klines.end(); i++)
696 results.push_back(sn+" 216 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
697 for (std::vector<KLine*>::iterator i = pklines.begin(); i != pklines.end(); i++)
698 results.push_back(sn+" 216 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
701 void XLineManager::stats_g(userrec* user, string_list &results)
703 std::string sn = ServerInstance->Config->ServerName;
704 for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
705 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
706 for (std::vector<GLine*>::iterator i = pglines.begin(); i != pglines.end(); i++)
707 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
710 void XLineManager::stats_q(userrec* user, string_list &results)
712 std::string sn = ServerInstance->Config->ServerName;
713 for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
714 results.push_back(sn+" 217 "+user->nick+" :"+(*i)->nick+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
715 for (std::vector<QLine*>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
716 results.push_back(sn+" 217 "+user->nick+" :"+(*i)->nick+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
719 void XLineManager::stats_z(userrec* user, string_list &results)
721 std::string sn = ServerInstance->Config->ServerName;
722 for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
723 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->ipaddr+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
724 for (std::vector<ZLine*>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
725 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->ipaddr+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
728 void XLineManager::stats_e(userrec* user, string_list &results)
730 std::string sn = ServerInstance->Config->ServerName;
731 for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
732 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
733 for (std::vector<ELine*>::iterator i = pelines.begin(); i != pelines.end(); i++)
734 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
737 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance)