From 1562cb76156c3b778f535f70a45466c132be6ea7 Mon Sep 17 00:00:00 2001 From: DNx5 Date: Tue, 3 Nov 2015 10:54:26 +0700 Subject: [PATCH 1/7] missed return statement. --- src/main/java/fr/xephi/authme/process/join/AsyncronousJoin.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/fr/xephi/authme/process/join/AsyncronousJoin.java b/src/main/java/fr/xephi/authme/process/join/AsyncronousJoin.java index 04317f771..f4831e961 100644 --- a/src/main/java/fr/xephi/authme/process/join/AsyncronousJoin.java +++ b/src/main/java/fr/xephi/authme/process/join/AsyncronousJoin.java @@ -70,6 +70,7 @@ public class AsyncronousJoin { } }); + return; } final String ip = plugin.getIP(player); From ce432aa25aa1678eb95f9c9d032b1da17a8a106d Mon Sep 17 00:00:00 2001 From: DNx5 Date: Tue, 3 Nov 2015 12:38:24 +0700 Subject: [PATCH 2/7] add exception to datasource method signature. --- .../executable/authme/AccountsCommand.java | 14 ++- .../executable/email/ChangeEmailCommand.java | 98 +++++++++-------- .../xephi/authme/commands/AdminCommand.java | 12 +- .../authme/datasource/CacheDataSource.java | 46 ++++---- .../xephi/authme/datasource/DataSource.java | 5 +- .../fr/xephi/authme/datasource/MySQL.java | 11 +- .../authme/datasource/SQLite_HIKARI.java | 20 +--- .../fr/xephi/authme/process/Management.java | 4 +- ...ronousRegister.java => AsyncRegister.java} | 103 ++++++++---------- ...ter.java => ProcessSyncEmailRegister.java} | 4 +- 10 files changed, 154 insertions(+), 163 deletions(-) rename src/main/java/fr/xephi/authme/process/register/{AsyncronousRegister.java => AsyncRegister.java} (62%) rename src/main/java/fr/xephi/authme/process/register/{ProcessSyncronousEmailRegister.java => ProcessSyncEmailRegister.java} (93%) diff --git a/src/main/java/fr/xephi/authme/command/executable/authme/AccountsCommand.java b/src/main/java/fr/xephi/authme/command/executable/authme/AccountsCommand.java index aa8753c42..abe066612 100644 --- a/src/main/java/fr/xephi/authme/command/executable/authme/AccountsCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/authme/AccountsCommand.java @@ -2,6 +2,7 @@ package fr.xephi.authme.command.executable.authme; import java.util.List; +import fr.xephi.authme.ConsoleLogger; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; @@ -79,11 +80,20 @@ public class AccountsCommand extends ExecutableCommand { }); return true; } else { - Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { + Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { @Override public void run() { + List accountList; + try { + accountList = plugin.database.getAllAuthsByIp(playerQueryFinal); + } catch (Exception e) { + ConsoleLogger.showError(e.getMessage()); + ConsoleLogger.writeStackTrace(e); + m.send(sender, "error"); + return; + } + StringBuilder message = new StringBuilder("[AuthMe] "); - List accountList = plugin.database.getAllAuthsByIp(playerQueryFinal); if (accountList == null || accountList.isEmpty()) { sender.sendMessage("[AuthMe] This IP does not exist in the database"); return; diff --git a/src/main/java/fr/xephi/authme/command/executable/email/ChangeEmailCommand.java b/src/main/java/fr/xephi/authme/command/executable/email/ChangeEmailCommand.java index b7f717362..0b5d7e3c0 100644 --- a/src/main/java/fr/xephi/authme/command/executable/email/ChangeEmailCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/email/ChangeEmailCommand.java @@ -1,27 +1,26 @@ package fr.xephi.authme.command.executable.email; -import java.util.Arrays; - -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - import fr.xephi.authme.AuthMe; +import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.command.CommandParts; import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.settings.Messages; import fr.xephi.authme.settings.Settings; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.util.Arrays; public class ChangeEmailCommand extends ExecutableCommand { /** * Execute the command. * - * @param sender The command sender. + * @param sender The command sender. * @param commandReference The command reference. * @param commandArguments The command arguments. - * * @return True if the command was executed successfully, false otherwise. */ @Override @@ -37,7 +36,7 @@ public class ChangeEmailCommand extends ExecutableCommand { String playerMailNew = commandArguments.get(1); // Make sure the current command executor is a player - if(!(sender instanceof Player)) { + if (!(sender instanceof Player)) { return true; } @@ -46,44 +45,51 @@ public class ChangeEmailCommand extends ExecutableCommand { final String playerName = player.getName(); // Command logic - if (Settings.getmaxRegPerEmail > 0) { - if (!plugin.authmePermissible(sender, "authme.allow2accounts") && plugin.database.getAllAuthsByEmail(playerMailNew).size() >= Settings.getmaxRegPerEmail) { - m.send(player, "max_reg"); - return true; - } - } - if (PlayerCache.getInstance().isAuthenticated(playerName)) { - PlayerAuth auth = PlayerCache.getInstance().getAuth(playerName); - if (auth.getEmail() == null || auth.getEmail().equals("your@email.com") || auth.getEmail().isEmpty()) { - m.send(player, "usage_email_add"); - return true; - } - if (!playerMailOld.equals(auth.getEmail())) { - m.send(player, "old_email_invalid"); - return true; - } - if (!Settings.isEmailCorrect(playerMailNew)) { - m.send(player, "new_email_invalid"); - return true; - } - auth.setEmail(playerMailNew); - if (!plugin.database.updateEmail(auth)) { - m.send(player, "error"); - return true; - } - PlayerCache.getInstance().updatePlayer(auth); - m.send(player, "email_changed"); - player.sendMessage(Arrays.toString(m.send("email_defined")) + auth.getEmail()); - } else if (PlayerCache.getInstance().isAuthenticated(playerName)) { - m.send(player, "email_confirm"); - } else { - if (!plugin.database.isAuthAvailable(playerName)) { - m.send(player, "login_msg"); - } else { - m.send(player, "reg_email_msg"); - } - } - return true; + try { + if (Settings.getmaxRegPerEmail > 0) { + if (!plugin.authmePermissible(sender, "authme.allow2accounts") && plugin.database.getAllAuthsByEmail(playerMailNew).size() >= Settings.getmaxRegPerEmail) { + m.send(player, "max_reg"); + return true; + } + } + if (PlayerCache.getInstance().isAuthenticated(playerName)) { + PlayerAuth auth = PlayerCache.getInstance().getAuth(playerName); + if (auth.getEmail() == null || auth.getEmail().equals("your@email.com") || auth.getEmail().isEmpty()) { + m.send(player, "usage_email_add"); + return true; + } + if (!playerMailOld.equals(auth.getEmail())) { + m.send(player, "old_email_invalid"); + return true; + } + if (!Settings.isEmailCorrect(playerMailNew)) { + m.send(player, "new_email_invalid"); + return true; + } + auth.setEmail(playerMailNew); + if (!plugin.database.updateEmail(auth)) { + m.send(player, "error"); + return true; + } + PlayerCache.getInstance().updatePlayer(auth); + m.send(player, "email_changed"); + player.sendMessage(Arrays.toString(m.send("email_defined")) + auth.getEmail()); + } else if (PlayerCache.getInstance().isAuthenticated(playerName)) { + m.send(player, "email_confirm"); + } else { + if (!plugin.database.isAuthAvailable(playerName)) { + m.send(player, "login_msg"); + } else { + m.send(player, "reg_email_msg"); + } + } + return true; + } catch (Exception e) { + ConsoleLogger.showError(e.getMessage()); + ConsoleLogger.writeStackTrace(e); + m.send(sender, "error"); + return false; + } } } diff --git a/src/main/java/fr/xephi/authme/commands/AdminCommand.java b/src/main/java/fr/xephi/authme/commands/AdminCommand.java index 5b0345ccb..fa27ee311 100644 --- a/src/main/java/fr/xephi/authme/commands/AdminCommand.java +++ b/src/main/java/fr/xephi/authme/commands/AdminCommand.java @@ -194,7 +194,7 @@ public class AdminCommand implements CommandExecutor { return true; } else { final String[] arguments = args; - Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { + Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { @Override public void run() { StringBuilder message = new StringBuilder("[AuthMe] "); @@ -202,7 +202,15 @@ public class AdminCommand implements CommandExecutor { sender.sendMessage("[AuthMe] Please put a valid IP"); return; } - List accountList = plugin.database.getAllAuthsByIp(arguments[1]); + List accountList = null; + try { + accountList = plugin.database.getAllAuthsByIp(arguments[1]); + } catch (Exception e) { + ConsoleLogger.showError(e.getMessage()); + ConsoleLogger.writeStackTrace(e); + m.send(sender, "error"); + return; + } if (accountList == null || accountList.isEmpty()) { sender.sendMessage("[AuthMe] This IP does not exist in the database"); return; diff --git a/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java b/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java index 0032b9a08..40ed684cb 100644 --- a/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java +++ b/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java @@ -1,18 +1,18 @@ package fr.xephi.authme.datasource; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -import org.bukkit.entity.Player; - import fr.xephi.authme.AuthMe; import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.util.Utils; +import org.bukkit.entity.Player; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.Callable; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; public class CacheDataSource implements DataSource { @@ -281,25 +281,21 @@ public class CacheDataSource implements DataSource { } @Override - public synchronized List getAllAuthsByIp(String ip) { - List result = new ArrayList<>(); - for (Map.Entry stringPlayerAuthEntry : cache.entrySet()) { - PlayerAuth p = stringPlayerAuthEntry.getValue(); - if (p.getIp().equals(ip)) - result.add(p.getNickname()); - } - return result; + public synchronized List getAllAuthsByIp(final String ip) throws Exception { + return exec.submit(new Callable>() { + public List call() throws Exception { + return source.getAllAuthsByIp(ip); + } + }).get(); } @Override - public synchronized List getAllAuthsByEmail(String email) { - List result = new ArrayList<>(); - for (Map.Entry stringPlayerAuthEntry : cache.entrySet()) { - PlayerAuth p = stringPlayerAuthEntry.getValue(); - if (p.getEmail().equals(email)) - result.add(p.getNickname()); - } - return result; + public synchronized List getAllAuthsByEmail(final String email) throws Exception { + return exec.submit(new Callable>() { + public List call() throws Exception { + return source.getAllAuthsByEmail(email); + } + }).get(); } @Override diff --git a/src/main/java/fr/xephi/authme/datasource/DataSource.java b/src/main/java/fr/xephi/authme/datasource/DataSource.java index 184a4fe94..40c3c883e 100644 --- a/src/main/java/fr/xephi/authme/datasource/DataSource.java +++ b/src/main/java/fr/xephi/authme/datasource/DataSource.java @@ -1,5 +1,6 @@ package fr.xephi.authme.datasource; +import java.sql.SQLException; import java.util.List; import fr.xephi.authme.cache.auth.PlayerAuth; @@ -35,9 +36,9 @@ public interface DataSource { List getAllAuthsByName(PlayerAuth auth); - List getAllAuthsByIp(String ip); + List getAllAuthsByIp(String ip) throws Exception; - List getAllAuthsByEmail(String email); + List getAllAuthsByEmail(String email) throws Exception; boolean updateEmail(PlayerAuth auth); diff --git a/src/main/java/fr/xephi/authme/datasource/MySQL.java b/src/main/java/fr/xephi/authme/datasource/MySQL.java index 8fdc2371d..e04794685 100644 --- a/src/main/java/fr/xephi/authme/datasource/MySQL.java +++ b/src/main/java/fr/xephi/authme/datasource/MySQL.java @@ -751,6 +751,7 @@ public class MySQL implements DataSource { o.close(); } catch (Exception ex) { ConsoleLogger.showError(ex.getMessage()); + ConsoleLogger.writeStackTrace(ex); } } } @@ -808,14 +809,13 @@ public class MySQL implements DataSource { } @Override - public synchronized List getAllAuthsByEmail(String email) { - Connection con = null; + public synchronized List getAllAuthsByEmail(String email) throws SQLException { + final Connection con = getConnection(); PreparedStatement pst = null; ResultSet rs = null; List countEmail = new ArrayList<>(); + try { - if ((con = getConnection()) == null) - return countEmail; pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnEmail + "=?;"); pst.setString(1, email); rs = pst.executeQuery(); @@ -823,9 +823,6 @@ public class MySQL implements DataSource { countEmail.add(rs.getString(columnName)); } return countEmail; - } catch (Exception ex) { - ConsoleLogger.showError(ex.getMessage()); - return new ArrayList<>(); } finally { close(rs); close(pst); diff --git a/src/main/java/fr/xephi/authme/datasource/SQLite_HIKARI.java b/src/main/java/fr/xephi/authme/datasource/SQLite_HIKARI.java index 5e531f3c9..87af9fe9b 100644 --- a/src/main/java/fr/xephi/authme/datasource/SQLite_HIKARI.java +++ b/src/main/java/fr/xephi/authme/datasource/SQLite_HIKARI.java @@ -472,13 +472,12 @@ public class SQLite_HIKARI implements DataSource { } @Override - public List getAllAuthsByIp(String ip) { - Connection con = null; + public List getAllAuthsByIp(String ip) throws SQLException { + final Connection con = getConnection(); PreparedStatement pst = null; ResultSet rs = null; List countIp = new ArrayList<>(); try { - con = getConnection(); pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;"); pst.setString(1, ip); rs = pst.executeQuery(); @@ -486,11 +485,6 @@ public class SQLite_HIKARI implements DataSource { countIp.add(rs.getString(columnName)); } return countIp; - } catch (SQLException ex) { - ConsoleLogger.showError(ex.getMessage()); - return new ArrayList<>(); - } catch (NullPointerException npe) { - return new ArrayList<>(); } finally { close(rs); close(pst); @@ -499,13 +493,12 @@ public class SQLite_HIKARI implements DataSource { } @Override - public List getAllAuthsByEmail(String email) { - Connection con = null; + public List getAllAuthsByEmail(String email) throws SQLException { + final Connection con = getConnection(); PreparedStatement pst = null; ResultSet rs = null; List countEmail = new ArrayList<>(); try { - con = getConnection(); pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnEmail + "=?;"); pst.setString(1, email); rs = pst.executeQuery(); @@ -513,11 +506,6 @@ public class SQLite_HIKARI implements DataSource { countEmail.add(rs.getString(columnName)); } return countEmail; - } catch (SQLException ex) { - ConsoleLogger.showError(ex.getMessage()); - return new ArrayList<>(); - } catch (NullPointerException npe) { - return new ArrayList<>(); } finally { close(rs); close(pst); diff --git a/src/main/java/fr/xephi/authme/process/Management.java b/src/main/java/fr/xephi/authme/process/Management.java index 1276012ab..f32ae8574 100644 --- a/src/main/java/fr/xephi/authme/process/Management.java +++ b/src/main/java/fr/xephi/authme/process/Management.java @@ -9,7 +9,7 @@ import fr.xephi.authme.process.join.AsyncronousJoin; import fr.xephi.authme.process.login.AsyncronousLogin; import fr.xephi.authme.process.logout.AsyncronousLogout; import fr.xephi.authme.process.quit.AsyncronousQuit; -import fr.xephi.authme.process.register.AsyncronousRegister; +import fr.xephi.authme.process.register.AsyncRegister; import fr.xephi.authme.process.unregister.AsyncronousUnregister; import fr.xephi.authme.security.RandomString; import fr.xephi.authme.settings.Settings; @@ -46,7 +46,7 @@ public class Management { @Override public void run() { - new AsyncronousRegister(player, password, email, plugin, plugin.database).process(); + new AsyncRegister(player, password, email, plugin, plugin.database).process(); } }); } diff --git a/src/main/java/fr/xephi/authme/process/register/AsyncronousRegister.java b/src/main/java/fr/xephi/authme/process/register/AsyncRegister.java similarity index 62% rename from src/main/java/fr/xephi/authme/process/register/AsyncronousRegister.java rename to src/main/java/fr/xephi/authme/process/register/AsyncRegister.java index cc516697b..9f5bdf775 100644 --- a/src/main/java/fr/xephi/authme/process/register/AsyncronousRegister.java +++ b/src/main/java/fr/xephi/authme/process/register/AsyncRegister.java @@ -1,10 +1,5 @@ package fr.xephi.authme.process.register; -import java.security.NoSuchAlgorithmException; -import java.util.Date; - -import org.bukkit.entity.Player; - import fr.xephi.authme.AuthMe; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.cache.auth.PlayerAuth; @@ -13,95 +8,90 @@ import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.security.PasswordSecurity; import fr.xephi.authme.settings.Messages; import fr.xephi.authme.settings.Settings; +import org.bukkit.entity.Player; -public class AsyncronousRegister { +import java.security.NoSuchAlgorithmException; +import java.util.Date; + +public class AsyncRegister { protected Player player; protected String name; protected String password; protected String email = ""; - protected boolean allowRegister; private AuthMe plugin; private DataSource database; private Messages m = Messages.getInstance(); - public AsyncronousRegister(Player player, String password, String email, - AuthMe plugin, DataSource data) { + public AsyncRegister(Player player, String password, String email, + AuthMe plugin, DataSource data) { this.player = player; this.password = password; name = player.getName().toLowerCase(); this.email = email; this.plugin = plugin; this.database = data; - this.allowRegister = true; } protected String getIp() { return plugin.getIP(player); } - protected void preRegister() { + protected boolean preRegisterCheck() throws Exception { String lowpass = password.toLowerCase(); if (PlayerCache.getInstance().isAuthenticated(name)) { m.send(player, "logged_in"); - allowRegister = false; - } - - else if (!Settings.isRegistrationEnabled) { + return false; + } else if (!Settings.isRegistrationEnabled) { m.send(player, "reg_disabled"); - allowRegister = false; - } - - else if (lowpass.contains("delete") || lowpass.contains("where") || lowpass.contains("insert") || lowpass.contains("modify") || lowpass.contains("from") || lowpass.contains("select") || lowpass.contains(";") || lowpass.contains("null") || !lowpass.matches(Settings.getPassRegex)) { + return false; + } else if (lowpass.contains("delete") || lowpass.contains("where") || lowpass.contains("insert") || lowpass.contains("modify") || lowpass.contains("from") || lowpass.contains("select") || lowpass.contains(";") || lowpass.contains("null") || !lowpass.matches(Settings.getPassRegex)) { m.send(player, "password_error"); - allowRegister = false; - } - - else if (lowpass.equalsIgnoreCase(player.getName())) { + return false; + } else if (lowpass.equalsIgnoreCase(player.getName())) { m.send(player, "password_error_nick"); - allowRegister = false; - } - - else if (password.length() < Settings.getPasswordMinLen || password.length() > Settings.passwordMaxLength) { + return false; + } else if (password.length() < Settings.getPasswordMinLen || password.length() > Settings.passwordMaxLength) { m.send(player, "pass_len"); - allowRegister = false; - } - - else if (!Settings.unsafePasswords.isEmpty() && Settings.unsafePasswords.contains(password.toLowerCase())) { + return false; + } else if (!Settings.unsafePasswords.isEmpty() && Settings.unsafePasswords.contains(password.toLowerCase())) { m.send(player, "password_error_unsafe"); - allowRegister = false; + return false; } else if (database.isAuthAvailable(name)) { m.send(player, "user_regged"); - allowRegister = false; - } - - else if (Settings.getmaxRegPerIp > 0) { + return false; + } else if (Settings.getmaxRegPerIp > 0) { if (!plugin.authmePermissible(player, "authme.allow2accounts") && database.getAllAuthsByIp(getIp()).size() >= Settings.getmaxRegPerIp && !getIp().equalsIgnoreCase("127.0.0.1") && !getIp().equalsIgnoreCase("localhost")) { m.send(player, "max_reg"); - allowRegister = false; + return false; } } - + return true; } public void process() { - preRegister(); - if (!allowRegister) - return; - if (!email.isEmpty() && !email.equals("")) { - if (Settings.getmaxRegPerEmail > 0) { - if (!plugin.authmePermissible(player, "authme.allow2accounts") && database.getAllAuthsByEmail(email).size() >= Settings.getmaxRegPerEmail) { - m.send(player, "max_reg"); - return; + try { + if (!preRegisterCheck()) + return; + if (!email.isEmpty() && !email.equals("")) { + if (Settings.getmaxRegPerEmail > 0) { + if (!plugin.authmePermissible(player, "authme.allow2accounts") && database.getAllAuthsByEmail(email).size() >= Settings.getmaxRegPerEmail) { + m.send(player, "max_reg"); + return; + } } + emailRegister(); + return; } - emailRegister(); - return; + passwordRegister(); + } catch (Exception e) { + ConsoleLogger.showError(e.getMessage()); + ConsoleLogger.writeStackTrace(e); + m.send(player, "error"); } - passwordRegister(); } - protected void emailRegister() { + protected void emailRegister() throws Exception { if (Settings.getmaxRegPerEmail > 0) { if (!plugin.authmePermissible(player, "authme.allow2accounts") && database.getAllAuthsByEmail(email).size() >= Settings.getmaxRegPerEmail) { m.send(player, "max_reg"); @@ -109,14 +99,8 @@ public class AsyncronousRegister { } } PlayerAuth auth; - try { - final String hashnew = PasswordSecurity.getHash(Settings.getPasswordHash, password, name); - auth = new PlayerAuth(name, hashnew, getIp(), 0, (int) player.getLocation().getX(), (int) player.getLocation().getY(), (int) player.getLocation().getZ(), player.getLocation().getWorld().getName(), email, player.getName()); - } catch (NoSuchAlgorithmException e) { - ConsoleLogger.showError(e.getMessage()); - m.send(player, "error"); - return; - } + final String hashnew = PasswordSecurity.getHash(Settings.getPasswordHash, password, name); + auth = new PlayerAuth(name, hashnew, getIp(), 0, (int) player.getLocation().getX(), (int) player.getLocation().getY(), (int) player.getLocation().getZ(), player.getLocation().getWorld().getName(), email, player.getName()); if (PasswordSecurity.userSalt.containsKey(name)) { auth.setSalt(PasswordSecurity.userSalt.get(name)); } @@ -124,8 +108,9 @@ public class AsyncronousRegister { database.updateEmail(auth); database.updateSession(auth); plugin.mail.main(auth, password); - ProcessSyncronousEmailRegister syncronous = new ProcessSyncronousEmailRegister(player, plugin); + ProcessSyncEmailRegister syncronous = new ProcessSyncEmailRegister(player, plugin); plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, syncronous); + } protected void passwordRegister() { diff --git a/src/main/java/fr/xephi/authme/process/register/ProcessSyncronousEmailRegister.java b/src/main/java/fr/xephi/authme/process/register/ProcessSyncEmailRegister.java similarity index 93% rename from src/main/java/fr/xephi/authme/process/register/ProcessSyncronousEmailRegister.java rename to src/main/java/fr/xephi/authme/process/register/ProcessSyncEmailRegister.java index 0b1a723dd..683320a19 100644 --- a/src/main/java/fr/xephi/authme/process/register/ProcessSyncronousEmailRegister.java +++ b/src/main/java/fr/xephi/authme/process/register/ProcessSyncEmailRegister.java @@ -14,14 +14,14 @@ import fr.xephi.authme.task.MessageTask; import fr.xephi.authme.task.TimeoutTask; import fr.xephi.authme.util.Utils; -public class ProcessSyncronousEmailRegister implements Runnable { +public class ProcessSyncEmailRegister implements Runnable { protected Player player; protected String name; private AuthMe plugin; private Messages m = Messages.getInstance(); - public ProcessSyncronousEmailRegister(Player player, AuthMe plugin) { + public ProcessSyncEmailRegister(Player player, AuthMe plugin) { this.player = player; this.name = player.getName().toLowerCase(); this.plugin = plugin; From e3166c68076637325f109164e114409a516a99aa Mon Sep 17 00:00:00 2001 From: DNx5 Date: Tue, 3 Nov 2015 12:52:07 +0700 Subject: [PATCH 3/7] update management class --- .../fr/xephi/authme/process/Management.java | 66 +++++++++---------- 1 file changed, 32 insertions(+), 34 deletions(-) diff --git a/src/main/java/fr/xephi/authme/process/Management.java b/src/main/java/fr/xephi/authme/process/Management.java index f32ae8574..350d9bfe7 100644 --- a/src/main/java/fr/xephi/authme/process/Management.java +++ b/src/main/java/fr/xephi/authme/process/Management.java @@ -1,10 +1,7 @@ package fr.xephi.authme.process; -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; -import org.bukkit.plugin.PluginManager; - import fr.xephi.authme.AuthMe; +import fr.xephi.authme.process.email.AsyncChangeEmail; import fr.xephi.authme.process.join.AsyncronousJoin; import fr.xephi.authme.process.login.AsyncronousLogin; import fr.xephi.authme.process.logout.AsyncronousLogout; @@ -13,83 +10,84 @@ import fr.xephi.authme.process.register.AsyncRegister; import fr.xephi.authme.process.unregister.AsyncronousUnregister; import fr.xephi.authme.security.RandomString; import fr.xephi.authme.settings.Settings; +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitScheduler; /** - * * @authors Xephi59, - * Possible - * + * Possible */ public class Management { - public AuthMe plugin; + private final AuthMe pl; + private final BukkitScheduler sched; public static RandomString rdm = new RandomString(Settings.captchaLength); - public PluginManager pm; public Management(AuthMe plugin) { - this.plugin = plugin; - this.pm = plugin.getServer().getPluginManager(); + this.pl = plugin; + this.sched = pl.getServer().getScheduler(); } public void performLogin(final Player player, final String password, final boolean forceLogin) { - Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { + sched.runTaskAsynchronously(pl, new Runnable() { @Override public void run() { - new AsyncronousLogin(player, password, forceLogin, plugin, plugin.database).process(); - } - }); - } - - public void performRegister(final Player player, final String password, final String email) { - Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { - - @Override - public void run() { - new AsyncRegister(player, password, email, plugin, plugin.database).process(); + new AsyncronousLogin(player, password, forceLogin, pl, pl.database).process(); } }); } public void performLogout(final Player player) { - Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { + sched.runTaskAsynchronously(pl, new Runnable() { @Override public void run() { - new AsyncronousLogout(player, plugin, plugin.database).process(); + new AsyncronousLogout(player, pl, pl.database).process(); } }); } - public void performQuit(final Player player, final boolean isKick) { - Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { + public void performRegister(final Player player, final String password, final String email) { + sched.runTaskAsynchronously(pl, new Runnable() { @Override public void run() { - new AsyncronousQuit(player, plugin, plugin.database, isKick).process(); + new AsyncRegister(player, password, email, pl, pl.database).process(); } + }); + } + public void performUnregister(final Player player, final String password, final boolean force) { + sched.runTaskAsynchronously(pl, new Runnable() { + + @Override + public void run() { + new AsyncronousUnregister(player, password, force, pl).process(); + } }); } public void performJoin(final Player player) { - Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { + sched.runTaskAsynchronously(pl, new Runnable() { @Override public void run() { - new AsyncronousJoin(player, plugin, plugin.database).process(); + new AsyncronousJoin(player, pl, pl.database).process(); } }); } - public void performUnregister(final Player player, final String password, final boolean force) { - Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { + public void performQuit(final Player player, final boolean isKick) { + sched.runTaskAsynchronously(pl, new Runnable() { @Override public void run() { - new AsyncronousUnregister(player, password, force, plugin).process(); + new AsyncronousQuit(player, pl, pl.database, isKick).process(); } + }); - } + } + } From 4cbd8aad31648c519308a1cea6a424edbb8cbc06 Mon Sep 17 00:00:00 2001 From: DNx5 Date: Tue, 3 Nov 2015 14:08:51 +0700 Subject: [PATCH 4/7] update ConsoleLogger --- .../java/fr/xephi/authme/ConsoleLogger.java | 47 +++++++++---------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/src/main/java/fr/xephi/authme/ConsoleLogger.java b/src/main/java/fr/xephi/authme/ConsoleLogger.java index b7e757905..e791e352e 100644 --- a/src/main/java/fr/xephi/authme/ConsoleLogger.java +++ b/src/main/java/fr/xephi/authme/ConsoleLogger.java @@ -1,5 +1,9 @@ package fr.xephi.authme; +import com.google.common.base.Throwables; +import fr.xephi.authme.api.NewAPI; +import fr.xephi.authme.settings.Settings; + import java.io.IOException; import java.nio.file.Files; import java.nio.file.StandardOpenOption; @@ -8,39 +12,30 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.logging.Logger; -import com.google.common.base.Throwables; - -import fr.xephi.authme.api.NewAPI; -import fr.xephi.authme.settings.Settings; - public class ConsoleLogger { private static final Logger log = AuthMe.getInstance().getLogger(); private static final DateFormat df = new SimpleDateFormat("[MM-dd HH:mm:ss]"); public static void info(String message) { - if (AuthMe.getInstance().isEnabled()) { - log.info("[AuthMe] " + message); - if (Settings.useLogging) { - String dateTime; - synchronized (df) { - dateTime = df.format(new Date()); - } - writeLog(dateTime + " " + message); + log.info("[AuthMe] " + message); + if (Settings.useLogging) { + String dateTime; + synchronized (df) { + dateTime = df.format(new Date()); } + writeLog(dateTime + " " + message); } } public static void showError(String message) { - if (AuthMe.getInstance().isEnabled()) { - log.warning("[AuthMe] " + message); - if (Settings.useLogging) { - String dateTime; - synchronized (df) { - dateTime = df.format(new Date()); - } - writeLog(dateTime + " ERROR: " + message); + log.warning("[AuthMe] " + message); + if (Settings.useLogging) { + String dateTime; + synchronized (df) { + dateTime = df.format(new Date()); } + writeLog(dateTime + " ERROR: " + message); } } @@ -54,10 +49,12 @@ public class ConsoleLogger { } public static void writeStackTrace(Exception ex) { - String dateTime; - synchronized (df) { - dateTime = df.format(new Date()); + if (Settings.useLogging) { + String dateTime; + synchronized (df) { + dateTime = df.format(new Date()); + } + writeLog(dateTime + " " + Throwables.getStackTraceAsString(ex)); } - writeLog(dateTime + " " + Throwables.getStackTraceAsString(ex)); } } From f6a91085bddd42f0d55ac22b1a917094aa2b33e5 Mon Sep 17 00:00:00 2001 From: DNx5 Date: Tue, 3 Nov 2015 14:16:57 +0700 Subject: [PATCH 5/7] added async change email process --- .../process/email/AsyncChangeEmail.java | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java diff --git a/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java b/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java new file mode 100644 index 000000000..fcdd01ea0 --- /dev/null +++ b/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java @@ -0,0 +1,83 @@ +package fr.xephi.authme.process.email; + +import fr.xephi.authme.AuthMe; +import fr.xephi.authme.ConsoleLogger; +import fr.xephi.authme.cache.auth.PlayerAuth; +import fr.xephi.authme.cache.auth.PlayerCache; +import fr.xephi.authme.settings.Messages; +import fr.xephi.authme.settings.Settings; +import org.bukkit.entity.Player; + +import java.util.Arrays; + +public class AsyncChangeEmail { + + private final Player player; + private final AuthMe plugin; + private final String oldEmail; + private final String newEmail; + private final String newEmailVerify; + private final Messages m; + + public AsyncChangeEmail(Player player, AuthMe plugin, String oldEmail, String newEmail, String newEmailVerify) { + this.player = player; + this.plugin = plugin; + this.oldEmail = oldEmail; + this.newEmail = newEmail; + this.newEmailVerify = newEmailVerify; + this.m = Messages.getInstance(); + } + + public AsyncChangeEmail(Player player, AuthMe plugin, String oldEmail, String newEmail) { + this(player, plugin, oldEmail, newEmail, newEmail); + } + + public void process() { + try { + String playerName = player.getName().toLowerCase(); + + if (Settings.getmaxRegPerEmail > 0) { + if (!plugin.authmePermissible(player, "authme.allow2accounts") && plugin.database.getAllAuthsByEmail(newEmail).size() >= Settings.getmaxRegPerEmail) { + m.send(player, "max_reg"); + return; + } + } + + if (PlayerCache.getInstance().isAuthenticated(playerName)) { + if (!newEmail.equals(newEmailVerify)) { + m.send(player, "email_confirm"); + return; + } + PlayerAuth auth = PlayerCache.getInstance().getAuth(playerName); + if (oldEmail != null) { + if (auth.getEmail() == null || auth.getEmail().equals("your@email.com") || auth.getEmail().isEmpty()) { + m.send(player, "usage_email_add"); + return; + } + if (!oldEmail.equals(auth.getEmail())) { + m.send(player, "old_email_invalid"); + return; + } + } + if (!Settings.isEmailCorrect(newEmail)) { + m.send(player, "new_email_invalid"); + return; + } + String old = auth.getEmail(); + auth.setEmail(newEmail); + if (!plugin.database.updateEmail(auth)) { + m.send(player, "error"); + auth.setEmail(old); + return; + } + PlayerCache.getInstance().updatePlayer(auth); + m.send(player, "email_changed"); + player.sendMessage(Arrays.toString(m.send("email_defined")) + auth.getEmail()); + } + } catch (Exception e) { + ConsoleLogger.showError(e.getMessage()); + ConsoleLogger.writeStackTrace(e); + m.send(player, "error"); + } + } +} From 4eb94787ac70757ffdc2f2277b5bec6b39f5d048 Mon Sep 17 00:00:00 2001 From: DNx5 Date: Tue, 3 Nov 2015 14:22:32 +0700 Subject: [PATCH 6/7] implement async change email into management class. --- .../fr/xephi/authme/process/Management.java | 47 +++++++++++++------ .../process/email/AsyncChangeEmail.java | 6 +++ 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/main/java/fr/xephi/authme/process/Management.java b/src/main/java/fr/xephi/authme/process/Management.java index 350d9bfe7..ccd88b21e 100644 --- a/src/main/java/fr/xephi/authme/process/Management.java +++ b/src/main/java/fr/xephi/authme/process/Management.java @@ -19,75 +19,92 @@ import org.bukkit.scheduler.BukkitScheduler; */ public class Management { - private final AuthMe pl; + private final AuthMe plugin; private final BukkitScheduler sched; public static RandomString rdm = new RandomString(Settings.captchaLength); public Management(AuthMe plugin) { - this.pl = plugin; - this.sched = pl.getServer().getScheduler(); + this.plugin = plugin; + this.sched = this.plugin.getServer().getScheduler(); } public void performLogin(final Player player, final String password, final boolean forceLogin) { - sched.runTaskAsynchronously(pl, new Runnable() { + sched.runTaskAsynchronously(plugin, new Runnable() { @Override public void run() { - new AsyncronousLogin(player, password, forceLogin, pl, pl.database).process(); + new AsyncronousLogin(player, password, forceLogin, plugin, plugin.database).process(); } }); } public void performLogout(final Player player) { - sched.runTaskAsynchronously(pl, new Runnable() { + sched.runTaskAsynchronously(plugin, new Runnable() { @Override public void run() { - new AsyncronousLogout(player, pl, pl.database).process(); + new AsyncronousLogout(player, plugin, plugin.database).process(); } }); } public void performRegister(final Player player, final String password, final String email) { - sched.runTaskAsynchronously(pl, new Runnable() { + sched.runTaskAsynchronously(plugin, new Runnable() { @Override public void run() { - new AsyncRegister(player, password, email, pl, pl.database).process(); + new AsyncRegister(player, password, email, plugin, plugin.database).process(); } }); } public void performUnregister(final Player player, final String password, final boolean force) { - sched.runTaskAsynchronously(pl, new Runnable() { + sched.runTaskAsynchronously(plugin, new Runnable() { @Override public void run() { - new AsyncronousUnregister(player, password, force, pl).process(); + new AsyncronousUnregister(player, password, force, plugin).process(); } }); } public void performJoin(final Player player) { - sched.runTaskAsynchronously(pl, new Runnable() { + sched.runTaskAsynchronously(plugin, new Runnable() { @Override public void run() { - new AsyncronousJoin(player, pl, pl.database).process(); + new AsyncronousJoin(player, plugin, plugin.database).process(); } }); } public void performQuit(final Player player, final boolean isKick) { - sched.runTaskAsynchronously(pl, new Runnable() { + sched.runTaskAsynchronously(plugin, new Runnable() { @Override public void run() { - new AsyncronousQuit(player, pl, pl.database, isKick).process(); + new AsyncronousQuit(player, plugin, plugin.database, isKick).process(); } }); } + public void performAddEmail(final Player player, final String newEmail, final String newEmailVerify) { + sched.runTaskAsynchronously(plugin, new Runnable() { + @Override + public void run() { + new AsyncChangeEmail(player, plugin, null, newEmail, newEmailVerify).process(); + } + }); + } + + public void performChangeEmail(final Player player, final String oldEmail, final String newEmail) { + sched.runTaskAsynchronously(plugin, new Runnable() { + @Override + public void run() { + new AsyncChangeEmail(player, plugin, oldEmail, newEmail).process(); + } + }); + } } diff --git a/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java b/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java index fcdd01ea0..8735aedf8 100644 --- a/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java +++ b/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java @@ -73,6 +73,12 @@ public class AsyncChangeEmail { PlayerCache.getInstance().updatePlayer(auth); m.send(player, "email_changed"); player.sendMessage(Arrays.toString(m.send("email_defined")) + auth.getEmail()); + } else { + if (!plugin.database.isAuthAvailable(playerName)) { + m.send(player, "login_msg"); + } else { + m.send(player, "reg_email_msg"); + } } } catch (Exception e) { ConsoleLogger.showError(e.getMessage()); From 375162ab67cdcb1d20a363759afce180ad909885 Mon Sep 17 00:00:00 2001 From: DNx5 Date: Tue, 3 Nov 2015 14:41:05 +0700 Subject: [PATCH 7/7] update email commands to use new async process --- .../executable/email/AddEmailCommand.java | 58 ++---------- .../executable/email/ChangeEmailCommand.java | 54 +---------- .../xephi/authme/commands/EmailCommand.java | 91 ++----------------- .../authme/datasource/CacheDataSource.java | 27 ++---- .../fr/xephi/authme/datasource/MySQL.java | 1 + .../process/email/AsyncChangeEmail.java | 12 ++- 6 files changed, 37 insertions(+), 206 deletions(-) diff --git a/src/main/java/fr/xephi/authme/command/executable/email/AddEmailCommand.java b/src/main/java/fr/xephi/authme/command/executable/email/AddEmailCommand.java index f91e6a24b..d73e43974 100644 --- a/src/main/java/fr/xephi/authme/command/executable/email/AddEmailCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/email/AddEmailCommand.java @@ -1,25 +1,20 @@ package fr.xephi.authme.command.executable.email; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - import fr.xephi.authme.AuthMe; -import fr.xephi.authme.cache.auth.PlayerAuth; -import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.command.CommandParts; import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.settings.Messages; -import fr.xephi.authme.settings.Settings; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; public class AddEmailCommand extends ExecutableCommand { /** * Execute the command. * - * @param sender The command sender. + * @param sender The command sender. * @param commandReference The command reference. * @param commandArguments The command arguments. - * * @return True if the command was executed successfully, false otherwise. */ @Override @@ -35,57 +30,16 @@ public class AddEmailCommand extends ExecutableCommand { String playerMailVerify = commandArguments.get(1); // Make sure the current command executor is a player - if(!(sender instanceof Player)) { + if (!(sender instanceof Player)) { return true; } // Get the player instance and name final Player player = (Player) sender; - final String playerName = player.getName(); + final String playerName = player.getName().toLowerCase(); // Command logic - if (Settings.getmaxRegPerEmail > 0) { - if (!plugin.authmePermissible(sender, "authme.allow2accounts") && plugin.database.getAllAuthsByEmail(playerMail).size() >= Settings.getmaxRegPerEmail) { - m.send(player, "max_reg"); - return true; - } - } - - if (playerMail.equals(playerMailVerify) && PlayerCache.getInstance().isAuthenticated(playerName)) { - PlayerAuth auth = PlayerCache.getInstance().getAuth(playerName); - - if (auth.getEmail() == null || (!auth.getEmail().equals("your@email.com") && !auth.getEmail().isEmpty())) { - m.send(player, "usage_email_change"); - return true; - } - - if (!Settings.isEmailCorrect(playerMail)) { - m.send(player, "email_invalid"); - return true; - } - - auth.setEmail(playerMail); - - if (!plugin.database.updateEmail(auth)) { - m.send(player, "error"); - return true; - } - - PlayerCache.getInstance().updatePlayer(auth); - m.send(player, "email_added"); - player.sendMessage(auth.getEmail()); - - } else if (PlayerCache.getInstance().isAuthenticated(playerName)) { - m.send(player, "email_confirm"); - - } else { - if (!plugin.database.isAuthAvailable(playerName)) { - m.send(player, "login_msg"); - } else { - m.send(player, "reg_email_msg"); - } - } - + plugin.management.performAddEmail(player, playerMail, playerMailVerify); return true; } } diff --git a/src/main/java/fr/xephi/authme/command/executable/email/ChangeEmailCommand.java b/src/main/java/fr/xephi/authme/command/executable/email/ChangeEmailCommand.java index 0b5d7e3c0..fe6a4be2c 100644 --- a/src/main/java/fr/xephi/authme/command/executable/email/ChangeEmailCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/email/ChangeEmailCommand.java @@ -1,18 +1,12 @@ package fr.xephi.authme.command.executable.email; import fr.xephi.authme.AuthMe; -import fr.xephi.authme.ConsoleLogger; -import fr.xephi.authme.cache.auth.PlayerAuth; -import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.command.CommandParts; import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.settings.Messages; -import fr.xephi.authme.settings.Settings; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import java.util.Arrays; - public class ChangeEmailCommand extends ExecutableCommand { /** @@ -45,51 +39,7 @@ public class ChangeEmailCommand extends ExecutableCommand { final String playerName = player.getName(); // Command logic - - try { - if (Settings.getmaxRegPerEmail > 0) { - if (!plugin.authmePermissible(sender, "authme.allow2accounts") && plugin.database.getAllAuthsByEmail(playerMailNew).size() >= Settings.getmaxRegPerEmail) { - m.send(player, "max_reg"); - return true; - } - } - if (PlayerCache.getInstance().isAuthenticated(playerName)) { - PlayerAuth auth = PlayerCache.getInstance().getAuth(playerName); - if (auth.getEmail() == null || auth.getEmail().equals("your@email.com") || auth.getEmail().isEmpty()) { - m.send(player, "usage_email_add"); - return true; - } - if (!playerMailOld.equals(auth.getEmail())) { - m.send(player, "old_email_invalid"); - return true; - } - if (!Settings.isEmailCorrect(playerMailNew)) { - m.send(player, "new_email_invalid"); - return true; - } - auth.setEmail(playerMailNew); - if (!plugin.database.updateEmail(auth)) { - m.send(player, "error"); - return true; - } - PlayerCache.getInstance().updatePlayer(auth); - m.send(player, "email_changed"); - player.sendMessage(Arrays.toString(m.send("email_defined")) + auth.getEmail()); - } else if (PlayerCache.getInstance().isAuthenticated(playerName)) { - m.send(player, "email_confirm"); - } else { - if (!plugin.database.isAuthAvailable(playerName)) { - m.send(player, "login_msg"); - } else { - m.send(player, "reg_email_msg"); - } - } - return true; - } catch (Exception e) { - ConsoleLogger.showError(e.getMessage()); - ConsoleLogger.writeStackTrace(e); - m.send(sender, "error"); - return false; - } + plugin.management.performChangeEmail(player, playerMailOld, playerMailNew); + return true; } } diff --git a/src/main/java/fr/xephi/authme/commands/EmailCommand.java b/src/main/java/fr/xephi/authme/commands/EmailCommand.java index 241d543db..c59bcbc38 100644 --- a/src/main/java/fr/xephi/authme/commands/EmailCommand.java +++ b/src/main/java/fr/xephi/authme/commands/EmailCommand.java @@ -1,13 +1,5 @@ package fr.xephi.authme.commands; -import java.security.NoSuchAlgorithmException; -import java.util.Arrays; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - import fr.xephi.authme.AuthMe; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.cache.auth.PlayerAuth; @@ -16,9 +8,14 @@ import fr.xephi.authme.security.PasswordSecurity; import fr.xephi.authme.security.RandomString; import fr.xephi.authme.settings.Messages; import fr.xephi.authme.settings.Settings; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.security.NoSuchAlgorithmException; /** - * * @author Xephi59 */ public class EmailCommand implements CommandExecutor { @@ -32,7 +29,7 @@ public class EmailCommand implements CommandExecutor { @Override public boolean onCommand(CommandSender sender, Command cmnd, String label, - String[] args) { + String[] args) { if (!(sender instanceof Player)) { return true; } @@ -57,81 +54,13 @@ public class EmailCommand implements CommandExecutor { m.send(player, "usage_email_add"); return true; } - if (Settings.getmaxRegPerEmail > 0) { - if (!plugin.authmePermissible(sender, "authme.allow2accounts") && plugin.database.getAllAuthsByEmail(args[1]).size() >= Settings.getmaxRegPerEmail) { - m.send(player, "max_reg"); - return true; - } - } - if (args[1].equals(args[2]) && PlayerCache.getInstance().isAuthenticated(name)) { - PlayerAuth auth = PlayerCache.getInstance().getAuth(name); - if (auth.getEmail() == null || (!auth.getEmail().equals("your@email.com") && !auth.getEmail().isEmpty())) { - m.send(player, "usage_email_change"); - return true; - } - if (!Settings.isEmailCorrect(args[1])) { - m.send(player, "email_invalid"); - return true; - } - auth.setEmail(args[1]); - if (!plugin.database.updateEmail(auth)) { - m.send(player, "error"); - return true; - } - PlayerCache.getInstance().updatePlayer(auth); - m.send(player, "email_added"); - player.sendMessage(auth.getEmail()); - } else if (PlayerCache.getInstance().isAuthenticated(name)) { - m.send(player, "email_confirm"); - } else { - if (!plugin.database.isAuthAvailable(name)) { - m.send(player, "login_msg"); - } else { - m.send(player, "reg_email_msg"); - } - } + plugin.management.performAddEmail(player, args[1], args[2]); } else if (args[0].equalsIgnoreCase("change")) { if (args.length != 3) { m.send(player, "usage_email_change"); return true; } - if (Settings.getmaxRegPerEmail > 0) { - if (!plugin.authmePermissible(sender, "authme.allow2accounts") && plugin.database.getAllAuthsByEmail(args[2]).size() >= Settings.getmaxRegPerEmail) { - m.send(player, "max_reg"); - return true; - } - } - if (PlayerCache.getInstance().isAuthenticated(name)) { - PlayerAuth auth = PlayerCache.getInstance().getAuth(name); - if (auth.getEmail() == null || auth.getEmail().equals("your@email.com") || auth.getEmail().isEmpty()) { - m.send(player, "usage_email_add"); - return true; - } - if (!args[1].equals(auth.getEmail())) { - m.send(player, "old_email_invalid"); - return true; - } - if (!Settings.isEmailCorrect(args[2])) { - m.send(player, "new_email_invalid"); - return true; - } - auth.setEmail(args[2]); - if (!plugin.database.updateEmail(auth)) { - m.send(player, "error"); - return true; - } - PlayerCache.getInstance().updatePlayer(auth); - m.send(player, "email_changed"); - player.sendMessage(Arrays.toString(m.send("email_defined")) + auth.getEmail()); - } else if (PlayerCache.getInstance().isAuthenticated(name)) { - m.send(player, "email_confirm"); - } else { - if (!plugin.database.isAuthAvailable(name)) { - m.send(player, "login_msg"); - } else { - m.send(player, "reg_email_msg"); - } - } + plugin.management.performChangeEmail(player, args[1], args[2]); } if (args[0].equalsIgnoreCase("recovery")) { if (args.length != 2) { @@ -151,7 +80,7 @@ public class EmailCommand implements CommandExecutor { RandomString rand = new RandomString(Settings.getRecoveryPassLength); String thePass = rand.nextString(); String hashnew = PasswordSecurity.getHash(Settings.getPasswordHash, thePass, name); - PlayerAuth auth = null; + PlayerAuth auth; if (PlayerCache.getInstance().isAuthenticated(name)) { auth = PlayerCache.getInstance().getAuth(name); } else if (plugin.database.isAuthAvailable(name)) { diff --git a/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java b/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java index 40ed684cb..dc2ec02c3 100644 --- a/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java +++ b/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java @@ -9,10 +9,7 @@ import org.bukkit.entity.Player; import java.util.ArrayList; import java.util.List; import java.util.Map; -import java.util.concurrent.Callable; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; +import java.util.concurrent.*; public class CacheDataSource implements DataSource { @@ -229,23 +226,15 @@ public class CacheDataSource implements DataSource { @Override public synchronized boolean updateEmail(final PlayerAuth auth) { - if (!cache.containsKey(auth.getNickname())) { + try { + return exec.submit(new Callable() { + public Boolean call() { + return source.updateEmail(auth); + } + }).get(); + } catch (Exception e) { return false; } - PlayerAuth cachedAuth = cache.get(auth.getNickname()); - final String oldEmail = cachedAuth.getEmail(); - cachedAuth.setEmail(auth.getEmail()); - exec.execute(new Runnable() { - @Override - public void run() { - if (!source.updateEmail(auth)) { - if (cache.containsKey(auth.getNickname())) { - cache.get(auth.getNickname()).setEmail(oldEmail); - } - } - } - }); - return true; } @Override diff --git a/src/main/java/fr/xephi/authme/datasource/MySQL.java b/src/main/java/fr/xephi/authme/datasource/MySQL.java index e04794685..f20d99eff 100644 --- a/src/main/java/fr/xephi/authme/datasource/MySQL.java +++ b/src/main/java/fr/xephi/authme/datasource/MySQL.java @@ -692,6 +692,7 @@ public class MySQL implements DataSource { pst.executeUpdate(); } catch (Exception ex) { ConsoleLogger.showError(ex.getMessage()); + ConsoleLogger.writeStackTrace(ex); return false; } finally { close(pst); diff --git a/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java b/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java index 8735aedf8..93f6f22ab 100644 --- a/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java +++ b/src/main/java/fr/xephi/authme/process/email/AsyncChangeEmail.java @@ -71,13 +71,21 @@ public class AsyncChangeEmail { return; } PlayerCache.getInstance().updatePlayer(auth); + if (oldEmail == null) { + m.send(player, "email_added"); + player.sendMessage(auth.getEmail()); + return; + } m.send(player, "email_changed"); player.sendMessage(Arrays.toString(m.send("email_defined")) + auth.getEmail()); } else { - if (!plugin.database.isAuthAvailable(playerName)) { + if (plugin.database.isAuthAvailable(playerName)) { m.send(player, "login_msg"); } else { - m.send(player, "reg_email_msg"); + if (Settings.emailRegistration) + m.send(player, "reg_email_msg"); + else + m.send(player, "reg_msg"); } } } catch (Exception e) {