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