#!/usr/bin/zx --quiet import { resolve, basename } from "path"; const pwd = (await $`pwd`).stdout.replace("\n", ""); $`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, '"' ); let html = mail_body.match(/\.*\<\/html\>/gis)[0]; const attachments = (await $`mu extract ${path}`).stdout; function getSimpleHeader(body, header_name) { const regexp = new RegExp(`:${header_name} (.*)\n`); 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(/ line.includes("image")) .map((line) => { return { filename: line.match(/\d+ (.*) image\//)[1], mime: line.match(/image\/[^ ]+/)[0], }; }); 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 */ `
  • ${filename}
  • ` ) .join("\n"); html = html.replace( /<\/body>/i, /* HTML */ `

    Załączniki:

    ` ); 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")); // console.log({ subject, from, to, date }); html = html.replace( //i, `` + /* HTML */ `

    ${subject}

    Temat wiadomości: ${subject}
    Wysłano: ${escape(date)}
    Od: ${escape(from)}
    Do: ${escape(to)}
    ` ); const output = (await $`mktemp --suffix=.html`).stdout.replace("\n", ""); await fs.writeFile(output, html); await $`wkhtmltopdf ${output} ${`${basename(path)}.pdf`}`; // 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" )}`; }