]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Add functor that does strict weak ordering based on national_case_insensitive_map
authorAttila Molnar <attilamolnar@hush.com>
Thu, 19 Dec 2013 16:30:22 +0000 (17:30 +0100)
committerAttila Molnar <attilamolnar@hush.com>
Sat, 4 Jan 2014 18:38:20 +0000 (19:38 +0100)
include/hashcomp.h
src/hashcomp.cpp
src/modules/m_alias.cpp
src/modules/m_helpop.cpp
src/modules/m_restrictchans.cpp

index 0bf306267e43fcbc1e63f29dc71a39c3edae7f97..de556f39316cf93216577eed69109bf50a3a9f92 100644 (file)
@@ -114,6 +114,11 @@ namespace irc
                size_t CoreExport operator()(const std::string &s) const;
        };
 
                size_t CoreExport operator()(const std::string &s) const;
        };
 
+       struct insensitive_swo
+       {
+               bool CoreExport operator()(const std::string& a, const std::string& b) const;
+       };
+
        /** The irc_char_traits class is used for RFC-style comparison of strings.
         * This class is used to implement irc::string, a case-insensitive, RFC-
         * comparing string class.
        /** The irc_char_traits class is used for RFC-style comparison of strings.
         * This class is used to implement irc::string, a case-insensitive, RFC-
         * comparing string class.
index 4eb416406426331923693a5fff99072b73b40fb6..f1d0f0678c739a27afb6c16f86fce149968750c7 100644 (file)
@@ -170,6 +170,25 @@ bool irc::StrHashComp::operator()(const std::string& s1, const std::string& s2)
        return (national_case_insensitive_map[*n1] == national_case_insensitive_map[*n2]);
 }
 
        return (national_case_insensitive_map[*n1] == national_case_insensitive_map[*n2]);
 }
 
+bool irc::insensitive_swo::operator()(const std::string& a, const std::string& b) const
+{
+       const unsigned char* charmap = national_case_insensitive_map;
+       std::string::size_type asize = a.size();
+       std::string::size_type bsize = b.size();
+       std::string::size_type maxsize = std::min(asize, bsize);
+
+       for (std::string::size_type i = 0; i < maxsize; i++)
+       {
+               unsigned char A = charmap[(unsigned char)a[i]];
+               unsigned char B = charmap[(unsigned char)b[i]];
+               if (A > B)
+                       return false;
+               else if (A < B)
+                       return true;
+       }
+       return (asize < bsize);
+}
+
 size_t irc::insensitive::operator()(const std::string &s) const
 {
        /* XXX: NO DATA COPIES! :)
 size_t irc::insensitive::operator()(const std::string &s) const
 {
        /* XXX: NO DATA COPIES! :)
index b0fda70bbdbe16b23f1b0964b0ad64c41ecf2b26..78628cf067350bedc8af11db5a80f13eaf45cac2 100644 (file)
@@ -28,7 +28,7 @@ class Alias
 {
  public:
        /** The text of the alias command */
 {
  public:
        /** The text of the alias command */
-       irc::string AliasedCommand;
+       std::string AliasedCommand;
 
        /** Text to replace with */
        std::string ReplaceFormat;
 
        /** Text to replace with */
        std::string ReplaceFormat;
@@ -63,7 +63,9 @@ class ModuleAlias : public Module
         * We can, however, use a fancy invention: the multimap. Maps a key to one or more values.
         *              -- w00t
      */
         * We can, however, use a fancy invention: the multimap. Maps a key to one or more values.
         *              -- w00t
      */
-       std::multimap<irc::string, Alias> Aliases;
+       typedef std::multimap<std::string, Alias, irc::insensitive_swo> AliasMap;
+
+       AliasMap Aliases;
 
        /* whether or not +B users are allowed to use fantasy commands */
        bool AllowBots;
 
        /* whether or not +B users are allowed to use fantasy commands */
        bool AllowBots;
@@ -83,8 +85,8 @@ class ModuleAlias : public Module
                {
                        ConfigTag* tag = i->second;
                        Alias a;
                {
                        ConfigTag* tag = i->second;
                        Alias a;
-                       std::string aliastext = tag->getString("text");
-                       a.AliasedCommand = aliastext.c_str();
+                       a.AliasedCommand = tag->getString("text");
+                       std::transform(a.AliasedCommand.begin(), a.AliasedCommand.end(), a.AliasedCommand.begin(), ::toupper);
                        tag->readString("replace", a.ReplaceFormat, true);
                        a.RequiredNick = tag->getString("requires");
                        a.ULineOnly = tag->getBool("uline");
                        tag->readString("replace", a.ReplaceFormat, true);
                        a.RequiredNick = tag->getString("requires");
                        a.ULineOnly = tag->getBool("uline");
@@ -134,7 +136,7 @@ class ModuleAlias : public Module
 
        ModResult OnPreCommand(std::string &command, std::vector<std::string> &parameters, LocalUser *user, bool validated, const std::string &original_line) CXX11_OVERRIDE
        {
 
        ModResult OnPreCommand(std::string &command, std::vector<std::string> &parameters, LocalUser *user, bool validated, const std::string &original_line) CXX11_OVERRIDE
        {
-               std::multimap<irc::string, Alias>::iterator i, upperbound;
+               AliasMap::iterator i, upperbound;
 
                /* If theyre not registered yet, we dont want
                 * to know.
 
                /* If theyre not registered yet, we dont want
                 * to know.
@@ -143,13 +145,12 @@ class ModuleAlias : public Module
                        return MOD_RES_PASSTHRU;
 
                /* We dont have any commands looking like this? Stop processing. */
                        return MOD_RES_PASSTHRU;
 
                /* We dont have any commands looking like this? Stop processing. */
-               i = Aliases.find(command.c_str());
+               i = Aliases.find(command);
                if (i == Aliases.end())
                        return MOD_RES_PASSTHRU;
                /* Avoid iterating on to different aliases if no patterns match. */
                if (i == Aliases.end())
                        return MOD_RES_PASSTHRU;
                /* Avoid iterating on to different aliases if no patterns match. */
-               upperbound = Aliases.upper_bound(command.c_str());
+               upperbound = Aliases.upper_bound(command);
 
 
-               irc::string c = command.c_str();
                /* The parameters for the command in their original form, with the command stripped off */
                std::string compare = original_line.substr(command.length());
                while (*(compare.c_str()) == ' ')
                /* The parameters for the command in their original form, with the command stripped off */
                std::string compare = original_line.substr(command.length());
                while (*(compare.c_str()) == ' ')
@@ -197,33 +198,32 @@ class ModuleAlias : public Module
                // text is like "!moo cows bite me", we want "!moo" first
                irc::spacesepstream ss(text);
                ss.GetToken(scommand);
                // text is like "!moo cows bite me", we want "!moo" first
                irc::spacesepstream ss(text);
                ss.GetToken(scommand);
-               irc::string fcommand = scommand.c_str();
 
 
-               if (fcommand.empty())
+               if (scommand.empty())
                {
                        return; // wtfbbq
                }
 
                // we don't want to touch non-fantasy stuff
                {
                        return; // wtfbbq
                }
 
                // we don't want to touch non-fantasy stuff
-               if (*fcommand.c_str() != fprefix)
+               if (*scommand.c_str() != fprefix)
                {
                        return;
                }
 
                // nor do we give a shit about the prefix
                {
                        return;
                }
 
                // nor do we give a shit about the prefix
-               fcommand.erase(fcommand.begin());
+               scommand.erase(scommand.begin());
 
 
-               std::multimap<irc::string, Alias>::iterator i = Aliases.find(fcommand);
+               AliasMap::iterator i = Aliases.find(scommand);
 
                if (i == Aliases.end())
                        return;
 
                /* Avoid iterating on to other aliases if no patterns match */
 
                if (i == Aliases.end())
                        return;
 
                /* Avoid iterating on to other aliases if no patterns match */
-               std::multimap<irc::string, Alias>::iterator upperbound = Aliases.upper_bound(fcommand);
+               AliasMap::iterator upperbound = Aliases.upper_bound(scommand);
 
 
                /* The parameters for the command in their original form, with the command stripped off */
 
 
                /* The parameters for the command in their original form, with the command stripped off */
-               std::string compare = text.substr(fcommand.length() + 1);
+               std::string compare = text.substr(scommand.length() + 1);
                while (*(compare.c_str()) == ' ')
                        compare.erase(compare.begin());
 
                while (*(compare.c_str()) == ' ')
                        compare.erase(compare.begin());
 
@@ -276,7 +276,7 @@ class ModuleAlias : public Module
                {
                        if (!ServerInstance->ULine(u->server))
                        {
                {
                        if (!ServerInstance->ULine(u->server))
                        {
-                               ServerInstance->SNO->WriteToSnoMask('a', "NOTICE -- Service "+a->RequiredNick+" required by alias "+std::string(a->AliasedCommand.c_str())+" is not on a u-lined server, possibly underhanded antics detected!");
+                               ServerInstance->SNO->WriteToSnoMask('a', "NOTICE -- Service "+a->RequiredNick+" required by alias "+a->AliasedCommand+" is not on a u-lined server, possibly underhanded antics detected!");
                                user->WriteNumeric(ERR_NOSUCHNICK, a->RequiredNick + " :is an imposter! Please inform an IRC operator as soon as possible.");
                                return 1;
                        }
                                user->WriteNumeric(ERR_NOSUCHNICK, a->RequiredNick + " :is an imposter! Please inform an IRC operator as soon as possible.");
                                return 1;
                        }
index d07898c90801c0a199c9e6bfd24f8d9ebe0ee8a9..64bdc2400c6410a99d665c24d545c7b1de5f9cb6 100644 (file)
@@ -23,7 +23,8 @@
 
 #include "inspircd.h"
 
 
 #include "inspircd.h"
 
-static std::map<irc::string, std::string> helpop_map;
+typedef std::map<std::string, std::string, irc::insensitive_swo> HelpopMap;
+static HelpopMap helpop_map;
 
 /** Handles user mode +h
  */
 
 /** Handles user mode +h
  */
@@ -40,23 +41,24 @@ class Helpop : public SimpleUserModeHandler
  */
 class CommandHelpop : public Command
 {
  */
 class CommandHelpop : public Command
 {
+       const std::string startkey;
  public:
  public:
-       CommandHelpop(Module* Creator) : Command(Creator, "HELPOP", 0)
+       CommandHelpop(Module* Creator)
+               : Command(Creator, "HELPOP", 0)
+               , startkey("start")
        {
                syntax = "<any-text>";
        }
 
        CmdResult Handle (const std::vector<std::string> &parameters, User *user)
        {
        {
                syntax = "<any-text>";
        }
 
        CmdResult Handle (const std::vector<std::string> &parameters, User *user)
        {
-               irc::string parameter("start");
-               if (parameters.size() > 0)
-                       parameter = parameters[0].c_str();
+               const std::string& parameter = (!parameters.empty() ? parameters[0] : startkey);
 
                if (parameter == "index")
                {
                        /* iterate over all helpop items */
                        user->WriteNumeric(290, ":HELPOP topic index");
 
                if (parameter == "index")
                {
                        /* iterate over all helpop items */
                        user->WriteNumeric(290, ":HELPOP topic index");
-                       for (std::map<irc::string, std::string>::iterator iter = helpop_map.begin(); iter != helpop_map.end(); iter++)
+                       for (HelpopMap::const_iterator iter = helpop_map.begin(); iter != helpop_map.end(); iter++)
                                user->WriteNumeric(292, ":  %s", iter->first.c_str());
                        user->WriteNumeric(292, ":*** End of HELPOP topic index");
                }
                                user->WriteNumeric(292, ":  %s", iter->first.c_str());
                        user->WriteNumeric(292, ":*** End of HELPOP topic index");
                }
@@ -65,14 +67,14 @@ class CommandHelpop : public Command
                        user->WriteNumeric(290, ":*** HELPOP for %s", parameter.c_str());
                        user->WriteNumeric(292, ": -");
 
                        user->WriteNumeric(290, ":*** HELPOP for %s", parameter.c_str());
                        user->WriteNumeric(292, ": -");
 
-                       std::map<irc::string, std::string>::iterator iter = helpop_map.find(parameter);
+                       HelpopMap::const_iterator iter = helpop_map.find(parameter);
 
                        if (iter == helpop_map.end())
                        {
                                iter = helpop_map.find("nohelp");
                        }
 
 
                        if (iter == helpop_map.end())
                        {
                                iter = helpop_map.find("nohelp");
                        }
 
-                       std::string value = iter->second;
+                       const std::string& value = iter->second;
                        irc::sepstream stream(value, '\n');
                        std::string token = "*";
 
                        irc::sepstream stream(value, '\n');
                        std::string token = "*";
 
@@ -112,7 +114,7 @@ class ModuleHelpop : public Module
                        for(ConfigIter i = tags.first; i != tags.second; ++i)
                        {
                                ConfigTag* tag = i->second;
                        for(ConfigIter i = tags.first; i != tags.second; ++i)
                        {
                                ConfigTag* tag = i->second;
-                               irc::string key = assign(tag->getString("key"));
+                               std::string key = tag->getString("key");
                                std::string value;
                                tag->readString("value", value, true); /* Linefeeds allowed */
 
                                std::string value;
                                tag->readString("value", value, true); /* Linefeeds allowed */
 
index f98c8c3707123e9c5fc656e9014fb3980961aa54..b619ee4487392beb8d6da39fb5f0e4721db55ad3 100644 (file)
@@ -24,7 +24,7 @@
 
 class ModuleRestrictChans : public Module
 {
 
 class ModuleRestrictChans : public Module
 {
-       std::set<irc::string> allowchans;
+       std::set<std::string, irc::insensitive_swo> allowchans;
 
  public:
        void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
 
  public:
        void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
@@ -35,19 +35,17 @@ class ModuleRestrictChans : public Module
                {
                        ConfigTag* tag = i->second;
                        std::string txt = tag->getString("name");
                {
                        ConfigTag* tag = i->second;
                        std::string txt = tag->getString("name");
-                       allowchans.insert(txt.c_str());
+                       allowchans.insert(txt);
                }
        }
 
        ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
        {
                }
        }
 
        ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
        {
-               irc::string x(cname.c_str());
-
                // channel does not yet exist (record is null, about to be created IF we were to allow it)
                if (!chan)
                {
                        // user is not an oper and its not in the allow list
                // channel does not yet exist (record is null, about to be created IF we were to allow it)
                if (!chan)
                {
                        // user is not an oper and its not in the allow list
-                       if ((!user->IsOper()) && (allowchans.find(x) == allowchans.end()))
+                       if ((!user->IsOper()) && (allowchans.find(cname) == allowchans.end()))
                        {
                                user->WriteNumeric(ERR_BANNEDFROMCHAN, "%s :Only IRC operators may create new channels", cname.c_str());
                                return MOD_RES_DENY;
                        {
                                user->WriteNumeric(ERR_BANNEDFROMCHAN, "%s :Only IRC operators may create new channels", cname.c_str());
                                return MOD_RES_DENY;