]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/xline.cpp
Update docs on how XLine system will be working
[user/henk/code/inspircd.git] / src / xline.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $Core: libIRCDxline */
15
16 #include "inspircd.h"
17 #include "wildcard.h"
18 #include "xline.h"
19
20 /*
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 ..
23  *
24  * Background:
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.
29  *
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.)
34  *
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.
37  *
38  * VERSION 3:
39  *  All lines are (as in v1) stored together -- no seperation of perm and non-perm. They are stored in
40  *  a map of maps (first map is line type, second map is for quick lookup on add/delete/etc).
41  *
42  *  Expiry is *no longer* performed on a timer, and no longer uses a sorted list of any variety. This
43  *  is now done by only checking for expiry when a line is accessed, meaning that expiry is no longer
44  *  a resource intensive problem.
45  *
46  *  Application no longer tries to apply every single line on every single user - instead, now only lines
47  *  added since the previous application are applied. This keeps S2S ADDLINE during burst nice and fast,
48  *  while at the same time not slowing things the fuck down when we try adding a ban with lots of preexisting
49  *  bans. :)
50  */
51
52 bool XLine::Matches(User *u)
53 {
54         return false;
55 }
56
57 /*
58  * Checks what users match a given vector of ELines and sets their ban exempt flag accordingly.
59  */
60 void XLineManager::CheckELines(std::map<std::string, XLine *> &ELines)
61 {
62         if (ELines.empty())
63                 return;
64
65         for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
66         {
67                 User* u = (User*)(*u2);
68
69                 for (std::map<std::string, XLine *>::iterator i = ELines.begin(); i != ELines.end(); i++)
70                 {
71                         XLine *e = i->second;
72                         u->exempt = e->Matches(u);
73                 }
74         }
75 }
76
77 // this should probably be moved to configreader, but atm it relies on CheckELines above.
78 bool DoneELine(ServerConfig* conf, const char* tag)
79 {
80         for (std::vector<User*>::const_iterator u2 = conf->GetInstance()->local_users.begin(); u2 != conf->GetInstance()->local_users.end(); u2++)
81         {
82                 User* u = (User*)(*u2);
83                 u->exempt = false;
84         }
85
86         conf->GetInstance()->XLines->CheckELines(conf->GetInstance()->XLines->lookup_lines['E']);
87         return true;
88 }
89
90
91 IdentHostPair XLineManager::IdentSplit(const std::string &ident_and_host)
92 {
93         IdentHostPair n = std::make_pair<std::string,std::string>("*","*");
94         std::string::size_type x = ident_and_host.find('@');
95         if (x != std::string::npos)
96         {
97                 n.second = ident_and_host.substr(x + 1,ident_and_host.length());
98                 n.first = ident_and_host.substr(0, x);
99                 if (!n.first.length())
100                         n.first.assign("*");
101                 if (!n.second.length())
102                         n.second.assign("*");
103         }
104         else
105         {
106                 n.second = ident_and_host;
107         }
108
109         return n;
110 }
111
112 // adds a g:line
113
114 /*bool XLineManager::AddELine(long duration, const char* source, const char* reason, const char* hostmask)*/
115 bool XLineManager::AddLine(XLine* line, User* user)
116 {
117         /*IdentHostPair ih = IdentSplit(hostmask);*/
118
119         if (DelLine(line->Displayable(), line->type, user, true))
120                 return false;
121
122         /*ELine* item = new ELine(ServerInstance, ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());*/
123
124         active_lines.push_back(line);
125         sort(active_lines.begin(), active_lines.end(), XLineManager::XSortComparison);
126         pending_lines.push_back(line);
127         lookup_lines[line->type][line->Displayable()] = line;
128         line->OnAdd();
129
130         if (!line->duration)
131                 PermLines++;
132
133         FOREACH_MOD(I_OnAddLine,OnAddLine(user, line)); 
134
135         return true;
136 }
137
138 /*bool XLineManager::AddZLine(long duration, const char* source, const char* reason, const char* ipaddr)
139 {
140         if (strchr(ipaddr,'@'))
141         {
142                 while (*ipaddr != '@')
143                         ipaddr++;
144                 ipaddr++;
145         }*/
146
147 // deletes a g:line, returns true if the line existed and was removed
148
149 bool XLineManager::DelLine(const char* hostmask, char type, User* user, bool simulate)
150 {
151         IdentHostPair ih = IdentSplit(hostmask);
152         for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
153         {
154                 if ((*i)->type == type)
155                 {
156                         if ((*i)->MatchesLiteral(hostmask))
157                         {
158                                 if (!simulate)
159                                 {
160                                         (*i)->Unset();
161
162                                         if (lookup_lines.find(type) != lookup_lines.end())
163                                                 lookup_lines[type].erase(hostmask);
164
165                                         FOREACH_MOD(I_OnDelLine,OnDelLine(user, *i));
166
167                                         std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), *i);                                 
168                                         if (pptr != pending_lines.end())
169                                                 pending_lines.erase(pptr);
170
171                                         if (!(*i)->duration)
172                                                 PermLines--;
173
174                                         delete *i;
175                                         active_lines.erase(i);
176                                 }
177                                 return true;
178                         }
179                 }
180         }
181
182         return false;
183 }
184
185
186 void ELine::Unset()
187 {
188         /* remove exempt from everyone and force recheck after deleting eline */
189         for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
190         {
191                 User* u = (User*)(*u2);
192                 u->exempt = false;
193         }
194
195         if (ServerInstance->XLines->lookup_lines.find('E') != ServerInstance->XLines->lookup_lines.end())
196                 ServerInstance->XLines->CheckELines(ServerInstance->XLines->lookup_lines['E']);
197 }
198
199 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
200
201 QLine* XLineManager::matches_qline(const char* nick)
202 {
203         if (lookup_lines.find('Q') == lookup_lines.end())
204                 return NULL;
205
206         if (lookup_lines.find('Q') != lookup_lines.end() && lookup_lines['Q'].empty())
207                 return NULL;
208
209         for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
210                 if ((*i)->type == 'Q' && (*i)->Matches(nick))
211                         return (QLine*)(*i);
212         return NULL;
213 }
214
215 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
216
217 GLine* XLineManager::matches_gline(User* user)
218 {
219         if (lookup_lines.find('G') == lookup_lines.end())
220                 return NULL;
221
222         if (lookup_lines.find('G') != lookup_lines.end() && lookup_lines['G'].empty())
223                 return NULL;
224
225         for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
226                 if ((*i)->type == 'G' && (*i)->Matches(user))
227                         return (GLine*)(*i);
228
229         return NULL;
230 }
231
232 ELine* XLineManager::matches_exception(User* user)
233 {
234         if (lookup_lines.find('E') == lookup_lines.end())
235                 return NULL;
236
237         if (lookup_lines.find('E') != lookup_lines.end() && lookup_lines['E'].empty())
238                 return NULL;
239
240         for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
241         {
242                 if ((*i)->type == 'E' && (*i)->Matches(user))
243                         return (ELine*)(*i);
244         }
245         return NULL;
246 }
247
248 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
249
250 ZLine* XLineManager::matches_zline(User *u)
251 {
252         if (lookup_lines.find('Z') == lookup_lines.end())
253                 return NULL;
254
255         if (lookup_lines.find('Z') != lookup_lines.end() && lookup_lines['Z'].empty())
256                 return NULL;
257
258         for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
259                 if ((*i)->type == 'Z' && (*i)->Matches(u))
260                         return (ZLine*)(*i);
261         return NULL;
262 }
263
264 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
265
266 KLine* XLineManager::matches_kline(User* user)
267 {
268         if (lookup_lines.find('K') == lookup_lines.end())
269                 return NULL;
270
271         if (lookup_lines.find('K') != lookup_lines.end() && lookup_lines['K'].empty())
272                 return NULL;
273
274         for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
275                 if ((*i)->Matches(user))
276                         return (KLine*)(*i);
277
278         return NULL;
279 }
280
281 bool XLineManager::XSortComparison(const XLine *one, const XLine *two)
282 {
283         return (one->expiry) < (two->expiry);
284 }
285
286 // removes lines that have expired
287 void XLineManager::expire_lines()
288 {
289         time_t current = ServerInstance->Time();
290
291         /* Because we now store all our XLines in sorted order using ((*i)->duration + (*i)->set_time) as a key, this
292          * means that to expire the XLines we just need to do a while, picking off the top few until there are
293          * none left at the head of the queue that are after the current time. We use PermLines as an offset into the
294          * vector past the first item with a duration 0.
295          */
296
297         std::vector<XLine*>::iterator start = active_lines.begin() + PermLines;
298
299         while ((start < active_lines.end()) && (current > (*start)->expiry))
300         {
301                 (*start)->DisplayExpiry();
302                 (*start)->Unset();
303
304                 if (lookup_lines.find((*start)->type) != lookup_lines.end())
305                         lookup_lines[(*start)->type].erase((*start)->Displayable());
306
307                 std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), *start);
308                 if (pptr != pending_lines.end())
309                         pending_lines.erase(pptr);
310
311                 if (!(*start)->duration)
312                         PermLines--;
313
314                 delete *start;
315                 active_lines.erase(start);
316         }
317 }
318
319 // applies lines, removing clients and changing nicks etc as applicable
320 void XLineManager::ApplyLines()
321 {
322         for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
323         {
324                 User* u = (User*)(*u2);
325
326                 for (std::vector<XLine *>::iterator i = pending_lines.begin(); i != pending_lines.end(); i++)
327                 {
328                         XLine *x = *i;
329                         if (x->Matches(u))
330                                 x->Apply(u);
331                 }
332         }
333
334         pending_lines.clear();
335 }
336
337 /* k: 216
338  * g: 223
339  * q: 217
340  * z: 223
341  * e: 223
342  */
343
344 void XLineManager::InvokeStats(const char type, int numeric, User* user, string_list &results)
345 {
346         std::string sn = ServerInstance->Config->ServerName;
347
348         std::map<const char, std::map<std::string, XLine*> >::iterator n = lookup_lines.find(type);
349
350         if (n != lookup_lines.end())
351         {
352                 std::map<std::string, XLine*>& list = n->second;
353                 for (std::map<std::string, XLine*>::iterator i = list.begin(); i != list.end(); i++)
354                         results.push_back(sn+" "+ConvToStr(numeric)+" "+user->nick+" :"+i->second->Displayable()+" "+
355                                         ConvToStr(i->second->set_time)+" "+ConvToStr(i->second->duration)+" "+std::string(i->second->source)+" :"+(i->second->reason));
356         }
357 }
358
359
360 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance), PermLines(0)
361 {
362         GFact = new GLineFactory(Instance);
363         EFact = new ELineFactory(Instance);
364         KFact = new KLineFactory(Instance);
365         QFact = new QLineFactory(Instance);
366         ZFact = new ZLineFactory(Instance);
367
368         RegisterFactory(GFact);
369         RegisterFactory(EFact);
370         RegisterFactory(KFact);
371         RegisterFactory(QFact);
372         RegisterFactory(ZFact);
373 }
374
375 XLineManager::~XLineManager()
376 {
377         UnregisterFactory(GFact);
378         UnregisterFactory(EFact);
379         UnregisterFactory(KFact);
380         UnregisterFactory(QFact);
381         UnregisterFactory(ZFact);
382
383         delete GFact;
384         delete EFact;
385         delete KFact;
386         delete QFact;
387         delete ZFact;
388 }
389
390 void XLine::Apply(User* u)
391 {
392 }
393
394 void XLine::DefaultApply(User* u, char line)
395 {
396         char reason[MAXBUF];
397         snprintf(reason, MAXBUF, "%c-Lined: %s", line, this->reason);
398         if (*ServerInstance->Config->MoronBanner)
399                 u->WriteServ("NOTICE %s :*** %s", u->nick, ServerInstance->Config->MoronBanner);
400         if (ServerInstance->Config->HideBans)
401                 User::QuitUser(ServerInstance, u, line + std::string("-Lined"), reason);
402         else
403                 User::QuitUser(ServerInstance, u, reason);
404 }
405
406 bool KLine::Matches(User *u)
407 {
408         if (u->exempt)
409                 return false;
410
411         if ((match(u->ident, this->identmask)))
412         {
413                 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
414                 {
415                         return true;
416                 }
417         }
418
419         return false;
420 }
421
422 void KLine::Apply(User* u)
423 {
424         DefaultApply(u, 'K');
425 }
426
427 bool GLine::Matches(User *u)
428 {
429         if (u->exempt)
430                 return false;
431
432         if ((match(u->ident, this->identmask)))
433         {
434                 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
435                 {
436                         return true;
437                 }
438         }
439
440         return false;
441 }
442
443 void GLine::Apply(User* u)
444 {       
445         DefaultApply(u, 'G');
446 }
447
448 bool ELine::Matches(User *u)
449 {
450         if (u->exempt)
451                 return false;
452
453         if ((match(u->ident, this->identmask)))
454         {
455                 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
456                 {
457                         return true;
458                 }
459         }
460
461         return false;
462 }
463
464 bool ZLine::Matches(User *u)
465 {
466         if (u->exempt)
467                 return false;
468
469         if (match(u->GetIPString(), this->ipaddr, true))
470                 return true;
471         else
472                 return false;
473 }
474
475 void ZLine::Apply(User* u)
476 {       
477         DefaultApply(u, 'Z');
478 }
479
480
481 bool QLine::Matches(User *u)
482 {
483         if (u->exempt)
484                 return false;
485
486         if (match(u->nick, this->nick))
487                 return true;
488
489         return false;
490 }
491
492 void QLine::Apply(User* u)
493 {       
494         /* Can we force the user to their uid here instead? */
495         DefaultApply(u, 'Q');
496 }
497
498
499 bool ZLine::Matches(const std::string &str)
500 {
501         if (match(str.c_str(), this->ipaddr, true))
502                 return true;
503         else
504                 return false;
505 }
506
507 bool QLine::Matches(const std::string &str)
508 {
509         if (match(str.c_str(), this->nick))
510                 return true;
511
512         return false;
513 }
514
515 bool ELine::Matches(const std::string &str)
516 {
517         return ((match(str.c_str(), matchtext.c_str(), true)));
518 }
519
520 bool KLine::Matches(const std::string &str)
521 {
522         return ((match(str.c_str(), matchtext.c_str(), true)));
523 }
524
525 bool GLine::Matches(const std::string &str)
526 {
527         return ((match(str.c_str(), matchtext.c_str(), true)));
528 }
529
530 bool ELine::MatchesLiteral(const std::string &str)
531 {
532         return (assign(str) == matchtext);
533 }
534
535 bool ZLine::MatchesLiteral(const std::string &str)
536 {       
537         return (assign(str) == this->ipaddr);
538 }
539
540 bool GLine::MatchesLiteral(const std::string &str)
541 {       
542         return (assign(str) == matchtext);
543 }
544
545 bool KLine::MatchesLiteral(const std::string &str)
546 {       
547         return (assign(str) == matchtext);
548 }
549
550 bool QLine::MatchesLiteral(const std::string &str)
551 {       
552         return (assign(str) == this->nick);
553 }
554
555 void ELine::OnAdd()
556 {
557         /* When adding one eline, only check the one eline */
558         for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
559         {
560                 User* u = (User*)(*u2);
561                 if (this->Matches(u))
562                         u->exempt = true;
563         }
564 }
565
566 void ELine::DisplayExpiry()
567 {
568         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed E-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
569 }
570
571 void QLine::DisplayExpiry()
572 {
573         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Q-Line %s (set by %s %d seconds ago)",this->nick,this->source,this->duration);
574 }
575
576 void ZLine::DisplayExpiry()
577 {
578         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Z-Line %s (set by %s %d seconds ago)",this->ipaddr,this->source,this->duration);
579 }
580
581 void KLine::DisplayExpiry()
582 {
583         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed K-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
584 }
585
586 void GLine::DisplayExpiry()
587 {
588         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
589 }
590
591 const char* ELine::Displayable()
592 {
593         return matchtext.c_str();
594 }
595
596 const char* KLine::Displayable()
597 {
598         return matchtext.c_str();
599 }
600
601 const char* GLine::Displayable()
602 {
603         return matchtext.c_str();
604 }
605
606 const char* ZLine::Displayable()
607 {
608         return ipaddr;
609 }
610
611 const char* QLine::Displayable()
612 {
613         return nick;
614 }
615
616 bool XLineManager::RegisterFactory(XLineFactory* xlf)
617 {
618         std::map<char, XLineFactory*>::iterator n = line_factory.find(xlf->GetType());
619
620         if (n != line_factory.end())
621                 return false;
622
623         line_factory[xlf->GetType()] = xlf;
624
625         return true;
626 }
627
628 bool XLineManager::UnregisterFactory(XLineFactory* xlf)
629 {
630         std::map<char, XLineFactory*>::iterator n = line_factory.find(xlf->GetType());
631
632         if (n == line_factory.end())
633                 return false;
634
635         line_factory.erase(n);
636
637         return true;
638 }
639
640 XLineFactory* XLineManager::GetFactory(const char type)
641 {
642         std::map<char, XLineFactory*>::iterator n = line_factory.find(type);
643
644         if (n != line_factory.end())
645                 return NULL;
646
647         return n->second;
648 }
649