@noisybrood/cli 0.2.0 → 0.4.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/dist/commands.js CHANGED
@@ -1,5 +1,5 @@
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";
1
+ import { RELATIONSHIP_REMOVAL, RULE_BOOK_KEYS, ADOPT_REFUSALS, CLASSIFICATION_STATES, TONE_TITLES, KEPT_KINDS, requestSaid, LETTER_CHOICES, } from "./brood/api/src/client.js";
2
+ import { API_PAGE_MAX, API_STRETCH_DAYS, APPROVAL_MINUTES, PORTRAIT_DAYS, PROPOSALS_AT_ONCE, } from "./brood/figures/index.js";
3
3
  import { call } from "./client.js";
4
4
  import { clean, Refusal, Refused } from "./io.js";
5
5
  import { day, dim, json, more, rows, state } from "./out.js";
@@ -137,6 +137,9 @@ async function chosen(io, style, options, name, verb) {
137
137
  throw new Refusal(`Run brood ${verb} with the id of the one you mean.`);
138
138
  }
139
139
  const accounts = async (io, style, options, args) => {
140
+ const [verb, ...rest] = args;
141
+ if (verb === "disconnect")
142
+ return disconnect(io, style, options, rest);
140
143
  none(args);
141
144
  const answer = await call(io, "list_accounts", owner(options));
142
145
  shown(io, style, answer, (data) => {
@@ -150,6 +153,18 @@ const accounts = async (io, style, options, args) => {
150
153
  ]));
151
154
  });
152
155
  };
156
+ /** `brood accounts disconnect`: a mailbox, by its address or id, let go once approved in the app. */
157
+ const disconnect = async (io, style, options, args) => {
158
+ ownAlone(options, "brood accounts disconnect");
159
+ const name = one(args, "mailbox, by its address or id");
160
+ const { accounts: mailboxes } = (await call(io, "list_accounts", {})).data;
161
+ const mailbox = mailboxes.find((a) => a.id === name || same(a.address, name));
162
+ if (!mailbox)
163
+ throw new Refusal(`No mailbox of yours is ${clean(name)}: brood accounts lists them.`);
164
+ if (!mailbox.connected)
165
+ throw new Refusal(`${mailbox.address} is already disconnected.`);
166
+ return approvedInApp(io, style, "disconnect_mailbox", { account: mailbox.id }, `disconnect ${mailbox.address}`);
167
+ };
153
168
  const week = async (io, style, options, args) => {
154
169
  none(args);
155
170
  const answer = await call(io, "summarise_week", {
@@ -169,7 +184,7 @@ const week = async (io, style, options, args) => {
169
184
  rows(io, lines.map((l) => ["", ...l]));
170
185
  };
171
186
  section("Wrote most", w.loudest.map((s) => [
172
- s.sender_written.customer ?? s.sender_written.root,
187
+ s.sender_written.relationship ?? s.sender_written.root,
173
188
  dim(style, s.tags.join(", ")),
174
189
  ]), "No one wrote.");
175
190
  section("Wrote for the first time", w.first_wrote.map((s) => [s.sender_written.root]), "No one new.");
@@ -208,7 +223,7 @@ const senders = async (io, style, options, args) => {
208
223
  };
209
224
  function printSender(io, style, s) {
210
225
  const w = s.sender_written;
211
- io.out(s.scope === "address" ? w.address : (w.customer ?? w.root));
226
+ io.out(s.scope === "address" ? w.address : (w.relationship ?? w.root));
212
227
  io.out(`${s.messages} messages in the last ${PORTRAIT_DAYS} days${s.mailing ? ", mostly mailings" : ""}.`);
213
228
  io.out(s.written_to ? "You have written to it." : "You have not written to it.");
214
229
  if (s.first_wrote)
@@ -346,6 +361,11 @@ function printRule(io, style, r) {
346
361
  ]);
347
362
  }
348
363
  const rules = async (io, style, options, args) => {
364
+ if (args[0] === "file") {
365
+ without(options, ["classification", "limit", "cursor"], "brood rules file");
366
+ const rule = anId(args.slice(1), "rule, by its id", "brood rules");
367
+ return shown(io, style, await call(io, "file_existing", { ...owner(options), rule }), (f) => io.out(`${FILED_EXISTING[f.outcome]}${f.filed ? ` ${f.filed === 1 ? "One message" : `${f.filed} messages`} filed so far.` : ""}`));
368
+ }
349
369
  if (args.length) {
350
370
  without(options, ["classification", "limit", "cursor"], "brood rules <id>");
351
371
  const answer = await call(io, "get_rule", {
@@ -396,7 +416,7 @@ const hush = async (io, style, options, args) => {
396
416
  if (id === null &&
397
417
  error instanceof Refused &&
398
418
  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.`);
419
+ throw new Refusal(`Brood does not file ${clean(name)} by hand: it is a relationship'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
420
  throw error;
401
421
  }
402
422
  shown(io, style, answer, (h) => {
@@ -427,15 +447,14 @@ const unhush = async (io, style, options, args) => {
427
447
  };
428
448
  const many = (n) => (n === 1 ? "one" : String(n));
429
449
  /**
430
- * `brood keep add` and `brood customers add`: the app's paste, checked first
450
+ * `brood keep add` and `brood relationships add`: the app's paste, checked first
431
451
  * and added on a yes. A list read from standard input leaves nobody to ask
432
452
  * there, so it is added only with `--yes`; `--check` stops at the check.
433
453
  */
434
454
  async function paste(io, style, options, args, spec) {
435
455
  if (args.length > 1)
436
456
  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.");
457
+ noCheckAndYes(options);
439
458
  without(options, ["limit", "cursor"], spec.command);
440
459
  const [file] = args;
441
460
  let given;
@@ -473,7 +492,7 @@ async function paste(io, style, options, args, spec) {
473
492
  if (fresh === 0)
474
493
  return io.out(spec.nothingNew);
475
494
  const yes = await asking(`${spec.question(fresh)} [y/N] `);
476
- if (!/^y(es)?$/i.test(yes.trim()))
495
+ if (!isYes(yes))
477
496
  return io.out("Nothing added.");
478
497
  shown(io, style, await spec.send(io, body), (a) => {
479
498
  if (spec.again(a))
@@ -499,10 +518,10 @@ const KEPT = {
499
518
  invalid_pattern: "refused: not a pattern",
500
519
  unrenderable: "refused: a filter cannot hold it exactly",
501
520
  };
502
- /** What became of a line given to `add_customers`, in words. */
521
+ /** What became of a line given to `add_relationships`, in words. */
503
522
  const NAMED = {
504
523
  added: "added",
505
- already: "a customer already",
524
+ already: "a relationship already",
506
525
  shared: "refused: a mail provider's domain or your own",
507
526
  invalid: "refused: no domain Brood can read",
508
527
  address: "refused: a site's address, so write its domain",
@@ -524,35 +543,362 @@ const KEEP = {
524
543
  : `Kept ${many(added(k))}. Brood never files their mail.`);
525
544
  },
526
545
  };
527
- const CUSTOMERS = {
528
- command: "brood customers add",
529
- holds: "customers",
530
- send: (io, body) => call(io, "add_customers", body),
546
+ /** The relationships' paste, into the one group the command line named. */
547
+ const placedIn = (group) => ({
548
+ command: "brood relationships add",
549
+ holds: "relationships",
550
+ send: (io, body) => call(io, "add_relationships", { ...body, group }),
531
551
  told: (c, style) => told(c, NAMED, style),
532
- question: (n) => `Add ${n === 1 ? "one customer" : `${n} customers`}?`,
533
- nothingNew: "No new customers to add.",
552
+ question: (n) => `Add ${n === 1 ? "one relationship" : `${n} relationships`} to ${clean(group)}?`,
553
+ nothingNew: "No new relationships to add.",
534
554
  again: (c) => c.lines.some((l) => l.outcome === "failed"),
535
555
  said(io, c) {
536
556
  const n = added(c);
537
557
  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"}.`);
558
+ return io.out("No new relationships were added.");
559
+ io.out(`Added ${n === 1 ? "one relationship" : `${n} relationships`}. Brood proposes a home for their mail ${c.later ? "shortly" : "now, among your proposals"}.`);
540
560
  },
561
+ });
562
+ const isYes = (answer) => /^y(es)?$/i.test(answer.trim());
563
+ /**
564
+ * Whether the person agreed to what was just shown: `--yes`, or a yes at a
565
+ * terminal. With nobody to ask, it is refused rather than assumed.
566
+ */
567
+ async function agreed(io, style, options, question,
568
+ /** Whether the command takes --check, so the refusal names it. */
569
+ checkable = true) {
570
+ if (options.yes)
571
+ return true;
572
+ if (style.json || io.ask === null)
573
+ throw new Refusal(checkable
574
+ ? style.json
575
+ ? "With --json, give --check to see what would happen, or --yes to do it."
576
+ : "Nobody is at a terminal to ask: give --yes to do it, or --check to see what would happen."
577
+ : style.json
578
+ ? "With --json, give --yes to do it."
579
+ : "Nobody is at a terminal to ask: give --yes to do it.");
580
+ const yes = await io.ask(`${question} [y/N] `);
581
+ if (isYes(yes))
582
+ return true;
583
+ io.out("Nothing changed.");
584
+ return false;
585
+ }
586
+ /**
587
+ * What is about to happen, shown first. With --json only the answer is
588
+ * printed, so a preview is the whole output of --check and nothing otherwise.
589
+ */
590
+ function previewed(io, style, options, answer, print) {
591
+ if (!style.json)
592
+ return print();
593
+ if (options.check)
594
+ json(io, answer);
595
+ }
596
+ const noCheckAndYes = (options) => {
597
+ if (options.check && options.yes)
598
+ throw new Refusal("--check and --yes ask for opposite things: give one.");
599
+ };
600
+ const anId = (args, what, listedBy) => {
601
+ const id = one(args, what);
602
+ if (!UUID.test(id))
603
+ throw new Refusal(`${clean(id)} is not an id: ${listedBy} lists them.`);
604
+ return id;
605
+ };
606
+ /** `brood proposals file`: the ones named, or with `--all` those Brood marks. */
607
+ async function fileProposals(io, style, options, args) {
608
+ noCheckAndYes(options);
609
+ if (options.all && args.length)
610
+ throw new Refusal("Name proposals or give --all, not both.");
611
+ if (args.length > PROPOSALS_AT_ONCE)
612
+ throw new Refusal(`Name at most ${PROPOSALS_AT_ONCE} at once.`);
613
+ if (!options.all && args.length === 0)
614
+ throw new Refusal("Name the proposals to file, by id, or give --all for those marked ready.");
615
+ const listed = await call(io, "list_proposals", { ...owner(options) });
616
+ const { proposals } = listed.data;
617
+ const named = args.map((id) => {
618
+ const found = proposals.find((p) => p.id === id);
619
+ if (!found)
620
+ throw new Refusal(`${clean(id)} is not among the proposals.`);
621
+ return found;
622
+ });
623
+ const ready = proposals.filter((p) => p.plain);
624
+ const picked = options.all ? ready.slice(0, PROPOSALS_AT_ONCE) : named;
625
+ if (picked.length === 0)
626
+ return shown(io, style, listed, () => io.out("Nothing to file."));
627
+ previewed(io, style, options, listed, () => {
628
+ rows(io, picked.map((p) => [p.sender_written.subject, p.sender_written.path]));
629
+ if (options.all && ready.length > picked.length)
630
+ io.out(`${ready.length - picked.length} more stay proposals, for another --all.`);
631
+ });
632
+ if (options.check)
633
+ return;
634
+ const n = picked.length;
635
+ if (!(await agreed(io, style, options, `File ${n === 1 ? "this one" : `these ${n}`} out of your inbox?`)))
636
+ return;
637
+ shown(io, style, await call(io, "file_proposals", {
638
+ ...owner(options),
639
+ classifications: picked.map((p) => p.id),
640
+ ...(options.all ? { plain: true } : {}),
641
+ }), (f) => {
642
+ io.out(`Filed ${f.filed}.`);
643
+ if (f.partly)
644
+ io.out(`${f.partly} filed, with a filter missing from some mailbox.`);
645
+ if (f.refused)
646
+ io.out(`${f.refused} refused, and not filed.`);
647
+ if (f.left)
648
+ io.out(`${f.left} not reached in time, and still proposals.`);
649
+ });
650
+ }
651
+ const REPROPOSED = {
652
+ reproposed: "It is back among the proposals.",
653
+ already_proposed: "It is among the proposals already.",
654
+ retiring: "Not yet: Brood has yet to remove its filters.",
655
+ returning: "Not yet: its mail is still coming back.",
656
+ not_declined: "It is not declined, so there is nothing to propose again.",
657
+ };
658
+ /** `brood proposals`, and filing, declining or proposing one again. */
659
+ const proposals = async (io, style, options, args) => {
660
+ const [verb, ...rest] = args;
661
+ if (verb === "file")
662
+ return fileProposals(io, style, options, rest);
663
+ without(options, ["all", "check", "yes"], `brood proposals ${verb ?? ""}`.trim());
664
+ if (verb === undefined) {
665
+ const answer = await call(io, "list_proposals", { ...owner(options) });
666
+ return shown(io, style, answer, (a) => {
667
+ if (a.proposals.length === 0)
668
+ return io.out("Nothing proposed.");
669
+ rows(io, a.proposals.map((p) => [
670
+ p.sender_written.subject,
671
+ p.sender_written.path,
672
+ p.plain ? "ready" : "",
673
+ p.reason ?? "",
674
+ dim(style, p.id),
675
+ ]));
676
+ });
677
+ }
678
+ if (verb === "decline") {
679
+ const classification = anId(rest, "proposal, by its id", "brood proposals");
680
+ return shown(io, style, await call(io, "decline_proposal", {
681
+ ...owner(options),
682
+ classification,
683
+ }), (d) => io.out(d.outcome === "declined"
684
+ ? "Declined. Brood will not propose it again."
685
+ : "It is no longer a proposal."));
686
+ }
687
+ if (verb === "again") {
688
+ const classification = anId(rest, "classification, by its id", "brood classifications --state declined");
689
+ return shown(io, style, await call(io, "propose_again", { ...owner(options), classification }), (a) => io.out(REPROPOSED[a.outcome]));
690
+ }
691
+ throw new Refusal(`brood proposals takes file, decline or again, not ${clean(verb)}.`);
692
+ };
693
+ const ASKED = {
694
+ asked: "Asked. The sender accepted.",
695
+ declined: "Asked, and the sender did not accept.",
696
+ unreachable: "The sender's address gave no answer.",
697
+ blocked: "Not sent: Brood does not contact that address.",
698
+ not_offered: "Not sent: its mailing no longer offers a one-click unsubscribe.",
699
+ never_quiet: "Not sent: it is on your never-quiet list.",
700
+ relationship: "Not sent: it is one of your relationships.",
701
+ limited: "Not sent: today's allowance is spent. Try again tomorrow.",
702
+ };
703
+ /** `brood unsubscribe`: who can be asked, one asked now, or several in turn. */
704
+ const unsubscribeCommand = async (io, style, options, args) => {
705
+ noCheckAndYes(options);
706
+ if (options.all && args.length)
707
+ throw new Refusal("Name senders or give --all, not both.");
708
+ const listed = await call(io, "list_unsubscribable", { ...owner(options) });
709
+ const { senders } = listed.data;
710
+ if (args.length === 0 && !options.all) {
711
+ without(options, ["check", "yes"], "brood unsubscribe with no sender");
712
+ return shown(io, style, listed, (a) => {
713
+ if (a.senders.length === 0)
714
+ return io.out("Nobody Brood can ask just now.");
715
+ rows(io, a.senders.map((s) => [s.sender_written.address, String(s.recent)]));
716
+ if (a.more)
717
+ io.out("These are the busiest, and there are more.");
718
+ });
719
+ }
720
+ const named = args.map((name) => {
721
+ const found = senders.find((s) => s.id === name || same(s.sender_written.address, name));
722
+ if (!found)
723
+ throw new Refusal(`Brood cannot ask ${clean(name)} just now: brood unsubscribe lists who it can.`);
724
+ return found;
725
+ });
726
+ const picked = options.all ? senders : named;
727
+ if (picked.length === 0)
728
+ return shown(io, style, listed, () => io.out("Nobody Brood can ask just now."));
729
+ previewed(io, style, options, listed, () => {
730
+ rows(io, picked.map((s) => [s.sender_written.address, String(s.recent)]));
731
+ if (options.all && listed.data.more)
732
+ io.out("These are the busiest, and only these are asked.");
733
+ });
734
+ if (options.check)
735
+ return;
736
+ const [only] = picked;
737
+ if (only && picked.length === 1 && !options.all) {
738
+ if (!(await agreed(io, style, options, `Ask ${clean(only.sender_written.address)} to stop? This cannot be taken back.`)))
739
+ return;
740
+ return shown(io, style, await call(io, "unsubscribe", { ...owner(options), sender: only.id }), (u) => io.out(ASKED[u.outcome]));
741
+ }
742
+ if (!(await agreed(io, style, options, `Ask these ${picked.length} to stop, in turn over the coming days? This cannot be taken back.`)))
743
+ return;
744
+ shown(io, style, await call(io, "choose_unsubscribes", {
745
+ ...owner(options),
746
+ senders: picked.map((s) => s.id),
747
+ }), (c) => io.out(`Chose ${c.chosen}. Brood asks them in turn.`));
748
+ };
749
+ const relationshipsNow = (io, options) => call(io, "list_relationships", { ...owner(options) });
750
+ const written = (r) => [...r.domains, ...r.addresses].join(", ");
751
+ /** One of `list`, by its id, its name, or a domain or address it covers. */
752
+ function named(list, name, what) {
753
+ const found = list.filter((r) => r.id === name ||
754
+ same(r.sender_written.name, name) ||
755
+ [...r.sender_written.domains, ...r.sender_written.addresses].some((d) => same(d, name)));
756
+ if (found.length === 0)
757
+ throw new Refusal(`${clean(name)} is not among your ${what}.`);
758
+ if (found.length > 1)
759
+ throw new Refusal(`Several match ${clean(name)}: name one by its id.`);
760
+ return found[0];
761
+ }
762
+ const GROUP_SAID = {
763
+ done: "Done.",
764
+ taken: "Another of your groups has that name.",
765
+ not_empty: "That group still has people in it. Move them first.",
541
766
  };
542
- /** `brood customers add`, a list of customers. */
543
- const customers = async (io, style, options, args) => {
767
+ /** `brood relationships`, and accepting, dismissing, moving or removing one. */
768
+ async function relationshipVerbs(io, style, options, verb, args) {
769
+ const form = `brood relationships ${verb ?? ""}`.trim();
770
+ if (verb === undefined) {
771
+ without(options, ["group", "check", "yes"], form);
772
+ none(args);
773
+ const answer = await relationshipsNow(io, options);
774
+ return shown(io, style, answer, (a) => {
775
+ const inGroup = new Map(a.groups.map((g) => [g.id, g.name]));
776
+ if (a.relationships.length === 0 && a.suggestions.length === 0)
777
+ return io.out("No relationships yet.");
778
+ rows(io, a.relationships.map((r) => [
779
+ r.sender_written.name,
780
+ written(r.sender_written),
781
+ clean((r.group && inGroup.get(r.group)) ?? "no group"),
782
+ dim(style, r.id),
783
+ ]));
784
+ if (a.suggestions.length) {
785
+ io.out("Brood suggests:");
786
+ rows(io, a.suggestions.map((r) => [
787
+ r.sender_written.name,
788
+ written(r.sender_written),
789
+ dim(style, r.id),
790
+ ]));
791
+ }
792
+ });
793
+ }
794
+ if (!["accept", "dismiss", "move", "remove"].includes(verb))
795
+ throw new Refusal(`brood relationships takes add, accept, dismiss, move or remove, not ${clean(verb)}.`);
796
+ const who = one(args, "relationship, by its id, name or domain");
797
+ const listed = await relationshipsNow(io, options);
798
+ const now = listed.data;
799
+ if (verb === "accept" || verb === "move") {
800
+ without(options, ["check", "yes"], form);
801
+ if (options.group === undefined)
802
+ throw new Refusal(`${form} needs --group <name>: the group they join, made if it is new.`);
803
+ const relationship = named(verb === "accept" ? now.suggestions : now.relationships, who, verb === "accept" ? "suggestions" : "relationships").id;
804
+ const body = { ...owner(options), relationship, group: options.group };
805
+ if (verb === "accept")
806
+ return shown(io, style, await call(io, "accept_suggestion", body), (a) => io.out(a.outcome === "done"
807
+ ? `Added. Brood proposes a home for their mail ${a.later ? "shortly" : "now, among your proposals"}.`
808
+ : "It is no longer a suggestion."));
809
+ return shown(io, style, await call(io, "move_relationship", body), (m) => io.out(GROUP_SAID[m.outcome]));
810
+ }
811
+ if (verb === "dismiss") {
812
+ without(options, ["group", "check", "yes"], form);
813
+ return shown(io, style, await call(io, "dismiss_suggestion", {
814
+ ...owner(options),
815
+ relationship: named(now.suggestions, who, "suggestions").id,
816
+ }), (d) => io.out(d.outcome === "done"
817
+ ? "Set aside. Brood will not suggest them again."
818
+ : "It is no longer a suggestion."));
819
+ }
820
+ if (verb === "remove") {
821
+ without(options, ["group"], form);
822
+ noCheckAndYes(options);
823
+ const r = named(now.relationships, who, "relationships");
824
+ previewed(io, style, options, listed, () => {
825
+ rows(io, [[r.sender_written.name, written(r.sender_written)]]);
826
+ io.out(RELATIONSHIP_REMOVAL);
827
+ });
828
+ if (options.check)
829
+ return;
830
+ if (!(await agreed(io, style, options, `Remove ${clean(r.sender_written.name)}?`)))
831
+ return;
832
+ return shown(io, style, await call(io, "remove_relationship", {
833
+ ...owner(options),
834
+ relationship: r.id,
835
+ }), (m) => io.out(GROUP_SAID[m.outcome]));
836
+ }
837
+ }
838
+ /** `brood groups`, and adding, renaming, removing or setting one. */
839
+ const groups = async (io, style, options, args) => {
544
840
  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);
841
+ if (verb === undefined) {
842
+ none(rest);
843
+ return shown(io, style, await relationshipsNow(io, options), (a) => {
844
+ if (a.groups.length === 0)
845
+ return io.out("No groups yet.");
846
+ rows(io, a.groups.map((g) => [
847
+ clean(g.name),
848
+ g.comes_back ? "comes back" : "stays filed",
849
+ dim(style, g.id),
850
+ ]));
851
+ });
852
+ }
853
+ if (verb === "add") {
854
+ const name = one(rest, "name for the group");
855
+ return shown(io, style, await call(io, "add_group", { ...owner(options), name }), (g) => io.out(GROUP_SAID[g.outcome]));
856
+ }
857
+ const [name, ...more] = rest;
858
+ if (name === undefined)
859
+ throw new Refusal(`Name the group to ${clean(verb)}.`);
860
+ if (!["rename", "comes-back", "remove"].includes(verb))
861
+ throw new Refusal(`brood groups takes add, rename, comes-back or remove, not ${clean(verb)}.`);
862
+ const { groups: all } = (await relationshipsNow(io, options)).data;
863
+ const found = all.find((g) => g.id === name || same(g.name, name));
864
+ if (!found)
865
+ throw new Refusal(`${clean(name)} is not one of your groups.`);
866
+ const group = found.id;
867
+ if (verb === "rename") {
868
+ const to = one(more, "new name");
869
+ return shown(io, style, await call(io, "rename_group", { ...owner(options), group, name: to }), (g) => io.out(GROUP_SAID[g.outcome]));
870
+ }
871
+ if (verb === "comes-back") {
872
+ const setting = one(more, "setting, on or off");
873
+ if (setting !== "on" && setting !== "off")
874
+ throw new Refusal("brood groups comes-back takes on or off.");
875
+ return shown(io, style, await call(io, "set_group_comes_back", {
876
+ ...owner(options),
877
+ group,
878
+ comes_back: setting === "on",
879
+ }), (g) => io.out(GROUP_SAID[g.outcome]));
880
+ }
881
+ none(more);
882
+ return shown(io, style, await call(io, "remove_group", { ...owner(options), group }), (g) => io.out(GROUP_SAID[g.outcome]));
883
+ };
884
+ /** `brood relationships add`, a list of them into one group. */
885
+ const relationships = async (io, style, options, args) => {
886
+ const [verb, ...rest] = args;
887
+ if (verb === undefined || verb !== "add")
888
+ return relationshipVerbs(io, style, options, verb, rest);
889
+ if (options.group === undefined)
890
+ throw new Refusal("brood relationships add needs --group <name>: the group they join, made if it is new.");
891
+ return paste(io, style, options, rest, placedIn(options.group));
548
892
  };
549
893
  /** `brood keep`, the never-quiet list; `brood keep add`, a list added to it. */
550
894
  const keep = async (io, style, options, args) => {
551
895
  const [verb, ...rest] = args;
552
896
  if (verb === "add")
553
897
  return paste(io, style, options, rest, KEEP);
898
+ if (verb === "remove")
899
+ return unkeep(io, style, options, rest);
554
900
  if (verb !== undefined)
555
- throw new Refusal(`brood keep takes add, or nothing: not ${clean(verb)}.`);
901
+ throw new Refusal(`brood keep takes add or remove, or nothing: not ${clean(verb)}.`);
556
902
  without(options, ["check", "yes"], "brood keep");
557
903
  const answer = await call(io, "list_kept", {
558
904
  ...owner(options),
@@ -573,6 +919,479 @@ const keep = async (io, style, options, args) => {
573
919
  rows(io, data.seeds.map((s) => [s.value, dim(style, s.why)]));
574
920
  });
575
921
  };
922
+ /**
923
+ * `brood keep remove`: an entry of the person's own off the never-quiet
924
+ * list, found on every page of it, once approved in the app. One kept for
925
+ * everyone is not theirs to remove, and is said so.
926
+ */
927
+ const unkeep = async (io, style, options, args) => {
928
+ ownAlone(options, "brood keep remove");
929
+ without(options, ["check", "yes", "limit", "cursor"], "brood keep remove");
930
+ const [value, kind, ...extra] = args;
931
+ if (value === undefined || extra.length)
932
+ throw new Refusal("Name one address or domain on your list, and its kind if it is there twice.");
933
+ if (kind !== undefined && !KEPT_KINDS.some((k) => k === kind))
934
+ throw new Refusal(`brood keep remove takes a kind of ${KEPT_KINDS.join(", ")}, not ${clean(kind)}.`);
935
+ const found = [];
936
+ let seeded = false;
937
+ let cursor;
938
+ for (let pages = 0;; pages++) {
939
+ if (pages === LOOKUP_PAGES)
940
+ throw tooMany(value);
941
+ const { data, next } = await call(io, "list_kept", {
942
+ ...(cursor ? { cursor } : {}),
943
+ });
944
+ found.push(...data.kept.filter((k) => same(k.value, value) && (kind === undefined || k.kind === kind)));
945
+ seeded ||= data.seeds.some((k) => same(k.value, value));
946
+ if (!next)
947
+ break;
948
+ cursor = next;
949
+ }
950
+ const [only, ...others] = found;
951
+ if (!only)
952
+ throw new Refusal(seeded
953
+ ? `Brood keeps ${clean(value)} in view for everyone, so it is not yours to remove.`
954
+ : `${clean(value)} is not on your never-quiet list: brood keep lists it.`);
955
+ if (others.length)
956
+ throw new Refusal(`${clean(value)} is on your list more than once, as ${found.map((k) => k.kind).join(" and ")}. Name the one you mean, as brood keep remove ${clean(value)} ${only.kind}.`);
957
+ return approvedInApp(io, style, "remove_never_quiet", { kind: only.kind, value: only.value }, `take ${only.value} off your never-quiet list`);
958
+ };
959
+ /**
960
+ * The one classification in `state` a name means: by its id, or what it
961
+ * covers, or its path. Several are put back to the person, as `chosen` does.
962
+ */
963
+ async function homeIn(io, style, options, name, state, form) {
964
+ const found = (await classificationsOf(io, options, name)).filter((c) => c.state === state);
965
+ const [only, ...others] = found;
966
+ if (!only)
967
+ throw new Refusal(state === "quieted"
968
+ ? `Nothing quieted is ${clean(name)}.`
969
+ : `Nothing filed is ${clean(name)}.`);
970
+ if (!others.length)
971
+ return only.id;
972
+ io.err(`Several classifications cover ${clean(name)}:`);
973
+ rows(io, found.map((c) => [c.sender_written.path, c.state, dim(style, c.id)]), io.err);
974
+ throw new Refusal(`Run ${form} with the id of the one you mean.`);
975
+ }
976
+ const RESTORED = {
977
+ restored: "Brought back. Brood files it again, and its home is back in place.",
978
+ restored_left: "Brought back. Brood files it again; a home you renamed or removed is left as it is.",
979
+ restored_later: "Brought back. Brood files it again, and moves its home back at the next rules pass.",
980
+ not_quieted: "It is not quieted, so nothing changed.",
981
+ };
982
+ /** `brood quieted`, and bringing one back or leaving it quiet. */
983
+ const quieted = async (io, style, options, args) => {
984
+ const [verb, ...rest] = args;
985
+ if (verb === undefined) {
986
+ const answer = await call(io, "list_classifications", {
987
+ ...owner(options),
988
+ ...page(options),
989
+ state: "quieted",
990
+ });
991
+ return shown(io, style, answer, (data) => {
992
+ if (!data.classifications.length)
993
+ return io.out("Nothing is quieted.");
994
+ rows(io, data.classifications.map((c) => [
995
+ c.sender_written.path,
996
+ c.decided_at ? `quieted ${day(c.decided_at)}` : "",
997
+ dim(style, c.id),
998
+ ]));
999
+ });
1000
+ }
1001
+ if (verb !== "restore" && verb !== "leave")
1002
+ throw new Refusal(`brood quieted takes restore or leave, not ${clean(verb)}.`);
1003
+ without(options, ["limit", "cursor"], `brood quieted ${verb}`);
1004
+ const name = one(rest, "quieted home, by what it covers, its path or its id");
1005
+ const classification = await homeIn(io, style, options, name, "quieted", `brood quieted ${verb}`);
1006
+ const body = { ...owner(options), classification };
1007
+ if (verb === "restore")
1008
+ return shown(io, style, await call(io, "restore_quieted", body), (r) => io.out(RESTORED[r.outcome]));
1009
+ return shown(io, style, await call(io, "leave_quiet", body), (l) => io.out(l.outcome === "not_quieted"
1010
+ ? "It is not quieted, so nothing changed."
1011
+ : "Left quiet."));
1012
+ };
1013
+ const BATCHED = {
1014
+ batched: "Batched. Its mail comes back to your inbox at your arrival hours.",
1015
+ unbatched: "No longer batched.",
1016
+ unchanged: "It was already so, or is not a home that can be batched.",
1017
+ no_hours: "Set your arrival hours on the account page first: they are when batched mail comes back.",
1018
+ cannot: "A mailbox filing it cannot return mail in batches, or nothing files it now.",
1019
+ };
1020
+ /** `brood batch <home> on|off`. */
1021
+ const batch = async (io, style, options, args) => {
1022
+ const [name, setting, ...rest] = args;
1023
+ if (name === undefined || setting === undefined || rest.length)
1024
+ throw new Refusal("brood batch takes a home and on or off.");
1025
+ if (setting !== "on" && setting !== "off")
1026
+ throw new Refusal("brood batch takes on or off.");
1027
+ const classification = await homeIn(io, style, options, name, "active", "brood batch");
1028
+ shown(io, style, await call(io, "set_batched", {
1029
+ ...owner(options),
1030
+ classification,
1031
+ batched: setting === "on",
1032
+ }), (b) => io.out(BATCHED[b.outcome]));
1033
+ };
1034
+ const CANNOT_ADOPT = {
1035
+ matches_more: "it matches more than a sender",
1036
+ not_one_sender: "it names no one sender",
1037
+ keeps_in_inbox: "it leaves mail in the inbox",
1038
+ forwards: "it forwards",
1039
+ does_more: "it does more than file",
1040
+ no_home: "it files under none of your homes",
1041
+ several_homes: "it files under several homes",
1042
+ };
1043
+ const DOES = {
1044
+ out_of_inbox: "out of the inbox",
1045
+ trash: "to the trash",
1046
+ read: "marked read",
1047
+ starred: "starred",
1048
+ important: "marked important",
1049
+ forwards: "forwarded",
1050
+ };
1051
+ const isAdoptRefusal = (o) => ADOPT_REFUSALS.includes(o);
1052
+ const ADOPTED = {
1053
+ adopted: "Taken over. Brood files that sender as your filter did, and brood unhush puts your filter back.",
1054
+ gone: "That filter is no longer in the mailbox.",
1055
+ no_mail: "Brood has no mail from its sender yet.",
1056
+ not_root: "It names a part of a domain, which Brood does not file by.",
1057
+ already_brood: "Brood already files or has quieted that sender.",
1058
+ never_quiet: "Your never-quiet list keeps that sender in view.",
1059
+ unrenderable: "Its sender is not one a filter of Brood's can hold exactly.",
1060
+ not_connected: "That mailbox is not connected.",
1061
+ };
1062
+ const adoptedSaid = (o) => isAdoptRefusal(o)
1063
+ ? `Brood cannot take it over: ${CANNOT_ADOPT[o]}.`
1064
+ : ADOPTED[o];
1065
+ /** `brood own-rules`, your filters read now; `adopt <mailbox> <id>` takes one over. */
1066
+ const ownRules = async (io, style, options, args) => {
1067
+ const [verb, ...rest] = args;
1068
+ if (verb === undefined) {
1069
+ without(options, ["yes"], "brood own-rules");
1070
+ const answer = await call(io, "list_own_rules", owner(options));
1071
+ return shown(io, style, answer, (a) => {
1072
+ if (!a.mailboxes.length)
1073
+ return io.out("No mailbox is connected.");
1074
+ for (const m of a.mailboxes) {
1075
+ io.out(clean(m.mailbox));
1076
+ if (m.rules === null) {
1077
+ io.out(" Brood could not read its filters just now.");
1078
+ continue;
1079
+ }
1080
+ if (!m.rules.shown.length)
1081
+ io.out(" No filter of yours names one sender.");
1082
+ rows(io, m.rules.shown.map((f) => [
1083
+ "",
1084
+ clean(f.sender.value),
1085
+ clean(f.under.join(", ")),
1086
+ // A filter that only files under a home leaves its mail where it was.
1087
+ f.does.length
1088
+ ? f.does.map((d) => DOES[d]).join(", ")
1089
+ : "left in the inbox",
1090
+ f.cannot_adopt ? CANNOT_ADOPT[f.cannot_adopt] : "can be taken over",
1091
+ dim(style, f.id),
1092
+ ]));
1093
+ if (m.rules.unreadable)
1094
+ io.out(` ${m.rules.unreadable === 1 ? "One more names" : `${m.rules.unreadable} more name`} no one sender.`);
1095
+ for (const l of m.rules.left_behind)
1096
+ io.out(` ${clean(l.by)} left ${l.homes.map(clean).join(", ")}.`);
1097
+ }
1098
+ });
1099
+ }
1100
+ if (verb !== "adopt")
1101
+ throw new Refusal(`brood own-rules takes adopt, not ${clean(verb)}.`);
1102
+ const [mailbox, rule, ...more] = rest;
1103
+ if (mailbox === undefined || rule === undefined || more.length)
1104
+ throw new Refusal("brood own-rules adopt takes a mailbox and a filter's id, as brood own-rules lists them.");
1105
+ const { accounts } = (await call(io, "list_accounts", owner(options))).data;
1106
+ const account = accounts.find((a) => a.id === mailbox || same(a.address, mailbox));
1107
+ if (!account)
1108
+ throw new Refusal(`${clean(mailbox)} is not one of your mailboxes.`);
1109
+ if (!(await agreed(io, style, options, `Take over filter ${clean(rule)} in ${clean(account.address)}, so Brood files its sender?`, false)))
1110
+ return;
1111
+ shown(io, style, await call(io, "adopt_rule", {
1112
+ ...owner(options),
1113
+ account: account.id,
1114
+ rule,
1115
+ }), (a) => io.out(adoptedSaid(a.outcome)));
1116
+ };
1117
+ /**
1118
+ * One address and its sender id, found in its root's portrait as its page
1119
+ * finds it: the root by a row of `list_senders` it is under, then the
1120
+ * address among the root's.
1121
+ */
1122
+ async function addressOf(io, options, address) {
1123
+ const at = address.lastIndexOf("@");
1124
+ if (at <= 0)
1125
+ throw new Refusal(`${clean(address)} is not an address: name the one to keep out, such as alerts@example.com.`);
1126
+ const domain = address.slice(at + 1).toLowerCase();
1127
+ const since = daysBack(io, PORTRAIT_DAYS);
1128
+ let cursor;
1129
+ for (let n = 0; n < LOOKUP_PAGES; n++) {
1130
+ const answer = await call(io, "list_senders", {
1131
+ ...owner(options),
1132
+ since,
1133
+ limit: API_PAGE_MAX,
1134
+ ...(cursor ? { cursor } : {}),
1135
+ });
1136
+ const row = answer.data.senders.find(({ sender_written: w }) => w.address === null
1137
+ ? domain === w.root.toLowerCase() ||
1138
+ domain.endsWith(`.${w.root.toLowerCase()}`)
1139
+ : same(w.address, address));
1140
+ if (row) {
1141
+ const portrait = (await call(io, "get_sender", { ...owner(options), sender: row.id })).data;
1142
+ const found = portrait.addresses.find((a) => same(a.sender_written.address, address));
1143
+ if (found)
1144
+ return found;
1145
+ throw new Refusal(portrait.more_addresses
1146
+ ? `${clean(address)} is not among the addresses Brood lists for ${clean(row.sender_written.root)}.`
1147
+ : `Brood has no mail from ${clean(address)} from ${since}.`);
1148
+ }
1149
+ if (!answer.next)
1150
+ throw new Refusal(`Brood has no mail from ${clean(address)} from ${since}.`);
1151
+ cursor = answer.next;
1152
+ }
1153
+ throw tooMany(address);
1154
+ }
1155
+ const KEPT_OUT = {
1156
+ kept_out: "Kept out. Brood will not file it again, and its mail is coming back to your inbox, the first of it now and the rest within the hour.",
1157
+ kept_out_pending: "Kept out, and its mail is coming back. Brood narrows the filter itself within the hour; until then new mail from it may still be filed.",
1158
+ own_home: "It has a home of its own: brood unhush undoes that.",
1159
+ not_filed: "Nothing files it now.",
1160
+ unrenderable: "Its address is not one a filter can leave out exactly, so nothing changed.",
1161
+ };
1162
+ /** `brood keep-out <address>`: one address out of its domain's filing. */
1163
+ const keepOutCommand = async (io, style, options, args) => {
1164
+ const found = await addressOf(io, options, one(args, "address"));
1165
+ const filed = found.filing.flatMap((f) => f.classification ? [f.classification.sender_written.path] : []);
1166
+ if (!filed.length)
1167
+ throw new Refusal(`Nothing files ${clean(found.sender_written.address)} now.`);
1168
+ if (!(await agreed(io, style, options, `Keep ${clean(found.sender_written.address)} out of ${[...new Set(filed)].map(clean).join(", ")}, and bring its mail back?`, false)))
1169
+ return;
1170
+ shown(io, style, await call(io, "keep_out", { ...owner(options), sender: found.id }), (k) => io.out(KEPT_OUT[k.outcome]));
1171
+ };
1172
+ const FILED_EXISTING = {
1173
+ filed: "Filed what was already there.",
1174
+ continues: "Filing what was already there; the hourly pass files the rest.",
1175
+ later: "Today's filing is spent; the hourly pass files it.",
1176
+ stopped: "It no longer files, so nothing more was moved.",
1177
+ };
1178
+ const RULE_BOOK_SAID = {
1179
+ saved: "Saved. Brood decides by them from its next run.",
1180
+ unchanged: "They were already so, so nothing changed.",
1181
+ no_mailbox: "Connect a mailbox first: with none, the numbers apply to nothing.",
1182
+ stale: "The numbers changed since this was checked, so nothing was saved. Run it again.",
1183
+ };
1184
+ /** `name=value`, each a rule book number, as `brood rule-book set` takes them. */
1185
+ function numbersOf(args) {
1186
+ if (!args.length)
1187
+ throw new Refusal(`Name a number to change, as name=value: ${RULE_BOOK_KEYS.join(", ")}.`);
1188
+ const numbers = {};
1189
+ for (const arg of args) {
1190
+ const parts = arg.split("=");
1191
+ if (parts.length !== 2)
1192
+ throw new Refusal(`${clean(arg)} is not name=value, as quiet.silence_days=60.`);
1193
+ const [name = "", value = ""] = parts;
1194
+ const key = RULE_BOOK_KEYS.find((k) => k === name);
1195
+ if (!key)
1196
+ throw new Refusal(`${clean(name)} is not one of the rule book's numbers: ${RULE_BOOK_KEYS.join(", ")}.`);
1197
+ if (!/^\d+$/.test(value))
1198
+ throw new Refusal(`${key} takes a whole number, as ${key}=60.`);
1199
+ numbers[key] = Number(value);
1200
+ }
1201
+ return numbers;
1202
+ }
1203
+ const differences = (d) => {
1204
+ if (d === null)
1205
+ return [];
1206
+ const said = (which, would) => [
1207
+ ...which.homes.map((p) => `${would} propose ${clean(p)}`),
1208
+ ...which.quieted.map((p) => `${would} quiet ${clean(p)}`),
1209
+ ...which.declined.map((p) => `${would} stop filing ${clean(p)}`),
1210
+ ...which.relationships.map((p) => `${would} suggest ${clean(p)}`),
1211
+ ];
1212
+ return [...said(d.more, "would"), ...said(d.fewer, "would no longer")];
1213
+ };
1214
+ /** `brood rule-book`, and changing its numbers or putting them back. */
1215
+ const ruleBook = async (io, style, options, args) => {
1216
+ const [verb, ...rest] = args;
1217
+ if (verb === undefined) {
1218
+ without(options, ["check", "yes"], "brood rule-book");
1219
+ return shown(io, style, await call(io, "get_rule_book", owner(options)), (b) => {
1220
+ rows(io, RULE_BOOK_KEYS.map((k) => [
1221
+ k,
1222
+ String(b.numbers[k]),
1223
+ b.numbers[k] === b.defaults[k]
1224
+ ? ""
1225
+ : dim(style, `default ${b.defaults[k]}`),
1226
+ ]));
1227
+ if (b.ignored.length)
1228
+ io.out(`Brood could not use your ${b.ignored.join(", ")}, so the default stands in.`);
1229
+ if (!b.can_change)
1230
+ io.out("Changing them is Pro's.");
1231
+ });
1232
+ }
1233
+ if (verb === "reset") {
1234
+ without(options, ["check"], "brood rule-book reset");
1235
+ none(rest);
1236
+ if (!(await agreed(io, style, options, "Put every number back to its default?", false)))
1237
+ return;
1238
+ return shown(io, style, await call(io, "reset_rule_book", owner(options)), (r) => io.out(RULE_BOOK_SAID[r.outcome]));
1239
+ }
1240
+ if (verb !== "set")
1241
+ throw new Refusal(`brood rule-book takes set or reset, not ${clean(verb)}.`);
1242
+ noCheckAndYes(options);
1243
+ const numbers = numbersOf(rest);
1244
+ const checked = await call(io, "set_rule_book", {
1245
+ ...owner(options),
1246
+ numbers,
1247
+ check: true,
1248
+ });
1249
+ previewed(io, style, options, checked, () => {
1250
+ const said = differences(checked.data.sender_written);
1251
+ if (!said.length)
1252
+ io.out("Brood would decide nothing differently today.");
1253
+ else
1254
+ for (const line of said)
1255
+ io.out(`Brood ${line}.`);
1256
+ });
1257
+ if (options.check)
1258
+ return;
1259
+ const inForce = checked.data.in_force;
1260
+ if (inForce === null)
1261
+ throw new Refusal("Brood did not check the change.");
1262
+ if (!(await agreed(io, style, options, "Save these numbers?")))
1263
+ return;
1264
+ shown(io, style, await call(io, "set_rule_book", {
1265
+ ...owner(options),
1266
+ numbers,
1267
+ in_force: inForce,
1268
+ }), (r) => io.out(RULE_BOOK_SAID[r.outcome ?? "saved"]));
1269
+ };
1270
+ const LETTER_SAID = {
1271
+ off: "no letter",
1272
+ month: "a letter each month",
1273
+ week: "a letter each week",
1274
+ };
1275
+ /** An hour of the day as a person reads one, as 08:00. */
1276
+ const clock = (h) => `${String(h).padStart(2, "0")}:00`;
1277
+ /** `brood preferences`, and choosing the letter, the first proposals' email or the hours. */
1278
+ const preferences = async (io, style, options, args) => {
1279
+ if (options.owner !== undefined)
1280
+ throw new Refusal("brood preferences is your own account's alone: --owner does not apply.");
1281
+ const [verb, ...rest] = args;
1282
+ if (verb === undefined)
1283
+ return shown(io, style, await call(io, "get_preferences", {}), (p) => {
1284
+ rows(io, [
1285
+ ["Letter", LETTER_SAID[p.letter]],
1286
+ [
1287
+ "First proposals",
1288
+ p.first_proposals.sent
1289
+ ? "the email has gone"
1290
+ : p.first_proposals.wanted
1291
+ ? "an email when they are ready"
1292
+ : "no email",
1293
+ ],
1294
+ [
1295
+ "Arrival hours",
1296
+ p.arrival_hours
1297
+ ? p.arrival_hours.hours.map(clock).join(" and ")
1298
+ : "not set",
1299
+ ],
1300
+ ]);
1301
+ });
1302
+ if (verb === "letter") {
1303
+ const choice = LETTER_CHOICES.find((c) => c === one(rest, "letter"));
1304
+ if (!choice)
1305
+ throw new Refusal(`brood preferences letter takes ${LETTER_CHOICES.join(", ")}.`);
1306
+ return shown(io, style, await call(io, "set_letter", { letter: choice }), (l) => io.out(`From now on, ${LETTER_SAID[l.letter]}.`));
1307
+ }
1308
+ if (verb === "first-proposals") {
1309
+ const setting = one(rest, "setting, on or off");
1310
+ if (setting !== "on" && setting !== "off")
1311
+ throw new Refusal("brood preferences first-proposals takes on or off.");
1312
+ return shown(io, style, await call(io, "set_first_proposals", { wanted: setting === "on" }), (f) => io.out(f.outcome === "sent"
1313
+ ? "The email has gone already, so nothing changed."
1314
+ : setting === "on"
1315
+ ? "Brood emails you once when your first proposals are ready."
1316
+ : "No email when your first proposals are ready."));
1317
+ }
1318
+ if (verb === "hours") {
1319
+ if (!rest.length ||
1320
+ rest.length > 2 ||
1321
+ !rest.every((h) => /^\d{1,2}$/.test(h)))
1322
+ throw new Refusal("brood preferences hours takes one or two hours of the day, 0 to 23, as 8 17.");
1323
+ // The machine's own offset now, as the account page takes the browser's.
1324
+ // `|| 0`: UTC's offset negated is -0, which a body should not carry.
1325
+ const offset = -new Date(io.now()).getTimezoneOffset() || 0;
1326
+ return shown(io, style, await call(io, "set_arrival_hours", {
1327
+ hours: rest.map(Number),
1328
+ utc_offset_minutes: offset,
1329
+ }), (h) => io.out(`Batched mail comes back at ${h.hours.map(clock).join(" and ")}, your time.`));
1330
+ }
1331
+ throw new Refusal(`brood preferences takes letter, first-proposals or hours, not ${clean(verb)}.`);
1332
+ };
1333
+ /** How often a change waiting in the app is asked after. */
1334
+ const APPROVAL_POLL_MS = 2000;
1335
+ /** Refuses --owner on a command that is the person's own account alone. */
1336
+ const ownAlone = (options, form) => {
1337
+ if (options.owner !== undefined)
1338
+ throw new Refusal(`${form} is your own account's alone: --owner does not apply.`);
1339
+ };
1340
+ /**
1341
+ * A change only the person makes, in the app (DD21 §4): asked for here,
1342
+ * approved on the page it names, signed in as the app asks, and waited for,
1343
+ * so the command ends saying what came of it. Nothing changes until then.
1344
+ */
1345
+ async function approvedInApp(io, style, operation, body, what) {
1346
+ const asked = await call(io, operation, body);
1347
+ const { request, approve_at } = asked.data;
1348
+ io.err(`To ${what}, approve it in Brood: ${approve_at}`);
1349
+ io.err(`Nothing changes unless you approve it there. Waiting up to ${APPROVAL_MINUTES} minutes.`);
1350
+ // Bounded here as well as by the request's own end, so a clock that
1351
+ // disagrees with Brood's cannot keep it waiting for good.
1352
+ const polls = Math.ceil((APPROVAL_MINUTES * 60_000) / APPROVAL_POLL_MS) + 5;
1353
+ for (let i = 0; i < polls; i++) {
1354
+ await io.sleep(APPROVAL_POLL_MS);
1355
+ const now = await call(io, "get_request", { request });
1356
+ if (now.data.state === "waiting")
1357
+ continue;
1358
+ const said = requestSaid(now.data);
1359
+ return shown(io, style, now, () => io.out(said));
1360
+ }
1361
+ throw new Refusal("Brood never said what came of it. Nothing was assumed.");
1362
+ }
1363
+ /** `brood programs`, and allowing or revoking one, approved in the app. */
1364
+ const programs = async (io, style, options, args) => {
1365
+ ownAlone(options, "brood programs");
1366
+ const [verb, ...rest] = args;
1367
+ const listed = await call(io, "list_programs", {});
1368
+ if (verb === undefined)
1369
+ return shown(io, style, listed, (p) => {
1370
+ if (!p.programs.length)
1371
+ return io.out("No program calls Brood for you.");
1372
+ rows(io, p.programs.map((g) => [
1373
+ g.name ?? g.client,
1374
+ g.revoked_at
1375
+ ? `revoked ${day(g.revoked_at)}${g.revoked_why === "unused" ? ", unused" : ""}`
1376
+ : `last used ${day(g.last_used_at)}`,
1377
+ g.this_program ? "this one" : "",
1378
+ dim(style, g.client),
1379
+ ]));
1380
+ });
1381
+ if (verb !== "allow" && verb !== "revoke")
1382
+ throw new Refusal(`brood programs takes allow or revoke, or nothing: not ${clean(verb)}.`);
1383
+ const name = one(rest, "program, by its name or client id");
1384
+ const found = listed.data.programs.filter((g) => g.client === name || same(g.name, name));
1385
+ const [only, ...others] = found;
1386
+ if (!only)
1387
+ throw new Refusal(`No program you allowed is ${clean(name)}: brood programs lists them.`);
1388
+ if (others.length)
1389
+ throw new Refusal(`Several programs are called ${clean(name)}. Name the one you mean by its client id.`);
1390
+ // Once revoked, this program could not ask what came of it.
1391
+ if (verb === "revoke" && only.this_program)
1392
+ throw new Refusal("This is the program asking. Revoke it on your account page, or run brood logout to end this session.");
1393
+ return approvedInApp(io, style, verb === "allow" ? "allow_program" : "revoke_program", { client: only.client }, `${verb} ${only.name ?? only.client}`);
1394
+ };
576
1395
  /** Each command, and the options it takes, so one it does not is refused rather than ignored. */
577
1396
  export const COMMANDS = {
578
1397
  accounts: { run: accounts, takes: [] },
@@ -592,5 +1411,15 @@ export const COMMANDS = {
592
1411
  hush: { run: hush, takes: [] },
593
1412
  unhush: { run: unhush, takes: [] },
594
1413
  keep: { run: keep, takes: ["check", "yes", "limit", "cursor"] },
595
- customers: { run: customers, takes: ["check", "yes"] },
1414
+ relationships: { run: relationships, takes: ["group", "check", "yes"] },
1415
+ groups: { run: groups, takes: [] },
1416
+ quieted: { run: quieted, takes: ["limit", "cursor"] },
1417
+ batch: { run: batch, takes: [] },
1418
+ "own-rules": { run: ownRules, takes: ["yes"] },
1419
+ "keep-out": { run: keepOutCommand, takes: ["yes"] },
1420
+ "rule-book": { run: ruleBook, takes: ["check", "yes"] },
1421
+ preferences: { run: preferences, takes: [] },
1422
+ programs: { run: programs, takes: [] },
1423
+ proposals: { run: proposals, takes: ["all", "check", "yes"] },
1424
+ unsubscribe: { run: unsubscribeCommand, takes: ["all", "check", "yes"] },
596
1425
  };