]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/xline.cpp
Put back different stats numerics for /stats g, /stats k etc
[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 /* k: 216
279  * g: 223
280  * q: 217
281  * z: 223
282  * e: 223
283  */
284
285 void XLineManager::InvokeStats(const std::string &type, int numeric, User* user, string_list &results)
286 {
287         std::string sn = ServerInstance->Config->ServerName;
288
289         ContainerIter n = lookup_lines.find(type);
290
291         if (n != lookup_lines.end())
292         {
293                 XLineLookup& list = n->second;
294                 for (LookupIter i = list.begin(); i != list.end(); i++)
295                         results.push_back(sn+" "+ConvToStr(numeric)+" "+user->nick+" :"+i->second->Displayable()+" "+
296                                         ConvToStr(i->second->set_time)+" "+ConvToStr(i->second->duration)+" "+std::string(i->second->source)+" :"+(i->second->reason));
297         }
298 }
299
300
301 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance)
302 {
303         GFact = new GLineFactory(Instance);
304         EFact = new ELineFactory(Instance);
305         KFact = new KLineFactory(Instance);
306         QFact = new QLineFactory(Instance);
307         ZFact = new ZLineFactory(Instance);
308
309         RegisterFactory(GFact);
310         RegisterFactory(EFact);
311         RegisterFactory(KFact);
312         RegisterFactory(QFact);
313         RegisterFactory(ZFact);
314 }
315
316 XLineManager::~XLineManager()
317 {
318         UnregisterFactory(GFact);
319         UnregisterFactory(EFact);
320         UnregisterFactory(KFact);
321         UnregisterFactory(QFact);
322         UnregisterFactory(ZFact);
323
324         delete GFact;
325         delete EFact;
326         delete KFact;
327         delete QFact;
328         delete ZFact;
329 }
330
331 void XLine::Apply(User* u)
332 {
333 }
334
335 void XLine::DefaultApply(User* u, const std::string &line)
336 {
337         char reason[MAXBUF];
338         snprintf(reason, MAXBUF, "%s-Lined: %s", line.c_str(), this->reason);
339         if (*ServerInstance->Config->MoronBanner)
340                 u->WriteServ("NOTICE %s :*** %s", u->nick, ServerInstance->Config->MoronBanner);
341         if (ServerInstance->Config->HideBans)
342                 User::QuitUser(ServerInstance, u, line + "-Lined", reason);
343         else
344                 User::QuitUser(ServerInstance, u, reason);
345 }
346
347 bool KLine::Matches(User *u)
348 {
349         if (u->exempt)
350                 return false;
351
352         if ((match(u->ident, this->identmask)))
353         {
354                 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
355                 {
356                         return true;
357                 }
358         }
359
360         return false;
361 }
362
363 void KLine::Apply(User* u)
364 {
365         DefaultApply(u, "K");
366 }
367
368 bool GLine::Matches(User *u)
369 {
370         if (u->exempt)
371                 return false;
372
373         if ((match(u->ident, this->identmask)))
374         {
375                 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
376                 {
377                         return true;
378                 }
379         }
380
381         return false;
382 }
383
384 void GLine::Apply(User* u)
385 {       
386         DefaultApply(u, "G");
387 }
388
389 bool ELine::Matches(User *u)
390 {
391         if (u->exempt)
392                 return false;
393
394         if ((match(u->ident, this->identmask)))
395         {
396                 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
397                 {
398                         return true;
399                 }
400         }
401
402         return false;
403 }
404
405 bool ZLine::Matches(User *u)
406 {
407         if (u->exempt)
408                 return false;
409
410         if (match(u->GetIPString(), this->ipaddr, true))
411                 return true;
412         else
413                 return false;
414 }
415
416 void ZLine::Apply(User* u)
417 {       
418         DefaultApply(u, "Z");
419 }
420
421
422 bool QLine::Matches(User *u)
423 {
424         if (u->exempt)
425                 return false;
426
427         if (match(u->nick, this->nick))
428                 return true;
429
430         return false;
431 }
432
433 void QLine::Apply(User* u)
434 {       
435         /* Can we force the user to their uid here instead? */
436         DefaultApply(u, "Q");
437 }
438
439
440 bool ZLine::Matches(const std::string &str)
441 {
442         if (match(str.c_str(), this->ipaddr, true))
443                 return true;
444         else
445                 return false;
446 }
447
448 bool QLine::Matches(const std::string &str)
449 {
450         if (match(str.c_str(), this->nick))
451                 return true;
452
453         return false;
454 }
455
456 bool ELine::Matches(const std::string &str)
457 {
458         return ((match(str.c_str(), matchtext.c_str(), true)));
459 }
460
461 bool KLine::Matches(const std::string &str)
462 {
463         return ((match(str.c_str(), matchtext.c_str(), true)));
464 }
465
466 bool GLine::Matches(const std::string &str)
467 {
468         return ((match(str.c_str(), matchtext.c_str(), true)));
469 }
470
471 void ELine::OnAdd()
472 {
473         /* When adding one eline, only check the one eline */
474         for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
475         {
476                 User* u = (User*)(*u2);
477                 if (this->Matches(u))
478                         u->exempt = true;
479         }
480 }
481
482 void ELine::DisplayExpiry()
483 {
484         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed E-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
485 }
486
487 void QLine::DisplayExpiry()
488 {
489         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Q-Line %s (set by %s %d seconds ago)",this->nick,this->source,this->duration);
490 }
491
492 void ZLine::DisplayExpiry()
493 {
494         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Z-Line %s (set by %s %d seconds ago)",this->ipaddr,this->source,this->duration);
495 }
496
497 void KLine::DisplayExpiry()
498 {
499         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed K-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
500 }
501
502 void GLine::DisplayExpiry()
503 {
504         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
505 }
506
507 const char* ELine::Displayable()
508 {
509         return matchtext.c_str();
510 }
511
512 const char* KLine::Displayable()
513 {
514         return matchtext.c_str();
515 }
516
517 const char* GLine::Displayable()
518 {
519         return matchtext.c_str();
520 }
521
522 const char* ZLine::Displayable()
523 {
524         return ipaddr;
525 }
526
527 const char* QLine::Displayable()
528 {
529         return nick;
530 }
531
532 bool XLineManager::RegisterFactory(XLineFactory* xlf)
533 {
534         XLineFactMap::iterator n = line_factory.find(xlf->GetType());
535
536         if (n != line_factory.end())
537                 return false;
538
539         line_factory[xlf->GetType()] = xlf;
540
541         return true;
542 }
543
544 bool XLineManager::UnregisterFactory(XLineFactory* xlf)
545 {
546         XLineFactMap::iterator n = line_factory.find(xlf->GetType());
547
548         if (n == line_factory.end())
549                 return false;
550
551         line_factory.erase(n);
552
553         return true;
554 }
555
556 XLineFactory* XLineManager::GetFactory(const std::string &type)
557 {
558         XLineFactMap::iterator n = line_factory.find(type);
559
560         if (n != line_factory.end())
561                 return NULL;
562
563         return n->second;
564 }
565