Enforce stricter world names using a blacklist
authorMatthew I <matttpt@gmail.com>
Sun, 2 Sep 2012 20:51:17 +0000 (16:51 -0400)
committerPerttu Ahola <celeron55@gmail.com>
Tue, 4 Sep 2012 22:17:28 +0000 (01:17 +0300)
Blacklisted characters are: / \

src/guiMainMenu.cpp
src/guiMainMenu.h
src/subgame.h
src/util/string.h

index cdf1bc7d5690b9f76c9e1bc1cfc8089f526c67f4..4ceecbb5f0261a83d25bc7c3af7d5a06036d41c9 100644 (file)
@@ -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
index fa3d83c45e167dcd704677fff99ab5a00cf58a28..715deb47d015cb565d1a31285dbd6c824ce24734 100644 (file)
@@ -92,6 +92,7 @@ public:
        void createNewWorld(std::wstring name, std::string gameid);
        void deleteWorld(const std::vector<std::string> &paths);
        int getTab();
+       void displayMessageMenu(std::wstring msg);
        
 private:
        MainMenuData *m_data;
index e3a299cbee8677b6dd7101b57fbadaff154d2662..bffa86e285813da61adc76443bb55da2e3800ce9 100644 (file)
@@ -24,6 +24,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <set>
 #include <vector>
 
+#define WORLDNAME_BLACKLISTED_CHARS "/\\"
+
 struct SubgameSpec
 {
        std::string id; // "" = game does not exist
index 97b07f2ffd9a4486e33e138aac44d134b47ca34e..71b11de3d1109cd8687fd6dfa91af21c6d754912 100644 (file)
@@ -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)