From f114019e5f43add7c3873ce7b0f15134364ceb65 Mon Sep 17 00:00:00 2001 From: Xephi59 Date: Thu, 9 Jul 2015 19:24:00 +0200 Subject: [PATCH] Option to send an image as new password --- .../java/fr/xephi/authme/ImageGenerator.java | 33 +++++++++++++++++++ .../java/fr/xephi/authme/SendMailSSL.java | 21 ++++++++++++ .../fr/xephi/authme/settings/Settings.java | 8 ++++- 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/main/java/fr/xephi/authme/ImageGenerator.java diff --git a/src/main/java/fr/xephi/authme/ImageGenerator.java b/src/main/java/fr/xephi/authme/ImageGenerator.java new file mode 100644 index 000000000..0a635c28a --- /dev/null +++ b/src/main/java/fr/xephi/authme/ImageGenerator.java @@ -0,0 +1,33 @@ +package fr.xephi.authme; + +import java.awt.Color; +import java.awt.Font; +import java.awt.GradientPaint; +import java.awt.Graphics2D; +import java.awt.image.BufferedImage; + +public class ImageGenerator { + + private String pass; + private AuthMe plugin; + + public ImageGenerator(AuthMe plugin, String pass) { + this.pass = pass; + this.plugin = plugin; + } + + public BufferedImage generateImage() { + BufferedImage image = new BufferedImage(200, 60, BufferedImage.TYPE_BYTE_INDEXED); + Graphics2D graphics = image.createGraphics(); + graphics.setColor(Color.BLACK); + graphics.fillRect(0, 0, 200, 40); + GradientPaint gradientPaint = new GradientPaint(10, 5, Color.WHITE, 20, 10, Color.WHITE, true); + graphics.setPaint(gradientPaint); + Font font = new Font("Comic Sans MS", Font.BOLD, 30); + graphics.setFont(font); + graphics.drawString(pass, 5, 30); + graphics.dispose(); + image.flush(); + return image; + } +} diff --git a/src/main/java/fr/xephi/authme/SendMailSSL.java b/src/main/java/fr/xephi/authme/SendMailSSL.java index 2382124f2..1927071f5 100644 --- a/src/main/java/fr/xephi/authme/SendMailSSL.java +++ b/src/main/java/fr/xephi/authme/SendMailSSL.java @@ -1,9 +1,14 @@ 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; @@ -73,10 +78,26 @@ public class SendMailSSL { messageBodyPart.setText(mailText); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); + + // Generate an image ? + File file = null; + if (Settings.generateImage) { + ImageGenerator gen = new ImageGenerator(plugin, 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); + } + message.setContent(multipart); Transport transport = session.getTransport("smtp"); transport.connect(smtp, acc, password); transport.sendMessage(message, message.getAllRecipients()); + if (file != null) + file.delete(); } catch (Exception e) { System.out.println("Some error occured while trying to send a mail to " + mail); diff --git a/src/main/java/fr/xephi/authme/settings/Settings.java b/src/main/java/fr/xephi/authme/settings/Settings.java index 0696c6041..fc6891974 100644 --- a/src/main/java/fr/xephi/authme/settings/Settings.java +++ b/src/main/java/fr/xephi/authme/settings/Settings.java @@ -70,7 +70,7 @@ public final class Settings extends YamlConfiguration { purgePermissions, enableProtection, enableAntiBot, recallEmail, useWelcomeMessage, broadcastWelcomeMessage, forceRegKick, forceRegLogin, checkVeryGames, delayJoinMessage, noTeleport, - applyBlindEffect, customAttributes; + applyBlindEffect, customAttributes, generateImage; public static String getNickRegex, getUnloggedinGroup, getMySQLHost, getMySQLPort, getMySQLUsername, getMySQLPassword, getMySQLDatabase, @@ -269,6 +269,7 @@ public final class Settings extends YamlConfiguration { forceRegisterCommands = (List) configFile.getList("settings.forceRegisterCommands", new ArrayList()); forceRegisterCommandsAsConsole = (List) configFile.getList("settings.forceRegisterCommandsAsConsole", new ArrayList()); customAttributes = configFile.getBoolean("Hooks.customAttributes"); + generateImage = configFile.getBoolean("Email.generateImage", true); // Load the welcome message getWelcomeMessage(plugin); @@ -435,6 +436,7 @@ public final class Settings extends YamlConfiguration { forceRegisterCommands = (List) configFile.getList("settings.forceRegisterCommands", new ArrayList()); forceRegisterCommandsAsConsole = (List) configFile.getList("settings.forceRegisterCommandsAsConsole", new ArrayList()); customAttributes = configFile.getBoolean("Hooks.customAttributes"); + generateImage = configFile.getBoolean("Email.generateImage", true); // Reload the welcome message getWelcomeMessage(AuthMe.getInstance()); @@ -587,6 +589,10 @@ public final class Settings extends YamlConfiguration { } if (contains("Hooks.notifications")) set("Hooks.notifications", null); + if (!contains("Email.generateImage")) { + set("Email.generateImage", true); + changes = true; + } if (changes) { plugin.getLogger().warning("Merge new Config Options - I'm not an error, please don't report me");