@forwardimpact/outpost 3.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +15 -0
- package/bin/fit-outpost.js +3 -0
- package/config/scheduler.json +34 -0
- package/package.json +63 -0
- package/src/agent-runner.js +189 -0
- package/src/index.js +5 -0
- package/src/kb-manager.js +228 -0
- package/src/outpost.js +417 -0
- package/src/scheduler.js +195 -0
- package/src/socket-server.js +336 -0
- package/src/state-manager.js +126 -0
- package/templates/.claude/agents/chief-of-staff.md +58 -0
- package/templates/.claude/agents/concierge.md +46 -0
- package/templates/.claude/agents/head-hunter.md +57 -0
- package/templates/.claude/agents/librarian.md +60 -0
- package/templates/.claude/agents/postman.md +47 -0
- package/templates/.claude/agents/recruiter.md +63 -0
- package/templates/.claude/settings.json +88 -0
- package/templates/.claude/skills/candidate-report/SKILL.md +139 -0
- package/templates/.claude/skills/candidate-report/references/report-template.html +203 -0
- package/templates/.claude/skills/candidate-report/references/report.css +438 -0
- package/templates/.claude/skills/candidate-report/references/rubric.md +76 -0
- package/templates/.claude/skills/candidate-report/scripts/render-pdf.mjs +48 -0
- package/templates/.claude/skills/deck-create/SKILL.md +75 -0
- package/templates/.claude/skills/deck-create/references/slide.css +35 -0
- package/templates/.claude/skills/deck-create/scripts/convert-to-pdf.mjs +47 -0
- package/templates/.claude/skills/deck-summarize/SKILL.md +139 -0
- package/templates/.claude/skills/deck-summarize/references/brief-template.md +85 -0
- package/templates/.claude/skills/deck-summarize/scripts/extract-pptx.mjs +210 -0
- package/templates/.claude/skills/doc-collab/SKILL.md +112 -0
- package/templates/.claude/skills/doc-create/SKILL.md +105 -0
- package/templates/.claude/skills/doc-create/scripts/convert-to-pdf.mjs +50 -0
- package/templates/.claude/skills/draft-emails/SKILL.md +141 -0
- package/templates/.claude/skills/draft-emails/references/template.md +26 -0
- package/templates/.claude/skills/draft-emails/scripts/scan-emails.mjs +66 -0
- package/templates/.claude/skills/draft-emails/scripts/send-email.mjs +139 -0
- package/templates/.claude/skills/extract-entities/SKILL.md +187 -0
- package/templates/.claude/skills/extract-entities/references/TEMPLATES.md +14 -0
- package/templates/.claude/skills/extract-entities/references/conditions.md +55 -0
- package/templates/.claude/skills/extract-entities/references/content.md +104 -0
- package/templates/.claude/skills/extract-entities/references/links.md +48 -0
- package/templates/.claude/skills/extract-entities/references/recruitment.md +57 -0
- package/templates/.claude/skills/extract-entities/references/resolution.md +71 -0
- package/templates/.claude/skills/extract-entities/references/sources.md +70 -0
- package/templates/.claude/skills/extract-entities/references/templates-conditions.md +36 -0
- package/templates/.claude/skills/extract-entities/references/templates-goals-priorities.md +63 -0
- package/templates/.claude/skills/extract-entities/references/templates-people-orgs.md +67 -0
- package/templates/.claude/skills/extract-entities/references/templates-projects-topics.md +68 -0
- package/templates/.claude/skills/extract-entities/scripts/state.mjs +131 -0
- package/templates/.claude/skills/hyprnote-follow/SKILL.md +174 -0
- package/templates/.claude/skills/hyprnote-follow/references/coaching.md +118 -0
- package/templates/.claude/skills/hyprnote-follow/references/debrief.md +59 -0
- package/templates/.claude/skills/hyprnote-follow/references/meeting-types.md +14 -0
- package/templates/.claude/skills/hyprnote-follow/scripts/follow.mjs +243 -0
- package/templates/.claude/skills/hyprnote-process/SKILL.md +147 -0
- package/templates/.claude/skills/hyprnote-process/references/extraction.md +84 -0
- package/templates/.claude/skills/hyprnote-process/references/sessions.md +81 -0
- package/templates/.claude/skills/hyprnote-process/scripts/scan.mjs +246 -0
- package/templates/.claude/skills/hyprnote-trim/SKILL.md +177 -0
- package/templates/.claude/skills/manage-tasks/SKILL.md +152 -0
- package/templates/.claude/skills/manage-tasks/references/format.md +73 -0
- package/templates/.claude/skills/meeting-prep/SKILL.md +180 -0
- package/templates/.claude/skills/organize-files/SKILL.md +146 -0
- package/templates/.claude/skills/organize-files/scripts/organize-by-type.mjs +105 -0
- package/templates/.claude/skills/organize-files/scripts/summarize.mjs +91 -0
- package/templates/.claude/skills/req-assess/SKILL.md +137 -0
- package/templates/.claude/skills/req-assess/references/interview-template.md +62 -0
- package/templates/.claude/skills/req-assess/references/panel-template.md +117 -0
- package/templates/.claude/skills/req-assess/references/rubric.md +65 -0
- package/templates/.claude/skills/req-decide/SKILL.md +163 -0
- package/templates/.claude/skills/req-decide/references/rubric.md +63 -0
- package/templates/.claude/skills/req-decide/references/template.md +118 -0
- package/templates/.claude/skills/req-forget/SKILL.md +154 -0
- package/templates/.claude/skills/req-forget/references/classify.md +23 -0
- package/templates/.claude/skills/req-forget/references/locations.md +59 -0
- package/templates/.claude/skills/req-forget/references/report-template.md +67 -0
- package/templates/.claude/skills/req-scan/SKILL.md +145 -0
- package/templates/.claude/skills/req-scan/references/fallbacks.md +76 -0
- package/templates/.claude/skills/req-scan/references/filters.md +83 -0
- package/templates/.claude/skills/req-scan/references/sources.md +76 -0
- package/templates/.claude/skills/req-scan/references/state.md +54 -0
- package/templates/.claude/skills/req-scan/references/template.md +36 -0
- package/templates/.claude/skills/req-scan/scripts/state.mjs +396 -0
- package/templates/.claude/skills/req-screen/SKILL.md +151 -0
- package/templates/.claude/skills/req-screen/references/rubric.md +94 -0
- package/templates/.claude/skills/req-screen/references/template.md +91 -0
- package/templates/.claude/skills/req-track/SKILL.md +189 -0
- package/templates/.claude/skills/req-track/references/fields.md +114 -0
- package/templates/.claude/skills/req-track/references/signals.md +42 -0
- package/templates/.claude/skills/req-track/references/statuses.md +32 -0
- package/templates/.claude/skills/req-track/references/templates.md +127 -0
- package/templates/.claude/skills/req-workday/SKILL.md +162 -0
- package/templates/.claude/skills/req-workday/references/field-mapping.md +40 -0
- package/templates/.claude/skills/req-workday/references/status-mapping.md +38 -0
- package/templates/.claude/skills/req-workday/references/templates.md +126 -0
- package/templates/.claude/skills/req-workday/references/xlsx-format.md +56 -0
- package/templates/.claude/skills/req-workday/scripts/parse-workday.mjs +313 -0
- package/templates/.claude/skills/send-chat/SKILL.md +170 -0
- package/templates/.claude/skills/sync-apple-calendar/SKILL.md +145 -0
- package/templates/.claude/skills/sync-apple-calendar/references/SCHEMA.md +80 -0
- package/templates/.claude/skills/sync-apple-calendar/scripts/query.mjs +307 -0
- package/templates/.claude/skills/sync-apple-calendar/scripts/sync.mjs +332 -0
- package/templates/.claude/skills/sync-apple-mail/SKILL.md +151 -0
- package/templates/.claude/skills/sync-apple-mail/references/SCHEMA.md +115 -0
- package/templates/.claude/skills/sync-apple-mail/scripts/parse-emlx.mjs +389 -0
- package/templates/.claude/skills/sync-apple-mail/scripts/sync-helpers.mjs +372 -0
- package/templates/.claude/skills/sync-apple-mail/scripts/sync.mjs +239 -0
- package/templates/.claude/skills/sync-teams/SKILL.md +177 -0
- package/templates/.claude/skills/sync-teams/scripts/idb-reader.mjs +147 -0
- package/templates/.claude/skills/sync-teams/scripts/leveldb-reader.mjs +288 -0
- package/templates/.claude/skills/sync-teams/scripts/sync.mjs +381 -0
- package/templates/.claude/skills/upstream-skill/SKILL.md +130 -0
- package/templates/.claude/skills/upstream-skill/references/examples.md +80 -0
- package/templates/.claude/skills/weekly-update/SKILL.md +141 -0
- package/templates/.claude/skills/weekly-update/references/template.md +55 -0
- package/templates/CLAUDE.md +134 -0
- package/templates/USER.md +3 -0
- package/templates/apm.yml +5 -0
- package/templates/knowledge/Briefings/.gitkeep +0 -0
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
/**
|
|
3
|
+
* Parse a macOS Mail .emlx or .partial.emlx file and output the plain text body.
|
|
4
|
+
*
|
|
5
|
+
* The .emlx format is: first line = byte count, then RFC822 message, then Apple
|
|
6
|
+
* plist. This script reads the RFC822 portion, walks MIME parts to find
|
|
7
|
+
* text/plain, and prints it to stdout. If the email is HTML-only, falls back to
|
|
8
|
+
* stripping tags and decoding entities.
|
|
9
|
+
*
|
|
10
|
+
* Also exports `parseEmlx()` and `extractBody()` for use by sync-apple-mail.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
if (process.argv.includes("-h") || process.argv.includes("--help")) {
|
|
14
|
+
console.log(`parse-emlx — extract plain text from .emlx files
|
|
15
|
+
|
|
16
|
+
Usage: node scripts/parse-emlx.mjs <path-to-emlx-file> [-h|--help]
|
|
17
|
+
|
|
18
|
+
Parses a macOS Mail .emlx or .partial.emlx file and prints the plain text
|
|
19
|
+
body to stdout. Falls back to stripping HTML tags for HTML-only emails.`);
|
|
20
|
+
process.exit(0);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
import { readFileSync } from "node:fs";
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Strip HTML tags and convert to plain text.
|
|
27
|
+
* @param {string} html
|
|
28
|
+
* @returns {string}
|
|
29
|
+
*/
|
|
30
|
+
export function htmlToText(html) {
|
|
31
|
+
let text = html;
|
|
32
|
+
// Remove style and script blocks
|
|
33
|
+
text = text.replace(/<(style|script)[^>]*>[\s\S]*?<\/\1>/gi, "");
|
|
34
|
+
// Replace br and p tags with newlines
|
|
35
|
+
text = text.replace(/<br\s*\/?>/gi, "\n");
|
|
36
|
+
text = text.replace(/<\/p>/gi, "\n");
|
|
37
|
+
// Strip remaining tags
|
|
38
|
+
text = text.replace(/<[^>]+>/g, "");
|
|
39
|
+
// Decode common HTML entities
|
|
40
|
+
text = decodeEntities(text);
|
|
41
|
+
// Collapse whitespace
|
|
42
|
+
text = text.replace(/[ \t]+/g, " ");
|
|
43
|
+
text = text.replace(/\n{3,}/g, "\n\n");
|
|
44
|
+
return text.trim();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Decode HTML entities. Handles named entities and numeric references.
|
|
49
|
+
* @param {string} text
|
|
50
|
+
* @returns {string}
|
|
51
|
+
*/
|
|
52
|
+
function decodeEntities(text) {
|
|
53
|
+
const named = {
|
|
54
|
+
amp: "&",
|
|
55
|
+
lt: "<",
|
|
56
|
+
gt: ">",
|
|
57
|
+
quot: '"',
|
|
58
|
+
apos: "'",
|
|
59
|
+
nbsp: " ",
|
|
60
|
+
ndash: "–",
|
|
61
|
+
mdash: "—",
|
|
62
|
+
lsquo: "\u2018",
|
|
63
|
+
rsquo: "\u2019",
|
|
64
|
+
ldquo: "\u201C",
|
|
65
|
+
rdquo: "\u201D",
|
|
66
|
+
hellip: "…",
|
|
67
|
+
copy: "©",
|
|
68
|
+
reg: "®",
|
|
69
|
+
trade: "™",
|
|
70
|
+
bull: "•",
|
|
71
|
+
middot: "·",
|
|
72
|
+
ensp: "\u2002",
|
|
73
|
+
emsp: "\u2003",
|
|
74
|
+
thinsp: "\u2009",
|
|
75
|
+
zwnj: "\u200C",
|
|
76
|
+
zwj: "\u200D",
|
|
77
|
+
};
|
|
78
|
+
return text.replace(/&(#x?[0-9a-fA-F]+|[a-zA-Z]+);/g, (match, ref) => {
|
|
79
|
+
if (ref.startsWith("#x") || ref.startsWith("#X")) {
|
|
80
|
+
const code = parseInt(ref.slice(2), 16);
|
|
81
|
+
return code ? String.fromCodePoint(code) : match;
|
|
82
|
+
}
|
|
83
|
+
if (ref.startsWith("#")) {
|
|
84
|
+
const code = parseInt(ref.slice(1), 10);
|
|
85
|
+
return code ? String.fromCodePoint(code) : match;
|
|
86
|
+
}
|
|
87
|
+
return named[ref.toLowerCase()] ?? match;
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Parse MIME headers from a raw buffer.
|
|
93
|
+
* Returns { headers: Map<lowercase-name, value[]>, bodyOffset }.
|
|
94
|
+
* @param {Buffer} raw
|
|
95
|
+
* @returns {{ headers: Map<string, string[]>, bodyOffset: number }}
|
|
96
|
+
*/
|
|
97
|
+
function pushHeader(headers, name, value) {
|
|
98
|
+
if (!name) return;
|
|
99
|
+
const arr = headers.get(name) ?? [];
|
|
100
|
+
arr.push(value);
|
|
101
|
+
headers.set(name, arr);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function parseHeaders(raw) {
|
|
105
|
+
const headers = new Map();
|
|
106
|
+
let i = 0;
|
|
107
|
+
let currentName = "";
|
|
108
|
+
let currentValue = "";
|
|
109
|
+
|
|
110
|
+
while (i < raw.length) {
|
|
111
|
+
// Find end of line
|
|
112
|
+
let eol = raw.indexOf(0x0a, i); // \n
|
|
113
|
+
if (eol === -1) eol = raw.length;
|
|
114
|
+
const lineEnd = eol > i && raw[eol - 1] === 0x0d ? eol - 1 : eol; // strip \r
|
|
115
|
+
const line = raw.subarray(i, lineEnd).toString("utf-8");
|
|
116
|
+
i = eol + 1;
|
|
117
|
+
|
|
118
|
+
// Empty line = end of headers
|
|
119
|
+
if (line === "") {
|
|
120
|
+
pushHeader(headers, currentName, currentValue);
|
|
121
|
+
return { headers, bodyOffset: i };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Continuation line (starts with whitespace)
|
|
125
|
+
if (line[0] === " " || line[0] === "\t") {
|
|
126
|
+
currentValue += " " + line.trimStart();
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// New header — save previous
|
|
131
|
+
pushHeader(headers, currentName, currentValue);
|
|
132
|
+
|
|
133
|
+
const colonIdx = line.indexOf(":");
|
|
134
|
+
if (colonIdx === -1) continue;
|
|
135
|
+
currentName = line.slice(0, colonIdx).toLowerCase().trim();
|
|
136
|
+
currentValue = line.slice(colonIdx + 1).trim();
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
pushHeader(headers, currentName, currentValue);
|
|
140
|
+
return { headers, bodyOffset: raw.length };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Parse Content-Type header value.
|
|
145
|
+
* @param {string} value - e.g. 'text/plain; charset="utf-8"; boundary="abc"'
|
|
146
|
+
* @returns {{ type: string, params: Record<string, string> }}
|
|
147
|
+
*/
|
|
148
|
+
function parseContentType(value) {
|
|
149
|
+
const parts = value.split(";");
|
|
150
|
+
const type = (parts[0] ?? "").trim().toLowerCase();
|
|
151
|
+
const params = {};
|
|
152
|
+
for (let k = 1; k < parts.length; k++) {
|
|
153
|
+
const eq = parts[k].indexOf("=");
|
|
154
|
+
if (eq === -1) continue;
|
|
155
|
+
const name = parts[k].slice(0, eq).trim().toLowerCase();
|
|
156
|
+
let val = parts[k].slice(eq + 1).trim();
|
|
157
|
+
if (val.startsWith('"') && val.endsWith('"')) val = val.slice(1, -1);
|
|
158
|
+
params[name] = val;
|
|
159
|
+
}
|
|
160
|
+
return { type, params };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Decode a quoted-printable encoded string to a byte array.
|
|
165
|
+
* Handles soft line breaks (=\r\n) and =XX hex escapes.
|
|
166
|
+
* @param {string} str
|
|
167
|
+
* @returns {number[]}
|
|
168
|
+
*/
|
|
169
|
+
function decodeQuotedPrintableBytes(str) {
|
|
170
|
+
const bytes = [];
|
|
171
|
+
for (let j = 0; j < str.length; j++) {
|
|
172
|
+
if (str[j] !== "=" || j + 2 >= str.length) {
|
|
173
|
+
bytes.push(str.charCodeAt(j));
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
if (str[j + 1] === "\r" || str[j + 1] === "\n") {
|
|
177
|
+
j = skipSoftLineBreak(str, j + 1);
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
const code = parseInt(str.slice(j + 1, j + 3), 16);
|
|
181
|
+
if (!isNaN(code)) {
|
|
182
|
+
bytes.push(code);
|
|
183
|
+
j += 2;
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
bytes.push(str.charCodeAt(j));
|
|
187
|
+
}
|
|
188
|
+
return bytes;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Advance past a soft line break (\r, \n, or \r\n).
|
|
193
|
+
* Returns the index of the last character consumed.
|
|
194
|
+
*/
|
|
195
|
+
function skipSoftLineBreak(str, pos) {
|
|
196
|
+
if (str[pos] === "\r" && pos + 1 < str.length && str[pos + 1] === "\n") {
|
|
197
|
+
return pos + 1;
|
|
198
|
+
}
|
|
199
|
+
return pos;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Decode a MIME body payload according to Content-Transfer-Encoding.
|
|
204
|
+
* @param {Buffer} data
|
|
205
|
+
* @param {string} encoding
|
|
206
|
+
* @returns {Buffer}
|
|
207
|
+
*/
|
|
208
|
+
function decodePayload(data, encoding) {
|
|
209
|
+
const enc = (encoding ?? "").toLowerCase().trim();
|
|
210
|
+
if (enc === "base64") {
|
|
211
|
+
return Buffer.from(data.toString("ascii").replace(/\s/g, ""), "base64");
|
|
212
|
+
}
|
|
213
|
+
if (enc === "quoted-printable") {
|
|
214
|
+
return Buffer.from(decodeQuotedPrintableBytes(data.toString("ascii")));
|
|
215
|
+
}
|
|
216
|
+
return data;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Decode text from a buffer using the given charset.
|
|
221
|
+
* @param {Buffer} data
|
|
222
|
+
* @param {string} [charset]
|
|
223
|
+
* @returns {string}
|
|
224
|
+
*/
|
|
225
|
+
function decodeText(data, charset) {
|
|
226
|
+
const cs = (charset ?? "utf-8").toLowerCase().replace(/^(x-|iso_)/i, "iso-");
|
|
227
|
+
try {
|
|
228
|
+
const decoder = new TextDecoder(cs, { fatal: false });
|
|
229
|
+
return decoder.decode(data);
|
|
230
|
+
} catch {
|
|
231
|
+
return data.toString("utf-8");
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Parse a MIME part from raw bytes. Returns { contentType, charset, body }.
|
|
237
|
+
* @param {Buffer} raw
|
|
238
|
+
* @returns {{ contentType: string, charset: string, body: Buffer, headers: Map<string, string[]> }}
|
|
239
|
+
*/
|
|
240
|
+
function parsePart(raw) {
|
|
241
|
+
const { headers, bodyOffset } = parseHeaders(raw);
|
|
242
|
+
const ctHeader = (headers.get("content-type") ?? ["text/plain"])[0];
|
|
243
|
+
const { type, params } = parseContentType(ctHeader);
|
|
244
|
+
const encoding = (headers.get("content-transfer-encoding") ?? ["7bit"])[0];
|
|
245
|
+
const bodyRaw = raw.subarray(bodyOffset);
|
|
246
|
+
const body = decodePayload(bodyRaw, encoding);
|
|
247
|
+
return {
|
|
248
|
+
contentType: type,
|
|
249
|
+
charset: params.charset,
|
|
250
|
+
body,
|
|
251
|
+
headers,
|
|
252
|
+
boundary: params.boundary,
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Walk all MIME parts of a message (like Python's email.walk()).
|
|
258
|
+
* @param {Buffer} raw
|
|
259
|
+
* @returns {Array<{ contentType: string, charset: string, body: Buffer }>}
|
|
260
|
+
*/
|
|
261
|
+
function extractNextPart(raw, delim, pos) {
|
|
262
|
+
const start = raw.indexOf(delim, pos);
|
|
263
|
+
if (start === -1) return null;
|
|
264
|
+
let afterDelim = start + delim.length;
|
|
265
|
+
if (
|
|
266
|
+
afterDelim + 1 < raw.length &&
|
|
267
|
+
raw[afterDelim] === 0x2d &&
|
|
268
|
+
raw[afterDelim + 1] === 0x2d
|
|
269
|
+
) {
|
|
270
|
+
return null;
|
|
271
|
+
}
|
|
272
|
+
while (
|
|
273
|
+
afterDelim < raw.length &&
|
|
274
|
+
(raw[afterDelim] === 0x0d || raw[afterDelim] === 0x0a)
|
|
275
|
+
) {
|
|
276
|
+
afterDelim++;
|
|
277
|
+
}
|
|
278
|
+
const nextBoundary = raw.indexOf(delim, afterDelim);
|
|
279
|
+
const partEnd = nextBoundary === -1 ? raw.length : nextBoundary;
|
|
280
|
+
let trimEnd = partEnd;
|
|
281
|
+
if (trimEnd > 0 && raw[trimEnd - 1] === 0x0a) trimEnd--;
|
|
282
|
+
if (trimEnd > 0 && raw[trimEnd - 1] === 0x0d) trimEnd--;
|
|
283
|
+
return {
|
|
284
|
+
subRaw: raw.subarray(afterDelim, trimEnd),
|
|
285
|
+
nextPos: nextBoundary === -1 ? raw.length : nextBoundary,
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function walkParts(raw) {
|
|
290
|
+
const part = parsePart(raw);
|
|
291
|
+
if (!part.contentType.startsWith("multipart/")) {
|
|
292
|
+
return [part];
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const boundary = part.boundary;
|
|
296
|
+
if (!boundary) return [part];
|
|
297
|
+
|
|
298
|
+
const delim = Buffer.from(`--${boundary}`);
|
|
299
|
+
const bodyStart = raw.indexOf(delim);
|
|
300
|
+
if (bodyStart === -1) return [part];
|
|
301
|
+
|
|
302
|
+
const parts = [];
|
|
303
|
+
let pos = bodyStart;
|
|
304
|
+
|
|
305
|
+
while (pos < raw.length) {
|
|
306
|
+
const result = extractNextPart(raw, delim, pos);
|
|
307
|
+
if (!result) break;
|
|
308
|
+
parts.push(...walkParts(result.subRaw));
|
|
309
|
+
pos = result.nextPos;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
return parts;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Extract plain text body from raw RFC822 bytes, with HTML fallback.
|
|
317
|
+
* @param {Buffer} raw
|
|
318
|
+
* @returns {string | null}
|
|
319
|
+
*/
|
|
320
|
+
export function extractBody(raw) {
|
|
321
|
+
const parts = walkParts(raw);
|
|
322
|
+
let textBody = null;
|
|
323
|
+
let htmlBody = null;
|
|
324
|
+
|
|
325
|
+
for (const part of parts) {
|
|
326
|
+
if (part.contentType === "text/plain" && textBody === null) {
|
|
327
|
+
textBody = decodeText(part.body, part.charset);
|
|
328
|
+
} else if (part.contentType === "text/html" && htmlBody === null) {
|
|
329
|
+
htmlBody = decodeText(part.body, part.charset);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
if (textBody) return textBody;
|
|
334
|
+
if (htmlBody) return htmlToText(htmlBody);
|
|
335
|
+
return null;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Parse an .emlx file and return the body text.
|
|
340
|
+
* @param {string} filePath
|
|
341
|
+
* @returns {string | null}
|
|
342
|
+
*/
|
|
343
|
+
export function parseEmlx(filePath) {
|
|
344
|
+
const data = readFileSync(filePath);
|
|
345
|
+
// First line is the byte count
|
|
346
|
+
const newline = data.indexOf(0x0a);
|
|
347
|
+
const byteCount = parseInt(data.subarray(0, newline).toString("ascii"), 10);
|
|
348
|
+
const raw = data.subarray(newline + 1, newline + 1 + byteCount);
|
|
349
|
+
return extractBody(raw);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Parse an .emlx file and print From, Date, body to stdout.
|
|
354
|
+
* @param {string} filePath
|
|
355
|
+
*/
|
|
356
|
+
function parseAndPrint(filePath) {
|
|
357
|
+
const data = readFileSync(filePath);
|
|
358
|
+
const newline = data.indexOf(0x0a);
|
|
359
|
+
const byteCount = parseInt(data.subarray(0, newline).toString("ascii"), 10);
|
|
360
|
+
const raw = data.subarray(newline + 1, newline + 1 + byteCount);
|
|
361
|
+
const { headers } = parseHeaders(raw);
|
|
362
|
+
|
|
363
|
+
const from = (headers.get("from") ?? ["Unknown"])[0];
|
|
364
|
+
const date = (headers.get("date") ?? [""])[0];
|
|
365
|
+
console.log(`From: ${from}`);
|
|
366
|
+
console.log(`Date: ${date}`);
|
|
367
|
+
console.log("---");
|
|
368
|
+
|
|
369
|
+
const body = extractBody(raw);
|
|
370
|
+
if (body) console.log(body);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// --- CLI ---
|
|
374
|
+
if (
|
|
375
|
+
process.argv[1] &&
|
|
376
|
+
(process.argv[1].endsWith("parse-emlx.mjs") ||
|
|
377
|
+
process.argv[1].endsWith("parse-emlx"))
|
|
378
|
+
) {
|
|
379
|
+
if (process.argv.length !== 3) {
|
|
380
|
+
console.error("Usage: node scripts/parse-emlx.mjs <path>");
|
|
381
|
+
process.exit(1);
|
|
382
|
+
}
|
|
383
|
+
try {
|
|
384
|
+
parseAndPrint(process.argv[2]);
|
|
385
|
+
} catch (err) {
|
|
386
|
+
console.error(`Error: ${err.message}`);
|
|
387
|
+
process.exit(1);
|
|
388
|
+
}
|
|
389
|
+
}
|