]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/xline.cpp
Add factories for other types
[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. Expiry will
40  *  still use a sorted list, and we'll just ignore anything permanent.
41  *
42  *  Application will be by a list of lines 'pending' application, meaning only the newly added lines
43  *  will be gone over. Much faster.
44  *
45  * More of course is to come.
46  */
47
48 /* Version two, now with optimized expiry!
49  *
50  * Because the old way was horrendously slow, the new way of expiring xlines is very
51  * very efficient. I have improved the efficiency of the algorithm in two ways:
52  *
53  * (1) There are now two lists of items for each linetype. One list holds temporary
54  *     items, and the other list holds permanent items (ones which will expire).
55  *     Items which are on the permanent list are NEVER checked at all by the
56  *     expire_lines() function.
57  * (2) The temporary xline lists are always kept in strict numerical order, keyed by
58  *     current time + duration. This means that the line which is due to expire the
59  *     soonest is always pointed at by vector::begin(), so a simple while loop can
60  *     very efficiently, very quickly and above all SAFELY pick off the first few
61  *     items in the vector which need zapping.
62  *
63  *     -- Brain
64  */
65
66 bool XLine::Matches(User *u)
67 {
68         return false;
69 }
70
71 /*
72  * Checks what users match a given vector of ELines and sets their ban exempt flag accordingly.
73  */
74 void XLineManager::CheckELines(std::map<std::string, XLine *> &ELines)
75 {
76         if (ELines.empty())
77                 return;
78
79         for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
80         {
81                 User* u = (User*)(*u2);
82
83                 for (std::map<std::string, XLine *>::iterator i = ELines.begin(); i != ELines.end(); i++)
84                 {
85                         XLine *e = i->second;
86                         u->exempt = e->Matches(u);
87                 }
88         }
89 }
90
91 // this should probably be moved to configreader, but atm it relies on CheckELines above.
92 bool DoneELine(ServerConfig* conf, const char* tag)
93 {
94         for (std::vector<User*>::const_iterator u2 = conf->GetInstance()->local_users.begin(); u2 != conf->GetInstance()->local_users.end(); u2++)
95         {
96                 User* u = (User*)(*u2);
97                 u->exempt = false;
98         }
99
100         conf->GetInstance()->XLines->CheckELines(conf->GetInstance()->XLines->lookup_lines['E']);
101         return true;
102 }
103
104
105 IdentHostPair XLineManager::IdentSplit(const std::string &ident_and_host)
106 {
107         IdentHostPair n = std::make_pair<std::string,std::string>("*","*");
108         std::string::size_type x = ident_and_host.find('@');
109         if (x != std::string::npos)
110         {
111                 n.second = ident_and_host.substr(x + 1,ident_and_host.length());
112                 n.first = ident_and_host.substr(0, x);
113                 if (!n.first.length())
114                         n.first.assign("*");
115                 if (!n.second.length())
116                         n.second.assign("*");
117         }
118         else
119         {
120                 n.second = ident_and_host;
121         }
122
123         return n;
124 }
125
126 // adds a g:line
127
128 /*bool XLineManager::AddELine(long duration, const char* source, const char* reason, const char* hostmask)*/
129 bool XLineManager::AddLine(XLine* line)
130 {
131         /*IdentHostPair ih = IdentSplit(hostmask);*/
132
133         if (DelLine(line->Displayable(), line->type, true))
134                 return false;
135
136         /*ELine* item = new ELine(ServerInstance, ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());*/
137
138         active_lines.push_back(line);
139         sort(active_lines.begin(), active_lines.end(), XLineManager::XSortComparison);
140         lookup_lines[line->type][line->Displayable()] = line;
141         line->OnAdd();
142
143         return true;
144 }
145
146 /*bool XLineManager::AddZLine(long duration, const char* source, const char* reason, const char* ipaddr)
147 {
148         if (strchr(ipaddr,'@'))
149         {
150                 while (*ipaddr != '@')
151                         ipaddr++;
152                 ipaddr++;
153         }*/
154
155 // deletes a g:line, returns true if the line existed and was removed
156
157 bool XLineManager::DelLine(const char* hostmask, char type, bool simulate)
158 {
159         IdentHostPair ih = IdentSplit(hostmask);
160         for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
161         {
162                 if ((*i)->type == type)
163                 {
164                         if ((*i)->MatchesLiteral(hostmask))
165                         {
166                                 if (!simulate)
167                                 {
168                                         (*i)->Unset();
169                                         delete *i;
170                                         active_lines.erase(i);
171                                         if (lookup_lines.find(type) != lookup_lines.end())
172                                                 lookup_lines[type].erase(hostmask);
173                                         /* XXX: Should erase from pending lines here */
174                                 }
175                                 return true;
176                         }
177                 }
178         }
179
180         return false;
181 }
182
183
184 void ELine::Unset()
185 {
186         /* remove exempt from everyone and force recheck after deleting eline */
187         for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
188         {
189                 User* u = (User*)(*u2);
190                 u->exempt = false;
191         }
192
193         if (ServerInstance->XLines->lookup_lines.find('E') != ServerInstance->XLines->lookup_lines.end())
194                 ServerInstance->XLines->CheckELines(ServerInstance->XLines->lookup_lines['E']);
195 }
196
197 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
198
199 QLine* XLineManager::matches_qline(const char* nick)
200 {
201         if (lookup_lines.find('Q') == lookup_lines.end())
202                 return NULL;
203
204         if (lookup_lines.find('Q') != lookup_lines.end() && lookup_lines['Q'].empty())
205                 return NULL;
206
207         for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
208                 if ((*i)->type == 'Q' && (*i)->Matches(nick))
209                         return (QLine*)(*i);
210         return NULL;
211 }
212
213 // returns a pointer to the reason if a host matches a gline, NULL if it didnt match
214
215 GLine* XLineManager::matches_gline(User* user)
216 {
217         if (lookup_lines.find('G') == lookup_lines.end())
218                 return NULL;
219
220         if (lookup_lines.find('G') != lookup_lines.end() && lookup_lines['G'].empty())
221                 return NULL;
222
223         for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
224                 if ((*i)->type == 'G' && (*i)->Matches(user))
225                         return (GLine*)(*i);
226
227         return NULL;
228 }
229
230 ELine* XLineManager::matches_exception(User* user)
231 {
232         if (lookup_lines.find('E') == lookup_lines.end())
233                 return NULL;
234
235         if (lookup_lines.find('E') != lookup_lines.end() && lookup_lines['E'].empty())
236                 return NULL;
237
238         for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
239         {
240                 if ((*i)->type == 'E' && (*i)->Matches(user))
241                         return (ELine*)(*i);
242         }
243         return NULL;
244 }
245
246
247 void XLineManager::gline_set_creation_time(const char* host, time_t create_time)
248 {
249         /*for (std::vector<XLine*>::iterator i = glines.begin(); i != glines.end(); i++)
250         {
251                 if (!strcasecmp(host,(*i)->hostmask))
252                 {
253                         (*i)->set_time = create_time;
254                         (*i)->expiry = create_time + (*i)->duration;
255                         return;
256                 }
257         }*/
258
259         return ;
260 }
261
262 void XLineManager::eline_set_creation_time(const char* host, time_t create_time)
263 {
264         /*for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
265         {
266                 if (!strcasecmp(host,(*i)->hostmask))
267                 {
268                         (*i)->set_time = create_time;
269                         (*i)->expiry = create_time + (*i)->duration;
270                         return;
271                 }
272         }*/
273
274         return;
275 }
276
277 void XLineManager::qline_set_creation_time(const char* nick, time_t create_time)
278 {
279         /*for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
280         {
281                 if (!strcasecmp(nick,(*i)->nick))
282                 {
283                         (*i)->set_time = create_time;
284                         (*i)->expiry = create_time + (*i)->duration;
285                         return;
286                 }
287         }*/
288
289         return;
290 }
291
292 void XLineManager::zline_set_creation_time(const char* ip, time_t create_time)
293 {
294         /*for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
295         {
296                 if (!strcasecmp(ip,(*i)->ipaddr))
297                 {
298                         (*i)->set_time = create_time;
299                         (*i)->expiry = create_time + (*i)->duration;
300                         return;
301                 }
302         }*/
303
304         return;
305 }
306
307 // returns a pointer to the reason if an ip address matches a zline, NULL if it didnt match
308
309 ZLine* XLineManager::matches_zline(User *u)
310 {
311         if (lookup_lines.find('Z') == lookup_lines.end())
312                 return NULL;
313
314         if (lookup_lines.find('Z') != lookup_lines.end() && lookup_lines['Z'].empty())
315                 return NULL;
316
317         for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
318                 if ((*i)->type == 'Z' && (*i)->Matches(u))
319                         return (ZLine*)(*i);
320         return NULL;
321 }
322
323 // returns a pointer to the reason if a host matches a kline, NULL if it didnt match
324
325 KLine* XLineManager::matches_kline(User* user)
326 {
327         if (lookup_lines.find('K') == lookup_lines.end())
328                 return NULL;
329
330         if (lookup_lines.find('K') != lookup_lines.end() && lookup_lines['K'].empty())
331                 return NULL;
332
333         for (std::vector<XLine*>::iterator i = active_lines.begin(); i != active_lines.end(); i++)
334                 if ((*i)->Matches(user))
335                         return (KLine*)(*i);
336
337         return NULL;
338 }
339
340 bool XLineManager::XSortComparison(const XLine *one, const XLine *two)
341 {
342         // account for permanent lines
343         if (one->expiry == 0)
344         {
345                 return false;
346         }
347         return (one->expiry) < (two->expiry);
348 }
349
350 // removes lines that have expired
351 void XLineManager::expire_lines()
352 {
353         time_t current = ServerInstance->Time();
354
355         /* Because we now store all our XLines in sorted order using ((*i)->duration + (*i)->set_time) as a key, this
356          * means that to expire the XLines we just need to do a while, picking off the top few until there are
357          * none left at the head of the queue that are after the current time.
358          */
359
360         while ((active_lines.size()) && (current > (*active_lines.begin())->expiry) && ((*active_lines.begin())->duration != 0))
361         {
362                 std::vector<XLine*>::iterator i = active_lines.begin();
363                 (*i)->DisplayExpiry();
364                 (*i)->Unset();
365                 active_lines.erase(i);
366                 delete *i;
367         }
368 }
369
370 // applies lines, removing clients and changing nicks etc as applicable
371 void XLineManager::ApplyLines()
372 {
373         for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
374         {
375                 User* u = (User*)(*u2);
376
377                 for (std::vector<XLine *>::iterator i = pending_lines.begin(); i != pending_lines.end(); i++)
378                 {
379                         XLine *x = *i;
380                         if (x->Matches(u))
381                                 x->Apply(u);
382                 }
383         }
384
385         pending_lines.clear();
386 }
387
388 void XLineManager::stats_k(User* user, string_list &results)
389 {
390         /*std::string sn = ServerInstance->Config->ServerName;
391         for (std::vector<KLine*>::iterator i = klines.begin(); i != klines.end(); i++)
392                 results.push_back(sn+" 216 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);*/
393 }
394
395 void XLineManager::stats_g(User* user, string_list &results)
396 {
397         /*std::string sn = ServerInstance->Config->ServerName;
398         for (std::vector<GLine*>::iterator i = glines.begin(); i != glines.end(); i++)
399                 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);*/
400 }
401
402 void XLineManager::stats_q(User* user, string_list &results)
403 {
404         /*std::string sn = ServerInstance->Config->ServerName;
405         for (std::vector<QLine*>::iterator i = qlines.begin(); i != qlines.end(); i++)
406                 results.push_back(sn+" 217 "+user->nick+" :"+(*i)->nick+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);*/
407 }
408
409 void XLineManager::stats_z(User* user, string_list &results)
410 {
411         /*std::string sn = ServerInstance->Config->ServerName;
412         for (std::vector<ZLine*>::iterator i = zlines.begin(); i != zlines.end(); i++)
413                 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->ipaddr+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);*/
414 }
415
416 void XLineManager::stats_e(User* user, string_list &results)
417 {
418         /*std::string sn = ServerInstance->Config->ServerName;
419         for (std::vector<ELine*>::iterator i = elines.begin(); i != elines.end(); i++)
420                 results.push_back(sn+" 223 "+user->nick+" :"+(*i)->identmask+"@"+(*i)->hostmask+" "+ConvToStr((*i)->set_time)+" "+ConvToStr((*i)->duration)+" "+(*i)->source+" :"+(*i)->reason);*/
421 }
422
423 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance)
424 {
425         GFact = new GLineFactory(Instance);
426         EFact = new ELineFactory(Instance);
427         KFact = new KLineFactory(Instance);
428         QFact = new QLineFactory(Instance);
429         ZFact = new ZLineFactory(Instance);
430
431         RegisterFactory(GFact);
432         RegisterFactory(EFact);
433         RegisterFactory(KFact);
434         RegisterFactory(QFact);
435         RegisterFactory(ZFact);
436 }
437
438 XLineManager::~XLineManager()
439 {
440         UnregisterFactory(GFact);
441         UnregisterFactory(EFact);
442         UnregisterFactory(KFact);
443         UnregisterFactory(QFact);
444         UnregisterFactory(ZFact);
445
446         delete GFact;
447         delete EFact;
448         delete KFact;
449         delete QFact;
450         delete ZFact;
451 }
452
453 void XLine::Apply(User* u)
454 {
455 }
456
457 void XLine::DefaultApply(User* u, char line)
458 {
459         char reason[MAXBUF];
460         snprintf(reason, MAXBUF, "%c-Lined: %s", line, this->reason);
461         if (*ServerInstance->Config->MoronBanner)
462                 u->WriteServ("NOTICE %s :*** %s", u->nick, ServerInstance->Config->MoronBanner);
463         if (ServerInstance->Config->HideBans)
464                 User::QuitUser(ServerInstance, u, line + std::string("-Lined"), reason);
465         else
466                 User::QuitUser(ServerInstance, u, reason);
467 }
468
469 bool KLine::Matches(User *u)
470 {
471         if (u->exempt)
472                 return false;
473
474         if ((match(u->ident, this->identmask)))
475         {
476                 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
477                 {
478                         return true;
479                 }
480         }
481
482         return false;
483 }
484
485 void KLine::Apply(User* u)
486 {
487         DefaultApply(u, 'K');
488 }
489
490 bool GLine::Matches(User *u)
491 {
492         if (u->exempt)
493                 return false;
494
495         if ((match(u->ident, this->identmask)))
496         {
497                 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
498                 {
499                         return true;
500                 }
501         }
502
503         return false;
504 }
505
506 void GLine::Apply(User* u)
507 {       
508         DefaultApply(u, 'G');
509 }
510
511 bool ELine::Matches(User *u)
512 {
513         if (u->exempt)
514                 return false;
515
516         if ((match(u->ident, this->identmask)))
517         {
518                 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
519                 {
520                         return true;
521                 }
522         }
523
524         return false;
525 }
526
527 bool ZLine::Matches(User *u)
528 {
529         if (u->exempt)
530                 return false;
531
532         if (match(u->GetIPString(), this->ipaddr, true))
533                 return true;
534         else
535                 return false;
536 }
537
538 void ZLine::Apply(User* u)
539 {       
540         DefaultApply(u, 'Z');
541 }
542
543
544 bool QLine::Matches(User *u)
545 {
546         if (u->exempt)
547                 return false;
548
549         if (match(u->nick, this->nick))
550                 return true;
551
552         return false;
553 }
554
555 void QLine::Apply(User* u)
556 {       
557         /* Can we force the user to their uid here instead? */
558         DefaultApply(u, 'Q');
559 }
560
561
562 bool ZLine::Matches(const std::string &str)
563 {
564         if (match(str.c_str(), this->ipaddr, true))
565                 return true;
566         else
567                 return false;
568 }
569
570 bool QLine::Matches(const std::string &str)
571 {
572         if (match(str.c_str(), this->nick))
573                 return true;
574
575         return false;
576 }
577
578 bool ELine::Matches(const std::string &str)
579 {
580         return ((match(str.c_str(), matchtext.c_str(), true)));
581 }
582
583 bool KLine::Matches(const std::string &str)
584 {
585         return ((match(str.c_str(), matchtext.c_str(), true)));
586 }
587
588 bool GLine::Matches(const std::string &str)
589 {
590         return ((match(str.c_str(), matchtext.c_str(), true)));
591 }
592
593 bool ELine::MatchesLiteral(const std::string &str)
594 {
595         return (assign(str) == matchtext);
596 }
597
598 bool ZLine::MatchesLiteral(const std::string &str)
599 {       
600         return (assign(str) == this->ipaddr);
601 }
602
603 bool GLine::MatchesLiteral(const std::string &str)
604 {       
605         return (assign(str) == matchtext);
606 }
607
608 bool KLine::MatchesLiteral(const std::string &str)
609 {       
610         return (assign(str) == matchtext);
611 }
612
613 bool QLine::MatchesLiteral(const std::string &str)
614 {       
615         return (assign(str) == this->nick);
616 }
617
618 void ELine::OnAdd()
619 {
620         /* When adding one eline, only check the one eline */
621         for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
622         {
623                 User* u = (User*)(*u2);
624                 if (this->Matches(u))
625                         u->exempt = true;
626         }
627 }
628
629 void ELine::DisplayExpiry()
630 {
631         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed E-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
632 }
633
634 void QLine::DisplayExpiry()
635 {
636         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Q-Line %s (set by %s %d seconds ago)",this->nick,this->source,this->duration);
637 }
638
639 void ZLine::DisplayExpiry()
640 {
641         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Z-Line %s (set by %s %d seconds ago)",this->ipaddr,this->source,this->duration);
642 }
643
644 void KLine::DisplayExpiry()
645 {
646         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed K-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
647 }
648
649 void GLine::DisplayExpiry()
650 {
651         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
652 }
653
654 const char* ELine::Displayable()
655 {
656         return matchtext.c_str();
657 }
658
659 const char* KLine::Displayable()
660 {
661         return matchtext.c_str();
662 }
663
664 const char* GLine::Displayable()
665 {
666         return matchtext.c_str();
667 }
668
669 const char* ZLine::Displayable()
670 {
671         return ipaddr;
672 }
673
674 const char* QLine::Displayable()
675 {
676         return nick;
677 }
678
679 bool XLineManager::RegisterFactory(XLineFactory* xlf)
680 {
681         std::map<char, XLineFactory*>::iterator n = line_factory.find(xlf->GetType());
682
683         if (n != line_factory.end())
684                 return false;
685
686         line_factory[xlf->GetType()] = xlf;
687
688         return true;
689 }
690
691 bool XLineManager::UnregisterFactory(XLineFactory* xlf)
692 {
693         std::map<char, XLineFactory*>::iterator n = line_factory.find(xlf->GetType());
694
695         if (n == line_factory.end())
696                 return false;
697
698         line_factory.erase(n);
699
700         return true;
701 }
702
703 XLineFactory* XLineManager::GetFactory(const char type)
704 {
705         std::map<char, XLineFactory*>::iterator n = line_factory.find(type);
706
707         if (n != line_factory.end())
708                 return NULL;
709
710         return n->second;
711 }
712