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);
125 item.duration = duration;
126 strlcpy(item.identmask,ih.first.c_str(),19);
127 strlcpy(item.hostmask,ih.second.c_str(),199);
128 strlcpy(item.reason,reason,MAXBUF);
129 strlcpy(item.source,source,255);
131 item.set_time = ServerInstance->Time();
135 glines.push_back(item);
136 sort(glines.begin(), glines.end(),XLineManager::GSortComparison);
140 pglines.push_back(item);
146 // adds an e:line (exception to bans)
148 bool XLineManager::add_eline(long duration, const char* source, const char* reason, const char* hostmask)
150 IdentHostPair ih = IdentSplit(hostmask);
152 bool ret = del_eline(hostmask);
155 item.duration = duration;
156 strlcpy(item.identmask,ih.first.c_str(),19);
157 strlcpy(item.hostmask,ih.second.c_str(),199);
158 strlcpy(item.reason,reason,MAXBUF);
159 strlcpy(item.source,source,255);
161 item.set_time = ServerInstance->Time();
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);
180 item.duration = duration;
181 strlcpy(item.nick,nickname,63);
182 strlcpy(item.reason,reason,MAXBUF);
183 strlcpy(item.source,source,255);
185 item.is_global = false;
186 item.set_time = ServerInstance->Time();
189 qlines.push_back(item);
190 sort(qlines.begin(), qlines.end(),XLineManager::QSortComparison);
194 pqlines.push_back(item);
201 bool XLineManager::add_zline(long duration, const char* source, const char* reason, const char* ipaddr)
203 bool ret = del_zline(ipaddr);
205 item.duration = duration;
206 if (strchr(ipaddr,'@'))
208 while (*ipaddr != '@')
212 strlcpy(item.ipaddr,ipaddr,39);
213 strlcpy(item.reason,reason,MAXBUF);
214 strlcpy(item.source,source,255);
216 item.is_global = false;
217 item.set_time = ServerInstance->Time();
220 zlines.push_back(item);
221 sort(zlines.begin(), zlines.end(),XLineManager::ZSortComparison);
225 pzlines.push_back(item);
232 bool XLineManager::add_kline(long duration, const char* source, const char* reason, const char* hostmask)
234 IdentHostPair ih = IdentSplit(hostmask);
236 bool ret = del_kline(hostmask);
239 item.duration = duration;
240 strlcpy(item.identmask,ih.first.c_str(),19);
241 strlcpy(item.hostmask,ih.second.c_str(),200);
242 strlcpy(item.reason,reason,MAXBUF);
243 strlcpy(item.source,source,255);
245 item.set_time = ServerInstance->Time();
248 klines.push_back(item);
249 sort(klines.begin(), klines.end(),XLineManager::KSortComparison);
253 pklines.push_back(item);
258 // deletes a g:line, returns true if the line existed and was removed
260 bool XLineManager::del_gline(const char* hostmask)
262 IdentHostPair ih = IdentSplit(hostmask);
263 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
265 if (!strcasecmp(ih.first.c_str(),i->identmask) && !strcasecmp(ih.second.c_str(),i->hostmask))
271 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
273 if (!strcasecmp(ih.first.c_str(),i->identmask) && !strcasecmp(ih.second.c_str(),i->hostmask))
282 // deletes a e:line, returns true if the line existed and was removed
284 bool XLineManager::del_eline(const char* hostmask)
286 IdentHostPair ih = IdentSplit(hostmask);
287 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
289 if (!strcasecmp(ih.first.c_str(),i->identmask) && !strcasecmp(ih.second.c_str(),i->hostmask))
295 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
297 if (!strcasecmp(ih.first.c_str(),i->identmask) && !strcasecmp(ih.second.c_str(),i->hostmask))
306 // deletes a q:line, returns true if the line existed and was removed
308 bool XLineManager::del_qline(const char* nickname)
310 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
312 if (!strcasecmp(nickname,i->nick))
318 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
320 if (!strcasecmp(nickname,i->nick))
329 bool XLineManager::qline_make_global(const char* nickname)
331 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
333 if (!strcasecmp(nickname,i->nick))
342 bool XLineManager::zline_make_global(const char* ipaddr)
344 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
346 if (!strcasecmp(ipaddr,i->ipaddr))
355 // deletes a z:line, returns true if the line existed and was removed
357 bool XLineManager::del_zline(const char* ipaddr)
359 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
361 if (!strcasecmp(ipaddr,i->ipaddr))
367 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
369 if (!strcasecmp(ipaddr,i->ipaddr))
378 // deletes a k:line, returns true if the line existed and was removed
380 bool XLineManager::del_kline(const char* hostmask)
382 IdentHostPair ih = IdentSplit(hostmask);
383 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
385 if (!strcasecmp(ih.first.c_str(),i->identmask) && !strcasecmp(ih.second.c_str(),i->hostmask))
391 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
393 if (!strcasecmp(ih.first.c_str(),i->identmask) && !strcasecmp(ih.second.c_str(),i->hostmask))
402 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
404 char* XLineManager::matches_qline(const char* nick)
406 if ((qlines.empty()) && (pqlines.empty()))
408 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
409 if (match(nick,i->nick))
411 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
412 if (match(nick,i->nick))
417 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
419 char* XLineManager::matches_gline(userrec* user)
421 if ((glines.empty()) && (pglines.empty()))
423 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
425 if ((match(user->ident,i->identmask)))
427 if ((match(user->host,i->hostmask, true)) || (match(user->GetIPString(),i->hostmask, true)))
433 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
435 if ((match(user->ident,i->identmask)))
437 if ((match(user->host,i->hostmask, true)) || (match(user->GetIPString(),i->hostmask, true)))
446 char* XLineManager::matches_exception(userrec* user)
448 if ((elines.empty()) && (pelines.empty()))
451 snprintf(host2,MAXBUF,"*@%s",user->host);
452 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
454 if ((match(user->ident,i->identmask)))
456 if ((match(user->host,i->hostmask, true)) || (match(user->GetIPString(),i->hostmask, true)))
462 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
464 if ((match(user->ident,i->identmask)))
466 if ((match(user->host,i->hostmask, true)) || (match(user->GetIPString(),i->hostmask, true)))
476 void XLineManager::gline_set_creation_time(const char* host, time_t create_time)
478 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
480 if (!strcasecmp(host,i->hostmask))
482 i->set_time = create_time;
486 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
488 if (!strcasecmp(host,i->hostmask))
490 i->set_time = create_time;
497 void XLineManager::eline_set_creation_time(const char* host, time_t create_time)
499 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
501 if (!strcasecmp(host,i->hostmask))
503 i->set_time = create_time;
507 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
509 if (!strcasecmp(host,i->hostmask))
511 i->set_time = create_time;
518 void XLineManager::qline_set_creation_time(const char* nick, time_t create_time)
520 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
522 if (!strcasecmp(nick,i->nick))
524 i->set_time = create_time;
528 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
530 if (!strcasecmp(nick,i->nick))
532 i->set_time = create_time;
539 void XLineManager::zline_set_creation_time(const char* ip, time_t create_time)
541 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
543 if (!strcasecmp(ip,i->ipaddr))
545 i->set_time = create_time;
549 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
551 if (!strcasecmp(ip,i->ipaddr))
553 i->set_time = create_time;
560 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
562 char* XLineManager::matches_zline(const char* ipaddr)
564 if ((zlines.empty()) && (pzlines.empty()))
566 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
567 if (match(ipaddr,i->ipaddr, true))
569 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
570 if (match(ipaddr,i->ipaddr, true))
575 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
577 char* XLineManager::matches_kline(userrec* user)
579 if ((klines.empty()) && (pklines.empty()))
581 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
583 if ((match(user->ident,i->identmask)))
585 if ((match(user->host,i->hostmask, true)) || (match(user->GetIPString(),i->hostmask, true)))
591 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
593 if ((match(user->ident,i->identmask)))
595 if ((match(user->host,i->hostmask, true)) || (match(user->GetIPString(),i->hostmask, true)))
604 bool XLineManager::GSortComparison ( const GLine one, const GLine two )
606 return (one.duration + one.set_time) < (two.duration + two.set_time);
609 bool XLineManager::ESortComparison ( const ELine one, const ELine two )
611 return (one.duration + one.set_time) < (two.duration + two.set_time);
614 bool XLineManager::ZSortComparison ( const ZLine one, const ZLine two )
616 return (one.duration + one.set_time) < (two.duration + two.set_time);
619 bool XLineManager::KSortComparison ( const KLine one, const KLine two )
621 return (one.duration + one.set_time) < (two.duration + two.set_time);
624 bool XLineManager::QSortComparison ( const QLine one, const QLine two )
626 return (one.duration + one.set_time) < (two.duration + two.set_time);
629 // removes lines that have expired
631 void XLineManager::expire_lines()
633 time_t current = ServerInstance->Time();
635 /* Because we now store all our XLines in sorted order using (i->duration + i->set_time) as a key, this
636 * means that to expire the XLines we just need to do a while, picking off the top few until there are
637 * none left at the head of the queue that are after the current time.
640 while ((glines.size()) && (current > (glines.begin()->duration + glines.begin()->set_time)))
642 std::vector<GLine>::iterator i = glines.begin();
643 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
647 while ((elines.size()) && (current > (elines.begin()->duration + elines.begin()->set_time)))
649 std::vector<ELine>::iterator i = elines.begin();
650 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed E-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
654 while ((zlines.size()) && (current > (zlines.begin()->duration + zlines.begin()->set_time)))
656 std::vector<ZLine>::iterator i = zlines.begin();
657 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Z-Line %s (set by %s %d seconds ago)",i->ipaddr,i->source,i->duration);
661 while ((klines.size()) && (current > (klines.begin()->duration + klines.begin()->set_time)))
663 std::vector<KLine>::iterator i = klines.begin();
664 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed K-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
668 while ((qlines.size()) && (current > (qlines.begin()->duration + qlines.begin()->set_time)))
670 std::vector<QLine>::iterator i = qlines.begin();
671 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Q-Line %s (set by %s %d seconds ago)",i->nick,i->source,i->duration);
677 // applies lines, removing clients and changing nicks etc as applicable
679 void XLineManager::apply_lines(const int What)
683 if ((!glines.size()) && (!klines.size()) && (!zlines.size()) && (!qlines.size()) &&
684 (!pglines.size()) && (!pklines.size()) && (!pzlines.size()) && (!pqlines.size()))
687 CullList* Goners = new CullList(ServerInstance);
689 for (std::vector<userrec*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
691 userrec* u = (userrec*)(*u2);
693 if (elines.size() || pelines.size())
695 // ignore people matching exempts
696 if (matches_exception(u))
699 if ((What & APPLY_GLINES) && (glines.size() || pglines.size()))
701 if ((check = matches_gline(u)))
703 snprintf(reason,MAXBUF,"G-Lined: %s",check);
704 Goners->AddItem(u,reason);
707 if ((What & APPLY_KLINES) && (klines.size() || pklines.size()))
709 if ((check = matches_kline(u)))
711 snprintf(reason,MAXBUF,"K-Lined: %s",check);
712 Goners->AddItem(u,reason);
715 if ((What & APPLY_QLINES) && (qlines.size() || pqlines.size()))
717 if ((check = matches_qline(u->nick)))
719 snprintf(reason,MAXBUF,"Q-Lined: %s",check);
720 Goners->AddItem(u,reason);
723 if ((What & APPLY_ZLINES) && (zlines.size() || pzlines.size()))
725 if ((check = matches_zline(u->GetIPString())))
727 snprintf(reason,MAXBUF,"Z-Lined: %s",check);
728 Goners->AddItem(u,reason);
737 void XLineManager::stats_k(userrec* user, string_list &results)
739 std::string sn = ServerInstance->Config->ServerName;
740 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
741 results.push_back(sn+" 216 "+user->nick+" :"+i->identmask+"@"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
742 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
743 results.push_back(sn+" 216 "+user->nick+" :"+i->identmask+"@"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
746 void XLineManager::stats_g(userrec* user, string_list &results)
748 std::string sn = ServerInstance->Config->ServerName;
749 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
750 results.push_back(sn+" 223 "+user->nick+" :"+i->identmask+"@"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
751 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
752 results.push_back(sn+" 223 "+user->nick+" :"+i->identmask+"@"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
755 void XLineManager::stats_q(userrec* user, string_list &results)
757 std::string sn = ServerInstance->Config->ServerName;
758 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
759 results.push_back(sn+" 217 "+user->nick+" :"+i->nick+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
760 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
761 results.push_back(sn+" 217 "+user->nick+" :"+i->nick+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
764 void XLineManager::stats_z(userrec* user, string_list &results)
766 std::string sn = ServerInstance->Config->ServerName;
767 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
768 results.push_back(sn+" 223 "+user->nick+" :"+i->ipaddr+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
769 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
770 results.push_back(sn+" 223 "+user->nick+" :"+i->ipaddr+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
773 void XLineManager::stats_e(userrec* user, string_list &results)
775 std::string sn = ServerInstance->Config->ServerName;
776 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
777 results.push_back(sn+" 223 "+user->nick+" :"+i->identmask+"@"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
778 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
779 results.push_back(sn+" 223 "+user->nick+" :"+i->identmask+"@"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
782 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance)