Package cleanup

- authme.cache to authme.data
- Rename PlayerData to LimboPlayer to match with LimboCache
- Move authme.converter to authme.datasource.converter
- Split output package into output and message
This commit is contained in:
Gabriele C
2016-10-04 21:49:14 +02:00
committed by ljacqu
parent 7e2912cc60
commit 58c42cf300
149 changed files with 491 additions and 475 deletions
@@ -0,0 +1,89 @@
package fr.xephi.authme.datasource.converter;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.datasource.DataSourceType;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import java.util.ArrayList;
import java.util.List;
/**
* Converts from one AuthMe data source type to another.
*
* @param <S> the source type to convert from
*/
public abstract class AbstractDataSourceConverter<S extends DataSource> implements Converter {
private DataSource destination;
private DataSourceType destinationType;
/**
* Constructor.
*
* @param destination the data source to convert to
* @param destinationType the data source type of the destination. The given data source is checked that its
* type corresponds to this type before the conversion is started, enabling us to just pass
* the current data source and letting this class check that the types correspond.
*/
public AbstractDataSourceConverter(DataSource destination, DataSourceType destinationType) {
this.destination = destination;
this.destinationType = destinationType;
}
// Implementation note: Because of ForceFlatToSqlite it is possible that the CommandSender is null,
// which is never the case when a converter is launched from the /authme converter command.
@Override
public void execute(CommandSender sender) {
if (!destinationType.equals(destination.getType())) {
if (sender != null) {
sender.sendMessage("Please configure your connection to "
+ destinationType + " and re-run this command");
}
return;
}
S source;
try {
source = getSource();
} catch (Exception e) {
logAndSendMessage(sender, "The data source to convert from could not be initialized");
ConsoleLogger.logException("Could not initialize source:", e);
return;
}
List<String> skippedPlayers = new ArrayList<>();
for (PlayerAuth auth : source.getAllAuths()) {
if (destination.isAuthAvailable(auth.getNickname())) {
skippedPlayers.add(auth.getNickname());
} else {
destination.saveAuth(auth);
destination.updateQuitLoc(auth);
}
}
if (!skippedPlayers.isEmpty()) {
logAndSendMessage(sender, "Skipped conversion for players which were already in "
+ destinationType + ": " + String.join(", ", skippedPlayers));
}
logAndSendMessage(sender, "Database successfully converted from " + source.getType()
+ " to " + destinationType);
}
/**
* @return the data source to convert from
* @throws Exception during initialization of source
*/
protected abstract S getSource() throws Exception;
private static void logAndSendMessage(CommandSender sender, String message) {
ConsoleLogger.info(message);
// Make sure sender is not console user, which will see the message from ConsoleLogger already
if (sender != null && !(sender instanceof ConsoleCommandSender)) {
sender.sendMessage(message);
}
}
}
@@ -0,0 +1,16 @@
package fr.xephi.authme.datasource.converter;
import org.bukkit.command.CommandSender;
/**
* Interface for AuthMe converters.
*/
public interface Converter {
/**
* Execute the conversion.
*
* @param sender the sender who initialized the conversion
*/
void execute(CommandSender sender);
}
@@ -0,0 +1,69 @@
package fr.xephi.authme.datasource.converter;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.initialization.DataFolder;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.ConverterSettings;
import org.bukkit.command.CommandSender;
import javax.inject.Inject;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
/**
* Converter for CrazyLogin to AuthMe.
*/
public class CrazyLoginConverter implements Converter {
private final DataSource database;
private final Settings settings;
private final File dataFolder;
@Inject
CrazyLoginConverter(@DataFolder File dataFolder, DataSource dataSource, Settings settings) {
this.dataFolder = dataFolder;
this.database = dataSource;
this.settings = settings;
}
@Override
public void execute(CommandSender sender) {
String fileName = settings.getProperty(ConverterSettings.CRAZYLOGIN_FILE_NAME);
File source = new File(dataFolder, fileName);
if (!source.exists()) {
sender.sendMessage("CrazyLogin file not found, please put " + fileName + " in AuthMe folder!");
return;
}
String line;
try (BufferedReader users = new BufferedReader(new FileReader(source))) {
while ((line = users.readLine()) != null) {
if (line.contains("|")) {
String[] args = line.split("\\|");
if (args.length < 2 || "name".equalsIgnoreCase(args[0])) {
continue;
}
String playerName = args[0];
String password = args[1];
if (password != null) {
PlayerAuth auth = PlayerAuth.builder()
.name(playerName.toLowerCase())
.realName(playerName)
.password(password, null)
.build();
database.saveAuth(auth);
}
}
}
ConsoleLogger.info("CrazyLogin database has been imported correctly");
} catch (IOException ex) {
ConsoleLogger.warning("Can't open the crazylogin database file! Does it exist?");
ConsoleLogger.logException("Encountered", ex);
}
}
}
@@ -0,0 +1,28 @@
package fr.xephi.authme.datasource.converter;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.datasource.FlatFile;
/**
* Mandatory migration from the deprecated flat file datasource to SQLite.
*/
public class ForceFlatToSqlite extends AbstractDataSourceConverter<FlatFile> {
private final FlatFile source;
/**
* Constructor.
*
* @param source The datasource to convert (flatfile)
* @param destination The datasource to copy the data to (sqlite)
*/
public ForceFlatToSqlite(FlatFile source, DataSource destination) {
super(destination, destination.getType());
this.source = source;
}
@Override
public FlatFile getSource() {
return source;
}
}
@@ -0,0 +1,28 @@
package fr.xephi.authme.datasource.converter;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.datasource.DataSourceType;
import fr.xephi.authme.datasource.MySQL;
import fr.xephi.authme.settings.Settings;
import javax.inject.Inject;
import java.sql.SQLException;
/**
* Converts from MySQL to SQLite.
*/
public class MySqlToSqlite extends AbstractDataSourceConverter<MySQL> {
private final Settings settings;
@Inject
MySqlToSqlite(DataSource dataSource, Settings settings) {
super(dataSource, DataSourceType.SQLITE);
this.settings = settings;
}
@Override
protected MySQL getSource() throws SQLException, ClassNotFoundException {
return new MySQL(settings);
}
}
@@ -0,0 +1,95 @@
package fr.xephi.authme.datasource.converter;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.initialization.DataFolder;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.ConverterSettings;
import org.bukkit.command.CommandSender;
import javax.inject.Inject;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map.Entry;
/**
* @author Xephi59
*/
public class RakamakConverter implements Converter {
private final DataSource database;
private final Settings settings;
private final File pluginFolder;
private final PasswordSecurity passwordSecurity;
@Inject
RakamakConverter(@DataFolder File dataFolder, DataSource dataSource, Settings settings,
PasswordSecurity passwordSecurity) {
this.database = dataSource;
this.settings = settings;
this.pluginFolder = dataFolder;
this.passwordSecurity = passwordSecurity;
}
@Override
// TODO ljacqu 20151229: Restructure this into smaller portions
public void execute(CommandSender sender) {
boolean useIP = settings.getProperty(ConverterSettings.RAKAMAK_USE_IP);
String fileName = settings.getProperty(ConverterSettings.RAKAMAK_FILE_NAME);
String ipFileName = settings.getProperty(ConverterSettings.RAKAMAK_IP_FILE_NAME);
File source = new File(pluginFolder, fileName);
File ipfiles = new File(pluginFolder, ipFileName);
HashMap<String, String> playerIP = new HashMap<>();
HashMap<String, HashedPassword> playerPSW = new HashMap<>();
try {
BufferedReader users;
BufferedReader ipFile;
ipFile = new BufferedReader(new FileReader(ipfiles));
String line;
if (useIP) {
String tempLine;
while ((tempLine = ipFile.readLine()) != null) {
if (tempLine.contains("=")) {
String[] args = tempLine.split("=");
playerIP.put(args[0], args[1]);
}
}
}
ipFile.close();
users = new BufferedReader(new FileReader(source));
while ((line = users.readLine()) != null) {
if (line.contains("=")) {
String[] arguments = line.split("=");
HashedPassword hashedPassword = passwordSecurity.computeHash(arguments[1], arguments[0]);
playerPSW.put(arguments[0], hashedPassword);
}
}
users.close();
for (Entry<String, HashedPassword> m : playerPSW.entrySet()) {
String playerName = m.getKey();
HashedPassword psw = playerPSW.get(playerName);
String ip = useIP ? playerIP.get(playerName) : "127.0.0.1";
PlayerAuth auth = PlayerAuth.builder()
.name(playerName)
.realName(playerName)
.ip(ip)
.password(psw)
.lastLogin(0)
.build();
database.saveAuth(auth);
}
ConsoleLogger.info("Rakamak database has been imported correctly");
sender.sendMessage("Rakamak database has been imported correctly");
} catch (IOException ex) {
ConsoleLogger.logException("Can't open the rakamak database file! Does it exist?", ex);
}
}
}
@@ -0,0 +1,55 @@
package fr.xephi.authme.datasource.converter;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import javax.inject.Inject;
import java.io.File;
import static fr.xephi.authme.util.FileUtils.makePath;
public class RoyalAuthConverter implements Converter {
private static final String LAST_LOGIN_PATH = "timestamps.quit";
private static final String PASSWORD_PATH = "login.password";
private final AuthMe plugin;
private final DataSource dataSource;
@Inject
RoyalAuthConverter(AuthMe plugin, DataSource dataSource) {
this.plugin = plugin;
this.dataSource = dataSource;
}
@Override
public void execute(CommandSender sender) {
for (OfflinePlayer player : plugin.getServer().getOfflinePlayers()) {
try {
String name = player.getName().toLowerCase();
File file = new File(makePath(".", "plugins", "RoyalAuth", "userdata", name + ".yml"));
if (dataSource.isAuthAvailable(name) || !file.exists()) {
continue;
}
FileConfiguration configuration = YamlConfiguration.loadConfiguration(file);
PlayerAuth auth = PlayerAuth.builder()
.name(name)
.password(configuration.getString(PASSWORD_PATH), null)
.lastLogin(configuration.getLong(LAST_LOGIN_PATH))
.realName(player.getName())
.build();
dataSource.saveAuth(auth);
} catch (Exception e) {
ConsoleLogger.logException("Error while trying to import " + player.getName() + " RoyalAuth data", e);
}
}
}
}
@@ -0,0 +1,28 @@
package fr.xephi.authme.datasource.converter;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.datasource.DataSourceType;
import fr.xephi.authme.datasource.SQLite;
import fr.xephi.authme.settings.Settings;
import javax.inject.Inject;
import java.sql.SQLException;
/**
* Converts from SQLite to MySQL.
*/
public class SqliteToSql extends AbstractDataSourceConverter<SQLite> {
private final Settings settings;
@Inject
SqliteToSql(Settings settings, DataSource dataSource) {
super(dataSource, DataSourceType.MYSQL);
this.settings = settings;
}
@Override
protected SQLite getSource() throws SQLException, ClassNotFoundException {
return new SQLite(settings);
}
}
@@ -0,0 +1,77 @@
package fr.xephi.authme.datasource.converter;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.initialization.DataFolder;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.UUID;
import static fr.xephi.authme.util.FileUtils.makePath;
public class vAuthConverter implements Converter {
private final DataSource dataSource;
private final File vAuthPasswordsFile;
@Inject
vAuthConverter(@DataFolder File dataFolder, DataSource dataSource) {
vAuthPasswordsFile = new File(dataFolder.getParent(), makePath("vAuth", "passwords.yml"));
this.dataSource = dataSource;
}
@Override
public void execute(CommandSender sender) {
try (Scanner scanner = new Scanner(vAuthPasswordsFile)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String name = line.split(": ")[0];
String password = line.split(": ")[1];
PlayerAuth auth;
if (isUuidInstance(password)) {
String pname;
try {
pname = Bukkit.getOfflinePlayer(UUID.fromString(name)).getName();
} catch (Exception | NoSuchMethodError e) {
pname = getName(UUID.fromString(name));
}
if (pname == null)
continue;
auth = PlayerAuth.builder()
.name(pname.toLowerCase())
.realName(pname)
.password(password, null).build();
} else {
auth = PlayerAuth.builder()
.name(name.toLowerCase())
.realName(name)
.password(password, null).build();
}
dataSource.saveAuth(auth);
}
} catch (IOException e) {
ConsoleLogger.logException("Error while trying to import some vAuth data", e);
}
}
private static boolean isUuidInstance(String s) {
return s.length() > 8 && s.charAt(8) == '-';
}
private String getName(UUID uuid) {
for (OfflinePlayer op : Bukkit.getOfflinePlayers()) {
if (op.getUniqueId().compareTo(uuid) == 0) {
return op.getName();
}
}
return null;
}
}
@@ -0,0 +1,146 @@
package fr.xephi.authme.datasource.converter;
import de.luricos.bukkit.xAuth.database.DatabaseTables;
import de.luricos.bukkit.xAuth.utils.xAuthLog;
import de.luricos.bukkit.xAuth.xAuth;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.initialization.DataFolder;
import fr.xephi.authme.util.CollectionUtils;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.PluginManager;
import javax.inject.Inject;
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import static fr.xephi.authme.util.FileUtils.makePath;
public class xAuthConverter implements Converter {
@Inject
@DataFolder
private File dataFolder;
@Inject
private DataSource database;
@Inject
private PluginManager pluginManager;
xAuthConverter() {
}
@Override
public void execute(CommandSender sender) {
try {
Class.forName("de.luricos.bukkit.xAuth.xAuth");
convert(sender);
} catch (ClassNotFoundException ce) {
sender.sendMessage("xAuth has not been found, please put xAuth.jar in your plugin folder and restart!");
}
}
private void convert(CommandSender sender) {
if (pluginManager.getPlugin("xAuth") == null) {
sender.sendMessage("[AuthMe] xAuth plugin not found");
return;
}
// TODO ljacqu 20160702: xAuthDb is not used except for the existence check -- is this intended?
File xAuthDb = new File(dataFolder.getParent(), makePath("xAuth", "xAuth.h2.db"));
if (!xAuthDb.exists()) {
sender.sendMessage("[AuthMe] xAuth H2 database not found, checking for MySQL or SQLite data...");
}
List<Integer> players = getXAuthPlayers();
if (CollectionUtils.isEmpty(players)) {
sender.sendMessage("[AuthMe] Error while importing xAuthPlayers: did not find any players");
return;
}
sender.sendMessage("[AuthMe] Starting import...");
for (int id : players) {
String pl = getIdPlayer(id);
String psw = getPassword(id);
if (psw != null && !psw.isEmpty() && pl != null) {
PlayerAuth auth = PlayerAuth.builder()
.name(pl.toLowerCase())
.realName(pl)
.password(psw, null).build();
database.saveAuth(auth);
}
}
sender.sendMessage("[AuthMe] Successfully converted from xAuth database");
}
private String getIdPlayer(int id) {
String realPass = "";
Connection conn = xAuth.getPlugin().getDatabaseController().getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = String.format("SELECT `playername` FROM `%s` WHERE `id` = ?",
xAuth.getPlugin().getDatabaseController().getTable(DatabaseTables.ACCOUNT));
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
rs = ps.executeQuery();
if (!rs.next())
return null;
realPass = rs.getString("playername").toLowerCase();
} catch (SQLException e) {
xAuthLog.severe("Failed to retrieve name for account: " + id, e);
return null;
} finally {
xAuth.getPlugin().getDatabaseController().close(conn, ps, rs);
}
return realPass;
}
private List<Integer> getXAuthPlayers() {
List<Integer> xP = new ArrayList<>();
Connection conn = xAuth.getPlugin().getDatabaseController().getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = String.format("SELECT * FROM `%s`",
xAuth.getPlugin().getDatabaseController().getTable(DatabaseTables.ACCOUNT));
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
xP.add(rs.getInt("id"));
}
} catch (SQLException e) {
xAuthLog.severe("Cannot import xAuthPlayers", e);
return new ArrayList<>();
} finally {
xAuth.getPlugin().getDatabaseController().close(conn, ps, rs);
}
return xP;
}
private String getPassword(int accountId) {
String realPass = "";
Connection conn = xAuth.getPlugin().getDatabaseController().getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = String.format("SELECT `password`, `pwtype` FROM `%s` WHERE `id` = ?",
xAuth.getPlugin().getDatabaseController().getTable(DatabaseTables.ACCOUNT));
ps = conn.prepareStatement(sql);
ps.setInt(1, accountId);
rs = ps.executeQuery();
if (!rs.next())
return null;
realPass = rs.getString("password");
} catch (SQLException e) {
xAuthLog.severe("Failed to retrieve password hash for account: " + accountId, e);
return null;
} finally {
xAuth.getPlugin().getDatabaseController().close(conn, ps, rs);
}
return realPass;
}
}