]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/xline.cpp
Dont propogate K-lines
[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         for (LookupIter i = x->second.begin(); i != x->second.end(); i++)
222         {
223                 if (i->second->Matches(user))
224                 {
225                         if (i->second->duration && current > i->second->expiry)
226                         {
227                                 /* Expire the line, return nothing */
228                                 ExpireLine(x, i);
229                                 return NULL;
230                         }
231                         else
232                                 return i->second;
233                 }
234         }
235         return NULL;
236 }
237
238 XLine* XLineManager::MatchesLine(const std::string &type, const std::string &pattern)
239 {
240         ContainerIter x = lookup_lines.find(type);
241
242         if (x == lookup_lines.end())
243                 return NULL;
244
245         const time_t current = ServerInstance->Time();
246
247         for (LookupIter i = x->second.begin(); i != x->second.end(); i++)
248         {
249                 if (i->second->Matches(pattern))
250                 {
251                         if (i->second->duration && current > i->second->expiry)
252                         {
253                                 /* Expire the line, return nothing */
254                                 ExpireLine(x, i);
255                                 return NULL;
256                         }
257                         else
258                                 return i->second;
259                 }
260         }
261         return NULL;
262 }
263
264 // removes lines that have expired
265 void XLineManager::ExpireLine(ContainerIter container, LookupIter item)
266 {
267                 item->second->DisplayExpiry();
268                 item->second->Unset();
269
270                 /* TODO: Can we skip this loop by having a 'pending' field in the XLine class, which is set when a line
271                  * is pending, cleared when it is no longer pending, so we skip over this loop if its not pending?
272                  * -- Brain
273                  */
274                 std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), item->second);
275                 if (pptr != pending_lines.end())
276                         pending_lines.erase(pptr);
277
278                 delete item->second;
279                 container->second.erase(item);
280 }
281
282
283 // applies lines, removing clients and changing nicks etc as applicable
284 void XLineManager::ApplyLines()
285 {
286         for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
287         {
288                 User* u = (User*)(*u2);
289
290                 for (std::vector<XLine *>::iterator i = pending_lines.begin(); i != pending_lines.end(); i++)
291                 {
292                         XLine *x = *i;
293                         if (x->Matches(u))
294                                 x->Apply(u);
295                 }
296         }
297
298         pending_lines.clear();
299 }
300
301 void XLineManager::InvokeStats(const std::string &type, int numeric, User* user, string_list &results)
302 {
303         std::string sn = ServerInstance->Config->ServerName;
304
305         ContainerIter n = lookup_lines.find(type);
306
307         time_t current = ServerInstance->Time();
308
309         LookupIter safei;
310
311         if (n != lookup_lines.end())
312         {
313                 XLineLookup& list = n->second;
314                 for (LookupIter i = list.begin(); i != list.end(); )
315                 {
316                         safei = i;
317                         safei++;
318
319                         if (i->second->duration && current > i->second->expiry)
320                         {
321                                 ExpireLine(n, i);
322                         }
323                         else
324                                 results.push_back(sn+" "+ConvToStr(numeric)+" "+user->nick+" :"+i->second->Displayable()+" "+
325                                         ConvToStr(i->second->set_time)+" "+ConvToStr(i->second->duration)+" "+std::string(i->second->source)+" :"+(i->second->reason));
326                         i = safei;
327                 }
328         }
329 }
330
331
332 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance)
333 {
334         GFact = new GLineFactory(Instance);
335         EFact = new ELineFactory(Instance);
336         KFact = new KLineFactory(Instance);
337         QFact = new QLineFactory(Instance);
338         ZFact = new ZLineFactory(Instance);
339
340         RegisterFactory(GFact);
341         RegisterFactory(EFact);
342         RegisterFactory(KFact);
343         RegisterFactory(QFact);
344         RegisterFactory(ZFact);
345 }
346
347 XLineManager::~XLineManager()
348 {
349         UnregisterFactory(GFact);
350         UnregisterFactory(EFact);
351         UnregisterFactory(KFact);
352         UnregisterFactory(QFact);
353         UnregisterFactory(ZFact);
354
355         delete GFact;
356         delete EFact;
357         delete KFact;
358         delete QFact;
359         delete ZFact;
360 }
361
362 void XLine::Apply(User* u)
363 {
364 }
365
366 void XLine::DefaultApply(User* u, const std::string &line)
367 {
368         char reason[MAXBUF];
369         snprintf(reason, MAXBUF, "%s-Lined: %s", line.c_str(), this->reason);
370         if (*ServerInstance->Config->MoronBanner)
371                 u->WriteServ("NOTICE %s :*** %s", u->nick, ServerInstance->Config->MoronBanner);
372         if (ServerInstance->Config->HideBans)
373                 User::QuitUser(ServerInstance, u, line + "-Lined", reason);
374         else
375                 User::QuitUser(ServerInstance, u, reason);
376 }
377
378 bool KLine::Matches(User *u)
379 {
380         if (u->exempt)
381                 return false;
382
383         if ((match(u->ident, this->identmask)))
384         {
385                 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
386                 {
387                         return true;
388                 }
389         }
390
391         return false;
392 }
393
394 void KLine::Apply(User* u)
395 {
396         DefaultApply(u, "K");
397 }
398
399 bool GLine::Matches(User *u)
400 {
401         if (u->exempt)
402                 return false;
403
404         if ((match(u->ident, this->identmask)))
405         {
406                 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
407                 {
408                         return true;
409                 }
410         }
411
412         return false;
413 }
414
415 void GLine::Apply(User* u)
416 {       
417         DefaultApply(u, "G");
418 }
419
420 bool ELine::Matches(User *u)
421 {
422         if (u->exempt)
423                 return false;
424
425         if ((match(u->ident, this->identmask)))
426         {
427                 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
428                 {
429                         return true;
430                 }
431         }
432
433         return false;
434 }
435
436 bool ZLine::Matches(User *u)
437 {
438         if (u->exempt)
439                 return false;
440
441         if (match(u->GetIPString(), this->ipaddr, true))
442                 return true;
443         else
444                 return false;
445 }
446
447 void ZLine::Apply(User* u)
448 {       
449         DefaultApply(u, "Z");
450 }
451
452
453 bool QLine::Matches(User *u)
454 {
455         if (u->exempt)
456                 return false;
457
458         if (match(u->nick, this->nick))
459                 return true;
460
461         return false;
462 }
463
464 void QLine::Apply(User* u)
465 {       
466         /* Can we force the user to their uid here instead? */
467         DefaultApply(u, "Q");
468 }
469
470
471 bool ZLine::Matches(const std::string &str)
472 {
473         if (match(str.c_str(), this->ipaddr, true))
474                 return true;
475         else
476                 return false;
477 }
478
479 bool QLine::Matches(const std::string &str)
480 {
481         if (match(str.c_str(), this->nick))
482                 return true;
483
484         return false;
485 }
486
487 bool ELine::Matches(const std::string &str)
488 {
489         return ((match(str.c_str(), matchtext.c_str(), true)));
490 }
491
492 bool KLine::Matches(const std::string &str)
493 {
494         return ((match(str.c_str(), matchtext.c_str(), true)));
495 }
496
497 bool GLine::Matches(const std::string &str)
498 {
499         return ((match(str.c_str(), matchtext.c_str(), true)));
500 }
501
502 void ELine::OnAdd()
503 {
504         /* When adding one eline, only check the one eline */
505         for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
506         {
507                 User* u = (User*)(*u2);
508                 if (this->Matches(u))
509                         u->exempt = true;
510         }
511 }
512
513 void ELine::DisplayExpiry()
514 {
515         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed E-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
516 }
517
518 void QLine::DisplayExpiry()
519 {
520         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Q-Line %s (set by %s %d seconds ago)",this->nick,this->source,this->duration);
521 }
522
523 void ZLine::DisplayExpiry()
524 {
525         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Z-Line %s (set by %s %d seconds ago)",this->ipaddr,this->source,this->duration);
526 }
527
528 void KLine::DisplayExpiry()
529 {
530         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed K-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
531 }
532
533 void GLine::DisplayExpiry()
534 {
535         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
536 }
537
538 const char* ELine::Displayable()
539 {
540         return matchtext.c_str();
541 }
542
543 const char* KLine::Displayable()
544 {
545         return matchtext.c_str();
546 }
547
548 const char* GLine::Displayable()
549 {
550         return matchtext.c_str();
551 }
552
553 const char* ZLine::Displayable()
554 {
555         return ipaddr;
556 }
557
558 const char* QLine::Displayable()
559 {
560         return nick;
561 }
562
563 bool XLineManager::RegisterFactory(XLineFactory* xlf)
564 {
565         XLineFactMap::iterator n = line_factory.find(xlf->GetType());
566
567         if (n != line_factory.end())
568                 return false;
569
570         line_factory[xlf->GetType()] = xlf;
571
572         return true;
573 }
574
575 bool XLineManager::UnregisterFactory(XLineFactory* xlf)
576 {
577         XLineFactMap::iterator n = line_factory.find(xlf->GetType());
578
579         if (n == line_factory.end())
580                 return false;
581
582         line_factory.erase(n);
583
584         return true;
585 }
586
587 XLineFactory* XLineManager::GetFactory(const std::string &type)
588 {
589         XLineFactMap::iterator n = line_factory.find(type);
590
591         if (n != line_factory.end())
592                 return NULL;
593
594         return n->second;
595 }
596