]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Fix SetModeParam to use std::string and handle edge cases.
authordanieldg <danieldg@e03df62e-2008-0410-955e-edbf42e46eb7>
Fri, 17 Apr 2009 13:54:53 +0000 (13:54 +0000)
committerdanieldg <danieldg@e03df62e-2008-0410-955e-edbf42e46eb7>
Fri, 17 Apr 2009 13:54:53 +0000 (13:54 +0000)
Previously, changing the vaule of a mode could require 3 calls to SetMode and SetModeParam.
This also fixes memory leaks caused by the strdup() not always being paired with a free().

git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@11307 e03df62e-2008-0410-955e-edbf42e46eb7

include/channels.h
src/channels.cpp
src/modes/cmode_k.cpp
src/modes/cmode_l.cpp
src/modules/m_joinflood.cpp
src/modules/m_kicknorejoin.cpp
src/modules/m_messageflood.cpp
src/modules/m_nickflood.cpp
src/modules/m_redirect.cpp

index afcdee41f08fe14822fb44016fe338792d0e597b..3bbcf067245eea1f0fb9b9bfec2193f2260bdd9d 100644 (file)
@@ -74,7 +74,7 @@ typedef CUList::const_iterator CUListConstIter;
 
 /** A list of custom modes parameters on a channel
  */
-typedef std::map<char,char*> CustomModeList;
+typedef std::map<char,std::string> CustomModeList;
 
 
 /** used to hold a channel and a users modes on that channel, e.g. +v, +h, +o
@@ -202,12 +202,12 @@ class CoreExport Channel : public Extensible
         */
        void SetMode(char mode,bool mode_on);
 
-       /** Sets or unsets the parameters for a custom mode in a channels info
+       /** Sets or unsets a custom mode in the channels info
         * @param mode The mode character to set or unset
-        * @param parameter The parameter string to associate with this mode character
-        * @param mode_on True if you want to set the mode or false if you want to remove it
+        * @param parameter The parameter string to associate with this mode character.
+        * If it is empty, the mode is unset; if it is nonempty, the mode is set.
         */
-       void SetModeParam(char mode,const char* parameter,bool mode_on);
+       void SetMode(char mode, std::string parameter);
 
        /** Returns true if a mode is set on a channel
          * @param mode The mode character you wish to query
index 19899d300e87821c6f144b77d353c5ac837da604..99b118d0dc90e1cb15c0d8e100fa2af2cd0c2803 100644 (file)
@@ -34,27 +34,22 @@ Channel::Channel(InspIRCd* Instance, const std::string &cname, time_t ts) : Serv
 void Channel::SetMode(char mode,bool mode_on)
 {
        modes[mode-65] = mode_on;
-       if (!mode_on)
-               this->SetModeParam(mode,"",false);
 }
 
-
-void Channel::SetModeParam(char mode,const char* parameter,bool mode_on)
+void Channel::SetMode(char mode, std::string parameter)
 {
        CustomModeList::iterator n = custom_mode_params.find(mode);
-
-       if (mode_on)
+       // always erase, even if changing, so that the map gets the new value
+       if (n != custom_mode_params.end())
+               custom_mode_params.erase(n);
+       if (parameter.empty())
        {
-               if (n == custom_mode_params.end())
-                       custom_mode_params[mode] = strdup(parameter);
+               modes[mode-65] = false;
        }
        else
        {
-               if (n != custom_mode_params.end())
-               {
-                       free(n->second);
-                       custom_mode_params.erase(n);
-               }
+               custom_mode_params[mode] = parameter;
+               modes[mode-65] = true;
        }
 }
 
index f2afb9ac025e10b56620615b89ac2b5dc33a1a8c..a7c491aa55a2441f5bc79b7717e3fc40ac688ba1 100644 (file)
@@ -94,20 +94,16 @@ ModeAction ModeChannelKey::OnModeChange(User* source, User*, Channel* channel, s
        if (parameter.rfind(' ') != std::string::npos)
                return MODEACTION_DENY;
 
-       /* must first unset if we are changing the key, otherwise it will be ignored */
-       if (exists && adding)
-               channel->SetMode('k', false);
-
-       /* must run setmode always, to process the change */
-       channel->SetMode('k', adding);
-
        if (adding)
        {
                std::string ckey;
                ckey.assign(parameter, 0, 32);
                parameter = ckey;
-               /* running this does not run setmode, despite the third parameter */
-               channel->SetModeParam('k', parameter.c_str(), true);
+               channel->SetMode('k', parameter);
+       }
+       else
+       {
+               channel->SetMode('k', "");
        }
        return MODEACTION_ALLOW;
 }
index e87a524b2d9b18d34fda764881f5851375b89d98..3d37fc900e645a881b81de770f15eff9b2b61014 100644 (file)
@@ -54,8 +54,7 @@ ModeAction ModeChannelLimit::OnModeChange(User*, User*, Channel* channel, std::s
                parameter = ConvToStr(limit);
 
                /* Set new limit */
-               channel->SetModeParam('l', parameter.c_str(), true);
-               channel->modes[CM_LIMIT] = 1;
+               channel->SetMode('l', parameter);
 
                return MODEACTION_ALLOW;
        }
@@ -71,9 +70,7 @@ ModeAction ModeChannelLimit::OnModeChange(User*, User*, Channel* channel, std::s
                }
 
                /* Removing old limit, no checks here */
-               channel->SetModeParam('l', "", false);
-               channel->modes[CM_LIMIT] = 0;
-
+               channel->SetMode('l', "");
                return MODEACTION_ALLOW;
        }
 }
index ae173bdb33d88140449a588d73604955b389d43e..7e4a05d2bb77da0c3ff0e7fcb7d6e237639a2f5e 100644 (file)
@@ -144,8 +144,7 @@ class JoinFlood : public ModeHandler
                                                parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
                                                joinfloodsettings *f = new joinfloodsettings(ServerInstance, nsecs, njoins);
                                                channel->Extend("joinflood", f);
-                                               channel->SetMode('j', true);
-                                               channel->SetModeParam('j', parameter.c_str(), true);
+                                               channel->SetMode('j', parameter);
                                                return MODEACTION_ALLOW;
                                        }
                                        else
@@ -168,8 +167,7 @@ class JoinFlood : public ModeHandler
                                                                f = new joinfloodsettings(ServerInstance, nsecs, njoins);
                                                                channel->Shrink("joinflood");
                                                                channel->Extend("joinflood", f);
-                                                               channel->SetModeParam('j', cur_param.c_str(), false);
-                                                               channel->SetModeParam('j', parameter.c_str(), true);
+                                                               channel->SetMode('j', parameter);
                                                                return MODEACTION_ALLOW;
                                                        }
                                                        else
@@ -194,7 +192,7 @@ class JoinFlood : public ModeHandler
                                channel->GetExt("joinflood", f);
                                delete f;
                                channel->Shrink("joinflood");
-                               channel->SetMode('j', false);
+                               channel->SetMode('j', "");
                                return MODEACTION_ALLOW;
                        }
                }
index 527022c11d511b327eeb80745cc7278814df1ace..c5abc80c2c0573a60832d79e9265ab2f85035411 100644 (file)
@@ -65,7 +65,7 @@ class KickRejoin : public ModeHandler
                        }
                        else
                        {
-                               channel->SetMode('J', false);
+                               channel->SetMode('J', "");
                                return MODEACTION_ALLOW;
                        }
                }
@@ -74,8 +74,7 @@ class KickRejoin : public ModeHandler
                        if (!channel->IsModeSet('J'))
                        {
                                parameter = ConvToStr(atoi(parameter.c_str()));
-                               channel->SetModeParam('J', parameter.c_str(), adding);
-                               channel->SetMode('J', adding);
+                               channel->SetMode('J', parameter);
                                return MODEACTION_ALLOW;
                        }
                        else
@@ -90,11 +89,9 @@ class KickRejoin : public ModeHandler
                                {
                                        // new mode param, replace old with new
                                        parameter = ConvToStr(atoi(parameter.c_str()));
-                                       cur_param = ConvToStr(atoi(cur_param.c_str()));
                                        if (parameter != "0")
                                        {
-                                               channel->SetModeParam('J', cur_param.c_str(), false);
-                                               channel->SetModeParam('J', parameter.c_str(), adding);
+                                               channel->SetMode('J', parameter);
                                                return MODEACTION_ALLOW;
                                        }
                                        else
index ed40fca5b5b1f372ffb0e8ee8e1385df542eaceb..f2082da3338c27a23a7c12ecf729aaece1fde295 100644 (file)
@@ -143,8 +143,7 @@ class MsgFlood : public ModeHandler
                                                parameter = std::string(ban ? "*" : "") + ConvToStr(nlines) + ":" +ConvToStr(nsecs);
                                                floodsettings *fs = new floodsettings(ServerInstance,ban,nsecs,nlines);
                                                channel->Extend("flood",fs);
-                                               channel->SetMode('f', true);
-                                               channel->SetModeParam('f', parameter.c_str(), true);
+                                               channel->SetMode('f', parameter);
                                                return MODEACTION_ALLOW;
                                        }
                                        else
@@ -164,8 +163,7 @@ class MsgFlood : public ModeHandler
                                                                floodsettings *fs = new floodsettings(ServerInstance,ban,nsecs,nlines);
                                                                channel->Shrink("flood");
                                                                channel->Extend("flood",fs);
-                                                               channel->SetModeParam('f', cur_param.c_str(), false);
-                                                               channel->SetModeParam('f', parameter.c_str(), true);
+                                                               channel->SetMode('f', parameter);
                                                                return MODEACTION_ALLOW;
                                                        }
                                                        else
@@ -189,7 +187,7 @@ class MsgFlood : public ModeHandler
                        {
                                delete f;
                                channel->Shrink("flood");
-                               channel->SetMode('f', false);
+                               channel->SetMode('f', "");
                                return MODEACTION_ALLOW;
                        }
                }
index f916946254138fac379d5e0ba0d27d5679c4df5b..0a8e9a31d2ecd9cb21640f5e7cc263422e77db61 100644 (file)
@@ -144,8 +144,7 @@ class NickFlood : public ModeHandler
                                                parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
                                                nickfloodsettings *f = new nickfloodsettings(ServerInstance, nsecs, nnicks);
                                                channel->Extend("nickflood", f);
-                                               channel->SetMode('F', true);
-                                               channel->SetModeParam('F', parameter.c_str(), true);
+                                               channel->SetMode('F', parameter);
                                                return MODEACTION_ALLOW;
                                        }
                                        else
@@ -168,8 +167,7 @@ class NickFlood : public ModeHandler
                                                                f = new nickfloodsettings(ServerInstance, nsecs, nnicks);
                                                                channel->Shrink("nickflood");
                                                                channel->Extend("nickflood", f);
-                                                               channel->SetModeParam('F', cur_param.c_str(), false);
-                                                               channel->SetModeParam('F', parameter.c_str(), true);
+                                                               channel->SetMode('F', parameter);
                                                                return MODEACTION_ALLOW;
                                                        }
                                                        else
@@ -194,7 +192,7 @@ class NickFlood : public ModeHandler
                                channel->GetExt("nickflood", f);
                                delete f;
                                channel->Shrink("nickflood");
-                               channel->SetMode('F', false);
+                               channel->SetMode('F', "");
                                return MODEACTION_ALLOW;
                        }
                }
index 9aa0dcf8b5f1432a96c6db6c9b8ce62570055b5d..00c191d26a74c30cebe3143662d4f5696160d63c 100644 (file)
@@ -68,15 +68,14 @@ class Redirect : public ModeHandler
                         * We used to do some checking for circular +L here, but there is no real need for this any more especially as we
                         * now catch +L looping in PreJoin. Remove it, since O(n) logic makes me sad, and we catch it anyway. :) -- w00t
                         */
-                       channel->SetMode('L', true);
-                       channel->SetModeParam('L', parameter.c_str(), true);
+                       channel->SetMode('L', parameter);
                        return MODEACTION_ALLOW;
                }
                else
                {
                        if (channel->IsModeSet('L'))
                        {
-                               channel->SetMode('L', false);
+                               channel->SetMode('L', "");
                                return MODEACTION_ALLOW;
                        }
                }