diff options
author | attilamolnar <attilamolnar@hush.com> | 2013-06-13 18:32:29 +0200 |
---|---|---|
committer | attilamolnar <attilamolnar@hush.com> | 2013-06-13 18:32:29 +0200 |
commit | 05c816fcb2a8ca0ff97472f756aa28a04084de33 (patch) | |
tree | ebab6acdb74c500ab42b91bef776fe539df0f1f2 | |
parent | ac72c53ce2425801e135ab57c5defa707adcef5d (diff) |
Rewrite CommandParser::TranslateUIDs()
-rw-r--r-- | include/command_parse.h | 19 | ||||
-rw-r--r-- | src/command_parse.cpp | 89 | ||||
-rw-r--r-- | src/modules/m_spanningtree/main.cpp | 4 | ||||
-rw-r--r-- | src/modules/m_spanningtree/postcommand.cpp | 3 | ||||
-rw-r--r-- | src/modules/m_spanningtree/protocolinterface.cpp | 5 |
5 files changed, 45 insertions, 75 deletions
diff --git a/include/command_parse.h b/include/command_parse.h index 7809b67b8..480151612 100644 --- a/include/command_parse.h +++ b/include/command_parse.h @@ -135,23 +135,22 @@ class CoreExport CommandParser */ void RemoveCommand(Command* x); - /** Translate nicknames in a string into UIDs, based on the TranslationType given. - * @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 + /** Translate a single item based on the TranslationType given. + * @param to The translation type to use for the process + * @param item The input string + * @param dest The output string. The translation result will be appended to this string + * @param custom_translator Used to translate the parameter if the translation type is TR_CUSTOM, if NULL, TR_CUSTOM will act like TR_TEXT */ - int TranslateUIDs(TranslateType to, const std::string &source, std::string &dest); + static void TranslateSingleParam(TranslateType to, const std::string& item, std::string& dest, Command* custom_translator = NULL, unsigned int paramnumber = 0); /** 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. + * @param custom_translator Used to translate the parameter if the translation type is TR_CUSTOM, if NULL, TR_CUSTOM will act like TR_TEXT + * @return dest The output string */ - int TranslateUIDs(const std::vector<TranslateType> to, const std::vector<std::string> &source, std::string &dest, bool prefix_final = false, Command* custom_translator = NULL); + static std::string TranslateUIDs(const std::vector<TranslateType>& to, const std::vector<std::string>& source, bool prefix_final = false, Command* custom_translator = NULL); }; /** A lookup table of values for multiplier characters used by diff --git a/src/command_parse.cpp b/src/command_parse.cpp index 35cb1601b..08e28557c 100644 --- a/src/command_parse.cpp +++ b/src/command_parse.cpp @@ -381,88 +381,65 @@ 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; + // If no custom translator was given, fall through + } case TR_END: case TR_TEXT: default: /* Do nothing */ - dest = source; + dest.append(item); break; } - - return translations; } diff --git a/src/modules/m_spanningtree/main.cpp b/src/modules/m_spanningtree/main.cpp index 8c5439fbb..cc8871abd 100644 --- a/src/modules/m_spanningtree/main.cpp +++ b/src/modules/m_spanningtree/main.cpp @@ -812,9 +812,7 @@ ModResult ModuleSpanningTree::OnSetAway(User* user, const std::string &awaymsg) void ModuleSpanningTree::ProtoSendMode(void* opaque, TargetTypeFlags target_type, void* target, const parameterlist &modeline, const std::vector<TranslateType> &translate) { TreeSocket* s = (TreeSocket*)opaque; - std::string output_text; - - ServerInstance->Parser->TranslateUIDs(translate, modeline, output_text); + std::string output_text = CommandParser::TranslateUIDs(translate, modeline); if (target) { diff --git a/src/modules/m_spanningtree/postcommand.cpp b/src/modules/m_spanningtree/postcommand.cpp index a2d0c1168..476453e20 100644 --- a/src/modules/m_spanningtree/postcommand.cpp +++ b/src/modules/m_spanningtree/postcommand.cpp @@ -75,8 +75,7 @@ void SpanningTreeUtilities::RouteCommand(TreeServer* origin, Command* thiscmd, c } } - std::string output_text; - ServerInstance->Parser->TranslateUIDs(thiscmd->translation, parameters, output_text, true, thiscmd); + std::string output_text = CommandParser::TranslateUIDs(thiscmd->translation, parameters, true, thiscmd); params.push_back(output_text); diff --git a/src/modules/m_spanningtree/protocolinterface.cpp b/src/modules/m_spanningtree/protocolinterface.cpp index ce824fef8..0f5c40cc1 100644 --- a/src/modules/m_spanningtree/protocolinterface.cpp +++ b/src/modules/m_spanningtree/protocolinterface.cpp @@ -104,12 +104,9 @@ void SpanningTreeProtocolInterface::SendMode(User* source, User* u, Channel* c, } else { - std::string output_text; - ServerInstance->Parser->TranslateUIDs(translate, modedata, output_text); - params.push_back(c->name); params.push_back(ConvToStr(c->age)); - params.push_back(output_text); + params.push_back(CommandParser::TranslateUIDs(translate, modedata)); Utils->DoOneToMany(source->uuid, "FMODE", params); } } |