#1034 Create subcommand to send test email

- Add test email feature
- Change debug command to lazily instantiate its subcommands
This commit is contained in:
ljacqu 2017-02-18 17:53:34 +01:00
parent c9b66183de
commit 6937dd37fb
3 changed files with 112 additions and 12 deletions

View File

@ -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<DebugSection> debugSectionFactory;
private Set<Class<? extends DebugSection>> sectionClasses =
ImmutableSet.of(PermissionGroups.class, TestEmailSender.class);
private Map<String, DebugSection> sections;
@PostConstruct
private void collectSections() {
Map<String, DebugSection> sections = Stream.of(permissionGroups)
.collect(Collectors.toMap(DebugSection::getName, Function.identity()));
this.sections = sections;
}
@Override
public void executeCommand(CommandSender sender, List<String> 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<String, DebugSection> getSections() {
if (sections == null) {
sections = sectionClasses.stream()
.map(debugSectionFactory::newInstance)
.collect(Collectors.toMap(DebugSection::getName, Function.identity()));
}
return sections;
}
}

View File

@ -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<String> 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<String> 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 <email>");
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;
}
}
}

View File

@ -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!<br />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");