#!/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 */ `
Temat wiadomości: | ${subject} |
---|---|
Wysłano: | ${escape(date)} |
Od: | ${escape(from)} |
Do: | ${escape(to)} |