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 DoneXLine(ServerConfig* conf, const char* tag)
46 conf->GetInstance()->XLines->apply_lines(APPLY_ALL);
50 bool DoZLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
52 const char* reason = values[0].GetString();
53 const char* ipmask = values[1].GetString();
55 conf->GetInstance()->XLines->add_zline(0,"<Config>",reason,ipmask);
56 conf->GetInstance()->Log(DEBUG,"Read Z line (badip tag): ipmask=%s reason=%s",ipmask,reason);
60 bool DoQLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
62 const char* reason = values[0].GetString();
63 const char* nick = values[1].GetString();
65 conf->GetInstance()->XLines->add_qline(0,"<Config>",reason,nick);
66 conf->GetInstance()->Log(DEBUG,"Read Q line (badnick tag): nick=%s reason=%s",nick,reason);
70 bool DoKLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
72 const char* reason = values[0].GetString();
73 const char* host = values[1].GetString();
75 conf->GetInstance()->XLines->add_kline(0,"<Config>",reason,host);
76 conf->GetInstance()->Log(DEBUG,"Read K line (badhost tag): host=%s reason=%s",host,reason);
80 bool DoELine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
82 const char* reason = values[0].GetString();
83 const char* host = values[1].GetString();
85 conf->GetInstance()->XLines->add_eline(0,"<Config>",reason,host);
86 conf->GetInstance()->Log(DEBUG,"Read E line (exception tag): host=%s reason=%s",host,reason);
90 IdentHostPair XLineManager::IdentSplit(const std::string &ident_and_host)
92 IdentHostPair n = std::make_pair<std::string,std::string>("*","*");
93 std::string::size_type x = ident_and_host.find('@');
94 if (x != std::string::npos)
96 n.second = ident_and_host.substr(x + 1,ident_and_host.length());
97 n.first = ident_and_host.substr(0, x);
98 if (!n.first.length())
100 if (!n.second.length())
105 n.second = ident_and_host;
113 bool XLineManager::add_gline(long duration, const char* source,const char* reason,const char* hostmask)
115 IdentHostPair ih = IdentSplit(hostmask);
117 bool ret = del_gline(hostmask);
119 GLine* item = new GLine(ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());
123 glines.push_back(item);
124 sort(glines.begin(), glines.end(),XLineManager::GSortComparison);
128 pglines.push_back(item);
134 // adds an e:line (exception to bans)
136 bool XLineManager::add_eline(long duration, const char* source, const char* reason, const char* hostmask)
138 IdentHostPair ih = IdentSplit(hostmask);
140 bool ret = del_eline(hostmask);
142 ELine* item = new ELine(ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());
146 elines.push_back(item);
147 sort(elines.begin(), elines.end(),XLineManager::ESortComparison);
151 pelines.push_back(item);
158 bool XLineManager::add_qline(long duration, const char* source, const char* reason, const char* nickname)
160 bool ret = del_qline(nickname);
161 QLine* item = new QLine(ServerInstance->Time(), duration, source, reason, nickname);
165 qlines.push_back(item);
166 sort(qlines.begin(), qlines.end(),XLineManager::QSortComparison);
170 pqlines.push_back(item);
177 bool XLineManager::add_zline(long duration, const char* source, const char* reason, const char* ipaddr)
179 bool ret = del_zline(ipaddr);
181 if (strchr(ipaddr,'@'))
183 while (*ipaddr != '@')
188 ZLine* item = new ZLine(ServerInstance->Time(), duration, source, reason, ipaddr);
192 zlines.push_back(item);
193 sort(zlines.begin(), zlines.end(),XLineManager::ZSortComparison);
197 pzlines.push_back(item);
204 bool XLineManager::add_kline(long duration, const char* source, const char* reason, const char* hostmask)
206 IdentHostPair ih = IdentSplit(hostmask);
208 bool ret = del_kline(hostmask);
210 KLine* item = new KLine(ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());
214 klines.push_back(item);
215 sort(klines.begin(), klines.end(),XLineManager::KSortComparison);
219 pklines.push_back(item);
224 // deletes a g:line, returns true if the line existed and was removed
226 bool XLineManager::del_gline(const char* hostmask)
228 IdentHostPair ih = IdentSplit(hostmask);
229 for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
231 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
238 for (std::vector<GLine*>::iterator i = pglines.begin(); i != pglines.end(); i++)
240 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
250 // deletes a e:line, returns true if the line existed and was removed
252 bool XLineManager::del_eline(const char* hostmask)
254 IdentHostPair ih = IdentSplit(hostmask);
255 for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
257 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
264 for (std::vector<ELine*>::iterator i = pelines.begin(); i != pelines.end(); i++)
266 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
276 // deletes a q:line, returns true if the line existed and was removed
278 bool XLineManager::del_qline(const char* nickname)
280 for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
282 if (!strcasecmp(nickname,(*i)->nick))
289 for (std::vector<QLine*>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
291 if (!strcasecmp(nickname,(*i)->nick))
301 // deletes a z:line, returns true if the line existed and was removed
303 bool XLineManager::del_zline(const char* ipaddr)
305 for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
307 if (!strcasecmp(ipaddr,(*i)->ipaddr))
314 for (std::vector<ZLine*>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
316 if (!strcasecmp(ipaddr,(*i)->ipaddr))
326 // deletes a k:line, returns true if the line existed and was removed
328 bool XLineManager::del_kline(const char* hostmask)
330 IdentHostPair ih = IdentSplit(hostmask);
331 for (std::vector<KLine*>::iterator i = klines.begin(); i != klines.end(); i++)
333 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
340 for (std::vector<KLine*>::iterator i = pklines.begin(); i != pklines.end(); i++)
342 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
352 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
354 QLine* XLineManager::matches_qline(const char* nick)
356 if ((qlines.empty()) && (pqlines.empty()))
358 for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
359 if (match(nick,(*i)->nick))
361 for (std::vector<QLine*>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
362 if (match(nick,(*i)->nick))
367 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
369 GLine* XLineManager::matches_gline(userrec* user)
371 if ((glines.empty()) && (pglines.empty()))
373 for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
375 if ((match(user->ident,(*i)->identmask)))
377 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
383 for (std::vector<GLine*>::iterator i = pglines.begin(); i != pglines.end(); i++)
385 if ((match(user->ident,(*i)->identmask)))
387 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
396 ELine* XLineManager::matches_exception(userrec* user)
398 if ((elines.empty()) && (pelines.empty()))
401 snprintf(host2,MAXBUF,"*@%s",user->host);
402 for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
404 if ((match(user->ident,(*i)->identmask)))
406 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
412 for (std::vector<ELine*>::iterator i = pelines.begin(); i != pelines.end(); i++)
414 if ((match(user->ident,(*i)->identmask)))
416 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
426 void XLineManager::gline_set_creation_time(const char* host, time_t create_time)
428 for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
430 if (!strcasecmp(host,(*i)->hostmask))
432 (*i)->set_time = create_time;
433 (*i)->expiry = create_time + (*i)->duration;
437 for (std::vector<GLine*>::iterator i = pglines.begin(); i != pglines.end(); i++)
439 if (!strcasecmp(host,(*i)->hostmask))
441 (*i)->set_time = create_time;
448 void XLineManager::eline_set_creation_time(const char* host, time_t create_time)
450 for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
452 if (!strcasecmp(host,(*i)->hostmask))
454 (*i)->set_time = create_time;
455 (*i)->expiry = create_time + (*i)->duration;
459 for (std::vector<ELine*>::iterator i = pelines.begin(); i != pelines.end(); i++)
461 if (!strcasecmp(host,(*i)->hostmask))
463 (*i)->set_time = create_time;
470 void XLineManager::qline_set_creation_time(const char* nick, time_t create_time)
472 for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
474 if (!strcasecmp(nick,(*i)->nick))
476 (*i)->set_time = create_time;
477 (*i)->expiry = create_time + (*i)->duration;
481 for (std::vector<QLine*>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
483 if (!strcasecmp(nick,(*i)->nick))
485 (*i)->set_time = create_time;
492 void XLineManager::zline_set_creation_time(const char* ip, time_t create_time)
494 for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
496 if (!strcasecmp(ip,(*i)->ipaddr))
498 (*i)->set_time = create_time;
499 (*i)->expiry = create_time + (*i)->duration;
503 for (std::vector<ZLine*>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
505 if (!strcasecmp(ip,(*i)->ipaddr))
507 (*i)->set_time = create_time;
514 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
516 ZLine* XLineManager::matches_zline(const char* ipaddr)
518 if ((zlines.empty()) && (pzlines.empty()))
520 for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
521 if (match(ipaddr,(*i)->ipaddr, true))
523 for (std::vector<ZLine*>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
524 if (match(ipaddr,(*i)->ipaddr, true))
529 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
531 KLine* XLineManager::matches_kline(userrec* user)
533 if ((klines.empty()) && (pklines.empty()))
535 for (std::vector<KLine*>::iterator i = klines.begin(); i != klines.end(); i++)
537 if ((match(user->ident,(*i)->identmask)))
539 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
545 for (std::vector<KLine*>::iterator i = pklines.begin(); i != pklines.end(); i++)
547 if ((match(user->ident,(*i)->identmask)))
549 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
558 bool XLineManager::GSortComparison ( const GLine* one, const GLine* two )
560 return (one->expiry) < (two->expiry);
563 bool XLineManager::ESortComparison ( const ELine* one, const ELine* two )
565 return (one->expiry) < (two->expiry);
568 bool XLineManager::ZSortComparison ( const ZLine* one, const ZLine* two )
570 return (one->expiry) < (two->expiry);
573 bool XLineManager::KSortComparison ( const KLine* one, const KLine* two )
575 return (one->expiry) < (two->expiry);
578 bool XLineManager::QSortComparison ( const QLine* one, const QLine* two )
580 return (one->expiry) < (two->expiry);
583 // removes lines that have expired
585 void XLineManager::expire_lines()
587 time_t current = ServerInstance->Time();
589 /* Because we now store all our XLines in sorted order using ((*i)->duration + (*i)->set_time) as a key, this
590 * means that to expire the XLines we just need to do a while, picking off the top few until there are
591 * none left at the head of the queue that are after the current time.
594 while ((glines.size()) && (current > (*glines.begin())->expiry))
596 std::vector<GLine*>::iterator i = glines.begin();
597 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s@%s (set by %s %d seconds ago)",(*i)->identmask,(*i)->hostmask,(*i)->source,(*i)->duration);
601 while ((elines.size()) && (current > (*elines.begin())->expiry))
603 std::vector<ELine*>::iterator i = elines.begin();
604 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed E-Line %s@%s (set by %s %d seconds ago)",(*i)->identmask,(*i)->hostmask,(*i)->source,(*i)->duration);
608 while ((zlines.size()) && (current > (*zlines.begin())->expiry))
610 std::vector<ZLine*>::iterator i = zlines.begin();
611 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Z-Line %s (set by %s %d seconds ago)",(*i)->ipaddr,(*i)->source,(*i)->duration);
615 while ((klines.size()) && (current > (*klines.begin())->expiry))
617 std::vector<KLine*>::iterator i = klines.begin();
618 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed K-Line %s@%s (set by %s %d seconds ago)",(*i)->identmask,(*i)->hostmask,(*i)->source,(*i)->duration);
622 while ((qlines.size()) && (current > (*qlines.begin())->expiry))
624 std::vector<QLine*>::iterator i = qlines.begin();
625 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Q-Line %s (set by %s %d seconds ago)",(*i)->nick,(*i)->source,(*i)->duration);
631 // applies lines, removing clients and changing nicks etc as applicable
633 void XLineManager::apply_lines(const int What)
637 if ((!glines.size()) && (!klines.size()) && (!zlines.size()) && (!qlines.size()) &&
638 (!pglines.size()) && (!pklines.size()) && (!pzlines.size()) && (!pqlines.size()))
641 CullList* Goners = new CullList(ServerInstance);
643 for (std::vector<userrec*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
645 userrec* u = (userrec*)(*u2);
647 if (elines.size() || pelines.size())
649 // ignore people matching exempts
650 if (matches_exception(u))
653 if ((What & APPLY_GLINES) && (glines.size() || pglines.size()))
655 if ((check = matches_gline(u)))
657 snprintf(reason,MAXBUF,"G-Lined: %s",check->reason);
658 Goners->AddItem(u,reason);
661 if ((What & APPLY_KLINES) && (klines.size() || pklines.size()))
663 if ((check = matches_kline(u)))
665 snprintf(reason,MAXBUF,"K-Lined: %s",check->reason);
666 Goners->AddItem(u,reason);
669 if ((What & APPLY_QLINES) && (qlines.size() || pqlines.size()))
671 if ((check = matches_qline(u->nick)))
673 snprintf(reason,MAXBUF,"Q-Lined: %s",check->reason);
674 Goners->AddItem(u,reason);
677 if ((What & APPLY_ZLINES) && (zlines.size() || pzlines.size()))
679 if ((check = matches_zline(u->GetIPString())))
681 snprintf(reason,MAXBUF,"Z-Lined: %s",check->reason);
682 Goners->AddItem(u,reason);
691 void XLineManager::stats_k(userrec* user, string_list &results)
693 std::string sn = ServerInstance->Config->ServerName;
694 for (std::vector<KLine*>::iterator i = klines.begin(); i != klines.end(); i++)
695 results.push_back(sn+" 216 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
696 for (std::vector<KLine*>::iterator i = pklines.begin(); i != pklines.end(); i++)
697 results.push_back(sn+" 216 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
700 void XLineManager::stats_g(userrec* user, string_list &results)
702 std::string sn = ServerInstance->Config->ServerName;
703 for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
704 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
705 for (std::vector<GLine*>::iterator i = pglines.begin(); i != pglines.end(); i++)
706 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
709 void XLineManager::stats_q(userrec* user, string_list &results)
711 std::string sn = ServerInstance->Config->ServerName;
712 for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
713 results.push_back(sn+" 217 "+user->nick+" :"+(*i)->nick+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
714 for (std::vector<QLine*>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
715 results.push_back(sn+" 217 "+user->nick+" :"+(*i)->nick+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
718 void XLineManager::stats_z(userrec* user, string_list &results)
720 std::string sn = ServerInstance->Config->ServerName;
721 for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
722 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->ipaddr+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
723 for (std::vector<ZLine*>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
724 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->ipaddr+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
727 void XLineManager::stats_e(userrec* user, string_list &results)
729 std::string sn = ServerInstance->Config->ServerName;
730 for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
731 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
732 for (std::vector<ELine*>::iterator i = pelines.begin(); i != pelines.end(); i++)
733 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
736 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance)