aboutsummaryrefslogtreecommitdiffstats
path: root/src/format/mail.js
blob: ca412f4523e3ca89df83232a85930c248ac02aa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// WhatsApp messages -> RFC 5322 (eml) / mboxrd. The base piece for neomutt:
// `--format mbox` opens directly with `neomutt -f`, and a Maildir bridge (like gwsmail)
// only has to write the `--format eml` output to ~/Mail/<account>/<folder>/new.
//
// Conventions (the sending side must follow them):
//   addresses     the JID as is: 5491122334455@c.us, 1203630...@g.us, 1234@lid
//   Message-ID    <stanzaId@wa>        (stanzaId = the "stanzaId" JSON field)
//   In-Reply-To   <quotedStanzaId@wa>  -> neomutt threads quoted replies
//   Reply-To      in groups, the group (so `r` answers the group, not privately)
//   X-WA-Id       serialized ID, for later actions (react, delete, download)
//   X-WA-Chat-Id  chat it belongs to

import crypto from 'node:crypto';
import { describeContent } from './text.js';

const ASCII = /^[\x20-\x7e]*$/;

function encodedWords(s) {
    const words = [];
    let chunk = '';
    for (const ch of s) {
        if (Buffer.byteLength(chunk + ch) > 45) {
            words.push(chunk);
            chunk = '';
        }
        chunk += ch;
    }
    if (chunk) words.push(chunk);
    return words.map((w) => `=?UTF-8?B?${Buffer.from(w).toString('base64')}?=`).join('\n ');
}

const headerText = (s) => (ASCII.test(s) ? s : encodedWords(s));

function address(name, addr) {
    if (!addr) return 'undisclosed-recipients:;';
    if (!name || name === addr.split('@')[0]) return `<${addr}>`;
    const display = ASCII.test(name) ? `"${name.replace(/[\\"]/g, '\\$&')}"` : encodedWords(name);
    return `${display} <${addr}>`;
}

function rfc2822Date(ts) {
    const d = new Date(ts * 1000);
    const days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
    const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    const p = (n) => String(n).padStart(2, '0');
    const off = -d.getTimezoneOffset();
    const sign = off >= 0 ? '+' : '-';
    return `${days[d.getDay()]}, ${d.getDate()} ${months[d.getMonth()]} ${d.getFullYear()} ` +
        `${p(d.getHours())}:${p(d.getMinutes())}:${p(d.getSeconds())} ` +
        `${sign}${p(Math.floor(Math.abs(off) / 60))}${p(Math.abs(off) % 60)}`;
}

function quotedPrintable(text) {
    const out = [];
    for (const line of text.split('\n')) {
        const bytes = Buffer.from(line);
        let cur = '';
        for (let i = 0; i < bytes.length; i++) {
            const b = bytes[i];
            const last = i === bytes.length - 1;
            let tok;
            // "From " at the start of a line becomes =46rom: the mbox then needs no escaping
            // (and mboxo readers such as mutt do not show ">From").
            const fromLine = i === 0 && b === 70 && line.startsWith('From ');
            if (!fromLine && ((b >= 33 && b <= 126 && b !== 61) || ((b === 32 || b === 9) && !last))) {
                tok = String.fromCharCode(b);
            }
            if (!tok) tok = `=${b.toString(16).toUpperCase().padStart(2, '0')}`;
            if (cur.length + tok.length > 75) {
                out.push(`${cur}=`);
                cur = '';
            }
            cur += tok;
        }
        out.push(cur);
    }
    return out.join('\n');
}

const base64Lines = (b64) => b64.replace(/\s+/g, '').match(/.{1,76}/g)?.join('\n') ?? '';

function filenameParams(name) {
    if (ASCII.test(name)) return `; name="${name.replace(/"/g, '')}"`;
    return `; name="${encodedWords(name).replace(/\n /g, '')}"`;
}
function dispositionParams(name) {
    if (ASCII.test(name)) return `; filename="${name.replace(/"/g, '')}"`;
    return `; filename*=UTF-8''${encodeURIComponent(name)}`;
}

function bodyText(m) {
    const lines = [];
    const content = describeContent(m);
    if (m.hasMedia && !m.media?.data) {
        const size = m.media?.size ? `, ${Math.round(m.media.size / 1024)} KB` : '';
        lines.push(`[attachment not downloaded: ${m.media?.mimetype ?? m.type}${size}]`);
        lines.push(`[wa messages download --params '{"messageId":"${m.id}"}']`);
        if (m.body) lines.push('', m.body);
    } else {
        lines.push(m.hasMedia ? m.body : content);
    }
    if (m.location) lines.push('', `https://maps.google.com/?q=${m.location.latitude},${m.location.longitude}`);
    if (m.links?.length && m.type !== 'chat') lines.push('', ...m.links);
    return lines.join('\n');
}

export function messageToEml(m) {
    const isGroup = m.chatId?.endsWith('@g.us');
    const subject = describeContent(m).split('\n')[0].slice(0, 80) || '(no text)';
    const from = address(m.senderName, m.sender);
    const to = isGroup ? address(m.chatName, m.chatId) : address(m.fromMe ? m.chatName : null, m.fromMe ? m.chatId : m.to);

    const h = [
        `From: ${from}`,
        `To: ${to}`,
        ...(isGroup ? [`Reply-To: ${address(m.chatName, m.chatId)}`] : []),
        `Subject: ${headerText(subject)}`,
        `Date: ${rfc2822Date(m.timestamp)}`,
        `Message-ID: <${m.stanzaId}@wa>`,
        ...(m.quotedStanzaId ? [`In-Reply-To: <${m.quotedStanzaId}@wa>`, `References: <${m.quotedStanzaId}@wa>`] : []),
        `X-WA-Id: ${m.id}`,
        `X-WA-Chat-Id: ${m.chatId}`,
        ...(m.chatName ? [`X-WA-Chat-Name: ${headerText(m.chatName)}`] : []),
        `X-WA-Type: ${m.type}`,
        `X-WA-From-Me: ${m.fromMe ? 'yes' : 'no'}`,
        ...(m.isForwarded ? ['X-WA-Forwarded: yes'] : []),
        'MIME-Version: 1.0',
    ];

    const textPart = [
        'Content-Type: text/plain; charset=utf-8',
        'Content-Transfer-Encoding: quoted-printable',
        '',
        quotedPrintable(bodyText(m)),
    ].join('\n');

    const attachments = [];
    if (m.media?.data) {
        const name = m.media.filename || `${m.stanzaId}`;
        const mimetype = (m.media.mimetype || 'application/octet-stream').split(';')[0];
        attachments.push([
            `Content-Type: ${mimetype}${filenameParams(name)}`,
            'Content-Transfer-Encoding: base64',
            `Content-Disposition: ${mimetype.startsWith('image/') ? 'inline' : 'attachment'}${dispositionParams(name)}`,
            '',
            base64Lines(m.media.data),
        ].join('\n'));
    }
    (m.vCards || []).forEach((vc, i) => attachments.push([
        `Content-Type: text/vcard; charset=utf-8; name="contacto-${i + 1}.vcf"`,
        'Content-Transfer-Encoding: quoted-printable',
        `Content-Disposition: attachment; filename="contacto-${i + 1}.vcf"`,
        '',
        quotedPrintable(vc.replace(/\r\n/g, '\n')),
    ].join('\n')));

    if (!attachments.length) return `${h.join('\n')}\n${textPart}\n`;

    const boundary = `wa-${crypto.randomBytes(12).toString('hex')}`;
    return [
        ...h,
        `Content-Type: multipart/mixed; boundary="${boundary}"`,
        '',
        ...[textPart, ...attachments].map((part) => `--${boundary}\n${part}`),
        `--${boundary}--`,
        '',
    ].join('\n');
}

function asMessages(data) {
    const list = Array.isArray(data) ? data : [data];
    if (!list.every((m) => m && typeof m === 'object' && 'stanzaId' in m)) {
        throw Object.assign(new Error('the eml/mbox formats only apply to messages'), { code: 'EINVAL' });
    }
    return list;
}

export function toEml(data