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"
35 #include "inspstring.h"
36 #include "helperfuncs.h"
39 #include "configreader.h"
40 #include "cull_list.h"
42 extern InspIRCd* ServerInstance;
45 extern std::vector<Module*> modules;
46 extern std::vector<ircd_module*> factory;
48 /* Version two, now with optimized expiry!
50 * Because the old way was horrendously slow, the new way of expiring xlines is very
51 * very efficient. I have improved the efficiency of the algorithm in two ways:
53 * (1) There are now two lists of items for each linetype. One list holds temporary
54 * items, and the other list holds permanent items (ones which will expire).
55 * Items which are on the permanent list are NEVER checked at all by the
56 * expire_lines() function.
57 * (2) The temporary xline lists are always kept in strict numerical order, keyed by
58 * current time + duration. This means that the line which is due to expire the
59 * soonest is always pointed at by vector::begin(), so a simple while loop can
60 * very efficiently, very quickly and above all SAFELY pick off the first few
61 * items in the vector which need zapping.
70 /* Lists for temporary lines with an expiry time */
72 std::vector<KLine> klines;
73 std::vector<GLine> glines;
74 std::vector<ZLine> zlines;
75 std::vector<QLine> qlines;
76 std::vector<ELine> elines;
78 /* Seperate lists for perm XLines that isnt checked by expiry functions */
80 std::vector<KLine> pklines;
81 std::vector<GLine> pglines;
82 std::vector<ZLine> pzlines;
83 std::vector<QLine> pqlines;
84 std::vector<ELine> pelines;
87 bool GSortComparison ( const GLine one, const GLine two );
88 bool ZSortComparison ( const ZLine one, const ZLine two );
89 bool ESortComparison ( const ELine one, const ELine two );
90 bool QSortComparison ( const QLine one, const QLine two );
91 bool KSortComparison ( const KLine one, const KLine two );
93 // Reads the default bans from the config file.
94 // only a very small number of bans are defined
95 // this way these days, such as qlines against
96 // services nicks, etc.
98 bool InitXLine(ServerConfig* conf, const char* tag)
103 bool DoneXLine(ServerConfig* conf, const char* tag)
105 apply_lines(APPLY_ALL);
109 bool DoZLine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
111 char* reason = (char*)values[0];
112 char* ipmask = (char*)values[1];
114 add_zline(0,"<Config>",reason,ipmask);
115 log(DEBUG,"Read Z line (badip tag): ipmask=%s reason=%s",ipmask,reason);
119 bool DoQLine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
121 char* reason = (char*)values[0];
122 char* nick = (char*)values[1];
124 add_qline(0,"<Config>",reason,nick);
125 log(DEBUG,"Read Q line (badnick tag): nick=%s reason=%s",nick,reason);
129 bool DoKLine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
131 char* reason = (char*)values[0];
132 char* host = (char*)values[1];
134 add_kline(0,"<Config>",reason,host);
135 log(DEBUG,"Read K line (badhost tag): host=%s reason=%s",host,reason);
139 bool DoELine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
141 char* reason = (char*)values[0];
142 char* host = (char*)values[1];
144 add_eline(0,"<Config>",reason,host);
145 log(DEBUG,"Read E line (exception tag): host=%s reason=%s",host,reason);
151 bool add_gline(long duration, const char* source,const char* reason,const char* hostmask)
153 bool ret = del_gline(hostmask);
156 item.duration = duration;
157 strlcpy(item.hostmask,hostmask,199);
158 strlcpy(item.reason,reason,MAXBUF);
159 strlcpy(item.source,source,255);
161 item.set_time = TIME;
165 glines.push_back(item);
166 sort(glines.begin(), glines.end(),GSortComparison);
170 pglines.push_back(item);
176 // adds an e:line (exception to bans)
178 bool add_eline(long duration, const char* source, const char* reason, const char* hostmask)
180 bool ret = del_eline(hostmask);
182 item.duration = duration;
183 strlcpy(item.hostmask,hostmask,199);
184 strlcpy(item.reason,reason,MAXBUF);
185 strlcpy(item.source,source,255);
187 item.set_time = TIME;
190 elines.push_back(item);
191 sort(elines.begin(), elines.end(),ESortComparison);
195 pelines.push_back(item);
202 bool add_qline(long duration, const char* source, const char* reason, const char* nickname)
204 bool ret = del_qline(nickname);
206 item.duration = duration;
207 strlcpy(item.nick,nickname,63);
208 strlcpy(item.reason,reason,MAXBUF);
209 strlcpy(item.source,source,255);
211 item.is_global = false;
212 item.set_time = TIME;
215 qlines.push_back(item);
216 sort(qlines.begin(), qlines.end(),QSortComparison);
220 pqlines.push_back(item);
227 bool add_zline(long duration, const char* source, const char* reason, const char* ipaddr)
229 bool ret = del_zline(ipaddr);
231 item.duration = duration;
232 if (strchr(ipaddr,'@'))
234 while (*ipaddr != '@')
238 strlcpy(item.ipaddr,ipaddr,39);
239 strlcpy(item.reason,reason,MAXBUF);
240 strlcpy(item.source,source,255);
242 item.is_global = false;
243 item.set_time = TIME;
246 zlines.push_back(item);
247 sort(zlines.begin(), zlines.end(),ZSortComparison);
251 pzlines.push_back(item);
258 bool add_kline(long duration, const char* source, const char* reason, const char* hostmask)
260 bool ret = del_kline(hostmask);
262 item.duration = duration;
263 strlcpy(item.hostmask,hostmask,200);
264 strlcpy(item.reason,reason,MAXBUF);
265 strlcpy(item.source,source,255);
267 item.set_time = TIME;
270 klines.push_back(item);
271 sort(klines.begin(), klines.end(),KSortComparison);
275 pklines.push_back(item);
280 // deletes a g:line, returns true if the line existed and was removed
282 bool del_gline(const char* hostmask)
284 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
286 if (!strcasecmp(hostmask,i->hostmask))
292 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
294 if (!strcasecmp(hostmask,i->hostmask))
303 // deletes a e:line, returns true if the line existed and was removed
305 bool del_eline(const char* hostmask)
307 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
309 if (!strcasecmp(hostmask,i->hostmask))
315 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
317 if (!strcasecmp(hostmask,i->hostmask))
326 // deletes a q:line, returns true if the line existed and was removed
328 bool del_qline(const char* nickname)
330 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
332 if (!strcasecmp(nickname,i->nick))
338 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
340 if (!strcasecmp(nickname,i->nick))
349 bool qline_make_global(const char* nickname)
351 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
353 if (!strcasecmp(nickname,i->nick))
362 bool zline_make_global(const char* ipaddr)
364 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
366 if (!strcasecmp(ipaddr,i->ipaddr))
375 // deletes a z:line, returns true if the line existed and was removed
377 bool del_zline(const char* ipaddr)
379 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
381 if (!strcasecmp(ipaddr,i->ipaddr))
387 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
389 if (!strcasecmp(ipaddr,i->ipaddr))
398 // deletes a k:line, returns true if the line existed and was removed
400 bool del_kline(const char* hostmask)
402 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
404 if (!strcasecmp(hostmask,i->hostmask))
410 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
412 if (!strcasecmp(hostmask,i->hostmask))
421 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
423 char* matches_qline(const char* nick)
425 if ((qlines.empty()) && (pqlines.empty()))
427 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
428 if (match(nick,i->nick))
430 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
431 if (match(nick,i->nick))
436 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
438 char* matches_gline(const char* host)
440 if ((glines.empty()) && (pglines.empty()))
442 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
443 if (match(host,i->hostmask, true))
445 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
446 if (match(host,i->hostmask, true))
451 char* matches_exception(const char* host)
453 if ((elines.empty()) && (pelines.empty()))
456 snprintf(host2,MAXBUF,"*@%s",host);
457 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
458 if ((match(host,i->hostmask)) || (match(host2,i->hostmask, true)))
460 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
461 if ((match(host,i->hostmask)) || (match(host2,i->hostmask, true)))
467 void gline_set_creation_time(const char* host, time_t create_time)
469 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
471 if (!strcasecmp(host,i->hostmask))
473 i->set_time = create_time;
477 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
479 if (!strcasecmp(host,i->hostmask))
481 i->set_time = create_time;
488 void eline_set_creation_time(const char* host, time_t create_time)
490 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
492 if (!strcasecmp(host,i->hostmask))
494 i->set_time = create_time;
498 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
500 if (!strcasecmp(host,i->hostmask))
502 i->set_time = create_time;
509 void qline_set_creation_time(const char* nick, time_t create_time)
511 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
513 if (!strcasecmp(nick,i->nick))
515 i->set_time = create_time;
519 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
521 if (!strcasecmp(nick,i->nick))
523 i->set_time = create_time;
530 void zline_set_creation_time(const char* ip, time_t create_time)
532 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
534 if (!strcasecmp(ip,i->ipaddr))
536 i->set_time = create_time;
540 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
542 if (!strcasecmp(ip,i->ipaddr))
544 i->set_time = create_time;
551 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
553 char* matches_zline(const char* ipaddr)
555 if ((zlines.empty()) && (pzlines.empty()))
557 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
558 if (match(ipaddr,i->ipaddr, true))
560 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
561 if (match(ipaddr,i->ipaddr, true))
566 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
568 char* matches_kline(const char* host)
570 if ((klines.empty()) && (pklines.empty()))
572 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
573 if (match(host,i->hostmask, true))
575 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
576 if (match(host,i->hostmask, true))
581 bool GSortComparison ( const GLine one, const GLine two )
583 return (one.duration + one.set_time) < (two.duration + two.set_time);
586 bool ESortComparison ( const ELine one, const ELine two )
588 return (one.duration + one.set_time) < (two.duration + two.set_time);
591 bool ZSortComparison ( const ZLine one, const ZLine two )
593 return (one.duration + one.set_time) < (two.duration + two.set_time);
596 bool KSortComparison ( const KLine one, const KLine two )
598 return (one.duration + one.set_time) < (two.duration + two.set_time);
601 bool QSortComparison ( const QLine one, const QLine two )
603 return (one.duration + one.set_time) < (two.duration + two.set_time);
606 // removes lines that have expired
610 time_t current = TIME;
612 /* Because we now store all our XLines in sorted order using (i->duration + i->set_time) as a key, this
613 * means that to expire the XLines we just need to do a while, picking off the top few until there are
614 * none left at the head of the queue that are after the current time.
617 while ((glines.size()) && (current > (glines.begin()->duration + glines.begin()->set_time)))
619 std::vector<GLine>::iterator i = glines.begin();
620 WriteOpers("*** Expiring timed G-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
624 while ((elines.size()) && (current > (elines.begin()->duration + elines.begin()->set_time)))
626 std::vector<ELine>::iterator i = elines.begin();
627 WriteOpers("*** Expiring timed E-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
631 while ((zlines.size()) && (current > (zlines.begin()->duration + zlines.begin()->set_time)))
633 std::vector<ZLine>::iterator i = zlines.begin();
634 WriteOpers("*** Expiring timed Z-Line %s (set by %s %d seconds ago)",i->ipaddr,i->source,i->duration);
638 while ((klines.size()) && (current > (klines.begin()->duration + klines.begin()->set_time)))
640 std::vector<KLine>::iterator i = klines.begin();
641 WriteOpers("*** Expiring timed K-Line %s (set by %s %d seconds ago)",i->hostmask,i->source,i->duration);
645 while ((qlines.size()) && (current > (qlines.begin()->duration + qlines.begin()->set_time)))
647 std::vector<QLine>::iterator i = qlines.begin();
648 WriteOpers("*** Expiring timed Q-Line %s (set by %s %d seconds ago)",i->nick,i->source,i->duration);
654 // applies lines, removing clients and changing nicks etc as applicable
656 void apply_lines(const int What)
661 if ((!glines.size()) && (!klines.size()) && (!zlines.size()) && (!qlines.size()) &&
662 (!pglines.size()) && (!pklines.size()) && (!pzlines.size()) && (!pqlines.size()))
665 CullList* Goners = new CullList();
667 for (std::vector<userrec*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
669 userrec* u = (userrec*)(*u2);
671 if (elines.size() || pelines.size())
673 // ignore people matching exempts
674 if (matches_exception(host))
677 if ((What & APPLY_GLINES) && (glines.size() || pglines.size()))
679 if ((check = matches_gline(host)))
681 snprintf(reason,MAXBUF,"G-Lined: %s",check);
682 Goners->AddItem(u,reason);
685 if ((What & APPLY_KLINES) && (klines.size() || pklines.size()))
687 if ((check = matches_kline(host)))
689 snprintf(reason,MAXBUF,"K-Lined: %s",check);
690 Goners->AddItem(u,reason);
693 if ((What & APPLY_QLINES) && (qlines.size() || pqlines.size()))
695 if ((check = matches_qline(u->nick)))
697 snprintf(reason,MAXBUF,"Q-Lined: %s",check);
698 Goners->AddItem(u,reason);
701 if ((What & APPLY_ZLINES) && (zlines.size() || pzlines.size()))
703 if ((check = matches_zline(u->GetIPString())))
705 snprintf(reason,MAXBUF,"Z-Lined: %s",check);
706 Goners->AddItem(u,reason);
715 void stats_k(userrec* user, string_list &results)
717 std::string sn = ServerInstance->Config->ServerName;
718 for (std::vector<KLine>::iterator i = klines.begin(); i != klines.end(); i++)
719 results.push_back(sn+" 216 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
720 for (std::vector<KLine>::iterator i = pklines.begin(); i != pklines.end(); i++)
721 results.push_back(sn+" 216 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
724 void stats_g(userrec* user, string_list &results)
726 std::string sn = ServerInstance->Config->ServerName;
727 for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++)
728 results.push_back(sn+" 223 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
729 for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++)
730 results.push_back(sn+" 223 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
733 void stats_q(userrec* user, string_list &results)
735 std::string sn = ServerInstance->Config->ServerName;
736 for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++)
737 results.push_back(sn+" 217 "+user->nick+" :"+i->nick+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
738 for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
739 results.push_back(sn+" 217 "+user->nick+" :"+i->nick+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
742 void stats_z(userrec* user, string_list &results)
744 std::string sn = ServerInstance->Config->ServerName;
745 for (std::vector<ZLine>::iterator i = zlines.begin(); i != zlines.end(); i++)
746 results.push_back(sn+" 223 "+user->nick+" :"+i->ipaddr+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
747 for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
748 results.push_back(sn+" 223 "+user->nick+" :"+i->ipaddr+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
751 void stats_e(userrec* user, string_list &results)
753 std::string sn = ServerInstance->Config->ServerName;
754 for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++)
755 results.push_back(sn+" 223 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);
756 for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++)
757 results.push_back(sn+" 223 "+user->nick+" :"+i->hostmask+" "+ConvToStr(i->set_time)+" "+ConvToStr(i->duration)+" "+i->source+" :"+i->reason);