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, XLine *> &ELines)
146 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
148 User* u = (User*)(*u2);
150 for (std::map<std::string, XLine *>::iterator i = ELines.begin(); i != ELines.end(); i++)
152 XLine *e = i->second;
153 u->exempt = e->Matches(u);
159 IdentHostPair XLineManager::IdentSplit(const std::string &ident_and_host)
161 IdentHostPair n = std::make_pair<std::string,std::string>("*","*");
162 std::string::size_type x = ident_and_host.find('@');
163 if (x != std::string::npos)
165 n.second = ident_and_host.substr(x + 1,ident_and_host.length());
166 n.first = ident_and_host.substr(0, x);
167 if (!n.first.length())
169 if (!n.second.length())
170 n.second.assign("*");
174 n.second = ident_and_host;
182 bool XLineManager::AddGLine(long duration, const char* source,const char* reason,const char* hostmask)
184 IdentHostPair ih = IdentSplit(hostmask);
186 if (DelLine(hostmask, 'G', true))
189 GLine* item = new GLine(ServerInstance, ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());
191 active_lines.push_back(item);
192 sort(active_lines.begin(), active_lines.end(),XLineManager::XSortComparison);
193 pending_lines.push_back(item);
194 lookup_lines['G'][hostmask] = item;
199 // adds an e:line (exception to bans)
201 bool XLineManager::AddELine(long duration, const char* source, const char* reason, const char* hostmask)
203 IdentHostPair ih = IdentSplit(hostmask);
205 if (DelLine(hostmask, 'E', true))
208 ELine* item = new ELine(ServerInstance, ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());
210 active_lines.push_back(item);
211 sort(active_lines.begin(), active_lines.end(),XLineManager::XSortComparison);
212 lookup_lines['E'][hostmask] = item;
214 // XXX we really only need to check one line (the new one) - this is a bit wasteful!
215 CheckELines(ServerInstance, lookup_lines['E']);
222 bool XLineManager::AddQLine(long duration, const char* source, const char* reason, const char* nickname)
224 if (DelLine(nickname, 'Q', true))
227 QLine* item = new QLine(ServerInstance, ServerInstance->Time(), duration, source, reason, nickname);
229 active_lines.push_back(item);
230 sort(active_lines.begin(), active_lines.end(), XLineManager::XSortComparison);
231 pending_lines.push_back(item);
232 lookup_lines['Q'][nickname] = item;
239 bool XLineManager::AddZLine(long duration, const char* source, const char* reason, const char* ipaddr)
241 if (strchr(ipaddr,'@'))
243 while (*ipaddr != '@')
248 if (DelLine(ipaddr, 'Z', true))
251 ZLine* item = new ZLine(ServerInstance, ServerInstance->Time(), duration, source, reason, ipaddr);
253 active_lines.push_back(item);
254 sort(active_lines.begin(), active_lines.end(),XLineManager::XSortComparison);
255 pending_lines.push_back(item);
256 lookup_lines['Z'][ipaddr] = item;
263 bool XLineManager::AddKLine(long duration, const char* source, const char* reason, const char* hostmask)
265 IdentHostPair ih = IdentSplit(hostmask);
267 if (DelLine(hostmask, 'K', true))
270 KLine* item = new KLine(ServerInstance, ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());
272 active_lines.push_back(item);
273 sort(active_lines.begin(), active_lines.end(),XLineManager::XSortComparison);
274 pending_lines.push_back(item);
275 lookup_lines['K'][hostmask] = item;
280 // deletes a g:line, returns true if the line existed and was removed
282 bool XLineManager::DelLine(const char* hostmask, char type, bool simulate)
284 IdentHostPair ih = IdentSplit(hostmask);
285 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
287 if ((*i)->type == type)
289 if ((*i)->Matches(hostmask))
295 active_lines.erase(i);
296 if (lookup_lines.find(type) != lookup_lines.end())
297 lookup_lines[type].erase(hostmask);
298 /* XXX: Should erase from pending lines here */
311 /* remove exempt from everyone and force recheck after deleting eline */
312 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
314 User* u = (User*)(*u2);
318 if (ServerInstance->XLines->lookup_lines.find('E') != ServerInstance->XLines->lookup_lines.end())
319 CheckELines(ServerInstance, ServerInstance->XLines->lookup_lines['E']);
322 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
324 QLine* XLineManager::matches_qline(const char* nick)
326 if (lookup_lines.find('Q') != lookup_lines.end() && lookup_lines['Q'].empty())
329 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
330 if ((*i)->type == 'Q' && (*i)->Matches(nick))
335 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
337 GLine* XLineManager::matches_gline(User* user)
339 if (lookup_lines.find('G') != lookup_lines.end() && lookup_lines['G'].empty())
342 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
343 if ((*i)->type == 'G' && (*i)->Matches(user))
349 ELine* XLineManager::matches_exception(User* user)
351 if (lookup_lines.find('E') != lookup_lines.end() && lookup_lines['E'].empty())
354 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
356 if ((*i)->type == 'E' && (*i)->Matches(user))
363 void XLineManager::gline_set_creation_time(const char* host, time_t create_time)
365 /*for (std::vector<XLine*>::iterator i = glines.begin(); i != glines.end(); i++)
367 if (!strcasecmp(host,(*i)->hostmask))
369 (*i)->set_time = create_time;
370 (*i)->expiry = create_time + (*i)->duration;
378 void XLineManager::eline_set_creation_time(const char* host, time_t create_time)
380 /*for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
382 if (!strcasecmp(host,(*i)->hostmask))
384 (*i)->set_time = create_time;
385 (*i)->expiry = create_time + (*i)->duration;
393 void XLineManager::qline_set_creation_time(const char* nick, time_t create_time)
395 /*for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
397 if (!strcasecmp(nick,(*i)->nick))
399 (*i)->set_time = create_time;
400 (*i)->expiry = create_time + (*i)->duration;
408 void XLineManager::zline_set_creation_time(const char* ip, time_t create_time)
410 /*for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
412 if (!strcasecmp(ip,(*i)->ipaddr))
414 (*i)->set_time = create_time;
415 (*i)->expiry = create_time + (*i)->duration;
423 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
425 ZLine* XLineManager::matches_zline(User *u)
427 if (lookup_lines.find('Z') != lookup_lines.end() && lookup_lines['Z'].empty())
430 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
431 if ((*i)->type == 'Z' && (*i)->Matches(u))
436 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
438 KLine* XLineManager::matches_kline(User* user)
440 if (lookup_lines.find('K') != lookup_lines.end() && lookup_lines['K'].empty())
443 for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
444 if ((*i)->Matches(user))
450 bool XLineManager::XSortComparison(const XLine *one, const XLine *two)
452 // account for permanent lines
453 if (one->expiry == 0)
457 return (one->expiry) < (two->expiry);
460 // removes lines that have expired
461 void XLineManager::expire_lines()
463 time_t current = ServerInstance->Time();
465 /* Because we now store all our XLines in sorted order using ((*i)->duration + (*i)->set_time) as a key, this
466 * means that to expire the XLines we just need to do a while, picking off the top few until there are
467 * none left at the head of the queue that are after the current time.
470 while ((active_lines.size()) && (current > (*active_lines.begin())->expiry) && ((*active_lines.begin())->duration != 0))
472 std::vector<XLine*>::iterator i = active_lines.begin();
473 (*i)->DisplayExpiry();
475 active_lines.erase(i);
480 // applies lines, removing clients and changing nicks etc as applicable
481 void XLineManager::ApplyLines()
483 for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
485 User* u = (User*)(*u2);
487 for (std::vector<XLine *>::iterator i = pending_lines.begin(); i != pending_lines.end(); i++)
495 pending_lines.clear();
498 void XLineManager::stats_k(User* user, string_list &results)
500 /*std::string sn = ServerInstance->Config->ServerName;
501 for (std::vector<KLine*>::iterator i = klines.begin(); i != klines.end(); i++)
502 results.push_back(sn+" 216 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);*/
505 void XLineManager::stats_g(User* user, string_list &results)
507 /*std::string sn = ServerInstance->Config->ServerName;
508 for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
509 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);*/
512 void XLineManager::stats_q(User* user, string_list &results)
514 /*std::string sn = ServerInstance->Config->ServerName;
515 for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
516 results.push_back(sn+" 217 "+user->nick+" :"+(*i)->nick+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);*/
519 void XLineManager::stats_z(User* user, string_list &results)
521 /*std::string sn = ServerInstance->Config->ServerName;
522 for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
523 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->ipaddr+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);*/
526 void XLineManager::stats_e(User* user, string_list &results)
528 /*std::string sn = ServerInstance->Config->ServerName;
529 for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
530 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);*/
533 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance)
537 bool XLine::Matches(const std::string &str)
542 void XLine::Apply(User* u)
546 void XLine::DefaultApply(User* u, char line)
549 snprintf(reason, MAXBUF, "%c-Lined: %s", line, this->reason);
550 if (*ServerInstance->Config->MoronBanner)
551 u->WriteServ("NOTICE %s :*** %s", u->nick, ServerInstance->Config->MoronBanner);
552 if (ServerInstance->Config->HideBans)
553 User::QuitUser(ServerInstance, u, line + std::string("-Lined"), reason);
555 User::QuitUser(ServerInstance, u, reason);
558 bool KLine::Matches(User *u)
563 if ((match(u->ident, this->identmask)))
565 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
574 void KLine::Apply(User* u)
576 DefaultApply(u, 'K');
579 bool GLine::Matches(User *u)
584 if ((match(u->ident, this->identmask)))
586 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
595 void GLine::Apply(User* u)
597 DefaultApply(u, 'G');
600 bool ELine::Matches(User *u)
605 if ((match(u->ident, this->identmask)))
607 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
616 bool ZLine::Matches(User *u)
621 if (match(u->GetIPString(), this->ipaddr, true))
627 void ZLine::Apply(User* u)
629 DefaultApply(u, 'Z');
633 bool QLine::Matches(User *u)
638 if (match(u->nick, this->nick))
644 void QLine::Apply(User* u)
646 /* Can we force the user to their uid here instead? */
647 DefaultApply(u, 'Q');
651 bool ZLine::Matches(const std::string &str)
653 if (match(str.c_str(), this->ipaddr, true))
659 bool QLine::Matches(const std::string &str)
661 if (match(str.c_str(), this->nick))
667 void ELine::DisplayExpiry()
669 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed E-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
672 void QLine::DisplayExpiry()
674 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s (set by %s %d seconds ago)",this->nick,this->source,this->duration);
677 void ZLine::DisplayExpiry()
679 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Z-Line %s (set by %s %d seconds ago)",this->ipaddr,this->source,this->duration);
682 void KLine::DisplayExpiry()
684 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed K-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
687 void GLine::DisplayExpiry()
689 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);