@noisybrood/cli 0.0.0-stage → 0.2.0
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 +202 -0
- package/README.md +83 -2
- package/dist/brood/api/src/client.js +58 -0
- package/dist/brood/api/src/origin.js +7 -0
- package/dist/brood/api/src/values.js +96 -0
- package/dist/brood/figures/index.js +4 -0
- package/dist/brood/tokens/dark.js +2 -0
- package/dist/brood/tokens/src/wren.js +58 -0
- package/dist/brood.js +33 -0
- package/dist/client.js +100 -0
- package/dist/commands.js +596 -0
- package/dist/io.js +50 -0
- package/dist/keychain.js +104 -0
- package/dist/main.js +213 -0
- package/dist/out.js +53 -0
- package/dist/sign-in.js +129 -0
- package/dist/store.js +138 -0
- package/dist/version.js +16 -0
- package/dist/wren.js +147 -0
- package/package.json +37 -4
package/dist/commands.js
ADDED
|
@@ -0,0 +1,596 @@
|
|
|
1
|
+
import { CLASSIFICATION_STATES, TONE_TITLES, } from "./brood/api/src/client.js";
|
|
2
|
+
import { API_PAGE_MAX, API_STRETCH_DAYS, PORTRAIT_DAYS } from "./brood/figures/index.js";
|
|
3
|
+
import { call } from "./client.js";
|
|
4
|
+
import { clean, Refusal, Refused } from "./io.js";
|
|
5
|
+
import { day, dim, json, more, rows, state } from "./out.js";
|
|
6
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
7
|
+
/**
|
|
8
|
+
* A lookup by name reads every page, so a match on a later one is never
|
|
9
|
+
* missed. This bounds it against an answer that never ends; reaching it is
|
|
10
|
+
* said, never taken as "none".
|
|
11
|
+
*/
|
|
12
|
+
const LOOKUP_PAGES = 100;
|
|
13
|
+
const tooMany = (name) => new Refusal(`Brood read ${LOOKUP_PAGES} pages looking for ${clean(name)} and stopped. Name it by its id.`);
|
|
14
|
+
/** Refuses options a command was given that it takes only in its other form. */
|
|
15
|
+
const without = (options, names, form) => {
|
|
16
|
+
const given = names.find((n) => options[n] !== undefined);
|
|
17
|
+
if (given)
|
|
18
|
+
throw new Refusal(`--${given} does not apply to ${form}.`);
|
|
19
|
+
};
|
|
20
|
+
const owner = (o) => (o.owner === undefined ? {} : { owner: o.owner });
|
|
21
|
+
const page = (o) => ({
|
|
22
|
+
...(o.limit === undefined ? {} : { limit: o.limit }),
|
|
23
|
+
...(o.cursor === undefined ? {} : { cursor: o.cursor }),
|
|
24
|
+
});
|
|
25
|
+
const stretch = (o) => ({
|
|
26
|
+
...(o.since === undefined ? {} : { since: o.since }),
|
|
27
|
+
...(o.until === undefined ? {} : { until: o.until }),
|
|
28
|
+
});
|
|
29
|
+
const same = (a, b) => a !== null && a.toLowerCase() === b.toLowerCase();
|
|
30
|
+
/** The answer, or for `--json` the answer as given, after its mailboxes' state. */
|
|
31
|
+
function shown(io, style, envelope, print) {
|
|
32
|
+
if (style.json)
|
|
33
|
+
return json(io, envelope);
|
|
34
|
+
state(io, style, envelope);
|
|
35
|
+
print(envelope.data);
|
|
36
|
+
more(io, style, envelope);
|
|
37
|
+
}
|
|
38
|
+
const one = (args, what) => {
|
|
39
|
+
const [arg, ...rest] = args;
|
|
40
|
+
if (arg === undefined || rest.length)
|
|
41
|
+
throw new Refusal(`Name one ${what}.`);
|
|
42
|
+
return arg;
|
|
43
|
+
};
|
|
44
|
+
const none = (args) => {
|
|
45
|
+
if (args.length)
|
|
46
|
+
throw new Refusal(`It takes no ${args.length === 1 ? "argument" : "arguments"}: ${clean(args.join(" "))}.`);
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* A sender's id: given as one, or found over a stretch by its root domain,
|
|
50
|
+
* or by its address under a root many unrelated people share. Such a root
|
|
51
|
+
* is listed one address at a time, each row naming the shared root, so a
|
|
52
|
+
* row with an address is matched by its address alone: matched by its
|
|
53
|
+
* root, one person's mail would be shown as the whole root's. And it is
|
|
54
|
+
* read by its address too, so the read does not widen to the root either.
|
|
55
|
+
*/
|
|
56
|
+
async function senderOf(io, options, name, over) {
|
|
57
|
+
if (UUID.test(name))
|
|
58
|
+
return { sender: name, scope: "domain" };
|
|
59
|
+
let cursor;
|
|
60
|
+
let whole = true;
|
|
61
|
+
for (let n = 0; n < LOOKUP_PAGES; n++) {
|
|
62
|
+
const answer = await call(io, "list_senders", {
|
|
63
|
+
...owner(options),
|
|
64
|
+
...over,
|
|
65
|
+
limit: API_PAGE_MAX,
|
|
66
|
+
...(cursor ? { cursor } : {}),
|
|
67
|
+
});
|
|
68
|
+
const found = answer.data.senders.find((s) => s.sender_written.address === null
|
|
69
|
+
? same(s.sender_written.root, name)
|
|
70
|
+
: same(s.sender_written.address, name));
|
|
71
|
+
if (found)
|
|
72
|
+
return {
|
|
73
|
+
sender: found.id,
|
|
74
|
+
scope: found.sender_written.address === null ? "domain" : "address",
|
|
75
|
+
};
|
|
76
|
+
whole &&= answer.complete;
|
|
77
|
+
if (!answer.next)
|
|
78
|
+
throw new Refusal(`Brood has no mail from ${clean(name)} from ${over.since}${over.until ? ` to ${over.until}` : ""}${whole ? "" : ", though it has not read every mailbox whole, so the mail may be there"}. Name a sender who wrote to you by its root domain, such as example.com, or under a shared one such as gmail.com, by its address.`);
|
|
79
|
+
cursor = answer.next;
|
|
80
|
+
}
|
|
81
|
+
throw tooMany(name);
|
|
82
|
+
}
|
|
83
|
+
/** The first day of the `days` ending `until`, or today, as YYYY-MM-DD in UTC. */
|
|
84
|
+
function daysBack(io, days, until) {
|
|
85
|
+
const end = until === undefined ? io.now() : Date.parse(`${until}T00:00:00Z`);
|
|
86
|
+
if (Number.isNaN(end) ||
|
|
87
|
+
(until !== undefined && !/^\d{4}-\d{2}-\d{2}$/.test(until)))
|
|
88
|
+
throw new Refusal("--until is a day, as YYYY-MM-DD.");
|
|
89
|
+
return new Date(end - (days - 1) * 86_400_000).toISOString().slice(0, 10);
|
|
90
|
+
}
|
|
91
|
+
/** The classifications covering a sender, by its id, or by what one covers or its path. */
|
|
92
|
+
async function classificationsOf(io, options, name) {
|
|
93
|
+
if (UUID.test(name)) {
|
|
94
|
+
const one = await call(io, "get_classification", {
|
|
95
|
+
...owner(options),
|
|
96
|
+
id: name,
|
|
97
|
+
});
|
|
98
|
+
return [one.data];
|
|
99
|
+
}
|
|
100
|
+
const found = [];
|
|
101
|
+
let cursor;
|
|
102
|
+
for (let n = 0; n < LOOKUP_PAGES; n++) {
|
|
103
|
+
const answer = await call(io, "list_classifications", {
|
|
104
|
+
...owner(options),
|
|
105
|
+
limit: API_PAGE_MAX,
|
|
106
|
+
...(cursor ? { cursor } : {}),
|
|
107
|
+
});
|
|
108
|
+
found.push(...answer.data.classifications.filter((c) => same(c.sender_written.subject, name) ||
|
|
109
|
+
same(c.sender_written.path, name)));
|
|
110
|
+
if (!answer.next)
|
|
111
|
+
return found;
|
|
112
|
+
cursor = answer.next;
|
|
113
|
+
}
|
|
114
|
+
throw tooMany(name);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* The one classification a name means. For `hush`, none is null, so the
|
|
118
|
+
* sender is filed by hand (#1355); for `unhush`, none is a refusal.
|
|
119
|
+
*/
|
|
120
|
+
async function chosen(io, style, options, name, verb) {
|
|
121
|
+
const all = await classificationsOf(io, options, name);
|
|
122
|
+
const found = verb === "unhush"
|
|
123
|
+
? all.filter((c) => c.state === "active" || c.state === "quieted")
|
|
124
|
+
: all;
|
|
125
|
+
const [only, ...others] = found;
|
|
126
|
+
if (!only) {
|
|
127
|
+
if (verb === "hush" && !UUID.test(name))
|
|
128
|
+
return null;
|
|
129
|
+
throw new Refusal(verb === "hush"
|
|
130
|
+
? `Brood has no classification ${clean(name)}.`
|
|
131
|
+
: `Nothing is filed for ${clean(name)}.`);
|
|
132
|
+
}
|
|
133
|
+
if (!others.length)
|
|
134
|
+
return only.id;
|
|
135
|
+
io.err(`Several classifications cover ${clean(name)}:`);
|
|
136
|
+
rows(io, found.map((c) => [c.sender_written.path, c.state, dim(style, c.id)]), io.err);
|
|
137
|
+
throw new Refusal(`Run brood ${verb} with the id of the one you mean.`);
|
|
138
|
+
}
|
|
139
|
+
const accounts = async (io, style, options, args) => {
|
|
140
|
+
none(args);
|
|
141
|
+
const answer = await call(io, "list_accounts", owner(options));
|
|
142
|
+
shown(io, style, answer, (data) => {
|
|
143
|
+
if (!data.accounts.length)
|
|
144
|
+
return io.out("No mailbox is connected.");
|
|
145
|
+
const phase = new Map(answer.accounts.map((a) => [a.id, a.sync.phase]));
|
|
146
|
+
rows(io, data.accounts.map((a) => [
|
|
147
|
+
a.address,
|
|
148
|
+
a.connected ? (phase.get(a.id) ?? "") : "disconnected",
|
|
149
|
+
dim(style, a.id),
|
|
150
|
+
]));
|
|
151
|
+
});
|
|
152
|
+
};
|
|
153
|
+
const week = async (io, style, options, args) => {
|
|
154
|
+
none(args);
|
|
155
|
+
const answer = await call(io, "summarise_week", {
|
|
156
|
+
...owner(options),
|
|
157
|
+
...(options.week === undefined ? {} : { week: options.week }),
|
|
158
|
+
});
|
|
159
|
+
shown(io, style, answer, (w) => {
|
|
160
|
+
io.out(`The week of ${w.monday} to ${w.sunday}.`);
|
|
161
|
+
io.out(TONE_TITLES[w.tone]);
|
|
162
|
+
const section = (title, lines, empty) => {
|
|
163
|
+
io.out("");
|
|
164
|
+
io.out("· · ·");
|
|
165
|
+
io.out("");
|
|
166
|
+
io.out(title);
|
|
167
|
+
if (!lines.length)
|
|
168
|
+
return io.out(` ${empty}`);
|
|
169
|
+
rows(io, lines.map((l) => ["", ...l]));
|
|
170
|
+
};
|
|
171
|
+
section("Wrote most", w.loudest.map((s) => [
|
|
172
|
+
s.sender_written.customer ?? s.sender_written.root,
|
|
173
|
+
dim(style, s.tags.join(", ")),
|
|
174
|
+
]), "No one wrote.");
|
|
175
|
+
section("Wrote for the first time", w.first_wrote.map((s) => [s.sender_written.root]), "No one new.");
|
|
176
|
+
section("Proposed", w.proposed.map((c) => [c.sender_written.path]), "Nothing.");
|
|
177
|
+
section("Quieted", w.quieted.map((c) => [c.sender_written.path]), "Nothing.");
|
|
178
|
+
});
|
|
179
|
+
};
|
|
180
|
+
const senders = async (io, style, options, args) => {
|
|
181
|
+
if (args.length) {
|
|
182
|
+
without(options, ["since", "until", "limit", "cursor"], "brood senders <name>");
|
|
183
|
+
// Over the days its portrait covers, so a sender with mail in it is found.
|
|
184
|
+
const found = await senderOf(io, options, one(args, "sender"), {
|
|
185
|
+
since: daysBack(io, PORTRAIT_DAYS),
|
|
186
|
+
});
|
|
187
|
+
const answer = await call(io, "get_sender", {
|
|
188
|
+
...owner(options),
|
|
189
|
+
...found,
|
|
190
|
+
});
|
|
191
|
+
return shown(io, style, answer, (s) => printSender(io, style, s));
|
|
192
|
+
}
|
|
193
|
+
const answer = await call(io, "list_senders", {
|
|
194
|
+
...owner(options),
|
|
195
|
+
...page(options),
|
|
196
|
+
...stretch(options),
|
|
197
|
+
});
|
|
198
|
+
shown(io, style, answer, (data) => {
|
|
199
|
+
io.err(dim(style, `${data.counted_from} to ${data.counted_to}, counted by the ${data.resolution === "day" ? "day" : "whole month"}.`));
|
|
200
|
+
if (!data.senders.length)
|
|
201
|
+
return io.out("No one wrote in that stretch.");
|
|
202
|
+
rows(io, data.senders.map((s) => [
|
|
203
|
+
s.sender_written.address ?? s.sender_written.root,
|
|
204
|
+
String(s.messages),
|
|
205
|
+
`last ${day(s.last)}`,
|
|
206
|
+
]));
|
|
207
|
+
});
|
|
208
|
+
};
|
|
209
|
+
function printSender(io, style, s) {
|
|
210
|
+
const w = s.sender_written;
|
|
211
|
+
io.out(s.scope === "address" ? w.address : (w.customer ?? w.root));
|
|
212
|
+
io.out(`${s.messages} messages in the last ${PORTRAIT_DAYS} days${s.mailing ? ", mostly mailings" : ""}.`);
|
|
213
|
+
io.out(s.written_to ? "You have written to it." : "You have not written to it.");
|
|
214
|
+
if (s.first_wrote)
|
|
215
|
+
io.out(`First wrote ${day(s.first_wrote)}.`);
|
|
216
|
+
if (s.classifications.length) {
|
|
217
|
+
io.out("");
|
|
218
|
+
io.out("Classifications");
|
|
219
|
+
rows(io, s.classifications.map((c) => [
|
|
220
|
+
"",
|
|
221
|
+
c.sender_written.path,
|
|
222
|
+
c.state,
|
|
223
|
+
dim(style, c.id),
|
|
224
|
+
]));
|
|
225
|
+
}
|
|
226
|
+
if (s.addresses.length) {
|
|
227
|
+
io.out("");
|
|
228
|
+
io.out("Addresses");
|
|
229
|
+
rows(io, s.addresses.map((a) => [
|
|
230
|
+
"",
|
|
231
|
+
a.sender_written.address,
|
|
232
|
+
String(a.messages),
|
|
233
|
+
a.never_quiet ? "never quiet" : a.kept_out ? "kept out" : "",
|
|
234
|
+
]));
|
|
235
|
+
if (s.more_addresses)
|
|
236
|
+
io.out(dim(style, " and more."));
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
const events = async (io, style, options, args) => {
|
|
240
|
+
none(args);
|
|
241
|
+
// Over the stretch asked for, so a sender who wrote in it is found.
|
|
242
|
+
const found = options.sender === undefined
|
|
243
|
+
? undefined
|
|
244
|
+
: await senderOf(io, options, options.sender, {
|
|
245
|
+
since: options.since ?? daysBack(io, API_STRETCH_DAYS, options.until),
|
|
246
|
+
...(options.until === undefined ? {} : { until: options.until }),
|
|
247
|
+
});
|
|
248
|
+
const answer = await call(io, "list_events", {
|
|
249
|
+
...owner(options),
|
|
250
|
+
...page(options),
|
|
251
|
+
...stretch(options),
|
|
252
|
+
...(found ?? {}),
|
|
253
|
+
});
|
|
254
|
+
shown(io, style, answer, (data) => {
|
|
255
|
+
io.err(dim(style, `Reaching back to ${day(data.retained_from)}.`));
|
|
256
|
+
if (!data.events.length)
|
|
257
|
+
return io.out("No messages in that stretch.");
|
|
258
|
+
rows(io, data.events.map((e) => [
|
|
259
|
+
e.at.slice(0, 16).replace("T", " "),
|
|
260
|
+
e.direction === "inbound" ? "from" : "to",
|
|
261
|
+
e.sender_written.address,
|
|
262
|
+
dim(style, [e.reply && "reply", e.mailing && "mailing"]
|
|
263
|
+
.filter(Boolean)
|
|
264
|
+
.join(", ")),
|
|
265
|
+
]));
|
|
266
|
+
});
|
|
267
|
+
};
|
|
268
|
+
const quiet = async (io, style, options, args) => {
|
|
269
|
+
none(args);
|
|
270
|
+
const answer = await call(io, "list_quiet_senders", {
|
|
271
|
+
...owner(options),
|
|
272
|
+
...page(options),
|
|
273
|
+
});
|
|
274
|
+
shown(io, style, answer, (data) => {
|
|
275
|
+
io.err(dim(style, `Silent for ${data.silence_days} days or more, after writing most months.`));
|
|
276
|
+
if (!data.quiet.length)
|
|
277
|
+
return io.out("No regular sender has gone silent.");
|
|
278
|
+
rows(io, data.quiet.map((s) => [
|
|
279
|
+
s.sender_written.address ?? s.sender_written.root,
|
|
280
|
+
`last heard ${day(s.last_heard)}`,
|
|
281
|
+
]));
|
|
282
|
+
});
|
|
283
|
+
};
|
|
284
|
+
const classifications = async (io, style, options, args) => {
|
|
285
|
+
const path = args.length ? one(args, "path") : undefined;
|
|
286
|
+
const wanted = options.state === undefined
|
|
287
|
+
? undefined
|
|
288
|
+
: CLASSIFICATION_STATES.find((s) => s === options.state);
|
|
289
|
+
if (options.state !== undefined && !wanted)
|
|
290
|
+
throw new Refusal(`--state is one of ${CLASSIFICATION_STATES.join(", ")}.`);
|
|
291
|
+
const answer = await call(io, "list_classifications", {
|
|
292
|
+
...owner(options),
|
|
293
|
+
...page(options),
|
|
294
|
+
...(path ? { path } : {}),
|
|
295
|
+
...(wanted ? { state: wanted } : {}),
|
|
296
|
+
});
|
|
297
|
+
shown(io, style, answer, (data) => {
|
|
298
|
+
if (!data.classifications.length)
|
|
299
|
+
return io.out("No classifications.");
|
|
300
|
+
rows(io, data.classifications.map((c) => [
|
|
301
|
+
c.sender_written.path,
|
|
302
|
+
c.state,
|
|
303
|
+
c.reason ?? "",
|
|
304
|
+
dim(style, c.id),
|
|
305
|
+
]));
|
|
306
|
+
});
|
|
307
|
+
};
|
|
308
|
+
const classification = async (io, style, options, args) => {
|
|
309
|
+
const id = one(args, "classification, by its id");
|
|
310
|
+
const answer = await call(io, "get_classification", {
|
|
311
|
+
...owner(options),
|
|
312
|
+
id,
|
|
313
|
+
});
|
|
314
|
+
shown(io, style, answer, (c) => {
|
|
315
|
+
io.out(c.sender_written.path);
|
|
316
|
+
io.out(`${c.state}${c.reason ? `, for ${c.reason}` : ""}. Covers ${c.subject_kind === "domain" ? "everything from" : "the one address"} ${c.sender_written.subject}.`);
|
|
317
|
+
io.out(`Proposed ${day(c.proposed_at)}${c.decided_at ? `, decided ${day(c.decided_at)}${c.decided_by === "user" ? " by you" : " by Brood"}` : ""}.`);
|
|
318
|
+
if (c.mailboxes.length) {
|
|
319
|
+
io.out("");
|
|
320
|
+
io.out("Filed in");
|
|
321
|
+
rows(io, c.mailboxes.map((m) => [
|
|
322
|
+
"",
|
|
323
|
+
m.mailbox,
|
|
324
|
+
`since ${day(m.installed_at)}`,
|
|
325
|
+
m.stranded ? "left behind" : "",
|
|
326
|
+
]));
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
};
|
|
330
|
+
function printRule(io, style, r) {
|
|
331
|
+
const w = r.sender_written;
|
|
332
|
+
rows(io, [
|
|
333
|
+
[
|
|
334
|
+
w.path ?? "",
|
|
335
|
+
`${w.from.kind === "domain" ? "everything from" : "the address"} ${w.from.value}`,
|
|
336
|
+
],
|
|
337
|
+
[
|
|
338
|
+
"",
|
|
339
|
+
`in ${r.mailbox}, since ${day(r.installed_at)}${r.stranded ? ", left behind" : ""}`,
|
|
340
|
+
],
|
|
341
|
+
...(w.exclude.length ? [["", `leaves out ${w.exclude.join(", ")}`]] : []),
|
|
342
|
+
...(w.exclude_cc.length
|
|
343
|
+
? [["", `leaves out, when copied, ${w.exclude_cc.join(", ")}`]]
|
|
344
|
+
: []),
|
|
345
|
+
["", dim(style, r.id)],
|
|
346
|
+
]);
|
|
347
|
+
}
|
|
348
|
+
const rules = async (io, style, options, args) => {
|
|
349
|
+
if (args.length) {
|
|
350
|
+
without(options, ["classification", "limit", "cursor"], "brood rules <id>");
|
|
351
|
+
const answer = await call(io, "get_rule", {
|
|
352
|
+
...owner(options),
|
|
353
|
+
id: one(args, "rule, by its id"),
|
|
354
|
+
});
|
|
355
|
+
return shown(io, style, answer, (r) => printRule(io, style, r));
|
|
356
|
+
}
|
|
357
|
+
const answer = await call(io, "list_rules", {
|
|
358
|
+
...owner(options),
|
|
359
|
+
...page(options),
|
|
360
|
+
...(options.classification === undefined
|
|
361
|
+
? {}
|
|
362
|
+
: { classification: options.classification }),
|
|
363
|
+
});
|
|
364
|
+
shown(io, style, answer, (data) => {
|
|
365
|
+
if (!data.rules.length)
|
|
366
|
+
return io.out("Brood has installed no filters.");
|
|
367
|
+
rows(io, data.rules.map((r) => [
|
|
368
|
+
r.sender_written.path ?? "",
|
|
369
|
+
r.mailbox,
|
|
370
|
+
r.stranded ? "left behind" : "",
|
|
371
|
+
dim(style, r.id),
|
|
372
|
+
]));
|
|
373
|
+
});
|
|
374
|
+
};
|
|
375
|
+
const hush = async (io, style, options, args) => {
|
|
376
|
+
const name = one(args, "sender, by its root domain or address, or a classification by its id");
|
|
377
|
+
const id = await chosen(io, style, options, name, "hush");
|
|
378
|
+
// None proposed: the sender itself, found over the days its portrait
|
|
379
|
+
// covers, as brood senders finds one, and filed by hand (DD21 §5, #1355).
|
|
380
|
+
let answer;
|
|
381
|
+
try {
|
|
382
|
+
answer = await call(io, "hush", {
|
|
383
|
+
...owner(options),
|
|
384
|
+
...(id !== null
|
|
385
|
+
? { classification: id }
|
|
386
|
+
: {
|
|
387
|
+
sender: (await senderOf(io, options, name, {
|
|
388
|
+
since: daysBack(io, PORTRAIT_DAYS),
|
|
389
|
+
})).sender,
|
|
390
|
+
}),
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
catch (error) {
|
|
394
|
+
// A sender the engine would never file, said as why rather than as an
|
|
395
|
+
// argument refused.
|
|
396
|
+
if (id === null &&
|
|
397
|
+
error instanceof Refused &&
|
|
398
|
+
error.code === "invalid_argument")
|
|
399
|
+
throw new Refusal(`Brood does not file ${clean(name)} by hand: it is a customer's, someone you write to or a colleague, one of your own filters already acts on it, or its address is one a filter cannot hold exactly.`);
|
|
400
|
+
throw error;
|
|
401
|
+
}
|
|
402
|
+
shown(io, style, answer, (h) => {
|
|
403
|
+
const what = h.sender_written.subject;
|
|
404
|
+
io.out(h.already
|
|
405
|
+
? `${what} was filed already.`
|
|
406
|
+
: `Filed ${what} out of your inbox, under ${h.sender_written.path}.`);
|
|
407
|
+
if (!same(what, name) && !UUID.test(name))
|
|
408
|
+
io.out(`Brood files ${clean(name)} address by address, since you write to someone there or it is shared, so only ${what} is filed.`);
|
|
409
|
+
if (h.failed.length)
|
|
410
|
+
io.out(`Brood could not install its filter in ${h.failed.length === 1 ? "one mailbox" : `${h.failed.length} mailboxes`}: ${h.failed.join(", ")}.`);
|
|
411
|
+
});
|
|
412
|
+
};
|
|
413
|
+
const unhush = async (io, style, options, args) => {
|
|
414
|
+
const name = one(args, "sender, by its root domain or address, or a classification by its id");
|
|
415
|
+
const id = await chosen(io, style, options, name, "unhush");
|
|
416
|
+
const answer = await call(io, "unhush", {
|
|
417
|
+
...owner(options),
|
|
418
|
+
classification: id,
|
|
419
|
+
});
|
|
420
|
+
shown(io, style, answer, (u) => {
|
|
421
|
+
if (u.outcome === "not_filed")
|
|
422
|
+
return io.out(`Nothing to undo: ${clean(name)} is not filed.`);
|
|
423
|
+
io.out(u.handed_back
|
|
424
|
+
? "Your own filter is back as it was, and your mail stays where it is."
|
|
425
|
+
: "Undone. Its mail is coming back to your inbox, the first of it now and the rest within the hour.");
|
|
426
|
+
});
|
|
427
|
+
};
|
|
428
|
+
const many = (n) => (n === 1 ? "one" : String(n));
|
|
429
|
+
/**
|
|
430
|
+
* `brood keep add` and `brood customers add`: the app's paste, checked first
|
|
431
|
+
* and added on a yes. A list read from standard input leaves nobody to ask
|
|
432
|
+
* there, so it is added only with `--yes`; `--check` stops at the check.
|
|
433
|
+
*/
|
|
434
|
+
async function paste(io, style, options, args, spec) {
|
|
435
|
+
if (args.length > 1)
|
|
436
|
+
throw new Refusal("Name one file, or none for standard input.");
|
|
437
|
+
if (options.check && options.yes)
|
|
438
|
+
throw new Refusal("--check and --yes ask for opposite things: give one.");
|
|
439
|
+
without(options, ["limit", "cursor"], spec.command);
|
|
440
|
+
const [file] = args;
|
|
441
|
+
let given;
|
|
442
|
+
try {
|
|
443
|
+
given = file === undefined ? await io.input() : await io.read(file);
|
|
444
|
+
}
|
|
445
|
+
catch (e) {
|
|
446
|
+
// The code alone, such as ENOENT: the path is the person's own, said back cleaned.
|
|
447
|
+
const code = e instanceof Error && "code" in e && typeof e.code === "string"
|
|
448
|
+
? ` (${e.code})`
|
|
449
|
+
: "";
|
|
450
|
+
throw new Refusal(`brood could not read ${file === undefined ? "standard input" : clean(file)}${code}.`);
|
|
451
|
+
}
|
|
452
|
+
if (!given.trim())
|
|
453
|
+
throw new Refusal(`${file === undefined ? "Standard input" : clean(file)} holds no ${spec.holds}.`);
|
|
454
|
+
const lines = (a) => rows(io, spec.told(a, style));
|
|
455
|
+
const body = { ...owner(options), lines: given };
|
|
456
|
+
if (options.yes)
|
|
457
|
+
return shown(io, style, await spec.send(io, body), (a) => {
|
|
458
|
+
lines(a);
|
|
459
|
+
spec.said(io, a);
|
|
460
|
+
});
|
|
461
|
+
const asking = file === undefined ? null : io.ask;
|
|
462
|
+
if (!options.check && (style.json || asking === null))
|
|
463
|
+
throw new Refusal(style.json
|
|
464
|
+
? "With --json, give --check to see what would be added, or --yes to add it."
|
|
465
|
+
: file === undefined
|
|
466
|
+
? "brood cannot ask while it reads the list from standard input: give --yes to add it, or --check to see what would be added."
|
|
467
|
+
: "Nobody is at a terminal to ask: give --yes to add the list, or --check to see what would be added.");
|
|
468
|
+
const checked = await spec.send(io, { ...body, check: true });
|
|
469
|
+
shown(io, style, checked, lines);
|
|
470
|
+
if (options.check || asking === null)
|
|
471
|
+
return;
|
|
472
|
+
const fresh = checked.data.lines.filter((l) => l.outcome === "added").length;
|
|
473
|
+
if (fresh === 0)
|
|
474
|
+
return io.out(spec.nothingNew);
|
|
475
|
+
const yes = await asking(`${spec.question(fresh)} [y/N] `);
|
|
476
|
+
if (!/^y(es)?$/i.test(yes.trim()))
|
|
477
|
+
return io.out("Nothing added.");
|
|
478
|
+
shown(io, style, await spec.send(io, body), (a) => {
|
|
479
|
+
if (spec.again(a))
|
|
480
|
+
lines(a);
|
|
481
|
+
spec.said(io, a);
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
/** A paste's lines as rows, each outcome in words; `to add` for `added` in a check. */
|
|
485
|
+
const told = (a, words, style) => a.lines.map((l) => [
|
|
486
|
+
dim(style, `line ${l.line}`),
|
|
487
|
+
l.text,
|
|
488
|
+
a.checked && l.outcome === "added" ? "to add" : words[l.outcome],
|
|
489
|
+
]);
|
|
490
|
+
const added = (a) => a.lines.filter((l) => l.outcome === "added").length;
|
|
491
|
+
/** What became of a line given to `keep`, in words. */
|
|
492
|
+
const KEPT = {
|
|
493
|
+
added: "added",
|
|
494
|
+
already_listed: "already kept",
|
|
495
|
+
repeated: "repeats an earlier line",
|
|
496
|
+
not_in_force: "not added",
|
|
497
|
+
invalid_address: "refused: not an address",
|
|
498
|
+
invalid_domain: "refused: not a domain",
|
|
499
|
+
invalid_pattern: "refused: not a pattern",
|
|
500
|
+
unrenderable: "refused: a filter cannot hold it exactly",
|
|
501
|
+
};
|
|
502
|
+
/** What became of a line given to `add_customers`, in words. */
|
|
503
|
+
const NAMED = {
|
|
504
|
+
added: "added",
|
|
505
|
+
already: "a customer already",
|
|
506
|
+
shared: "refused: a mail provider's domain or your own",
|
|
507
|
+
invalid: "refused: no domain Brood can read",
|
|
508
|
+
address: "refused: a site's address, so write its domain",
|
|
509
|
+
failed: "not added: Brood failed on it, so try it again",
|
|
510
|
+
};
|
|
511
|
+
const KEEP = {
|
|
512
|
+
command: "brood keep add",
|
|
513
|
+
holds: "addresses or domains",
|
|
514
|
+
send: (io, body) => call(io, "keep", body),
|
|
515
|
+
told: (k, style) => told(k, KEPT, style),
|
|
516
|
+
question: (n) => `Keep ${many(n)}?`,
|
|
517
|
+
nothingNew: "Nothing new to keep.",
|
|
518
|
+
again: (k) => !k.in_force,
|
|
519
|
+
said(io, k) {
|
|
520
|
+
if (!k.in_force)
|
|
521
|
+
return io.out("Nothing was added: one of Brood's filters could not change, and the list holds only what every filter keeps. Try again later.");
|
|
522
|
+
io.out(added(k) === 0
|
|
523
|
+
? "Nothing new to keep."
|
|
524
|
+
: `Kept ${many(added(k))}. Brood never files their mail.`);
|
|
525
|
+
},
|
|
526
|
+
};
|
|
527
|
+
const CUSTOMERS = {
|
|
528
|
+
command: "brood customers add",
|
|
529
|
+
holds: "customers",
|
|
530
|
+
send: (io, body) => call(io, "add_customers", body),
|
|
531
|
+
told: (c, style) => told(c, NAMED, style),
|
|
532
|
+
question: (n) => `Add ${n === 1 ? "one customer" : `${n} customers`}?`,
|
|
533
|
+
nothingNew: "No new customers to add.",
|
|
534
|
+
again: (c) => c.lines.some((l) => l.outcome === "failed"),
|
|
535
|
+
said(io, c) {
|
|
536
|
+
const n = added(c);
|
|
537
|
+
if (n === 0)
|
|
538
|
+
return io.out("No new customers were added.");
|
|
539
|
+
io.out(`Added ${n === 1 ? "one customer" : `${n} customers`}. Brood proposes a home for their mail ${c.later ? "shortly" : "now, among your proposals"}.`);
|
|
540
|
+
},
|
|
541
|
+
};
|
|
542
|
+
/** `brood customers add`, a list of customers. */
|
|
543
|
+
const customers = async (io, style, options, args) => {
|
|
544
|
+
const [verb, ...rest] = args;
|
|
545
|
+
if (verb !== "add")
|
|
546
|
+
throw new Refusal(`brood customers takes add${verb === undefined ? "" : `, not ${clean(verb)}`}.`);
|
|
547
|
+
return paste(io, style, options, rest, CUSTOMERS);
|
|
548
|
+
};
|
|
549
|
+
/** `brood keep`, the never-quiet list; `brood keep add`, a list added to it. */
|
|
550
|
+
const keep = async (io, style, options, args) => {
|
|
551
|
+
const [verb, ...rest] = args;
|
|
552
|
+
if (verb === "add")
|
|
553
|
+
return paste(io, style, options, rest, KEEP);
|
|
554
|
+
if (verb !== undefined)
|
|
555
|
+
throw new Refusal(`brood keep takes add, or nothing: not ${clean(verb)}.`);
|
|
556
|
+
without(options, ["check", "yes"], "brood keep");
|
|
557
|
+
const answer = await call(io, "list_kept", {
|
|
558
|
+
...owner(options),
|
|
559
|
+
...page(options),
|
|
560
|
+
});
|
|
561
|
+
shown(io, style, answer, (data) => {
|
|
562
|
+
if (!data.kept.length)
|
|
563
|
+
io.out("Nothing of your own is on the list.");
|
|
564
|
+
rows(io, data.kept.map((k) => [
|
|
565
|
+
k.value,
|
|
566
|
+
k.kind,
|
|
567
|
+
k.last_wrote ? `last wrote ${day(k.last_wrote)}` : "",
|
|
568
|
+
]));
|
|
569
|
+
if (!data.seeds.length)
|
|
570
|
+
return;
|
|
571
|
+
io.out("");
|
|
572
|
+
io.out("Kept for everyone:");
|
|
573
|
+
rows(io, data.seeds.map((s) => [s.value, dim(style, s.why)]));
|
|
574
|
+
});
|
|
575
|
+
};
|
|
576
|
+
/** Each command, and the options it takes, so one it does not is refused rather than ignored. */
|
|
577
|
+
export const COMMANDS = {
|
|
578
|
+
accounts: { run: accounts, takes: [] },
|
|
579
|
+
week: { run: week, takes: ["week"] },
|
|
580
|
+
senders: { run: senders, takes: ["since", "until", "limit", "cursor"] },
|
|
581
|
+
events: {
|
|
582
|
+
run: events,
|
|
583
|
+
takes: ["since", "until", "limit", "cursor", "sender"],
|
|
584
|
+
},
|
|
585
|
+
quiet: { run: quiet, takes: ["limit", "cursor"] },
|
|
586
|
+
classifications: {
|
|
587
|
+
run: classifications,
|
|
588
|
+
takes: ["state", "limit", "cursor"],
|
|
589
|
+
},
|
|
590
|
+
classification: { run: classification, takes: [] },
|
|
591
|
+
rules: { run: rules, takes: ["classification", "limit", "cursor"] },
|
|
592
|
+
hush: { run: hush, takes: [] },
|
|
593
|
+
unhush: { run: unhush, takes: [] },
|
|
594
|
+
keep: { run: keep, takes: ["check", "yes", "limit", "cursor"] },
|
|
595
|
+
customers: { run: customers, takes: ["check", "yes"] },
|
|
596
|
+
};
|
package/dist/io.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/** A refusal said to the person in a sentence, with nothing else printed. */
|
|
2
|
+
export class Refusal extends Error {
|
|
3
|
+
}
|
|
4
|
+
/** The session is over at Clerk, and its file is gone. */
|
|
5
|
+
export class SessionEnded extends Refusal {
|
|
6
|
+
}
|
|
7
|
+
/** Brood refused the call, with the status it answered. */
|
|
8
|
+
export class Refused extends Refusal {
|
|
9
|
+
code;
|
|
10
|
+
constructor(message, code) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.code = code;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export const isRecord = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
16
|
+
// Built from code points: Prettier writes a `\u` escape in a pattern
|
|
17
|
+
// literal as the character itself, which leaves invisible characters in
|
|
18
|
+
// the source, and a line separator breaks the literal (voice.md).
|
|
19
|
+
const UNSAFE = new RegExp(`[${[
|
|
20
|
+
[0x00, 0x1f],
|
|
21
|
+
[0x7f, 0x9f],
|
|
22
|
+
[0x061c],
|
|
23
|
+
[0x200b],
|
|
24
|
+
[0x200e, 0x200f],
|
|
25
|
+
[0x2028, 0x202e],
|
|
26
|
+
[0x2060],
|
|
27
|
+
[0x2066, 0x2069],
|
|
28
|
+
[0xfeff],
|
|
29
|
+
]
|
|
30
|
+
.map((range) => range.map((c) => `\\u${c.toString(16).padStart(4, "0")}`).join("-"))
|
|
31
|
+
.join("")}]`, "gu");
|
|
32
|
+
/**
|
|
33
|
+
* Text from outside brood, made safe to print. A sender writes its own
|
|
34
|
+
* address, name and domain (DD21 §8), and a control character or a
|
|
35
|
+
* direction override in one would let it rewrite the terminal or disguise
|
|
36
|
+
* what is shown, so every one is removed: the C0 and C1 controls, every
|
|
37
|
+
* direction mark and override, the line and paragraph separators, and the
|
|
38
|
+
* zero-width space, word joiner and byte order mark, which can make two
|
|
39
|
+
* names look alike. The zero-width joiner and non-joiner stay, since some
|
|
40
|
+
* scripts need them to spell a name.
|
|
41
|
+
*/
|
|
42
|
+
export const clean = (text) => text.replace(UNSAFE, "");
|
|
43
|
+
/** Every string in a value from outside, cleaned, so nothing printed from it can be missed. */
|
|
44
|
+
export const cleaned = (value) => typeof value === "string"
|
|
45
|
+
? clean(value)
|
|
46
|
+
: Array.isArray(value)
|
|
47
|
+
? value.map(cleaned)
|
|
48
|
+
: typeof value === "object" && value !== null
|
|
49
|
+
? Object.fromEntries(Object.entries(value).map(([k, v]) => [clean(k), cleaned(v)]))
|
|
50
|
+
: value;
|