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

50
pom.xml
View File

@ -51,13 +51,12 @@
<!-- Change Compiler Version (JDK) HERE! -->
<javaVersion>1.7</javaVersion>
<!-- Change Bukkit Version HERE! -->
<bukkitVersion>1.8.8-R0.1-SNAPSHOT</bukkitVersion>
</properties>
<build>
<finalName>AuthMe-${project.version}</finalName>
<finalName>${project.name}</finalName>
<resources>
<resource>
<targetPath>.</targetPath>
@ -93,12 +92,6 @@
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<minimizeJar>false</minimizeJar>
<!-- TODO: simplify these relocations if possible -->
<artifactSet>
<excludes>
<exclude>com.sun:*</exclude>
</excludes>
</artifactSet>
<relocations>
<relocation>
<pattern>org.mcstats</pattern>
@ -108,38 +101,6 @@
<pattern>com.google.gson</pattern>
<shadedPattern>fr.xephi.authme.libs.gson</shadedPattern>
</relocation>
<relocation>
<pattern>com.zaxxer.hikari</pattern>
<shadedPattern>fr.xephi.authme.libs.hikari</shadedPattern>
</relocation>
<relocation>
<pattern>org.slf4j</pattern>
<shadedPattern>fr.xephi.authme.libs.slf4j</shadedPattern>
</relocation>
<relocation>
<pattern>javax.mail</pattern>
<shadedPattern>fr.xephi.authme.libs.mail</shadedPattern>
</relocation>
<relocation>
<pattern>javax.activation</pattern>
<shadedPattern>fr.xephi.authme.libs.mail.activation</shadedPattern>
</relocation>
<relocation>
<pattern>com.sun.mail</pattern>
<shadedPattern>fr.xephi.authme.libs.mail</shadedPattern>
</relocation>
<relocation>
<pattern>com.sun.activation</pattern>
<shadedPattern>fr.xephi.authme.libs.mail.activation</shadedPattern>
</relocation>
<relocation>
<pattern>com.maxmind.geoip</pattern>
<shadedPattern>fr.xephi.authme.libs.geoip</shadedPattern>
</relocation>
<relocation>
<pattern>net.ricecode.similarity</pattern>
<shadedPattern>fr.xephi.authme.libs.similarity</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
@ -240,19 +201,18 @@
<!-- Java Email Implementation -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>mailapi</artifactId>
<artifactId>javax.mail</artifactId>
<version>1.5.4</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>smtp</artifactId>
<version>1.5.4</version>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.4</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<!-- GSON (required to provide 1.7.10 and below compatibility) -->
<dependency>
<groupId>com.google.code.gson</groupId>

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;

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();