Skip to content

Commit

Permalink
Convert HTML-messages to Markdown via "Copy Down"-lib.
Browse files Browse the repository at this point in the history
  • Loading branch information
glasen committed Apr 14, 2024
1 parent ee33df1 commit e23814c
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 13 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/rocketgateway/RocketGateway.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/rocketgateway/message/RocketEmlMessage.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,6 +14,7 @@ public class RocketEmlMessage {
private final InputStream data;
private final Set<RocketEmlAddress> recipients;
private final List<RocketEmlAttachment> attachments;
private final CopyDown converter;
private String date;
private String sender;
private String subject;
Expand All @@ -30,6 +30,7 @@ public RocketEmlMessage(InputStream data) {
this.body = "";
this.recipients = new HashSet<>();
this.attachments = new ArrayList<>();
this.converter = new CopyDown();
}

/**
Expand Down Expand Up @@ -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
Expand All @@ -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();

Expand Down

0 comments on commit e23814c

Please sign in to comment.