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 * ---------------------------------------------------
14 /* $Core: libIRCDxline */
21 * This is now version 3 of the XLine subsystem, let's see if we can get it as nice and
22 * efficient as we can this time so we can close this file and never ever touch it again ..
25 * Version 1 stored all line types in one list (one for g, one for z, etc). This was fine,
26 * but both version 1 and 2 suck at applying lines efficiently. That is, every time a new line
27 * was added, it iterated every existing line for every existing user. Ow. Expiry was also
28 * expensive, as the lists were NOT sorted.
30 * Version 2 moved permanent lines into a seperate list from non-permanent to help optimize
31 * matching speed, but matched in the same way.
32 * Expiry was also sped up by sorting the list by expiry (meaning just remove the items at the
33 * head of the list that are outdated.)
35 * This was fine and good, but it looked less than ideal in code, and matching was still slower
36 * than it could have been, something which we address here.
39 * All lines are (as in v1) stored together -- no seperation of perm and non-perm. Expiry will
40 * still use a sorted list, and we'll just ignore anything permanent.
42 * Application will be by a list of lines 'pending' application, meaning only the newly added lines
43 * will be gone over. Much faster.
45 * More of course is to come.
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.
66 bool InitXLine(ServerConfig* conf, const char* tag)
71 bool DoneZLine(ServerConfig* conf, const char* tag)
73 // XXX we should really only call this once - after we've finished processing configuration all together
74 conf->GetInstance()->XLines->ApplyLines();
78 bool DoneQLine(ServerConfig* conf, const char* tag)
80 // XXX we should really only call this once - after we've finished processing configuration all together
81 conf->GetInstance()->XLines->ApplyLines();
85 bool DoneKLine(ServerConfig* conf, const char* tag)
87 // XXX we should really only call this once - after we've finished processing configuration all together
88 conf->GetInstance()->XLines->ApplyLines();
92 bool DoneELine(ServerConfig* conf, const char* tag)
94 // XXX we should really only call this once - after we've finished processing configuration all together
95 conf->GetInstance()->XLines->ApplyLines();
99 bool DoZLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
101 const char* reason = values[0].GetString();
102 const char* ipmask = values[1].GetString();
104 conf->GetInstance()->XLines->AddZLine(0,"<Config>",reason,ipmask);
108 bool DoQLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
110 const char* reason = values[0].GetString();
111 const char* nick = values[1].GetString();
113 conf->GetInstance()->XLines->AddQLine(0,"<Config>",reason,nick);
117 bool DoKLine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
119 const char* reason = values[0].GetString();
120 const char* host = values[1].GetString();
122 conf->GetInstance()->XLines->AddKLine(0,"<Config>",reason,host);
126 bool DoELine(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
128 const char* reason = values[0].GetString();
129 const char* host = values[1].GetString();
131 conf->GetInstance()->XLines->AddELine(0,"<Config>",reason,host);
135 bool XLine::Matches(User *u)
140 //XXX perhaps move into xlinemanager
141 void CheckELines(InspIRCd *ServerInstance, std::map<std::string, ELine *> &ELines)
143 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
145 User* u = (User*)(*u2);
147 for (std::map<std::string, ELine *>::iterator i = ELines.begin(); i != ELines.end(); i++)
149 ELine *e = i->second;
150 u->exempt = e->Matches(u);
156 IdentHostPair XLineManager::IdentSplit(const std::string &ident_and_host)
158 IdentHostPair n = std::make_pair<std::string,std::string>("*","*");
159 std::string::size_type x = ident_and_host.find('@');
160 if (x != std::string::npos)
162 n.second = ident_and_host.substr(x + 1,ident_and_host.length());
163 n.first = ident_and_host.substr(0, x);
164 if (!n.first.length())
166 if (!n.second.length())
167 n.second.assign("*");
171 n.second = ident_and_host;
179 bool XLineManager::AddGLine(long duration, const char* source,const char* reason,const char* hostmask)
181 IdentHostPair ih = IdentSplit(hostmask);
183 if (DelLine(hostmask, 'G', true))
186 GLine* item = new GLine(ServerInstance, ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());
188 active_lines.push_back(item);
189 sort(active_lines.begin(), active_lines.end(),XLineManager::XSortComparison);
190 pending_lines.push_back(item);
195 // adds an e:line (exception to bans)
197 bool XLineManager::AddELine(long duration, const char* source, const char* reason, const char* hostmask)
199 IdentHostPair ih = IdentSplit(hostmask);
201 if (DelLine(hostmask, 'E', true))
204 ELine* item = new ELine(ServerInstance, ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());
206 active_lines.push_back(item);
207 sort(active_lines.begin(), active_lines.end(),XLineManager::XSortComparison);
208 elines[hostmask] = item;
210 // XXX we really only need to check one line (the new one) - this is a bit wasteful!
211 CheckELines(ServerInstance, elines);
218 bool XLineManager::AddQLine(long duration, const char* source, const char* reason, const char* nickname)
220 if (DelLine(nickname, 'Q', true))
223 QLine* item = new QLine(ServerInstance, ServerInstance->Time(), duration, source, reason, nickname);
225 active_lines.push_back(item);
226 sort(active_lines.begin(), active_lines.end(), XLineManager::XSortComparison);
227 pending_lines.push_back(item);
234 bool XLineManager::AddZLine(long duration, const char* source, const char* reason, const char* ipaddr)
236 if (strchr(ipaddr,'@'))
238 while (*ipaddr != '@')
243 if (DelLine(ipaddr, 'Z', true))
246 ZLine* item = new ZLine(ServerInstance, ServerInstance->Time(), duration, source, reason, ipaddr);
248 active_lines.push_back(item);
249 sort(active_lines.begin(), active_lines.end(),XLineManager::XSortComparison);
250 pending_lines.push_back(item);
257 bool XLineManager::AddKLine(long duration, const char* source, const char* reason, const char* hostmask)
259 IdentHostPair ih = IdentSplit(hostmask);
261 if (DelLine(hostmask, 'K', true))
264 KLine* item = new KLine(ServerInstance, ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());
266 active_lines.push_back(item);
267 sort(active_lines.begin(), active_lines.end(),XLineManager::XSortComparison);
268 pending_lines.push_back(item);
273 // deletes a g:line, returns true if the line existed and was removed
275 bool XLineManager::DelLine(const char* hostmask, char type, bool simulate)
277 IdentHostPair ih = IdentSplit(hostmask);
278 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
280 if ((*i)->type == type)
282 if ((*i)->Matches(hostmask))
288 active_lines.erase(i);
289 /* XXX: Should erase from pending lines here */
302 /* remove exempt from everyone and force recheck after deleting eline */
303 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
305 User* u = (User*)(*u2);
308 ServerInstance->XLines->elines.erase(this->identmask + std::string("@") + this->hostmask);
309 CheckELines(ServerInstance, ServerInstance->XLines->elines);
312 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
314 QLine* XLineManager::matches_qline(const char* nick)
316 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
317 if ((*i)->type == 'Q' && (*i)->Matches(nick))
322 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
324 GLine* XLineManager::matches_gline(User* user)
326 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
327 if ((*i)->type == 'G' && (*i)->Matches(user))
333 ELine* XLineManager::matches_exception(User* user)
338 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
340 if ((*i)->type == 'E' && (*i)->Matches(user))
347 void XLineManager::gline_set_creation_time(const char* host, time_t create_time)
349 /*for (std::vector<XLine*>::iterator i = glines.begin(); i != glines.end(); i++)
351 if (!strcasecmp(host,(*i)->hostmask))
353 (*i)->set_time = create_time;
354 (*i)->expiry = create_time + (*i)->duration;
362 void XLineManager::eline_set_creation_time(const char* host, time_t create_time)
364 /*for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
366 if (!strcasecmp(host,(*i)->hostmask))
368 (*i)->set_time = create_time;
369 (*i)->expiry = create_time + (*i)->duration;
377 void XLineManager::qline_set_creation_time(const char* nick, time_t create_time)
379 /*for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
381 if (!strcasecmp(nick,(*i)->nick))
383 (*i)->set_time = create_time;
384 (*i)->expiry = create_time + (*i)->duration;
392 void XLineManager::zline_set_creation_time(const char* ip, time_t create_time)
394 /*for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
396 if (!strcasecmp(ip,(*i)->ipaddr))
398 (*i)->set_time = create_time;
399 (*i)->expiry = create_time + (*i)->duration;
407 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
409 ZLine* XLineManager::matches_zline(User *u)
411 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
412 if ((*i)->type == 'Z' && (*i)->Matches(u))
417 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
419 KLine* XLineManager::matches_kline(User* user)
421 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
422 if ((*i)->Matches(user))
428 bool XLineManager::XSortComparison(const XLine *one, const XLine *two)
430 // account for permanent lines
431 if (one->expiry == 0)
435 return (one->expiry) < (two->expiry);
438 // removes lines that have expired
439 void XLineManager::expire_lines()
441 time_t current = ServerInstance->Time();
443 /* Because we now store all our XLines in sorted order using ((*i)->duration + (*i)->set_time) as a key, this
444 * means that to expire the XLines we just need to do a while, picking off the top few until there are
445 * none left at the head of the queue that are after the current time.
448 while ((active_lines.size()) && (current > (*active_lines.begin())->expiry) && ((*active_lines.begin())->duration != 0))
450 std::vector<XLine*>::iterator i = active_lines.begin();
451 (*i)->DisplayExpiry();
453 active_lines.erase(i);
458 // applies lines, removing clients and changing nicks etc as applicable
459 void XLineManager::ApplyLines()
461 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
463 User* u = (User*)(*u2);
465 for (std::vector<XLine *>::iterator i = pending_lines.begin(); i != pending_lines.end(); i++)
473 pending_lines.clear();
476 void XLineManager::stats_k(User* user, string_list &results)
478 /*std::string sn = ServerInstance->Config->ServerName;
479 for (std::vector<KLine*>::iterator i = klines.begin(); i != klines.end(); i++)
480 results.push_back(sn+" 216 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);*/
483 void XLineManager::stats_g(User* user, string_list &results)
485 /*std::string sn = ServerInstance->Config->ServerName;
486 for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
487 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);*/
490 void XLineManager::stats_q(User* user, string_list &results)
492 /*std::string sn = ServerInstance->Config->ServerName;
493 for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
494 results.push_back(sn+" 217 "+user->nick+" :"+(*i)->nick+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);*/
497 void XLineManager::stats_z(User* user, string_list &results)
499 /*std::string sn = ServerInstance->Config->ServerName;
500 for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
501 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->ipaddr+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);*/
504 void XLineManager::stats_e(User* user, string_list &results)
506 /*std::string sn = ServerInstance->Config->ServerName;
507 for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
508 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);*/
511 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance)
515 bool XLine::Matches(const std::string &str)
520 void XLine::Apply(User* u)
524 void XLine::DefaultApply(User* u, char line)
527 snprintf(reason, MAXBUF, "%c-Lined: %s", line, this->reason);
528 if (*ServerInstance->Config->MoronBanner)
529 u->WriteServ("NOTICE %s :*** %s", u->nick, ServerInstance->Config->MoronBanner);
530 if (ServerInstance->Config->HideBans)
531 User::QuitUser(ServerInstance, u, line + std::string("-Lined"), reason);
533 User::QuitUser(ServerInstance, u, reason);
536 bool KLine::Matches(User *u)
541 if ((match(u->ident, this->identmask)))
543 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
552 void KLine::Apply(User* u)
554 DefaultApply(u, 'K');
557 bool GLine::Matches(User *u)
562 if ((match(u->ident, this->identmask)))
564 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
573 void GLine::Apply(User* u)
575 DefaultApply(u, 'G');
578 bool ELine::Matches(User *u)
583 if ((match(u->ident, this->identmask)))
585 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
594 bool ZLine::Matches(User *u)
599 if (match(u->GetIPString(), this->ipaddr, true))
605 void ZLine::Apply(User* u)
607 DefaultApply(u, 'Z');
611 bool QLine::Matches(User *u)
616 if (match(u->nick, this->nick))
622 void QLine::Apply(User* u)
624 /* Can we force the user to their uid here instead? */
625 DefaultApply(u, 'Q');
629 bool ZLine::Matches(const std::string &str)
631 if (match(str.c_str(), this->ipaddr, true))
637 bool QLine::Matches(const std::string &str)
639 if (match(str.c_str(), this->nick))
645 void ELine::DisplayExpiry()
647 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed E-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
650 void QLine::DisplayExpiry()
652 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s (set by %s %d seconds ago)",this->nick,this->source,this->duration);
655 void ZLine::DisplayExpiry()
657 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Z-Line %s (set by %s %d seconds ago)",this->ipaddr,this->source,this->duration);
660 void KLine::DisplayExpiry()
662 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed K-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
665 void GLine::DisplayExpiry()
667 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);