]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/users.cpp
Fix server prefix on JOIN.
[user/henk/code/inspircd.git] / src / users.cpp
index 6dab2549505fb5aab0bbd92e561a1c4e9f9578bc..4a77b450f2422304cbebdcb9dd434595129c9831 100644 (file)
@@ -2,14 +2,11 @@
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
- *                    E-mail:
- *             <brain@chatspike.net>
- *               <Craig@chatspike.net>
- *     
- * Written by Craig Edwards, Craig McLure, and others.
+ *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
+ * See: http://www.inspircd.org/wiki/index.php/Credits
+ *
  * This program is free but copyrighted software; see
- *         the file COPYING for details.
+ *            the file COPYING for details.
  *
  * ---------------------------------------------------
  */
@@ -156,7 +153,7 @@ void userrec::StartDNSLookup()
                res_reverse = new UserResolver(this->ServerInstance, this, this->GetIPString(), DNS_QUERY_REVERSE);
                this->ServerInstance->AddResolver(res_reverse);
        }
-       catch (ModuleException& e)
+       catch (CoreException& e)
        {
                ServerInstance->Log(DEBUG,"Error in resolver: %s",e.GetReason());
        }
@@ -189,7 +186,7 @@ void UserResolver::OnLookupComplete(const std::string &result)
                                this->ServerInstance->AddResolver(bound_user->res_forward);
                        }
                }
-               catch (ModuleException& e)
+               catch (CoreException& e)
                {
                        ServerInstance->Log(DEBUG,"Error in resolver: %s",e.GetReason());
                }
@@ -309,26 +306,12 @@ userrec::userrec(InspIRCd* Instance) : ServerInstance(Instance)
        ip = NULL;
        chans.clear();
        invites.clear();
-       chans.resize(MAXCHANS);
        memset(modes,0,sizeof(modes));
        memset(snomasks,0,sizeof(snomasks));
-       
-       for (unsigned int n = 0; n < MAXCHANS; n++)
-       {
-               chans[n] = new ucrec();
-               chans[n]->channel = NULL;
-               chans[n]->uc_modes = 0;
-       }
 }
 
 userrec::~userrec()
 {
-       for (std::vector<ucrec*>::iterator n = chans.begin(); n != chans.end(); n++)
-       {
-               ucrec* x = (ucrec*)*n;
-               delete x;
-       }
-
        if (ip)
        {
                clonemap::iterator x = ServerInstance->local_clones.find(this->GetIPString());
@@ -453,13 +436,11 @@ char* userrec::GetFullRealHost()
        return fresult;
 }
 
-bool userrec::IsInvited(irc::string &channel)
+bool userrec::IsInvited(const irc::string &channel)
 {
        for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
        {
-               irc::string compare = i->channel;
-               
-               if (compare == channel)
+               if (channel == *i)
                {
                        return true;
                }
@@ -472,30 +453,22 @@ InvitedList* userrec::GetInviteList()
        return &invites;
 }
 
-void userrec::InviteTo(irc::string &channel)
+void userrec::InviteTo(const irc::string &channel)
 {
-       Invited i;
-       i.channel = channel;
-       invites.push_back(i);
+       invites.push_back(channel);
 }
 
-void userrec::RemoveInvite(irc::string &channel)
+void userrec::RemoveInvite(const irc::string &channel)
 {
        ServerInstance->Log(DEBUG,"Removing invites");
-       
-       if (invites.size())
+       for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
        {
-               for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
+               if (channel == *i)
                {
-                       irc::string compare = i->channel;
-                       
-                       if (compare == channel)
-                       {
-                               invites.erase(i);
-                               return;
-                               }
-                       }
-               }
+                       invites.erase(i);
+                       return;
+               }
+       }
 }
 
 bool userrec::HasPermission(const std::string &command)
@@ -653,16 +626,9 @@ void userrec::AddWriteBuf(const std::string &data)
        try 
        {
                if (data.length() > 512)
-               {
-                       std::string newdata(data);
-                       newdata.resize(510);
-                       newdata.append("\r\n");
-                       sendq.append(newdata);
-               }
+                       sendq.append(data.substr(0,510)).append("\r\n");
                else
-               {
                        sendq.append(data);
-               }
        }
        catch (...)
        {
@@ -683,28 +649,31 @@ void userrec::FlushWriteBuf()
                if ((sendq.length()) && (this->fd != FD_MAGIC_NUMBER))
                {
                        int old_sendq_length = sendq.length();
-                       const char* tb = this->sendq.c_str();
-                       int n_sent = write(this->fd,tb,this->sendq.length());
+                       int n_sent = write(this->fd, this->sendq.data(), this->sendq.length());
                        if (n_sent == -1)
                        {
                                if (errno == EAGAIN)
                                {
+                                       /* The socket buffer is full. This isnt fatal,
+                                        * try again later.
+                                        */
                                        ServerInstance->Log(DEBUG,"EAGAIN, want write");
                                        this->ServerInstance->SE->WantWrite(this);
                                }
                                else
                                {
-                                       this->QuitUser(ServerInstance, this, strerror(errno));
+                                       /* Fatal error, set write error and bail
+                                        */
+                                       this->SetWriteError(strerror(errno));
                                        return;
                                }
                        }
                        else
                        {
-                               /*ServerInstance->Log(DEBUG,"Wrote: %d of %d: %s", n_sent, old_sendq_length, sendq.substr(0, n_sent).c_str());*/
-                               // advance the queue
-                               tb += n_sent;
-                               this->sendq = tb;
-                               // update the user's stats counters
+                               /* 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++;
                                if (n_sent != old_sendq_length)
@@ -726,9 +695,8 @@ void userrec::SetWriteError(const std::string &error)
 {
        try
        {
-               ServerInstance->Log(DEBUG,"SetWriteError: %s",error.c_str());
                // don't try to set the error twice, its already set take the first string.
-               if (!this->WriteError.length())
+               if (this->WriteError.empty())
                {
                        ServerInstance->Log(DEBUG,"Setting error string for %s to '%s'",this->nick,error.c_str());
                        this->WriteError = error;
@@ -800,13 +768,17 @@ void userrec::QuitUser(InspIRCd* Instance, userrec *user, const std::string &qui
                reason.resize(MAXQUIT - 1);
        
        if (IS_LOCAL(user))
+       {
                user->Write("ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,reason.c_str());
+               if ((!user->sendq.empty()) && (!(*user->GetWriteError())))
+                       user->FlushWriteBuf();
+       }
 
        if (user->registered == REG_ALL)
        {
                user->PurgeEmptyChannels();
-               FOREACH_MOD_I(Instance,I_OnUserQuit,OnUserQuit(user,reason));
                user->WriteCommonExcept("QUIT :%s",reason.c_str());
+               FOREACH_MOD_I(Instance,I_OnUserQuit,OnUserQuit(user,reason));
        }
 
        FOREACH_MOD_I(Instance,I_OnUserDisconnect,OnUserDisconnect(user));
@@ -819,9 +791,9 @@ void userrec::QuitUser(InspIRCd* Instance, userrec *user, const std::string &qui
                        {
                                Instance->Config->GetIOHook(user->GetPort())->OnRawSocketClose(user->fd);
                        }
-                       catch (ModuleException& modexcept)
+                       catch (CoreException& modexcept)
                        {
-                               Instance->Log(DEBUG,"Module exception cought: %s",modexcept.GetReason());
+                               Instance->Log(DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
                        }
                }
                
@@ -1350,7 +1322,6 @@ int userrec::GetPort()
                }
                break;
                default:
-                       ServerInstance->Log(DEBUG,"Ut oh, '%s' has an unknown protocol family!",this->nick);
                break;
        }
        return 0;
@@ -1400,7 +1371,6 @@ const char* userrec::GetIPString()
                }
                break;
                default:
-                       ServerInstance->Log(DEBUG,"Ut oh, '%s' has an unknown protocol family!",this->nick);
                break;
        }
        return "";
@@ -1443,7 +1413,6 @@ const char* userrec::GetIPString(char* buf)
                break;
 
                default:
-                       ServerInstance->Log(DEBUG,"Ut oh, '%s' has an unknown protocol family!",this->nick);
                break;
        }
        return "";
@@ -1476,9 +1445,9 @@ void userrec::Write(std::string text)
                {
                        ServerInstance->Config->GetIOHook(this->GetPort())->OnRawSocketWrite(this->fd, text.data(), text.length());
                }
-               catch (ModuleException& modexcept)
+               catch (CoreException& modexcept)
                {
-                       ServerInstance->Log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
+                       ServerInstance->Log(DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
                }
        }
        else
@@ -1598,21 +1567,16 @@ void userrec::WriteCommon(const std::string &text)
        
                uniq_id++;
        
-               for (std::vector<ucrec*>::const_iterator v = this->chans.begin(); v != this->chans.end(); v++)
+               for (UCListIter v = this->chans.begin(); v != this->chans.end(); v++)
                {
-                       ucrec *n = *v;
-                       if (n->channel)
+                       CUList* ulist = v->first->GetUsers();
+                       for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
                        {
-                               CUList *ulist= n->channel->GetUsers();
-               
-                               for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
+                               if ((IS_LOCAL(i->second)) && (already_sent[i->second->fd] != uniq_id))
                                {
-                                       if ((IS_LOCAL(i->second)) && (already_sent[i->second->fd] != uniq_id))
-                                       {
-                                               already_sent[i->second->fd] = uniq_id;
-                                               i->second->WriteFrom(this, std::string(text));
-                                               sent_to_at_least_one = true;
-                                       }
+                                       already_sent[i->second->fd] = uniq_id;
+                                       i->second->WriteFrom(this, std::string(text));
+                                       sent_to_at_least_one = true;
                                }
                        }
                }
@@ -1701,25 +1665,20 @@ void userrec::WriteCommonExcept(const std::string &text)
                }
        }
 
-       for (std::vector<ucrec*>::const_iterator v = this->chans.begin(); v != this->chans.end(); v++)
+       for (UCListIter v = this->chans.begin(); v != this->chans.end(); v++)
        {
-               ucrec* n = *v;
-               if (n->channel)
+               CUList *ulist = v->first->GetUsers();
+               for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
                {
-                       CUList *ulist= n->channel->GetUsers();
-
-                       for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
+                       if (this != i->second)
                        {
-                               if (this != i->second)
+                               if ((IS_LOCAL(i->second)) && (already_sent[i->second->fd] != uniq_id))
                                {
-                                       if ((IS_LOCAL(i->second)) && (already_sent[i->second->fd] != uniq_id))
-                                       {
-                                               already_sent[i->second->fd] = uniq_id;
-                                               if (quit_munge)
-                                                       i->second->WriteFrom(this, *i->second->oper ? std::string(oper_quit) : std::string(textbuffer));
-                                               else
-                                                       i->second->WriteFrom(this, std::string(textbuffer));
-                                       }
+                                       already_sent[i->second->fd] = uniq_id;
+                                       if (quit_munge)
+                                               i->second->WriteFrom(this, *i->second->oper ? std::string(oper_quit) : std::string(textbuffer));
+                                       else
+                                               i->second->WriteFrom(this, std::string(textbuffer));
                                }
                        }
                }
@@ -1783,33 +1742,17 @@ bool userrec::SharesChannelWith(userrec *other)
                return false;
 
        /* Outer loop */
-       for (std::vector<ucrec*>::const_iterator i = this->chans.begin(); i != this->chans.end(); i++)
+       for (UCListIter i = this->chans.begin(); i != this->chans.end(); i++)
        {
-               /* Fetch the channel from the user */
-               ucrec* user_channel = *i;
-
-               if (user_channel->channel)
-               {
-                       /* Eliminate the inner loop (which used to be ~equal in size to the outer loop)
-                        * by replacing it with a map::find which *should* be more efficient
-                        */
-                       if (user_channel->channel->HasUser(other))
-                               return true;
-               }
+               /* Eliminate the inner loop (which used to be ~equal in size to the outer loop)
+                * by replacing it with a map::find which *should* be more efficient
+                */
+               if (i->first->HasUser(other))
+                       return true;
        }
        return false;
 }
 
-int userrec::CountChannels()
-{
-       return ChannelCount;
-}
-
-void userrec::ModChannelCount(int n)
-{
-       ChannelCount += n;
-}
-
 bool userrec::ChangeName(const char* gecos)
 {
        if (!strcmp(gecos, this->fullname))
@@ -1848,15 +1791,12 @@ bool userrec::ChangeDisplayedHost(const char* host)
 
        if (this->ServerInstance->Config->CycleHosts)
        {
-               for (std::vector<ucrec*>::const_iterator i = this->chans.begin(); i != this->chans.end(); i++)
+               for (UCListIter i = this->chans.begin(); i != this->chans.end(); i++)
                {
-                       if ((*i)->channel)
-                       {
-                               (*i)->channel->WriteAllExceptSender(this, false, 0, "JOIN %s", (*i)->channel->name);
-                               std::string n = this->ServerInstance->Modes->ModeString(this, (*i)->channel);
-                               if (n.length() > 0)
-                                       (*i)->channel->WriteAllExceptSender(this, true, 0, "MODE %s +%s", (*i)->channel->name, n.c_str());
-                       }
+                       i->first->WriteAllExceptSender(this, false, 0, "JOIN %s", i->first->name);
+                       std::string n = this->ServerInstance->Modes->ModeString(this, i->first);
+                       if (n.length() > 0)
+                               i->first->WriteAllExceptSender(this, true, 0, "MODE %s +%s", i->first->name, n.c_str());
                }
        }
 
@@ -1878,15 +1818,12 @@ bool userrec::ChangeIdent(const char* newident)
 
        if (this->ServerInstance->Config->CycleHosts)
        {
-               for (std::vector<ucrec*>::const_iterator i = this->chans.begin(); i != this->chans.end(); i++)
+               for (UCListIter i = this->chans.begin(); i != this->chans.end(); i++)
                {
-                       if ((*i)->channel)
-                       {
-                               (*i)->channel->WriteAllExceptSender(this, false, 0, "JOIN %s", (*i)->channel->name);
-                               std::string n = this->ServerInstance->Modes->ModeString(this, (*i)->channel);
-                               if (n.length() > 0)
-                                       (*i)->channel->WriteAllExceptSender(this, true, 0, "MODE %s +%s", (*i)->channel->name, n.c_str());
-                       }
+                       i->first->WriteAllExceptSender(this, false, 0, "JOIN %s", i->first->name);
+                       std::string n = this->ServerInstance->Modes->ModeString(this, i->first);
+                       if (n.length() > 0)
+                               i->first->WriteAllExceptSender(this, true, 0, "MODE %s +%s", i->first->name, n.c_str());
                }
        }
 
@@ -1918,20 +1855,15 @@ std::string userrec::ChannelList(userrec* source)
        try
        {
                std::string list;
-               for (std::vector<ucrec*>::const_iterator i = this->chans.begin(); i != this->chans.end(); i++)
+               for (UCListIter i = this->chans.begin(); i != this->chans.end(); i++)
                {
-                       ucrec* rec = *i;
-       
-                       if(rec->channel && rec->channel->name)
+                       /* If the target is the same as the sender, let them see all their channels.
+                        * If the channel is NOT private/secret OR the user shares a common channel
+                        * If the user is an oper, and the <options:operspywhois> option is set.
+                        */
+                       if ((source == this) || (*source->oper && ServerInstance->Config->OperSpyWhois) || (((!i->first->modes[CM_PRIVATE]) && (!i->first->modes[CM_SECRET])) || (i->first->HasUser(source))))
                        {
-                               /* If the target is the same as the sender, let them see all their channels.
-                                * If the channel is NOT private/secret OR the user shares a common channel
-                                * If the user is an oper, and the <options:operspywhois> option is set.
-                                */
-                               if ((source == this) || (*source->oper && ServerInstance->Config->OperSpyWhois) || (((!rec->channel->modes[CM_PRIVATE]) && (!rec->channel->modes[CM_SECRET])) || (rec->channel->HasUser(source))))
-                               {
-                                       list.append(rec->channel->GetPrefixChar(this)).append(rec->channel->name).append(" ");
-                               }
+                               list.append(i->first->GetPrefixChar(this)).append(i->first->name).append(" ");
                        }
                }
                return list;
@@ -2010,24 +1942,19 @@ void userrec::PurgeEmptyChannels()
        std::vector<chanrec*> to_delete;
 
        // firstly decrement the count on each channel
-       for (std::vector<ucrec*>::iterator f = this->chans.begin(); f != this->chans.end(); f++)
+       for (UCListIter f = this->chans.begin(); f != this->chans.end(); f++)
        {
-               ucrec* uc = *f;
-               if (uc->channel)
+               f->first->RemoveAllPrefixes(this);
+               if (f->first->DelUser(this) == 0)
                {
-                       uc->channel->RemoveAllPrefixes(this);
-                       if (uc->channel->DelUser(this) == 0)
+                       /* No users left in here, mark it for deletion */
+                       try
                        {
-                               /* No users left in here, mark it for deletion */
-                               try
-                               {
-                                       to_delete.push_back(uc->channel);
-                               }
-                               catch (...)
-                               {
-                                       ServerInstance->Log(DEBUG,"Exception in userrec::PurgeEmptyChannels to_delete.push_back()");
-                               }
-                               uc->channel = NULL;
+                               to_delete.push_back(f->first);
+                       }
+                       catch (...)
+                       {
+                               ServerInstance->Log(DEBUG,"Exception in userrec::PurgeEmptyChannels to_delete.push_back()");
                        }
                }
        }
@@ -2056,8 +1983,8 @@ void userrec::ShowMOTD()
        }
        this->WriteServ("375 %s :%s message of the day", this->nick, ServerInstance->Config->ServerName);
 
-       for (unsigned int i = 0; i < ServerInstance->Config->MOTD.size(); i++)
-               this->WriteServ("372 %s :- %s",this->nick,ServerInstance->Config->MOTD[i].c_str());
+       for (file_cache::iterator i = ServerInstance->Config->MOTD.begin(); i != ServerInstance->Config->MOTD.end(); i++)
+               this->WriteServ("372 %s :- %s",this->nick,i->c_str());
 
        this->WriteServ("376 %s :End of message of the day.", this->nick);
 }
@@ -2071,8 +1998,8 @@ void userrec::ShowRULES()
        }
        this->WriteServ("NOTICE %s :%s rules",this->nick,ServerInstance->Config->ServerName);
 
-       for (unsigned int i = 0; i < ServerInstance->Config->RULES.size(); i++)
-               this->WriteServ("NOTICE %s :%s",this->nick,ServerInstance->Config->RULES[i].c_str());
+       for (file_cache::iterator i = ServerInstance->Config->RULES.begin(); i != ServerInstance->Config->RULES.end(); i++)
+               this->WriteServ("NOTICE %s :%s",this->nick,i->c_str());
 
        this->WriteServ("NOTICE %s :End of %s rules.",this->nick,ServerInstance->Config->ServerName);
 }
@@ -2086,13 +2013,14 @@ void userrec::HandleEvent(EventType et, int errornum)
                {
                        case EVENT_READ:
                                ServerInstance->ProcessUser(this);
+
                        break;
                        case EVENT_WRITE:
                                this->FlushWriteBuf();
                        break;
                        case EVENT_ERROR:
                                /** This should be safe, but dont DARE do anything after it -- Brain */
-                               userrec::QuitUser(ServerInstance, this, errornum ? strerror(errornum) : "EOF from client");
+                               this->SetWriteError(errornum ? strerror(errornum) : "EOF from client");
                        break;
                }
        }
@@ -2100,4 +2028,11 @@ void userrec::HandleEvent(EventType et, int errornum)
        {
                ServerInstance->Log(DEBUG,"Exception in userrec::HandleEvent intercepted");
        }
+
+       /* If the user has raised an error whilst being processed, quit them now we're safe to */
+       if (!WriteError.empty())
+       {
+               userrec::QuitUser(ServerInstance, this, GetWriteError());
+       }
 }
+