#450 Move Settings#loadEmailText and #getWelcomeMessage

This commit is contained in:
ljacqu
2016-02-03 22:36:01 +01:00
parent 155881ef05
commit 3e30a34714
9 changed files with 183 additions and 181 deletions
@@ -1,25 +1,26 @@
package fr.xephi.authme.mail;
import java.io.File;
import java.io.IOException;
import java.security.Security;
import java.util.Properties;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.imageio.ImageIO;
import javax.mail.Session;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.bukkit.Bukkit;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.ImageGenerator;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.properties.EmailSettings;
import fr.xephi.authme.util.StringUtils;
import org.apache.commons.mail.EmailConstants;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.bukkit.Bukkit;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.imageio.ImageIO;
import javax.mail.Session;
import java.io.File;
import java.io.IOException;
import java.security.Security;
import java.util.Properties;
/**
* @author Xephi59
@@ -27,13 +28,15 @@ import fr.xephi.authme.util.StringUtils;
public class SendMailSSL {
private final AuthMe plugin;
private final NewSetting settings;
public SendMailSSL(AuthMe plugin) {
public SendMailSSL(AuthMe plugin, NewSetting settings) {
this.plugin = plugin;
this.settings = settings;
}
public void main(final PlayerAuth auth, final String newPass) {
final String mailText = replaceMailTags(Settings.getMailText, plugin, auth, newPass);
final String mailText = replaceMailTags(settings.getEmailMessage(), plugin, auth, newPass);
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override
@@ -41,21 +44,23 @@ public class SendMailSSL {
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
HtmlEmail email;
try {
email = initializeMail(auth);
email = initializeMail(auth, settings);
} catch (EmailException e) {
ConsoleLogger.showError("Failed to create email with the given settings: " + StringUtils.formatException(e));
ConsoleLogger.showError("Failed to create email with the given settings: "
+ StringUtils.formatException(e));
return;
}
String content = mailText;
// Generate an image?
File file = null;
if (Settings.generateImage) {
if (settings.getProperty(EmailSettings.PASSWORD_AS_IMAGE)) {
try {
file = generateImage(auth, plugin, newPass);
content = embedImageIntoEmailContent(file, email, content);
} catch (IOException | EmailException e) {
ConsoleLogger.showError("Unable to send new password as image for email " + auth.getEmail() + ": " + StringUtils.formatException(e));
ConsoleLogger.showError("Unable to send new password as image for email " + auth.getEmail()
+ ": " + StringUtils.formatException(e));
}
}
@@ -68,43 +73,39 @@ public class SendMailSSL {
});
}
private static File generateImage(PlayerAuth auth, AuthMe plugin,
String newPass) throws IOException {
private static File generateImage(PlayerAuth auth, AuthMe plugin, String newPass) throws IOException {
ImageGenerator gen = new ImageGenerator(newPass);
File file = new File(plugin.getDataFolder() + File.separator + auth.getNickname() + "_new_pass.jpg");
File file = new File(plugin.getDataFolder(), auth.getNickname() + "_new_pass.jpg");
ImageIO.write(gen.generateImage(), "jpg", file);
return file;
}
private static String embedImageIntoEmailContent(File image,
HtmlEmail email, String content) throws EmailException {
private static String embedImageIntoEmailContent(File image, HtmlEmail email, String content)
throws EmailException {
DataSource source = new FileDataSource(image);
String tag = email.embed(source, image.getName());
return content.replace("<image />", "<img src=\"cid:" + tag + "\">");
}
private static HtmlEmail initializeMail(PlayerAuth auth)
private static HtmlEmail initializeMail(PlayerAuth auth, NewSetting settings)
throws EmailException {
String senderName;
if (StringUtils.isEmpty(Settings.getmailSenderName)) {
senderName = Settings.getmailAccount;
} else {
senderName = Settings.getmailSenderName;
}
String senderMail = Settings.getmailAccount;
String mailPassword = Settings.getmailPassword;
int port = Settings.getMailPort;
String senderMail = settings.getProperty(EmailSettings.MAIL_ACCOUNT);
String senderName = StringUtils.isEmpty(settings.getProperty(EmailSettings.MAIL_SENDER_NAME))
? senderMail
: settings.getProperty(EmailSettings.MAIL_SENDER_NAME);
String mailPassword = settings.getProperty(EmailSettings.MAIL_PASSWORD);
int port = settings.getProperty(EmailSettings.SMTP_PORT);
HtmlEmail email = new HtmlEmail();
email.setCharset(org.apache.commons.mail.EmailConstants.UTF_8);
email.setCharset(EmailConstants.UTF_8);
email.setSmtpPort(port);
email.setHostName(Settings.getmailSMTP);
email.setHostName(settings.getProperty(EmailSettings.SMTP_HOST));
email.addTo(auth.getEmail());
email.setFrom(senderMail, senderName);
email.setSubject(Settings.getMailSubject);
email.setSubject(settings.getProperty(EmailSettings.RECOVERY_MAIL_SUBJECT));
email.setAuthentication(senderMail, mailPassword);
setPropertiesForPort(email, port);
setPropertiesForPort(email, port, settings);
return email;
}
@@ -113,7 +114,8 @@ public class SendMailSSL {
email.setHtmlMsg(content);
email.setTextMsg(content);
} catch (EmailException e) {
ConsoleLogger.showError("Your email.html config contains an error and cannot be sent: " + StringUtils.formatException(e));
ConsoleLogger.showError("Your email.html config contains an error and cannot be sent: "
+ StringUtils.formatException(e));
return false;
}
try {
@@ -125,17 +127,19 @@ public class SendMailSSL {
}
}
private static String replaceMailTags(String mailText, AuthMe plugin,
PlayerAuth auth, String newPass) {
return mailText.replace("<playername />", auth.getNickname()).replace("<servername />", plugin.getServer().getServerName()).replace("<generatedpass />", newPass);
private static String replaceMailTags(String mailText, AuthMe plugin, PlayerAuth auth, String newPass) {
return mailText
.replace("<playername />", auth.getNickname())
.replace("<servername />", plugin.getServer().getServerName())
.replace("<generatedpass />", newPass);
}
@SuppressWarnings("deprecation")
private static void setPropertiesForPort(HtmlEmail email, int port)
private static void setPropertiesForPort(HtmlEmail email, int port, NewSetting settings)
throws EmailException {
switch (port) {
case 587:
if (!Settings.emailOauth2Token.isEmpty()) {
String oAuth2Token = settings.getProperty(EmailSettings.OAUTH2_TOKEN);
if (!oAuth2Token.isEmpty()) {
if (Security.getProvider("Google OAuth2 Provider") == null) {
Security.addProvider(new OAuth2Provider());
}
@@ -146,7 +150,7 @@ public class SendMailSSL {
mailProperties.setProperty("mail.smtp.sasl.mechanisms", "XOAUTH2");
mailProperties.setProperty("mail.smtp.auth.login.disable", "true");
mailProperties.setProperty("mail.smtp.auth.plain.disable", "true");
mailProperties.setProperty(OAuth2SaslClientFactory.OAUTH_TOKEN_PROP, Settings.emailOauth2Token);
mailProperties.setProperty(OAuth2SaslClientFactory.OAUTH_TOKEN_PROP, oAuth2Token);
email.setMailSession(Session.getInstance(mailProperties));
} else {
email.setStartTLSEnabled(true);
@@ -159,7 +163,7 @@ public class SendMailSSL {
email.setSSLCheckServerIdentity(true);
break;
case 465:
email.setSslSmtpPort("" + port);
email.setSslSmtpPort(Integer.toString(port));
email.setSSL(true);
break;
default: