Skip to content

Commit

Permalink
campaignd: Move several anonymous namespace members to a new file
Browse files Browse the repository at this point in the history
This step covers the markup char check function and the add-on feedback
URL formatter.

(Backported 0d81fae from master.)
  • Loading branch information
irydacea committed Oct 11, 2014
1 parent 04c97ad commit 8a65dfc
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 81 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Expand Up @@ -1069,6 +1069,7 @@ if(ENABLE_CAMPAIGN_SERVER)
set(campaignd_SRC
network_worker.cpp # NEEDED when compiling with ANA support
addon/validation.cpp
campaign_server/addon_utils.cpp
campaign_server/blacklist.cpp
campaign_server/campaign_server.cpp
server/input_stream.cpp
Expand Down
1 change: 1 addition & 0 deletions src/SConscript
Expand Up @@ -568,6 +568,7 @@ if env["host"] in ["x86_64-nacl", "i686-nacl"]:
client_env.WesnothProgram("wesnoth", wesnoth_objects, have_client_prereqs)

campaignd_sources = Split("""
campaign_server/addon_utils.cpp
campaign_server/blacklist.cpp
server/input_stream.cpp
""")
Expand Down
88 changes: 88 additions & 0 deletions src/campaign_server/addon_utils.cpp
@@ -0,0 +1,88 @@
/*
Copyright (C) 2013 - 2014 by Ignacio Riquelme Morelle <shadowm2006@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program 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; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/

#include "campaign_server/addon_utils.hpp"

#include "config.hpp"
#include "serialization/string_utils.hpp"

#include <boost/foreach.hpp>

namespace {

typedef std::map<std::string, std::string> plain_string_map;

/**
* Quick and dirty alternative to @a utils::interpolate_variables_into_string
* that doesn't require formula AI code. It is definitely NOT safe for normal
* use since it doesn't do strict checks on where variable placeholders
* ("$foobar") end and doesn't support pipe ("|") terminators.
*
* @param str The format string.
* @param symbols The symbols table.
*/
std::string fast_interpolate_variables_into_string(const std::string &str, const plain_string_map * const symbols)
{
std::string res = str;

if(symbols) {
BOOST_FOREACH(const plain_string_map::value_type& sym, *symbols) {
res = utils::replace(res, "$" + sym.first, sym.second);
}
}

return res;
}

} // end anonymous namespace

namespace campaignd {

// Markup characters recognized by GUI1 code. These must be
// the same as the constants defined in marked-up_text.cpp.
const std::string illegal_markup_chars = "*`~{^}|@#<&";

std::string format_addon_feedback_url(const std::string& format, const config& params)
{
if(!format.empty() && !params.empty()) {
plain_string_map escaped;

config::const_attr_itors attrs = params.attribute_range();

// Percent-encode parameter values for URL interpolation. This is
// VERY important since otherwise people could e.g. alter query
// strings from the format string.
BOOST_FOREACH(const config::attribute& a, attrs) {
escaped[a.first] = utils::urlencode(a.second.str());
}

// FIXME: We cannot use utils::interpolate_variables_into_string
// because it is implemented using a lot of formula AI junk
// that really doesn't belong in campaignd.
const std::string& res =
fast_interpolate_variables_into_string(format, &escaped);

if(res != format) {
return res;
}

// If we get here, that means that no interpolation took place; in
// that case, the parameters table probably contains entries that
// do not match the format string expectations.
}

return std::string();
}

} // end namespace campaignd
54 changes: 54 additions & 0 deletions src/campaign_server/addon_utils.hpp
@@ -0,0 +1,54 @@
/*
Copyright (C) 2013 - 2014 by Ignacio Riquelme Morelle <shadowm2006@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program 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; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/

#ifndef CAMPAIGN_SERVER_ADDON_UTILS_HPP_INCLUDED
#define CAMPAIGN_SERVER_ADDON_UTILS_HPP_INCLUDED

#include <string>

class config;

namespace campaignd {

/**
* Markup characters recognized by GUI1 code.
*
* These must be the same as the constants defined in marked-up_text.cpp.
*/
extern const std::string illegal_markup_chars;

inline bool is_text_markup_char(char c)
{
return illegal_markup_chars.find(c) != std::string::npos;
}

/**
* Format a feedback URL for an add-on.
*
* @param format The format string for the URL, presumably obtained
* from the add-ons server identification.
*
* @param params The URL format parameters table.
*
* @return A string containing a feedback URL or an empty string if that
* is not possible (e.g. empty or invalid @a format, empty
* @a params table, or a result that is identical in content to
* the @a format suggesting that the @a params table contains
* incorrect data).
*/
std::string format_addon_feedback_url(const std::string& format, const config& params);

}

#endif
82 changes: 1 addition & 81 deletions src/campaign_server/campaign_server.cpp
Expand Up @@ -29,6 +29,7 @@
#include "serialization/string_utils.hpp"
#include "game_config.hpp"
#include "addon/validation.hpp"
#include "campaign_server/addon_utils.hpp"
#include "campaign_server/blacklist.hpp"
#include "version.hpp"
#include "util.hpp"
Expand Down Expand Up @@ -73,87 +74,6 @@ static void exit_sigterm(int signal) {
exit(128 + SIGTERM);
}

namespace {
// Markup characters recognized by GUI1 code. These must be
// the same as the constants defined in marked-up_text.cpp.
const std::string illegal_markup_chars = "*`~{^}|@#<&";

inline bool is_text_markup_char(char c)
{
return illegal_markup_chars.find(c) != std::string::npos;
}

typedef std::map<std::string, std::string> plain_string_map;

/**
* Quick and dirty alternative to @a uitls::interpolate_variables_into_string that
* doesn't require formula AI code. It is definitely NOT safe for normal
* use since it doesn't do strict checks on where variable placeholders
* ("$foobar") end and doesn't support pipe ("|") terminators.
*
* @param str The format string.
* @param symbols The symbols table.
*/
std::string fast_interpolate_variables_into_string(const std::string &str, const plain_string_map * const symbols)
{
std::string res = str;

if(symbols) {
BOOST_FOREACH(const plain_string_map::value_type& sym, *symbols) {
res = utils::replace(res, "$" + sym.first, sym.second);
}
}

return res;
}

/**
* Format a feedback URL for an add-on.
*
* @param format The format string for the URL, presumably obtained
* from the add-ons server identification.
*
* @param params The URL format parameters table.
*
* @return A string containing a feedback URL or an empty string if that
* is not possible (e.g. empty or invalid @a format, empty
* @a params table, or a result that is identical in content to
* the @a format suggesting that the @a params table contains
* incorrect data).
*/
std::string format_addon_feedback_url(const std::string& format, const config& params)
{
if(!format.empty() && !params.empty()) {
plain_string_map escaped;

config::const_attr_itors attrs = params.attribute_range();

// Percent-encode parameter values for URL interpolation. This is
// VERY important since otherwise people could e.g. alter query
// strings from the format string.
BOOST_FOREACH(const config::attribute& a, attrs) {
escaped[a.first] = utils::urlencode(a.second.str());
}

// FIXME: We cannot use utils::interpolate_variables_into_string
// because it is implemented using a lot of formula AI junk
// that really doesn't belong in campaignd.
const std::string& res =
fast_interpolate_variables_into_string(format, &escaped);

if(res != format) {
return res;
}

// If we get here, that means that no interpolation took place; in
// that case, the parameters table probably contains entries that
// do not match the format string expectations.
}

return std::string();
}
} // end anonymous namespace 1

namespace campaignd {

server::server(const std::string& cfg_file, size_t min_threads, size_t max_threads)
Expand Down

0 comments on commit 8a65dfc

Please sign in to comment.