Del all files
This commit is contained in:
@@ -1,59 +0,0 @@
|
||||
package fr.xephi.authme.message.updater;
|
||||
|
||||
import ch.jalu.configme.properties.Property;
|
||||
import ch.jalu.configme.resource.PropertyReader;
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
import fr.xephi.authme.output.ConsoleLoggerFactory;
|
||||
import fr.xephi.authme.util.FileUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* Returns messages from the JAR's message files. Favors a local JAR (e.g. messages_nl.yml)
|
||||
* before falling back to the default language (messages_en.yml).
|
||||
*/
|
||||
public class JarMessageSource {
|
||||
|
||||
private final ConsoleLogger logger = ConsoleLoggerFactory.get(JarMessageSource.class);
|
||||
private final PropertyReader localJarMessages;
|
||||
private final PropertyReader defaultJarMessages;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param localJarPath path to the messages file of the language the plugin is configured to use (may not exist)
|
||||
* @param defaultJarPath path to the default messages file in the JAR (must exist)
|
||||
*/
|
||||
public JarMessageSource(String localJarPath, String defaultJarPath) {
|
||||
localJarMessages = localJarPath.equals(defaultJarPath) ? null : loadJarFile(localJarPath);
|
||||
defaultJarMessages = loadJarFile(defaultJarPath);
|
||||
|
||||
if (defaultJarMessages == null) {
|
||||
throw new IllegalStateException("Default JAR file '" + defaultJarPath + "' could not be loaded");
|
||||
}
|
||||
}
|
||||
|
||||
public String getMessageFromJar(Property<?> property) {
|
||||
String key = property.getPath();
|
||||
String message = getString(key, localJarMessages);
|
||||
return message == null ? getString(key, defaultJarMessages) : message;
|
||||
}
|
||||
|
||||
private static String getString(String path, PropertyReader reader) {
|
||||
return reader == null ? null : reader.getString(path);
|
||||
}
|
||||
|
||||
private MessageMigraterPropertyReader loadJarFile(String jarPath) {
|
||||
try (InputStream stream = FileUtils.getResourceFromJar(jarPath)) {
|
||||
if (stream == null) {
|
||||
logger.debug("Could not load '" + jarPath + "' from JAR");
|
||||
return null;
|
||||
}
|
||||
return MessageMigraterPropertyReader.loadFromStream(stream);
|
||||
} catch (IOException e) {
|
||||
logger.logException("Exception while handling JAR path '" + jarPath + "'", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
package fr.xephi.authme.message.updater;
|
||||
|
||||
import ch.jalu.configme.configurationdata.ConfigurationDataImpl;
|
||||
import ch.jalu.configme.properties.Property;
|
||||
import ch.jalu.configme.properties.convertresult.PropertyValue;
|
||||
import ch.jalu.configme.resource.PropertyReader;
|
||||
import fr.xephi.authme.message.MessageKey;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class MessageKeyConfigurationData extends ConfigurationDataImpl {
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param propertyListBuilder property list builder for message key properties
|
||||
* @param allComments registered comments
|
||||
*/
|
||||
public MessageKeyConfigurationData(MessageUpdater.MessageKeyPropertyListBuilder propertyListBuilder,
|
||||
Map<String, List<String>> allComments) {
|
||||
super(propertyListBuilder.getAllProperties(), allComments);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initializeValues(PropertyReader reader) {
|
||||
for (Property<String> property : getAllMessageProperties()) {
|
||||
PropertyValue<String> value = property.determineValue(reader);
|
||||
if (value.isValidInResource()) {
|
||||
setValue(property, value.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getValue(Property<T> property) {
|
||||
// Override to silently return null if property is unknown
|
||||
return (T) getValues().get(property.getPath());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Property<String>> getAllMessageProperties() {
|
||||
return (List) getProperties();
|
||||
}
|
||||
|
||||
public String getMessage(MessageKey messageKey) {
|
||||
return getValue(new MessageUpdater.MessageKeyProperty(messageKey));
|
||||
}
|
||||
|
||||
public void setMessage(MessageKey messageKey, String message) {
|
||||
setValue(new MessageUpdater.MessageKeyProperty(messageKey), message);
|
||||
}
|
||||
}
|
||||
@@ -1,126 +0,0 @@
|
||||
package fr.xephi.authme.message.updater;
|
||||
|
||||
import ch.jalu.configme.exception.ConfigMeException;
|
||||
import ch.jalu.configme.resource.PropertyReader;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Implementation of {@link PropertyReader} which can read a file or a stream with
|
||||
* a specified charset.
|
||||
*/
|
||||
final class MessageMigraterPropertyReader implements PropertyReader {
|
||||
|
||||
private static final Charset CHARSET = StandardCharsets.UTF_8;
|
||||
|
||||
private Map<String, Object> root;
|
||||
|
||||
private MessageMigraterPropertyReader(Map<String, Object> valuesMap) {
|
||||
root = valuesMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new property reader for the given file.
|
||||
*
|
||||
* @param file the file to load
|
||||
* @return the created property reader
|
||||
*/
|
||||
public static MessageMigraterPropertyReader loadFromFile(File file) {
|
||||
try (InputStream is = new FileInputStream(file)) {
|
||||
return loadFromStream(is);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("Error while reading file '" + file + "'", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static MessageMigraterPropertyReader loadFromStream(InputStream inputStream) {
|
||||
Map<String, Object> valuesMap = readStreamToMap(inputStream);
|
||||
return new MessageMigraterPropertyReader(valuesMap);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(String path) {
|
||||
return getObject(path) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getKeys(boolean b) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getChildKeys(String s) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getObject(String path) {
|
||||
if (path.isEmpty()) {
|
||||
return root.get("");
|
||||
}
|
||||
Object node = root;
|
||||
String[] keys = path.split("\\.");
|
||||
for (String key : keys) {
|
||||
node = getIfIsMap(key, node);
|
||||
if (node == null) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String path) {
|
||||
Object o = getObject(path);
|
||||
return o instanceof String ? (String) o : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getInt(String path) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getDouble(String path) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean getBoolean(String path) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<?> getList(String path) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private static Map<String, Object> readStreamToMap(InputStream inputStream) {
|
||||
try (InputStreamReader isr = new InputStreamReader(inputStream, CHARSET)) {
|
||||
Object obj = new Yaml().load(isr);
|
||||
return obj == null ? new HashMap<>() : (Map<String, Object>) obj;
|
||||
} catch (IOException e) {
|
||||
throw new ConfigMeException("Could not read stream", e);
|
||||
} catch (ClassCastException e) {
|
||||
throw new ConfigMeException("Top-level is not a map", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static Object getIfIsMap(String key, Object value) {
|
||||
if (value instanceof Map<?, ?>) {
|
||||
return ((Map<?, ?>) value).get(key);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,199 +0,0 @@
|
||||
package fr.xephi.authme.message.updater;
|
||||
|
||||
import ch.jalu.configme.configurationdata.ConfigurationData;
|
||||
import ch.jalu.configme.configurationdata.PropertyListBuilder;
|
||||
import ch.jalu.configme.properties.Property;
|
||||
import ch.jalu.configme.properties.StringProperty;
|
||||
import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder;
|
||||
import ch.jalu.configme.resource.PropertyReader;
|
||||
import ch.jalu.configme.resource.PropertyResource;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.io.Files;
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
import fr.xephi.authme.message.MessageKey;
|
||||
import fr.xephi.authme.output.ConsoleLoggerFactory;
|
||||
import fr.xephi.authme.util.FileUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static java.util.Collections.singletonList;
|
||||
|
||||
/**
|
||||
* Migrates the used messages file to a complete, up-to-date version when necessary.
|
||||
*/
|
||||
public class MessageUpdater {
|
||||
|
||||
private ConsoleLogger logger = ConsoleLoggerFactory.get(MessageUpdater.class);
|
||||
|
||||
/**
|
||||
* Applies any necessary migrations to the user's messages file and saves it if it has been modified.
|
||||
*
|
||||
* @param userFile the user's messages file (yml file in the plugin's folder)
|
||||
* @param localJarPath path to the messages file in the JAR for the same language (may not exist)
|
||||
* @param defaultJarPath path to the messages file in the JAR for the default language
|
||||
* @return true if the file has been migrated and saved, false if it is up-to-date
|
||||
*/
|
||||
public boolean migrateAndSave(File userFile, String localJarPath, String defaultJarPath) {
|
||||
JarMessageSource jarMessageSource = new JarMessageSource(localJarPath, defaultJarPath);
|
||||
return migrateAndSave(userFile, jarMessageSource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the migration.
|
||||
*
|
||||
* @param userFile the file to verify and migrate
|
||||
* @param jarMessageSource jar message source to get texts from if missing
|
||||
* @return true if the file has been migrated and saved, false if it is up-to-date
|
||||
*/
|
||||
private boolean migrateAndSave(File userFile, JarMessageSource jarMessageSource) {
|
||||
// YamlConfiguration escapes all special characters when saving, making the file hard to use, so use ConfigMe
|
||||
MessageKeyConfigurationData configurationData = createConfigurationData();
|
||||
PropertyResource userResource = new MigraterYamlFileResource(userFile);
|
||||
|
||||
PropertyReader reader = userResource.createReader();
|
||||
configurationData.initializeValues(reader);
|
||||
|
||||
// Step 1: Migrate any old keys in the file to the new paths
|
||||
boolean movedOldKeys = migrateOldKeys(reader, configurationData);
|
||||
// Step 2: Perform newer migrations
|
||||
boolean movedNewerKeys = migrateKeys(reader, configurationData);
|
||||
// Step 3: Take any missing messages from the message files shipped in the AuthMe JAR
|
||||
boolean addedMissingKeys = addMissingKeys(jarMessageSource, configurationData);
|
||||
|
||||
if (movedOldKeys || movedNewerKeys || addedMissingKeys) {
|
||||
backupMessagesFile(userFile);
|
||||
|
||||
userResource.exportProperties(configurationData);
|
||||
logger.debug("Successfully saved {0}", userFile);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean migrateKeys(PropertyReader propertyReader, MessageKeyConfigurationData configurationData) {
|
||||
return moveIfApplicable(propertyReader, configurationData,
|
||||
"misc.two_factor_create", MessageKey.TWO_FACTOR_CREATE);
|
||||
}
|
||||
|
||||
private static boolean moveIfApplicable(PropertyReader reader, MessageKeyConfigurationData configurationData,
|
||||
String oldPath, MessageKey messageKey) {
|
||||
if (configurationData.getMessage(messageKey) == null && reader.getString(oldPath) != null) {
|
||||
configurationData.setMessage(messageKey, reader.getString(oldPath));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean migrateOldKeys(PropertyReader propertyReader, MessageKeyConfigurationData configurationData) {
|
||||
boolean hasChange = OldMessageKeysMigrater.migrateOldPaths(propertyReader, configurationData);
|
||||
if (hasChange) {
|
||||
logger.info("Old keys have been moved to the new ones in your messages_xx.yml file");
|
||||
}
|
||||
return hasChange;
|
||||
}
|
||||
|
||||
private boolean addMissingKeys(JarMessageSource jarMessageSource, MessageKeyConfigurationData configurationData) {
|
||||
List<String> addedKeys = new ArrayList<>();
|
||||
for (Property<String> property : configurationData.getAllMessageProperties()) {
|
||||
final String key = property.getPath();
|
||||
if (configurationData.getValue(property) == null) {
|
||||
configurationData.setValue(property, jarMessageSource.getMessageFromJar(property));
|
||||
addedKeys.add(key);
|
||||
}
|
||||
}
|
||||
if (!addedKeys.isEmpty()) {
|
||||
logger.info(
|
||||
"Added " + addedKeys.size() + " missing keys to your messages_xx.yml file: " + addedKeys);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void backupMessagesFile(File messagesFile) {
|
||||
String backupName = FileUtils.createBackupFilePath(messagesFile);
|
||||
File backupFile = new File(backupName);
|
||||
try {
|
||||
Files.copy(messagesFile, backupFile);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("Could not back up '" + messagesFile + "' to '" + backupFile + "'", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs the {@link ConfigurationData} for exporting a messages file in its entirety.
|
||||
*
|
||||
* @return the configuration data to export with
|
||||
*/
|
||||
public static MessageKeyConfigurationData createConfigurationData() {
|
||||
Map<String, String> comments = ImmutableMap.<String, String>builder()
|
||||
.put("registration", "Registration")
|
||||
.put("password", "Password errors on registration")
|
||||
.put("login", "Login")
|
||||
.put("error", "Errors")
|
||||
.put("antibot", "AntiBot")
|
||||
.put("unregister", "Unregister")
|
||||
.put("misc", "Other messages")
|
||||
.put("session", "Session messages")
|
||||
.put("on_join_validation", "Error messages when joining")
|
||||
.put("email", "Email")
|
||||
.put("recovery", "Password recovery by email")
|
||||
.put("captcha", "Captcha")
|
||||
.put("verification", "Verification code")
|
||||
.put("time", "Time units")
|
||||
.put("two_factor", "Two-factor authentication")
|
||||
.build();
|
||||
|
||||
Set<String> addedKeys = new HashSet<>();
|
||||
MessageKeyPropertyListBuilder builder = new MessageKeyPropertyListBuilder();
|
||||
// Add one key per section based on the comments map above so that the order is clear
|
||||
for (String path : comments.keySet()) {
|
||||
MessageKey key = Arrays.stream(MessageKey.values()).filter(p -> p.getKey().startsWith(path + "."))
|
||||
.findFirst().orElseThrow(() -> new IllegalStateException(path));
|
||||
builder.addMessageKey(key);
|
||||
addedKeys.add(key.getKey());
|
||||
}
|
||||
// Add all remaining keys to the property list builder
|
||||
Arrays.stream(MessageKey.values())
|
||||
.filter(key -> !addedKeys.contains(key.getKey()))
|
||||
.forEach(builder::addMessageKey);
|
||||
|
||||
// Create ConfigurationData instance
|
||||
Map<String, List<String>> commentsMap = comments.entrySet().stream()
|
||||
.collect(Collectors.toMap(e -> e.getKey(), e -> singletonList(e.getValue())));
|
||||
return new MessageKeyConfigurationData(builder, commentsMap);
|
||||
}
|
||||
|
||||
static final class MessageKeyProperty extends StringProperty {
|
||||
|
||||
MessageKeyProperty(MessageKey messageKey) {
|
||||
super(messageKey.getKey(), "");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getFromReader(PropertyReader reader, ConvertErrorRecorder errorRecorder) {
|
||||
return reader.getString(getPath());
|
||||
}
|
||||
}
|
||||
|
||||
static final class MessageKeyPropertyListBuilder {
|
||||
|
||||
private PropertyListBuilder propertyListBuilder = new PropertyListBuilder();
|
||||
|
||||
void addMessageKey(MessageKey key) {
|
||||
propertyListBuilder.add(new MessageKeyProperty(key));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
List<MessageKeyProperty> getAllProperties() {
|
||||
return (List) propertyListBuilder.create();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
package fr.xephi.authme.message.updater;
|
||||
|
||||
import ch.jalu.configme.resource.PropertyReader;
|
||||
import ch.jalu.configme.resource.YamlFileResource;
|
||||
import org.yaml.snakeyaml.DumperOptions;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* Extension of {@link YamlFileResource} to fine-tune the export style.
|
||||
*/
|
||||
public class MigraterYamlFileResource extends YamlFileResource {
|
||||
|
||||
private Yaml singleQuoteYaml;
|
||||
|
||||
public MigraterYamlFileResource(File file) {
|
||||
super(file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PropertyReader createReader() {
|
||||
return MessageMigraterPropertyReader.loadFromFile(getFile());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Yaml createNewYaml() {
|
||||
if (singleQuoteYaml == null) {
|
||||
DumperOptions options = new DumperOptions();
|
||||
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
||||
options.setAllowUnicode(true);
|
||||
options.setDefaultScalarStyle(DumperOptions.ScalarStyle.SINGLE_QUOTED);
|
||||
// Overridden setting: don't split lines
|
||||
options.setSplitLines(false);
|
||||
singleQuoteYaml = new Yaml(options);
|
||||
}
|
||||
return singleQuoteYaml;
|
||||
}
|
||||
}
|
||||
@@ -1,169 +0,0 @@
|
||||
package fr.xephi.authme.message.updater;
|
||||
|
||||
import ch.jalu.configme.resource.PropertyReader;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import fr.xephi.authme.message.MessageKey;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static com.google.common.collect.ImmutableMap.of;
|
||||
|
||||
/**
|
||||
* Migrates message files from the old keys (before 5.5) to the new ones.
|
||||
*
|
||||
* @see <a href="https://github.com/AuthMe/AuthMeReloaded/issues/1467">Issue #1467</a>
|
||||
*/
|
||||
final class OldMessageKeysMigrater {
|
||||
|
||||
@VisibleForTesting
|
||||
static final Map<MessageKey, String> KEYS_TO_OLD_PATH = ImmutableMap.<MessageKey, String>builder()
|
||||
.put(MessageKey.LOGIN_SUCCESS, "login")
|
||||
.put(MessageKey.ERROR, "error")
|
||||
.put(MessageKey.DENIED_COMMAND, "denied_command")
|
||||
.put(MessageKey.SAME_IP_ONLINE, "same_ip_online")
|
||||
.put(MessageKey.DENIED_CHAT, "denied_chat")
|
||||
.put(MessageKey.KICK_ANTIBOT, "kick_antibot")
|
||||
.put(MessageKey.UNKNOWN_USER, "unknown_user")
|
||||
.put(MessageKey.NOT_LOGGED_IN, "not_logged_in")
|
||||
.put(MessageKey.USAGE_LOGIN, "usage_log")
|
||||
.put(MessageKey.WRONG_PASSWORD, "wrong_pwd")
|
||||
.put(MessageKey.UNREGISTERED_SUCCESS, "unregistered")
|
||||
.put(MessageKey.REGISTRATION_DISABLED, "reg_disabled")
|
||||
.put(MessageKey.ACCOUNT_NOT_ACTIVATED, "vb_nonActiv")
|
||||
.put(MessageKey.NAME_ALREADY_REGISTERED, "user_regged")
|
||||
.put(MessageKey.NO_PERMISSION, "no_perm")
|
||||
.put(MessageKey.LOGIN_MESSAGE, "login_msg")
|
||||
.put(MessageKey.REGISTER_MESSAGE, "reg_msg")
|
||||
.put(MessageKey.MAX_REGISTER_EXCEEDED, "max_reg")
|
||||
.put(MessageKey.USAGE_REGISTER, "usage_reg")
|
||||
.put(MessageKey.USAGE_UNREGISTER, "usage_unreg")
|
||||
.put(MessageKey.PASSWORD_CHANGED_SUCCESS, "pwd_changed")
|
||||
.put(MessageKey.PASSWORD_MATCH_ERROR, "password_error")
|
||||
.put(MessageKey.PASSWORD_IS_USERNAME_ERROR, "password_error_nick")
|
||||
.put(MessageKey.PASSWORD_UNSAFE_ERROR, "password_error_unsafe")
|
||||
.put(MessageKey.PASSWORD_CHARACTERS_ERROR, "password_error_chars")
|
||||
.put(MessageKey.SESSION_EXPIRED, "invalid_session")
|
||||
.put(MessageKey.MUST_REGISTER_MESSAGE, "reg_only")
|
||||
.put(MessageKey.ALREADY_LOGGED_IN_ERROR, "logged_in")
|
||||
.put(MessageKey.LOGOUT_SUCCESS, "logout")
|
||||
.put(MessageKey.USERNAME_ALREADY_ONLINE_ERROR, "same_nick")
|
||||
.put(MessageKey.REGISTER_SUCCESS, "registered")
|
||||
.put(MessageKey.INVALID_PASSWORD_LENGTH, "pass_len")
|
||||
.put(MessageKey.CONFIG_RELOAD_SUCCESS, "reload")
|
||||
.put(MessageKey.LOGIN_TIMEOUT_ERROR, "timeout")
|
||||
.put(MessageKey.USAGE_CHANGE_PASSWORD, "usage_changepassword")
|
||||
.put(MessageKey.INVALID_NAME_LENGTH, "name_len")
|
||||
.put(MessageKey.INVALID_NAME_CHARACTERS, "regex")
|
||||
.put(MessageKey.ADD_EMAIL_MESSAGE, "add_email")
|
||||
.put(MessageKey.FORGOT_PASSWORD_MESSAGE, "recovery_email")
|
||||
.put(MessageKey.USAGE_CAPTCHA, "usage_captcha")
|
||||
.put(MessageKey.CAPTCHA_WRONG_ERROR, "wrong_captcha")
|
||||
.put(MessageKey.CAPTCHA_SUCCESS, "valid_captcha")
|
||||
.put(MessageKey.CAPTCHA_FOR_REGISTRATION_REQUIRED, "captcha_for_registration")
|
||||
.put(MessageKey.REGISTER_CAPTCHA_SUCCESS, "register_captcha_valid")
|
||||
.put(MessageKey.KICK_FOR_VIP, "kick_forvip")
|
||||
.put(MessageKey.KICK_FULL_SERVER, "kick_fullserver")
|
||||
.put(MessageKey.USAGE_ADD_EMAIL, "usage_email_add")
|
||||
.put(MessageKey.USAGE_CHANGE_EMAIL, "usage_email_change")
|
||||
.put(MessageKey.USAGE_RECOVER_EMAIL, "usage_email_recovery")
|
||||
.put(MessageKey.INVALID_NEW_EMAIL, "new_email_invalid")
|
||||
.put(MessageKey.INVALID_OLD_EMAIL, "old_email_invalid")
|
||||
.put(MessageKey.INVALID_EMAIL, "email_invalid")
|
||||
.put(MessageKey.EMAIL_ADDED_SUCCESS, "email_added")
|
||||
.put(MessageKey.CONFIRM_EMAIL_MESSAGE, "email_confirm")
|
||||
.put(MessageKey.EMAIL_CHANGED_SUCCESS, "email_changed")
|
||||
.put(MessageKey.EMAIL_SHOW, "email_show")
|
||||
.put(MessageKey.SHOW_NO_EMAIL, "show_no_email")
|
||||
.put(MessageKey.RECOVERY_EMAIL_SENT_MESSAGE, "email_send")
|
||||
.put(MessageKey.COUNTRY_BANNED_ERROR, "country_banned")
|
||||
.put(MessageKey.ANTIBOT_AUTO_ENABLED_MESSAGE, "antibot_auto_enabled")
|
||||
.put(MessageKey.ANTIBOT_AUTO_DISABLED_MESSAGE, "antibot_auto_disabled")
|
||||
.put(MessageKey.EMAIL_ALREADY_USED_ERROR, "email_already_used")
|
||||
.put(MessageKey.TWO_FACTOR_CREATE, "two_factor_create")
|
||||
.put(MessageKey.NOT_OWNER_ERROR, "not_owner_error")
|
||||
.put(MessageKey.INVALID_NAME_CASE, "invalid_name_case")
|
||||
.put(MessageKey.TEMPBAN_MAX_LOGINS, "tempban_max_logins")
|
||||
.put(MessageKey.ACCOUNTS_OWNED_SELF, "accounts_owned_self")
|
||||
.put(MessageKey.ACCOUNTS_OWNED_OTHER, "accounts_owned_other")
|
||||
.put(MessageKey.KICK_FOR_ADMIN_REGISTER, "kicked_admin_registered")
|
||||
.put(MessageKey.INCOMPLETE_EMAIL_SETTINGS, "incomplete_email_settings")
|
||||
.put(MessageKey.EMAIL_SEND_FAILURE, "email_send_failure")
|
||||
.put(MessageKey.RECOVERY_CODE_SENT, "recovery_code_sent")
|
||||
.put(MessageKey.INCORRECT_RECOVERY_CODE, "recovery_code_incorrect")
|
||||
.put(MessageKey.RECOVERY_TRIES_EXCEEDED, "recovery_tries_exceeded")
|
||||
.put(MessageKey.RECOVERY_CODE_CORRECT, "recovery_code_correct")
|
||||
.put(MessageKey.RECOVERY_CHANGE_PASSWORD, "recovery_change_password")
|
||||
.put(MessageKey.CHANGE_PASSWORD_EXPIRED, "change_password_expired")
|
||||
.put(MessageKey.EMAIL_COOLDOWN_ERROR, "email_cooldown_error")
|
||||
.put(MessageKey.VERIFICATION_CODE_REQUIRED, "verification_code_required")
|
||||
.put(MessageKey.USAGE_VERIFICATION_CODE, "usage_verification_code")
|
||||
.put(MessageKey.INCORRECT_VERIFICATION_CODE, "incorrect_verification_code")
|
||||
.put(MessageKey.VERIFICATION_CODE_VERIFIED, "verification_code_verified")
|
||||
.put(MessageKey.VERIFICATION_CODE_ALREADY_VERIFIED, "verification_code_already_verified")
|
||||
.put(MessageKey.VERIFICATION_CODE_EXPIRED, "verification_code_expired")
|
||||
.put(MessageKey.VERIFICATION_CODE_EMAIL_NEEDED, "verification_code_email_needed")
|
||||
.put(MessageKey.SECOND, "second")
|
||||
.put(MessageKey.SECONDS, "seconds")
|
||||
.put(MessageKey.MINUTE, "minute")
|
||||
.put(MessageKey.MINUTES, "minutes")
|
||||
.put(MessageKey.HOUR, "hour")
|
||||
.put(MessageKey.HOURS, "hours")
|
||||
.put(MessageKey.DAY, "day")
|
||||
.put(MessageKey.DAYS, "days")
|
||||
.build();
|
||||
|
||||
private static final Map<MessageKey, Map<String, String>> PLACEHOLDER_REPLACEMENTS =
|
||||
ImmutableMap.<MessageKey, Map<String, String>>builder()
|
||||
.put(MessageKey.PASSWORD_CHARACTERS_ERROR, of("REG_EX", "%valid_chars"))
|
||||
.put(MessageKey.INVALID_NAME_CHARACTERS, of("REG_EX", "%valid_chars"))
|
||||
.put(MessageKey.USAGE_CAPTCHA, of("<theCaptcha>", "%captcha_code"))
|
||||
.put(MessageKey.CAPTCHA_FOR_REGISTRATION_REQUIRED, of("<theCaptcha>", "%captcha_code"))
|
||||
.put(MessageKey.CAPTCHA_WRONG_ERROR, of("THE_CAPTCHA", "%captcha_code"))
|
||||
.build();
|
||||
|
||||
private OldMessageKeysMigrater() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Migrates any existing old key paths to their new paths if no text has been defined for the new key.
|
||||
*
|
||||
* @param reader the property reader to get values from
|
||||
* @param configurationData the configuration data to write to
|
||||
* @return true if at least one message could be migrated, false otherwise
|
||||
*/
|
||||
static boolean migrateOldPaths(PropertyReader reader, MessageKeyConfigurationData configurationData) {
|
||||
boolean wasPropertyMoved = false;
|
||||
for (Map.Entry<MessageKey, String> migrationEntry : KEYS_TO_OLD_PATH.entrySet()) {
|
||||
wasPropertyMoved |= moveIfApplicable(reader, configurationData,
|
||||
migrationEntry.getKey(), migrationEntry.getValue());
|
||||
}
|
||||
return wasPropertyMoved;
|
||||
}
|
||||
|
||||
private static boolean moveIfApplicable(PropertyReader reader, MessageKeyConfigurationData configurationData,
|
||||
MessageKey messageKey, String oldPath) {
|
||||
if (configurationData.getMessage(messageKey) == null) {
|
||||
String textAtOldPath = reader.getString(oldPath);
|
||||
if (textAtOldPath != null) {
|
||||
textAtOldPath = replaceOldPlaceholders(messageKey, textAtOldPath);
|
||||
configurationData.setMessage(messageKey, textAtOldPath);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static String replaceOldPlaceholders(MessageKey key, String text) {
|
||||
Map<String, String> replacements = PLACEHOLDER_REPLACEMENTS.get(key);
|
||||
if (replacements == null) {
|
||||
return text;
|
||||
}
|
||||
|
||||
String newText = text;
|
||||
for (Map.Entry<String, String> replacement : replacements.entrySet()) {
|
||||
newText = newText.replace(replacement.getKey(), replacement.getValue());
|
||||
}
|
||||
return newText;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user