diff --git a/README.md b/README.md index b5730a8..e4c76b6 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,7 @@ market use a normal email server as an intermediate step. For this reason I developed this small program which provides a SMTP-to-RocketChat-gateway. You can simply send an e-mail to a specific user (The e-mail address must be provided for each user account) and he will get a message directly in RocketChat. Attachments in the email are converted to file uploads in RocketChat. Since RocketChat, for security -reasons, does not support HTML-rendering, HTML-emails are converted to plain-text messages. The original messages -are additionally sent as file uploads. +reasons, does not support HTML-rendering, HTML-emails are converted into Markdown-format. # How to use the program diff --git a/build.gradle b/build.gradle index 5f3c711..852d749 100644 --- a/build.gradle +++ b/build.gradle @@ -17,6 +17,7 @@ dependencies { implementation('com.google.code.gson:gson:2.10.1') implementation('net.sourceforge.argparse4j:argparse4j:0.9.0') implementation('org.jsoup:jsoup:1.17.2') + implementation('io.github.furstenheim:copy_down:1.1') } test { diff --git a/src/main/java/rocketgateway/RocketGateway.java b/src/main/java/rocketgateway/RocketGateway.java index 81f7893..02e8fae 100644 --- a/src/main/java/rocketgateway/RocketGateway.java +++ b/src/main/java/rocketgateway/RocketGateway.java @@ -16,8 +16,8 @@ public static void main(String[] args) throws IOException { CommandLineParser parser = new CommandLineParser(args); Namespace res = parser.getRes(); - String configfile = res.get("configfile"); - ConfigFileParser configFileParser = new ConfigFileParser(configfile); + String configFile = res.get("configfile"); + ConfigFileParser configFileParser = new ConfigFileParser(configFile); boolean error = configFileParser.parse(); if (error) { diff --git a/src/main/java/rocketgateway/message/RocketEmlMessage.java b/src/main/java/rocketgateway/message/RocketEmlMessage.java index 2fe8127..25cf83b 100644 --- a/src/main/java/rocketgateway/message/RocketEmlMessage.java +++ b/src/main/java/rocketgateway/message/RocketEmlMessage.java @@ -1,11 +1,10 @@ package rocketgateway.message; +import io.github.furstenheim.CopyDown; import jakarta.activation.DataSource; import jakarta.mail.*; import jakarta.mail.internet.InternetAddress; import jakarta.mail.internet.MimeMessage; -import org.jsoup.Jsoup; -import org.jsoup.nodes.Document; import rocketgateway.apache_commons_email.MimeMessageParser; import java.io.InputStream; @@ -15,6 +14,7 @@ public class RocketEmlMessage { private final InputStream data; private final Set recipients; private final List attachments; + private final CopyDown converter; private String date; private String sender; private String subject; @@ -30,6 +30,7 @@ public RocketEmlMessage(InputStream data) { this.body = ""; this.recipients = new HashSet<>(); this.attachments = new ArrayList<>(); + this.converter = new CopyDown(); } /** @@ -68,13 +69,7 @@ public void parseEML() { // If there is no plain body check if there is a html body. if (this.body.isEmpty() & mimeMessageParser.hasHtmlContent()) { String htmlContent = mimeMessageParser.getHtmlContent(); - - // Strip html body of all tags. - Document doc = Jsoup.parse(htmlContent); - this.body = doc.text().strip().replaceAll("\r\n", "\n"); - - // Make an attachment from the original html body so the user can see the original message. - this.attachments.add(new RocketEmlAttachment(this.subject+".html", "text/html", htmlContent.getBytes())); + this.body = converter.convert(htmlContent); } // Check if there are attachments @@ -83,6 +78,7 @@ public void parseEML() { for (DataSource attachment : mimeMessageParser.getAttachmentList()) { String filename = Optional.ofNullable(attachment.getName()).orElse("unknown.dat"); String mimeType = Optional.ofNullable(attachment.getContentType()).orElse("application/octet-stream"); + try (InputStream stream = attachment.getInputStream()) { byte[] content = stream.readAllBytes();