diff options
author | KHobbits <rob@khobbits.co.uk> | 2012-10-02 00:19:42 +0100 |
---|---|---|
committer | KHobbits <rob@khobbits.co.uk> | 2012-10-02 00:19:42 +0100 |
commit | 9a6616b302eaf37e1adbafa037956f0b9b276576 (patch) | |
tree | f47cfe3cb5ccb0c8433312ae4a4e705759cd623f | |
parent | 2e115c73a252448804a8037b5c7564d0ef7b0f40 (diff) | |
download | Essentials-9a6616b302eaf37e1adbafa037956f0b9b276576.tar Essentials-9a6616b302eaf37e1adbafa037956f0b9b276576.tar.gz Essentials-9a6616b302eaf37e1adbafa037956f0b9b276576.tar.lz Essentials-9a6616b302eaf37e1adbafa037956f0b9b276576.tar.xz Essentials-9a6616b302eaf37e1adbafa037956f0b9b276576.zip |
Better handle invalid warp names
5 files changed, 26 insertions, 9 deletions
diff --git a/Essentials/src/com/earth2me/essentials/EssentialsConf.java b/Essentials/src/com/earth2me/essentials/EssentialsConf.java index d6414fb05..70e683da4 100644 --- a/Essentials/src/com/earth2me/essentials/EssentialsConf.java +++ b/Essentials/src/com/earth2me/essentials/EssentialsConf.java @@ -344,6 +344,11 @@ public class EssentialsConf extends YamlConfiguration } } + public void saveWithError() throws IOException + { + save(configFile); + } + @Override public synchronized void save(final File file) throws IOException { diff --git a/Essentials/src/com/earth2me/essentials/UserData.java b/Essentials/src/com/earth2me/essentials/UserData.java index e9b84e656..76bac71b3 100644 --- a/Essentials/src/com/earth2me/essentials/UserData.java +++ b/Essentials/src/com/earth2me/essentials/UserData.java @@ -161,7 +161,7 @@ public abstract class UserData extends PlayerExtension implements IConf public void setHome(String name, Location loc) { //Invalid names will corrupt the yaml - name = Util.sanitizeFileName(name); + name = Util.safeString(name); homes.put(name, loc); config.setProperty("homes." + name, loc); config.save(); @@ -172,7 +172,7 @@ public abstract class UserData extends PlayerExtension implements IConf String search = getHomeName(name); if (!homes.containsKey(search)) { - search = Util.sanitizeFileName(search); + search = Util.safeString(search); } if (homes.containsKey(search)) { diff --git a/Essentials/src/com/earth2me/essentials/Util.java b/Essentials/src/com/earth2me/essentials/Util.java index 34e5c6420..cdbf3f82c 100644 --- a/Essentials/src/com/earth2me/essentials/Util.java +++ b/Essentials/src/com/earth2me/essentials/Util.java @@ -23,16 +23,20 @@ public class Util private final static Logger logger = Logger.getLogger("Minecraft"); private final static Pattern INVALIDFILECHARS = Pattern.compile("[^a-z0-9]"); private final static Pattern INVALIDCHARS = Pattern.compile("[^\t\n\r\u0020-\u007E\u0085\u00A0-\uD7FF\uE000-\uFFFC]"); - private final static Pattern BADFILENAMES = Pattern.compile("^(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])(\\.(.+))?$", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.COMMENTS); + //Used to clean file names before saving to disk public static String sanitizeFileName(final String name) { - String newName = INVALIDFILECHARS.matcher(name.toLowerCase(Locale.ENGLISH)).replaceAll("_"); - if(BADFILENAMES.matcher(newName).matches()) - newName = "_" + newName; - return newName; + return safeString(name); } + //Used to clean strings/names before saving as filenames/permissions + public static String safeString(final String string) + { + return INVALIDFILECHARS.matcher(string.toLowerCase(Locale.ENGLISH)).replaceAll("_"); + } + + //Less restrictive string sanitizing, when not used as perm or filename public static String sanitizeString(final String string) { return INVALIDCHARS.matcher(string).replaceAll(""); diff --git a/Essentials/src/com/earth2me/essentials/Warps.java b/Essentials/src/com/earth2me/essentials/Warps.java index ece987aba..6c3448f4e 100644 --- a/Essentials/src/com/earth2me/essentials/Warps.java +++ b/Essentials/src/com/earth2me/essentials/Warps.java @@ -3,6 +3,7 @@ package com.earth2me.essentials; import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.commands.WarpNotFoundException; import java.io.File; +import java.io.IOException; import java.util.*; import java.util.logging.Level; import java.util.logging.Logger; @@ -70,7 +71,14 @@ public class Warps implements IConf } conf.setProperty(null, loc); conf.setProperty("name", name); - conf.save(); + try + { + conf.saveWithError(); + } + catch (IOException ex) + { + throw new IOException(_("invalidWarpName")); + } } public void delWarp(String name) throws Exception diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandsetwarp.java b/Essentials/src/com/earth2me/essentials/commands/Commandsetwarp.java index ba8c71626..2469f6ec1 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandsetwarp.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandsetwarp.java @@ -40,7 +40,7 @@ public class Commandsetwarp extends EssentialsCommand { } - if (warpLoc == null || user.isAuthorized("essentials.warp.overwrite." + Util.sanitizeFileName(args[0]))) + if (warpLoc == null || user.isAuthorized("essentials.warp.overwrite." + Util.safeString(args[0]))) { warps.setWarp(args[0], loc); } |