Change default mail api to apache one

This commit is contained in:
Xephi59
2015-11-11 01:28:04 +01:00
parent 604f2f3ee9
commit 10606a22e6
3 changed files with 65 additions and 129 deletions
+43 -29
View File
@@ -72,7 +72,9 @@ public class AuthMe extends JavaPlugin {
/** Defines the current AuthMeReloaded version name. */
private static final String PLUGIN_VERSION_NAME = "5.1-SNAPSHOT";
/** Defines the current AuthMeReloaded version code. */
private static final int PLUGIN_VERSION_CODE = 100; // Increase this number by one when an update is released
private static final int PLUGIN_VERSION_CODE = 100; // Increase this number
// by one when an update
// is released
private static AuthMe plugin;
private static Server server;
@@ -114,14 +116,14 @@ public class AuthMe extends JavaPlugin {
private boolean canConnect = true;
public boolean canConnect() {
return canConnect;
}
return canConnect;
}
public void setCanConnect(boolean canConnect) {
this.canConnect = canConnect;
}
public void setCanConnect(boolean canConnect) {
this.canConnect = canConnect;
}
public static AuthMe getInstance() {
public static AuthMe getInstance() {
return plugin;
}
@@ -186,18 +188,20 @@ public class AuthMe extends JavaPlugin {
Graph databaseBackend = metrics.createGraph("Database backend");
// Custom graphs
if(Settings.messageFile.exists()) {
if (Settings.messageFile.exists()) {
messagesLanguage.addPlotter(new Metrics.Plotter(Settings.messagesLanguage) {
@Override
public int getValue() {
return 1;
return 1;
}
});
}
databaseBackend.addPlotter(new Metrics.Plotter(Settings.getDataSource.toString()) {
@Override
public int getValue() {
return 1;
return 1;
}
});
@@ -335,17 +339,19 @@ public class AuthMe extends JavaPlugin {
pm.registerEvents(new AuthMeEntityListener(this), this);
pm.registerEvents(new AuthMeServerListener(this), this);
// TODO: This is moved to CommandManager.registerCommands() handled by AuthMe.onCommand() -- timvisee
// TODO: This is moved to CommandManager.registerCommands() handled by
// AuthMe.onCommand() -- timvisee
// Register commands
//getCommand("authme").setExecutor(new AdminCommand(this));
//getCommand("register").setExecutor(new RegisterCommand(this));
//getCommand("login").setExecutor(new LoginCommand(this));
//getCommand("changepassword").setExecutor(new ChangePasswordCommand(this));
//getCommand("logout").setExecutor(new LogoutCommand(this));
//getCommand("unregister").setExecutor(new UnregisterCommand(this));
//getCommand("email").setExecutor(new EmailCommand(this));
//getCommand("captcha").setExecutor(new CaptchaCommand(this));
//getCommand("converter").setExecutor(new ConverterCommand(this));
// getCommand("authme").setExecutor(new AdminCommand(this));
// getCommand("register").setExecutor(new RegisterCommand(this));
// getCommand("login").setExecutor(new LoginCommand(this));
// getCommand("changepassword").setExecutor(new
// ChangePasswordCommand(this));
// getCommand("logout").setExecutor(new LogoutCommand(this));
// getCommand("unregister").setExecutor(new UnregisterCommand(this));
// getCommand("email").setExecutor(new EmailCommand(this));
// getCommand("captcha").setExecutor(new CaptchaCommand(this));
// getCommand("converter").setExecutor(new ConverterCommand(this));
// Purge on start if enabled
autoPurge();
@@ -419,7 +425,8 @@ public class AuthMe extends JavaPlugin {
// Initialize and setup the database
public void setupDatabase() throws Exception {
if (database != null) database.close();
if (database != null)
database.close();
// Backend MYSQL - FILE - SQLITE - SQLITEHIKARI
boolean isSQLite = false;
switch (Settings.getDataSource) {
@@ -830,18 +837,23 @@ public class AuthMe extends JavaPlugin {
/**
* Handle Bukkit commands.
*
* @param sender The command sender (Bukkit).
* @param cmd The command (Bukkit).
* @param commandLabel The command label (Bukkit).
* @param args The command arguments (Bukkit).
* @param sender
* The command sender (Bukkit).
* @param cmd
* The command (Bukkit).
* @param commandLabel
* The command label (Bukkit).
* @param args
* The command arguments (Bukkit).
*
* @return True if the command was executed, false otherwise.
*/
@Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
public boolean onCommand(CommandSender sender, Command cmd,
String commandLabel, String[] args) {
// Get the command handler, and make sure it's valid
CommandHandler commandHandler = this.getCommandHandler();
if(commandHandler == null)
if (commandHandler == null)
return false;
// Handle the command, return the result
@@ -851,7 +863,8 @@ public class AuthMe extends JavaPlugin {
/**
* Get the current installed AuthMeReloaded version name.
*
* @return The version name of the currently installed AuthMeReloaded instance.
* @return The version name of the currently installed AuthMeReloaded
* instance.
*/
public static String getVersionName() {
return PLUGIN_VERSION_NAME;
@@ -860,7 +873,8 @@ public class AuthMe extends JavaPlugin {
/**
* Get the current installed AuthMeReloaded version code.
*
* @return The version code of the currently installed AuthMeReloaded instance.
* @return The version code of the currently installed AuthMeReloaded
* instance.
*/
public static int getVersionCode() {
return PLUGIN_VERSION_CODE;
+17 -55
View File
@@ -1,25 +1,12 @@
package fr.xephi.authme;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.imageio.ImageIO;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.apache.commons.mail.HtmlEmail;
import org.bukkit.Bukkit;
import fr.xephi.authme.cache.auth.PlayerAuth;
@@ -47,7 +34,7 @@ public class SendMailSSL {
}
final String sender = sendername;
final String port = String.valueOf(Settings.getMailPort);
final int port = Settings.getMailPort;
final String acc = Settings.getmailAccount;
final String subject = Settings.getMailSubject;
final String smtp = Settings.getmailSMTP;
@@ -59,35 +46,18 @@ public class SendMailSSL {
@Override
public void run() {
try {
Properties props = new Properties();
props.put("mail.smtp.host", smtp);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", port);
props.put("mail.smtp.socketFactory.port", port);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.starttls.enable", true);
props.put("mail.smtp.socketFactory.fallback", false);
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(acc, password);
}
});
Message message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(acc, sender));
} catch (UnsupportedEncodingException uee) {
message.setFrom(new InternetAddress(acc));
}
message.addRecipient(Message.RecipientType.TO, new InternetAddress(mail));
message.setSubject(subject);
message.setSentDate(new Date());
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(mailText, "text/html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
HtmlEmail email = new HtmlEmail();
email.setSmtpPort(port);
email.setHostName(smtp);
email.addTo(mail);
email.setFrom(acc, sender);
email.setSubject(subject);
email.setHtmlMsg(mailText);
email.setAuthentication(acc, password);
email.setStartTLSEnabled(true);
email.setStartTLSRequired(true);
email.setSSLCheckServerIdentity(true);
// Generate an image ?
File file = null;
if (Settings.generateImage) {
@@ -95,25 +65,17 @@ public class SendMailSSL {
ImageGenerator gen = new ImageGenerator(newPass);
file = new File(plugin.getDataFolder() + File.separator + auth.getNickname() + "_new_pass.jpg");
ImageIO.write(gen.generateImage(), "jpg", file);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(auth.getNickname() + "_new_pass.jpg");
multipart.addBodyPart(messageBodyPart);
email.embed(source, auth.getNickname() + "_new_pass.jpg");
} catch (Exception e) {
ConsoleLogger.showError("Unable to send new password as image! Using normal text! Dest: " + mail);
}
}
message.setContent(multipart);
try {
Transport transport = session.getTransport("smtp");
transport.connect(smtp, Integer.parseInt(port), acc, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
email.send();
} catch (Exception e) {
e.printStackTrace();
ConsoleLogger.showError("Cannot send email to " + mail + ", an error occured!");
ConsoleLogger.showError("Fail to send a mail to " + mail);
}
if (file != null)
file.delete();