]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/channels.cpp
Flesh this out some more
[user/henk/code/inspircd.git] / src / channels.cpp
index cc5a2d7d5cc1960a91b3c9dabf9b2475bc0cb0fe..b0de06267ed7c1e81fbdbc5a1b5c16012acfeb04 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
 #include "wildcard.h"
 #include "mode.h"
 
-Channel::Channel(InspIRCd* Instance) : ServerInstance(Instance)
+Channel::Channel(InspIRCd* Instance, const std::string &cname, time_t ts) : ServerInstance(Instance)
 {
-       *name = *topic = *setby = *key = 0;
-       maxbans = created = topicset = limit = 0;
+       chan_hash::iterator findchan = ServerInstance->chanlist->find(cname);
+       if (findchan != Instance->chanlist->end())
+               throw CoreException("Cannot create duplicate channel " + cname);
+
+       (*(ServerInstance->chanlist))[cname.c_str()] = this;
+       strlcpy(this->name, cname.c_str(), CHANMAX);
+       this->created = ts ? ts : ServerInstance->Time();
+       this->age = this->created;
+
+
+
+
+       *topic = *setby = *key = 0;
+       maxbans = topicset = limit = 0;
        memset(&modes,0,64);
-       age = ServerInstance->Time(true);
 }
 
 void Channel::SetMode(char mode,bool mode_on)
@@ -173,6 +184,7 @@ CUList* Channel::GetVoicedUsers()
 
 void Channel::SetDefaultModes()
 {
+       ServerInstance->Logs->Log("CHANNELS", DEBUG, "SetDefaultModes %s", ServerInstance->Config->DefaultModes);
        irc::spacesepstream list(ServerInstance->Config->DefaultModes);
        std::string modeseq;
        std::string parameter;
@@ -198,7 +210,7 @@ void Channel::SetDefaultModes()
  * add a channel to a user, creating the record for it if needed and linking
  * it to the user record 
  */
-Channel* Channel::JoinUser(InspIRCd* Instance, User *user, const char* cn, bool override, const char* key, time_t TS)
+Channel* Channel::JoinUser(InspIRCd* Instance, User *user, const char* cn, bool override, const char* key, bool bursting, time_t TS)
 {
        if (!user || !cn)
                return NULL;
@@ -220,7 +232,7 @@ Channel* Channel::JoinUser(InspIRCd* Instance, User *user, const char* cn, bool
                {
                        if (user->chans.size() >= user->GetMaxChans())
                        {
-                               user->WriteServ("405 %s %s :You are on too many channels",user->nick, cn);
+                               user->WriteNumeric(405, "%s %s :You are on too many channels",user->nick, cn);
                                return NULL;
                        }
                }
@@ -230,7 +242,7 @@ Channel* Channel::JoinUser(InspIRCd* Instance, User *user, const char* cn, bool
                        {
                                if (user->chans.size() >= Instance->Config->OperMaxChans)
                                {
-                                       user->WriteServ("405 %s %s :You are on too many channels",user->nick, cn);
+                                       user->WriteNumeric(405, "%s %s :You are on too many channels",user->nick, cn);
                                        return NULL;
                                }
                        }
@@ -238,7 +250,7 @@ Channel* Channel::JoinUser(InspIRCd* Instance, User *user, const char* cn, bool
                        {
                                if (user->chans.size() >= Instance->Config->MaxChans)
                                {
-                                       user->WriteServ("405 %s %s :You are on too many channels",user->nick, cn);
+                                       user->WriteNumeric(405, "%s %s :You are on too many channels",user->nick, cn);
                                        return NULL;
                                }
                        }
@@ -256,7 +268,7 @@ Channel* Channel::JoinUser(InspIRCd* Instance, User *user, const char* cn, bool
                if (!IS_LOCAL(user))
                {
                        if (!TS)
-                               Instance->Log(DEBUG,"*** BUG *** Channel::JoinUser called for REMOTE user '%s' on channel '%s' but no TS given!", user->nick, cn);
+                               Instance->Logs->Log("CHANNEL",DEBUG,"*** BUG *** Channel::JoinUser called for REMOTE user '%s' on channel '%s' but no TS given!", user->nick, cn);
                }
                else
                {
@@ -266,26 +278,12 @@ Channel* Channel::JoinUser(InspIRCd* Instance, User *user, const char* cn, bool
                if (IS_LOCAL(user) && override == false)
                {
                        MOD_RESULT = 0;
-                       FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,NULL,cname,privs));
+                       FOREACH_RESULT_I(Instance,I_OnUserPreJoin, OnUserPreJoin(user, NULL, cname, privs, key ? key : ""));
                        if (MOD_RESULT == 1)
                                return NULL;
                }
 
-               /* create a new one */
-               Ptr = new Channel(Instance);
-               (*(Instance->chanlist))[cname] = Ptr;
-
-               strlcpy(Ptr->name, cname,CHANMAX);
-
-               /* As spotted by jilles, dont bother to set this on remote users */
-               if (IS_LOCAL(user))
-                       Ptr->SetDefaultModes();
-
-               Ptr->created = TS ? TS : Instance->Time();
-               Ptr->age = Ptr->created;
-               *Ptr->topic = 0;
-               *Ptr->setby = 0;
-               Ptr->topicset = 0;
+               Ptr = new Channel(Instance, cname, TS);
        }
        else
        {
@@ -300,7 +298,7 @@ Channel* Channel::JoinUser(InspIRCd* Instance, User *user, const char* cn, bool
                if (IS_LOCAL(user) && override == false)
                {
                        MOD_RESULT = 0;
-                       FOREACH_RESULT_I(Instance,I_OnUserPreJoin,OnUserPreJoin(user,Ptr,cname,privs));
+                       FOREACH_RESULT_I(Instance,I_OnUserPreJoin, OnUserPreJoin(user, Ptr, cname, privs, key ? key : ""));
                        if (MOD_RESULT == 1)
                        {
                                return NULL;
@@ -315,7 +313,7 @@ Channel* Channel::JoinUser(InspIRCd* Instance, User *user, const char* cn, bool
                                        {
                                                if ((!key) || strcmp(key,Ptr->key))
                                                {
-                                                       user->WriteServ("475 %s %s :Cannot join channel (Incorrect channel key)",user->nick, Ptr->name);
+                                                       user->WriteNumeric(475, "%s %s :Cannot join channel (Incorrect channel key)",user->nick, Ptr->name);
                                                        return NULL;
                                                }
                                        }
@@ -328,7 +326,7 @@ Channel* Channel::JoinUser(InspIRCd* Instance, User *user, const char* cn, bool
                                        {
                                                if (!user->IsInvited(Ptr->name))
                                                {
-                                                       user->WriteServ("473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
+                                                       user->WriteNumeric(473, "%s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
                                                        return NULL;
                                                }
                                        }
@@ -342,7 +340,7 @@ Channel* Channel::JoinUser(InspIRCd* Instance, User *user, const char* cn, bool
                                        {
                                                if (Ptr->GetUserCounter() >= Ptr->limit)
                                                {
-                                                       user->WriteServ("471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
+                                                       user->WriteNumeric(471, "%s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
                                                        return NULL;
                                                }
                                        }
@@ -351,7 +349,7 @@ Channel* Channel::JoinUser(InspIRCd* Instance, User *user, const char* cn, bool
                                {
                                        if (Ptr->IsBanned(user))
                                        {
-                                               user->WriteServ("474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
+                                               user->WriteNumeric(474, "%s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
                                                return NULL;
                                        }
                                }
@@ -359,10 +357,14 @@ Channel* Channel::JoinUser(InspIRCd* Instance, User *user, const char* cn, bool
                }
        }
 
-       return Channel::ForceChan(Instance, Ptr, user, privs);
+       /* As spotted by jilles, dont bother to set this on remote users */
+       if (IS_LOCAL(user) && Ptr->GetUserCounter() == 0)
+               Ptr->SetDefaultModes();
+
+       return Channel::ForceChan(Instance, Ptr, user, privs, bursting);
 }
 
-Channel* Channel::ForceChan(InspIRCd* Instance, Channel* Ptr, User* user, const std::string &privs)
+Channel* Channel::ForceChan(InspIRCd* Instance, Channel* Ptr, User* user, const std::string &privs, bool bursting)
 {
        std::string nick = user->nick;
        bool silent = false;
@@ -378,18 +380,15 @@ Channel* Channel::ForceChan(InspIRCd* Instance, Channel* Ptr, User* user, const
                ModeHandler* mh = Instance->Modes->FindPrefix(status);
                if (mh)
                {
+                       /* Set, and make sure that the mode handler knows this mode was now set */
                        Ptr->SetPrefix(user, status, mh->GetPrefixRank(), true);
-                       /* Make sure that the mode handler knows this mode was now set */
                        mh->OnModeChange(Instance->FakeClient, Instance->FakeClient, Ptr, nick, true);
 
                        switch (mh->GetPrefix())
                        {
-                               /* These logic ops are SAFE IN THIS CASE
-                                * because if the entry doesnt exist,
-                                * addressing operator[] creates it.
-                                * If they do exist, it points to it.
-                                * At all other times where we dont want
-                                * to create an item if it doesnt exist, we
+                               /* These logic ops are SAFE IN THIS CASE because if the entry doesnt exist,
+                                * addressing operator[] creates it. If they do exist, it points to it.
+                                * At all other times where we dont want to create an item if it doesnt exist, we
                                 * must stick to ::find().
                                 */
                                case '@':
@@ -405,7 +404,7 @@ Channel* Channel::ForceChan(InspIRCd* Instance, Channel* Ptr, User* user, const
                }
        }
 
-       FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user, Ptr, silent));
+       FOREACH_MOD_I(Instance,I_OnUserJoin,OnUserJoin(user, Ptr, bursting, silent));
 
        if (!silent)
                Ptr->WriteChannel(user,"JOIN :%s",Ptr->name);
@@ -420,8 +419,8 @@ Channel* Channel::ForceChan(InspIRCd* Instance, Channel* Ptr, User* user, const
        {
                if (Ptr->topicset)
                {
-                       user->WriteServ("332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
-                       user->WriteServ("333 %s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
+                       user->WriteNumeric(332, "%s %s :%s", user->nick, Ptr->name, Ptr->topic);
+                       user->WriteNumeric(333, "%s %s %s %lu", user->nick, Ptr->name, Ptr->setby, (unsigned long)Ptr->topicset);
                }
                Ptr->UserList(user);
        }
@@ -434,7 +433,10 @@ bool Channel::IsBanned(User* user)
        char mask[MAXBUF];
        int MOD_RESULT = 0;
        FOREACH_RESULT(I_OnCheckBan,OnCheckBan(user, this));
-       if (!MOD_RESULT)
+
+       if (MOD_RESULT == -1)
+               return true;
+       else if (MOD_RESULT == 0)
        {
                snprintf(mask, MAXBUF, "%s!%s@%s", user->nick, user->ident, user->GetIPString());
                for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
@@ -452,6 +454,37 @@ bool Channel::IsBanned(User* user)
        return false;
 }
 
+bool Channel::IsExtBanned(User *user, char type)
+{
+       // XXX. do we need events?
+       char mask[MAXBUF];
+       char *maskptr;
+
+       snprintf(mask, MAXBUF, "%s!%s@%s", user->nick, user->ident, user->GetIPString());
+
+       for (BanList::iterator i = this->bans.begin(); i != this->bans.end(); i++)
+       {
+               if (i->data[0] != type || i->data[1] != ':')
+                       continue;
+
+               // Iterate past char and : to get to the mask without doing a data copy(!)
+               maskptr = i->data;
+               maskptr++; // past the char
+               maskptr++; // past the :
+
+               /* This allows CIDR ban matching
+                * 
+                *        Full masked host                             Full unmasked host                     IP with/without CIDR
+                */
+               if ((match(user->GetFullHost(), maskptr)) || (match(user->GetFullRealHost(), maskptr)) || (match(mask, maskptr, true)))
+               {
+                       return true;
+               }
+       }
+
+       return false;
+}
+
 /* Channel::PartUser
  * remove a channel from a users record, and return the number of users left.
  * Therefore, if this function returns 0 the caller should delete the Channel.
@@ -481,7 +514,11 @@ long Channel::PartUser(User *user, const char* reason)
                /* kill the record */
                if (iter != ServerInstance->chanlist->end())
                {
-                       FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
+                       int MOD_RESULT = 0;
+                       FOREACH_RESULT_I(ServerInstance,I_OnChannelPreDelete, OnChannelPreDelete(this));
+                       if (MOD_RESULT == 1)
+                               return 1; // delete halted by module
+                       FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(this));
                        ServerInstance->chanlist->erase(iter);
                }
                return 0;
@@ -490,7 +527,7 @@ long Channel::PartUser(User *user, const char* reason)
        return this->GetUserCounter();
 }
 
-long Channel::ServerKickUser(User* user, const char* reason, bool triggerevents)
+long Channel::ServerKickUser(User* user, const char* reason, bool triggerevents, const char* servername)
 {
        bool silent = false;
 
@@ -506,6 +543,9 @@ long Channel::ServerKickUser(User* user, const char* reason, bool triggerevents)
                }
        }
 
+       if (servername == NULL || *ServerInstance->Config->HideWhoisServer)
+               servername = ServerInstance->Config->ServerName;
+
        if (triggerevents)
        {
                FOREACH_MOD(I_OnUserKick,OnUserKick(NULL, user, this, reason, silent));
@@ -515,7 +555,7 @@ long Channel::ServerKickUser(User* user, const char* reason, bool triggerevents)
        if (i != user->chans.end())
        {
                if (!silent)
-                       this->WriteChannelWithServ(ServerInstance->Config->ServerName, "KICK %s %s :%s", this->name, user->nick, reason);
+                       this->WriteChannelWithServ(servername, "KICK %s %s :%s", this->name, user->nick, reason);
 
                user->chans.erase(i);
                this->RemoveAllPrefixes(user);
@@ -527,7 +567,11 @@ long Channel::ServerKickUser(User* user, const char* reason, bool triggerevents)
                /* kill the record */
                if (iter != ServerInstance->chanlist->end())
                {
-                       FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
+                       int MOD_RESULT = 0;
+                       FOREACH_RESULT_I(ServerInstance,I_OnChannelPreDelete, OnChannelPreDelete(this));
+                       if (MOD_RESULT == 1)
+                               return 1; // delete halted by module
+                       FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(this));
                        ServerInstance->chanlist->erase(iter);
                }
                return 0;
@@ -547,12 +591,12 @@ long Channel::KickUser(User *src, User *user, const char* reason)
        {
                if (!this->HasUser(user))
                {
-                       src->WriteServ("441 %s %s %s :They are not on that channel",src->nick, user->nick, this->name);
+                       src->WriteNumeric(441, "%s %s %s :They are not on that channel",src->nick, user->nick, this->name);
                        return this->GetUserCounter();
                }
                if ((ServerInstance->ULine(user->server)) && (!ServerInstance->ULine(src->server)))
                {
-                       src->WriteServ("482 %s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
+                       src->WriteNumeric(482, "%s %s :Only a u-line may kick a u-line from a channel.",src->nick, this->name);
                        return this->GetUserCounter();
                }
                int MOD_RESULT = 0;
@@ -577,7 +621,7 @@ long Channel::KickUser(User *src, User *user, const char* reason)
                                int us = this->GetStatus(user);
                                if ((them < STATUS_HOP) || (them < us))
                                {
-                                       src->WriteServ("482 %s %s :You must be a channel %soperator",src->nick, this->name, them == STATUS_HOP ? "" : "half-");
+                                       src->WriteNumeric(482, "%s %s :You must be a channel %soperator",src->nick, this->name, them == STATUS_HOP ? "" : "half-");
                                        return this->GetUserCounter();
                                }
                        }
@@ -605,7 +649,11 @@ long Channel::KickUser(User *src, User *user, const char* reason)
                /* kill the record */
                if (iter != ServerInstance->chanlist->end())
                {
-                       FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(this));
+                       int MOD_RESULT = 0;
+                       FOREACH_RESULT_I(ServerInstance,I_OnChannelPreDelete, OnChannelPreDelete(this));
+                       if (MOD_RESULT == 1)
+                               return 1; // delete halted by module
+                       FOREACH_MOD(I_OnChannelDelete, OnChannelDelete(this));
                        ServerInstance->chanlist->erase(iter);
                }
                return 0;
@@ -614,7 +662,7 @@ long Channel::KickUser(User *src, User *user, const char* reason)
        return this->GetUserCounter();
 }
 
-void Channel::WriteChannel(User* user, char* text, ...)
+void Channel::WriteChannel(User* user, const char* text, ...)
 {
        char textbuffer[MAXBUF];
        va_list argsPtr;
@@ -679,7 +727,7 @@ void Channel::WriteChannelWithServ(const char* ServName, const std::string &text
 
 /* write formatted text from a source user to all users on a channel except
  * for the sender (for privmsg etc) */
-void Channel::WriteAllExceptSender(User* user, bool serversource, char status, char* text, ...)
+void Channel::WriteAllExceptSender(User* user, bool serversource, char status, const char* text, ...)
 {
        char textbuffer[MAXBUF];
        va_list argsPtr;
@@ -694,7 +742,7 @@ void Channel::WriteAllExceptSender(User* user, bool serversource, char status, c
        this->WriteAllExceptSender(user, serversource, status, std::string(textbuffer));
 }
 
-void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, char* text, ...)
+void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const char* text, ...)
 {
        char textbuffer[MAXBUF];
        va_list argsPtr;
@@ -711,25 +759,9 @@ void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList
 
 void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList &except_list, const std::string &text)
 {
-       CUList *ulist;
+       CUList *ulist = this->GetUsers();
        char tb[MAXBUF];
 
-       switch (status)
-       {
-               case '@':
-                       ulist = this->GetOppedUsers();
-                       break;
-               case '%':
-                       ulist = this->GetHalfoppedUsers();
-                       break;
-               case '+':
-                       ulist = this->GetVoicedUsers();
-                       break;
-               default:
-                       ulist = this->GetUsers();
-                       break;
-       }
-
        snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),text.c_str());
        std::string out = tb;
 
@@ -737,6 +769,10 @@ void Channel::WriteAllExcept(User* user, bool serversource, char status, CUList
        {
                if ((IS_LOCAL(i->first)) && (except_list.find(i->first) == except_list.end()))
                {
+                       /* User doesnt have the status we're after */
+                       if (status && !strchr(this->GetAllPrefixChars(i->first), status))
+                               continue;
+
                        if (serversource)
                                i->first->WriteServ(text);
                        else
@@ -828,15 +864,25 @@ void Channel::UserList(User *user, CUList *ulist)
        char list[MAXBUF];
        size_t dlen, curlen;
        int MOD_RESULT = 0;
+       bool call_modules = true;
 
        if (!IS_LOCAL(user))
                return;
 
        FOREACH_RESULT(I_OnUserList,OnUserList(user, this, ulist));
        if (MOD_RESULT == 1)
-               return;
+               call_modules = false;
+
+       if (MOD_RESULT != -1)
+       {
+               if ((this->IsModeSet('s')) && (!this->HasUser(user)))
+               {
+                       user->WriteNumeric(401, "%s %s :No such nick/channel",user->nick, this->name);
+                       return;
+               }
+       }
 
-       dlen = curlen = snprintf(list,MAXBUF,"353 %s %c %s :", user->nick, this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=',  this->name);
+       dlen = curlen = snprintf(list,MAXBUF,"%s %c %s :", user->nick, this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=',  this->name);
 
        int numusers = 0;
        char* ptr = list + dlen;
@@ -863,14 +909,19 @@ void Channel::UserList(User *user, CUList *ulist)
                if (i->first->Visibility && !i->first->Visibility->VisibleTo(user))
                        continue;
 
-               size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", this->GetPrefixChar(i->first), i->second.c_str());
-               /* OnUserList can change this - reset it back to normal */
-               i->second = i->first->nick;
+               std::string prefixlist = this->GetPrefixChar(i->first);
+               std::string nick = i->first->nick;
 
-               curlen += ptrlen;
-               ptr += ptrlen;
-
-               numusers++;
+               if (call_modules)
+               {
+                       FOREACH_MOD(I_OnNamesListItem, OnNamesListItem(user, i->first, this, prefixlist, nick));
+       
+                       /* Nick was nuked, a module wants us to skip it */
+                       if (nick.empty())
+                               continue;
+               }
+               
+               size_t ptrlen = 0;
 
                if (curlen > (480-NICKMAX))
                {
@@ -878,21 +929,28 @@ void Channel::UserList(User *user, CUList *ulist)
                        user->WriteServ(std::string(list));
 
                        /* reset our lengths */
-                       dlen = curlen = snprintf(list,MAXBUF,"353 %s %c %s :", user->nick, this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name);
+                       dlen = curlen = snprintf(list,MAXBUF,"%s %c %s :", user->nick, this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name);
                        ptr = list + dlen;
 
                        ptrlen = 0;
                        numusers = 0;
                }
+
+               ptrlen = snprintf(ptr, MAXBUF, "%s%s ", prefixlist.c_str(), nick.c_str());
+
+               curlen += ptrlen;
+               ptr += ptrlen;
+
+               numusers++;
        }
 
        /* if whats left in the list isnt empty, send it */
        if (numusers)
        {
-               user->WriteServ(std::string(list));
+               user->WriteNumeric(353,std::string(list));
        }
 
-       user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, this->name);
+       user->WriteNumeric(366, "%s %s :End of /NAMES list.", user->nick, this->name);
 }
 
 long Channel::GetMaxBans()
@@ -948,6 +1006,7 @@ const char* Channel::GetPrefixChar(User *user)
        return pf;
 }
 
+
 const char* Channel::GetAllPrefixChars(User* user)
 {
        static char prefix[MAXBUF];