]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/users.cpp
Remove unneccessary temp value which caused a win32 problem by using uint32_t type.
[user/henk/code/inspircd.git] / src / users.cpp
index d6d7b72ecb9b03181e23206ab0bb988febf36fb8..f7b6467548062d75184aa174cd83ee856fd2a4e9 100644 (file)
@@ -82,7 +82,7 @@ std::string User::ProcessNoticeMasks(const char *sm)
                                        }
                                }
                                else
-                                       this->WriteNumeric(501, "%s %c :is unknown snomask char to me", this->nick.c_str(), *c);
+                                       this->WriteNumeric(ERR_UNKNOWNSNOMASK, "%s %c :is unknown snomask char to me", this->nick.c_str(), *c);
 
                                oldadding = adding;
                        break;
@@ -189,11 +189,12 @@ User::User(InspIRCd* Instance, const std::string &uid) : ServerInstance(Instance
        reset_due = ServerInstance->Time();
        age = ServerInstance->Time();
        Penalty = 0;
-       sendqpos = sendqlength = lines_in = lastping = signon = idle_lastmsg = nping = registered = 0;
+       lines_in = lastping = signon = idle_lastmsg = nping = registered = 0;
        ChannelCount = timeout = bytes_in = bytes_out = cmds_in = cmds_out = 0;
        quietquit = OverPenalty = ExemptFromPenalty = quitting = exempt = haspassed = dns_done = false;
        fd = -1;
        recvq.clear();
+       sendq.clear();
        WriteError.clear();
        res_forward = res_reverse = NULL;
        Visibility = NULL;
@@ -617,12 +618,12 @@ std::string User::GetBuffer()
        }
 }
 
-void User::AddWriteBuf(LineBuffer *l)
+void User::AddWriteBuf(const std::string &data)
 {
        if (*this->GetWriteError())
                return;
 
-       if (this->MyClass && (sendqlength + l->GetMessageLength() > this->MyClass->GetSendqMax()))
+       if (this->MyClass && (sendq.length() + data.length() > this->MyClass->GetSendqMax()))
        {
                /*
                 * Fix by brain - Set the error text BEFORE calling, because
@@ -630,79 +631,69 @@ void User::AddWriteBuf(LineBuffer *l)
                 * to repeatedly add the text to the sendq!
                 */
                this->SetWriteError("SendQ exceeded");
-               ServerInstance->SNO->WriteToSnoMask('A', "User %s SendQ of %lu exceeds connect class maximum of %lu",this->nick.c_str(), sendqlength + l->GetMessageLength(), this->MyClass->GetSendqMax());
+               ServerInstance->SNO->WriteToSnoMask('A', "User %s SendQ of %lu exceeds connect class maximum of %lu",this->nick.c_str(),(unsigned long int)sendq.length() + data.length(),this->MyClass->GetSendqMax());
                return;
        }
 
-       sendq.push_back(l);
-       ServerInstance->stats->statsSent += l->GetMessageLength();
-       this->ServerInstance->SE->WantWrite(this);
+       if (data.length() > MAXBUF - 2) /* MAXBUF has a value of 514, to account for line terminators */
+               sendq.append(data.substr(0,MAXBUF - 4)).append("\r\n"); /* MAXBUF-4 = 510 */
+       else
+               sendq.append(data);
 }
 
 // send AS MUCH OF THE USERS SENDQ as we are able to (might not be all of it)
 void User::FlushWriteBuf()
 {
-       if ((this->fd == FD_MAGIC_NUMBER) || (*this->GetWriteError()))
-       {
-               return; // Don't do this for module created users, nor for users with a write error.
-       }
-
-       // While the sendq has lines to send..
-       while (!sendq.empty())
+       try
        {
-               LineBuffer *l = sendq.front();
-
-               int s = 0;
-
-               // We want to send from where we're up to to the end of the line if possible. I know this looks confusing. Where we're up to is sendqpos,
-               // which makes message length total length - sendqpos.
-               s = ServerInstance->SE->Send(this, l->GetMessage().substr(sendqpos, (l->GetMessageLength() - sendqpos)).data(), l->GetMessageLength() - sendqpos, 0);
-
-               if (s == -1)
+               if ((this->fd == FD_MAGIC_NUMBER) || (*this->GetWriteError()))
                {
-                       // Write error.
-                       if (errno == EAGAIN)
-                       {
-
-                               // Non-fatal; writing would just block. We don't want to block.
-                               // So try write again later.
-                               this->ServerInstance->SE->WantWrite(this);
-                       }
-                       else
-                       {
-                               this->SetWriteError(errno ? strerror(errno) : "Write error");
-                               return;
-                       }
+                       sendq.clear();
                }
-               else
+               if ((sendq.length()) && (this->fd != FD_MAGIC_NUMBER))
                {
-                       // Update bytes sent.
-                       this->bytes_out += s;
+                       int old_sendq_length = sendq.length();
+                       int n_sent = ServerInstance->SE->Send(this, this->sendq.data(), this->sendq.length(), 0);
 
-                       // If what was just written + already written is not the whole message
-                       if ((s + sendqpos) != l->GetMessageLength())
+                       if (n_sent == -1)
                        {
-                               sendqpos = s; // save our current position
+                               if (errno == EAGAIN)
+                               {
+                                       /* The socket buffer is full. This isnt fatal,
+                                        * try again later.
+                                        */
+                                       this->ServerInstance->SE->WantWrite(this);
+                               }
+                               else
+                               {
+                                       /* Fatal error, set write error and bail
+                                        */
+                                       this->SetWriteError(errno ? strerror(errno) : "EOF from client");
+                                       return;
+                               }
                        }
                        else
                        {
-                               sendqpos = 0; // it was the full message.
+                               /* advance the queue */
+                               if (n_sent)
+                                       this->sendq = this->sendq.substr(n_sent);
+                               /* update the user's stats counters */
+                               this->bytes_out += n_sent;
                                this->cmds_out++;
-                               sendq.pop_front();
-
-                               // If we're the last one to use this line buffer, delete it
-                               if (l->DecrementCount() == 0)
-                               {
-                                       delete l;
-                               }
+                               if (n_sent != old_sendq_length)
+                                       this->ServerInstance->SE->WantWrite(this);
                        }
                }
        }
 
+       catch (...)
+       {
+               ServerInstance->Logs->Log("USERS", DEBUG,"Exception in User::FlushWriteBuf()");
+       }
+
        if (this->sendq.empty())
        {
-               sendq.resize(0);
-               FOREACH_MOD(I_OnBufferFlushed, OnBufferFlushed(this));
+               FOREACH_MOD(I_OnBufferFlushed,OnBufferFlushed(this));
        }
 }
 
@@ -895,7 +886,7 @@ void User::CheckClass()
        this->MaxChans = a->GetMaxChans();
 }
 
-void User::CheckLines()
+bool User::CheckLines()
 {
        const char* check[] = { "G" , "K", NULL };
 
@@ -908,10 +899,12 @@ void User::CheckLines()
                        if (r)
                        {
                                r->Apply(this);
-                               return;
+                               return true;
                        }
                }
        }
+
+       return false;
 }
 
 void User::FullConnect()
@@ -936,16 +929,17 @@ void User::FullConnect()
                return;
        }
 
-       CheckLines();
+       if (this->CheckLines())
+               return;
 
        this->WriteServ("NOTICE Auth :Welcome to \002%s\002!",ServerInstance->Config->Network);
-       this->WriteNumeric(001, "%s :Welcome to the %s IRC Network %s!%s@%s",this->nick.c_str(), ServerInstance->Config->Network, this->nick.c_str(), this->ident.c_str(), this->host.c_str());
-       this->WriteNumeric(002, "%s :Your host is %s, running version InspIRCd-1.2",this->nick.c_str(),ServerInstance->Config->ServerName);
-       this->WriteNumeric(003, "%s :This server was created %s %s", this->nick.c_str(), __TIME__, __DATE__);
-       this->WriteNumeric(004, "%s %s InspIRCd-1.2 %s %s %s", this->nick.c_str(), ServerInstance->Config->ServerName, ServerInstance->Modes->UserModeList().c_str(), ServerInstance->Modes->ChannelModeList().c_str(), ServerInstance->Modes->ParaModeList().c_str());
+       this->WriteNumeric(RPL_WELCOME, "%s :Welcome to the %s IRC Network %s!%s@%s",this->nick.c_str(), ServerInstance->Config->Network, this->nick.c_str(), this->ident.c_str(), this->host.c_str());
+       this->WriteNumeric(RPL_YOURHOSTIS, "%s :Your host is %s, running version InspIRCd-1.2",this->nick.c_str(),ServerInstance->Config->ServerName);
+       this->WriteNumeric(RPL_SERVERCREATED, "%s :This server was created %s %s", this->nick.c_str(), __TIME__, __DATE__);
+       this->WriteNumeric(RPL_SERVERVERSION, "%s %s InspIRCd-1.2 %s %s %s", this->nick.c_str(), ServerInstance->Config->ServerName, ServerInstance->Modes->UserModeList().c_str(), ServerInstance->Modes->ChannelModeList().c_str(), ServerInstance->Modes->ParaModeList().c_str());
 
        ServerInstance->Config->Send005(this);
-       this->WriteNumeric(42, "%s %s :your unique ID", this->nick.c_str(), this->uuid.c_str());
+       this->WriteNumeric(RPL_YOURUUID, "%s %s :your unique ID", this->nick.c_str(), this->uuid.c_str());
 
 
        this->ShowMOTD();
@@ -1107,13 +1101,116 @@ int User::GetProtocolFamily()
        return sin->sin_family;
 }
 
-/*
- * XXX the duplication here is horrid..
- * do we really need two methods doing essentially the same thing?
- */
+const char* User::GetCIDRMask(int range)
+{
+       static char buf[44];
+
+       if (this->ip == NULL)
+               return "";
+
+       if (range < 0)
+               throw "Negative range, sorry, no.";
+
+       /*
+        * Original code written by Oliver Lupton (Om).
+        * Integrated by me. Thanks. :) -- w00t
+        */
+       switch (this->GetProtocolFamily())
+       {
+#ifdef SUPPORT_IP6LINKS
+               case AF_INET6:
+               {
+                       /* unsigned char s6_addr[16]; */
+                       struct in6_addr v6;
+                       sockaddr_in6* sin;
+                       int i, bytestozero, extrabits;
+                       char buffer[40];
+                       
+                       if(range > 128)
+                               throw "CIDR mask width greater than address width (IPv6, 128 bit)";
+
+                       /* Access the user's IP structure directly */
+                       sin = (sockaddr_in6*)this->ip;
+
+                       /* To create the CIDR mask we want to set all the bits after 'range' bits of the address
+                        * to zero. This means the last (128 - range) bits of the address must be set to zero.
+                        * Hence this number divided by 8 is the number of whole bytes from the end of the address
+                        * which must be set to zero.
+                        */
+                       bytestozero = (128 - range) / 8;
+                       
+                       /* Some of the least significant bits of the next most significant byte may also have to
+                        * be zeroed. The number of bits is the remainder of the above division.
+                        */
+                       extrabits = (128 - range) % 8;
+                       
+                       /* Populate our working struct with the parts of the user's IP which are required in the
+                        * final CIDR mask. Set all the subsequent bytes to zero.
+                        * (16 - bytestozero) is the number of bytes which must be populated with actual IP data.
+                        */
+                       for(i = 0; i < (16 - bytestozero); i++)
+                       {
+                               v6.s6_addr[i] = sin->sin6_addr.s6_addr[i];
+                       }
+                       
+                       /* And zero all the remaining bytes in the IP. */
+                       for(; i < 16; i++)
+                       {
+                               v6.s6_addr[i] = 0;
+                       }
+                                       
+                       /* And finally, zero the extra bits required. */
+                       v6.s6_addr[15 - bytestozero] = (v6.s6_addr[15 - bytestozero] >> extrabits) << extrabits;
+
+                       snprintf(buf, 44, "%s/%d", inet_ntop(AF_INET6, &v6, buffer, 40), range);
+                       return buf;
+               }
+               break;
+#endif
+               case AF_INET:
+               {
+                       struct in_addr v4;
+                       sockaddr_in* sin;
+                       char buffer[16];
+
+                       if (range > 32)
+                               throw "CIDR mask width greater than address width (IPv4, 32 bit)";
+
+                       /* Users already have a sockaddr* pointer (User::ip) which contains either a v4 or v6 structure */
+                       sin = (sockaddr_in*)this->ip;
+                       v4.s_addr = sin->sin_addr.s_addr;
+
+                       /* To create the CIDR mask we want to set all the bits after 'range' bits of the address
+                        * to zero. This means the last (32 - range) bits of the address must be set to zero.
+                        * This is done by shifting the value right and then back left by (32 - range) bits.
+                        */
+                       if(range > 0)
+                       {
+                               v4.s_addr = ntohl(v4.s_addr);
+                               v4.s_addr = (v4.s_addr >> (32 - range)) << (32 - range);
+                               v4.s_addr = htonl(v4.s_addr);
+                       }
+                       else
+                       {
+                               /* a range of zero would cause a 32 bit value to be shifted by 32 bits.
+                                * this has undefined behaviour, but for CIDR purposes the resulting mask
+                                * from a.b.c.d/0 is 0.0.0.0/0
+                                */
+                               v4.s_addr = 0;
+                       }
+
+                       snprintf(buf, 44, "%s/%d", inet_ntop(AF_INET, &v4, buffer, 16), range);
+                       return buf;
+               }
+               break;
+       }
+
+       return ""; // unused, but oh well
+}
+
 const char* User::GetIPString(bool translate4in6)
 {
-       static char buf[1024];
+       static char buf[40];
 
        if (this->ip == NULL)
                return "";
@@ -1204,10 +1301,10 @@ void User::Write(std::string text)
        }
        else
        {
-               LineBuffer *l = new LineBuffer(text);
-               l->SetRefcount(1);
-               this->AddWriteBuf(l);
+               this->AddWriteBuf(text);
        }
+       ServerInstance->stats->statsSent += text.length();
+       this->ServerInstance->SE->WantWrite(this);
 }
 
 /** Write()
@@ -1348,13 +1445,8 @@ void User::WriteCommon(const std::string &text)
                InitializeAlreadySent(ServerInstance->SE);
 
        /* We dont want to be doing this n times, just once */
-       std::string buf = ":";
-       buf.append(this->GetFullHost());
-       buf.append(" ");
-       buf.append(text);
-
-       LineBuffer *l = NULL;
-       unsigned long total = 0;
+       snprintf(tb,MAXBUF,":%s %s",this->GetFullHost().c_str(),text.c_str());
+       std::string out = tb;
 
        for (UCListIter v = this->chans.begin(); v != this->chans.end(); v++)
        {
@@ -1363,14 +1455,8 @@ void User::WriteCommon(const std::string &text)
                {
                        if ((IS_LOCAL(i->first)) && (already_sent[i->first->fd] != uniq_id))
                        {
-                               if (!l)
-                               {
-                                       // sending to the first user
-                                       l = new LineBuffer(buf);
-                               }
                                already_sent[i->first->fd] = uniq_id;
-                               i->first->AddWriteBuf(l);
-                               total++;
+                               i->first->Write(out);
                                sent_to_at_least_one = true;
                        }
                }
@@ -1384,10 +1470,6 @@ void User::WriteCommon(const std::string &text)
        {
                this->Write(std::string(tb));
        }
-       else
-       {
-               l->SetRefcount(total);
-       }
 }
 
 
@@ -1409,6 +1491,9 @@ void User::WriteCommonExcept(const char* text, ...)
 
 void User::WriteCommonQuit(const std::string &normal_text, const std::string &oper_text)
 {
+       char tb1[MAXBUF];
+       char tb2[MAXBUF];
+
        if (this->registered != REG_ALL)
                return;
 
@@ -1417,21 +1502,10 @@ void User::WriteCommonQuit(const std::string &normal_text, const std::string &op
        if (!already_sent)
                InitializeAlreadySent(ServerInstance->SE);
 
-       unsigned int opercount = 0;
-       unsigned int usercount = 0;
-
-       std::string operquit = ":";
-       operquit.append(this->GetFullHost());
-       operquit.append(" QUIT :");
-       operquit.append(oper_text);
-
-       std::string userquit = ":";
-       userquit.append(this->GetFullHost());
-       userquit.append(" QUIT :");
-       userquit.append(normal_text);
-
-       LineBuffer *ol = new LineBuffer(operquit);
-       LineBuffer *ul = new LineBuffer(userquit);
+       snprintf(tb1,MAXBUF,":%s QUIT :%s",this->GetFullHost().c_str(),normal_text.c_str());
+       snprintf(tb2,MAXBUF,":%s QUIT :%s",this->GetFullHost().c_str(),oper_text.c_str());
+       std::string out1 = tb1;
+       std::string out2 = tb2;
 
        for (UCListIter v = this->chans.begin(); v != this->chans.end(); v++)
        {
@@ -1443,35 +1517,18 @@ void User::WriteCommonQuit(const std::string &normal_text, const std::string &op
                                if ((IS_LOCAL(i->first)) && (already_sent[i->first->fd] != uniq_id))
                                {
                                        already_sent[i->first->fd] = uniq_id;
-
-                                       if (IS_OPER(i->first))
-                                       {
-                                               i->first->AddWriteBuf(ol);
-                                               opercount++;
-                                       }
-                                       else
-                                       {
-                                               i->first->AddWriteBuf(ul);
-                                               usercount++;
-                                       }
+                                       i->first->Write(IS_OPER(i->first) ? out2 : out1);
                                }
                        }
                }
        }
-
-       if (opercount == 0)
-               free(ol);
-       else
-               ol->SetRefcount(opercount);
-
-       if (usercount == 0)
-               free(ul);
-       else
-               ul->SetRefcount(usercount);
 }
 
 void User::WriteCommonExcept(const std::string &text)
 {
+       char tb1[MAXBUF];
+       std::string out1;
+
        if (this->registered != REG_ALL)
                return;
 
@@ -1480,13 +1537,8 @@ void User::WriteCommonExcept(const std::string &text)
        if (!already_sent)
                InitializeAlreadySent(ServerInstance->SE);
 
-       unsigned long total = 0;
-       LineBuffer *l = NULL;
-
-       std::string buf = ":";
-       buf.append(this->GetFullHost());
-       buf.append(" ");
-       buf.append(text);
+       snprintf(tb1,MAXBUF,":%s %s",this->GetFullHost().c_str(),text.c_str());
+       out1 = tb1;
 
        for (UCListIter v = this->chans.begin(); v != this->chans.end(); v++)
        {
@@ -1497,26 +1549,17 @@ void User::WriteCommonExcept(const std::string &text)
                        {
                                if ((IS_LOCAL(i->first)) && (already_sent[i->first->fd] != uniq_id))
                                {
-                                       if (!l)
-                                               l = new LineBuffer(buf);
-
                                        already_sent[i->first->fd] = uniq_id;
-                                       i->first->AddWriteBuf(l);
-                                       total++;
+                                       i->first->Write(out1);
                                }
                        }
                }
        }
 
-       if (l)
-       {
-               l->SetRefcount(total);
-       }
 }
 
 void User::WriteWallOps(const std::string &text)
 {
-       // XXX: this does not yet abuse refcounted linebuffers for sending -- w00t
        if (!IS_LOCAL(this))
                return;
 
@@ -1627,7 +1670,7 @@ bool User::ChangeDisplayedHost(const char* shost)
        }
 
        if (IS_LOCAL(this))
-               this->WriteNumeric(396, "%s %s :is now your displayed host",this->nick.c_str(),this->dhost.c_str());
+               this->WriteNumeric(RPL_YOURDISPLAYEDHOST, "%s %s :is now your displayed host",this->nick.c_str(),this->dhost.c_str());
 
        return true;
 }
@@ -1661,29 +1704,20 @@ bool User::ChangeIdent(const char* newident)
 void User::SendAll(const char* command, const char* text, ...)
 {
        char textbuffer[MAXBUF];
+       char formatbuffer[MAXBUF];
        va_list argsPtr;
 
        va_start(argsPtr, text);
        vsnprintf(textbuffer, MAXBUF, text, argsPtr);
        va_end(argsPtr);
 
-       std::string buf = ":";
-       buf.append(this->GetFullHost());
-       buf.append(" ");
-       buf.append(command);
-       buf.append(" $* :");
-       buf.append(textbuffer);
-       LineBuffer *l = NULL;
+       snprintf(formatbuffer,MAXBUF,":%s %s $* :%s", this->GetFullHost().c_str(), command, textbuffer);
+       std::string fmt = formatbuffer;
 
        for (std::vector<User*>::const_iterator i = ServerInstance->Users->local_users.begin(); i != ServerInstance->Users->local_users.end(); i++)
        {
-               if (!l)
-                       l = new LineBuffer(buf);
-               (*i)->AddWriteBuf(l);
+               (*i)->Write(fmt);
        }
-
-       if (l)
-               l->SetRefcount(ServerInstance->Users->local_users.size());
 }
 
 
@@ -1913,31 +1947,31 @@ void User::ShowMOTD()
 {
        if (!ServerInstance->Config->MOTD.size())
        {
-               this->WriteNumeric(422, "%s :Message of the day file is missing.",this->nick.c_str());
+               this->WriteNumeric(ERR_NOMOTD, "%s :Message of the day file is missing.",this->nick.c_str());
                return;
        }
-       this->WriteNumeric(375, "%s :%s message of the day", this->nick.c_str(), ServerInstance->Config->ServerName);
+       this->WriteNumeric(RPL_MOTDSTART, "%s :%s message of the day", this->nick.c_str(), ServerInstance->Config->ServerName);
 
        for (file_cache::iterator i = ServerInstance->Config->MOTD.begin(); i != ServerInstance->Config->MOTD.end(); i++)
-               this->WriteNumeric(372, "%s :- %s",this->nick.c_str(),i->c_str());
+               this->WriteNumeric(RPL_MOTD, "%s :- %s",this->nick.c_str(),i->c_str());
 
-       this->WriteNumeric(376, "%s :End of message of the day.", this->nick.c_str());
+       this->WriteNumeric(RPL_ENDOFMOTD, "%s :End of message of the day.", this->nick.c_str());
 }
 
 void User::ShowRULES()
 {
        if (!ServerInstance->Config->RULES.size())
        {
-               this->WriteNumeric(434, "%s :RULES File is missing",this->nick.c_str());
+               this->WriteNumeric(ERR_NORULES, "%s :RULES File is missing",this->nick.c_str());
                return;
        }
 
-       this->WriteNumeric(308, "%s :- %s Server Rules -",this->nick.c_str(),ServerInstance->Config->ServerName);
+       this->WriteNumeric(RPL_RULESTART, "%s :- %s Server Rules -",this->nick.c_str(),ServerInstance->Config->ServerName);
 
        for (file_cache::iterator i = ServerInstance->Config->RULES.begin(); i != ServerInstance->Config->RULES.end(); i++)
-               this->WriteNumeric(232, "%s :- %s",this->nick.c_str(),i->c_str());
+               this->WriteNumeric(RPL_RULES, "%s :- %s",this->nick.c_str(),i->c_str());
 
-       this->WriteNumeric(309, "%s :End of RULES command.",this->nick.c_str());
+       this->WriteNumeric(RPL_RULESEND, "%s :End of RULES command.",this->nick.c_str());
 }
 
 void User::HandleEvent(EventType et, int errornum)