Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create safe and unsafe HTML files for emails #62

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -91,7 +91,7 @@ public void sendToTelegram(Message message) throws Exception {
if (filename != null)
sendTelegramMessage.sendMessage(parsedMail.toString(),
chatId,
(String) filename);
(String[]) filename);
else {
sendTelegramMessage.sendMessage(parsedMail.toString(),
chatId);
Expand Down
Expand Up @@ -61,7 +61,7 @@ public void sendMessage(String message, long chatId) {
}

@Async
public void sendMessage(String message, long chatId, String filename) {
public void sendMessage(String message, long chatId, String[] filename) {
String telegramURI = imapClientServiceConfig.getTelegram().getUrl() +
imapClientServiceConfig.getTelegram().getBotToken() +
"/sendMessage";
Expand Down Expand Up @@ -93,16 +93,23 @@ public void sendMessage(String message, long chatId, String filename) {
*/
InlineKeyboardMarkup markupKeyboard = new InlineKeyboardMarkup();

InlineKeyboardButton keyboardButton = new InlineKeyboardButton();
keyboardButton.setText("HTML version");
keyboardButton.setUrl(
InlineKeyboardButton keyboardButtonSafeHTML = new InlineKeyboardButton();
keyboardButtonSafeHTML.setText("Safe HTML version");
keyboardButtonSafeHTML.setUrl(
imapClientServiceConfig.getEmails().getHostPath() +
filename);
filename[0]);

InlineKeyboardButton keyboardButtonUnSafeHTML = new InlineKeyboardButton();
keyboardButtonUnSafeHTML.setText("Unsafe HTML version");
keyboardButtonUnSafeHTML.setUrl(
imapClientServiceConfig.getEmails().getHostPath() +
filename[1]);

List<List<InlineKeyboardButton>> buttonList = new ArrayList<>();

buttonList.add(new ArrayList<>());
buttonList.get(0).add(keyboardButton);
buttonList.get(0).add(keyboardButtonSafeHTML);
buttonList.get(0).add(keyboardButtonUnSafeHTML);

markupKeyboard.setInlineKeyboardButtonList(buttonList);

Expand Down
Expand Up @@ -17,22 +17,32 @@ public class SaveMailToHTMLFile {
private static final Logger log = LoggerFactory.getLogger(
SaveMailToHTMLFile.class);

public Object saveToFile(String htmlContent){
private static String safeHTMLCSPPolicy = "<meta http-equiv=\"Content-Security-Policy\" content=\"default-src 'self';style-src 'unsafe-inline';img-src 'self' data:\">";

public Object saveToFile(String htmlContent) {
try {
String filename = UUID.randomUUID().toString() + ".html";
String safeHTMLFilename = UUID.randomUUID().toString() + "_safe" + ".html";
String UnSafeHTMLFilename = UUID.randomUUID().toString() + "_unsafe" + ".html";

FileWriter myWriter = new FileWriter(
imapClientServiceConfig
.getEmails()
.getDownloadPath() + filename);
.getDownloadPath() + safeHTMLFilename);
myWriter.write(safeHTMLCSPPolicy + htmlContent);
myWriter.close();
log.debug("File written to disk: " + safeHTMLFilename);

myWriter = new FileWriter(
imapClientServiceConfig
.getEmails()
.getDownloadPath() + UnSafeHTMLFilename);

myWriter.write(htmlContent);
myWriter.close();
log.debug("File written to disk: " + UnSafeHTMLFilename);

log.debug("File written to disk: "+ filename);
return filename;
}
catch (Exception e) {
return new String[]{safeHTMLFilename, UnSafeHTMLFilename};
} catch (Exception e) {
log.error("Unable to save to HTML file. " + e.getMessage());
return null;
}
Expand Down