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);
97 bool XLineManager::add_gline(long duration, const char* source,const char* reason,const char* hostmask)
99 bool ret = del_gline(hostmask);
102 item.duration = duration;
103 strlcpy(item.hostmask,hostmask,199);
104 strlcpy(item.reason,reason,MAXBUF);
105 strlcpy(item.source,source,255);
107 item.set_time = ServerInstance->Time();
111 glines.push_back(item);
112 sort(glines.begin(), glines.end(),XLineManager::GSortComparison);
116 pglines.push_back(item);
122 // adds an e:line (exception to bans)
124 bool XLineManager::add_eline(long duration, const char* source, const char* reason, const char* hostmask)
126 bool ret = del_eline(hostmask);
128 item.duration = duration;
129 strlcpy(item.hostmask,hostmask,199);
130 strlcpy(item.reason,reason,MAXBUF);
131 strlcpy(item.source,source,255);
133 item.set_time = ServerInstance->Time();
136 elines.push_back(item);
137 sort(elines.begin(), elines.end(),XLineManager::ESortComparison);
141 pelines.push_back(item);
148 bool XLineManager::add_qline(long duration, const char* source, const char* reason, const char* nickname)
150 bool ret = del_qline(nickname);
152 item.duration = duration;
153 strlcpy(item.nick,nickname,63);
154 strlcpy(item.reason,reason,MAXBUF);
155 strlcpy(item.source,source,255);
157 item.is_global = false;
158 item.set_time = ServerInstance->Time();
161 qlines.push_back(item);
162 sort(qlines.begin(), qlines.end(),XLineManager::QSortComparison);
166 pqlines.push_back(item);
173 bool XLineManager::add_zline(long duration, const char* source, const char* reason, const char* ipaddr)
175 bool ret = del_zline(ipaddr);
177 item.duration = duration;
178 if (strchr(ipaddr,'@'))
180 while (*ipaddr != '@')
184 strlcpy(item.ipaddr,ipaddr,39);
185 strlcpy(item.reason,reason,MAXBUF);
186 strlcpy(item.source,source,255);
188 item.is_global = false;
189 item.set_time = ServerInstance->Time();
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 bool ret = del_kline(hostmask);
208 item.duration = duration;
209 strlcpy(item.hostmask,hostmask,200);
210 strlcpy(item.reason,reason,MAXBUF);
211 strlcpy(item.source,source,255);
213 item.set_time = ServerInstance->Time();
216 klines.push_back(item);
217 sort(klines.begin(), klines.end(),XLineManager::KSortComparison);
221 pklines.push_back(item);
226 // deletes a g:line, returns true if the line existed and was removed
228 bool XLineManager::del_gline(const char* hostmask)
230 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
232 if (!strcasecmp(hostmask,i->hostmask))
238 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
240 if (!strcasecmp(hostmask,i->hostmask))
249 // deletes a e:line, returns true if the line existed and was removed
251 bool XLineManager::del_eline(const char* hostmask)
253 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
255 if (!strcasecmp(hostmask,i->hostmask))
261 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
263 if (!strcasecmp(hostmask,i->hostmask))
272 // deletes a q:line, returns true if the line existed and was removed
274 bool XLineManager::del_qline(const char* nickname)
276 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
278 if (!strcasecmp(nickname,i->nick))
284 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
286 if (!strcasecmp(nickname,i->nick))
295 bool XLineManager::qline_make_global(const char* nickname)
297 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
299 if (!strcasecmp(nickname,i->nick))
308 bool XLineManager::zline_make_global(const char* ipaddr)
310 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
312 if (!strcasecmp(ipaddr,i->ipaddr))
321 // deletes a z:line, returns true if the line existed and was removed
323 bool XLineManager::del_zline(const char* ipaddr)
325 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
327 if (!strcasecmp(ipaddr,i->ipaddr))
333 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
335 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 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
350 if (!strcasecmp(hostmask,i->hostmask))
356 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
358 if (!strcasecmp(hostmask,i->hostmask))
367 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
369 char* XLineManager::matches_qline(const char* nick)
371 if ((qlines.empty()) && (pqlines.empty()))
373 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
374 if (match(nick,i->nick))
376 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
377 if (match(nick,i->nick))
382 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
384 char* XLineManager::matches_gline(const char* host)
386 if ((glines.empty()) && (pglines.empty()))
388 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
389 if (match(host,i->hostmask, true))
391 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
392 if (match(host,i->hostmask, true))
397 char* XLineManager::matches_exception(const char* host)
399 if ((elines.empty()) && (pelines.empty()))
402 snprintf(host2,MAXBUF,"*@%s",host);
403 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
404 if ((match(host,i->hostmask)) || (match(host2,i->hostmask, true)))
406 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
407 if ((match(host,i->hostmask)) || (match(host2,i->hostmask, true)))
413 void XLineManager::gline_set_creation_time(const char* host, time_t create_time)
415 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
417 if (!strcasecmp(host,i->hostmask))
419 i->set_time = create_time;
423 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
425 if (!strcasecmp(host,i->hostmask))
427 i->set_time = create_time;
434 void XLineManager::eline_set_creation_time(const char* host, time_t create_time)
436 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
438 if (!strcasecmp(host,i->hostmask))
440 i->set_time = create_time;
444 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
446 if (!strcasecmp(host,i->hostmask))
448 i->set_time = create_time;
455 void XLineManager::qline_set_creation_time(const char* nick, time_t create_time)
457 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
459 if (!strcasecmp(nick,i->nick))
461 i->set_time = create_time;
465 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
467 if (!strcasecmp(nick,i->nick))
469 i->set_time = create_time;
476 void XLineManager::zline_set_creation_time(const char* ip, time_t create_time)
478 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
480 if (!strcasecmp(ip,i->ipaddr))
482 i->set_time = create_time;
486 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
488 if (!strcasecmp(ip,i->ipaddr))
490 i->set_time = create_time;
497 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
499 char* XLineManager::matches_zline(const char* ipaddr)
501 if ((zlines.empty()) && (pzlines.empty()))
503 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
504 if (match(ipaddr,i->ipaddr, true))
506 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
507 if (match(ipaddr,i->ipaddr, true))
512 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
514 char* XLineManager::matches_kline(const char* host)
516 if ((klines.empty()) && (pklines.empty()))
518 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
519 if (match(host,i->hostmask, true))
521 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
522 if (match(host,i->hostmask, true))
527 bool XLineManager::GSortComparison ( const GLine one, const GLine two )
529 return (one.duration + one.set_time) < (two.duration + two.set_time);
532 bool XLineManager::ESortComparison ( const ELine one, const ELine two )
534 return (one.duration + one.set_time) < (two.duration + two.set_time);
537 bool XLineManager::ZSortComparison ( const ZLine one, const ZLine two )
539 return (one.duration + one.set_time) < (two.duration + two.set_time);
542 bool XLineManager::KSortComparison ( const KLine one, const KLine two )
544 return (one.duration + one.set_time) < (two.duration + two.set_time);
547 bool XLineManager::QSortComparison ( const QLine one, const QLine two )
549 return (one.duration + one.set_time) < (two.duration + two.set_time);
552 // removes lines that have expired
554 void XLineManager::expire_lines()
556 time_t current = ServerInstance->Time();
558 /* Because we now store all our XLines in sorted order using (i->duration + i->set_time) as a key, this
559 * means that to expire the XLines we just need to do a while, picking off the top few until there are
560 * none left at the head of the queue that are after the current time.
563 while ((glines.size()) && (current > (glines.begin()->duration + glines.begin()->set_time)))
565 std::vector<GLine>::iterator i = glines.begin();
566 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
570 while ((elines.size()) && (current > (elines.begin()->duration + elines.begin()->set_time)))
572 std::vector<ELine>::iterator i = elines.begin();
573 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed E-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
577 while ((zlines.size()) && (current > (zlines.begin()->duration + zlines.begin()->set_time)))
579 std::vector<ZLine>::iterator i = zlines.begin();
580 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Z-Line %s (set by %s %d seconds ago)",i->ipaddr,i->source,i->duration);
584 while ((klines.size()) && (current > (klines.begin()->duration + klines.begin()->set_time)))
586 std::vector<KLine>::iterator i = klines.begin();
587 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed K-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
591 while ((qlines.size()) && (current > (qlines.begin()->duration + qlines.begin()->set_time)))
593 std::vector<QLine>::iterator i = qlines.begin();
594 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Q-Line %s (set by %s %d seconds ago)",i->nick,i->source,i->duration);
600 // applies lines, removing clients and changing nicks etc as applicable
602 void XLineManager::apply_lines(const int What)
607 if ((!glines.size()) && (!klines.size()) && (!zlines.size()) && (!qlines.size()) &&
608 (!pglines.size()) && (!pklines.size()) && (!pzlines.size()) && (!pqlines.size()))
611 CullList* Goners = new CullList(ServerInstance);
613 for (std::vector<userrec*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
615 userrec* u = (userrec*)(*u2);
617 if (elines.size() || pelines.size())
619 // ignore people matching exempts
620 if (matches_exception(host))
623 if ((What & APPLY_GLINES) && (glines.size() || pglines.size()))
625 if ((check = matches_gline(host)))
627 snprintf(reason,MAXBUF,"G-Lined: %s",check);
628 Goners->AddItem(u,reason);
631 if ((What & APPLY_KLINES) && (klines.size() || pklines.size()))
633 if ((check = matches_kline(host)))
635 snprintf(reason,MAXBUF,"K-Lined: %s",check);
636 Goners->AddItem(u,reason);
639 if ((What & APPLY_QLINES) && (qlines.size() || pqlines.size()))
641 if ((check = matches_qline(u->nick)))
643 snprintf(reason,MAXBUF,"Q-Lined: %s",check);
644 Goners->AddItem(u,reason);
647 if ((What & APPLY_ZLINES) && (zlines.size() || pzlines.size()))
649 if ((check = matches_zline(u->GetIPString())))
651 snprintf(reason,MAXBUF,"Z-Lined: %s",check);
652 Goners->AddItem(u,reason);
661 void XLineManager::stats_k(userrec* user, string_list &results)
663 std::string sn = ServerInstance->Config->ServerName;
664 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
665 results.push_back(sn+" 216 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
666 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
667 results.push_back(sn+" 216 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
670 void XLineManager::stats_g(userrec* user, string_list &results)
672 std::string sn = ServerInstance->Config->ServerName;
673 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
674 results.push_back(sn+" 223 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
675 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
676 results.push_back(sn+" 223 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
679 void XLineManager::stats_q(userrec* user, string_list &results)
681 std::string sn = ServerInstance->Config->ServerName;
682 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
683 results.push_back(sn+" 217 "+user->nick+" :"+i->nick+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
684 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
685 results.push_back(sn+" 217 "+user->nick+" :"+i->nick+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
688 void XLineManager::stats_z(userrec* user, string_list &results)
690 std::string sn = ServerInstance->Config->ServerName;
691 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
692 results.push_back(sn+" 223 "+user->nick+" :"+i->ipaddr+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
693 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
694 results.push_back(sn+" 223 "+user->nick+" :"+i->ipaddr+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
697 void XLineManager::stats_e(userrec* user, string_list &results)
699 std::string sn = ServerInstance->Config->ServerName;
700 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
701 results.push_back(sn+" 223 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
702 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
703 results.push_back(sn+" 223 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
706 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance)