]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/xline.cpp
This won't work yet.
[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()
61 {
62         ContainerIter n = lookup_lines.find("E");
63
64         if (n == lookup_lines.end())
65                 return;
66
67         XLineLookup& ELines = n->second;
68
69         if (ELines.empty())
70                 return;
71
72         for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
73         {
74                 User* u = (User*)(*u2);
75
76                 /* This uses safe iteration to ensure that if a line expires here, it doenst trash the iterator */
77                 LookupIter safei;
78
79                 for (LookupIter i = ELines.begin(); i != ELines.end(); )
80                 {
81                         safei = i;
82                         safei++;
83
84                         XLine *e = i->second;
85                         u->exempt = e->Matches(u);
86
87                         i = safei;
88                 }
89         }
90 }
91
92
93 XLineLookup* XLineManager::GetAll(const std::string &type)
94 {
95         ContainerIter n = lookup_lines.find(type);
96
97         if (n == lookup_lines.end())
98                 return NULL;
99
100         LookupIter safei;
101         const time_t current = ServerInstance->Time();
102
103         /* Expire any dead ones, before sending */
104         for (LookupIter x = n->second.begin(); x != n->second.end(); )
105         {
106                 safei = x;
107                 safei++;
108                 if (x->second->duration && current > x->second->expiry)
109                 {
110                         ExpireLine(n, x);
111                 }
112                 x = safei;
113         }
114
115         return &(n->second);
116 }
117
118 std::vector<std::string> XLineManager::GetAllTypes()
119 {
120         std::vector<std::string> items;
121         for (ContainerIter x = lookup_lines.begin(); x != lookup_lines.end(); ++x)
122                 items.push_back(x->first);
123         return items;
124 }
125
126 IdentHostPair XLineManager::IdentSplit(const std::string &ident_and_host)
127 {
128         IdentHostPair n = std::make_pair<std::string,std::string>("*","*");
129         std::string::size_type x = ident_and_host.find('@');
130         if (x != std::string::npos)
131         {
132                 n.second = ident_and_host.substr(x + 1,ident_and_host.length());
133                 n.first = ident_and_host.substr(0, x);
134                 if (!n.first.length())
135                         n.first.assign("*");
136                 if (!n.second.length())
137                         n.second.assign("*");
138         }
139         else
140         {
141                 n.second = ident_and_host;
142         }
143
144         return n;
145 }
146
147 // adds a line
148
149 bool XLineManager::AddLine(XLine* line, User* user)
150 {
151         /*IdentHostPair ih = IdentSplit(hostmask);*/
152
153         if (DelLine(line->Displayable(), line->type, user, true))
154                 return false;
155
156         /*ELine* item = new ELine(ServerInstance, ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());*/
157         pending_lines.push_back(line);
158         lookup_lines[line->type][line->Displayable()] = line;
159         line->OnAdd();
160
161         FOREACH_MOD(I_OnAddLine,OnAddLine(user, line)); 
162
163         return true;
164 }
165
166 // deletes a line, returns true if the line existed and was removed
167
168 bool XLineManager::DelLine(const char* hostmask, const std::string &type, User* user, bool simulate)
169 {
170         ContainerIter x = lookup_lines.find(type);
171
172         if (x == lookup_lines.end())
173                 return false;
174
175         LookupIter y = x->second.find(hostmask);
176
177         if (y == x->second.end())
178                 return false;
179
180         if (simulate)
181                 return true;
182
183         FOREACH_MOD(I_OnDelLine,OnDelLine(user, y->second));
184
185         y->second->Unset();
186
187         std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), y->second);
188         if (pptr != pending_lines.end())
189                 pending_lines.erase(pptr);
190
191         delete y->second;
192         x->second.erase(y);
193
194         return true;
195 }
196
197
198 void ELine::Unset()
199 {
200         /* remove exempt from everyone and force recheck after deleting eline */
201         for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
202         {
203                 User* u = (User*)(*u2);
204                 u->exempt = false;
205         }
206
207         ServerInstance->XLines->CheckELines();
208 }
209
210 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
211
212 XLine* XLineManager::MatchesLine(const std::string &type, User* user)
213 {
214         ContainerIter x = lookup_lines.find(type);
215
216         if (x == lookup_lines.end())
217                 return NULL;
218
219         const time_t current = ServerInstance->Time();
220
221         LookupIter safei;
222
223         for (LookupIter i = x->second.begin(); i != x->second.end(); )
224         {
225                 safei = i;
226                 safei++;
227
228                 if (i->second->Matches(user))
229                 {
230                         if (i->second->duration && current > i->second->expiry)
231                         {
232                                 /* Expire the line, return nothing */
233                                 ExpireLine(x, i);
234                                 /* Continue, there may be another that matches
235                                  * (thanks aquanight)
236                                  */
237                                 i = safei;
238                                 continue;
239                         }
240                         else
241                                 return i->second;
242                 }
243
244                 i = safei;
245         }
246         return NULL;
247 }
248
249 XLine* XLineManager::MatchesLine(const std::string &type, const std::string &pattern)
250 {
251         ContainerIter x = lookup_lines.find(type);
252
253         if (x == lookup_lines.end())
254                 return NULL;
255
256         const time_t current = ServerInstance->Time();
257
258          LookupIter safei;
259
260         for (LookupIter i = x->second.begin(); i != x->second.end(); )
261         {
262                 safei = i;
263                 safei++;
264
265                 if (i->second->Matches(pattern))
266                 {
267                         if (i->second->duration && current > i->second->expiry)
268                         {
269                                 /* Expire the line, return nothing */
270                                 ExpireLine(x, i);
271                                 /* See above */
272                                 i = safei;
273                                 continue;
274                         }
275                         else
276                                 return i->second;
277                 }
278
279                 i = safei;
280         }
281         return NULL;
282 }
283
284 // removes lines that have expired
285 void XLineManager::ExpireLine(ContainerIter container, LookupIter item)
286 {
287                 item->second->DisplayExpiry();
288                 item->second->Unset();
289
290                 /* TODO: Can we skip this loop by having a 'pending' field in the XLine class, which is set when a line
291                  * is pending, cleared when it is no longer pending, so we skip over this loop if its not pending?
292                  * -- Brain
293                  */
294                 std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), item->second);
295                 if (pptr != pending_lines.end())
296                         pending_lines.erase(pptr);
297
298                 delete item->second;
299                 container->second.erase(item);
300 }
301
302
303 // applies lines, removing clients and changing nicks etc as applicable
304 void XLineManager::ApplyLines()
305 {
306         for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
307         {
308                 User* u = (User*)(*u2);
309
310                 for (std::vector<XLine *>::iterator i = pending_lines.begin(); i != pending_lines.end(); i++)
311                 {
312                         XLine *x = *i;
313                         if (x->Matches(u))
314                                 x->Apply(u);
315                 }
316         }
317
318         pending_lines.clear();
319 }
320
321 void XLineManager::InvokeStats(const std::string &type, int numeric, User* user, string_list &results)
322 {
323         std::string sn = ServerInstance->Config->ServerName;
324
325         ContainerIter n = lookup_lines.find(type);
326
327         time_t current = ServerInstance->Time();
328
329         LookupIter safei;
330
331         if (n != lookup_lines.end())
332         {
333                 XLineLookup& list = n->second;
334                 for (LookupIter i = list.begin(); i != list.end(); )
335                 {
336                         safei = i;
337                         safei++;
338
339                         if (i->second->duration && current > i->second->expiry)
340                         {
341                                 ExpireLine(n, i);
342                         }
343                         else
344                                 results.push_back(sn+" "+ConvToStr(numeric)+" "+user->nick+" :"+i->second->Displayable()+" "+
345                                         ConvToStr(i->second->set_time)+" "+ConvToStr(i->second->duration)+" "+std::string(i->second->source)+" :"+(i->second->reason));
346                         i = safei;
347                 }
348         }
349 }
350
351
352 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance)
353 {
354         GFact = new GLineFactory(Instance);
355         EFact = new ELineFactory(Instance);
356         KFact = new KLineFactory(Instance);
357         QFact = new QLineFactory(Instance);
358         ZFact = new ZLineFactory(Instance);
359
360         RegisterFactory(GFact);
361         RegisterFactory(EFact);
362         RegisterFactory(KFact);
363         RegisterFactory(QFact);
364         RegisterFactory(ZFact);
365 }
366
367 XLineManager::~XLineManager()
368 {
369         UnregisterFactory(GFact);
370         UnregisterFactory(EFact);
371         UnregisterFactory(KFact);
372         UnregisterFactory(QFact);
373         UnregisterFactory(ZFact);
374
375         delete GFact;
376         delete EFact;
377         delete KFact;
378         delete QFact;
379         delete ZFact;
380 }
381
382 void XLine::Apply(User* u)
383 {
384 }
385
386 void XLine::DefaultApply(User* u, const std::string &line)
387 {
388         char reason[MAXBUF];
389         snprintf(reason, MAXBUF, "%s-Lined: %s", line.c_str(), this->reason);
390         if (*ServerInstance->Config->MoronBanner)
391                 u->WriteServ("NOTICE %s :*** %s", u->nick, ServerInstance->Config->MoronBanner);
392         if (ServerInstance->Config->HideBans)
393                 User::QuitUser(ServerInstance, u, line + "-Lined", reason);
394         else
395                 User::QuitUser(ServerInstance, u, reason);
396 }
397
398 bool KLine::Matches(User *u)
399 {
400         if (u->exempt)
401                 return false;
402
403         if ((match(u->ident, this->identmask)))
404         {
405                 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
406                 {
407                         return true;
408                 }
409         }
410
411         return false;
412 }
413
414 void KLine::Apply(User* u)
415 {
416         DefaultApply(u, "K");
417 }
418
419 bool GLine::Matches(User *u)
420 {
421         if (u->exempt)
422                 return false;
423
424         if ((match(u->ident, this->identmask)))
425         {
426                 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
427                 {
428                         return true;
429                 }
430         }
431
432         return false;
433 }
434
435 void GLine::Apply(User* u)
436 {       
437         DefaultApply(u, "G");
438 }
439
440 bool ELine::Matches(User *u)
441 {
442         if (u->exempt)
443                 return false;
444
445         if ((match(u->ident, this->identmask)))
446         {
447                 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
448                 {
449                         return true;
450                 }
451         }
452
453         return false;
454 }
455
456 bool ZLine::Matches(User *u)
457 {
458         if (u->exempt)
459                 return false;
460
461         if (match(u->GetIPString(), this->ipaddr, true))
462                 return true;
463         else
464                 return false;
465 }
466
467 void ZLine::Apply(User* u)
468 {       
469         DefaultApply(u, "Z");
470 }
471
472
473 bool QLine::Matches(User *u)
474 {
475         if (u->exempt)
476                 return false;
477
478         if (match(u->nick, this->nick))
479                 return true;
480
481         return false;
482 }
483
484 void QLine::Apply(User* u)
485 {       
486         /* Force to uuid on apply of qline, no need to disconnect any more :) */
487         u->ForceNickChange(u->uuid);
488 }
489
490
491 bool ZLine::Matches(const std::string &str)
492 {
493         if (match(str.c_str(), this->ipaddr, true))
494                 return true;
495         else
496                 return false;
497 }
498
499 bool QLine::Matches(const std::string &str)
500 {
501         if (match(str.c_str(), this->nick))
502                 return true;
503
504         return false;
505 }
506
507 bool ELine::Matches(const std::string &str)
508 {
509         return ((match(str.c_str(), matchtext.c_str(), true)));
510 }
511
512 bool KLine::Matches(const std::string &str)
513 {
514         return ((match(str.c_str(), matchtext.c_str(), true)));
515 }
516
517 bool GLine::Matches(const std::string &str)
518 {
519         return ((match(str.c_str(), matchtext.c_str(), true)));
520 }
521
522 void ELine::OnAdd()
523 {
524         /* When adding one eline, only check the one eline */
525         for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
526         {
527                 User* u = (User*)(*u2);
528                 if (this->Matches(u))
529                         u->exempt = true;
530         }
531 }
532
533 void ELine::DisplayExpiry()
534 {
535         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed E-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
536 }
537
538 void QLine::DisplayExpiry()
539 {
540         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Q-Line %s (set by %s %d seconds ago)",this->nick,this->source,this->duration);
541 }
542
543 void ZLine::DisplayExpiry()
544 {
545         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Z-Line %s (set by %s %d seconds ago)",this->ipaddr,this->source,this->duration);
546 }
547
548 void KLine::DisplayExpiry()
549 {
550         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed K-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
551 }
552
553 void GLine::DisplayExpiry()
554 {
555         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
556 }
557
558 const char* ELine::Displayable()
559 {
560         return matchtext.c_str();
561 }
562
563 const char* KLine::Displayable()
564 {
565         return matchtext.c_str();
566 }
567
568 const char* GLine::Displayable()
569 {
570         return matchtext.c_str();
571 }
572
573 const char* ZLine::Displayable()
574 {
575         return ipaddr;
576 }
577
578 const char* QLine::Displayable()
579 {
580         return nick;
581 }
582
583 bool XLineManager::RegisterFactory(XLineFactory* xlf)
584 {
585         XLineFactMap::iterator n = line_factory.find(xlf->GetType());
586
587         if (n != line_factory.end())
588                 return false;
589
590         line_factory[xlf->GetType()] = xlf;
591
592         return true;
593 }
594
595 bool XLineManager::UnregisterFactory(XLineFactory* xlf)
596 {
597         XLineFactMap::iterator n = line_factory.find(xlf->GetType());
598
599         if (n == line_factory.end())
600                 return false;
601
602         line_factory.erase(n);
603
604         return true;
605 }
606
607 XLineFactory* XLineManager::GetFactory(const std::string &type)
608 {
609         XLineFactMap::iterator n = line_factory.find(type);
610
611         if (n != line_factory.end())
612                 return NULL;
613
614         return n->second;
615 }
616