Fix parsing issues, list all attachments

master
Kuba Orlik 3 years ago
parent 1b44541a1e
commit d8b1b414e2

1
.gitignore vendored

@ -0,0 +1 @@
/node_modules/

@ -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

@ -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\>.*\<\/html\>/gis)[0];
let html = mail_obj["body-html"]
? mail_obj["body-html"]
: /* HTML */ `<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<pre style="white-space: pre-wrap">${mail_obj["body-txt"]}</pre>
</body>
</html>`;
const attachments = (await $`mu extract ${path}`).stdout;
@ -36,6 +57,8 @@ function escape(str) {
return str.replace(/</g, "&lt;");
}
console.log({ attachments });
const to_download = attachments
.split("\n")
.slice(1)
@ -47,6 +70,20 @@ const to_download = attachments
};
});
const attachment_info = attachments
.split("\n")
.slice(1)
.map((line) => line.split(" ").slice(3))
.filter((e) => e[0] != "<none>")
.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 */ `<hr/><h3>Załączniki:</h3>
<table><tbody>${attachment_info.map(
(a) =>
`<tr><th>${a.name}</th><td>${a.size}</td><td><em>${a.type}</em></td></tr>`
)}<tbody></table>
<ul>
${attachments_items}
</ul></body>`
@ -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(
/<body>/i,
`<body>` +
@ -99,13 +138,19 @@ html = html.replace(
<th>Do:</th>
<td>${escape(to)}</td>
</tr>
</table>`
</table>
<hr />`
);
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"
)}`;
}
}

28
package-lock.json generated

@ -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="
}
}
}

@ -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"
}
}
Loading…
Cancel
Save