From d8b1b414e2c77fd48e1f3203de490eb129defa45 Mon Sep 17 00:00:00 2001 From: Kuba Orlik Date: Mon, 13 Sep 2021 21:45:31 +0200 Subject: [PATCH] Fix parsing issues, list all attachments --- .gitignore | 1 + README.md | 2 ++ mail-to-pdf.mjs | 65 +++++++++++++++++++++++++++++++++++++++-------- package-lock.json | 28 ++++++++++++++++++++ package.json | 18 +++++++++++++ 5 files changed, 104 insertions(+), 10 deletions(-) create mode 100644 .gitignore create mode 100644 package-lock.json create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2ccbe46 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/node_modules/ diff --git a/README.md b/README.md index 18265c9..ab0209d 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ Skrypt konwertujący pliki .eml do plików PDF, razem z załącznikami w postaci - zx - wkhtmltopdf +- emacs +- mu ## Sposób użycia diff --git a/mail-to-pdf.mjs b/mail-to-pdf.mjs index b20e21b..fdbf514 100755 --- a/mail-to-pdf.mjs +++ b/mail-to-pdf.mjs @@ -4,16 +4,37 @@ import { resolve, basename } from "path"; const pwd = (await $`pwd`).stdout.replace("\n", ""); +const sexpr = require("sexpr-plus").parse; + $`echo $PWD`; const path = resolve(process.env.PWD, process.argv.slice(-1)[0]); -const mail_body = (await $`mu view ${path} -o sexp`).stdout.replace( - /\\"/g, - '"' -); +const mail_body = (await $`mu view ${path} -o sexp`).stdout; + +function parse_mail(sexp_string) { + const array = sexpr(sexp_string)[0].content; + const result = {}; + let i = 0; + while (i < array.length) { + result[array[i].content.slice(1)] = array[i + 1].content; + i += 2; + } + return result; +} + +const mail_obj = parse_mail(mail_body); -let html = mail_body.match(/\.*\<\/html\>/gis)[0]; +let html = mail_obj["body-html"] + ? mail_obj["body-html"] + : /* HTML */ ` + + + + +
${mail_obj["body-txt"]}
+ + `; const attachments = (await $`mu extract ${path}`).stdout; @@ -36,6 +57,8 @@ function escape(str) { return str.replace(/ line.split(" ").slice(3)) + .filter((e) => e[0] != "") + .slice(0, -1) + .map(([name, type, role, size1, size2]) => ({ + name, + type, + role, + size: (size1 + " " + size2).replace(/[()]/g, ""), + })) + .filter(({ role }) => role == "[attach]"); + const files = await Promise.all( to_download.map(async ({ filename, mime }) => { await $`mu extract --overwrite --target-dir=/tmp ${path} ${filename}`; @@ -66,6 +103,10 @@ const attachments_items = files html = html.replace( /<\/body>/i, /* HTML */ `

Załączniki:

+${attachment_info.map( + (a) => + `` + )}
${a.name}${a.size}${a.type}
    ${attachments_items}
` @@ -76,8 +117,6 @@ const from = formatAddress(getSimpleHeader(mail_body, "from")); const to = formatAddress(getSimpleHeader(mail_body, "to")); const date = await formatDate(getSimpleHeader(mail_body, "date")); -// console.log({ subject, from, to, date }); - html = html.replace( //i, `` + @@ -99,13 +138,19 @@ html = html.replace( Do: ${escape(to)} - ` + +
` ); const output = (await $`mktemp --suffix=.html`).stdout.replace("\n", ""); await fs.writeFile(output, html); -await $`wkhtmltopdf ${output} ${`${basename(path)}.pdf`}`; + +const output_file = `${resolve(path, "../")}/${basename(path)}.pdf`; + +console.log("Wrote to", output_file); + +await $`wkhtmltopdf ${output} ${output_file}`; // console.log(pwd + "output.html"); // console.log(mail_body); @@ -117,4 +162,4 @@ async function formatDate(sexp) { 2, "0" )}`; -} \ No newline at end of file +} diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..300a544 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,28 @@ +{ + "name": "mail-to-pdf", + "version": "1.0.0", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "mail-to-pdf", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "sexpr-plus": "^7.0.0" + } + }, + "node_modules/sexpr-plus": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/sexpr-plus/-/sexpr-plus-7.0.0.tgz", + "integrity": "sha1-PsWDDuWuhfLTpiWFaWj3ZBycjJ8=" + } + }, + "dependencies": { + "sexpr-plus": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/sexpr-plus/-/sexpr-plus-7.0.0.tgz", + "integrity": "sha1-PsWDDuWuhfLTpiWFaWj3ZBycjJ8=" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..0229344 --- /dev/null +++ b/package.json @@ -0,0 +1,18 @@ +{ + "name": "mail-to-pdf", + "description": "Skrypt konwertujący pliki .eml do plików PDF, razem z załącznikami w postaci zdjęć.", + "version": "1.0.0", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "https://git.kuba-orlik.name/kuba/mail-to-pdf.git" + }, + "author": "", + "license": "ISC", + "dependencies": { + "sexpr-plus": "^7.0.0" + } +}