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 extern InspIRCd* ServerInstance;
43 /* Version two, now with optimized expiry!
45 * Because the old way was horrendously slow, the new way of expiring xlines is very
46 * very efficient. I have improved the efficiency of the algorithm in two ways:
48 * (1) There are now two lists of items for each linetype. One list holds temporary
49 * items, and the other list holds permanent items (ones which will expire).
50 * Items which are on the permanent list are NEVER checked at all by the
51 * expire_lines() function.
52 * (2) The temporary xline lists are always kept in strict numerical order, keyed by
53 * current time + duration. This means that the line which is due to expire the
54 * soonest is always pointed at by vector::begin(), so a simple while loop can
55 * very efficiently, very quickly and above all SAFELY pick off the first few
56 * items in the vector which need zapping.
61 /* Lists for temporary lines with an expiry time */
63 std::vector<KLine> klines;
64 std::vector<GLine> glines;
65 std::vector<ZLine> zlines;
66 std::vector<QLine> qlines;
67 std::vector<ELine> elines;
69 /* Seperate lists for perm XLines that isnt checked by expiry functions */
71 std::vector<KLine> pklines;
72 std::vector<GLine> pglines;
73 std::vector<ZLine> pzlines;
74 std::vector<QLine> pqlines;
75 std::vector<ELine> pelines;
78 bool GSortComparison ( const GLine one, const GLine two );
79 bool ZSortComparison ( const ZLine one, const ZLine two );
80 bool ESortComparison ( const ELine one, const ELine two );
81 bool QSortComparison ( const QLine one, const QLine two );
82 bool KSortComparison ( const KLine one, const KLine two );
84 // Reads the default bans from the config file.
85 // only a very small number of bans are defined
86 // this way these days, such as qlines against
87 // services nicks, etc.
89 bool InitXLine(ServerConfig* conf, const char* tag)
94 bool DoneXLine(ServerConfig* conf, const char* tag)
96 apply_lines(APPLY_ALL);
100 bool DoZLine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
102 char* reason = (char*)values[0];
103 char* ipmask = (char*)values[1];
105 add_zline(0,"<Config>",reason,ipmask);
106 log(DEBUG,"Read Z line (badip tag): ipmask=%s reason=%s",ipmask,reason);
110 bool DoQLine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
112 char* reason = (char*)values[0];
113 char* nick = (char*)values[1];
115 add_qline(0,"<Config>",reason,nick);
116 log(DEBUG,"Read Q line (badnick tag): nick=%s reason=%s",nick,reason);
120 bool DoKLine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
122 char* reason = (char*)values[0];
123 char* host = (char*)values[1];
125 add_kline(0,"<Config>",reason,host);
126 log(DEBUG,"Read K line (badhost tag): host=%s reason=%s",host,reason);
130 bool DoELine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
132 char* reason = (char*)values[0];
133 char* host = (char*)values[1];
135 add_eline(0,"<Config>",reason,host);
136 log(DEBUG,"Read E line (exception tag): host=%s reason=%s",host,reason);
142 bool add_gline(long duration, const char* source,const char* reason,const char* hostmask)
144 bool ret = del_gline(hostmask);
147 item.duration = duration;
148 strlcpy(item.hostmask,hostmask,199);
149 strlcpy(item.reason,reason,MAXBUF);
150 strlcpy(item.source,source,255);
152 item.set_time = ServerInstance->Time();
156 glines.push_back(item);
157 sort(glines.begin(), glines.end(),GSortComparison);
161 pglines.push_back(item);
167 // adds an e:line (exception to bans)
169 bool add_eline(long duration, const char* source, const char* reason, const char* hostmask)
171 bool ret = del_eline(hostmask);
173 item.duration = duration;
174 strlcpy(item.hostmask,hostmask,199);
175 strlcpy(item.reason,reason,MAXBUF);
176 strlcpy(item.source,source,255);
178 item.set_time = ServerInstance->Time();
181 elines.push_back(item);
182 sort(elines.begin(), elines.end(),ESortComparison);
186 pelines.push_back(item);
193 bool add_qline(long duration, const char* source, const char* reason, const char* nickname)
195 bool ret = del_qline(nickname);
197 item.duration = duration;
198 strlcpy(item.nick,nickname,63);
199 strlcpy(item.reason,reason,MAXBUF);
200 strlcpy(item.source,source,255);
202 item.is_global = false;
203 item.set_time = ServerInstance->Time();
206 qlines.push_back(item);
207 sort(qlines.begin(), qlines.end(),QSortComparison);
211 pqlines.push_back(item);
218 bool add_zline(long duration, const char* source, const char* reason, const char* ipaddr)
220 bool ret = del_zline(ipaddr);
222 item.duration = duration;
223 if (strchr(ipaddr,'@'))
225 while (*ipaddr != '@')
229 strlcpy(item.ipaddr,ipaddr,39);
230 strlcpy(item.reason,reason,MAXBUF);
231 strlcpy(item.source,source,255);
233 item.is_global = false;
234 item.set_time = ServerInstance->Time();
237 zlines.push_back(item);
238 sort(zlines.begin(), zlines.end(),ZSortComparison);
242 pzlines.push_back(item);
249 bool add_kline(long duration, const char* source, const char* reason, const char* hostmask)
251 bool ret = del_kline(hostmask);
253 item.duration = duration;
254 strlcpy(item.hostmask,hostmask,200);
255 strlcpy(item.reason,reason,MAXBUF);
256 strlcpy(item.source,source,255);
258 item.set_time = ServerInstance->Time();
261 klines.push_back(item);
262 sort(klines.begin(), klines.end(),KSortComparison);
266 pklines.push_back(item);
271 // deletes a g:line, returns true if the line existed and was removed
273 bool del_gline(const char* hostmask)
275 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
277 if (!strcasecmp(hostmask,i->hostmask))
283 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
285 if (!strcasecmp(hostmask,i->hostmask))
294 // deletes a e:line, returns true if the line existed and was removed
296 bool del_eline(const char* hostmask)
298 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
300 if (!strcasecmp(hostmask,i->hostmask))
306 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
308 if (!strcasecmp(hostmask,i->hostmask))
317 // deletes a q:line, returns true if the line existed and was removed
319 bool del_qline(const char* nickname)
321 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
323 if (!strcasecmp(nickname,i->nick))
329 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
331 if (!strcasecmp(nickname,i->nick))
340 bool qline_make_global(const char* nickname)
342 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
344 if (!strcasecmp(nickname,i->nick))
353 bool zline_make_global(const char* ipaddr)
355 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
357 if (!strcasecmp(ipaddr,i->ipaddr))
366 // deletes a z:line, returns true if the line existed and was removed
368 bool del_zline(const char* ipaddr)
370 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
372 if (!strcasecmp(ipaddr,i->ipaddr))
378 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
380 if (!strcasecmp(ipaddr,i->ipaddr))
389 // deletes a k:line, returns true if the line existed and was removed
391 bool del_kline(const char* hostmask)
393 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
395 if (!strcasecmp(hostmask,i->hostmask))
401 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
403 if (!strcasecmp(hostmask,i->hostmask))
412 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
414 char* matches_qline(const char* nick)
416 if ((qlines.empty()) && (pqlines.empty()))
418 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
419 if (match(nick,i->nick))
421 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
422 if (match(nick,i->nick))
427 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
429 char* matches_gline(const char* host)
431 if ((glines.empty()) && (pglines.empty()))
433 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
434 if (match(host,i->hostmask, true))
436 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
437 if (match(host,i->hostmask, true))
442 char* matches_exception(const char* host)
444 if ((elines.empty()) && (pelines.empty()))
447 snprintf(host2,MAXBUF,"*@%s",host);
448 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
449 if ((match(host,i->hostmask)) || (match(host2,i->hostmask, true)))
451 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
452 if ((match(host,i->hostmask)) || (match(host2,i->hostmask, true)))
458 void gline_set_creation_time(const char* host, time_t create_time)
460 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
462 if (!strcasecmp(host,i->hostmask))
464 i->set_time = create_time;
468 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
470 if (!strcasecmp(host,i->hostmask))
472 i->set_time = create_time;
479 void eline_set_creation_time(const char* host, time_t create_time)
481 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
483 if (!strcasecmp(host,i->hostmask))
485 i->set_time = create_time;
489 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
491 if (!strcasecmp(host,i->hostmask))
493 i->set_time = create_time;
500 void qline_set_creation_time(const char* nick, time_t create_time)
502 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
504 if (!strcasecmp(nick,i->nick))
506 i->set_time = create_time;
510 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
512 if (!strcasecmp(nick,i->nick))
514 i->set_time = create_time;
521 void zline_set_creation_time(const char* ip, time_t create_time)
523 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
525 if (!strcasecmp(ip,i->ipaddr))
527 i->set_time = create_time;
531 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
533 if (!strcasecmp(ip,i->ipaddr))
535 i->set_time = create_time;
542 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
544 char* matches_zline(const char* ipaddr)
546 if ((zlines.empty()) && (pzlines.empty()))
548 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
549 if (match(ipaddr,i->ipaddr, true))
551 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
552 if (match(ipaddr,i->ipaddr, true))
557 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
559 char* matches_kline(const char* host)
561 if ((klines.empty()) && (pklines.empty()))
563 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
564 if (match(host,i->hostmask, true))
566 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
567 if (match(host,i->hostmask, true))
572 bool GSortComparison ( const GLine one, const GLine two )
574 return (one.duration + one.set_time) < (two.duration + two.set_time);
577 bool ESortComparison ( const ELine one, const ELine two )
579 return (one.duration + one.set_time) < (two.duration + two.set_time);
582 bool ZSortComparison ( const ZLine one, const ZLine two )
584 return (one.duration + one.set_time) < (two.duration + two.set_time);
587 bool KSortComparison ( const KLine one, const KLine two )
589 return (one.duration + one.set_time) < (two.duration + two.set_time);
592 bool QSortComparison ( const QLine one, const QLine two )
594 return (one.duration + one.set_time) < (two.duration + two.set_time);
597 // removes lines that have expired
601 time_t current = ServerInstance->Time();
603 /* Because we now store all our XLines in sorted order using (i->duration + i->set_time) as a key, this
604 * means that to expire the XLines we just need to do a while, picking off the top few until there are
605 * none left at the head of the queue that are after the current time.
608 while ((glines.size()) && (current > (glines.begin()->duration + glines.begin()->set_time)))
610 std::vector<GLine>::iterator i = glines.begin();
611 ServerInstance->WriteOpers("*** Expiring timed G-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
615 while ((elines.size()) && (current > (elines.begin()->duration + elines.begin()->set_time)))
617 std::vector<ELine>::iterator i = elines.begin();
618 ServerInstance->WriteOpers("*** Expiring timed E-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
622 while ((zlines.size()) && (current > (zlines.begin()->duration + zlines.begin()->set_time)))
624 std::vector<ZLine>::iterator i = zlines.begin();
625 ServerInstance->WriteOpers("*** Expiring timed Z-Line %s (set by %s %d seconds ago)",i->ipaddr,i->source,i->duration);
629 while ((klines.size()) && (current > (klines.begin()->duration + klines.begin()->set_time)))
631 std::vector<KLine>::iterator i = klines.begin();
632 ServerInstance->WriteOpers("*** Expiring timed K-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
636 while ((qlines.size()) && (current > (qlines.begin()->duration + qlines.begin()->set_time)))
638 std::vector<QLine>::iterator i = qlines.begin();
639 ServerInstance->WriteOpers("*** Expiring timed Q-Line %s (set by %s %d seconds ago)",i->nick,i->source,i->duration);
645 // applies lines, removing clients and changing nicks etc as applicable
647 void apply_lines(const int What)
652 if ((!glines.size()) && (!klines.size()) && (!zlines.size()) && (!qlines.size()) &&
653 (!pglines.size()) && (!pklines.size()) && (!pzlines.size()) && (!pqlines.size()))
656 CullList* Goners = new CullList(ServerInstance);
658 for (std::vector<userrec*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
660 userrec* u = (userrec*)(*u2);
662 if (elines.size() || pelines.size())
664 // ignore people matching exempts
665 if (matches_exception(host))
668 if ((What & APPLY_GLINES) && (glines.size() || pglines.size()))
670 if ((check = matches_gline(host)))
672 snprintf(reason,MAXBUF,"G-Lined: %s",check);
673 Goners->AddItem(u,reason);
676 if ((What & APPLY_KLINES) && (klines.size() || pklines.size()))
678 if ((check = matches_kline(host)))
680 snprintf(reason,MAXBUF,"K-Lined: %s",check);
681 Goners->AddItem(u,reason);
684 if ((What & APPLY_QLINES) && (qlines.size() || pqlines.size()))
686 if ((check = matches_qline(u->nick)))
688 snprintf(reason,MAXBUF,"Q-Lined: %s",check);
689 Goners->AddItem(u,reason);
692 if ((What & APPLY_ZLINES) && (zlines.size() || pzlines.size()))
694 if ((check = matches_zline(u->GetIPString())))
696 snprintf(reason,MAXBUF,"Z-Lined: %s",check);
697 Goners->AddItem(u,reason);
706 void stats_k(userrec* user, string_list &results)
708 std::string sn = ServerInstance->Config->ServerName;
709 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
710 results.push_back(sn+" 216 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
711 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
712 results.push_back(sn+" 216 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
715 void stats_g(userrec* user, string_list &results)
717 std::string sn = ServerInstance->Config->ServerName;
718 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
719 results.push_back(sn+" 223 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
720 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
721 results.push_back(sn+" 223 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
724 void stats_q(userrec* user, string_list &results)
726 std::string sn = ServerInstance->Config->ServerName;
727 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
728 results.push_back(sn+" 217 "+user->nick+" :"+i->nick+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
729 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
730 results.push_back(sn+" 217 "+user->nick+" :"+i->nick+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
733 void stats_z(userrec* user, string_list &results)
735 std::string sn = ServerInstance->Config->ServerName;
736 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
737 results.push_back(sn+" 223 "+user->nick+" :"+i->ipaddr+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
738 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
739 results.push_back(sn+" 223 "+user->nick+" :"+i->ipaddr+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
742 void stats_e(userrec* user, string_list &results)
744 std::string sn = ServerInstance->Config->ServerName;
745 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
746 results.push_back(sn+" 223 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
747 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
748 results.push_back(sn+" 223 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);