-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
425 lines (371 loc) · 12.1 KB
/
Copy pathmain.ts
File metadata and controls
425 lines (371 loc) · 12.1 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import { accounts } from "./commands/accounts.ts";
import { mailboxes } from "./commands/mailboxes.ts";
import { list } from "./commands/list.ts";
import { show } from "./commands/show.ts";
import { search } from "./commands/search.ts";
import { send } from "./commands/send.ts";
import { draft } from "./commands/draft.ts";
import { mark } from "./commands/mark.ts";
import { move } from "./commands/move.ts";
import { junk } from "./commands/junk.ts";
import { attachments } from "./commands/attachments.ts";
import { unread } from "./commands/unread.ts";
import { today } from "./commands/today.ts";
import { flagged } from "./commands/flagged.ts";
import { reply } from "./commands/reply.ts";
import { forward } from "./commands/forward.ts";
export interface Flags {
[key: string]: string;
}
export interface ParsedArgs {
flags: Flags;
listFlags: Record<string, string[]>;
positional: string[];
}
const booleanFlags = new Set([
"json",
"unread",
"read",
"flagged",
"unflagged",
"html",
"help",
"h",
"all",
"send",
]);
export function parseArgs(args: string[]): ParsedArgs {
const flags: Flags = {};
const listFlags: Record<string, string[]> = {};
const positional: string[] = [];
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg === "--") {
positional.push(...args.slice(i + 1));
break;
}
if (arg.startsWith("--")) {
const key = arg.slice(2);
if (booleanFlags.has(key)) {
flags[key] = "true";
} else if (i + 1 < args.length && !args[i + 1].startsWith("-")) {
const val = args[++i];
if (key in flags) {
if (!listFlags[key]) listFlags[key] = [flags[key]];
listFlags[key].push(val);
}
flags[key] = val;
} else {
die(`flag --${key} requires a value`);
}
} else if (arg.startsWith("-") && arg.length === 2) {
const key = arg.slice(1);
if (booleanFlags.has(key)) {
flags[key] = "true";
} else if (i + 1 < args.length && !args[i + 1].startsWith("-")) {
flags[key] = args[++i];
} else {
die(`flag -${key} requires a value`);
}
} else {
positional.push(arg);
}
}
return { flags, listFlags, positional };
}
export function die(msg: string): never {
console.error(`nanomail: ${msg}`);
Deno.exit(1);
}
export function parseIntFlag(value: string, name: string, fallback: number): number {
const n = parseInt(value, 10);
if (isNaN(n)) die(`--${name} must be a number, got: ${value}`);
return n;
}
export function parseLimit(flags: Flags, fallback: number): number {
return Math.max(1, parseIntFlag(flags.limit || String(fallback), "limit", fallback));
}
export function parseOffset(flags: Flags): number {
return Math.max(0, parseIntFlag(flags.offset || "0", "offset", 0));
}
const validCommands = new Set([
"accounts",
"mailboxes",
"list",
"show",
"search",
"send",
"draft",
"reply",
"forward",
"mark",
"move",
"junk",
"attachments",
"unread",
"today",
"flagged",
]);
function printUsage(): void {
console.log(`Usage: nanomail <command> [options]
Commands:
accounts List email accounts
mailboxes List mailboxes for an account
list List messages in a mailbox
show Show full message details
search Search messages by subject/sender
unread Unread messages across all accounts
today Messages received today
flagged Flagged messages
send Send an email
draft Save a draft email
reply Reply to a message
forward Forward a message
mark Mark messages read/unread/flagged/unflagged
move Move a message to another mailbox
junk Mark as junk and move to Junk folder
attachments List or save message attachments
help Show help for a command
Global options:
--json Output JSON instead of human-readable text
Run 'nanomail help <command>' for details.`);
}
function printCommandHelp(command: string): void {
switch (command) {
case "accounts":
console.log(`Usage: nanomail accounts [--json]
List all configured email accounts.`);
break;
case "mailboxes":
console.log(`Usage: nanomail mailboxes -a <account> [--json]
List mailboxes for an account with unread counts.`);
break;
case "list":
console.log(`Usage: nanomail list -a <account> -m <mailbox> [options]
List messages in a mailbox. Shows most recent first.
Options:
-a <account> Account name (required)
-m <mailbox> Mailbox name (required)
--unread Show only unread messages
--flagged Show only flagged messages
--since <date> Show messages since date (ISO 8601)
--limit <n> Max messages to return (default: 20)
--offset <n> Skip first N messages
--json Output JSON`);
break;
case "show":
console.log(`Usage: nanomail show -a <account> -m <mailbox> --id <id> [--json]
Show full message details including body.
Options:
--html Include raw HTML source`);
break;
case "search":
console.log(`Usage: nanomail search <query> -a <account> [options]
Search messages by subject. Optionally filter by sender or mailbox.
Options:
-a <account> Account name (required)
-m <mailbox> Search within specific mailbox (default: all)
--from <sender> Filter by sender address/name
--limit <n> Max results (default: 20)
--offset <n> Skip first N results
--json Output JSON`);
break;
case "unread":
console.log(`Usage: nanomail unread [-a <account>] [--json]
Without -a: show unread messages from all accounts' INBOXes.
With -a: show unread messages from that account's INBOX.
Options:
-a <account> Filter to one account
--limit <n> Max messages (default: 50)
--json Output JSON`);
break;
case "today":
console.log(`Usage: nanomail today [-a <account>] [--json]
Messages received today across all accounts (or one with -a).
Scans INBOX only.
Options:
-a <account> Filter to one account
--limit <n> Max messages (default: 50)
--offset <n> Skip first N messages
--json Output JSON`);
break;
case "flagged":
console.log(`Usage: nanomail flagged [-a <account>] [-m <mailbox>] [--json]
Flagged messages across all accounts (or filtered).
Scans INBOX by default.
Options:
-a <account> Filter to one account
-m <mailbox> Specific mailbox (default: INBOX)
--limit <n> Max messages (default: 50)
--offset <n> Skip first N messages
--json Output JSON`);
break;
case "send":
console.log(`Usage: nanomail send -a <account> --to <addr> --subject <subj> --body <text>
Send an email immediately.
Options:
-a <account> Account to send from (required)
--to <address> Recipient (required)
--cc <address> CC recipient
--bcc <address> BCC recipient
--subject <text> Subject line (required)
--body <text> Email body (required)
--attachment <f> Attach file (repeatable)
--json Output JSON`);
break;
case "draft":
console.log(`Usage: nanomail draft -a <account> --to <addr> --subject <subj> --body <text>
nanomail draft reply -a <account> -m <mailbox> --id <id> --body <text>
nanomail draft forward -a <account> -m <mailbox> --id <id> --to <addr>
Create a draft in Mail.app's Drafts folder for review before sending.
Sub-commands 'reply' and 'forward' create reply/forward drafts.
Options (compose):
-a <account> Account to draft from (required)
--to <address> Recipient (required)
--cc <address> CC recipient
--bcc <address> BCC recipient
--subject <text> Subject line (required)
--body <text> Email body (required)
--attachment <f> Attach file (repeatable)
--json Output JSON
See 'nanomail help reply' and 'nanomail help forward' for sub-command options.`);
break;
case "mark":
console.log(`Usage: nanomail mark -a <account> -m <mailbox> --id <id> [flags]
Mark a message. At least one flag required.
Options:
--read Mark as read
--unread Mark as unread
--flagged Mark as flagged
--unflagged Remove flag
--json Output JSON`);
break;
case "move":
console.log(`Usage: nanomail move -a <account> -m <mailbox> --id <id> --to <target>
Move a message. Target can be "trash", "archive", or a mailbox name.
Options:
--json Output JSON`);
break;
case "junk":
console.log(`Usage: nanomail junk -a <account> -m <mailbox> --id <id>
Mark a message as junk and move to Junk/Spam folder.
Options:
--json Output JSON`);
break;
case "reply":
console.log(`Usage: nanomail reply -a <account> -m <mailbox> --id <id> --body <text> [options]
Reply to a message. Quotes the original message body.
Saves as draft by default. Use --send to send immediately.
Options:
-a <account> Account name (required)
-m <mailbox> Mailbox name (required)
--id <id> Message ID to reply to (required)
--body <text> Reply body text (required)
--all Reply to all recipients
--send Send immediately instead of saving as draft
--json Output JSON
Also available as: nanomail draft reply ...`);
break;
case "forward":
console.log(`Usage: nanomail forward -a <account> -m <mailbox> --id <id> --to <addr> [options]
Forward a message. Includes original message headers and body.
Saves as draft by default. Use --send to send immediately.
Attachments are NOT forwarded.
Options:
-a <account> Account name (required)
-m <mailbox> Mailbox name (required)
--id <id> Message ID to forward (required)
--to <address> Recipient address (required)
--body <text> Optional message above forwarded content
--send Send immediately instead of saving as draft
--json Output JSON
Also available as: nanomail draft forward ...`);
break;
case "attachments":
console.log(`Usage: nanomail attachments -a <account> -m <mailbox> --id <id> [--save <dir>]
List attachments, or save them to a directory.
Options:
--save <dir> Save attachments to directory
--json Output JSON`);
break;
default:
die(`unknown command: ${command}`);
}
}
async function main() {
const [command, ...rest] = Deno.args;
if (!command || command === "--help" || command === "-h") {
printUsage();
Deno.exit(0);
}
if (command === "help") {
const sub = rest[0];
if (sub) {
printCommandHelp(sub);
} else {
printUsage();
}
Deno.exit(0);
}
if (!validCommands.has(command)) {
die(`unknown command: ${command}`);
}
const { flags, listFlags, positional } = parseArgs(rest);
const json = flags.json === "true";
try {
switch (command) {
case "accounts":
await accounts(json);
break;
case "mailboxes":
await mailboxes(flags, json);
break;
case "list":
await list(flags, json);
break;
case "show":
await show(flags, json);
break;
case "search":
await search(flags, positional, json);
break;
case "send":
await send(flags, listFlags, json);
break;
case "draft":
await draft(flags, listFlags, positional, json);
break;
case "reply":
await reply(flags, json);
break;
case "forward":
await forward(flags, json);
break;
case "mark":
await mark(flags, json);
break;
case "move":
await move(flags, json);
break;
case "junk":
await junk(flags, json);
break;
case "attachments":
await attachments(flags, json);
break;
case "unread":
await unread(flags, json);
break;
case "today":
await today(flags, json);
break;
case "flagged":
await flagged(flags, json);
break;
}
} catch (err: unknown) {
const msg = err instanceof Error ? err.message : String(err);
die(msg);
}
}
main();