From 7d3a9d076d0be517cc10e24b2df34fda31700cae Mon Sep 17 00:00:00 2001 From: Me Car Date: Wed, 13 Jan 2016 00:38:59 +0900 Subject: [PATCH] Use mailgun for sending emails. --- Mage.Server/config/config.xml | 2 + Mage.Server/pom.xml | 15 ++++++++ .../main/java/mage/server/MageServerImpl.java | 2 +- .../main/java/mage/server/MailgunClient.java | 37 +++++++++++++++++++ .../src/main/java/mage/server/Main.java | 12 +----- .../src/main/java/mage/server/Session.java | 2 +- .../java/mage/server/util/ConfigSettings.java | 8 ++++ .../main/xml-resources/jaxb/Config/Config.xsd | 2 + 8 files changed, 67 insertions(+), 13 deletions(-) create mode 100644 Mage.Server/src/main/java/mage/server/MailgunClient.java diff --git a/Mage.Server/config/config.xml b/Mage.Server/config/config.xml index 7695e7a663..4d22d71917 100644 --- a/Mage.Server/config/config.xml +++ b/Mage.Server/config/config.xml @@ -39,6 +39,8 @@ saveGameActivated="false" authenticationActivated="false" googleAccount="" + mailgunApiKey="" + mailgunDomain="" /> diff --git a/Mage.Server/pom.xml b/Mage.Server/pom.xml index 8b435de671..436fee4192 100644 --- a/Mage.Server/pom.xml +++ b/Mage.Server/pom.xml @@ -184,6 +184,21 @@ 1.4.2 jar + + com.sun.jersey + jersey-core + 1.19 + + + com.sun.jersey + jersey-client + 1.19 + + + com.sun.jersey.contribs + jersey-multipart + 1.19 + diff --git a/Mage.Server/src/main/java/mage/server/MageServerImpl.java b/Mage.Server/src/main/java/mage/server/MageServerImpl.java index 2059324b70..1412d25d3d 100644 --- a/Mage.Server/src/main/java/mage/server/MageServerImpl.java +++ b/Mage.Server/src/main/java/mage/server/MageServerImpl.java @@ -136,7 +136,7 @@ public class MageServerImpl implements MageServer { } String authToken = generateAuthToken(); activeAuthTokens.put(email, authToken); - if (!GmailClient.sendMessage(email, "XMage Password Reset Auth Token", + if (!MailgunClient.sendMessage(email, "XMage Password Reset Auth Token", "Use this auth token to reset your password: " + authToken + "\n" + "It's valid until the next server restart.")) { sendErrorMessageToClient(sessionId, "There was an error inside the server while emailing an auth token"); diff --git a/Mage.Server/src/main/java/mage/server/MailgunClient.java b/Mage.Server/src/main/java/mage/server/MailgunClient.java new file mode 100644 index 0000000000..77ba777783 --- /dev/null +++ b/Mage.Server/src/main/java/mage/server/MailgunClient.java @@ -0,0 +1,37 @@ +package mage.server; + +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter; +import com.sun.jersey.core.util.MultivaluedMapImpl; +import javax.ws.rs.core.MediaType; +import mage.server.util.ConfigSettings; +import org.apache.log4j.Logger; + +public class MailgunClient { + + private static final Logger logger = Logger.getLogger(Main.class); + + public static boolean sendMessage(String email, String subject, String text) { + if (email.length() == 0) { + logger.info("Email is not sent because the address is empty"); + return false; + } + Client client = Client.create(); + client.addFilter(new HTTPBasicAuthFilter("api", ConfigSettings.getInstance().getMailgunApiKey())); + String domain = ConfigSettings.getInstance().getMailgunDomain(); + WebResource webResource = client.resource("https://api.mailgun.net/v3/" + domain + "/messages"); + MultivaluedMapImpl formData = new MultivaluedMapImpl(); + formData.add("from", "XMage "); + formData.add("to", email); + formData.add("subject", subject); + formData.add("text", text); + ClientResponse response = webResource.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, formData); + boolean succeeded = response.getStatus() == 200; + if (!succeeded) { + logger.error("Error sending message to " + email + ". Status code: " + response.getStatus()); + } + return succeeded; + } +} diff --git a/Mage.Server/src/main/java/mage/server/Main.java b/Mage.Server/src/main/java/mage/server/Main.java index b739b2c2c7..bf36251db7 100644 --- a/Mage.Server/src/main/java/mage/server/Main.java +++ b/Mage.Server/src/main/java/mage/server/Main.java @@ -102,17 +102,6 @@ public class Main { } } - ConfigSettings config = ConfigSettings.getInstance(); - if (config.isAuthenticationActivated()) { - logger.info("Initializing GmailClient. This will open up a tab in your browser to ask for an OAuth access token."); - if (GmailClient.initilize()) { - logger.info("GmailClient initilized successfully."); - } else { - logger.fatal("GmailClient initialization failed."); - return; - } - } - logger.info("Loading cards..."); if (fastDbMode) { CardScanner.scanned = true; @@ -122,6 +111,7 @@ public class Main { logger.info("Done."); deleteSavedGames(); + ConfigSettings config = ConfigSettings.getInstance(); for (GamePlugin plugin : config.getGameTypes()) { GameFactory.getInstance().addGameType(plugin.getName(), loadGameType(plugin), loadPlugin(plugin)); } diff --git a/Mage.Server/src/main/java/mage/server/Session.java b/Mage.Server/src/main/java/mage/server/Session.java index 9c7913f834..67610f4abe 100644 --- a/Mage.Server/src/main/java/mage/server/Session.java +++ b/Mage.Server/src/main/java/mage/server/Session.java @@ -99,7 +99,7 @@ public class Session { return returnMessage; } AuthorizedUserRepository.instance.add(userName, password, email); - if (GmailClient.sendMessage(email, "XMage Registration Completed", + if (MailgunClient.sendMessage(email, "XMage Registration Completed", "You are successfully registered as " + userName + ".")) { logger.info("Sent a registration confirmation email to " + email + " for " + userName); } else { diff --git a/Mage.Server/src/main/java/mage/server/util/ConfigSettings.java b/Mage.Server/src/main/java/mage/server/util/ConfigSettings.java index b73961956a..4cda0d9195 100644 --- a/Mage.Server/src/main/java/mage/server/util/ConfigSettings.java +++ b/Mage.Server/src/main/java/mage/server/util/ConfigSettings.java @@ -139,6 +139,14 @@ public class ConfigSettings { return config.getServer().getGoogleAccount(); } + public String getMailgunApiKey() { + return config.getServer().getMailgunApiKey(); + } + + public String getMailgunDomain() { + return config.getServer().getMailgunDomain(); + } + public List getPlayerTypes() { return config.getPlayerTypes().getPlayerType(); } diff --git a/Mage.Server/src/main/xml-resources/jaxb/Config/Config.xsd b/Mage.Server/src/main/xml-resources/jaxb/Config/Config.xsd index c2fa00ef8a..1c25007dd2 100644 --- a/Mage.Server/src/main/xml-resources/jaxb/Config/Config.xsd +++ b/Mage.Server/src/main/xml-resources/jaxb/Config/Config.xsd @@ -36,6 +36,8 @@ + +