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