]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/xline.cpp
I gots a better fix :p
[user/henk/code/inspircd.git] / src / xline.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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 #include "bancache.h"
20
21 /*
22  * This is now version 3 of the XLine subsystem, let's see if we can get it as nice and 
23  * efficient as we can this time so we can close this file and never ever touch it again ..
24  *
25  * Background:
26  *  Version 1 stored all line types in one list (one for g, one for z, etc). This was fine,
27  *  but both version 1 and 2 suck at applying lines efficiently. That is, every time a new line
28  *  was added, it iterated every existing line for every existing user. Ow. Expiry was also
29  *  expensive, as the lists were NOT sorted.
30  *
31  *  Version 2 moved permanent lines into a seperate list from non-permanent to help optimize
32  *  matching speed, but matched in the same way.
33  *  Expiry was also sped up by sorting the list by expiry (meaning just remove the items at the
34  *  head of the list that are outdated.)
35  *
36  * This was fine and good, but it looked less than ideal in code, and matching was still slower
37  * than it could have been, something which we address here.
38  *
39  * VERSION 3:
40  *  All lines are (as in v1) stored together -- no seperation of perm and non-perm. They are stored in
41  *  a map of maps (first map is line type, second map is for quick lookup on add/delete/etc).
42  *
43  *  Expiry is *no longer* performed on a timer, and no longer uses a sorted list of any variety. This
44  *  is now done by only checking for expiry when a line is accessed, meaning that expiry is no longer
45  *  a resource intensive problem.
46  *
47  *  Application no longer tries to apply every single line on every single user - instead, now only lines
48  *  added since the previous application are applied. This keeps S2S ADDLINE during burst nice and fast,
49  *  while at the same time not slowing things the fuck down when we try adding a ban with lots of preexisting
50  *  bans. :)
51  */
52
53 bool XLine::Matches(User *u)
54 {
55         return false;
56 }
57
58 /*
59  * Checks what users match a given vector of ELines and sets their ban exempt flag accordingly.
60  */
61 void XLineManager::CheckELines()
62 {
63         ContainerIter n = lookup_lines.find("E");
64
65         if (n == lookup_lines.end())
66                 return;
67
68         XLineLookup& ELines = n->second;
69
70         if (ELines.empty())
71                 return;
72
73         for (std::vector<User*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
74         {
75                 User* u = (User*)(*u2);
76
77                 /* This uses safe iteration to ensure that if a line expires here, it doenst trash the iterator */
78                 LookupIter safei;
79
80                 for (LookupIter i = ELines.begin(); i != ELines.end(); )
81                 {
82                         safei = i;
83                         safei++;
84
85                         XLine *e = i->second;
86                         u->exempt = e->Matches(u);
87
88                         i = safei;
89                 }
90         }
91 }
92
93
94 XLineLookup* XLineManager::GetAll(const std::string &type)
95 {
96         ContainerIter n = lookup_lines.find(type);
97
98         if (n == lookup_lines.end())
99                 return NULL;
100
101         LookupIter safei;
102         const time_t current = ServerInstance->Time();
103
104         /* Expire any dead ones, before sending */
105         for (LookupIter x = n->second.begin(); x != n->second.end(); )
106         {
107                 safei = x;
108                 safei++;
109                 if (x->second->duration && current > x->second->expiry)
110                 {
111                         ExpireLine(n, x);
112                 }
113                 x = safei;
114         }
115
116         return &(n->second);
117 }
118
119 std::vector<std::string> XLineManager::GetAllTypes()
120 {
121         std::vector<std::string> items;
122         for (ContainerIter x = lookup_lines.begin(); x != lookup_lines.end(); ++x)
123                 items.push_back(x->first);
124         return items;
125 }
126
127 IdentHostPair XLineManager::IdentSplit(const std::string &ident_and_host)
128 {
129         IdentHostPair n = std::make_pair<std::string,std::string>("*","*");
130         std::string::size_type x = ident_and_host.find('@');
131         if (x != std::string::npos)
132         {
133                 n.second = ident_and_host.substr(x + 1,ident_and_host.length());
134                 n.first = ident_and_host.substr(0, x);
135                 if (!n.first.length())
136                         n.first.assign("*");
137                 if (!n.second.length())
138                         n.second.assign("*");
139         }
140         else
141         {
142                 n.first = "";
143                 n.second = ident_and_host;
144         }
145
146         return n;
147 }
148
149 // adds a line
150
151 bool XLineManager::AddLine(XLine* line, User* user)
152 {
153         /*IdentHostPair ih = IdentSplit(hostmask);*/
154
155         ServerInstance->BanCache->RemoveEntries(line->type, false); // XXX perhaps remove ELines here?
156
157         if (DelLine(line->Displayable(), line->type, user, true))
158                 return false;
159
160         /*ELine* item = new ELine(ServerInstance, ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());*/
161         pending_lines.push_back(line);
162         lookup_lines[line->type][line->Displayable()] = line;
163         line->OnAdd();
164
165         FOREACH_MOD(I_OnAddLine,OnAddLine(user, line)); 
166
167         return true;
168 }
169
170 // deletes a line, returns true if the line existed and was removed
171
172 bool XLineManager::DelLine(const char* hostmask, const std::string &type, User* user, bool simulate)
173 {
174         ContainerIter x = lookup_lines.find(type);
175
176         if (x == lookup_lines.end())
177                 return false;
178
179         LookupIter y = x->second.find(hostmask);
180
181         if (y == x->second.end())
182                 return false;
183
184         if (simulate)
185                 return true;
186
187         ServerInstance->BanCache->RemoveEntries(y->second->type, true);
188
189         FOREACH_MOD(I_OnDelLine,OnDelLine(user, y->second));
190
191         y->second->Unset();
192
193         std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), y->second);
194         if (pptr != pending_lines.end())
195                 pending_lines.erase(pptr);
196
197         delete y->second;
198         x->second.erase(y);
199
200         return true;
201 }
202
203
204 void ELine::Unset()
205 {
206         /* remove exempt from everyone and force recheck after deleting eline */
207         for (std::vector<User*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
208         {
209                 User* u = (User*)(*u2);
210                 u->exempt = false;
211         }
212
213         ServerInstance->XLines->CheckELines();
214 }
215
216 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
217
218 XLine* XLineManager::MatchesLine(const std::string &type, User* user)
219 {
220         ContainerIter x = lookup_lines.find(type);
221
222         if (x == lookup_lines.end())
223                 return NULL;
224
225         const time_t current = ServerInstance->Time();
226
227         LookupIter safei;
228
229         for (LookupIter i = x->second.begin(); i != x->second.end(); )
230         {
231                 safei = i;
232                 safei++;
233
234                 if (i->second->Matches(user))
235                 {
236                         if (i->second->duration && current > i->second->expiry)
237                         {
238                                 /* Expire the line, return nothing */
239                                 ExpireLine(x, i);
240                                 /* Continue, there may be another that matches
241                                  * (thanks aquanight)
242                                  */
243                                 i = safei;
244                                 continue;
245                         }
246                         else
247                                 return i->second;
248                 }
249
250                 i = safei;
251         }
252         return NULL;
253 }
254
255 XLine* XLineManager::MatchesLine(const std::string &type, const std::string &pattern)
256 {
257         ContainerIter x = lookup_lines.find(type);
258
259         if (x == lookup_lines.end())
260                 return NULL;
261
262         const time_t current = ServerInstance->Time();
263
264          LookupIter safei;
265
266         for (LookupIter i = x->second.begin(); i != x->second.end(); )
267         {
268                 safei = i;
269                 safei++;
270
271                 if (i->second->Matches(pattern))
272                 {
273                         if (i->second->duration && current > i->second->expiry)
274                         {
275                                 /* Expire the line, return nothing */
276                                 ExpireLine(x, i);
277                                 /* See above */
278                                 i = safei;
279                                 continue;
280                         }
281                         else
282                                 return i->second;
283                 }
284
285                 i = safei;
286         }
287         return NULL;
288 }
289
290 // removes lines that have expired
291 void XLineManager::ExpireLine(ContainerIter container, LookupIter item)
292 {
293         FOREACH_MOD(I_OnExpireLine, OnExpireLine(item->second));
294
295         item->second->DisplayExpiry();
296         item->second->Unset();
297
298         /* TODO: Can we skip this loop by having a 'pending' field in the XLine class, which is set when a line
299          * is pending, cleared when it is no longer pending, so we skip over this loop if its not pending?
300          * -- Brain
301          */
302         std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), item->second);
303         if (pptr != pending_lines.end())
304                 pending_lines.erase(pptr);
305
306         delete item->second;
307         container->second.erase(item);
308 }
309
310
311 // applies lines, removing clients and changing nicks etc as applicable
312 void XLineManager::ApplyLines()
313 {
314         for (std::vector<User*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
315         {
316                 User* u = (User*)(*u2);
317
318                 for (std::vector<XLine *>::iterator i = pending_lines.begin(); i != pending_lines.end(); i++)
319                 {
320                         XLine *x = *i;
321                         if (x->Matches(u))
322                                 x->Apply(u);
323                 }
324         }
325
326         pending_lines.clear();
327 }
328
329 void XLineManager::InvokeStats(const std::string &type, int numeric, User* user, string_list &results)
330 {
331         std::string sn = ServerInstance->Config->ServerName;
332
333         ContainerIter n = lookup_lines.find(type);
334
335         time_t current = ServerInstance->Time();
336
337         LookupIter safei;
338
339         if (n != lookup_lines.end())
340         {
341                 XLineLookup& list = n->second;
342                 for (LookupIter i = list.begin(); i != list.end(); )
343                 {
344                         safei = i;
345                         safei++;
346
347                         if (i->second->duration && current > i->second->expiry)
348                         {
349                                 ExpireLine(n, i);
350                         }
351                         else
352                                 results.push_back(sn+" "+ConvToStr(numeric)+" "+user->nick+" :"+i->second->Displayable()+" "+
353                                         ConvToStr(i->second->set_time)+" "+ConvToStr(i->second->duration)+" "+std::string(i->second->source)+" :"+(i->second->reason));
354                         i = safei;
355                 }
356         }
357 }
358
359
360 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance)
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 bool XLine::IsBurstable()
395 {
396         return true;
397 }
398
399 void XLine::DefaultApply(User* u, const std::string &line, bool bancache)
400 {
401         char sreason[MAXBUF];
402         snprintf(sreason, MAXBUF, "%s-Lined: %s", line.c_str(), this->reason);
403         if (*ServerInstance->Config->MoronBanner)
404                 u->WriteServ("NOTICE %s :*** %s", u->nick, ServerInstance->Config->MoronBanner);
405         if (ServerInstance->Config->HideBans)
406                 User::QuitUser(ServerInstance, u, line + "-Lined", sreason);
407         else
408                 User::QuitUser(ServerInstance, u, sreason);
409
410
411         if (bancache)
412         {
413                 ServerInstance->Log(DEBUG, std::string("BanCache: Adding positive hit (") + line + ") for " + u->GetIPString());
414                 ServerInstance->BanCache->AddHit(u->GetIPString(), this->type, line + "-Lined: " + this->reason);
415         }
416 }
417
418 bool KLine::Matches(User *u)
419 {
420         if (u->exempt)
421                 return false;
422
423         if ((match(u->ident, this->identmask)))
424         {
425                 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
426                 {
427                         return true;
428                 }
429         }
430
431         return false;
432 }
433
434 void KLine::Apply(User* u)
435 {
436         DefaultApply(u, "K", (strcmp(this->identmask, "*") == 0) ? true : false);
437 }
438
439 bool GLine::Matches(User *u)
440 {
441         if (u->exempt)
442                 return false;
443
444         if ((match(u->ident, this->identmask)))
445         {
446                 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
447                 {
448                         return true;
449                 }
450         }
451
452         return false;
453 }
454
455 void GLine::Apply(User* u)
456 {       
457         DefaultApply(u, "G", (strcmp(this->identmask, "*") == 0) ? true : false);
458 }
459
460 bool ELine::Matches(User *u)
461 {
462         if (u->exempt)
463                 return false;
464
465         if ((match(u->ident, this->identmask)))
466         {
467                 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
468                 {
469                         return true;
470                 }
471         }
472
473         return false;
474 }
475
476 bool ZLine::Matches(User *u)
477 {
478         if (u->exempt)
479                 return false;
480
481         if (match(u->GetIPString(), this->ipaddr, true))
482                 return true;
483         else
484                 return false;
485 }
486
487 void ZLine::Apply(User* u)
488 {       
489         DefaultApply(u, "Z", true);
490 }
491
492
493 bool QLine::Matches(User *u)
494 {
495         if (u->exempt)
496                 return false;
497
498         if (match(u->nick, this->nick))
499                 return true;
500
501         return false;
502 }
503
504 void QLine::Apply(User* u)
505 {       
506         /* Force to uuid on apply of qline, no need to disconnect any more :) */
507         u->ForceNickChange(u->uuid);
508 }
509
510
511 bool ZLine::Matches(const std::string &str)
512 {
513         if (match(str.c_str(), this->ipaddr, true))
514                 return true;
515         else
516                 return false;
517 }
518
519 bool QLine::Matches(const std::string &str)
520 {
521         if (match(str.c_str(), this->nick))
522                 return true;
523
524         return false;
525 }
526
527 bool ELine::Matches(const std::string &str)
528 {
529         return ((match(str.c_str(), matchtext.c_str(), true)));
530 }
531
532 bool KLine::Matches(const std::string &str)
533 {
534         return ((match(str.c_str(), matchtext.c_str(), true)));
535 }
536
537 bool GLine::Matches(const std::string &str)
538 {
539         return ((match(str.c_str(), matchtext.c_str(), true)));
540 }
541
542 void ELine::OnAdd()
543 {
544         /* When adding one eline, only check the one eline */
545         for (std::vector<User*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
546         {
547                 User* u = (User*)(*u2);
548                 if (this->Matches(u))
549                         u->exempt = true;
550         }
551 }
552
553 void ELine::DisplayExpiry()
554 {
555         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed E-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
556 }
557
558 void QLine::DisplayExpiry()
559 {
560         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Q-Line %s (set by %s %d seconds ago)",this->nick,this->source,this->duration);
561 }
562
563 void ZLine::DisplayExpiry()
564 {
565         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Z-Line %s (set by %s %d seconds ago)",this->ipaddr,this->source,this->duration);
566 }
567
568 void KLine::DisplayExpiry()
569 {
570         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed K-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
571 }
572
573 void GLine::DisplayExpiry()
574 {
575         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
576 }
577
578 const char* ELine::Displayable()
579 {
580         return matchtext.c_str();
581 }
582
583 const char* KLine::Displayable()
584 {
585         return matchtext.c_str();
586 }
587
588 const char* GLine::Displayable()
589 {
590         return matchtext.c_str();
591 }
592
593 const char* ZLine::Displayable()
594 {
595         return ipaddr;
596 }
597
598 const char* QLine::Displayable()
599 {
600         return nick;
601 }
602
603 bool KLine::IsBurstable()
604 {
605         return false;
606 }
607
608 bool XLineManager::RegisterFactory(XLineFactory* xlf)
609 {
610         XLineFactMap::iterator n = line_factory.find(xlf->GetType());
611
612         if (n != line_factory.end())
613                 return false;
614
615         line_factory[xlf->GetType()] = xlf;
616
617         return true;
618 }
619
620 bool XLineManager::UnregisterFactory(XLineFactory* xlf)
621 {
622         XLineFactMap::iterator n = line_factory.find(xlf->GetType());
623
624         if (n == line_factory.end())
625                 return false;
626
627         line_factory.erase(n);
628
629         return true;
630 }
631
632 XLineFactory* XLineManager::GetFactory(const std::string &type)
633 {
634         XLineFactMap::iterator n = line_factory.find(type);
635
636         if (n == line_factory.end())
637                 return NULL;
638
639         return n->second;
640 }
641