From 52222d98e0d50defb9ebdfab4a4b13431ec5693d Mon Sep 17 00:00:00 2001 From: ljacqu Date: Wed, 30 Dec 2015 23:55:49 +0100 Subject: [PATCH] Fix #321 Message verifier should understand line breaks - Make messages verifier understand that indented lines belong to the same message - Revert newline to &n replacements done in f5583f4 --- src/main/resources/messages/messages_bg.yml | 3 +- src/main/resources/messages/messages_gl.yml | 6 ++-- src/tools/messages/MessageFileVerifier.java | 33 ++++++++++++++++++++- src/tools/utils/TagReplacer.java | 2 +- 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/main/resources/messages/messages_bg.yml b/src/main/resources/messages/messages_bg.yml index 9d9dc848..921d616b 100644 --- a/src/main/resources/messages/messages_bg.yml +++ b/src/main/resources/messages/messages_bg.yml @@ -1,7 +1,8 @@ unknown_user: '&fПотребителя не е в БД' unsafe_spawn: '&fТвоята локация когато излезе не беше безопасна, телепортиран си на Spawn!' not_logged_in: '&cНе си влязъл!' -reg_voluntarily: '&fМоже да се регистрираш с тази команда:&n "/register парола парола"' +reg_voluntarily: '&fМоже да се регистрираш с тази команда: + "/register парола парола"' usage_log: '&cКоманда: /login парола' wrong_pwd: '&cГрешна парола!' unregistered: '&cУспешно от-регистриран!' diff --git a/src/main/resources/messages/messages_gl.yml b/src/main/resources/messages/messages_gl.yml index 29944cec..807b4160 100644 --- a/src/main/resources/messages/messages_gl.yml +++ b/src/main/resources/messages/messages_gl.yml @@ -1,7 +1,8 @@ unknown_user: '&fO usuario non está na base de datos' unsafe_spawn: '&fA localización dende a que saíches era insegura, teletransportándote ao spawn do mundo' not_logged_in: '&cNon te identificaches!' -reg_voluntarily: '&fPodes rexistrar o teu nome no servidor co comando "/register "' +reg_voluntarily: '&fPodes rexistrar o teu nome no servidor co comando + "/register "' usage_log: '&cUso: /login ' wrong_pwd: '&cContrasinal equivocado' unregistered: '&cFeito! Xa non estás rexistrado!' @@ -54,6 +55,7 @@ email_changed: '[AuthMe] Cambiouse o correo!' email_send: '[AuthMe] Enviouse o correo de confirmación!' country_banned: 'O teu país está bloqueado neste servidor' antibot_auto_enabled: '[AuthMe] AntiBotMod conectouse automáticamente debido a conexións masivas!' -antibot_auto_disabled: '[AuthMe] AntiBotMod desactivouse automáticamente despois de %m minutos,&n esperemos que a invasión se detivera' +antibot_auto_disabled: '[AuthMe] AntiBotMod desactivouse automáticamente despois de %m minutos, + esperemos que a invasión se detivera' kick_antibot: 'AntiBot protection mode is enabled! You have to wait some minutes before joining the server.' email_exists: '&cA recovery email was already sent! You can discard it and send a new one using the command below:' diff --git a/src/tools/messages/MessageFileVerifier.java b/src/tools/messages/MessageFileVerifier.java index 904ea1af..3da84912 100644 --- a/src/tools/messages/MessageFileVerifier.java +++ b/src/tools/messages/MessageFileVerifier.java @@ -3,6 +3,7 @@ package messages; import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; import fr.xephi.authme.output.MessageKey; +import fr.xephi.authme.util.StringUtils; import utils.FileUtils; import java.util.ArrayList; @@ -69,7 +70,7 @@ public class MessageFileVerifier { private void verifyKeys() { List messageKeys = getAllMessageKeys(); - List fileLines = FileUtils.readLinesFromFile(messagesFile); + List fileLines = readFileLines(); for (String line : fileLines) { // Skip comments and empty lines if (!line.startsWith("#") && !line.trim().isEmpty()) { @@ -138,4 +139,34 @@ public class MessageFileVerifier { private static List getAllMessageKeys() { return new ArrayList<>(Arrays.asList(MessageKey.values())); } + + /** + * Read all lines from the messages file and skip empty lines and comment lines. + * This method appends lines starting with two spaces to the previously read line, + * akin to a YAML parser. + */ + private List readFileLines() { + String[] rawLines = FileUtils.readFromFile(messagesFile).split("\\n"); + List lines = new ArrayList<>(); + for (String line : rawLines) { + // Skip comments and empty lines + if (!line.startsWith("#") && !StringUtils.isEmpty(line)) { + // Line is indented, i.e. it needs to be appended to the previous line + if (line.startsWith(" ")) { + appendToLastElement(lines, line.substring(1)); + } else { + lines.add(line); + } + } + } + return lines; + } + + private static void appendToLastElement(List list, String text) { + if (list.isEmpty()) { + throw new IllegalStateException("List cannot be empty!"); + } + int lastIndex = list.size() - 1; + list.set(lastIndex, list.get(lastIndex).concat(text)); + } } diff --git a/src/tools/utils/TagReplacer.java b/src/tools/utils/TagReplacer.java index fedab4f9..7f359b4d 100644 --- a/src/tools/utils/TagReplacer.java +++ b/src/tools/utils/TagReplacer.java @@ -53,7 +53,7 @@ public class TagReplacer { } private static String replaceOptionalTag(String text, String tagName, String tagValue) { - Pattern regex = Pattern.compile("\\[" + tagName + "\\](.*?)\\[/" + tagName + "\\]", Pattern.DOTALL); + Pattern regex = Pattern.compile("\\[" + tagName + "](.*?)\\[/" + tagName + "]", Pattern.DOTALL); Matcher matcher = regex.matcher(text); if (!matcher.find()) {