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 * ---------------------------------------------------
19 #include "inspircd_config.h"
34 #include "inspstring.h"
35 #include "helperfuncs.h"
38 #include "configreader.h"
39 #include "cull_list.h"
41 /* Version two, now with optimized expiry!
43 * Because the old way was horrendously slow, the new way of expiring xlines is very
44 * very efficient. I have improved the efficiency of the algorithm in two ways:
46 * (1) There are now two lists of items for each linetype. One list holds temporary
47 * items, and the other list holds permanent items (ones which will expire).
48 * Items which are on the permanent list are NEVER checked at all by the
49 * expire_lines() function.
50 * (2) The temporary xline lists are always kept in strict numerical order, keyed by
51 * current time + duration. This means that the line which is due to expire the
52 * soonest is always pointed at by vector::begin(), so a simple while loop can
53 * very efficiently, very quickly and above all SAFELY pick off the first few
54 * items in the vector which need zapping.
59 bool InitXLine(ServerConfig* conf, const char* tag)
64 bool DoneXLine(ServerConfig* conf, const char* tag)
66 conf->GetInstance()->XLines->apply_lines(APPLY_ALL);
70 bool DoZLine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
72 char* reason = (char*)values[0];
73 char* ipmask = (char*)values[1];
75 conf->GetInstance()->XLines->add_zline(0,"<Config>",reason,ipmask);
76 conf->GetInstance()->Log(DEBUG,"Read Z line (badip tag): ipmask=%s reason=%s",ipmask,reason);
80 bool DoQLine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
82 char* reason = (char*)values[0];
83 char* nick = (char*)values[1];
85 conf->GetInstance()->XLines->add_qline(0,"<Config>",reason,nick);
86 conf->GetInstance()->Log(DEBUG,"Read Q line (badnick tag): nick=%s reason=%s",nick,reason);
90 bool DoKLine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
92 char* reason = (char*)values[0];
93 char* host = (char*)values[1];
95 conf->GetInstance()->XLines->add_kline(0,"<Config>",reason,host);
96 conf->GetInstance()->Log(DEBUG,"Read K line (badhost tag): host=%s reason=%s",host,reason);
100 bool DoELine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
102 char* reason = (char*)values[0];
103 char* host = (char*)values[1];
105 conf->GetInstance()->XLines->add_eline(0,"<Config>",reason,host);
106 conf->GetInstance()->Log(DEBUG,"Read E line (exception tag): host=%s reason=%s",host,reason);
112 bool XLineManager::add_gline(long duration, const char* source,const char* reason,const char* hostmask)
114 bool ret = del_gline(hostmask);
117 item.duration = duration;
118 strlcpy(item.hostmask,hostmask,199);
119 strlcpy(item.reason,reason,MAXBUF);
120 strlcpy(item.source,source,255);
122 item.set_time = ServerInstance->Time();
126 glines.push_back(item);
127 sort(glines.begin(), glines.end(),XLineManager::GSortComparison);
131 pglines.push_back(item);
137 // adds an e:line (exception to bans)
139 bool XLineManager::add_eline(long duration, const char* source, const char* reason, const char* hostmask)
141 bool ret = del_eline(hostmask);
143 item.duration = duration;
144 strlcpy(item.hostmask,hostmask,199);
145 strlcpy(item.reason,reason,MAXBUF);
146 strlcpy(item.source,source,255);
148 item.set_time = ServerInstance->Time();
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);
167 item.duration = duration;
168 strlcpy(item.nick,nickname,63);
169 strlcpy(item.reason,reason,MAXBUF);
170 strlcpy(item.source,source,255);
172 item.is_global = false;
173 item.set_time = ServerInstance->Time();
176 qlines.push_back(item);
177 sort(qlines.begin(), qlines.end(),XLineManager::QSortComparison);
181 pqlines.push_back(item);
188 bool XLineManager::add_zline(long duration, const char* source, const char* reason, const char* ipaddr)
190 bool ret = del_zline(ipaddr);
192 item.duration = duration;
193 if (strchr(ipaddr,'@'))
195 while (*ipaddr != '@')
199 strlcpy(item.ipaddr,ipaddr,39);
200 strlcpy(item.reason,reason,MAXBUF);
201 strlcpy(item.source,source,255);
203 item.is_global = false;
204 item.set_time = ServerInstance->Time();
207 zlines.push_back(item);
208 sort(zlines.begin(), zlines.end(),XLineManager::ZSortComparison);
212 pzlines.push_back(item);
219 bool XLineManager::add_kline(long duration, const char* source, const char* reason, const char* hostmask)
221 bool ret = del_kline(hostmask);
223 item.duration = duration;
224 strlcpy(item.hostmask,hostmask,200);
225 strlcpy(item.reason,reason,MAXBUF);
226 strlcpy(item.source,source,255);
228 item.set_time = ServerInstance->Time();
231 klines.push_back(item);
232 sort(klines.begin(), klines.end(),XLineManager::KSortComparison);
236 pklines.push_back(item);
241 // deletes a g:line, returns true if the line existed and was removed
243 bool XLineManager::del_gline(const char* hostmask)
245 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
247 if (!strcasecmp(hostmask,i->hostmask))
253 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
255 if (!strcasecmp(hostmask,i->hostmask))
264 // deletes a e:line, returns true if the line existed and was removed
266 bool XLineManager::del_eline(const char* hostmask)
268 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
270 if (!strcasecmp(hostmask,i->hostmask))
276 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
278 if (!strcasecmp(hostmask,i->hostmask))
287 // deletes a q:line, returns true if the line existed and was removed
289 bool XLineManager::del_qline(const char* nickname)
291 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
293 if (!strcasecmp(nickname,i->nick))
299 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
301 if (!strcasecmp(nickname,i->nick))
310 bool XLineManager::qline_make_global(const char* nickname)
312 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
314 if (!strcasecmp(nickname,i->nick))
323 bool XLineManager::zline_make_global(const char* ipaddr)
325 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
327 if (!strcasecmp(ipaddr,i->ipaddr))
336 // deletes a z:line, returns true if the line existed and was removed
338 bool XLineManager::del_zline(const char* ipaddr)
340 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
342 if (!strcasecmp(ipaddr,i->ipaddr))
348 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
350 if (!strcasecmp(ipaddr,i->ipaddr))
359 // deletes a k:line, returns true if the line existed and was removed
361 bool XLineManager::del_kline(const char* hostmask)
363 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
365 if (!strcasecmp(hostmask,i->hostmask))
371 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
373 if (!strcasecmp(hostmask,i->hostmask))
382 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
384 char* XLineManager::matches_qline(const char* nick)
386 if ((qlines.empty()) && (pqlines.empty()))
388 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
389 if (match(nick,i->nick))
391 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
392 if (match(nick,i->nick))
397 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
399 char* XLineManager::matches_gline(const char* host)
401 if ((glines.empty()) && (pglines.empty()))
403 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
404 if (match(host,i->hostmask, true))
406 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
407 if (match(host,i->hostmask, true))
412 char* XLineManager::matches_exception(const char* host)
414 if ((elines.empty()) && (pelines.empty()))
417 snprintf(host2,MAXBUF,"*@%s",host);
418 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
419 if ((match(host,i->hostmask)) || (match(host2,i->hostmask, true)))
421 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
422 if ((match(host,i->hostmask)) || (match(host2,i->hostmask, true)))
428 void XLineManager::gline_set_creation_time(const char* host, time_t create_time)
430 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
432 if (!strcasecmp(host,i->hostmask))
434 i->set_time = create_time;
438 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
440 if (!strcasecmp(host,i->hostmask))
442 i->set_time = create_time;
449 void XLineManager::eline_set_creation_time(const char* host, time_t create_time)
451 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
453 if (!strcasecmp(host,i->hostmask))
455 i->set_time = create_time;
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;
480 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
482 if (!strcasecmp(nick,i->nick))
484 i->set_time = create_time;
491 void XLineManager::zline_set_creation_time(const char* ip, time_t create_time)
493 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
495 if (!strcasecmp(ip,i->ipaddr))
497 i->set_time = create_time;
501 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
503 if (!strcasecmp(ip,i->ipaddr))
505 i->set_time = create_time;
512 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
514 char* XLineManager::matches_zline(const char* ipaddr)
516 if ((zlines.empty()) && (pzlines.empty()))
518 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
519 if (match(ipaddr,i->ipaddr, true))
521 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
522 if (match(ipaddr,i->ipaddr, true))
527 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
529 char* XLineManager::matches_kline(const char* host)
531 if ((klines.empty()) && (pklines.empty()))
533 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
534 if (match(host,i->hostmask, true))
536 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
537 if (match(host,i->hostmask, true))
542 bool XLineManager::GSortComparison ( const GLine one, const GLine two )
544 return (one.duration + one.set_time) < (two.duration + two.set_time);
547 bool XLineManager::ESortComparison ( const ELine one, const ELine two )
549 return (one.duration + one.set_time) < (two.duration + two.set_time);
552 bool XLineManager::ZSortComparison ( const ZLine one, const ZLine two )
554 return (one.duration + one.set_time) < (two.duration + two.set_time);
557 bool XLineManager::KSortComparison ( const KLine one, const KLine two )
559 return (one.duration + one.set_time) < (two.duration + two.set_time);
562 bool XLineManager::QSortComparison ( const QLine one, const QLine two )
564 return (one.duration + one.set_time) < (two.duration + two.set_time);
567 // removes lines that have expired
569 void XLineManager::expire_lines()
571 time_t current = ServerInstance->Time();
573 /* Because we now store all our XLines in sorted order using (i->duration + i->set_time) as a key, this
574 * means that to expire the XLines we just need to do a while, picking off the top few until there are
575 * none left at the head of the queue that are after the current time.
578 while ((glines.size()) && (current > (glines.begin()->duration + glines.begin()->set_time)))
580 std::vector<GLine>::iterator i = glines.begin();
581 ServerInstance->WriteOpers("*** Expiring timed G-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
585 while ((elines.size()) && (current > (elines.begin()->duration + elines.begin()->set_time)))
587 std::vector<ELine>::iterator i = elines.begin();
588 ServerInstance->WriteOpers("*** Expiring timed E-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
592 while ((zlines.size()) && (current > (zlines.begin()->duration + zlines.begin()->set_time)))
594 std::vector<ZLine>::iterator i = zlines.begin();
595 ServerInstance->WriteOpers("*** Expiring timed Z-Line %s (set by %s %d seconds ago)",i->ipaddr,i->source,i->duration);
599 while ((klines.size()) && (current > (klines.begin()->duration + klines.begin()->set_time)))
601 std::vector<KLine>::iterator i = klines.begin();
602 ServerInstance->WriteOpers("*** Expiring timed K-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
606 while ((qlines.size()) && (current > (qlines.begin()->duration + qlines.begin()->set_time)))
608 std::vector<QLine>::iterator i = qlines.begin();
609 ServerInstance->WriteOpers("*** Expiring timed Q-Line %s (set by %s %d seconds ago)",i->nick,i->source,i->duration);
615 // applies lines, removing clients and changing nicks etc as applicable
617 void XLineManager::apply_lines(const int What)
622 if ((!glines.size()) && (!klines.size()) && (!zlines.size()) && (!qlines.size()) &&
623 (!pglines.size()) && (!pklines.size()) && (!pzlines.size()) && (!pqlines.size()))
626 CullList* Goners = new CullList(ServerInstance);
628 for (std::vector<userrec*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
630 userrec* u = (userrec*)(*u2);
632 if (elines.size() || pelines.size())
634 // ignore people matching exempts
635 if (matches_exception(host))
638 if ((What & APPLY_GLINES) && (glines.size() || pglines.size()))
640 if ((check = matches_gline(host)))
642 snprintf(reason,MAXBUF,"G-Lined: %s",check);
643 Goners->AddItem(u,reason);
646 if ((What & APPLY_KLINES) && (klines.size() || pklines.size()))
648 if ((check = matches_kline(host)))
650 snprintf(reason,MAXBUF,"K-Lined: %s",check);
651 Goners->AddItem(u,reason);
654 if ((What & APPLY_QLINES) && (qlines.size() || pqlines.size()))
656 if ((check = matches_qline(u->nick)))
658 snprintf(reason,MAXBUF,"Q-Lined: %s",check);
659 Goners->AddItem(u,reason);
662 if ((What & APPLY_ZLINES) && (zlines.size() || pzlines.size()))
664 if ((check = matches_zline(u->GetIPString())))
666 snprintf(reason,MAXBUF,"Z-Lined: %s",check);
667 Goners->AddItem(u,reason);
676 void XLineManager::stats_k(userrec* user, string_list &results)
678 std::string sn = ServerInstance->Config->ServerName;
679 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
680 results.push_back(sn+" 216 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
681 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
682 results.push_back(sn+" 216 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
685 void XLineManager::stats_g(userrec* user, string_list &results)
687 std::string sn = ServerInstance->Config->ServerName;
688 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
689 results.push_back(sn+" 223 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
690 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
691 results.push_back(sn+" 223 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
694 void XLineManager::stats_q(userrec* user, string_list &results)
696 std::string sn = ServerInstance->Config->ServerName;
697 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
698 results.push_back(sn+" 217 "+user->nick+" :"+i->nick+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
699 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
700 results.push_back(sn+" 217 "+user->nick+" :"+i->nick+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
703 void XLineManager::stats_z(userrec* user, string_list &results)
705 std::string sn = ServerInstance->Config->ServerName;
706 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
707 results.push_back(sn+" 223 "+user->nick+" :"+i->ipaddr+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
708 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
709 results.push_back(sn+" 223 "+user->nick+" :"+i->ipaddr+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
712 void XLineManager::stats_e(userrec* user, string_list &results)
714 std::string sn = ServerInstance->Config->ServerName;
715 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
716 results.push_back(sn+" 223 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
717 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
718 results.push_back(sn+" 223 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
721 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance)