You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
4.4 KiB
JavaScript

#!/usr/bin/zx --quiet
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;
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_obj["body-html"] && mail_obj["body-html"].match(/body/i) !== null
? 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;
function getSimpleHeader(body, header_name) {
const regexp = new RegExp(`:${header_name} (.*)\n`);
console.log("get simple header", header_name, body);
return body.match(regexp)[1];
}
function formatAddress(sexp) {
sexp = sexp
.replace(/(^\(\(|\)\)$)/g, "")
.replace(/"? \. "/, " <")
.replace(/"$/, ">")
.replace(/^"/, "")
.replace(/^nil <(.*)>$/, "$1");
return sexp;
}
function escape(str) {
return str.replace(/</g, "&lt;");
}
const to_download = attachments
.split("\n")
.slice(1)
.map((line) => {
//someimtes images have the wrong mime type, like for example in photos from straż miejska confirmation emails
if (line.includes(".jpg") && line.includes("application/octet-stream")) {
return line.replace("application/octet-stream", "image/jpg");
} else {
return line;
}
})
.filter((line) => line.includes("image"))
.map((line) => {
return {
filename: line.match(/\d+ (.*) image\//)[1],
mime: line.match(/image\/[^ ]+/)[0],
};
});
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}`;
const base64 = (await $`base64 -w 0 < ${`/tmp/${filename}`}`).stdout;
return { filename, base64, mime };
})
);
const attachments_items = files
.map(
({ filename, mime, base64 }) => /* HTML */ `<li>
${filename}<img style="width: 100%" src="data:${mime};base64,${base64}" />
</li>`
)
.join("\n");
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>`
);
const subject = getSimpleHeader(mail_body, "subject").slice(1, -1); // slice to remove "
const from = formatAddress(getSimpleHeader(mail_body, "from"));
const to = formatAddress(getSimpleHeader(mail_body, "to"));
const date = await formatDate(getSimpleHeader(mail_body, "date"));
html = html.replace(
/<body>/i,
`<meta charset="utf-8"><body>` +
/* HTML */ `<h1>${subject}</h1>
<table>
<tr>
<th>Temat wiadomości:</th>
<td>${subject}</td>
</tr>
<tr>
<th>Wysłano:</th>
<td>${escape(date)}</td>
</tr>
<tr>
<th>Od:</th>
<td>${escape(from)}</td>
</tr>
<tr>
<th>Do:</th>
<td>${escape(to)}</td>
</tr>
</table>
<hr />`
);
const output = (await $`mktemp --suffix=.html`).stdout.replace("\n", "");
await fs.writeFile(output, html);
const output_file = `${resolve(path, "../")}/${basename(path)}.pdf`.replace(
",",
" "
);
await $`wkhtmltopdf ${output} ${output_file}`;
// console.log(pwd + "output.html");
// console.log(mail_body);
async function formatDate(sexp) {
const parsed = (await $`emacsclient --eval ${`(decode-time '${sexp})`}`)
.stdout;
const [, minutes, hours, date, month, year] = parsed.split(" ");
return `${year}-${month.padStart(2, "0")}-${date} ${hours}:${minutes.padStart(
2,
"0"
)}`;
}