diff --git a/src/main/java/fr/xephi/authme/command/executable/authme/debug/DebugCommand.java b/src/main/java/fr/xephi/authme/command/executable/authme/debug/DebugCommand.java index f9541deb4..8e7168f2d 100644 --- a/src/main/java/fr/xephi/authme/command/executable/authme/debug/DebugCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/authme/debug/DebugCommand.java @@ -1,15 +1,16 @@ package fr.xephi.authme.command.executable.authme.debug; +import com.google.common.collect.ImmutableSet; import fr.xephi.authme.command.ExecutableCommand; +import fr.xephi.authme.initialization.factory.Factory; import org.bukkit.command.CommandSender; -import javax.annotation.PostConstruct; import javax.inject.Inject; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; -import java.util.stream.Stream; /** * Debug command main. @@ -17,25 +18,21 @@ import java.util.stream.Stream; public class DebugCommand implements ExecutableCommand { @Inject - private PermissionGroups permissionGroups; + private Factory debugSectionFactory; + + private Set> sectionClasses = + ImmutableSet.of(PermissionGroups.class, TestEmailSender.class); private Map sections; - @PostConstruct - private void collectSections() { - Map sections = Stream.of(permissionGroups) - .collect(Collectors.toMap(DebugSection::getName, Function.identity())); - this.sections = sections; - } - @Override public void executeCommand(CommandSender sender, List arguments) { if (arguments.isEmpty()) { sender.sendMessage("Available sections:"); - sections.values() + getSections().values() .forEach(e -> sender.sendMessage("- " + e.getName() + ": " + e.getDescription())); } else { - DebugSection debugSection = sections.get(arguments.get(0).toLowerCase()); + DebugSection debugSection = getSections().get(arguments.get(0).toLowerCase()); if (debugSection == null) { sender.sendMessage("Unknown subcommand"); } else { @@ -43,4 +40,14 @@ public class DebugCommand implements ExecutableCommand { } } } + + // Lazy getter + private Map getSections() { + if (sections == null) { + sections = sectionClasses.stream() + .map(debugSectionFactory::newInstance) + .collect(Collectors.toMap(DebugSection::getName, Function.identity())); + } + return sections; + } } diff --git a/src/main/java/fr/xephi/authme/command/executable/authme/debug/TestEmailSender.java b/src/main/java/fr/xephi/authme/command/executable/authme/debug/TestEmailSender.java new file mode 100644 index 000000000..cfad4013a --- /dev/null +++ b/src/main/java/fr/xephi/authme/command/executable/authme/debug/TestEmailSender.java @@ -0,0 +1,78 @@ +package fr.xephi.authme.command.executable.authme.debug; + +import fr.xephi.authme.data.auth.PlayerAuth; +import fr.xephi.authme.datasource.DataSource; +import fr.xephi.authme.mail.SendMailSSL; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; + +import javax.inject.Inject; +import java.util.List; + +/** + * Sends out a test email. + */ +class TestEmailSender implements DebugSection { + + @Inject + private DataSource dataSource; + + @Inject + private SendMailSSL sendMailSSL; + + + @Override + public String getName() { + return "mail"; + } + + @Override + public String getDescription() { + return "Sends out a test email"; + } + + @Override + public void execute(CommandSender sender, List arguments) { + if (!sendMailSSL.hasAllInformation()) { + sender.sendMessage(ChatColor.RED + "You haven't set all required configurations in config.yml " + + "for sending emails. Please check your config.yml"); + return; + } + + String email = getEmail(sender, arguments); + + // getEmail() takes care of informing the sender of the error if email == null + if (email != null) { + boolean sendMail = sendMailSSL.sendTestEmail(email); + if (sendMail) { + sender.sendMessage("Test email sent to " + email + " with success"); + } else { + sender.sendMessage(ChatColor.RED + "Failed to send test mail to " + email + "; please check your logs"); + } + } + } + + private String getEmail(CommandSender sender, List arguments) { + if (arguments.isEmpty()) { + PlayerAuth auth = dataSource.getAuth(sender.getName()); + if (auth == null) { + sender.sendMessage(ChatColor.RED + "Please provide an email address, " + + "e.g. /authme debug mail test@example.com"); + return null; + } + String email = auth.getEmail(); + if (email == null || "your@email.com".equals(email)) { + sender.sendMessage(ChatColor.RED + "No email set for your account! Please use /authme debug mail "); + return null; + } + return email; + } else { + String email = arguments.get(0); + if (email.contains("@")) { + return email; + } + sender.sendMessage(ChatColor.RED + "Invalid email! Usage: /authme debug mail test@example.com"); + return null; + } + } +} diff --git a/src/main/java/fr/xephi/authme/mail/SendMailSSL.java b/src/main/java/fr/xephi/authme/mail/SendMailSSL.java index a782d3a13..f5952e40b 100644 --- a/src/main/java/fr/xephi/authme/mail/SendMailSSL.java +++ b/src/main/java/fr/xephi/authme/mail/SendMailSSL.java @@ -109,6 +109,21 @@ public class SendMailSSL { return sendEmail(message, htmlEmail); } + public boolean sendTestEmail(String email) { + HtmlEmail htmlEmail; + try { + htmlEmail = initializeMail(email); + } catch (EmailException e) { + ConsoleLogger.logException("Failed to create email for sample email:", e); + return false; + } + + htmlEmail.setSubject("AuthMe test email"); + String message = "Hello there!
This is a sample email sent to you from a Minecraft server (" + + serverName + ") via /authme debug mail. If you're seeing this, sending emails should be fine."; + return sendEmail(message, htmlEmail); + } + private File generateImage(String name, String newPass) throws IOException { ImageGenerator gen = new ImageGenerator(newPass); File file = new File(dataFolder, name + "_new_pass.jpg");