]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/xline.cpp
New logging implementation. Also write messages about InspIRCd::Log() being deprecate...
[user/henk/code/inspircd.git] / src / xline.cpp
index 028067794fb5f34db689653387cfd5d860ac4ff4..268804f9dce804981a14f4f9e700e6746ffef23b 100644 (file)
@@ -2,7 +2,7 @@
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
+ *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
  * See: http://www.inspircd.org/wiki/index.php/Credits
  *
  * This program is free but copyrighted software; see
@@ -16,6 +16,7 @@
 #include "inspircd.h"
 #include "wildcard.h"
 #include "xline.h"
+#include "bancache.h"
 
 /*
  * This is now version 3 of the XLine subsystem, let's see if we can get it as nice and 
@@ -69,14 +70,22 @@ void XLineManager::CheckELines()
        if (ELines.empty())
                return;
 
-       for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
+       for (std::vector<User*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
        {
                User* u = (User*)(*u2);
 
-               for (LookupIter i = ELines.begin(); i != ELines.end(); i++)
+               /* This uses safe iteration to ensure that if a line expires here, it doenst trash the iterator */
+               LookupIter safei;
+
+               for (LookupIter i = ELines.begin(); i != ELines.end(); )
                {
+                       safei = i;
+                       safei++;
+
                        XLine *e = i->second;
                        u->exempt = e->Matches(u);
+
+                       i = safei;
                }
        }
 }
@@ -89,6 +98,21 @@ XLineLookup* XLineManager::GetAll(const std::string &type)
        if (n == lookup_lines.end())
                return NULL;
 
+       LookupIter safei;
+       const time_t current = ServerInstance->Time();
+
+       /* Expire any dead ones, before sending */
+       for (LookupIter x = n->second.begin(); x != n->second.end(); )
+       {
+               safei = x;
+               safei++;
+               if (x->second->duration && current > x->second->expiry)
+               {
+                       ExpireLine(n, x);
+               }
+               x = safei;
+       }
+
        return &(n->second);
 }
 
@@ -127,6 +151,8 @@ bool XLineManager::AddLine(XLine* line, User* user)
 {
        /*IdentHostPair ih = IdentSplit(hostmask);*/
 
+       ServerInstance->BanCache->RemoveEntries(line->type, false); // XXX perhaps remove ELines here?
+
        if (DelLine(line->Displayable(), line->type, user, true))
                return false;
 
@@ -157,6 +183,8 @@ bool XLineManager::DelLine(const char* hostmask, const std::string &type, User*
        if (simulate)
                return true;
 
+       ServerInstance->BanCache->RemoveEntries(y->second->type, true);
+
        FOREACH_MOD(I_OnDelLine,OnDelLine(user, y->second));
 
        y->second->Unset();
@@ -175,7 +203,7 @@ bool XLineManager::DelLine(const char* hostmask, const std::string &type, User*
 void ELine::Unset()
 {
        /* remove exempt from everyone and force recheck after deleting eline */
-       for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
+       for (std::vector<User*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
        {
                User* u = (User*)(*u2);
                u->exempt = false;
@@ -195,19 +223,30 @@ XLine* XLineManager::MatchesLine(const std::string &type, User* user)
 
        const time_t current = ServerInstance->Time();
 
-       for (LookupIter i = x->second.begin(); i != x->second.end(); i++)
+       LookupIter safei;
+
+       for (LookupIter i = x->second.begin(); i != x->second.end(); )
        {
+               safei = i;
+               safei++;
+
                if (i->second->Matches(user))
                {
-                       if (current > i->second->expiry)
+                       if (i->second->duration && current > i->second->expiry)
                        {
                                /* Expire the line, return nothing */
                                ExpireLine(x, i);
-                               return NULL;
+                               /* Continue, there may be another that matches
+                                * (thanks aquanight)
+                                */
+                               i = safei;
+                               continue;
                        }
                        else
                                return i->second;
                }
+
+               i = safei;
        }
        return NULL;
 }
@@ -221,19 +260,28 @@ XLine* XLineManager::MatchesLine(const std::string &type, const std::string &pat
 
        const time_t current = ServerInstance->Time();
 
-       for (LookupIter i = x->second.begin(); i != x->second.end(); i++)
+        LookupIter safei;
+
+       for (LookupIter i = x->second.begin(); i != x->second.end(); )
        {
+               safei = i;
+               safei++;
+
                if (i->second->Matches(pattern))
                {
-                       if (current > i->second->expiry)
+                       if (i->second->duration && current > i->second->expiry)
                        {
                                /* Expire the line, return nothing */
                                ExpireLine(x, i);
-                               return NULL;
+                               /* See above */
+                               i = safei;
+                               continue;
                        }
                        else
                                return i->second;
                }
+
+               i = safei;
        }
        return NULL;
 }
@@ -241,26 +289,28 @@ XLine* XLineManager::MatchesLine(const std::string &type, const std::string &pat
 // removes lines that have expired
 void XLineManager::ExpireLine(ContainerIter container, LookupIter item)
 {
-               item->second->DisplayExpiry();
-               item->second->Unset();
+       FOREACH_MOD(I_OnExpireLine, OnExpireLine(item->second));
 
-               /* TODO: Can we skip this loop by having a 'pending' field in the XLine class, which is set when a line
-                * is pending, cleared when it is no longer pending, so we skip over this loop if its not pending?
-                * -- Brain
-                */
-               std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), item->second);
-               if (pptr != pending_lines.end())
-                       pending_lines.erase(pptr);
+       item->second->DisplayExpiry();
+       item->second->Unset();
 
-               delete item->second;
-               container->second.erase(item);
+       /* TODO: Can we skip this loop by having a 'pending' field in the XLine class, which is set when a line
+        * is pending, cleared when it is no longer pending, so we skip over this loop if its not pending?
+        * -- Brain
+        */
+       std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), item->second);
+       if (pptr != pending_lines.end())
+               pending_lines.erase(pptr);
+
+       delete item->second;
+       container->second.erase(item);
 }
 
 
 // applies lines, removing clients and changing nicks etc as applicable
 void XLineManager::ApplyLines()
 {
-       for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
+       for (std::vector<User*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
        {
                User* u = (User*)(*u2);
 
@@ -275,25 +325,33 @@ void XLineManager::ApplyLines()
        pending_lines.clear();
 }
 
-/* k: 216
- * g: 223
- * q: 217
- * z: 223
- * e: 223
- */
-
 void XLineManager::InvokeStats(const std::string &type, int numeric, User* user, string_list &results)
 {
        std::string sn = ServerInstance->Config->ServerName;
 
        ContainerIter n = lookup_lines.find(type);
 
+       time_t current = ServerInstance->Time();
+
+       LookupIter safei;
+
        if (n != lookup_lines.end())
        {
                XLineLookup& list = n->second;
-               for (LookupIter i = list.begin(); i != list.end(); i++)
-                       results.push_back(sn+" "+ConvToStr(numeric)+" "+user->nick+" :"+i->second->Displayable()+" "+
+               for (LookupIter i = list.begin(); i != list.end(); )
+               {
+                       safei = i;
+                       safei++;
+
+                       if (i->second->duration && current > i->second->expiry)
+                       {
+                               ExpireLine(n, i);
+                       }
+                       else
+                               results.push_back(sn+" "+ConvToStr(numeric)+" "+user->nick+" :"+i->second->Displayable()+" "+
                                        ConvToStr(i->second->set_time)+" "+ConvToStr(i->second->duration)+" "+std::string(i->second->source)+" :"+(i->second->reason));
+                       i = safei;
+               }
        }
 }
 
@@ -332,7 +390,7 @@ void XLine::Apply(User* u)
 {
 }
 
-void XLine::DefaultApply(User* u, const std::string &line)
+void XLine::DefaultApply(User* u, const std::string &line, bool bancache)
 {
        char reason[MAXBUF];
        snprintf(reason, MAXBUF, "%s-Lined: %s", line.c_str(), this->reason);
@@ -342,6 +400,13 @@ void XLine::DefaultApply(User* u, const std::string &line)
                User::QuitUser(ServerInstance, u, line + "-Lined", reason);
        else
                User::QuitUser(ServerInstance, u, reason);
+
+
+       if (bancache)
+       {
+               ServerInstance->Log(DEBUG, std::string("BanCache: Adding positive hit (") + line + ") for " + u->GetIPString());
+               ServerInstance->BanCache->AddHit(u->GetIPString(), this->type, line + "-Lined: " + this->reason);
+       }
 }
 
 bool KLine::Matches(User *u)
@@ -362,7 +427,7 @@ bool KLine::Matches(User *u)
 
 void KLine::Apply(User* u)
 {
-       DefaultApply(u, "K");
+       DefaultApply(u, "K", (strcmp(this->identmask, "*") == 0) ? true : false);
 }
 
 bool GLine::Matches(User *u)
@@ -383,7 +448,7 @@ bool GLine::Matches(User *u)
 
 void GLine::Apply(User* u)
 {       
-       DefaultApply(u, "G");
+       DefaultApply(u, "G", (strcmp(this->identmask, "*") == 0) ? true : false);
 }
 
 bool ELine::Matches(User *u)
@@ -415,7 +480,7 @@ bool ZLine::Matches(User *u)
 
 void ZLine::Apply(User* u)
 {       
-       DefaultApply(u, "Z");
+       DefaultApply(u, "Z", true);
 }
 
 
@@ -432,8 +497,8 @@ bool QLine::Matches(User *u)
 
 void QLine::Apply(User* u)
 {       
-       /* Can we force the user to their uid here instead? */
-       DefaultApply(u, "Q");
+       /* Force to uuid on apply of qline, no need to disconnect any more :) */
+       u->ForceNickChange(u->uuid);
 }
 
 
@@ -471,7 +536,7 @@ bool GLine::Matches(const std::string &str)
 void ELine::OnAdd()
 {
        /* When adding one eline, only check the one eline */
-       for (std::vector<User*>::const_iterator u2 = ServerInstance->local_users.begin(); u2 != ServerInstance->local_users.end(); u2++)
+       for (std::vector<User*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
        {
                User* u = (User*)(*u2);
                if (this->Matches(u))
@@ -557,7 +622,7 @@ XLineFactory* XLineManager::GetFactory(const std::string &type)
 {
        XLineFactMap::iterator n = line_factory.find(type);
 
-       if (n != line_factory.end())
+       if (n == line_factory.end())
                return NULL;
 
        return n->second;