From 158060a3b0c877f399cdd5f031337e0e03e6b350 Mon Sep 17 00:00:00 2001 From: Maxime Dor Date: Wed, 6 Sep 2017 23:08:47 +0200 Subject: [PATCH] Add some placeholders handling for e-mail template --- application.example.yaml | 6 +++++- .../controller/v1/InvitationController.groovy | 6 +++++- .../kamax/mxisd/invitation/IThreePidInvite.java | 4 ++++ .../kamax/mxisd/invitation/InvitationManager.java | 13 +++++++++---- .../io/kamax/mxisd/invitation/ThreePidInvite.java | 15 +++++++++++++++ .../invitation/sender/EmailInviteSender.java | 13 ++++++++++--- 6 files changed, 48 insertions(+), 9 deletions(-) diff --git a/application.example.yaml b/application.example.yaml index ca027bd..7d82e2c 100644 --- a/application.example.yaml +++ b/application.example.yaml @@ -234,5 +234,9 @@ invite: # The display name used in the e-mail name: "Matrix Identity" - # The MIME content to send + # The MIME content to send, UTF-8 expected + # + # The following placeholders are possible: + # - %SENDER_DISPLAY_NAME% + # - %ROOM_NAME% contentPath: "/absolute/path/to/file" diff --git a/src/main/groovy/io/kamax/mxisd/controller/v1/InvitationController.groovy b/src/main/groovy/io/kamax/mxisd/controller/v1/InvitationController.groovy index ee40c95..aadf3ed 100644 --- a/src/main/groovy/io/kamax/mxisd/controller/v1/InvitationController.groovy +++ b/src/main/groovy/io/kamax/mxisd/controller/v1/InvitationController.groovy @@ -63,7 +63,11 @@ class InvitationController { @RequestParam String medium, @RequestParam String address, @RequestParam("room_id") String roomId) { - IThreePidInvite invite = new ThreePidInvite(new MatrixID(sender), medium, address, roomId) + Map parameters = new HashMap<>() + for (String key : request.getParameterMap().keySet()) { + parameters.put(key, request.getParameter(key)); + } + IThreePidInvite invite = new ThreePidInvite(new MatrixID(sender), medium, address, roomId, parameters) IThreePidInviteReply reply = mgr.storeInvite(invite) return gson.toJson(new ThreePidInviteReplyIO(reply, keyMgr.getPublicKeyBase64(keyMgr.getCurrentIndex()), srvCfg.getPublicUrl())) diff --git a/src/main/groovy/io/kamax/mxisd/invitation/IThreePidInvite.java b/src/main/groovy/io/kamax/mxisd/invitation/IThreePidInvite.java index 3b7ff47..62b43dd 100644 --- a/src/main/groovy/io/kamax/mxisd/invitation/IThreePidInvite.java +++ b/src/main/groovy/io/kamax/mxisd/invitation/IThreePidInvite.java @@ -22,6 +22,8 @@ package io.kamax.mxisd.invitation; import io.kamax.matrix._MatrixID; +import java.util.Map; + public interface IThreePidInvite { _MatrixID getSender(); @@ -32,4 +34,6 @@ public interface IThreePidInvite { String getRoomId(); + Map getProperties(); + } diff --git a/src/main/groovy/io/kamax/mxisd/invitation/InvitationManager.java b/src/main/groovy/io/kamax/mxisd/invitation/InvitationManager.java index 30db078..7f654c5 100644 --- a/src/main/groovy/io/kamax/mxisd/invitation/InvitationManager.java +++ b/src/main/groovy/io/kamax/mxisd/invitation/InvitationManager.java @@ -29,6 +29,7 @@ import io.kamax.mxisd.lookup.SingleLookupRequest; import io.kamax.mxisd.lookup.ThreePidMapping; import io.kamax.mxisd.lookup.strategy.LookupStrategy; import io.kamax.mxisd.signature.SignatureManager; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang.RandomStringUtils; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; @@ -184,7 +185,8 @@ public class InvitationManager { log.warn("No HS found for domain {} - ignoring publishing", domain); } else { // TODO this is needed as this will block if called during authentication cycle due to synapse implementation - new Thread(() -> { // FIXME need to make this retryable and within a general background working pool + + new Thread(() -> { // FIXME need to make this retry-able and within a general background working pool HttpPost req = new HttpPost(hsUrlOpt.get() + "/_matrix/federation/v1/3pid/onbind"); // Expected body: https://matrix.to/#/!HUeDbmFUsWAhxHHvFG:matrix.org/$150469846739DCLWc:matrix.trancendances.fr JSONObject obj = new JSONObject(); // TODO use Gson instead @@ -200,7 +202,7 @@ public class InvitationManager { objUp.put("room_id", reply.getInvite().getRoomId()); objUp.put("signed", obj); - String mapping = gson.toJson(objUp); // FIXME we shouldn't need to be doign this + String mapping = gson.toJson(objUp); // FIXME we shouldn't need to be doing this JSONObject content = new JSONObject(); // TODO use Gson instead JSONArray invites = new JSONArray(); @@ -212,14 +214,17 @@ public class InvitationManager { content.put("signatures", signMgr.signMessageJson(content.toString())); - log.info("Will send following JSON to {}: {}", domain, content.toString()); StringEntity entity = new StringEntity(content.toString(), StandardCharsets.UTF_8); entity.setContentType("application/json"); req.setEntity(entity); try { log.info("Posting onBind event to {}", req.getURI()); CloseableHttpResponse response = client.execute(req); - log.info("Answer code: {}", response.getStatusLine().getStatusCode()); + int statusCode = response.getStatusLine().getStatusCode(); + log.info("Answer code: {}", statusCode); + if (statusCode >= 400) { + log.warn("Answer body: {}", IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8)); + } response.close(); } catch (IOException e) { log.warn("Unable to tell HS {} about invite being mapped", domain, e); diff --git a/src/main/groovy/io/kamax/mxisd/invitation/ThreePidInvite.java b/src/main/groovy/io/kamax/mxisd/invitation/ThreePidInvite.java index acfb90f..1294c87 100644 --- a/src/main/groovy/io/kamax/mxisd/invitation/ThreePidInvite.java +++ b/src/main/groovy/io/kamax/mxisd/invitation/ThreePidInvite.java @@ -22,18 +22,28 @@ package io.kamax.mxisd.invitation; import io.kamax.matrix._MatrixID; +import java.util.HashMap; +import java.util.Map; + public class ThreePidInvite implements IThreePidInvite { private _MatrixID sender; private String medium; private String address; private String roomId; + private Map properties; public ThreePidInvite(_MatrixID sender, String medium, String address, String roomId) { this.sender = sender; this.medium = medium; this.address = address; this.roomId = roomId; + this.properties = new HashMap<>(); + } + + public ThreePidInvite(_MatrixID sender, String medium, String address, String roomId, Map properties) { + this(sender, medium, address, roomId); + this.properties = properties; } @Override @@ -56,4 +66,9 @@ public class ThreePidInvite implements IThreePidInvite { return roomId; } + @Override + public Map getProperties() { + return properties; + } + } diff --git a/src/main/groovy/io/kamax/mxisd/invitation/sender/EmailInviteSender.java b/src/main/groovy/io/kamax/mxisd/invitation/sender/EmailInviteSender.java index 72045f0..49f7b9a 100644 --- a/src/main/groovy/io/kamax/mxisd/invitation/sender/EmailInviteSender.java +++ b/src/main/groovy/io/kamax/mxisd/invitation/sender/EmailInviteSender.java @@ -25,6 +25,7 @@ import io.kamax.matrix.ThreePidMedium; import io.kamax.mxisd.config.invite.sender.EmailSenderConfig; import io.kamax.mxisd.exception.ConfigurationException; import io.kamax.mxisd.invitation.IThreePidInviteReply; +import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -39,6 +40,7 @@ import javax.mail.internet.MimeMessage; import java.io.FileInputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.util.Date; @Component @@ -75,11 +77,16 @@ public class EmailInviteSender implements IInviteSender { } try { - MimeMessage msg = new MimeMessage(session, new FileInputStream(cfg.getContentPath())); - msg.setHeader("X-Mailer", "mxisd"); + String templateBody = IOUtils.toString(new FileInputStream(cfg.getContentPath()), StandardCharsets.UTF_8); + templateBody = + templateBody.replace("%SENDER_DISPLAY_NAME%", invite.getInvite().getProperties().get("sender_display_name")) + .replace("%ROOM_NAME%", invite.getInvite().getProperties().get("room_name")); + + MimeMessage msg = new MimeMessage(session, IOUtils.toInputStream(templateBody, StandardCharsets.UTF_8)); + msg.setHeader("X-Mailer", "mxisd"); // TODO set version msg.setSentDate(new Date()); - msg.setRecipients(Message.RecipientType.TO, invite.getInvite().getAddress()); msg.setFrom(sender); + msg.setRecipients(Message.RecipientType.TO, invite.getInvite().getAddress()); log.info("Sending invite to {} via SMTP using {}:{}", invite.getInvite().getAddress(), cfg.getHost(), cfg.getPort()); SMTPTransport transport = (SMTPTransport) session.getTransport("smtp");