1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
20 /* Version two, now with optimized expiry!
22 * Because the old way was horrendously slow, the new way of expiring xlines is very
23 * very efficient. I have improved the efficiency of the algorithm in two ways:
25 * (1) There are now two lists of items for each linetype. One list holds temporary
26 * items, and the other list holds permanent items (ones which will expire).
27 * Items which are on the permanent list are NEVER checked at all by the
28 * expire_lines() function.
29 * (2) The temporary xline lists are always kept in strict numerical order, keyed by
30 * current time + duration. This means that the line which is due to expire the
31 * soonest is always pointed at by vector::begin(), so a simple while loop can
32 * very efficiently, very quickly and above all SAFELY pick off the first few
33 * items in the vector which need zapping.
38 bool InitXLine(ServerConfig* conf, const char* tag)
43 bool DoneZLine(ServerConfig* conf, const char* tag)
45 conf->GetInstance()->XLines->apply_lines(APPLY_ZLINES|APPLY_PERM_ONLY);
49 bool DoneQLine(ServerConfig* conf, const char* tag)
51 conf->GetInstance()->XLines->apply_lines(APPLY_QLINES|APPLY_PERM_ONLY);
55 bool DoneKLine(ServerConfig* conf, const char* tag)
57 conf->GetInstance()->XLines->apply_lines(APPLY_KLINES|APPLY_PERM_ONLY);
61 bool DoneELine(ServerConfig* conf, const char* tag)
63 /* Yes, this is supposed to do nothing, we dont 'apply' these */
67 bool DoZLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
69 const char* reason = values[0].GetString();
70 const char* ipmask = values[1].GetString();
72 conf->GetInstance()->XLines->add_zline(0,"<Config>",reason,ipmask);
76 bool DoQLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
78 const char* reason = values[0].GetString();
79 const char* nick = values[1].GetString();
81 conf->GetInstance()->XLines->add_qline(0,"<Config>",reason,nick);
85 bool DoKLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
87 const char* reason = values[0].GetString();
88 const char* host = values[1].GetString();
90 conf->GetInstance()->XLines->add_kline(0,"<Config>",reason,host);
94 bool DoELine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
96 const char* reason = values[0].GetString();
97 const char* host = values[1].GetString();
99 conf->GetInstance()->XLines->add_eline(0,"<Config>",reason,host);
103 IdentHostPair XLineManager::IdentSplit(const std::string &ident_and_host)
105 IdentHostPair n = std::make_pair<std::string,std::string>("*","*");
106 std::string::size_type x = ident_and_host.find('@');
107 if (x != std::string::npos)
109 n.second = ident_and_host.substr(x + 1,ident_and_host.length());
110 n.first = ident_and_host.substr(0, x);
111 if (!n.first.length())
113 if (!n.second.length())
118 n.second = ident_and_host;
126 bool XLineManager::add_gline(long duration, const char* source,const char* reason,const char* hostmask)
128 IdentHostPair ih = IdentSplit(hostmask);
130 bool ret = del_gline(hostmask);
132 GLine* item = new GLine(ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());
136 glines.push_back(item);
137 sort(glines.begin(), glines.end(),XLineManager::GSortComparison);
141 pglines.push_back(item);
147 // adds an e:line (exception to bans)
149 bool XLineManager::add_eline(long duration, const char* source, const char* reason, const char* hostmask)
151 IdentHostPair ih = IdentSplit(hostmask);
153 bool ret = del_eline(hostmask);
155 ELine* item = new ELine(ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());
159 elines.push_back(item);
160 sort(elines.begin(), elines.end(),XLineManager::ESortComparison);
164 pelines.push_back(item);
171 bool XLineManager::add_qline(long duration, const char* source, const char* reason, const char* nickname)
173 bool ret = del_qline(nickname);
174 QLine* item = new QLine(ServerInstance->Time(), duration, source, reason, nickname);
178 qlines.push_back(item);
179 sort(qlines.begin(), qlines.end(),XLineManager::QSortComparison);
183 pqlines.push_back(item);
190 bool XLineManager::add_zline(long duration, const char* source, const char* reason, const char* ipaddr)
192 bool ret = del_zline(ipaddr);
194 if (strchr(ipaddr,'@'))
196 while (*ipaddr != '@')
201 ZLine* item = new ZLine(ServerInstance->Time(), duration, source, reason, ipaddr);
205 zlines.push_back(item);
206 sort(zlines.begin(), zlines.end(),XLineManager::ZSortComparison);
210 pzlines.push_back(item);
217 bool XLineManager::add_kline(long duration, const char* source, const char* reason, const char* hostmask)
219 IdentHostPair ih = IdentSplit(hostmask);
221 bool ret = del_kline(hostmask);
223 KLine* item = new KLine(ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());
227 klines.push_back(item);
228 sort(klines.begin(), klines.end(),XLineManager::KSortComparison);
232 pklines.push_back(item);
237 // deletes a g:line, returns true if the line existed and was removed
239 bool XLineManager::del_gline(const char* hostmask)
241 IdentHostPair ih = IdentSplit(hostmask);
242 for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
244 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
251 for (std::vector<GLine*>::iterator i = pglines.begin(); i != pglines.end(); i++)
253 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
263 // deletes a e:line, returns true if the line existed and was removed
265 bool XLineManager::del_eline(const char* hostmask)
267 IdentHostPair ih = IdentSplit(hostmask);
268 for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
270 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
277 for (std::vector<ELine*>::iterator i = pelines.begin(); i != pelines.end(); i++)
279 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
289 // deletes a q:line, returns true if the line existed and was removed
291 bool XLineManager::del_qline(const char* nickname)
293 for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
295 if (!strcasecmp(nickname,(*i)->nick))
302 for (std::vector<QLine*>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
304 if (!strcasecmp(nickname,(*i)->nick))
314 // deletes a z:line, returns true if the line existed and was removed
316 bool XLineManager::del_zline(const char* ipaddr)
318 for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
320 if (!strcasecmp(ipaddr,(*i)->ipaddr))
327 for (std::vector<ZLine*>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
329 if (!strcasecmp(ipaddr,(*i)->ipaddr))
339 // deletes a k:line, returns true if the line existed and was removed
341 bool XLineManager::del_kline(const char* hostmask)
343 IdentHostPair ih = IdentSplit(hostmask);
344 for (std::vector<KLine*>::iterator i = klines.begin(); i != klines.end(); i++)
346 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
353 for (std::vector<KLine*>::iterator i = pklines.begin(); i != pklines.end(); i++)
355 if (!strcasecmp(ih.first.c_str(),(*i)->identmask) && !strcasecmp(ih.second.c_str(),(*i)->hostmask))
365 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
367 QLine* XLineManager::matches_qline(const char* nick, bool permonly)
369 if ((qlines.empty()) && (pqlines.empty()))
373 for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
374 if (match(nick,(*i)->nick))
377 for (std::vector<QLine*>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
378 if (match(nick,(*i)->nick))
383 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
385 GLine* XLineManager::matches_gline(userrec* user, bool permonly)
387 if ((glines.empty()) && (pglines.empty()))
391 for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
393 if ((match(user->ident,(*i)->identmask)))
395 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
402 for (std::vector<GLine*>::iterator i = pglines.begin(); i != pglines.end(); i++)
404 if ((match(user->ident,(*i)->identmask)))
406 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
415 ELine* XLineManager::matches_exception(userrec* user, bool permonly)
417 if ((elines.empty()) && (pelines.empty()))
420 snprintf(host2,MAXBUF,"*@%s",user->host);
423 for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
425 if ((match(user->ident,(*i)->identmask)))
427 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
434 for (std::vector<ELine*>::iterator i = pelines.begin(); i != pelines.end(); i++)
436 if ((match(user->ident,(*i)->identmask)))
438 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
448 void XLineManager::gline_set_creation_time(const char* host, time_t create_time)
450 for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
452 if (!strcasecmp(host,(*i)->hostmask))
454 (*i)->set_time = create_time;
455 (*i)->expiry = create_time + (*i)->duration;
459 for (std::vector<GLine*>::iterator i = pglines.begin(); i != pglines.end(); i++)
461 if (!strcasecmp(host,(*i)->hostmask))
463 (*i)->set_time = create_time;
470 void XLineManager::eline_set_creation_time(const char* host, time_t create_time)
472 for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
474 if (!strcasecmp(host,(*i)->hostmask))
476 (*i)->set_time = create_time;
477 (*i)->expiry = create_time + (*i)->duration;
481 for (std::vector<ELine*>::iterator i = pelines.begin(); i != pelines.end(); i++)
483 if (!strcasecmp(host,(*i)->hostmask))
485 (*i)->set_time = create_time;
492 void XLineManager::qline_set_creation_time(const char* nick, time_t create_time)
494 for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
496 if (!strcasecmp(nick,(*i)->nick))
498 (*i)->set_time = create_time;
499 (*i)->expiry = create_time + (*i)->duration;
503 for (std::vector<QLine*>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
505 if (!strcasecmp(nick,(*i)->nick))
507 (*i)->set_time = create_time;
514 void XLineManager::zline_set_creation_time(const char* ip, time_t create_time)
516 for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
518 if (!strcasecmp(ip,(*i)->ipaddr))
520 (*i)->set_time = create_time;
521 (*i)->expiry = create_time + (*i)->duration;
525 for (std::vector<ZLine*>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
527 if (!strcasecmp(ip,(*i)->ipaddr))
529 (*i)->set_time = create_time;
536 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
538 ZLine* XLineManager::matches_zline(const char* ipaddr, bool permonly)
540 if ((zlines.empty()) && (pzlines.empty()))
544 for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
545 if (match(ipaddr,(*i)->ipaddr, true))
548 for (std::vector<ZLine*>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
549 if (match(ipaddr,(*i)->ipaddr, true))
554 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
556 KLine* XLineManager::matches_kline(userrec* user, bool permonly)
558 if ((klines.empty()) && (pklines.empty()))
562 for (std::vector<KLine*>::iterator i = klines.begin(); i != klines.end(); i++)
564 if ((match(user->ident,(*i)->identmask)))
566 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
573 for (std::vector<KLine*>::iterator i = pklines.begin(); i != pklines.end(); i++)
575 if ((match(user->ident,(*i)->identmask)))
577 if ((match(user->host,(*i)->hostmask, true)) || (match(user->GetIPString(),(*i)->hostmask, true)))
586 bool XLineManager::GSortComparison ( const GLine* one, const GLine* two )
588 return (one->expiry) < (two->expiry);
591 bool XLineManager::ESortComparison ( const ELine* one, const ELine* two )
593 return (one->expiry) < (two->expiry);
596 bool XLineManager::ZSortComparison ( const ZLine* one, const ZLine* two )
598 return (one->expiry) < (two->expiry);
601 bool XLineManager::KSortComparison ( const KLine* one, const KLine* two )
603 return (one->expiry) < (two->expiry);
606 bool XLineManager::QSortComparison ( const QLine* one, const QLine* two )
608 return (one->expiry) < (two->expiry);
611 // removes lines that have expired
613 void XLineManager::expire_lines()
615 time_t current = ServerInstance->Time();
617 /* Because we now store all our XLines in sorted order using ((*i)->duration + (*i)->set_time) as a key, this
618 * means that to expire the XLines we just need to do a while, picking off the top few until there are
619 * none left at the head of the queue that are after the current time.
622 while ((glines.size()) && (current > (*glines.begin())->expiry))
624 std::vector<GLine*>::iterator i = glines.begin();
625 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s@%s (set by %s %d seconds ago)",(*i)->identmask,(*i)->hostmask,(*i)->source,(*i)->duration);
629 while ((elines.size()) && (current > (*elines.begin())->expiry))
631 std::vector<ELine*>::iterator i = elines.begin();
632 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed E-Line %s@%s (set by %s %d seconds ago)",(*i)->identmask,(*i)->hostmask,(*i)->source,(*i)->duration);
636 while ((zlines.size()) && (current > (*zlines.begin())->expiry))
638 std::vector<ZLine*>::iterator i = zlines.begin();
639 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Z-Line %s (set by %s %d seconds ago)",(*i)->ipaddr,(*i)->source,(*i)->duration);
643 while ((klines.size()) && (current > (*klines.begin())->expiry))
645 std::vector<KLine*>::iterator i = klines.begin();
646 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed K-Line %s@%s (set by %s %d seconds ago)",(*i)->identmask,(*i)->hostmask,(*i)->source,(*i)->duration);
650 while ((qlines.size()) && (current > (*qlines.begin())->expiry))
652 std::vector<QLine*>::iterator i = qlines.begin();
653 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Q-Line %s (set by %s %d seconds ago)",(*i)->nick,(*i)->source,(*i)->duration);
659 // applies lines, removing clients and changing nicks etc as applicable
661 void XLineManager::apply_lines(const int What)
666 if (What & APPLY_PERM_ONLY)
670 if ((!pglines.size()) && (!pklines.size()) && (!pzlines.size()) && (!pqlines.size()))
674 for (std::vector<userrec*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
676 userrec* u = (userrec*)(*u2);
678 if (elines.size() || pelines.size())
679 if (matches_exception(u))
682 if ((What & APPLY_GLINES) && pglines.size())
684 if ((check = matches_gline(u,true)))
686 snprintf(reason,MAXBUF,"G-Lined: %s",check->reason);
687 ServerInstance->GlobalCulls.AddItem(u,reason);
691 if ((What & APPLY_KLINES) && pklines.size())
693 if ((check = matches_kline(u,true)))
695 snprintf(reason,MAXBUF,"K-Lined: %s",check->reason);
696 ServerInstance->GlobalCulls.AddItem(u,reason);
700 if ((What & APPLY_QLINES) && pqlines.size())
702 if ((check = matches_qline(u->nick,true)))
704 snprintf(reason,MAXBUF,"Q-Lined: %s",check->reason);
705 ServerInstance->GlobalCulls.AddItem(u,reason);
709 if ((What & APPLY_ZLINES) && pzlines.size())
711 if ((check = matches_zline(u->GetIPString(),true)))
713 snprintf(reason,MAXBUF,"Z-Lined: %s",check->reason);
714 ServerInstance->GlobalCulls.AddItem(u,reason);
723 if ((!glines.size()) && (!klines.size()) && (!zlines.size()) && (!qlines.size()) &&
724 (!pglines.size()) && (!pklines.size()) && (!pzlines.size()) && (!pqlines.size()))
728 for (std::vector<userrec*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
730 userrec* u = (userrec*)(*u2);
732 if (elines.size() || pelines.size())
734 // ignore people matching exempts
735 if (matches_exception(u))
738 if ((What & APPLY_GLINES) && (glines.size() || pglines.size()))
740 if ((check = matches_gline(u)))
742 snprintf(reason,MAXBUF,"G-Lined: %s",check->reason);
743 ServerInstance->GlobalCulls.AddItem(u,reason);
746 if ((What & APPLY_KLINES) && (klines.size() || pklines.size()))
748 if ((check = matches_kline(u)))
750 snprintf(reason,MAXBUF,"K-Lined: %s",check->reason);
751 ServerInstance->GlobalCulls.AddItem(u,reason);
754 if ((What & APPLY_QLINES) && (qlines.size() || pqlines.size()))
756 if ((check = matches_qline(u->nick)))
758 snprintf(reason,MAXBUF,"Q-Lined: %s",check->reason);
759 ServerInstance->GlobalCulls.AddItem(u,reason);
762 if ((What & APPLY_ZLINES) && (zlines.size() || pzlines.size()))
764 if ((check = matches_zline(u->GetIPString())))
766 snprintf(reason,MAXBUF,"Z-Lined: %s",check->reason);
767 ServerInstance->GlobalCulls.AddItem(u,reason);
774 void XLineManager::stats_k(userrec* user, string_list &results)
776 std::string sn = ServerInstance->Config->ServerName;
777 for (std::vector<KLine*>::iterator i = klines.begin(); i != klines.end(); i++)
778 results.push_back(sn+" 216 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
779 for (std::vector<KLine*>::iterator i = pklines.begin(); i != pklines.end(); i++)
780 results.push_back(sn+" 216 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
783 void XLineManager::stats_g(userrec* user, string_list &results)
785 std::string sn = ServerInstance->Config->ServerName;
786 for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
787 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
788 for (std::vector<GLine*>::iterator i = pglines.begin(); i != pglines.end(); i++)
789 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
792 void XLineManager::stats_q(userrec* user, string_list &results)
794 std::string sn = ServerInstance->Config->ServerName;
795 for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
796 results.push_back(sn+" 217 "+user->nick+" :"+(*i)->nick+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
797 for (std::vector<QLine*>::iterator i = pqlines.begin(); i != pqlines.end(); i++)
798 results.push_back(sn+" 217 "+user->nick+" :"+(*i)->nick+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
801 void XLineManager::stats_z(userrec* user, string_list &results)
803 std::string sn = ServerInstance->Config->ServerName;
804 for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
805 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->ipaddr+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
806 for (std::vector<ZLine*>::iterator i = pzlines.begin(); i != pzlines.end(); i++)
807 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->ipaddr+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
810 void XLineManager::stats_e(userrec* user, string_list &results)
812 std::string sn = ServerInstance->Config->ServerName;
813 for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
814 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
815 for (std::vector<ELine*>::iterator i = pelines.begin(); i != pelines.end(); i++)
816 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);
819 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance)