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