From: Matthew I Date: Sun, 2 Sep 2012 20:51:17 +0000 (-0400) Subject: Enforce stricter world names using a blacklist X-Git-Url: http://81.2.79.47:8989/gitweb/?a=commitdiff_plain;h=5dd1d354f86692e4c08cc78f3d9743557103449e;p=zefram%2Fminetest%2Fminetest_engine.git Enforce stricter world names using a blacklist Blacklisted characters are: / \ --- diff --git a/src/guiMainMenu.cpp b/src/guiMainMenu.cpp index cdf1bc7d..4ceecbb5 100644 --- a/src/guiMainMenu.cpp +++ b/src/guiMainMenu.cpp @@ -39,6 +39,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "tile.h" // getTexturePath #include "filesys.h" #include "util/string.h" +#include "subgame.h" struct CreateWorldDestMainMenu : public CreateWorldDest { @@ -47,7 +48,10 @@ struct CreateWorldDestMainMenu : public CreateWorldDest {} void accepted(std::wstring name, std::string gameid) { - m_menu->createNewWorld(name, gameid); + if(!string_allowed_blacklist(wide_to_narrow(name), WORLDNAME_BLACKLISTED_CHARS)) + m_menu->displayMessageMenu(wgettext("Cannot create world: Name contains invalid characters")); + else + m_menu->createNewWorld(name, gameid); } GUIMainMenu *m_menu; }; @@ -929,3 +933,7 @@ int GUIMainMenu::getTab() return TAB_SINGLEPLAYER; // Default } +void GUIMainMenu::displayMessageMenu(std::wstring msg) +{ + (new GUIMessageMenu(env, parent, -1, menumgr, msg))->drop(); +} \ No newline at end of file diff --git a/src/guiMainMenu.h b/src/guiMainMenu.h index fa3d83c4..715deb47 100644 --- a/src/guiMainMenu.h +++ b/src/guiMainMenu.h @@ -92,6 +92,7 @@ public: void createNewWorld(std::wstring name, std::string gameid); void deleteWorld(const std::vector &paths); int getTab(); + void displayMessageMenu(std::wstring msg); private: MainMenuData *m_data; diff --git a/src/subgame.h b/src/subgame.h index e3a299cb..bffa86e2 100644 --- a/src/subgame.h +++ b/src/subgame.h @@ -24,6 +24,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include +#define WORLDNAME_BLACKLISTED_CHARS "/\\" + struct SubgameSpec { std::string id; // "" = game does not exist diff --git a/src/util/string.h b/src/util/string.h index 97b07f2f..71b11de3 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -242,6 +242,29 @@ inline bool string_allowed(const std::string &s, const std::string &allowed_char return true; } +/* + Checks if a string contains no blacklisted characters (opposite + function of string_allowed()) +*/ +inline bool string_allowed_blacklist(const std::string & s, const std::string & blacklisted_chars) +{ + for(unsigned int i = 0; i < s.length(); i++) + { + bool invalid = false; + for(unsigned int j = 0; j < blacklisted_chars.length(); j++) + { + if(s[i] == blacklisted_chars[j]) + { + invalid = true; + break; + } + } + if(invalid) + return false; + } + return true; +} + /* Forcefully wraps string into rows using \n (no word wrap, used for showing paths in gui)