domma-cms 1.28.4 → 1.29.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/CHANGELOG.md +5 -0
- package/admin/js/lib/contacts-arrange.js +1 -1
- package/docs/DommaFeatures.md +5 -3
- 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
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
// Source: @domma-mail/core 0.18.0, packages/core/src/mail/imapPoolRuntime.js.
|
|
2
|
+
// Update with domma-mail/scripts/sync-cms.mjs; edit the shared source.
|
|
3
|
+
/**
|
|
4
|
+
* IMAP connection pool for the Mail Reader plugin.
|
|
5
|
+
*
|
|
6
|
+
* IMAP is unlike anything else the CMS talks to: the connection is long-lived,
|
|
7
|
+
* stateful, and handles exactly one command at a time. Opening a fresh
|
|
8
|
+
* connection per request would mean a TLS handshake and a LOGIN for every
|
|
9
|
+
* click, and most mail servers rate-limit that fairly aggressively.
|
|
10
|
+
*
|
|
11
|
+
* So connections are cached per user and reaped when idle. All mailbox access
|
|
12
|
+
* goes through `withMailbox()`, which takes ImapFlow's own mailbox lock, so
|
|
13
|
+
* two overlapping requests from the same user queue rather than corrupting
|
|
14
|
+
* each other's command stream.
|
|
15
|
+
*
|
|
16
|
+
* ## Read-only, twice over
|
|
17
|
+
*
|
|
18
|
+
* Every mailbox is opened `{readOnly: true}` and ImapFlow fetches with
|
|
19
|
+
* `BODY.PEEK[]` (see its commands/fetch.js). Either alone would stop the
|
|
20
|
+
* server setting `\Seen` as a side effect of reading a message; phase 1 has
|
|
21
|
+
* both, because a reader that silently marks a user's mail as read on their
|
|
22
|
+
* phone is worse than no reader at all. Do not relax this without making
|
|
23
|
+
* mark-as-read a deliberate, user-visible action.
|
|
24
|
+
*
|
|
25
|
+
* **Single-process only.** The pool is module state, exactly like the messages
|
|
26
|
+
* plugin's SSE registry. Running the site across workers would give each
|
|
27
|
+
* worker its own connections and blow through per-user connection limits.
|
|
28
|
+
*
|
|
29
|
+
* @module mail-reader/services/imapPool
|
|
30
|
+
*/
|
|
31
|
+
import {ImapFlow} from 'imapflow';
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Identify a connection's target. A change to any of these means the cached
|
|
35
|
+
* connection is for a different account and must be thrown away.
|
|
36
|
+
*
|
|
37
|
+
* @param {object} connection
|
|
38
|
+
* @returns {string}
|
|
39
|
+
*/
|
|
40
|
+
function fingerprint(connection) {
|
|
41
|
+
return [connection.host, connection.port, connection.secure, connection.auth.user].join('|');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Create an IMAP connection pool.
|
|
46
|
+
*
|
|
47
|
+
* @param {{connectionIdleMs: number, maxConnections: number, connectionTimeoutMs: number, allowInsecureTLS: boolean}} config
|
|
48
|
+
* @returns {object} pool API
|
|
49
|
+
*/
|
|
50
|
+
export function createImapPool(config, {pinMailHost, ImapClient = ImapFlow} = {}) {
|
|
51
|
+
if (typeof pinMailHost !== 'function') throw new Error('A mail host policy is required.');
|
|
52
|
+
/** @type {Map<string, {client: ImapFlow, fingerprint: string, lastUsed: number}>} */
|
|
53
|
+
const pool = new Map();
|
|
54
|
+
|
|
55
|
+
/** In-flight connects, so two parallel requests do not open two connections. */
|
|
56
|
+
const pending = new Map();
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Build the ImapFlow options for a connection.
|
|
60
|
+
*
|
|
61
|
+
* @param {object} connection
|
|
62
|
+
* @returns {object}
|
|
63
|
+
*/
|
|
64
|
+
function clientOptions(connection) {
|
|
65
|
+
return {
|
|
66
|
+
host: connection.host,
|
|
67
|
+
port: connection.port,
|
|
68
|
+
secure: connection.secure,
|
|
69
|
+
auth: connection.auth,
|
|
70
|
+
logger: false,
|
|
71
|
+
// A dead mail server should fail the request, not hang the admin.
|
|
72
|
+
greetingTimeout: config.connectionTimeoutMs,
|
|
73
|
+
socketTimeout: config.connectionTimeoutMs,
|
|
74
|
+
connectionTimeout: config.connectionTimeoutMs,
|
|
75
|
+
tls: {rejectUnauthorized: !config.allowInsecureTLS}
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Open and sign in one client, renewing an OAuth2 token on the way.
|
|
81
|
+
*
|
|
82
|
+
* A password connection is a single attempt, as it always was. A signed-in
|
|
83
|
+
* (OAuth2) one carries `refresh` from `toConnection()`: the stored token is
|
|
84
|
+
* taken (renewed first when under a minute is left), and renewed once more -
|
|
85
|
+
* forced - when the server refuses it anyway (revoked early, a clock out
|
|
86
|
+
* of step). Never more than once: a second refusal is the answer.
|
|
87
|
+
*
|
|
88
|
+
* @param {object} connection
|
|
89
|
+
* @param {(client: ImapFlow) => void} [prepare] - listeners to add before connect
|
|
90
|
+
* @returns {Promise<ImapFlow>}
|
|
91
|
+
*/
|
|
92
|
+
async function connectClient(connection, prepare = () => {}) {
|
|
93
|
+
let auth = connection.auth;
|
|
94
|
+
const canRenew = typeof connection.refresh === 'function';
|
|
95
|
+
if (canRenew) {
|
|
96
|
+
// Asked every time, not only near expiry: it reads the STORED
|
|
97
|
+
// sign-in (no network while the token is good), so a connection
|
|
98
|
+
// object built a while ago neither presents a token another
|
|
99
|
+
// request has since replaced, nor tries one already known dead.
|
|
100
|
+
const r = await connection.refresh(false);
|
|
101
|
+
auth = {user: auth.user, accessToken: r.accessToken};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Checked here, at every connect, and connected to the checked address
|
|
105
|
+
// (the name kept for TLS) - see hostGuard.js pinMailHost.
|
|
106
|
+
const pinned = await pinMailHost(connection.host, connection.port, {kind: 'imap', allowPrivate: config.allowPrivateMailHosts === true});
|
|
107
|
+
|
|
108
|
+
const attempt = async (withAuth) => {
|
|
109
|
+
const client = new ImapClient({
|
|
110
|
+
...clientOptions({...connection, auth: withAuth}),
|
|
111
|
+
host: pinned.address,
|
|
112
|
+
...(pinned.servername && {servername: pinned.servername})
|
|
113
|
+
});
|
|
114
|
+
prepare(client);
|
|
115
|
+
try {
|
|
116
|
+
await client.connect();
|
|
117
|
+
return client;
|
|
118
|
+
} catch (err) {
|
|
119
|
+
try { client.close(); } catch { /* already down */ }
|
|
120
|
+
throw err;
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
try {
|
|
125
|
+
return await attempt(auth);
|
|
126
|
+
} catch (err) {
|
|
127
|
+
if (!canRenew || !err?.authenticationFailed) throw err;
|
|
128
|
+
const r = await connection.refresh(true);
|
|
129
|
+
return attempt({user: auth.user, accessToken: r.accessToken});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Drop a pooled connection, closing it best-effort.
|
|
135
|
+
*
|
|
136
|
+
* @param {string} userId
|
|
137
|
+
* @returns {void}
|
|
138
|
+
*/
|
|
139
|
+
function evict(userId) {
|
|
140
|
+
const entry = pool.get(userId);
|
|
141
|
+
if (!entry) return;
|
|
142
|
+
pool.delete(userId);
|
|
143
|
+
entry.client.logout().catch(() => entry.client.close());
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Get a live, authenticated client for a user.
|
|
148
|
+
*
|
|
149
|
+
* @param {string} userId
|
|
150
|
+
* @param {object} connection
|
|
151
|
+
* @returns {Promise<ImapFlow>}
|
|
152
|
+
*/
|
|
153
|
+
async function acquire(userId, connection) {
|
|
154
|
+
const fp = fingerprint(connection);
|
|
155
|
+
const existing = pool.get(userId);
|
|
156
|
+
|
|
157
|
+
if (existing && existing.fingerprint === fp && existing.client.usable) {
|
|
158
|
+
existing.lastUsed = Date.now();
|
|
159
|
+
return existing.client;
|
|
160
|
+
}
|
|
161
|
+
// Stale, or the account details changed under it.
|
|
162
|
+
if (existing) evict(userId);
|
|
163
|
+
|
|
164
|
+
const inFlight = pending.get(userId);
|
|
165
|
+
if (inFlight) return inFlight;
|
|
166
|
+
|
|
167
|
+
if (pool.size >= config.maxConnections) {
|
|
168
|
+
// Evict the least recently used rather than refusing the request -
|
|
169
|
+
// the cap exists to bound resource use, not to lock people out.
|
|
170
|
+
const oldest = [...pool.entries()].sort((a, b) => a[1].lastUsed - b[1].lastUsed)[0];
|
|
171
|
+
if (oldest) evict(oldest[0]);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const connect = (async () => {
|
|
175
|
+
const client = await connectClient(connection, (c) => {
|
|
176
|
+
// ImapFlow emits 'error' on an already-failed connection; without a
|
|
177
|
+
// listener that becomes an unhandled event and takes the process out.
|
|
178
|
+
c.on('error', () => evict(userId));
|
|
179
|
+
c.on('close', () => {
|
|
180
|
+
if (pool.get(userId)?.client === c) pool.delete(userId);
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
pool.set(userId, {client, fingerprint: fp, lastUsed: Date.now()});
|
|
184
|
+
return client;
|
|
185
|
+
})();
|
|
186
|
+
|
|
187
|
+
pending.set(userId, connect);
|
|
188
|
+
try {
|
|
189
|
+
return await connect;
|
|
190
|
+
} finally {
|
|
191
|
+
pending.delete(userId);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Run a function against a user's connection, no mailbox selected.
|
|
197
|
+
*
|
|
198
|
+
* @param {string} userId
|
|
199
|
+
* @param {object} connection
|
|
200
|
+
* @param {(client: ImapFlow) => Promise<any>} fn
|
|
201
|
+
* @returns {Promise<any>}
|
|
202
|
+
*/
|
|
203
|
+
async function withClient(userId, connection, fn) {
|
|
204
|
+
const client = await acquire(userId, connection);
|
|
205
|
+
try {
|
|
206
|
+
const result = await fn(client);
|
|
207
|
+
const entry = pool.get(userId);
|
|
208
|
+
if (entry) entry.lastUsed = Date.now();
|
|
209
|
+
return result;
|
|
210
|
+
} catch (err) {
|
|
211
|
+
evict(userId);
|
|
212
|
+
throw err;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Run a function with a mailbox open and locked.
|
|
218
|
+
*
|
|
219
|
+
* Read-only unless asked otherwise, and the asking is deliberate: the free
|
|
220
|
+
* reader's entire guarantee is that looking at mail never changes it, and
|
|
221
|
+
* a default of read-write would quietly break that the first time a shared
|
|
222
|
+
* helper forgot the option. Only the Pro edition's write operations pass
|
|
223
|
+
* `writable`, and only for the one call that needs it.
|
|
224
|
+
*
|
|
225
|
+
* @param {string} userId
|
|
226
|
+
* @param {object} connection
|
|
227
|
+
* @param {string} mailbox
|
|
228
|
+
* @param {(client: ImapFlow, mailbox: object) => Promise<any>} fn
|
|
229
|
+
* @param {{writable?: boolean}} [options]
|
|
230
|
+
* @returns {Promise<any>}
|
|
231
|
+
*/
|
|
232
|
+
async function withMailbox(userId, connection, mailbox, fn, {writable = false} = {}) {
|
|
233
|
+
return withClient(userId, connection, async (client) => {
|
|
234
|
+
const lock = await client.getMailboxLock(mailbox, {readOnly: !writable});
|
|
235
|
+
try {
|
|
236
|
+
return await fn(client, client.mailbox);
|
|
237
|
+
} finally {
|
|
238
|
+
lock.release();
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* One-shot connect used by the "Test connection" button.
|
|
245
|
+
*
|
|
246
|
+
* Deliberately not pooled: it runs against details that have not been
|
|
247
|
+
* saved yet, and a failed attempt should leave nothing behind.
|
|
248
|
+
*
|
|
249
|
+
* @param {object} connection
|
|
250
|
+
* @returns {Promise<{ok: true, greeting: string|null} | {ok: false, error: string}>}
|
|
251
|
+
*/
|
|
252
|
+
async function test(connection) {
|
|
253
|
+
let client = null;
|
|
254
|
+
try {
|
|
255
|
+
client = await connectClient(connection, c => c.on('error', () => {}));
|
|
256
|
+
const greeting = client.serverInfo?.greeting ?? null;
|
|
257
|
+
await client.logout();
|
|
258
|
+
return {ok: true, greeting};
|
|
259
|
+
} catch (err) {
|
|
260
|
+
try {
|
|
261
|
+
client?.close();
|
|
262
|
+
} catch {
|
|
263
|
+
// Already down - nothing to close.
|
|
264
|
+
}
|
|
265
|
+
return {ok: false, error: err?.needsReconnect ? err.message : describeError(err, connection)};
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Close a specific user's connection - used when their account changes or
|
|
271
|
+
* is deleted, so the next request does not reuse the old credentials.
|
|
272
|
+
*
|
|
273
|
+
* @param {string} userId
|
|
274
|
+
* @returns {void}
|
|
275
|
+
*/
|
|
276
|
+
function release(userId) {
|
|
277
|
+
evict(userId);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Close every connection a user holds, whatever mailbox it belongs to.
|
|
282
|
+
*
|
|
283
|
+
* Keys are `userId:accountId`, so this is a prefix sweep. Used when a user
|
|
284
|
+
* is deleted - their sockets should not outlive their account either.
|
|
285
|
+
*
|
|
286
|
+
* @param {string} userId
|
|
287
|
+
* @returns {void}
|
|
288
|
+
*/
|
|
289
|
+
function releaseUser(userId) {
|
|
290
|
+
const prefix = `${userId}:`;
|
|
291
|
+
for (const key of [...pool.keys()]) {
|
|
292
|
+
if (key === userId || key.startsWith(prefix)) evict(key);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Close everything. Called on plugin disable and server shutdown.
|
|
298
|
+
*
|
|
299
|
+
* @returns {void}
|
|
300
|
+
*/
|
|
301
|
+
function closeAll() {
|
|
302
|
+
clearInterval(reaper);
|
|
303
|
+
for (const userId of [...pool.keys()]) evict(userId);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const reaper = setInterval(() => {
|
|
307
|
+
const cutoff = Date.now() - config.connectionIdleMs;
|
|
308
|
+
for (const [userId, entry] of pool) {
|
|
309
|
+
if (entry.lastUsed < cutoff) evict(userId);
|
|
310
|
+
}
|
|
311
|
+
}, Math.max(30_000, Math.floor(config.connectionIdleMs / 2)));
|
|
312
|
+
// Never hold the process open on account of an idle mail connection.
|
|
313
|
+
reaper.unref?.();
|
|
314
|
+
|
|
315
|
+
return {withClient, withMailbox, test, release, releaseUser, closeAll, connectClient,
|
|
316
|
+
// What send.js passes to hostGuard.js pinMailHost for this plugin's outgoing servers.
|
|
317
|
+
mailHostOptions: {allowPrivate: config.allowPrivateMailHosts === true},
|
|
318
|
+
get size() {
|
|
319
|
+
return pool.size;
|
|
320
|
+
}};
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Turn an IMAP/SMTP/network error into something worth showing a user.
|
|
325
|
+
*
|
|
326
|
+
* ImapFlow's own messages are decent but its auth failures surface as raw
|
|
327
|
+
* server responses, which vary by server and read like a stack trace. TLS
|
|
328
|
+
* failures are worse: OpenSSL's text ("…:tls_validate_record_header:wrong
|
|
329
|
+
* version number:…") names nothing the user can change. nodemailer also wraps
|
|
330
|
+
* every socket error as `ESOCKET`, dropping the TLS code, so those are
|
|
331
|
+
* recognised by `reason`/message as well as by code.
|
|
332
|
+
*
|
|
333
|
+
* @param {Error & {code?: string, reason?: string, authenticationFailed?: boolean}} err
|
|
334
|
+
* @param {{host?: string, port?: number, secure?: boolean}} [connection] - what was tried, so the message can name it
|
|
335
|
+
* @returns {string}
|
|
336
|
+
*/
|
|
337
|
+
export function describeError(err, connection = {}) {
|
|
338
|
+
if (!err) return 'Unknown error.';
|
|
339
|
+
if (err.needsReconnect || err.mailHostRefused) return err.message;
|
|
340
|
+
// A signed-in (OAuth2) mailbox has no password to blame.
|
|
341
|
+
if ((err.authenticationFailed || err.code === 'EAUTH' || err.code === 'EOAUTH2') && (connection.auth?.accessToken !== undefined || connection.auth?.type === 'OAuth2')) {
|
|
342
|
+
return 'The mail server refused the sign-in. If it keeps happening, Reconnect the mailbox.';
|
|
343
|
+
}
|
|
344
|
+
if (err.authenticationFailed || err.code === 'EAUTH') return 'The mail server rejected that username and password.';
|
|
345
|
+
|
|
346
|
+
const host = connection.host || 'The mail server';
|
|
347
|
+
const port = connection.port ? `Port ${connection.port}` : 'That port';
|
|
348
|
+
const text = `${err.reason || ''} ${err.message || ''}`;
|
|
349
|
+
|
|
350
|
+
// Implicit TLS against a port that starts in plain text (587, 143, 25):
|
|
351
|
+
// the "server hello" is a greeting line, which OpenSSL reads as a bad version.
|
|
352
|
+
if (err.code === 'ERR_SSL_WRONG_VERSION_NUMBER' || /wrong version number/i.test(text)) {
|
|
353
|
+
return connection.secure
|
|
354
|
+
? `${port} does not start with TLS, so Implicit TLS must be off for it - ports 587 and 143 switch to TLS with STARTTLS. Untick Implicit TLS, or use the implicit-TLS port (465 to send, 993 to read).`
|
|
355
|
+
: `${port} answered in a different TLS mode from the one chosen. Check the port and the Implicit TLS setting.`;
|
|
356
|
+
}
|
|
357
|
+
if (['DEPTH_ZERO_SELF_SIGNED_CERT', 'SELF_SIGNED_CERT_IN_CHAIN'].includes(err.code) || /self[- ]signed certificate/i.test(text)) {
|
|
358
|
+
return `${host === 'The mail server' ? 'The mail server’s' : `${host}’s`} TLS certificate is self-signed, so the server cannot prove who it is. `
|
|
359
|
+
+ `Install a trusted certificate (Let’s Encrypt, for example) that covers this hostname on the mail server. `
|
|
360
|
+
+ `For a server you control, the plugin’s allowInsecureTLS setting accepts it instead.`;
|
|
361
|
+
}
|
|
362
|
+
if (err.code === 'ERR_TLS_CERT_ALTNAME_INVALID' || /altnames|does not match certificate/i.test(text)) {
|
|
363
|
+
return `The mail server’s TLS certificate does not cover ${connection.host || 'that hostname'}. Use the hostname the certificate was issued for, or reissue it to include this one.`;
|
|
364
|
+
}
|
|
365
|
+
if (err.code === 'CERT_HAS_EXPIRED' || /certificate has expired/i.test(text)) {
|
|
366
|
+
return 'The mail server’s TLS certificate has expired. It needs renewing on the server.';
|
|
367
|
+
}
|
|
368
|
+
if (['UNABLE_TO_VERIFY_LEAF_SIGNATURE', 'UNABLE_TO_GET_ISSUER_CERT_LOCALLY'].includes(err.code) || /unable to (verify the first certificate|get local issuer)/i.test(text)) {
|
|
369
|
+
return 'The mail server’s TLS certificate could not be verified - it is missing its intermediate certificate, or was issued by an authority this server does not trust.';
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// nodemailer reports these as ESOCKET/EDNS; the system code survives only in the message.
|
|
373
|
+
const netCode = text.match(/\b(ENOTFOUND|EAI_AGAIN|ECONNREFUSED|ETIMEDOUT|ECONNRESET)\b/)?.[1] ?? err.code;
|
|
374
|
+
switch (netCode) {
|
|
375
|
+
case 'ENOTFOUND':
|
|
376
|
+
case 'EAI_AGAIN':
|
|
377
|
+
return 'That mail server hostname could not be resolved.';
|
|
378
|
+
case 'ECONNREFUSED':
|
|
379
|
+
return 'The mail server refused the connection on that port.';
|
|
380
|
+
case 'ETIMEDOUT':
|
|
381
|
+
case 'ECONNRESET':
|
|
382
|
+
return 'The mail server did not respond in time.';
|
|
383
|
+
default:
|
|
384
|
+
// Words that are ours, or a mail server's answer to a command after
|
|
385
|
+
// it greeted and (for IMAP) signed us in - those say nothing about
|
|
386
|
+
// what else listens on a host and port.
|
|
387
|
+
if (err.name === 'MailKeyError' || err.responseStatus === 'NO' || err.responseStatus === 'BAD'
|
|
388
|
+
|| err.code === 'EENVELOPE' || err.code === 'EMESSAGE') {
|
|
389
|
+
return err.message || 'The mail server connection failed.';
|
|
390
|
+
}
|
|
391
|
+
// Anything else stays out of the browser: a greeting, a banner or a
|
|
392
|
+
// socket error is exactly what a port scan reads. The log has it.
|
|
393
|
+
console.warn(`[mail] connection to ${connection.host || 'the mail server'}:${connection.port || '?'} failed${err.code ? ` (${err.code})` : ''}: ${err.message || err}`);
|
|
394
|
+
return 'The mail server connection failed. Check the server settings.';
|
|
395
|
+
}
|
|
396
|
+
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// Source: @domma-mail/core 0.18.0, packages/core/src/mail/message-action.js.
|
|
2
|
+
// Update with domma-mail/scripts/sync-cms.mjs; edit the shared source.
|
|
1
3
|
/** Remember replies and forwards on the original message after delivery. */
|
|
2
4
|
export const SOURCE_HEADER = 'X-Domma-Message-Source';
|
|
3
5
|
|
|
@@ -1,111 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
* S/MIME. An edition that does (Email Pro) registers a provider here at boot,
|
|
6
|
-
* and every path that sends - the send route, the scheduler behind undo and
|
|
7
|
-
* send-later - or reads a message body goes through it. A registry rather
|
|
8
|
-
* than an option threaded through each caller, because the scheduler is core
|
|
9
|
-
* code started by the edition and would otherwise need the provider passed
|
|
10
|
-
* down three layers to reach the one call that uses it.
|
|
11
|
-
*
|
|
12
|
-
* The provider:
|
|
13
|
-
* protect({raw, draft, account, userId}) -> Promise<Buffer>
|
|
14
|
-
* the message as it should leave and be filed; called only when the
|
|
15
|
-
* draft asks for `sign` or `encrypt`. Throws a plain Error to refuse.
|
|
16
|
-
* protectDraft({raw, draft, account, userId}) -> Promise<Buffer> (optional)
|
|
17
|
-
* a draft of a message to be encrypted, encrypted to the sender alone.
|
|
18
|
-
* unwrap({raw, account, userId}) -> Promise<{raw: Buffer, security: object|null}>
|
|
19
|
-
* the message as it should be read; never throws for a bad message,
|
|
20
|
-
* it says so in `security.problem` and hands the original back.
|
|
21
|
-
*
|
|
22
|
-
* @module _lib/mail/messageSecurity
|
|
23
|
-
*/
|
|
24
|
-
import {simpleParser} from 'mailparser';
|
|
25
|
-
|
|
26
|
-
let provider = null;
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Register (or, with null, remove) the provider. One at a time: the editions
|
|
30
|
-
* never run together.
|
|
31
|
-
*
|
|
32
|
-
* @param {{protect: Function, unwrap: Function}|null} next
|
|
33
|
-
* @returns {void}
|
|
34
|
-
*/
|
|
35
|
-
export function setMessageSecurity(next) {
|
|
36
|
-
provider = next && typeof next.protect === 'function' && typeof next.unwrap === 'function' ? next : null;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** @returns {boolean} whether a message can be signed or encrypted here */
|
|
40
|
-
export function canProtect() {
|
|
41
|
-
return provider !== null;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* The message as it should leave. A draft that asks for neither comes back as
|
|
46
|
-
* it was; one that asks with no provider registered is refused, because
|
|
47
|
-
* sending it unprotected is the one outcome the sender ruled out.
|
|
48
|
-
*
|
|
49
|
-
* @param {{raw: Buffer, draft: object, account?: object|null, userId?: string|null}} input
|
|
50
|
-
* @returns {Promise<Buffer>}
|
|
51
|
-
*/
|
|
52
|
-
export async function protectRaw(input) {
|
|
53
|
-
const wanted = input.draft?.sign === true || input.draft?.encrypt === true;
|
|
54
|
-
if (!wanted) return input.raw;
|
|
55
|
-
if (!provider) {
|
|
56
|
-
throw new Error('Signing and encryption need Email Pro with a certificate set up. The message was not sent.');
|
|
57
|
-
}
|
|
58
|
-
return provider.protect(input);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* A draft as it should be stored: encrypted to the sender alone when the
|
|
63
|
-
* message is to be encrypted and the provider can (`protectDraft`), so the
|
|
64
|
-
* Drafts folder - which every client syncs - does not hold the plain text of
|
|
65
|
-
* a message meant to be secret. Otherwise, and on any failure, as it was: a
|
|
66
|
-
* draft that cannot be saved is worse, and the send itself still refuses to
|
|
67
|
-
* go out unencrypted.
|
|
68
|
-
*
|
|
69
|
-
* @param {{raw: Buffer, draft: object, account?: object|null, userId?: string|null}} input
|
|
70
|
-
* @returns {Promise<Buffer>}
|
|
71
|
-
*/
|
|
72
|
-
export async function protectDraftRaw(input) {
|
|
73
|
-
if (input.draft?.encrypt !== true || typeof provider?.protectDraft !== 'function') return input.raw;
|
|
74
|
-
try {
|
|
75
|
-
return (await provider.protectDraft(input)) ?? input.raw;
|
|
76
|
-
} catch {
|
|
77
|
-
return input.raw;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* The message as it should be read.
|
|
83
|
-
*
|
|
84
|
-
* @param {{raw: Buffer, account?: object|null, userId?: string|null}} input
|
|
85
|
-
* @returns {Promise<{raw: Buffer, security: object|null}>}
|
|
86
|
-
*/
|
|
87
|
-
export async function unwrapRaw(input) {
|
|
88
|
-
if (!provider) return {raw: input.raw, security: null};
|
|
89
|
-
try {
|
|
90
|
-
const result = await provider.unwrap(input);
|
|
91
|
-
return result?.raw ? result : {raw: input.raw, security: null};
|
|
92
|
-
} catch {
|
|
93
|
-
// A provider fault must never cost the reader the message itself.
|
|
94
|
-
return {raw: input.raw, security: null};
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Parse a message as it should be read: unwrapped first, then mailparser.
|
|
100
|
-
* Every route that shows, quotes or hands off a message's content goes
|
|
101
|
-
* through this, so an encrypted message reads the same in the pane, in a
|
|
102
|
-
* reply's quote and in its attachments.
|
|
103
|
-
*
|
|
104
|
-
* @param {Buffer} raw
|
|
105
|
-
* @param {{account?: object|null, userId?: string|null}} [context]
|
|
106
|
-
* @returns {Promise<{parsed: object, security: object|null}>}
|
|
107
|
-
*/
|
|
108
|
-
export async function parseReadable(raw, {account = null, userId = null} = {}) {
|
|
109
|
-
const {raw: readable, security} = await unwrapRaw({raw, account, userId});
|
|
110
|
-
return {parsed: await simpleParser(readable), security};
|
|
111
|
-
}
|
|
1
|
+
// Source: @domma-mail/core 0.18.0, packages/core/src/mail/messageSecurity.js.
|
|
2
|
+
// Update with domma-mail/scripts/sync-cms.mjs; edit the shared source.
|
|
3
|
+
import {createMessageSecurity} from './messageSecurityProvider.js';
|
|
4
|
+
export const {setMessageSecurity, canProtect, protectRaw, protectDraftRaw, unwrapRaw, parseReadable} = createMessageSecurity();
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// Source: @domma-mail/core 0.18.0, packages/core/src/mail/messageSecurityProvider.js.
|
|
2
|
+
// Update with domma-mail/scripts/sync-cms.mjs; edit the shared source.
|
|
3
|
+
/**
|
|
4
|
+
* Where signing and encryption plug into the mail core.
|
|
5
|
+
*
|
|
6
|
+
* The core builds, sends, files and reads raw messages; it does not know
|
|
7
|
+
* S/MIME. An edition that does (Email Pro) registers a provider here at boot,
|
|
8
|
+
* and every path that sends - the send route, the scheduler behind undo and
|
|
9
|
+
* send-later - or reads a message body goes through it. A registry rather
|
|
10
|
+
* than an option threaded through each caller, because the scheduler is core
|
|
11
|
+
* code started by the edition and would otherwise need the provider passed
|
|
12
|
+
* down three layers to reach the one call that uses it.
|
|
13
|
+
*
|
|
14
|
+
* The provider:
|
|
15
|
+
* protect({raw, draft, account, userId}) -> Promise<Buffer>
|
|
16
|
+
* the message as it should leave and be filed; called only when the
|
|
17
|
+
* draft asks for `sign` or `encrypt`. Throws a plain Error to refuse.
|
|
18
|
+
* protectDraft({raw, draft, account, userId}) -> Promise<Buffer> (optional)
|
|
19
|
+
* a draft of a message to be encrypted, encrypted to the sender alone.
|
|
20
|
+
* unwrap({raw, account, userId}) -> Promise<{raw: Buffer, security: object|null}>
|
|
21
|
+
* the message as it should be read; never throws for a bad message,
|
|
22
|
+
* it says so in `security.problem` and hands the original back.
|
|
23
|
+
*
|
|
24
|
+
* @module _lib/mail/messageSecurity
|
|
25
|
+
*/
|
|
26
|
+
import {simpleParser} from 'mailparser';
|
|
27
|
+
|
|
28
|
+
export function createMessageSecurity() {
|
|
29
|
+
let provider = null;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Register (or, with null, remove) the provider. One at a time: the editions
|
|
33
|
+
* never run together.
|
|
34
|
+
*
|
|
35
|
+
* @param {{protect: Function, unwrap: Function}|null} next
|
|
36
|
+
* @returns {void}
|
|
37
|
+
*/
|
|
38
|
+
function setMessageSecurity(next) {
|
|
39
|
+
provider = next && typeof next.protect === 'function' && typeof next.unwrap === 'function' ? next : null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** @returns {boolean} whether a message can be signed or encrypted here */
|
|
43
|
+
function canProtect() {
|
|
44
|
+
return provider !== null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The message as it should leave. A draft that asks for neither comes back as
|
|
49
|
+
* it was; one that asks with no provider registered is refused, because
|
|
50
|
+
* sending it unprotected is the one outcome the sender ruled out.
|
|
51
|
+
*
|
|
52
|
+
* @param {{raw: Buffer, draft: object, account?: object|null, userId?: string|null}} input
|
|
53
|
+
* @returns {Promise<Buffer>}
|
|
54
|
+
*/
|
|
55
|
+
async function protectRaw(input) {
|
|
56
|
+
const wanted = input.draft?.sign === true || input.draft?.encrypt === true;
|
|
57
|
+
if (!wanted) return input.raw;
|
|
58
|
+
if (!provider) {
|
|
59
|
+
throw new Error('Signing and encryption need Email Pro with a certificate set up. The message was not sent.');
|
|
60
|
+
}
|
|
61
|
+
return provider.protect(input);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* A draft as it should be stored: encrypted to the sender alone when the
|
|
66
|
+
* message is to be encrypted and the provider can (`protectDraft`), so the
|
|
67
|
+
* Drafts folder - which every client syncs - does not hold the plain text of
|
|
68
|
+
* a message meant to be secret. Otherwise, and on any failure, as it was: a
|
|
69
|
+
* draft that cannot be saved is worse, and the send itself still refuses to
|
|
70
|
+
* go out unencrypted.
|
|
71
|
+
*
|
|
72
|
+
* @param {{raw: Buffer, draft: object, account?: object|null, userId?: string|null}} input
|
|
73
|
+
* @returns {Promise<Buffer>}
|
|
74
|
+
*/
|
|
75
|
+
async function protectDraftRaw(input) {
|
|
76
|
+
if (input.draft?.encrypt !== true || typeof provider?.protectDraft !== 'function') return input.raw;
|
|
77
|
+
try {
|
|
78
|
+
return (await provider.protectDraft(input)) ?? input.raw;
|
|
79
|
+
} catch {
|
|
80
|
+
return input.raw;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The message as it should be read.
|
|
86
|
+
*
|
|
87
|
+
* @param {{raw: Buffer, account?: object|null, userId?: string|null}} input
|
|
88
|
+
* @returns {Promise<{raw: Buffer, security: object|null}>}
|
|
89
|
+
*/
|
|
90
|
+
async function unwrapRaw(input) {
|
|
91
|
+
if (!provider) return {raw: input.raw, security: null};
|
|
92
|
+
try {
|
|
93
|
+
const result = await provider.unwrap(input);
|
|
94
|
+
return result?.raw ? result : {raw: input.raw, security: null};
|
|
95
|
+
} catch {
|
|
96
|
+
// A provider fault must never cost the reader the message itself.
|
|
97
|
+
return {raw: input.raw, security: null};
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Parse a message as it should be read: unwrapped first, then mailparser.
|
|
103
|
+
* Every route that shows, quotes or hands off a message's content goes
|
|
104
|
+
* through this, so an encrypted message reads the same in the pane, in a
|
|
105
|
+
* reply's quote and in its attachments.
|
|
106
|
+
*
|
|
107
|
+
* @param {Buffer} raw
|
|
108
|
+
* @param {{account?: object|null, userId?: string|null}} [context]
|
|
109
|
+
* @returns {Promise<{parsed: object, security: object|null}>}
|
|
110
|
+
*/
|
|
111
|
+
async function parseReadable(raw, {account = null, userId = null} = {}) {
|
|
112
|
+
const {raw: readable, security} = await unwrapRaw({raw, account, userId});
|
|
113
|
+
return {parsed: await simpleParser(readable), security};
|
|
114
|
+
}
|
|
115
|
+
return {setMessageSecurity, canProtect, protectRaw, protectDraftRaw, unwrapRaw, parseReadable};
|
|
116
|
+
}
|