mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-11-18 16:25:11 +01:00
Message files verifier - refactoring
- Separate logic from output (still potential for improvement) - Prompt user for options (single file mode / write missing keys or not)
This commit is contained in:
parent
5b92b30a96
commit
186ef965ca
@ -59,3 +59,5 @@ email_send: '[AuthMe] Recovery Email Send !'
|
||||
country_banned: 'Your country is banned from this server'
|
||||
antibot_auto_enabled: '[AuthMe] AntiBotMod automatically enabled due to massive connections!'
|
||||
antibot_auto_disabled: '[AuthMe] AntiBotMod automatically disabled after %m Minutes, hope invasion stopped'
|
||||
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:'
|
||||
|
@ -21,7 +21,7 @@ usage_unreg: '&eSử dụng: /unregister mật-khẩu'
|
||||
pwd_changed: '&cĐã đổi mật khẩu!'
|
||||
user_unknown: '&cTài khoản này chưa được đăng kí'
|
||||
password_error: '&fMật khẩu không khớp'
|
||||
unvalid_session: '&fPhiên đăng nhập không hồi đáp, vui lòng chờ phiên đăng nhập kết thúc'
|
||||
invalid_session: '&fPhiên đăng nhập không hồi đáp, vui lòng chờ phiên đăng nhập kết thúc'
|
||||
reg_only: '&fChỉ cho phép người đã đăng kí! Hãy vào trang http://web-của.bạn/ để đăng kí'
|
||||
logged_in: '&cĐã đăng nhập!'
|
||||
logout: '&cThoát đăng nhập thành công'
|
||||
|
@ -2,47 +2,61 @@ package messages;
|
||||
|
||||
import fr.xephi.authme.output.MessageKey;
|
||||
import utils.FileUtils;
|
||||
import utils.ToolsConstants;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Verifies that a message file has all keys as given in {@link MessageKey}.
|
||||
* Verifies a message file's keys to ensure that it is in sync with {@link MessageKey}, i.e. that the file contains
|
||||
* all keys and that it doesn't have any unknown ones.
|
||||
*/
|
||||
public class MessageFileVerifier {
|
||||
|
||||
public static final String MESSAGES_FOLDER = ToolsConstants.MAIN_RESOURCES_ROOT + "messages/";
|
||||
private static final String NEW_LINE = "\n";
|
||||
private static final char NEW_LINE = '\n';
|
||||
|
||||
private final String messagesFile;
|
||||
private final Map<String, String> defaultMessages;
|
||||
private final Set<String> unknownKeys = new HashSet<>();
|
||||
// Map with the missing key and a boolean indicating whether or not it was added to the file by this object
|
||||
private final Map<String, Boolean> missingKeys = new HashMap<>();
|
||||
|
||||
public MessageFileVerifier(Map<String, String> defaultMessages, String messagesFile) {
|
||||
public MessageFileVerifier(String messagesFile) {
|
||||
this.messagesFile = messagesFile;
|
||||
this.defaultMessages = defaultMessages;
|
||||
analyze();
|
||||
}
|
||||
|
||||
public void verify(boolean addMissingKeys) {
|
||||
public Set<String> getUnknownKeys() {
|
||||
return unknownKeys;
|
||||
}
|
||||
|
||||
public Map<String, Boolean> getMissingKeys() {
|
||||
return missingKeys;
|
||||
}
|
||||
|
||||
private void analyze() {
|
||||
findMissingKeys();
|
||||
}
|
||||
|
||||
private void findMissingKeys() {
|
||||
Set<String> messageKeys = getAllMessageKeys();
|
||||
List<String> fileLines = FileUtils.readLinesFromFile(messagesFile);
|
||||
for (String line : fileLines) {
|
||||
// Skip comments and empty lines
|
||||
if (!line.startsWith("#") && !line.trim().isEmpty()) {
|
||||
handleMessagesLine(line, messageKeys);
|
||||
verifyKeyInFile(line, messageKeys);
|
||||
}
|
||||
}
|
||||
|
||||
if (messageKeys.isEmpty()) {
|
||||
System.out.println("Found all message keys");
|
||||
} else {
|
||||
handleMissingKeys(messageKeys, addMissingKeys);
|
||||
// All keys that remain are keys that are absent in the file
|
||||
for (String missingKey : messageKeys) {
|
||||
missingKeys.put(missingKey, false);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleMessagesLine(String line, Set<String> messageKeys) {
|
||||
private void verifyKeyInFile(String line, Set<String> messageKeys) {
|
||||
if (line.indexOf(':') == -1) {
|
||||
System.out.println("Skipping line in unknown format: '" + line + "'");
|
||||
return;
|
||||
@ -52,20 +66,38 @@ public class MessageFileVerifier {
|
||||
if (messageKeys.contains(key)) {
|
||||
messageKeys.remove(key);
|
||||
} else {
|
||||
System.out.println("Warning: Unknown key '" + key + "' for line '" + line + "'");
|
||||
unknownKeys.add(key);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleMissingKeys(Set<String> missingKeys, boolean addMissingKeys) {
|
||||
for (String key : missingKeys) {
|
||||
if (addMissingKeys) {
|
||||
String defaultMessage = defaultMessages.get(key);
|
||||
FileUtils.appendToFile(messagesFile, NEW_LINE + key + ":" + defaultMessage);
|
||||
System.out.println("Added missing key '" + key + "' to file");
|
||||
} else {
|
||||
System.out.println("Error: Missing key '" + key + "'");
|
||||
public void addMissingKeys(Map<String, String> defaultMessages) {
|
||||
List<String> keysToAdd = new ArrayList<>();
|
||||
|
||||
for (Map.Entry<String, Boolean> entry : missingKeys.entrySet()) {
|
||||
if (Boolean.FALSE.equals(entry.getValue())) {
|
||||
String defaultMessage = defaultMessages.get(entry.getKey());
|
||||
if (defaultMessage == null) {
|
||||
System.out.println("Error: Key '" + entry.getKey() + "' not present in default messages");
|
||||
} else {
|
||||
keysToAdd.add(entry.getKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Very ugly way of verifying if the last char in the file is a new line, in which case we won't start by
|
||||
// adding a new line to the file. It's grossly inefficient but with the scale of the messages file it's fine
|
||||
String fileContents = FileUtils.readFromFile(messagesFile);
|
||||
String contentsToAdd = "";
|
||||
if (fileContents.charAt(fileContents.length() - 1) == NEW_LINE) {
|
||||
contentsToAdd += NEW_LINE;
|
||||
}
|
||||
|
||||
// We know that all keys in keysToAdd are safe to retrieve and add
|
||||
for (String keyToAdd : keysToAdd) {
|
||||
contentsToAdd += keyToAdd + ":" + defaultMessages.get(keyToAdd) + NEW_LINE;
|
||||
missingKeys.put(keyToAdd, true);
|
||||
}
|
||||
FileUtils.appendToFile(messagesFile, contentsToAdd);
|
||||
}
|
||||
|
||||
private static Set<String> getAllMessageKeys() {
|
||||
|
@ -1,40 +1,110 @@
|
||||
package messages;
|
||||
|
||||
import fr.xephi.authme.util.StringUtils;
|
||||
import utils.FileUtils;
|
||||
import utils.ToolsConstants;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static java.lang.String.format;
|
||||
|
||||
/**
|
||||
* TODO: ljacqu write JavaDoc
|
||||
*/
|
||||
public final class MessagesVerifierRunner {
|
||||
|
||||
public static final String MESSAGES_FOLDER = ToolsConstants.MAIN_RESOURCES_ROOT + "messages/";
|
||||
private static final String SOURCES_TAG = "{msgdir}";
|
||||
|
||||
private MessagesVerifierRunner() {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
final Map<String, String> defaultMessages = constructDefaultMessages();
|
||||
final List<File> messagesFiles = getMessagesFiles();
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
System.out.println("Check a specific file only?");
|
||||
System.out.println("- Empty line will check all files in the resources messages folder (default)");
|
||||
System.out.println(format("- %s will be replaced to the messages folder %s", SOURCES_TAG, MESSAGES_FOLDER));
|
||||
String inputFile = scanner.nextLine();
|
||||
|
||||
if (messagesFiles.isEmpty()) {
|
||||
throw new RuntimeException("Error getting messages file: list of files is empty");
|
||||
System.out.println("Add any missing keys to files? ['y' = yes]");
|
||||
boolean addMissingKeys = "y".equals(scanner.nextLine());
|
||||
scanner.close();
|
||||
|
||||
Map<String, String> defaultMessages = null;
|
||||
if (addMissingKeys) {
|
||||
defaultMessages = constructDefaultMessages();
|
||||
}
|
||||
|
||||
for (File file : messagesFiles) {
|
||||
List<File> messageFiles;
|
||||
if (StringUtils.isEmpty(inputFile)) {
|
||||
messageFiles = getMessagesFiles();
|
||||
} else {
|
||||
File customFile = new File(inputFile.replace(SOURCES_TAG, MESSAGES_FOLDER));
|
||||
messageFiles = Collections.singletonList(customFile);
|
||||
if (messageFiles.isEmpty()) {
|
||||
throw new RuntimeException("Error getting message files: list of files is empty");
|
||||
}
|
||||
}
|
||||
|
||||
for (File file : messageFiles) {
|
||||
System.out.println("Verifying '" + file.getName() + "'");
|
||||
MessageFileVerifier messageFileVerifier = new MessageFileVerifier(defaultMessages, file.getAbsolutePath());
|
||||
messageFileVerifier.verify(false);
|
||||
System.out.println();
|
||||
MessageFileVerifier verifier = new MessageFileVerifier(file.getAbsolutePath());
|
||||
if (addMissingKeys) {
|
||||
verifyFileAndAddKeys(verifier, defaultMessages);
|
||||
} else {
|
||||
verifyFile(verifier);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void verifyFile(MessageFileVerifier verifier) {
|
||||
Map<String, Boolean> missingKeys = verifier.getMissingKeys();
|
||||
if (missingKeys.isEmpty()) {
|
||||
System.out.println(" No missing keys");
|
||||
} else {
|
||||
System.out.println(" Missing keys: " + missingKeys.keySet());
|
||||
}
|
||||
|
||||
Set<String> unknownKeys = verifier.getUnknownKeys();
|
||||
if (!unknownKeys.isEmpty()) {
|
||||
System.out.println(" Unknown keys: " + unknownKeys);
|
||||
}
|
||||
}
|
||||
|
||||
private static void verifyFileAndAddKeys(MessageFileVerifier verifier, Map<String, String> defaultMessages) {
|
||||
Map<String, Boolean> missingKeys = verifier.getMissingKeys();
|
||||
if (missingKeys.isEmpty()) {
|
||||
System.out.println(" No missing keys");
|
||||
} else {
|
||||
verifier.addMissingKeys(defaultMessages);
|
||||
missingKeys = verifier.getMissingKeys();
|
||||
List<String> addedKeys = getKeysWithValue(Boolean.TRUE, missingKeys);
|
||||
System.out.println("Could add missing keys " + addedKeys);
|
||||
|
||||
List<String> unsuccessfulKeys = getKeysWithValue(Boolean.FALSE, missingKeys);
|
||||
if (!unsuccessfulKeys.isEmpty()) {
|
||||
System.out.println(" Warning! Could not add all missing keys (problem with loading " +
|
||||
"default messages?)");
|
||||
System.out.println(" Could not add keys " + unsuccessfulKeys);
|
||||
}
|
||||
}
|
||||
|
||||
Set<String> unknownKeys = verifier.getUnknownKeys();
|
||||
if (!unknownKeys.isEmpty()) {
|
||||
System.out.println(" Unknown keys: " + unknownKeys);
|
||||
}
|
||||
}
|
||||
|
||||
private static Map<String, String> constructDefaultMessages() {
|
||||
String defaultMessagesFile = MessageFileVerifier.MESSAGES_FOLDER + "messages_en.yml";
|
||||
String defaultMessagesFile = MESSAGES_FOLDER + "messages_en.yml";
|
||||
List<String> lines = FileUtils.readLinesFromFile(defaultMessagesFile);
|
||||
Map<String, String> messages = new HashMap<>(lines.size());
|
||||
for (String line : lines) {
|
||||
@ -51,9 +121,19 @@ public final class MessagesVerifierRunner {
|
||||
return messages;
|
||||
}
|
||||
|
||||
private static <K, V> List<K> getKeysWithValue(V value, Map<K, V> map) {
|
||||
List<K> result = new ArrayList<>();
|
||||
for (Map.Entry<K, V> entry : map.entrySet()) {
|
||||
if (value.equals(entry.getValue())) {
|
||||
result.add(entry.getKey());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static List<File> getMessagesFiles() {
|
||||
final Pattern messageFilePattern = Pattern.compile("messages_[a-z]{2,3}\\.yml");
|
||||
File folder = new File(MessageFileVerifier.MESSAGES_FOLDER);
|
||||
File folder = new File(MESSAGES_FOLDER);
|
||||
File[] files = folder.listFiles();
|
||||
if (files == null) {
|
||||
throw new RuntimeException("Could not read files from folder '" + folder.getName() + "'");
|
||||
|
Loading…
Reference in New Issue
Block a user