]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/command_parse.cpp
Change the syntax of FOREACH macros to be less dumb.
[user/henk/code/inspircd.git] / src / command_parse.cpp
index 35cb1601bedaf056b4b73485122002e1af31e734..51358519bf3f9983cb2d5d66f158e2bc8d065fe3 100644 (file)
@@ -100,7 +100,7 @@ bool CommandParser::LoopCall(User* user, Command* handler, const std::vector<std
                                // Run the OnPostCommand hook with the last parameter (original line) being empty
                                // to indicate that the command had more targets in its original form.
                                item.clear();
-                               FOREACH_MOD(I_OnPostCommand, OnPostCommand(handler, new_parameters, localuser, result, item));
+                               FOREACH_MOD(OnPostCommand, (handler, new_parameters, localuser, result, item));
                        }
                }
        }
@@ -108,30 +108,6 @@ bool CommandParser::LoopCall(User* user, Command* handler, const std::vector<std
        return true;
 }
 
-bool CommandParser::IsValidCommand(const std::string &commandname, unsigned int pcnt, User * user)
-{
-       Commandtable::iterator n = cmdlist.find(commandname);
-
-       if (n != cmdlist.end())
-       {
-               if ((pcnt >= n->second->min_params))
-               {
-                       if (IS_LOCAL(user) && n->second->flags_needed)
-                       {
-                               if (user->IsModeSet(n->second->flags_needed))
-                               {
-                                       return (user->HasPermission(commandname));
-                               }
-                       }
-                       else
-                       {
-                               return true;
-                       }
-               }
-       }
-       return false;
-}
-
 Command* CommandParser::GetHandler(const std::string &commandname)
 {
        Commandtable::iterator n = cmdlist.find(commandname);
@@ -340,7 +316,7 @@ void CommandParser::ProcessCommand(LocalUser *user, std::string &cmd)
                 */
                CmdResult result = handler->Handle(command_p, user);
 
-               FOREACH_MOD(I_OnPostCommand, OnPostCommand(handler, command_p, user, result, cmd));
+               FOREACH_MOD(OnPostCommand, (handler, command_p, user, result, cmd));
        }
 }
 
@@ -381,88 +357,64 @@ CommandParser::CommandParser()
 {
 }
 
-int CommandParser::TranslateUIDs(const std::vector<TranslateType> to, const std::vector<std::string> &source, std::string &dest, bool prefix_final, Command* custom_translator)
+std::string CommandParser::TranslateUIDs(const std::vector<TranslateType>& to, const std::vector<std::string>& source, bool prefix_final, Command* custom_translator)
 {
        std::vector<TranslateType>::const_iterator types = to.begin();
-       User* user = NULL;
-       unsigned int i;
-       int translations = 0;
-       dest.clear();
+       std::string dest;
 
-       for(i=0; i < source.size(); i++)
+       for (unsigned int i = 0; i < source.size(); i++)
        {
-               TranslateType t;
-               std::string item = source[i];
-
-               if (types == to.end())
-                       t = TR_TEXT;
-               else
+               TranslateType t = TR_TEXT;
+               // They might supply less translation types than parameters,
+               // in that case pretend that all remaining types are TR_TEXT
+               if (types != to.end())
                {
                        t = *types;
                        types++;
                }
 
-               if (prefix_final && i == source.size() - 1)
-                       dest.append(":");
+               bool last = (i == (source.size() - 1));
+               if (prefix_final && last)
+                       dest.push_back(':');
 
-               switch (t)
-               {
-                       case TR_NICK:
-                               /* Translate single nickname */
-                               user = ServerInstance->FindNick(item);
-                               if (user)
-                               {
-                                       dest.append(user->uuid);
-                                       translations++;
-                               }
-                               else
-                                       dest.append(item);
-                       break;
-                       case TR_CUSTOM:
-                               if (custom_translator)
-                                       custom_translator->EncodeParameter(item, i);
-                               dest.append(item);
-                       break;
-                       case TR_END:
-                       case TR_TEXT:
-                       default:
-                               /* Do nothing */
-                               dest.append(item);
-                       break;
-               }
-               if (i != source.size() - 1)
-                       dest.append(" ");
+               TranslateSingleParam(t, source[i], dest, custom_translator, i);
+
+               if (!last)
+                       dest.push_back(' ');
        }
 
-       return translations;
+       return dest;
 }
 
-int CommandParser::TranslateUIDs(TranslateType to, const std::string &source, std::string &dest)
+void CommandParser::TranslateSingleParam(TranslateType to, const std::string& item, std::string& dest, Command* custom_translator, unsigned int paramnumber)
 {
-       User* user = NULL;
-       int translations = 0;
-       dest.clear();
-
        switch (to)
        {
                case TR_NICK:
+               {
                        /* Translate single nickname */
-                       user = ServerInstance->FindNick(source);
+                       User* user = ServerInstance->FindNick(item);
                        if (user)
+                               dest.append(user->uuid);
+                       else
+                               dest.append(item);
+                       break;
+               }
+               case TR_CUSTOM:
+               {
+                       if (custom_translator)
                        {
-                               dest = user->uuid;
-                               translations++;
+                               std::string translated = item;
+                               custom_translator->EncodeParameter(translated, paramnumber);
+                               dest.append(translated);
+                               break;
                        }
-                       else
-                               dest = source;
-               break;
-               case TR_END:
+                       // If no custom translator was given, fall through
+               }
                case TR_TEXT:
                default:
                        /* Do nothing */
-                       dest = source;
+                       dest.append(item);
                break;
        }
-
-       return translations;
 }