]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_namedmodes.cpp
Merge insp20
[user/henk/code/inspircd.git] / src / modules / m_namedmodes.cpp
index 46710946b67a7adde9193e01aa4a14733a8d93d3..617ee43b368f5a3f6f48e588e4ab15ea5f227b35 100644 (file)
  */
 
 
-/* $ModDesc: Provides the ability to manipulate modes via long names. */
-
 #include "inspircd.h"
 
 static void DisplayList(User* user, Channel* channel)
 {
        std::stringstream items;
-       for(char letter = 'A'; letter <= 'z'; letter++)
+       const ModeParser::ModeHandlerMap& mhs = ServerInstance->Modes->GetModes(MODETYPE_CHANNEL);
+       for (ModeParser::ModeHandlerMap::const_iterator i = mhs.begin(); i != mhs.end(); ++i)
        {
-               ModeHandler* mh = ServerInstance->Modes->FindMode(letter, MODETYPE_CHANNEL);
-               if (!mh || mh->IsListMode())
-                       continue;
-               if (!channel->IsModeSet(letter))
+               ModeHandler* mh = i->second;
+               if (!channel->IsModeSet(mh))
                        continue;
                items << " +" << mh->name;
                if (mh->GetNumParams(true))
                {
-                       if ((letter == 'k') && (!channel->HasUser(user)) && (!user->HasPrivPermission("channels/auspex")))
+                       if ((mh->name == "key") && (!channel->HasUser(user)) && (!user->HasPrivPermission("channels/auspex")))
                                items << " <key>";
                        else
-                               items << " " << channel->GetModeParameter(letter);
+                               items << " " << channel->GetModeParameter(mh);
                }
        }
-       char pfx[MAXBUF];
-       snprintf(pfx, MAXBUF, ":%s 961 %s %s", ServerInstance->Config->ServerName.c_str(), user->nick.c_str(), channel->name.c_str());
-       user->SendText(std::string(pfx), items);
-       user->WriteNumeric(960, "%s %s :End of mode list", user->nick.c_str(), channel->name.c_str());
+       const std::string line = ":" + ServerInstance->Config->ServerName + " 961 " + user->nick + " " + channel->name;
+       user->SendText(line, items);
+       user->WriteNumeric(960, "%s :End of mode list", channel->name.c_str());
 }
 
 class CommandProp : public Command
@@ -56,17 +52,20 @@ class CommandProp : public Command
 
        CmdResult Handle(const std::vector<std::string> &parameters, User *src)
        {
+               Channel* const chan = ServerInstance->FindChan(parameters[0]);
+               if (!chan)
+               {
+                       src->WriteNumeric(ERR_NOSUCHNICK, "%s :No such nick/channel", parameters[0].c_str());
+                       return CMD_FAILURE;
+               }
+
                if (parameters.size() == 1)
                {
-                       Channel* chan = ServerInstance->FindChan(parameters[0]);
-                       if (chan)
-                               DisplayList(src, chan);
+                       DisplayList(src, chan);
                        return CMD_SUCCESS;
                }
                unsigned int i = 1;
-               std::vector<std::string> modes;
-               modes.push_back(parameters[0]);
-               modes.push_back("");
+               Modes::ChangeList modes;
                while (i < parameters.size())
                {
                        std::string prop = parameters[i++];
@@ -76,21 +75,19 @@ class CommandProp : public Command
                        if (prop[0] == '+' || prop[0] == '-')
                                prop.erase(prop.begin());
 
-                       for(char letter = 'A'; letter <= 'z'; letter++)
+                       ModeHandler* mh = ServerInstance->Modes->FindMode(prop, MODETYPE_CHANNEL);
+                       if (mh)
                        {
-                               ModeHandler* mh = ServerInstance->Modes->FindMode(letter, MODETYPE_CHANNEL);
-                               if (mh && mh->name == prop)
+                               if (mh->GetNumParams(plus))
                                {
-                                       modes[1].append((plus ? "+" : "-") + std::string(1, letter));
-                                       if (mh->GetNumParams(plus))
-                                       {
-                                               if (i != parameters.size())
-                                                       modes.push_back(parameters[i++]);
-                                       }
+                                       if (i != parameters.size())
+                                               modes.push(mh, plus, parameters[i++]);
                                }
+                               else
+                                       modes.push(mh, plus);
                        }
                }
-               ServerInstance->SendGlobalMode(modes, src);
+               ServerInstance->Modes->ProcessSingle(src, chan, NULL, modes, ModeParser::MODE_CHECKACCESS);
                return CMD_SUCCESS;
        }
 };
@@ -102,6 +99,12 @@ class DummyZ : public ModeHandler
        {
                list = true;
        }
+
+       // Handle /MODE #chan Z
+       void DisplayList(User* user, Channel* chan)
+       {
+               ::DisplayList(user, chan);
+       }
 };
 
 class ModuleNamedModes : public Module
@@ -113,16 +116,7 @@ class ModuleNamedModes : public Module
        {
        }
 
-       void init()
-       {
-               ServerInstance->Modules->AddService(cmd);
-               ServerInstance->Modules->AddService(dummyZ);
-
-               Implementation eventlist[] = { I_OnPreMode };
-               ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
-       }
-
-       Version GetVersion()
+       Version GetVersion() CXX11_OVERRIDE
        {
                return Version("Provides the ability to manipulate modes via long names.",VF_VENDOR);
        }
@@ -132,80 +126,59 @@ class ModuleNamedModes : public Module
                ServerInstance->Modules->SetPriority(this, I_OnPreMode, PRIORITY_FIRST);
        }
 
-       ModResult OnPreMode(User* source, User* dest, Channel* channel, const std::vector<std::string>& parameters)
+       ModResult OnPreMode(User* source, User* dest, Channel* channel, Modes::ChangeList& modes) CXX11_OVERRIDE
        {
                if (!channel)
                        return MOD_RES_PASSTHRU;
-               if (parameters[1].find('Z') == std::string::npos)
-                       return MOD_RES_PASSTHRU;
-               if (parameters.size() <= 2)
-               {
-                       DisplayList(source, channel);
-                       return MOD_RES_DENY;
-               }
-
-               std::vector<std::string> newparms;
-               newparms.push_back(parameters[0]);
-               newparms.push_back(parameters[1]);
 
-               std::string modelist = newparms[1];
-               bool adding = true;
-               unsigned int param_at = 2;
-               for(unsigned int i = 0; i < modelist.length(); i++)
+               Modes::ChangeList::List& list = modes.getlist();
+               for (Modes::ChangeList::List::iterator i = list.begin(); i != list.end(); )
                {
-                       unsigned char modechar = modelist[i];
-                       if (modechar == '+' || modechar == '-')
-                       {
-                               adding = (modechar == '+');
-                               continue;
-                       }
-                       ModeHandler *mh = ServerInstance->Modes->FindMode(modechar, MODETYPE_CHANNEL);
-                       if (modechar == 'Z')
+                       Modes::Change& curr = *i;
+                       // Replace all namebase (dummyZ) modes being changed with the actual
+                       // mode handler and parameter. The parameter format of the namebase mode is
+                       // <modename>[=<parameter>].
+                       if (curr.mh == &dummyZ)
                        {
-                               modechar = 0;
-                               std::string name, value;
-                               if (param_at < parameters.size())
-                                       name = parameters[param_at++];
+                               std::string name = curr.param;
+                               std::string value;
                                std::string::size_type eq = name.find('=');
                                if (eq != std::string::npos)
                                {
-                                       value = name.substr(eq + 1);
-                                       name = name.substr(0, eq);
+                                       value.assign(name, eq + 1, std::string::npos);
+                                       name.erase(eq);
+                               }
+
+                               ModeHandler* mh = ServerInstance->Modes->FindMode(name, MODETYPE_CHANNEL);
+                               if (!mh)
+                               {
+                                       // Mode handler not found
+                                       i = list.erase(i);
+                                       continue;
                                }
-                               for(char letter = 'A'; modechar == 0 && letter <= 'z'; letter++)
+
+                               curr.param.clear();
+                               if (mh->GetNumParams(curr.adding))
                                {
-                                       mh = ServerInstance->Modes->FindMode(letter, MODETYPE_CHANNEL);
-                                       if (mh && mh->name == name)
+                                       if (value.empty())
                                        {
-                                               if (mh->GetNumParams(adding))
-                                               {
-                                                       if (!value.empty())
-                                                       {
-                                                               newparms.push_back(value);
-                                                               modechar = letter;
-                                                               break;
-                                                       }
-                                               }
-                                               else
-                                               {
-                                                       modechar = letter;
-                                                       break;
-                                               }
+                                               // Mode needs a parameter but there wasn't one
+                                               i = list.erase(i);
+                                               continue;
                                        }
+
+                                       // Change parameter to the text after the '='
+                                       curr.param = value;
                                }
-                               if (modechar)
-                                       modelist[i] = modechar;
-                               else
-                                       modelist.erase(i--, 1);
-                       }
-                       else if (mh && mh->GetNumParams(adding) && param_at < parameters.size())
-                       {
-                               newparms.push_back(parameters[param_at++]);
+
+                               // Put the actual ModeHandler in place of the namebase handler
+                               curr.mh = mh;
                        }
+
+                       ++i;
                }
-               newparms[1] = modelist;
-               ServerInstance->Modes->Process(newparms, source, false);
-               return MOD_RES_DENY;
+
+               return MOD_RES_PASSTHRU;
        }
 };