diff --git a/protocols/managesieve/src/main/java/org/apache/james/managesieve/transcode/ArgumentParser.java b/protocols/managesieve/src/main/java/org/apache/james/managesieve/transcode/ArgumentParser.java index 28b307dc89b..bb573cb9fca 100644 --- a/protocols/managesieve/src/main/java/org/apache/james/managesieve/transcode/ArgumentParser.java +++ b/protocols/managesieve/src/main/java/org/apache/james/managesieve/transcode/ArgumentParser.java @@ -29,10 +29,7 @@ import org.apache.james.managesieve.api.commands.CoreCommands; import org.apache.james.managesieve.util.ParserUtils; -import java.util.InputMismatchException; import java.util.Iterator; -import java.util.NoSuchElementException; -import java.util.Scanner; /** * Parses the user input and calls the underlying command processor @@ -81,28 +78,27 @@ public String authenticate(Session session, String suppliedData) { } public String deleteScript(Session session, String args) { - String scriptName = ParserUtils.getScriptName(args); - if (null == scriptName || scriptName.isEmpty()) { + Iterator argumentIterator = Splitter.on(' ').omitEmptyStrings().split(args).iterator(); + if (!argumentIterator.hasNext()) { return "NO \"Missing argument: script name\""; } - - Scanner scanner = new Scanner(args.substring(scriptName.length()).trim()).useDelimiter("\\A"); - if (scanner.hasNext()) { - return "NO \"Too many arguments: " + scanner.next() + "\""; + String scriptName = ParserUtils.unquote(argumentIterator.next()); + if (argumentIterator.hasNext()) { + return "NO \"Too many arguments: " + argumentIterator.next() + "\""; } - return core.deleteScript(session, ParserUtils.unquote(scriptName)); + return core.deleteScript(session, scriptName); } public String getScript(Session session, String args) { - String scriptName = ParserUtils.getScriptName(args); - if (scriptName == null || scriptName.isEmpty()) { + Iterator argumentIterator = Splitter.on(' ').omitEmptyStrings().split(args).iterator(); + if (!argumentIterator.hasNext()) { return "NO \"Missing argument: script name\""; } - Scanner scanner = new Scanner(args.substring(scriptName.length()).trim()).useDelimiter("\\A"); - if (scanner.hasNext()) { - return "NO \"Too many arguments: " + scanner.next() + "\""; + String scriptName = ParserUtils.unquote(argumentIterator.next()); + if (argumentIterator.hasNext()) { + return "NO \"Too many arguments: " + argumentIterator.next() + "\""; } - return core.getScript(session, ParserUtils.unquote(scriptName)); + return core.getScript(session, scriptName); } public String checkScript(Session session, String args) { @@ -130,24 +126,24 @@ public String checkScript(Session session, String args) { } public String haveSpace(Session session, String args) { - String scriptName = ParserUtils.getScriptName(args); - if (null == scriptName || scriptName.isEmpty()) { + Iterator argumentIterator = Splitter.on(' ').omitEmptyStrings().split(args.trim()).iterator(); + if (!argumentIterator.hasNext()) { return "NO \"Missing argument: script name\""; } - Scanner scanner = new Scanner(args.substring(scriptName.length()).trim()); + String scriptName = ParserUtils.unquote(argumentIterator.next()); long size; + if (!argumentIterator.hasNext()) { + return "NO \"Missing argument: script size\""; + } try { - size = scanner.nextLong(); - } catch (InputMismatchException ex) { + size = Long.parseLong(argumentIterator.next()); + } catch (NumberFormatException e) { return "NO \"Invalid argument: script size\""; - } catch (NoSuchElementException ex) { - return "NO \"Missing argument: script size\""; } - scanner.useDelimiter("\\A"); - if (scanner.hasNext()) { - return "NO \"Too many arguments: " + scanner.next().trim() + "\""; + if (argumentIterator.hasNext()) { + return "NO \"Too many arguments: " + argumentIterator.next().trim() + "\""; } - return core.haveSpace(session, ParserUtils.unquote(scriptName), size); + return core.haveSpace(session, scriptName, size); } public String listScripts(Session session, String args) { @@ -188,41 +184,31 @@ public String putScript(Session session, String args) { } public String renameScript(Session session, String args) { - String oldName = ParserUtils.getScriptName(args); - if (null == oldName || oldName.isEmpty()) { - return "NO \"Missing argument: old script name\""; + Iterator argumentIterator = Splitter.on(' ').omitEmptyStrings().split(args).iterator(); + if (!argumentIterator.hasNext()) { + return "NO \"Missing argument: old script name\""; } - - String newName = ParserUtils.getScriptName(args.substring(oldName.length())); - if (null == newName || newName.isEmpty()) { + String oldName = ParserUtils.unquote(argumentIterator.next()); + if (!argumentIterator.hasNext()) { return "NO \"Missing argument: new script name\""; - } - - Scanner scanner = new Scanner(args.substring(oldName.length() + 1 + newName.length()).trim()).useDelimiter("\\A"); - if (scanner.hasNext()) { - return "NO \"Too many arguments: " + scanner.next() + "\""; } - return core.renameScript(session, ParserUtils.unquote(oldName), ParserUtils.unquote(newName)); + String newName = ParserUtils.unquote(argumentIterator.next()); + if (argumentIterator.hasNext()) { + return "NO \"Too many arguments: " + argumentIterator.next() + "\""; + } + return core.renameScript(session, oldName, newName); } public String setActive(Session session, String args) { - String scriptName = ParserUtils.getScriptName(args); - if (null == scriptName || scriptName.isEmpty()) { + Iterator argumentIterator = Splitter.on(' ').omitEmptyStrings().split(args).iterator(); + if (!argumentIterator.hasNext()) { return "NO \"Missing argument: script name\""; } - Scanner scanner = new Scanner(args.substring(scriptName.length()).trim()).useDelimiter("\\A"); - if (scanner.hasNext()) { - return "NO \"Too many arguments: " + scanner.next() + "\""; - } - return core.setActive(session, ParserUtils.unquote(scriptName)); - } - - public String getActive(Session session, String args) { - Scanner scanner = new Scanner(args.trim()).useDelimiter("\\A"); - if (scanner.hasNext()) { - return "NO \"Too many arguments: " + scanner.next() + "\""; + String scriptName = ParserUtils.unquote(argumentIterator.next()); + if (argumentIterator.hasNext()) { + return "NO \"Too many arguments: " + argumentIterator.next() + "\""; } - return core.getActive(session); + return core.setActive(session, scriptName); } public String startTLS(Session session) { diff --git a/protocols/managesieve/src/main/java/org/apache/james/managesieve/util/ParserUtils.java b/protocols/managesieve/src/main/java/org/apache/james/managesieve/util/ParserUtils.java index 52f100b628e..a6c41730312 100644 --- a/protocols/managesieve/src/main/java/org/apache/james/managesieve/util/ParserUtils.java +++ b/protocols/managesieve/src/main/java/org/apache/james/managesieve/util/ParserUtils.java @@ -22,13 +22,8 @@ import org.apache.james.managesieve.api.ArgumentException; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - public class ParserUtils { - private static final Pattern SCRIPT_NAME_REGEX = Pattern.compile("[^\\s\"']+|\"[^\"]*\"|'[^']*'"); - public static long getSize(String args) throws ArgumentException { if (args != null && args.length() > 3 && args.charAt(0) == '{' @@ -43,19 +38,6 @@ public static long getSize(String args) throws ArgumentException { throw new ArgumentException(args + " is an invalid size literal : it should be at least 4 char looking like {_+}"); } - public static String getScriptName(String args) { - Matcher regexMatcher = SCRIPT_NAME_REGEX.matcher(args); - regexMatcher.find(); - String name = null; - try { - name = regexMatcher.group(); - } catch(IllegalStateException ex) - { - // no op - } - return name; - } - public static String unquote(String quoted) { String result = quoted; if (quoted != null) {