]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_namedmodes.cpp
Convert the ISO 8859-2 nationalchars files to codepage configs.
[user/henk/code/inspircd.git] / src / modules / m_namedmodes.cpp
index 6f6dda491c474c9518feede4ea3bfd2dd6e46790..f25cc219ca18a455bbea93f0ba1dbdcd8a6f9947 100644 (file)
-/*       +------------------------------------+
- *       | Inspire Internet Relay Chat Daemon |
- *       +------------------------------------+
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
  *
- *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
- * See: http://wiki.inspircd.org/Credits
+ *   Copyright (C) 2017 B00mX0r <b00mx0r@aureus.pw>
+ *   Copyright (C) 2013-2016 Attila Molnar <attilamolnar@hush.com>
+ *   Copyright (C) 2013, 2017-2019 Sadie Powell <sadie@witchery.services>
+ *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
+ *   Copyright (C) 2010 Craig Edwards <brain@inspircd.org>
+ *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
  *
- * This program is free but copyrighted software; see
- *            the file COPYING for details.
+ * This file is part of InspIRCd.  InspIRCd is free software: you can
+ * redistribute it and/or modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation, version 2.
  *
- * ---------------------------------------------------
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+
 #include "inspircd.h"
 
-class ModuleNamedModes : public Module
+enum
 {
- public:
-       ModuleNamedModes()
-       {
-               Implementation eventlist[] = { I_OnPreMode, I_On005Numeric };
-               ServerInstance->Modules->Attach(eventlist, this, 2);
-       }
+       // InspIRCd-specific.
+       RPL_ENDOFPROPLIST = 960,
+       RPL_PROPLIST = 961
+};
+
+static void DisplayList(LocalUser* user, Channel* channel)
+{
+       Numeric::ParamBuilder<1> numeric(user, RPL_PROPLIST);
+       numeric.AddStatic(channel->name);
 
-       Version GetVersion()
+       const ModeParser::ModeHandlerMap& mhs = ServerInstance->Modes->GetModes(MODETYPE_CHANNEL);
+       for (ModeParser::ModeHandlerMap::const_iterator i = mhs.begin(); i != mhs.end(); ++i)
        {
-               return Version("Provides the ability to manipulate modes via long names.",VF_VENDOR);
+               ModeHandler* mh = i->second;
+               if (!channel->IsModeSet(mh))
+                       continue;
+               numeric.Add("+" + mh->name);
+               ParamModeBase* pm = mh->IsParameterMode();
+               if (pm)
+               {
+                       if ((pm->IsParameterSecret()) && (!channel->HasUser(user)) && (!user->HasPrivPermission("channels/auspex")))
+                               numeric.Add("<" + mh->name + ">");
+                       else
+                               numeric.Add(channel->GetModeParameter(mh));
+               }
        }
+       numeric.Flush();
+       user->WriteNumeric(RPL_ENDOFPROPLIST, channel->name, "End of mode list");
+}
 
-       void Prioritize()
+class CommandProp : public SplitCommand
+{
+ public:
+       CommandProp(Module* parent)
+               : SplitCommand(parent, "PROP", 1)
        {
-               ServerInstance->Modules->SetPriority(this, I_OnPreMode, PRIORITY_FIRST);
+               syntax = "<channel> [[(+|-)]<mode> [<value>]]";
        }
 
-       void On005Numeric(std::string& line)
+       CmdResult HandleLocal(LocalUser* src, const Params& parameters) CXX11_OVERRIDE
        {
-               std::string::size_type pos = line.find(" CHANMODES=");
-               if (pos != std::string::npos)
+               Channel* const chan = ServerInstance->FindChan(parameters[0]);
+               if (!chan)
                {
-                       pos += 11;
-                       while (line[pos] > 'A' && line[pos] < 'Z')
-                               pos++;
-                       line.insert(pos, 1, 'Z');
+                       src->WriteNumeric(Numerics::NoSuchChannel(parameters[0]));
+                       return CMD_FAILURE;
                }
-       }
 
-       void DisplayList(User* user, Channel* channel)
-       {
-               std::stringstream items;
-               for(char letter = 'A'; letter <= 'z'; letter++)
+               if (parameters.size() == 1)
                {
-                       ModeHandler* mh = ServerInstance->Modes->FindMode(letter, MODETYPE_CHANNEL);
-                       if (!mh || mh->IsListMode())
-                               continue;
-                       if (!channel->IsModeSet(letter))
+                       DisplayList(src, chan);
+                       return CMD_SUCCESS;
+               }
+               unsigned int i = 1;
+               Modes::ChangeList modes;
+               while (i < parameters.size())
+               {
+                       std::string prop = parameters[i++];
+                       if (prop.empty())
                                continue;
-                       std::string item = mh->name;
-                       if (mh->GetNumParams(true))
-                               item += "=" + channel->GetModeParameter(letter);
-                       items << item << " ";
+                       bool plus = prop[0] != '-';
+                       if (prop[0] == '+' || prop[0] == '-')
+                               prop.erase(prop.begin());
+
+                       ModeHandler* mh = ServerInstance->Modes->FindMode(prop, MODETYPE_CHANNEL);
+                       if (mh)
+                       {
+                               if (mh->NeedsParam(plus))
+                               {
+                                       if (i != parameters.size())
+                                               modes.push(mh, plus, parameters[i++]);
+                               }
+                               else
+                                       modes.push(mh, plus);
+                       }
                }
-               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());
+               ServerInstance->Modes->ProcessSingle(src, chan, NULL, modes, ModeParser::MODE_CHECKACCESS);
+               return CMD_SUCCESS;
+       }
+};
+
+class DummyZ : public ModeHandler
+{
+ public:
+       DummyZ(Module* parent) : ModeHandler(parent, "namebase", 'Z', PARAM_ALWAYS, MODETYPE_CHANNEL)
+       {
+               list = true;
+       }
+
+       // Handle /MODE #chan Z
+       void DisplayList(User* user, Channel* chan) CXX11_OVERRIDE
+       {
+               LocalUser* luser = IS_LOCAL(user);
+               if (luser)
+                       ::DisplayList(luser, chan);
        }
+};
 
-       ModResult OnPreMode(User* source, User* dest, Channel* channel, const std::vector<std::string>& parameters)
+class ModuleNamedModes : public Module
+{
+       CommandProp cmd;
+       DummyZ dummyZ;
+ public:
+       ModuleNamedModes() : cmd(this), dummyZ(this)
+       {
+       }
+
+       Version GetVersion() CXX11_OVERRIDE
+       {
+               return Version("Provides support for adding and removing modes via their long names.", VF_VENDOR);
+       }
+
+       void Prioritize() CXX11_OVERRIDE
+       {
+               ServerInstance->Modules->SetPriority(this, I_OnPreMode, PRIORITY_FIRST);
+       }
+
+       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->NeedsParam(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;
        }
 };