]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/mode.cpp
Nicer mode character loop
[user/henk/code/inspircd.git] / src / mode.cpp
index e1bfe56e2d5a810a04ddc2369d2d816dc5ae3bda..643228fe6ee2fff5073f2fed2cf6ecdedfe37b74 100644 (file)
@@ -58,18 +58,11 @@ char* ModeParser::GiveOps(userrec *user,char *dest,chanrec *chan,int status)
 {
        userrec *d;
        
-       if ((!user) || (!dest) || (!chan))
+       if ((!user) || (!dest) || (!chan) || (!*dest))
        {
                log(DEFAULT,"*** BUG *** GiveOps was given an invalid parameter");
                return NULL;
        }
-
-       if (!isnick(dest))
-       {
-               log(DEFAULT,"the target nickname given to GiveOps was invalid");
-               WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
-               return NULL;
-       }
        d = Find(dest);
        if (!d)
        {
@@ -121,18 +114,13 @@ char* ModeParser::GiveHops(userrec *user,char *dest,chanrec *chan,int status)
 {
        userrec *d;
        
-       if ((!user) || (!dest) || (!chan))
+       if ((!user) || (!dest) || (!chan) || (!*dest))
        {
                log(DEFAULT,"*** BUG *** GiveHops was given an invalid parameter");
                return NULL;
        }
 
        d = Find(dest);
-       if (!isnick(dest))
-       {
-               WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
-               return NULL;
-       }
        if (!d)
        {
                WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
@@ -177,18 +165,13 @@ char* ModeParser::GiveVoice(userrec *user,char *dest,chanrec *chan,int status)
 {
        userrec *d;
        
-       if ((!user) || (!dest) || (!chan))
+       if ((!user) || (!dest) || (!chan) || (!*dest))
        {
                log(DEFAULT,"*** BUG *** GiveVoice was given an invalid parameter");
                return NULL;
        }
 
        d = Find(dest);
-       if (!isnick(dest))
-       {
-               WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
-               return NULL;
-       }
        if (!d)
        {
                WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
@@ -233,19 +216,13 @@ char* ModeParser::TakeOps(userrec *user,char *dest,chanrec *chan,int status)
 {
        userrec *d;
        
-       if ((!user) || (!dest) || (!chan))
+       if ((!user) || (!dest) || (!chan) || (!*dest))
        {
                log(DEFAULT,"*** BUG *** TakeOps was given an invalid parameter");
                return NULL;
        }
 
        d = Find(dest);
-       if (!isnick(dest))
-       {
-               log(DEBUG,"TakeOps was given an invalid target nickname of %s",dest);
-               WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
-               return NULL;
-       }
        if (!d)
        {
                log(DEBUG,"TakeOps couldnt resolve the target nickname: %s",dest);
@@ -292,18 +269,13 @@ char* ModeParser::TakeHops(userrec *user,char *dest,chanrec *chan,int status)
 {
        userrec *d;
        
-       if ((!user) || (!dest) || (!chan))
+       if ((!user) || (!dest) || (!chan) || (!*dest))
        {
                log(DEFAULT,"*** BUG *** TakeHops was given an invalid parameter");
                return NULL;
        }
 
        d = Find(dest);
-       if (!isnick(dest))
-       {
-               WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
-               return NULL;
-       }
        if (!d)
        {
                WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
@@ -349,18 +321,13 @@ char* ModeParser::TakeVoice(userrec *user,char *dest,chanrec *chan,int status)
 {
        userrec *d;
        
-       if ((!user) || (!dest) || (!chan))
+       if ((!user) || (!dest) || (!chan) || (!*dest))
        {
                log(DEFAULT,"*** BUG *** TakeVoice was given an invalid parameter");
                return NULL;
        }
 
        d = Find(dest);
-       if (!isnick(dest))
-       {
-               WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
-               return NULL;
-       }
        if (!d)
        {
                WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
@@ -403,36 +370,37 @@ char* ModeParser::TakeVoice(userrec *user,char *dest,chanrec *chan,int status)
 
 char* ModeParser::AddBan(userrec *user,char *dest,chanrec *chan,int status)
 {
-       if ((!user) || (!dest) || (!chan)) {
+       BanItem b;
+       int toomanyexclamation = 0;
+       int toomanyat = 0;
+
+       if ((!user) || (!dest) || (!chan) || (!*dest))
+       {
                log(DEFAULT,"*** BUG *** AddBan was given an invalid parameter");
                return NULL;
        }
 
-       BanItem b;
-       if ((!user) || (!dest) || (!chan))
-               return NULL;
-       unsigned int l = strlen(dest);
-       if (strchr(dest,'!')==0)
-               return NULL;
-       if (strchr(dest,'@')==0)
-               return NULL;
-       for (unsigned int i = 0; i < l; i++)
-               if (dest[i] < 32)
-                       return NULL;
-       for (unsigned int i = 0; i < l; i++)
-               if (dest[i] > 126)
+       for (char* i = dest; *i; i++)
+       {
+               if ((*i < 32) || (*i > 126))
+               {
                        return NULL;
-       int c = 0;
-       for (unsigned int i = 0; i < l; i++)
-               if (dest[i] == '!')
-                       c++;
-       if (c>1)
-               return NULL;
-       c = 0;
-       for (unsigned int i = 0; i < l; i++)
-               if (dest[i] == '@')
-                       c++;
-       if (c>1)
+               }
+               else if (*i == '!')
+               {
+                       toomanyexclamation++;
+               }
+               else if (*i == '@')
+               {
+                       toomanyat++;
+               }
+       }
+
+       if (toomanyexclamation != 1 || toomanyat != 1)
+               /*
+                * this stops sillyness like n!u!u!u@h, though note that most
+                * ircds don't actually verify banmask validity. --w00t
+                */
                return NULL;
 
        long maxbans = GetMaxBans(chan->name);
@@ -468,7 +436,7 @@ char* ModeParser::AddBan(userrec *user,char *dest,chanrec *chan,int status)
 
 char* ModeParser::TakeBan(userrec *user,char *dest,chanrec *chan,int status)
 {
-       if ((!user) || (!dest) || (!chan)) {
+       if ((!user) || (!dest) || (!chan) || (!*dest)) {
                log(DEFAULT,"*** BUG *** TakeBan was given an invalid parameter");
                return 0;
        }
@@ -562,7 +530,7 @@ void ModeParser::ProcessModes(char **parameters,userrec* user,chanrec *chan,int
        }
 
        int MOD_RESULT = 0;
-       FOREACH_RESULT(I_OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
+       FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
        
        if (MOD_RESULT == ACR_DENY)
                return;
@@ -572,7 +540,8 @@ void ModeParser::ProcessModes(char **parameters,userrec* user,chanrec *chan,int
        strlcpy(modelist,parameters[1],MAXBUF); /* mode list, e.g. +oo-o *
                                                 * parameters[2] onwards are parameters for
                                                 * modes that require them :) */
-       strlcpy(outlist,"+",MAXBUF);
+       *outlist = '+';
+       *(outlist+1) = 0;
        mdir = 1;
 
        log(DEBUG,"process_modes: modelist: %s",modelist);
@@ -580,18 +549,16 @@ void ModeParser::ProcessModes(char **parameters,userrec* user,chanrec *chan,int
        std::string tidied = this->CompressModes(modelist,true);
        strlcpy(modelist,tidied.c_str(),MAXBUF);
 
-       int len = strlen(modelist);
+       int len = tidied.length();
        while (modelist[len-1] == ' ')
                modelist[--len] = '\0';
-       for (ptr = 0; ptr < len; ptr++)
+
+       for (char* modechar = modelist; *modechar; ptr++, modechar++)
        {
                r = NULL;
 
                {
-                       log(DEBUG,"process_modes: modechar: %c",modelist[ptr]);
-
-                       char modechar = modelist[ptr];
-                       switch (modelist[ptr])
+                       switch (*modechar)
                        {
                                case '-':
                                        if (mdir != 0)
@@ -761,7 +728,7 @@ void ModeParser::ProcessModes(char **parameters,userrec* user,chanrec *chan,int
                                                        break;
                                                previously_set_k = true;
                                                
-                                               if (!strcmp(chan->key,""))
+                                               if (!*chan->key)
                                                {
                                                        MOD_RESULT = 0;
                                                        FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'k', parameters[param], true, 1));
@@ -991,27 +958,27 @@ void ModeParser::ProcessModes(char **parameters,userrec* user,chanrec *chan,int
                                break;
                                
                                default:
-                                       log(DEBUG,"Preprocessing custom mode %c: modelist: %s",modechar,chan->custom_modes);
+                                       log(DEBUG,"Preprocessing custom mode %c: modelist: %s",*modechar,chan->custom_modes);
                                        string_list p;
                                        p.clear();
-                                       if (((!strchr(chan->custom_modes,modechar)) && (!mdir)) || ((strchr(chan->custom_modes,modechar)) && (mdir)))
+                                       if (((!strchr(chan->custom_modes,*modechar)) && (!mdir)) || ((strchr(chan->custom_modes,*modechar)) && (mdir)))
                                        {
-                                               if (!ModeIsListMode(modechar,MT_CHANNEL))
+                                               if (!ModeIsListMode(*modechar,MT_CHANNEL))
                                                {
-                                                       log(DEBUG,"Mode %c isnt set on %s but trying to remove!",modechar,chan->name);
+                                                       log(DEBUG,"Mode %c isnt set on %s but trying to remove!",*modechar,chan->name);
                                                        break;
                                                }
                                        }
-                                       if (ModeDefined(modechar,MT_CHANNEL))
+                                       if (ModeDefined(*modechar,MT_CHANNEL))
                                        {
                                                log(DEBUG,"A module has claimed this mode");
                                                if (param<pcnt)
                                                {
-                                                       if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
+                                                       if ((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir))
                                                        {
                                                                p.push_back(parameters[param]);
                                                        }
-                                                       if ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir))
+                                                       if ((ModeDefinedOff(*modechar,MT_CHANNEL)>0) && (!mdir))
                                                        {
                                                                p.push_back(parameters[param]);
                                                        }
@@ -1020,9 +987,9 @@ void ModeParser::ProcessModes(char **parameters,userrec* user,chanrec *chan,int
                                                if (param>=pcnt)
                                                {
                                                        // we're supposed to have a parameter, but none was given... so dont handle the mode.
-                                                       if (((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir))) 
+                                                       if (((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(*modechar,MT_CHANNEL)>0) && (!mdir)))       
                                                        {
-                                                               log(DEBUG,"Not enough parameters for module-mode %c",modechar);
+                                                               log(DEBUG,"Not enough parameters for module-mode %c",*modechar);
                                                                handled = true;
                                                                param++;
                                                        }
@@ -1036,19 +1003,19 @@ void ModeParser::ProcessModes(char **parameters,userrec* user,chanrec *chan,int
                                                std::string para = "";
                                                if (p.size())
                                                        para = p[0];
-                                               FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, modechar, para, mdir, pcnt));
+                                               FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, *modechar, para, mdir, pcnt));
                                                if (!MOD_RESULT)
                                                {
                                                        for (int i = 0; i <= MODCOUNT; i++)
                                                        {
                                                                if (!handled)
                                                                {
-                                                                       int t = modules[i]->OnExtendedMode(user,chan,modechar,MT_CHANNEL,mdir,p);
+                                                                       int t = modules[i]->OnExtendedMode(user,chan,*modechar,MT_CHANNEL,mdir,p);
                                                                        if (t != 0)
                                                                        {
                                                                                log(DEBUG,"OnExtendedMode returned nonzero for a module");
-                                                                               char app[] = {modechar, 0};
-                                                                               if (ModeIsListMode(modechar,MT_CHANNEL))
+                                                                               char app[] = {*modechar, 0};
+                                                                               if (ModeIsListMode(*modechar,MT_CHANNEL))
                                                                                {
                                                                                        if (t == -1)
                                                                                        {
@@ -1072,14 +1039,14 @@ void ModeParser::ProcessModes(char **parameters,userrec* user,chanrec *chan,int
                                                                                                {
                                                                                                        strlcat(outlist, app,MAXBUF);
                                                                                                }
-                                                                                               else if (!strchr(outlist,modechar))
+                                                                                               else if (!strchr(outlist,*modechar))
                                                                                                {
                                                                                                        strlcat(outlist, app,MAXBUF);
                                                                                                }
                                                                                        }
-                                                                                       chan->SetCustomMode(modechar,mdir);
+                                                                                       chan->SetCustomMode(*modechar,mdir);
                                                                                        // include parameters in output if mode has them
-                                                                                       if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
+                                                                                       if ((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir))
                                                                                        {
                                                                                                chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
                                                                                                strlcpy(outpars[pc++],parameters[param++],MAXBUF);
@@ -1094,7 +1061,7 @@ void ModeParser::ProcessModes(char **parameters,userrec* user,chanrec *chan,int
                                        }
                                        else
                                        {
-                                               WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,modechar);
+                                               WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,*modechar);
                                        }
                                break;