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