aboutsummaryrefslogtreecommitdiffstats
path: root/src/wamail/neomutt.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/wamail/neomutt.js')
-rw-r--r--src/wamail/neomutt.js46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/wamail/neomutt.js b/src/wamail/neomutt.js
new file mode 100644
index 0000000..22d6dc8
--- /dev/null
+++ b/src/wamail/neomutt.js
@@ -0,0 +1,46 @@
+// Mailbox list for neomutt, regenerated on every sync: one mailbox per chat, ordered
+// like WhatsApp (pinned, then by last activity; archived at the end).
+// The neomutt account includes it with `source`.
+
+import fs from 'node:fs';
+import path from 'node:path';
+import { CACHE_DIR, ensureMaildir } from './store.js';
+
+export function mailboxesPath(account) {
+ return path.join(CACHE_DIR, account, 'mailboxes.muttrc');
+}
+
+// Names come from other people (group names): no quotes, backticks or `$`,
+// which neomutt would interpret on `source`.
+const safe = (s) => String(s ?? '').replace(/[\x00-\x1f'"`$\\]/g, '').trim() || '?';
+const regexEscape = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
+
+export function writeMailboxes(account, root, state) {
+ const chats = Object.values(state.chats).filter((c) => c.dir);
+ const byActivity = (a, b) => (b.timestamp ?? 0) - (a.timestamp ?? 0);
+ const active = chats.filter((c) => !c.archived).sort((a, b) => (Number(Boolean(b.pinned)) - Number(Boolean(a.pinned))) || byActivity(a, b));
+ const archived = chats.filter((c) => c.archived).sort(byActivity);
+
+ const lines = ['# Generated by `wamail sync` — do not edit by hand.'];
+ if (active[0]) lines.push(`set spoolfile = '${path.join(root, active[0].dir)}'`);
+ if (state.me?.id) lines.push(`alternates '^${regexEscape(state.me.id)}$'`);
+ for (const c of active) {
+ const icon = c.isGroup ? '' : '';
+ lines.push(`named-mailboxes '${icon} ${c.pinned ? ' ' : ''}${safe(c.name)}' '${path.join(root, c.dir)}'`);
+ }
+ for (const c of archived) lines.push(`named-mailboxes ' ${safe(c.name)}' '${path.join(root, c.dir)}'`);
+ ensureMaildir(path.join(root, 'Drafts')); // neomutt rechaza buzones inexistentes
+ lines.push(`named-mailboxes ' Drafts' '${path.join(root, 'Drafts')}'`);
+
+ const file = mailboxesPath(account);
+ const text = `${lines.join('\n')}\n`;
+ let current = null;
+ try {
+ current = fs.readFileSync(file, 'utf8');
+ } catch { /* primera vez */ }
+ if (current === text) return file;
+ fs.mkdirSync(path.dirname(file), { recursive: true });
+ fs.writeFileSync(`${file}.tmp`, text);
+ fs.renameSync(`${file}.tmp`, file);
+ return file;
+}