domma-cms 1.28.4 → 1.29.1
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/CHANGELOG.md +9 -0
- package/admin/js/lib/contacts-arrange.js +1 -1
- package/docs/DommaFeatures.md +15 -9
- package/docs/plugin-development.md +1 -1
- package/package.json +4 -1
- package/plugins/_lib/admin/mail/body-menu.js +2 -0
- package/plugins/_lib/admin/mail/cms-host.js +22 -0
- package/plugins/_lib/admin/mail/compose-window.js +1027 -1021
- package/plugins/_lib/admin/mail/contacts.js +10 -8
- package/plugins/_lib/admin/mail/diagnostics-section.js +2 -0
- package/plugins/_lib/admin/mail/drag-drop.js +46 -0
- package/plugins/_lib/admin/mail/folder-tree.js +2 -0
- package/plugins/_lib/admin/mail/font-controls.js +6 -4
- package/plugins/_lib/admin/mail/fonts.js +1 -1
- package/plugins/_lib/admin/mail/host-context.js +87 -0
- package/plugins/_lib/admin/mail/identity.js +2 -0
- package/plugins/_lib/admin/mail/image-senders-section.js +2 -0
- package/plugins/_lib/admin/mail/keyboard.js +2 -0
- package/plugins/_lib/admin/mail/mail.css +1 -1
- package/plugins/_lib/admin/mail/mail.html +4 -0
- package/plugins/_lib/admin/mail/message-actions.js +2 -0
- package/plugins/_lib/admin/mail/oauth-panel.js +2 -0
- package/plugins/_lib/admin/mail/panes.js +2 -0
- package/plugins/_lib/admin/mail/reader-kit.js +2 -0
- package/plugins/_lib/admin/mail/reader-runtime.js +6534 -0
- package/plugins/_lib/admin/mail/reader-view.js +9 -6380
- package/plugins/_lib/admin/mail/resizable.js +2 -0
- package/plugins/_lib/admin/mail/restore-spam.js +13 -0
- package/plugins/_lib/admin/mail/rules.js +14 -0
- package/plugins/_lib/admin/mail/scheduling.js +4 -1
- package/plugins/_lib/admin/mail/section-kit.js +4 -1
- package/plugins/_lib/admin/mail/security-note.js +2 -0
- package/plugins/_lib/admin/mail/settings-section.js +11 -10
- package/plugins/_lib/admin/mail/templates.js +5 -2
- package/plugins/_lib/admin/mail/threads.js +2 -0
- package/plugins/_lib/admin/mail/vacation.js +16 -2
- package/plugins/_lib/admin/mail/when-closed.js +24 -2
- package/plugins/_lib/admin/ui/dates.js +2 -0
- package/plugins/_lib/admin/ui/resizable.js +7 -5
- package/plugins/_lib/contacts/mailContact.js +69 -0
- package/plugins/_lib/contacts/model.js +199 -0
- package/plugins/_lib/domma-mail-contacts.lock.json +10 -0
- package/plugins/_lib/domma-mail.lock.json +85 -0
- package/plugins/_lib/mail/accountFileStore.js +66 -0
- package/plugins/_lib/mail/accountRepository.js +617 -0
- package/plugins/_lib/mail/accountRoutes.js +130 -0
- package/plugins/_lib/mail/accounts.js +66 -940
- package/plugins/_lib/mail/automationLock.js +10 -0
- package/plugins/_lib/mail/bodyTokenProvider.js +36 -0
- package/plugins/_lib/mail/bodyTokens.js +5 -99
- package/plugins/_lib/mail/compose.js +5 -3
- package/plugins/_lib/mail/defaults.js +2 -0
- package/plugins/_lib/mail/envelope.js +3 -1
- package/plugins/_lib/mail/folderActions.js +83 -0
- package/plugins/_lib/mail/folderRuntime.js +109 -0
- package/plugins/_lib/mail/folders.js +4 -9
- package/plugins/_lib/mail/imapPool.js +11 -389
- package/plugins/_lib/mail/imapPoolRuntime.js +396 -0
- package/plugins/_lib/mail/mbox.js +2 -0
- package/plugins/_lib/mail/message-action.js +2 -0
- package/plugins/_lib/mail/messageSecurity.js +4 -111
- package/plugins/_lib/mail/messageSecurityProvider.js +116 -0
- package/plugins/_lib/mail/networkAddresses.js +77 -0
- package/plugins/_lib/mail/networkPolicy.js +62 -0
- package/plugins/_lib/mail/oauth.js +3 -451
- package/plugins/_lib/mail/oauthAccounts.js +178 -0
- package/plugins/_lib/mail/oauthRouteRuntime.js +282 -0
- package/plugins/_lib/mail/oauthRoutes.js +7 -289
- package/plugins/_lib/mail/oauthRuntime.js +456 -0
- package/plugins/_lib/mail/pollSchedule.js +2 -0
- package/plugins/_lib/mail/poller.js +4 -136
- package/plugins/_lib/mail/pollerRuntime.js +150 -0
- package/plugins/_lib/mail/priority.js +2 -0
- package/plugins/_lib/mail/publicMailbox.js +27 -0
- package/plugins/_lib/mail/readRoutes.js +14 -827
- package/plugins/_lib/mail/readRuntime.js +701 -0
- package/plugins/_lib/mail/render.js +2 -0
- package/plugins/_lib/mail/ruleRunner.js +27 -10
- package/plugins/_lib/mail/scheduler.js +11 -279
- package/plugins/_lib/mail/schedulerRuntime.js +259 -0
- package/plugins/_lib/mail/secretProvider.js +112 -0
- package/plugins/_lib/mail/secretbox.js +12 -225
- package/plugins/_lib/mail/send.js +21 -537
- package/plugins/_lib/mail/sieve.js +211 -0
- package/plugins/_lib/mail/sieveHost.js +24 -0
- package/plugins/_lib/mail/smtpService.js +553 -0
- package/plugins/_lib/mail/snoozeRuntime.js +60 -0
- package/plugins/_lib/mail/store.js +122 -64
- package/plugins/_lib/mail/subscriptions.js +11 -0
- package/plugins/_lib/mail/sync.js +64 -290
- package/plugins/_lib/mail/syncPlan.js +2 -0
- package/plugins/_lib/mail/syncRuntime.js +255 -0
- package/plugins/_lib/mail/syncSelection.js +2 -0
- package/plugins/_lib/mail/unreadBadge.js +2 -0
- package/plugins/_lib/mail/unsubscribe.js +2 -0
- package/plugins/_lib/mail/vacationRunner.js +3 -116
- package/plugins/_lib/mail/vacationRuntime.js +70 -0
- package/plugins/_lib/mail/write.js +44 -13
- package/server/routes/api/contacts.js +2 -196
- package/server/routes/api/forms.js +8 -1
- package/server/services/viewerSession.js +1 -1
|
@@ -0,0 +1,617 @@
|
|
|
1
|
+
// Source: @domma-mail/core 0.18.0, packages/core/src/mail/accountRepository.js.
|
|
2
|
+
// Update with domma-mail/scripts/sync-cms.mjs; edit the shared source.
|
|
3
|
+
export const POLL_INTERVALS = Object.freeze([3, 5, 10, 15]);
|
|
4
|
+
import {randomUUID} from 'node:crypto';
|
|
5
|
+
import {normaliseIdentityList, findIdentity, bareAddress, signatureToText} from '../admin/mail/identity.js';
|
|
6
|
+
import {normaliseTemplates} from '../admin/mail/templates.js';
|
|
7
|
+
import {sanitiseSignature} from './render.js';
|
|
8
|
+
import {normaliseVacation} from '../admin/mail/vacation.js';
|
|
9
|
+
import {normaliseUndoSeconds} from '../admin/mail/scheduling.js';
|
|
10
|
+
import {normaliseRules, normaliseRuleGroups, rulesInEffect} from '../admin/mail/rules.js';
|
|
11
|
+
|
|
12
|
+
/** Shared lifecycle extracted unchanged from the matching CMS and DCM account implementations. */
|
|
13
|
+
export function createAccountRepository({store, secrets, isOAuth = account => account?.auth === 'oauth' && !!account.oauth, oauthConnection} = {}) {
|
|
14
|
+
if (!store || !secrets) throw new Error('Mail storage and a secret provider are required.');
|
|
15
|
+
const {readAll, writeAll, withStoreLock} = store;
|
|
16
|
+
const {seal, open} = secrets;
|
|
17
|
+
function validate(input, existing) {
|
|
18
|
+
const host = String(input.host ?? '').trim();
|
|
19
|
+
const username = String(input.username ?? '').trim();
|
|
20
|
+
const label = String(input.label ?? '').trim();
|
|
21
|
+
const port = Number(input.port ?? (input.secure === false ? 143 : 993));
|
|
22
|
+
const secure = input.secure !== false;
|
|
23
|
+
const password = typeof input.password === 'string' ? input.password : '';
|
|
24
|
+
|
|
25
|
+
// A signed-in mailbox keeps its provider's servers and address: only the
|
|
26
|
+
// name is the person's to change here. A sign-in is changed by Reconnect.
|
|
27
|
+
if (isOAuth(existing)) {
|
|
28
|
+
return {ok: true, value: {
|
|
29
|
+
label: label || existing.label, host: existing.host, port: existing.port,
|
|
30
|
+
secure: existing.secure, username: existing.username, password: '', oauth: true
|
|
31
|
+
}};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (!host) return {ok: false, error: 'A mail server hostname is required.'};
|
|
35
|
+
if (!username) return {ok: false, error: 'A username is required.'};
|
|
36
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
|
37
|
+
return {ok: false, error: 'Port must be a whole number between 1 and 65535.'};
|
|
38
|
+
}
|
|
39
|
+
if (!password && !existing?.passwordSealed) {
|
|
40
|
+
return {ok: false, error: 'A password is required.'};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
ok: true,
|
|
45
|
+
value: {
|
|
46
|
+
label: label || host,
|
|
47
|
+
host,
|
|
48
|
+
port,
|
|
49
|
+
secure,
|
|
50
|
+
username,
|
|
51
|
+
// An empty password on update means "keep the one you have" - the
|
|
52
|
+
// admin form never round-trips the stored password, so a blank
|
|
53
|
+
// field is the normal case when editing the hostname.
|
|
54
|
+
password
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Every mailbox belonging to one user: the default first, then oldest first.
|
|
61
|
+
*
|
|
62
|
+
* @param {string} userId
|
|
63
|
+
* @returns {Promise<object[]>}
|
|
64
|
+
*/
|
|
65
|
+
async function listAccounts(userId) {
|
|
66
|
+
const all = await readAll();
|
|
67
|
+
return all
|
|
68
|
+
.filter(a => a.userId === userId)
|
|
69
|
+
.sort((a, b) => (b.isDefault === true) - (a.isDefault === true)
|
|
70
|
+
|| String(a.createdAt).localeCompare(String(b.createdAt)));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Make one mailbox this user's default, or clear the default.
|
|
75
|
+
*
|
|
76
|
+
* At most one per user, so setting it takes it off every other mailbox they
|
|
77
|
+
* own. The default is listed first and is the one the reader opens with.
|
|
78
|
+
*
|
|
79
|
+
* @param {string} userId
|
|
80
|
+
* @param {string} accountId
|
|
81
|
+
* @param {boolean} isDefault - false clears it from this mailbox only
|
|
82
|
+
* @returns {Promise<object|null>} the stored record, or null if not theirs
|
|
83
|
+
*/
|
|
84
|
+
async function setDefaultAccount(userId, accountId, isDefault) {
|
|
85
|
+
return withStoreLock(async () => {
|
|
86
|
+
const all = await readAll();
|
|
87
|
+
const index = all.findIndex(a => a.id === accountId && a.userId === userId);
|
|
88
|
+
if (index === -1) return null;
|
|
89
|
+
|
|
90
|
+
const now = new Date().toISOString();
|
|
91
|
+
for (let i = 0; i < all.length; i++) {
|
|
92
|
+
if (all[i].userId !== userId) continue;
|
|
93
|
+
const want = i === index ? !!isDefault : (isDefault ? false : all[i].isDefault === true);
|
|
94
|
+
if ((all[i].isDefault === true) !== want) all[i] = {...all[i], isDefault: want, updatedAt: now};
|
|
95
|
+
}
|
|
96
|
+
await writeAll(all);
|
|
97
|
+
return all[index];
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* One mailbox, but only if this user owns it.
|
|
103
|
+
*
|
|
104
|
+
* Takes both ids on purpose: there is no way to ask for an account without
|
|
105
|
+
* saying who is asking.
|
|
106
|
+
*
|
|
107
|
+
* @param {string} userId
|
|
108
|
+
* @param {string} accountId
|
|
109
|
+
* @returns {Promise<object|null>}
|
|
110
|
+
*/
|
|
111
|
+
async function getAccount(userId, accountId) {
|
|
112
|
+
const all = await readAll();
|
|
113
|
+
return all.find(a => a.id === accountId && a.userId === userId) ?? null;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Create a mailbox, or update one this user owns.
|
|
118
|
+
*
|
|
119
|
+
* @param {string} userId
|
|
120
|
+
* @param {string|null} accountId - null to create
|
|
121
|
+
* @param {object} fields - output of validate()
|
|
122
|
+
* @returns {Promise<object|null>} the stored record, or null if not theirs
|
|
123
|
+
*/
|
|
124
|
+
async function saveAccount(userId, accountId, fields) {
|
|
125
|
+
return withStoreLock(async () => {
|
|
126
|
+
const all = await readAll();
|
|
127
|
+
const now = new Date().toISOString();
|
|
128
|
+
|
|
129
|
+
if (accountId) {
|
|
130
|
+
const index = all.findIndex(a => a.id === accountId && a.userId === userId);
|
|
131
|
+
if (index === -1) return null;
|
|
132
|
+
const existing = all[index];
|
|
133
|
+
if (isOAuth(existing)) {
|
|
134
|
+
all[index] = {...existing, label: fields.label, updatedAt: now};
|
|
135
|
+
await writeAll(all);
|
|
136
|
+
return all[index];
|
|
137
|
+
}
|
|
138
|
+
all[index] = {
|
|
139
|
+
...existing,
|
|
140
|
+
label: fields.label,
|
|
141
|
+
host: fields.host,
|
|
142
|
+
port: fields.port,
|
|
143
|
+
secure: fields.secure,
|
|
144
|
+
username: fields.username,
|
|
145
|
+
passwordSealed: fields.password ? seal(fields.password) : existing.passwordSealed,
|
|
146
|
+
updatedAt: now
|
|
147
|
+
};
|
|
148
|
+
await writeAll(all);
|
|
149
|
+
return all[index];
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const record = {
|
|
153
|
+
id: randomUUID(),
|
|
154
|
+
userId,
|
|
155
|
+
label: fields.label,
|
|
156
|
+
host: fields.host,
|
|
157
|
+
port: fields.port,
|
|
158
|
+
secure: fields.secure,
|
|
159
|
+
username: fields.username,
|
|
160
|
+
passwordSealed: seal(fields.password),
|
|
161
|
+
createdAt: now,
|
|
162
|
+
updatedAt: now
|
|
163
|
+
};
|
|
164
|
+
all.push(record);
|
|
165
|
+
await writeAll(all);
|
|
166
|
+
return record;
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Remove a mailbox this user owns.
|
|
172
|
+
*
|
|
173
|
+
* @param {string} userId
|
|
174
|
+
* @param {string} accountId
|
|
175
|
+
* @returns {Promise<boolean>} true when something was removed
|
|
176
|
+
*/
|
|
177
|
+
async function deleteAccount(userId, accountId) {
|
|
178
|
+
return withStoreLock(async () => {
|
|
179
|
+
const all = await readAll();
|
|
180
|
+
const next = all.filter(a => !(a.id === accountId && a.userId === userId));
|
|
181
|
+
if (next.length === all.length) return false;
|
|
182
|
+
await writeAll(next);
|
|
183
|
+
return true;
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Forget every mailbox belonging to one user.
|
|
189
|
+
*
|
|
190
|
+
* Called when a CMS user is deleted. Their stored passwords have no owner
|
|
191
|
+
* left and must not outlive the account.
|
|
192
|
+
*
|
|
193
|
+
* @param {string} userId
|
|
194
|
+
* @returns {Promise<number>} how many were removed
|
|
195
|
+
*/
|
|
196
|
+
async function deleteAccountsForUser(userId) {
|
|
197
|
+
return withStoreLock(async () => {
|
|
198
|
+
const all = await readAll();
|
|
199
|
+
const next = all.filter(a => a.userId !== userId);
|
|
200
|
+
const removed = all.length - next.length;
|
|
201
|
+
if (removed) await writeAll(next);
|
|
202
|
+
return removed;
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Drop mailboxes whose owner no longer exists.
|
|
208
|
+
*
|
|
209
|
+
* The `user:deleted` hook covers the normal case, but only while a plugin
|
|
210
|
+
* using this core is loaded to hear it - a user deleted while the Mail Reader
|
|
211
|
+
* was disabled would otherwise leave credentials behind for good. This sweeps
|
|
212
|
+
* those up on the next start, so the hook is the fast path rather than the
|
|
213
|
+
* only one.
|
|
214
|
+
*
|
|
215
|
+
* @param {Set<string>|string[]} existingUserIds
|
|
216
|
+
* @returns {Promise<number>} how many were removed
|
|
217
|
+
*/
|
|
218
|
+
async function pruneOrphans(existingUserIds) {
|
|
219
|
+
return withStoreLock(async () => {
|
|
220
|
+
if (!(existingUserIds instanceof Set) && !Array.isArray(existingUserIds)) {
|
|
221
|
+
throw new Error('A complete user ID set is required before pruning mail accounts.');
|
|
222
|
+
}
|
|
223
|
+
const live = existingUserIds instanceof Set ? existingUserIds : new Set(existingUserIds);
|
|
224
|
+
const all = await readAll();
|
|
225
|
+
const next = all.filter(a => live.has(a.userId));
|
|
226
|
+
const removed = all.length - next.length;
|
|
227
|
+
if (removed) await writeAll(next);
|
|
228
|
+
return removed;
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Build an ImapFlow-shaped connection config.
|
|
234
|
+
*
|
|
235
|
+
* Throws MailKeyError (from secretbox) if the stored password cannot be
|
|
236
|
+
* opened - callers surface that message directly, it is written for humans.
|
|
237
|
+
*
|
|
238
|
+
* @param {object} account
|
|
239
|
+
* @returns {{host: string, port: number, secure: boolean, auth: {user: string, pass: string}}}
|
|
240
|
+
*/
|
|
241
|
+
function toConnection(account) {
|
|
242
|
+
if (isOAuth(account)) {
|
|
243
|
+
if (typeof oauthConnection !== 'function') throw new Error('OAuth mailboxes require a provider connection adapter.');
|
|
244
|
+
return oauthConnection(account);
|
|
245
|
+
}
|
|
246
|
+
return {
|
|
247
|
+
host: account.host,
|
|
248
|
+
port: account.port,
|
|
249
|
+
secure: account.secure,
|
|
250
|
+
auth: {user: account.username, pass: open(account.passwordSealed)}
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
async function setSmtp(userId, accountId, smtp) {
|
|
256
|
+
return withStoreLock(async () => {
|
|
257
|
+
const all = await readAll();
|
|
258
|
+
const index = all.findIndex(a => a.id === accountId && a.userId === userId);
|
|
259
|
+
if (index === -1) return null;
|
|
260
|
+
|
|
261
|
+
const existing = all[index].smtp ?? {};
|
|
262
|
+
// Recheck under the store lock: an OAuth reconnect may have replaced
|
|
263
|
+
// a password mailbox after the route read it. Never redirect a token.
|
|
264
|
+
if (isOAuth(all[index])) smtp = {...smtp, host: existing.host, port: existing.port,
|
|
265
|
+
secure: existing.secure, username: all[index].username, password: ''};
|
|
266
|
+
all[index] = {
|
|
267
|
+
...all[index],
|
|
268
|
+
smtp: {
|
|
269
|
+
host: smtp.host,
|
|
270
|
+
port: smtp.port,
|
|
271
|
+
secure: smtp.secure,
|
|
272
|
+
username: smtp.username,
|
|
273
|
+
passwordSealed: smtp.password
|
|
274
|
+
? seal(smtp.password)
|
|
275
|
+
: (existing.passwordSealed ?? null),
|
|
276
|
+
from: smtp.from,
|
|
277
|
+
sentFolder: smtp.sentFolder ?? null
|
|
278
|
+
},
|
|
279
|
+
updatedAt: new Date().toISOString()
|
|
280
|
+
};
|
|
281
|
+
await writeAll(all);
|
|
282
|
+
return all[index];
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Build a nodemailer transport config for a mailbox.
|
|
288
|
+
*
|
|
289
|
+
* Falls back to the IMAP credentials when the outgoing server has none of its
|
|
290
|
+
* own, which is the common case.
|
|
291
|
+
*
|
|
292
|
+
* `identityId` chooses which of the mailbox's identities is sending. Absent,
|
|
293
|
+
* it is the default one - which is what every message sent before aliases
|
|
294
|
+
* existed did, and what most messages still do.
|
|
295
|
+
*
|
|
296
|
+
* @param {object} account
|
|
297
|
+
* @param {string|null} [identityId]
|
|
298
|
+
* @returns {{transport: object, from: string}|null}
|
|
299
|
+
*/
|
|
300
|
+
function toSmtpConnection(account, identityId = null) {
|
|
301
|
+
const smtp = account?.smtp;
|
|
302
|
+
if (!smtp?.host) return null;
|
|
303
|
+
|
|
304
|
+
let auth;
|
|
305
|
+
if (isOAuth(account)) {
|
|
306
|
+
if (typeof oauthConnection !== 'function') throw new Error('OAuth mailboxes require a provider connection adapter.');
|
|
307
|
+
const linked = oauthConnection(account);
|
|
308
|
+
const accessToken = linked.auth.accessToken;
|
|
309
|
+
const expiresAt = linked.expiresAt;
|
|
310
|
+
const renew = linked.refresh;
|
|
311
|
+
auth = {
|
|
312
|
+
type: 'OAuth2',
|
|
313
|
+
user: smtp.username || account.username,
|
|
314
|
+
accessToken,
|
|
315
|
+
// 0 = unknown: nodemailer then asks provisionCallback straight away.
|
|
316
|
+
...(expiresAt ? {expires: expiresAt - 60_000} : {}),
|
|
317
|
+
// nodemailer calls this when the token has expired (renew=false)
|
|
318
|
+
// and once more after a refused AUTH XOAUTH2 (renew=true) - which
|
|
319
|
+
// is exactly "refresh on an auth failure, once".
|
|
320
|
+
provisionCallback: (user, renewNow, callback) => {
|
|
321
|
+
// Both branches guarded, and a final catch: a throw inside
|
|
322
|
+
// nodemailer's callback must never become an unhandled rejection.
|
|
323
|
+
renew(Boolean(renewNow))
|
|
324
|
+
.then(r => callback(null, r.accessToken, r.expiresAt), err => callback(err))
|
|
325
|
+
.catch(() => {});
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
} else {
|
|
329
|
+
const pass = smtp.passwordSealed
|
|
330
|
+
? open(smtp.passwordSealed)
|
|
331
|
+
: open(account.passwordSealed);
|
|
332
|
+
auth = {user: smtp.username || account.username, pass};
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const identity = findIdentity(normaliseIdentityList(account), identityId);
|
|
336
|
+
const mailboxAddress = smtp.from || account.username;
|
|
337
|
+
|
|
338
|
+
return {
|
|
339
|
+
transport: {
|
|
340
|
+
host: smtp.host,
|
|
341
|
+
port: smtp.port,
|
|
342
|
+
secure: smtp.secure !== false,
|
|
343
|
+
auth
|
|
344
|
+
},
|
|
345
|
+
// Still the bare address, as it always was. The display name travels
|
|
346
|
+
// beside it rather than folded into it, because the two go to
|
|
347
|
+
// different places: the name belongs in the From header and must stay
|
|
348
|
+
// out of the SMTP envelope.
|
|
349
|
+
//
|
|
350
|
+
// An alias replaces it outright: sending AS sales@ means the From and
|
|
351
|
+
// the envelope both say sales@, or the alias fails DMARC alignment
|
|
352
|
+
// and lands in spam at a good many receivers.
|
|
353
|
+
from: identity?.address || mailboxAddress,
|
|
354
|
+
fromName: identity?.displayName ?? '',
|
|
355
|
+
replyTo: identity?.replyTo ?? '',
|
|
356
|
+
identity: identity ?? null,
|
|
357
|
+
sentFolder: smtp.sentFolder ?? null
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* The outgoing settings, without the password.
|
|
363
|
+
*
|
|
364
|
+
* @param {object} account
|
|
365
|
+
* @returns {object|null}
|
|
366
|
+
*/
|
|
367
|
+
function smtpToPublic(account) {
|
|
368
|
+
const smtp = account?.smtp;
|
|
369
|
+
if (!smtp) return null;
|
|
370
|
+
const {passwordSealed, ...rest} = smtp;
|
|
371
|
+
return {...rest, hasPassword: typeof passwordSealed === 'string' && passwordSealed.length > 0};
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
async function trustImageSender(userId, accountId, address) {
|
|
376
|
+
return withStoreLock(async () => {
|
|
377
|
+
const clean = String(address ?? '').trim().toLowerCase();
|
|
378
|
+
if (!clean) return null;
|
|
379
|
+
|
|
380
|
+
const all = await readAll();
|
|
381
|
+
const index = all.findIndex(a => a.id === accountId && a.userId === userId);
|
|
382
|
+
if (index === -1) return null;
|
|
383
|
+
|
|
384
|
+
const current = Array.isArray(all[index].imageSenders) ? all[index].imageSenders : [];
|
|
385
|
+
if (current.includes(clean)) return all[index];
|
|
386
|
+
|
|
387
|
+
all[index] = {...all[index], imageSenders: [...current, clean], updatedAt: new Date().toISOString()};
|
|
388
|
+
await writeAll(all);
|
|
389
|
+
return all[index];
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Stop trusting a sender's images.
|
|
395
|
+
*
|
|
396
|
+
* @param {string} userId
|
|
397
|
+
* @param {string} accountId
|
|
398
|
+
* @param {string} address
|
|
399
|
+
* @returns {Promise<object|null>}
|
|
400
|
+
*/
|
|
401
|
+
async function untrustImageSender(userId, accountId, address) {
|
|
402
|
+
return withStoreLock(async () => {
|
|
403
|
+
const clean = String(address ?? '').trim().toLowerCase();
|
|
404
|
+
const all = await readAll();
|
|
405
|
+
const index = all.findIndex(a => a.id === accountId && a.userId === userId);
|
|
406
|
+
if (index === -1) return null;
|
|
407
|
+
|
|
408
|
+
const current = Array.isArray(all[index].imageSenders) ? all[index].imageSenders : [];
|
|
409
|
+
all[index] = {
|
|
410
|
+
...all[index],
|
|
411
|
+
imageSenders: current.filter(a => a !== clean),
|
|
412
|
+
updatedAt: new Date().toISOString()
|
|
413
|
+
};
|
|
414
|
+
await writeAll(all);
|
|
415
|
+
return all[index];
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Turn the whole remembering feature on or off for a mailbox.
|
|
421
|
+
*
|
|
422
|
+
* Off means the button is never offered and any existing list is ignored
|
|
423
|
+
* without being thrown away - turning it back on should not mean rebuilding
|
|
424
|
+
* a list someone curated.
|
|
425
|
+
*
|
|
426
|
+
* @param {string} userId
|
|
427
|
+
* @param {string} accountId
|
|
428
|
+
* @param {boolean} enabled
|
|
429
|
+
* @returns {Promise<object|null>}
|
|
430
|
+
*/
|
|
431
|
+
async function setRememberImageSenders(userId, accountId, enabled) {
|
|
432
|
+
return withStoreLock(async () => {
|
|
433
|
+
const all = await readAll();
|
|
434
|
+
const index = all.findIndex(a => a.id === accountId && a.userId === userId);
|
|
435
|
+
if (index === -1) return null;
|
|
436
|
+
|
|
437
|
+
all[index] = {...all[index], rememberImageSenders: !!enabled, updatedAt: new Date().toISOString()};
|
|
438
|
+
await writeAll(all);
|
|
439
|
+
return all[index];
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* Should this message's remote images load without being asked?
|
|
445
|
+
*
|
|
446
|
+
* @param {object} account
|
|
447
|
+
* @param {Array<{address?: string}>} from
|
|
448
|
+
* @returns {boolean}
|
|
449
|
+
*/
|
|
450
|
+
function sendersImagesTrusted(account, from) {
|
|
451
|
+
if (account?.rememberImageSenders === false) return false;
|
|
452
|
+
const trusted = Array.isArray(account?.imageSenders) ? account.imageSenders : [];
|
|
453
|
+
if (!trusted.length) return false;
|
|
454
|
+
return (from ?? []).some(entry => trusted.includes(String(entry?.address ?? '').trim().toLowerCase()));
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
async function setPollInterval(userId, accountId, minutes) {
|
|
458
|
+
return withStoreLock(async () => {
|
|
459
|
+
if (minutes !== null && !POLL_INTERVALS.includes(minutes)) {
|
|
460
|
+
return {ok: false, error: `Poll interval must be one of ${POLL_INTERVALS.join(', ')} minutes.`};
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
const all = await readAll();
|
|
464
|
+
const index = all.findIndex(a => a.id === accountId && a.userId === userId);
|
|
465
|
+
if (index === -1) return {ok: false, error: 'That mailbox does not exist.'};
|
|
466
|
+
|
|
467
|
+
all[index] = {...all[index], pollIntervalMinutes: minutes, updatedAt: new Date().toISOString()};
|
|
468
|
+
await writeAll(all);
|
|
469
|
+
return {ok: true, account: all[index]};
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
async function setSyncFolders(userId, accountId, folders) {
|
|
473
|
+
return withStoreLock(async () => {
|
|
474
|
+
const all = await readAll();
|
|
475
|
+
const index = all.findIndex(a => a.id === accountId && a.userId === userId);
|
|
476
|
+
if (index === -1) return null;
|
|
477
|
+
|
|
478
|
+
const value = folders === null
|
|
479
|
+
? null
|
|
480
|
+
: [...new Set(folders.filter(f => typeof f === 'string' && f.trim()))];
|
|
481
|
+
|
|
482
|
+
all[index] = {...all[index], syncFolders: value, updatedAt: new Date().toISOString()};
|
|
483
|
+
await writeAll(all);
|
|
484
|
+
return all[index];
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
async function listAllAccounts() { return readAll(); }
|
|
489
|
+
|
|
490
|
+
/** Replace stored compose profiles in the same transaction queue as credentials. */
|
|
491
|
+
async function setProfile(userId, accountId, entries, kind) {
|
|
492
|
+
return withStoreLock(async () => {
|
|
493
|
+
const all = await readAll();
|
|
494
|
+
const index = all.findIndex(account => account.id === accountId && account.userId === userId);
|
|
495
|
+
if (index === -1) return null;
|
|
496
|
+
const html = kind === 'identities' ? 'signatureHtml' : 'bodyHtml';
|
|
497
|
+
const cleaned = (Array.isArray(entries) ? entries : []).map(entry => ({
|
|
498
|
+
...entry, [html]: sanitiseSignature(entry?.[html] ?? '')
|
|
499
|
+
}));
|
|
500
|
+
const values = {[kind]: cleaned};
|
|
501
|
+
const normalise = kind === 'identities' ? normaliseIdentityList : normaliseTemplates;
|
|
502
|
+
const record = {...all[index], [kind]: normalise(values), updatedAt: new Date().toISOString()};
|
|
503
|
+
if (kind === 'identities') delete record.identity;
|
|
504
|
+
all[index] = record;
|
|
505
|
+
await writeAll(all);
|
|
506
|
+
return record;
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
const setIdentities = (userId, accountId, entries) => setProfile(userId, accountId, entries, 'identities');
|
|
510
|
+
const setTemplates = (userId, accountId, entries) => setProfile(userId, accountId, entries, 'templates');
|
|
511
|
+
|
|
512
|
+
/** Sieve uses the mailbox credential; no password is exposed in this shape. */
|
|
513
|
+
function sieveToPublic(account) {
|
|
514
|
+
return {
|
|
515
|
+
host: account?.sieve?.host || account?.host || '',
|
|
516
|
+
port: account?.sieve?.port ?? 4190,
|
|
517
|
+
security: account?.sieve?.security ?? 'starttls',
|
|
518
|
+
configured: account?.sieve?.configured === true,
|
|
519
|
+
filtering: account?.sieve?.filtering === true
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/** Merge under the same lock as every other mailbox setting. */
|
|
524
|
+
async function setSieve(userId, accountId, changes) {
|
|
525
|
+
return withStoreLock(async () => {
|
|
526
|
+
const all = await readAll();
|
|
527
|
+
const index = all.findIndex(a => a.id === accountId && a.userId === userId);
|
|
528
|
+
if (index === -1) return null;
|
|
529
|
+
const sieve = {...sieveToPublic(all[index]), ...changes};
|
|
530
|
+
all[index] = {...all[index], sieve, updatedAt: new Date().toISOString()};
|
|
531
|
+
await writeAll(all);
|
|
532
|
+
return all[index];
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
const vacationToPublic = account => normaliseVacation(account);
|
|
537
|
+
const sendingAddresses = account => [...new Set([account?.username, account?.smtp?.from,
|
|
538
|
+
...normaliseIdentityList(account).map(identity => identity.address)].map(value => bareAddress(value ?? '').toLowerCase()).filter(Boolean))];
|
|
539
|
+
async function setVacation(userId, accountId, input = {}) {
|
|
540
|
+
return withStoreLock(async () => {
|
|
541
|
+
const all = await readAll();
|
|
542
|
+
const index = all.findIndex(account => account.id === accountId && account.userId === userId);
|
|
543
|
+
if (index === -1) return null;
|
|
544
|
+
const account = all[index], bodyHtml = sanitiseSignature(input.bodyHtml ?? '');
|
|
545
|
+
const vacation = normaliseVacation({vacation: {...input, bodyHtml, bodyText: input.bodyText ?? signatureToText(bodyHtml)}});
|
|
546
|
+
const newlyEnabled = vacation.enabled && (!normaliseVacation(account).enabled || !account.vacationEpoch);
|
|
547
|
+
all[index] = {...account, vacation, vacationRevision: randomUUID(),
|
|
548
|
+
...(newlyEnabled ? {vacationEpoch: randomUUID(), vacationFrom: new Date().toISOString()} : {}), updatedAt: new Date().toISOString()};
|
|
549
|
+
await writeAll(all); return all[index];
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
const snoozeFolder = account => String(account?.snoozeFolder ?? '').trim() || 'Snoozed';
|
|
554
|
+
const undoSendSeconds = account => normaliseUndoSeconds(account?.undoSendSeconds);
|
|
555
|
+
async function setSendingOptions(userId, accountId, options = {}) {
|
|
556
|
+
return withStoreLock(async () => {
|
|
557
|
+
const all = await readAll();
|
|
558
|
+
const index = all.findIndex(account => account.id === accountId && account.userId === userId);
|
|
559
|
+
if (index === -1) return null;
|
|
560
|
+
all[index] = {...all[index],
|
|
561
|
+
...(options.undoSendSeconds !== undefined ? {undoSendSeconds: normaliseUndoSeconds(options.undoSendSeconds)} : {}),
|
|
562
|
+
...(options.snoozeFolder !== undefined ? {snoozeFolder: String(options.snoozeFolder ?? '').trim() || null} : {}),
|
|
563
|
+
...(Array.isArray(options.ruleFolders) ? {ruleFolders: [...new Set(options.ruleFolders.filter(path => typeof path === 'string' && path.trim()))]} : {}),
|
|
564
|
+
updatedAt: new Date().toISOString()};
|
|
565
|
+
await writeAll(all);
|
|
566
|
+
return all[index];
|
|
567
|
+
});
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
const rulesToPublic = account => normaliseRules(account);
|
|
571
|
+
const ruleGroupsToPublic = account => normaliseRuleGroups({ruleGroups: account?.ruleGroups, rules: normaliseRules(account)});
|
|
572
|
+
const rulesToRun = account => rulesInEffect(rulesToPublic(account), ruleGroupsToPublic(account));
|
|
573
|
+
const ruleFolders = account => Array.isArray(account?.ruleFolders) ? account.ruleFolders : ['INBOX'];
|
|
574
|
+
|
|
575
|
+
/** Rules, groups and source folders commit together; an older caller preserves omitted settings. */
|
|
576
|
+
async function setRules(userId, accountId, rules, groups = undefined, folders = undefined) {
|
|
577
|
+
return withStoreLock(async () => {
|
|
578
|
+
const all = await readAll();
|
|
579
|
+
const index = all.findIndex(account => account.id === accountId && account.userId === userId);
|
|
580
|
+
if (index === -1) return null;
|
|
581
|
+
const saved = normaliseRules({rules});
|
|
582
|
+
all[index] = {
|
|
583
|
+
...all[index], rules: saved,
|
|
584
|
+
...(Array.isArray(groups) ? {ruleGroups: normaliseRuleGroups({ruleGroups: groups, rules: saved})} : {}),
|
|
585
|
+
...(Array.isArray(folders) ? {ruleFolders: [...new Set(folders)]} : {}),
|
|
586
|
+
rulesFrom: all[index].rulesFrom ?? new Date().toISOString(), rulesRevision: randomUUID(),
|
|
587
|
+
updatedAt: new Date().toISOString()
|
|
588
|
+
};
|
|
589
|
+
await writeAll(all);
|
|
590
|
+
return all[index];
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/** Rewrite persisted references after a confirmed server folder change. */
|
|
595
|
+
async function rewriteFolderPaths(userId, accountId, changes) {
|
|
596
|
+
return withStoreLock(async () => {
|
|
597
|
+
const all = await readAll();
|
|
598
|
+
const index = all.findIndex(account => account.id === accountId && account.userId === userId);
|
|
599
|
+
if (index === -1) return null;
|
|
600
|
+
const mapping = new Map(changes.filter(item => typeof item.from === 'string').map(item => [item.from, item.to]));
|
|
601
|
+
const rewrite = path => mapping.has(path) ? mapping.get(path) : path;
|
|
602
|
+
const account = {...all[index]};
|
|
603
|
+
if (Array.isArray(account.syncFolders)) account.syncFolders = [...new Set(account.syncFolders.map(rewrite).filter(path => path !== null))];
|
|
604
|
+
if (Array.isArray(account.ruleFolders)) account.ruleFolders = [...new Set(account.ruleFolders.map(rewrite).filter(path => path !== null))];
|
|
605
|
+
if (Array.isArray(account.rules)) account.rules = account.rules.map(rule => {
|
|
606
|
+
const missing = (rule.actions ?? []).some(action => mapping.has(action.folder) && rewrite(action.folder) === null);
|
|
607
|
+
return {...rule, ...(missing ? {enabled: false} : {}), actions: (rule.actions ?? []).map(action => ({...action, folder: rewrite(action.folder) ?? action.folder}))};
|
|
608
|
+
});
|
|
609
|
+
if (mapping.has(snoozeFolder(account))) account.snoozeFolder = rewrite(snoozeFolder(account));
|
|
610
|
+
if (account.smtp?.sentFolder && mapping.has(account.smtp.sentFolder)) account.smtp = {...account.smtp, sentFolder: rewrite(account.smtp.sentFolder)};
|
|
611
|
+
all[index] = {...account, updatedAt: new Date().toISOString()};
|
|
612
|
+
await writeAll(all);
|
|
613
|
+
return all[index];
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
return {sieveToPublic, setSieve, vacationToPublic, sendingAddresses, setVacation, snoozeFolder, undoSendSeconds, setSendingOptions, rulesToPublic, ruleGroupsToPublic, rulesToRun, ruleFolders, setRules, rewriteFolderPaths, setIdentities, setTemplates, listAllAccounts, setPollInterval, setSyncFolders, isOAuth, validate, listAccounts, setDefaultAccount, getAccount, saveAccount, deleteAccount, deleteAccountsForUser, pruneOrphans, toConnection, setSmtp, toSmtpConnection, smtpToPublic, trustImageSender, untrustImageSender, setRememberImageSenders, sendersImagesTrusted};
|
|
617
|
+
}
|