Merge pull request #1462 from menocar/mailgun

Use mailgun for sending emails.
This commit is contained in:
LevelX2 2016-01-13 11:41:30 +01:00
commit e366cc1757
8 changed files with 67 additions and 13 deletions

View file

@ -39,6 +39,8 @@
saveGameActivated="false" saveGameActivated="false"
authenticationActivated="false" authenticationActivated="false"
googleAccount="" googleAccount=""
mailgunApiKey=""
mailgunDomain=""
/> />
<playerTypes> <playerTypes>
<playerType name="Human" jar="mage-player-human.jar" className="mage.player.human.HumanPlayer"/> <playerType name="Human" jar="mage-player-human.jar" className="mage.player.human.HumanPlayer"/>

View file

@ -184,6 +184,21 @@
<version>1.4.2</version> <version>1.4.2</version>
<type>jar</type> <type>jar</type>
</dependency> </dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.19</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View file

@ -136,7 +136,7 @@ public class MageServerImpl implements MageServer {
} }
String authToken = generateAuthToken(); String authToken = generateAuthToken();
activeAuthTokens.put(email, authToken); 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" + "Use this auth token to reset your password: " + authToken + "\n" +
"It's valid until the next server restart.")) { "It's valid until the next server restart.")) {
sendErrorMessageToClient(sessionId, "There was an error inside the server while emailing an auth token"); sendErrorMessageToClient(sessionId, "There was an error inside the server while emailing an auth token");

View file

@ -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 <postmaster@" + domain + ">");
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;
}
}

View file

@ -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..."); logger.info("Loading cards...");
if (fastDbMode) { if (fastDbMode) {
CardScanner.scanned = true; CardScanner.scanned = true;
@ -122,6 +111,7 @@ public class Main {
logger.info("Done."); logger.info("Done.");
deleteSavedGames(); deleteSavedGames();
ConfigSettings config = ConfigSettings.getInstance();
for (GamePlugin plugin : config.getGameTypes()) { for (GamePlugin plugin : config.getGameTypes()) {
GameFactory.getInstance().addGameType(plugin.getName(), loadGameType(plugin), loadPlugin(plugin)); GameFactory.getInstance().addGameType(plugin.getName(), loadGameType(plugin), loadPlugin(plugin));
} }

View file

@ -99,7 +99,7 @@ public class Session {
return returnMessage; return returnMessage;
} }
AuthorizedUserRepository.instance.add(userName, password, email); 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 + ".")) { "You are successfully registered as " + userName + ".")) {
logger.info("Sent a registration confirmation email to " + email + " for " + userName); logger.info("Sent a registration confirmation email to " + email + " for " + userName);
} else { } else {

View file

@ -139,6 +139,14 @@ public class ConfigSettings {
return config.getServer().getGoogleAccount(); return config.getServer().getGoogleAccount();
} }
public String getMailgunApiKey() {
return config.getServer().getMailgunApiKey();
}
public String getMailgunDomain() {
return config.getServer().getMailgunDomain();
}
public List<Plugin> getPlayerTypes() { public List<Plugin> getPlayerTypes() {
return config.getPlayerTypes().getPlayerType(); return config.getPlayerTypes().getPlayerType();
} }

View file

@ -36,6 +36,8 @@
<xs:attribute name="saveGameActivated" type="xs:boolean" use="optional"/> <xs:attribute name="saveGameActivated" type="xs:boolean" use="optional"/>
<xs:attribute name="authenticationActivated" type="xs:boolean" use="optional"/> <xs:attribute name="authenticationActivated" type="xs:boolean" use="optional"/>
<xs:attribute name="googleAccount" type="xs:string" use="optional"/> <xs:attribute name="googleAccount" type="xs:string" use="optional"/>
<xs:attribute name="mailgunApiKey" type="xs:string" use="optional"/>
<xs:attribute name="mailgunDomain" type="xs:string" use="optional"/>
</xs:complexType> </xs:complexType>
</xs:element> </xs:element>