]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Include explicit routing information in Command, will replace CMD_LOCALONLY return...
authordanieldg <danieldg@e03df62e-2008-0410-955e-edbf42e46eb7>
Wed, 2 Sep 2009 00:44:50 +0000 (00:44 +0000)
committerdanieldg <danieldg@e03df62e-2008-0410-955e-edbf42e46eb7>
Wed, 2 Sep 2009 00:44:50 +0000 (00:44 +0000)
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@11601 e03df62e-2008-0410-955e-edbf42e46eb7

include/command_parse.h
include/ctables.h
src/command_parse.cpp
src/modules/m_spanningtree/postcommand.cpp

index 62f47bd2df8ef2f440e8f9e0393221ef142b9828..3177dc95b0d0a8aaf54b5ccb0a42ee4309f68105 100644 (file)
@@ -199,11 +199,19 @@ class CoreExport CommandParser : public classbase
         * @param to The translation type to use for the process.
         * @param source The input string
         * @param dest The output string, it is safe to pass source and dest as the same variable only for translation type TR_TEXT.
-        * @return returns the number of substitutions made. Will always be 0 or 1 for TR_TEXT and 0..n for other types.
+        * @return returns the number of substitutions made. Will always be 0 or 1
         */
        int TranslateUIDs(TranslateType to, const std::string &source, std::string &dest);
 
-       int TranslateUIDs(const std::vector<TranslateType> to, const std::vector<std::string> &source, std::string &dest);
+       /** Translate nicknames in a list of strings into UIDs, based on the TranslateTypes given.
+        * @param to The translation types to use for the process. If this list is too short, TR_TEXT is assumed for the rest.
+        * @param source The strings to translate
+        * @param dest The output string
+        * @param prefix_final True if the final source argument should have a colon prepended (if it could contain a space)
+        * @param custom_translator Used to translate the parameter if the TR_CUSTOM type is found in to
+        * @return returns the number of substitutions made.
+        */
+       int TranslateUIDs(const std::vector<TranslateType> to, const std::vector<std::string> &source, std::string &dest, bool prefix_final = false, Command* custom_translator = NULL);
 };
 
 /** Command handler class for the RELOAD command.
index 9018d9c351981be442df857fcb630c74ab43e17f..b07d3f4e0fd3abddcebe7cacd42f4d0679e70095 100644 (file)
@@ -21,6 +21,7 @@ enum CmdResult
        CMD_FAILURE = 0,        /* Command exists, but failed */
        CMD_SUCCESS = 1,        /* Command exists, and succeeded */
        CMD_INVALID = 2         /* Command doesnt exist at all! */
+#define CMD_LOCALONLY CMD_FAILURE
 };
 
 /** Translation types for translation of parameters to UIDs.
@@ -36,14 +37,26 @@ enum TranslateType
        TR_CUSTOM               /* Custom translation handled by EncodeParameter/DecodeParameter */
 };
 
-/** For commands which should not be replicated to other
- * servers, we usually return CMD_FAILURE. this isnt readable,
- * so we define this alias for CMD_FAILURE called
- * CMD_LOCALONLY, which of course does the same thing but is
- * much more readable.
- */
-#define CMD_LOCALONLY CMD_FAILURE
+enum RouteType
+{
+       ROUTE_TYPE_LOCALONLY,
+       ROUTE_TYPE_BROADCAST,
+       ROUTE_TYPE_UNICAST
+};
+
+struct RouteDescriptor
+{
+       const RouteType type;
+       /** For unicast, the destination server's name
+        */
+       const std::string serverdest;
+       RouteDescriptor(RouteType t, const std::string d)
+               : type(t), serverdest(d) { }
+};
 
+#define ROUTE_LOCALONLY (RouteDescriptor(ROUTE_TYPE_LOCALONLY, ""))
+#define ROUTE_BROADCAST (RouteDescriptor(ROUTE_TYPE_BROADCAST, ""))
+#define ROUTE_UNICAST(x) (RouteDescriptor(ROUTE_TYPE_UNICAST, x))
 
 /** A structure that defines a command. Every command available
  * in InspIRCd must be defined as derived from Command.
@@ -145,6 +158,11 @@ class CoreExport Command : public Extensible
         */
        virtual CmdResult Handle(const std::vector<std::string>& parameters, User* user) = 0;
 
+       virtual RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
+       {
+               return ROUTE_BROADCAST;
+       }
+
        /** Handle an internal request from another command, the core, or a module
         * @param Command ID
         * @param Zero or more parameters, whos form is specified by the command ID.
index b446d2b9af9224a8e1f6effc686772087443ab4c..b063e392ce9293b2dbb3baf5b5e41c612ea9f6ca 100644 (file)
@@ -607,20 +607,29 @@ void CommandParser::SetupCommandTable()
                this->CreateCommand(new CommandReload(ServerInstance));
 }
 
-int CommandParser::TranslateUIDs(const std::vector<TranslateType> to, const std::vector<std::string> &source, std::string &dest)
+int CommandParser::TranslateUIDs(const std::vector<TranslateType> to, const std::vector<std::string> &source, std::string &dest, bool prefix_final, Command* custom_translator)
 {
-       std::vector<std::string>::const_iterator items = source.begin();
        std::vector<TranslateType>::const_iterator types = to.begin();
        User* user = NULL;
+       unsigned int i;
        int translations = 0;
        dest.clear();
 
-       while (items != source.end() && types != to.end())
+       for(i=0; i < source.size(); i++)
        {
-               TranslateType t = *types;
-               std::string item = *items;
-               types++;
-               items++;
+               TranslateType t;
+               std::string item = source[i];
+
+               if (types == to.end())
+                       t = TR_TEXT;
+               else
+               {
+                       t = *types;
+                       types++;
+               }
+
+               if (prefix_final && i == source.size() - 1)
+                       dest.append(":");
 
                switch (t)
                {
@@ -635,6 +644,10 @@ int CommandParser::TranslateUIDs(const std::vector<TranslateType> to, const std:
                                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:
@@ -643,11 +656,10 @@ int CommandParser::TranslateUIDs(const std::vector<TranslateType> to, const std:
                                dest.append(item);
                        break;
                }
-               dest.append(" ");
+               if (i != source.size() - 1)
+                       dest.append(" ");
        }
 
-       if (!dest.empty())
-               dest.erase(dest.end() - 1);
        return translations;
 }
 
index 719ff00466024f2d91094231ee66c7c0b6c3b5cb..428079c23f31d997db2131ec1289e856343b6d94 100644 (file)
 
 void ModuleSpanningTree::OnPostCommand(const std::string &command, const std::vector<std::string>& parameters, User *user, CmdResult result, const std::string &original_line)
 {
-       if ((result == CMD_SUCCESS) && (ServerInstance->IsValidModuleCommand(command, parameters.size(), user)))
-       {
-               /* Safe, we know its non-null because IsValidModuleCommand returned true */
-               Command* thiscmd = ServerInstance->Parser->GetHandler(command);
+       if (result != CMD_SUCCESS)
+               return;
+       if (!ServerInstance->IsValidModuleCommand(command, parameters.size(), user))
+               return;
 
-               Module* srcmodule = ServerInstance->Modules->Find(thiscmd->source);
+       /* We know it's non-null because IsValidModuleCommand returned true */
+       Command* thiscmd = ServerInstance->Parser->GetHandler(command);
 
-               if (srcmodule && !(srcmodule->GetVersion().Flags & VF_COMMON)) {
-                       ServerInstance->Logs->Log("m_spanningtree",ERROR,"Routed command %s from non-VF_COMMON module %s",
-                               command.c_str(), thiscmd->source.c_str());
-                       return;
-               }
+       RouteDescriptor routing = thiscmd->GetRouting(user, parameters);
 
-               // this bit of code cleverly routes all module commands
-               // to all remote severs *automatically* so that modules
-               // can just handle commands locally, without having
-               // to have any special provision in place for remote
-               // commands and linking protocols.
-               parameterlist params;
-               params.clear();
-               unsigned int n_translate = thiscmd->translation.size();
-               TranslateType translate_to;
+       if (routing.type == ROUTE_TYPE_LOCALONLY)
+               return;
 
-               /* To make sure that parameters with spaces, or empty
-                * parameters, etc, are always sent properly, *always*
-                * prefix the last parameter with a :. This also removes
-                * an extra strchr() */
-               for (unsigned int j = 0; j < parameters.size(); j++)
-               {
-                       std::string target;
+       Module* srcmodule = ServerInstance->Modules->Find(thiscmd->source);
 
-                       /* Map all items to UUIDs where neccessary */
-                       if (j < n_translate)
-                       {
-                               /* We have a translation mapping for this index */
-                               translate_to = thiscmd->translation[j] != TR_END ? thiscmd->translation[j] : TR_TEXT;
-                       }
-                       else
-                               translate_to = TR_TEXT;
+       if (srcmodule && !(srcmodule->GetVersion().Flags & VF_COMMON)) {
+               ServerInstance->Logs->Log("m_spanningtree",ERROR,"Routed command %s from non-VF_COMMON module %s",
+                       command.c_str(), thiscmd->source.c_str());
+               return;
+       }
+
+       std::string output_text;
+       ServerInstance->Parser->TranslateUIDs(thiscmd->translation, parameters, output_text, true, thiscmd);
 
-                       ServerInstance->Logs->Log("m_spanningtree",DEBUG,"TRANSLATION: %s - type is %d", parameters[j].c_str(), translate_to);
-                       if (translate_to == TR_CUSTOM)
-                       {
-                               target = parameters[j];
-                               thiscmd->EncodeParameter(target, j);
-                       }
-                       else
-                       {
-                               ServerInstance->Parser->TranslateUIDs(translate_to, parameters[j], target);
-                       }
+       parameterlist params;
+       params.push_back(output_text);
 
-                       if (j == (parameters.size() - 1))
-                               params.push_back(":" + target);
-                       else
-                               params.push_back(target);
-               }
+       if (routing.type == ROUTE_TYPE_BROADCAST)
                Utils->DoOneToMany(user->uuid, command, params);
-       }
+       else
+               Utils->DoOneToOne(user->uuid, command, params, routing.serverdest);
 }
-