@bobfrankston/rmfmail 1.2.197 → 1.2.198

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.
@@ -9503,24 +9503,16 @@ var CompatImapClient = class {
9503
9503
  await this.native.closeMailbox();
9504
9504
  return msgs.map((m) => new FetchedMessage(m));
9505
9505
  }
9506
- /** Search messages in a mailbox */
9506
+ /** Search messages in a mailbox.
9507
+ * Criteria: from/to/cc/subject/body/text (string or string[], each key
9508
+ * repeats and ANDs), since/before (Date), seen/unseen/flagged/answered/
9509
+ * draft (boolean flags), `not` (criteria object or array — each becomes
9510
+ * a NOT'd key group), `or` (array of ≥2 criteria objects — compiled to
9511
+ * IMAP's binary prefix OR chain). */
9507
9512
  async searchMessages(mailbox, criteria) {
9508
9513
  await this.ensureConnected();
9509
9514
  await this.native.select(mailbox);
9510
- const parts = [];
9511
- if (criteria.from)
9512
- parts.push(`FROM "${criteria.from}"`);
9513
- if (criteria.to)
9514
- parts.push(`TO "${criteria.to}"`);
9515
- if (criteria.subject)
9516
- parts.push(`SUBJECT "${criteria.subject}"`);
9517
- if (criteria.body)
9518
- parts.push(`BODY "${criteria.body}"`);
9519
- if (criteria.since)
9520
- parts.push(`SINCE ${formatDate(criteria.since)}`);
9521
- if (criteria.before)
9522
- parts.push(`BEFORE ${formatDate(criteria.before)}`);
9523
- const searchStr = parts.length > 0 ? parts.join(" ") : "ALL";
9515
+ const searchStr = buildSearchString(criteria || {});
9524
9516
  const uids = await this.native.search(searchStr);
9525
9517
  await this.native.closeMailbox();
9526
9518
  return uids;
@@ -9680,6 +9672,71 @@ var CompatImapClient = class {
9680
9672
  return msg?.flags ? [...msg.flags] : [];
9681
9673
  }
9682
9674
  };
9675
+ function imapQuote(v) {
9676
+ return `"${String(v).replace(/([\\"])/g, "\\$1")}"`;
9677
+ }
9678
+ function searchKeys(c) {
9679
+ const parts = [];
9680
+ const each = (v, fn) => {
9681
+ for (const x of Array.isArray(v) ? v : [v])
9682
+ if (x)
9683
+ fn(String(x));
9684
+ };
9685
+ if (c.from)
9686
+ each(c.from, (v) => parts.push(`FROM ${imapQuote(v)}`));
9687
+ if (c.to)
9688
+ each(c.to, (v) => parts.push(`TO ${imapQuote(v)}`));
9689
+ if (c.cc)
9690
+ each(c.cc, (v) => parts.push(`CC ${imapQuote(v)}`));
9691
+ if (c.subject)
9692
+ each(c.subject, (v) => parts.push(`SUBJECT ${imapQuote(v)}`));
9693
+ if (c.body)
9694
+ each(c.body, (v) => parts.push(`BODY ${imapQuote(v)}`));
9695
+ if (c.text)
9696
+ each(c.text, (v) => parts.push(`TEXT ${imapQuote(v)}`));
9697
+ if (c.since)
9698
+ parts.push(`SINCE ${formatDate(c.since)}`);
9699
+ if (c.before)
9700
+ parts.push(`BEFORE ${formatDate(c.before)}`);
9701
+ if (c.seen)
9702
+ parts.push("SEEN");
9703
+ if (c.unseen)
9704
+ parts.push("UNSEEN");
9705
+ if (c.flagged)
9706
+ parts.push("FLAGGED");
9707
+ if (c.unflagged)
9708
+ parts.push("UNFLAGGED");
9709
+ if (c.answered)
9710
+ parts.push("ANSWERED");
9711
+ if (c.draft)
9712
+ parts.push("DRAFT");
9713
+ return parts;
9714
+ }
9715
+ function keyGroup(c) {
9716
+ const keys = searchKeys(c);
9717
+ if (keys.length === 0)
9718
+ return null;
9719
+ return keys.length === 1 ? keys[0] : `(${keys.join(" ")})`;
9720
+ }
9721
+ function buildSearchString(criteria) {
9722
+ const parts = searchKeys(criteria);
9723
+ const nots = criteria.not ? Array.isArray(criteria.not) ? criteria.not : [criteria.not] : [];
9724
+ for (const n of nots) {
9725
+ const g = keyGroup(n);
9726
+ if (g)
9727
+ parts.push(`NOT ${g}`);
9728
+ }
9729
+ const ors = (Array.isArray(criteria.or) ? criteria.or : []).map(keyGroup).filter(Boolean);
9730
+ if (ors.length === 1)
9731
+ parts.push(ors[0]);
9732
+ else if (ors.length > 1) {
9733
+ let expr = ors[ors.length - 1];
9734
+ for (let i = ors.length - 2; i >= 0; i--)
9735
+ expr = `OR ${ors[i]} ${expr}`;
9736
+ parts.push(expr);
9737
+ }
9738
+ return parts.length > 0 ? parts.join(" ") : "ALL";
9739
+ }
9683
9740
  function formatDate(d) {
9684
9741
  const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
9685
9742
  return `${d.getDate()}-${months[d.getMonth()]}-${d.getFullYear()}`;