|
| 1 | +package fr.xephi.authme.datasource.converter; |
| 2 | + |
| 3 | +import fr.xephi.authme.ConsoleLogger; |
| 4 | +import fr.xephi.authme.data.auth.PlayerAuth; |
| 5 | +import fr.xephi.authme.datasource.DataSource; |
| 6 | +import fr.xephi.authme.output.ConsoleLoggerFactory; |
| 7 | +import fr.xephi.authme.security.crypts.HashedPassword; |
| 8 | +import org.bukkit.command.CommandSender; |
| 9 | + |
| 10 | +import javax.inject.Inject; |
| 11 | +import java.io.File; |
| 12 | +import java.sql.Connection; |
| 13 | +import java.sql.PreparedStatement; |
| 14 | +import java.sql.ResultSet; |
| 15 | +import java.sql.SQLException; |
| 16 | +import java.util.Locale; |
| 17 | + |
| 18 | +import static fr.xephi.authme.util.Utils.logAndSendMessage; |
| 19 | + |
| 20 | +/** |
| 21 | + * Converts data from OpeNLogin to AuthMe. |
| 22 | + * <p> |
| 23 | + * OpeNLogin stores accounts in a SQLite file at {@code plugins/OpeNLogin/accounts.db}. |
| 24 | + * No shared database is required; the converter reads the file directly. |
| 25 | + * <p> |
| 26 | + * OpeNLogin uses BCrypt exclusively. Set {@code passwordHash} to {@code BCRYPT} in AuthMe's |
| 27 | + * {@code config.yml} before running the conversion. |
| 28 | + */ |
| 29 | +public class OpeNLoginConverter extends AbstractSqlitePluginConverter { |
| 30 | + |
| 31 | + private static final String DB_PATH = "plugins/OpeNLogin/accounts.db"; |
| 32 | + private static final String QUERY = |
| 33 | + "SELECT name, realname, password, address, lastlogin, regdate FROM openlogin"; |
| 34 | + |
| 35 | + private final ConsoleLogger logger = ConsoleLoggerFactory.get(OpeNLoginConverter.class); |
| 36 | + |
| 37 | + @Inject |
| 38 | + OpeNLoginConverter(DataSource dataSource) { |
| 39 | + super(dataSource); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void execute(CommandSender sender) { |
| 44 | + File dbFile = new File(DB_PATH); |
| 45 | + if (!dbFile.exists()) { |
| 46 | + logAndSendMessage(sender, "OpeNLogin conversion failed: database file not found at " + DB_PATH); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + try (Connection conn = openConnection(dbFile); |
| 51 | + PreparedStatement ps = conn.prepareStatement(QUERY); |
| 52 | + ResultSet rs = ps.executeQuery()) { |
| 53 | + |
| 54 | + long imported = 0; |
| 55 | + long skipped = 0; |
| 56 | + while (rs.next()) { |
| 57 | + String name = rs.getString("name"); |
| 58 | + if (name == null || name.isEmpty()) { |
| 59 | + continue; |
| 60 | + } |
| 61 | + name = name.toLowerCase(Locale.ROOT); |
| 62 | + |
| 63 | + if (getDataSource().isAuthAvailable(name)) { |
| 64 | + ++skipped; |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + String hash = rs.getString("password"); |
| 69 | + if (hash == null || hash.isEmpty()) { |
| 70 | + logger.warning("No password for player '" + name + "', skipping"); |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + String realName = rs.getString("realname"); |
| 75 | + long lastLogin = rs.getLong("lastlogin"); |
| 76 | + |
| 77 | + PlayerAuth auth = PlayerAuth.builder() |
| 78 | + .name(name) |
| 79 | + .realName(realName != null && !realName.isEmpty() ? realName : name) |
| 80 | + .password(new HashedPassword(hash)) |
| 81 | + .lastIp(rs.getString("address")) |
| 82 | + .registrationDate(rs.getLong("regdate")) |
| 83 | + .lastLogin(lastLogin > 0 ? lastLogin : null) |
| 84 | + .build(); |
| 85 | + |
| 86 | + getDataSource().saveAuth(auth); |
| 87 | + ++imported; |
| 88 | + } |
| 89 | + |
| 90 | + logAndSendMessage(sender, "OpeNLogin conversion: " + imported + " account(s) imported, " |
| 91 | + + skipped + " skipped (already exist)"); |
| 92 | + |
| 93 | + } catch (SQLException e) { |
| 94 | + logAndSendMessage(sender, "OpeNLogin conversion failed: " + e.getMessage()); |
| 95 | + logger.logException("OpeNLogin conversion error:", e); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments