emailengine-app 2.74.1 → 2.74.2
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 +16 -0
- package/data/google-crawlers.json +1 -1
- package/lib/account.js +23 -45
- package/lib/api-routes/changes-routes.js +51 -0
- package/lib/api-routes/index.js +160 -0
- package/lib/consts.js +15 -0
- package/lib/email-client/gmail/send-request.js +69 -0
- package/lib/email-client/gmail-client.js +4 -41
- package/lib/email-client/imap/detach-client.js +27 -0
- package/lib/email-client/imap/listing-diff.js +165 -0
- package/lib/email-client/imap/mailbox.js +32 -13
- package/lib/email-client/imap/subconnection.js +2 -4
- package/lib/email-client/imap/sync-operations.js +5 -3
- package/lib/email-client/imap-client.js +50 -84
- package/lib/email-client/outlook/send-request.js +48 -0
- package/lib/email-client/outlook-client.js +5 -14
- package/lib/export.js +97 -21
- package/lib/imapproxy/imap-core/lib/imap-connection.js +1 -1
- package/lib/logger.js +14 -6
- package/lib/rate-limit.js +33 -11
- package/lib/response-stream.js +138 -0
- package/lib/sentry.js +8 -0
- package/lib/smtp-message-processor.js +131 -0
- package/lib/tools.js +28 -5
- package/lib/ui-routes/internals-routes.js +32 -0
- package/package.json +6 -6
- package/sbom.json +1 -1
- package/static/licenses.html +22 -12
- package/translations/messages.pot +35 -35
- package/views/internals/index.hbs +8 -0
- package/workers/api.js +20 -216
- package/workers/export.js +29 -15
- package/workers/imap.js +47 -10
- package/workers/smtp.js +50 -91
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Pure helpers behind IMAPClient.getCurrentListing().
|
|
4
|
+
//
|
|
5
|
+
// Every IMAP sync pass re-runs LIST and has to work out which folders are new, which changed
|
|
6
|
+
// shape, and which disappeared, then persist the result. That comparison is the part worth
|
|
7
|
+
// testing on its own: it is pure, it runs on every sync for every account, and getting it wrong
|
|
8
|
+
// either floods operators with spurious mailboxNew notifications or silently drops folders.
|
|
9
|
+
// The surrounding IO (the LIST call, clearing a deleted folder's index, the Redis write) stays
|
|
10
|
+
// in imap-client.js.
|
|
11
|
+
//
|
|
12
|
+
// Folder paths are compared through normalizePath(), which case-folds INBOX so a server that
|
|
13
|
+
// reports "Inbox" one pass and "INBOX" the next does not look like a delete plus an add.
|
|
14
|
+
|
|
15
|
+
const msgpack = require('msgpack5')();
|
|
16
|
+
const { normalizePath } = require('../../tools');
|
|
17
|
+
const { STORED_MAILBOX_FIELDS } = require('../../consts');
|
|
18
|
+
|
|
19
|
+
// The fields diffMailboxListing() compares, i.e. everything buildStoredListingObject() persists
|
|
20
|
+
// except the path, which is the hash key and therefore equal by construction. Derived from the
|
|
21
|
+
// stored field list so that persisting a new field can not silently leave it uncompared.
|
|
22
|
+
const COMPARED_MAILBOX_FIELDS = STORED_MAILBOX_FIELDS.filter(field => field !== 'path');
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Decodes one msgpack-encoded entry of the stored mailbox listing hash, or returns false if the
|
|
26
|
+
* value is unusable. Callers drop a false entry rather than failing: a single corrupt value must
|
|
27
|
+
* not take down the sync (or the mailboxes API response) for every other folder in the account.
|
|
28
|
+
*
|
|
29
|
+
* The shape check is not redundant with the try/catch. msgpack5 only throws on input it can not
|
|
30
|
+
* parse at all - a corrupt buffer whose first byte happens to be a valid type marker decodes to
|
|
31
|
+
* a plain number or string instead. Such an entry has no `path`, so downstream it would register
|
|
32
|
+
* as a phantom mailbox keyed `undefined` and (via Object.assign onto a primitive) surface as a
|
|
33
|
+
* boxed Number in the mailboxes API response.
|
|
34
|
+
*
|
|
35
|
+
* @param {Buffer} value - msgpack-encoded hash value
|
|
36
|
+
* @returns {Object|false} Decoded mailbox entry, or false when the value is missing or corrupt
|
|
37
|
+
*/
|
|
38
|
+
function decodeStoredMailboxEntry(value) {
|
|
39
|
+
let entry;
|
|
40
|
+
try {
|
|
41
|
+
entry = msgpack.decode(value);
|
|
42
|
+
} catch (err) {
|
|
43
|
+
// should not happen
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
return entry && typeof entry === 'object' && typeof entry.path === 'string' ? entry : false;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Decodes the msgpack-encoded mailbox listing hash read back from Redis, separating out the
|
|
51
|
+
* entries that did not decode.
|
|
52
|
+
*
|
|
53
|
+
* The corrupt paths are reported rather than just dropped because a dropped entry is invisible to
|
|
54
|
+
* every later step: it is not in the decoded listing, so it can never be matched against a server
|
|
55
|
+
* folder, so it can never register as deleted either. Without an explicit rewrite the bad field
|
|
56
|
+
* survives every sync pass forever while the mailboxes API keeps skipping that folder.
|
|
57
|
+
*
|
|
58
|
+
* @param {Object} storedListing - Redis hash (path -> msgpack Buffer), possibly null
|
|
59
|
+
* @returns {Object} { entries, corruptPaths } - decoded mailbox entries and the unusable fields
|
|
60
|
+
*/
|
|
61
|
+
function decodeStoredListing(storedListing) {
|
|
62
|
+
const entries = [];
|
|
63
|
+
const corruptPaths = [];
|
|
64
|
+
|
|
65
|
+
for (let path of Object.keys(storedListing || {})) {
|
|
66
|
+
let entry = decodeStoredMailboxEntry(storedListing[path]);
|
|
67
|
+
if (entry) {
|
|
68
|
+
entries.push(entry);
|
|
69
|
+
} else {
|
|
70
|
+
corruptPaths.push(path);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return { entries, corruptPaths };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Compares a fresh server listing against the stored one.
|
|
79
|
+
*
|
|
80
|
+
* Mutates the entries of `listing`: a folder with no stored counterpart is flagged `isNew`.
|
|
81
|
+
* That flag is the one-shot carrier that makes processListing() emit mailboxNew, so it has to
|
|
82
|
+
* ride on the returned listing rather than being reported separately.
|
|
83
|
+
*
|
|
84
|
+
* @param {Array} listing - Mailbox entries from the server (already filtered of \Noselect)
|
|
85
|
+
* @param {Array} storedListing - Decoded entries previously persisted for this account
|
|
86
|
+
* @returns {Object} { hasChanges, deletedEntries } - deletedEntries are stored folders the
|
|
87
|
+
* server no longer lists, whose message index the caller must clear
|
|
88
|
+
*/
|
|
89
|
+
function diffMailboxListing(listing, storedListing) {
|
|
90
|
+
let hasChanges = false;
|
|
91
|
+
|
|
92
|
+
// Map for O(1) lookups - a linear scan per folder is quadratic on accounts with thousands
|
|
93
|
+
// of folders, which is common on shared/public namespaces.
|
|
94
|
+
const storedListingMap = new Map();
|
|
95
|
+
for (const entry of storedListing) {
|
|
96
|
+
storedListingMap.set(normalizePath(entry.path), entry);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
for (let mailbox of listing) {
|
|
100
|
+
let existingMailbox = storedListingMap.get(normalizePath(mailbox.path));
|
|
101
|
+
if (!existingMailbox) {
|
|
102
|
+
// found new!
|
|
103
|
+
mailbox.isNew = true;
|
|
104
|
+
hasChanges = true;
|
|
105
|
+
} else {
|
|
106
|
+
// Every persisted field has to be compared, not just the structural ones. The
|
|
107
|
+
// mailboxes API answers from the stored hash (Account.getMailboxListing merges only
|
|
108
|
+
// the live status counters onto it), so a field that changed without setting
|
|
109
|
+
// hasChanges is never written back and the stale value is served until some
|
|
110
|
+
// unrelated folder happens to be created or deleted. Unsubscribing a folder moves
|
|
111
|
+
// `subscribed` alone, and modifyMailbox() explicitly refreshes the listing right
|
|
112
|
+
// afterwards expecting exactly that to be picked up.
|
|
113
|
+
for (let field of COMPARED_MAILBOX_FIELDS) {
|
|
114
|
+
if (existingMailbox[field] !== mailbox[field]) {
|
|
115
|
+
hasChanges = true;
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Check for deleted mailboxes
|
|
123
|
+
const listingPathSet = new Set(listing.map(mailbox => normalizePath(mailbox.path)));
|
|
124
|
+
const deletedEntries = storedListing.filter(entry => !listingPathSet.has(normalizePath(entry.path)));
|
|
125
|
+
if (deletedEntries.length) {
|
|
126
|
+
hasChanges = true;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return { hasChanges, deletedEntries };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Builds the Redis hash written back when the listing changed: normalized path -> msgpack of
|
|
134
|
+
* the persisted field subset.
|
|
135
|
+
*
|
|
136
|
+
* The accumulator has a null prototype because folder names are attacker-influenced strings and
|
|
137
|
+
* one of them is special on a plain object: assigning `listingObject['__proto__']` would invoke
|
|
138
|
+
* the Object.prototype setter instead of creating an own property, so Object.keys() (which is
|
|
139
|
+
* how ioredis serializes the hash) would silently omit that folder. It would then look unseen on
|
|
140
|
+
* every following sync: a fresh mailboxNew notification plus a full initial sync of the folder,
|
|
141
|
+
* every pass, forever.
|
|
142
|
+
*
|
|
143
|
+
* @param {Array} listing - Mailbox entries from the server
|
|
144
|
+
* @returns {Object} Hash suitable for HMSET
|
|
145
|
+
*/
|
|
146
|
+
function buildStoredListingObject(listing) {
|
|
147
|
+
const listingObject = Object.create(null);
|
|
148
|
+
listing.forEach(entry => {
|
|
149
|
+
let mailbox = {};
|
|
150
|
+
Object.keys(entry).forEach(key => {
|
|
151
|
+
if (STORED_MAILBOX_FIELDS.includes(key)) {
|
|
152
|
+
mailbox[key] = entry[key];
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
listingObject[normalizePath(entry.path)] = msgpack.encode(mailbox);
|
|
156
|
+
});
|
|
157
|
+
return listingObject;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
module.exports = {
|
|
161
|
+
decodeStoredMailboxEntry,
|
|
162
|
+
decodeStoredListing,
|
|
163
|
+
diffMailboxListing,
|
|
164
|
+
buildStoredListingObject
|
|
165
|
+
};
|
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
|
|
3
|
+
const {
|
|
4
|
+
serialize,
|
|
5
|
+
unserialize,
|
|
6
|
+
compareExisting,
|
|
7
|
+
normalizePath,
|
|
8
|
+
download,
|
|
9
|
+
filterEmptyObjectValues,
|
|
10
|
+
validUidValidity,
|
|
11
|
+
mailboxPathHash,
|
|
12
|
+
getMailboxStatusKey
|
|
13
|
+
} = require('../../tools');
|
|
5
14
|
const msgpack = require('msgpack5')();
|
|
6
15
|
const he = require('he');
|
|
7
16
|
const libmime = require('libmime');
|
|
@@ -29,7 +38,6 @@ const {
|
|
|
29
38
|
REDIS_PREFIX,
|
|
30
39
|
MAX_INLINE_ATTACHMENT_SIZE,
|
|
31
40
|
MAX_ALLOWED_DOWNLOAD_SIZE,
|
|
32
|
-
MAILBOX_HASH,
|
|
33
41
|
TRANSIENT_NETWORK_CODES,
|
|
34
42
|
PHANTOM_SELECT_FAIL_THRESHOLD,
|
|
35
43
|
PHANTOM_REPROBE_INTERVAL
|
|
@@ -88,7 +96,13 @@ class Mailbox {
|
|
|
88
96
|
this.previouslyConnected = false;
|
|
89
97
|
|
|
90
98
|
// Generate unique Redis key for this mailbox based on path hash
|
|
91
|
-
this.redisKey =
|
|
99
|
+
this.redisKey = mailboxPathHash(this.path);
|
|
100
|
+
|
|
101
|
+
// Built once, through the shared helper: Account.getMailboxListing() reads these counters
|
|
102
|
+
// back through the same helper and the two have to agree byte for byte, but the key is
|
|
103
|
+
// also read several times per folder on every sync pass and rebuilding it would re-run
|
|
104
|
+
// the SHA1 + BigInt + base36 derivation every time.
|
|
105
|
+
this.mailboxStatusKey = getMailboxStatusKey(this.connection.account, this.path);
|
|
92
106
|
|
|
93
107
|
this.runPartialSyncTimer = false; // Timer for delayed partial sync after EXISTS
|
|
94
108
|
|
|
@@ -367,7 +381,7 @@ class Mailbox {
|
|
|
367
381
|
* Gets Redis key for storing mailbox metadata (hash)
|
|
368
382
|
*/
|
|
369
383
|
getMailboxKey() {
|
|
370
|
-
return
|
|
384
|
+
return this.mailboxStatusKey;
|
|
371
385
|
}
|
|
372
386
|
|
|
373
387
|
/**
|
|
@@ -2600,11 +2614,22 @@ class Mailbox {
|
|
|
2600
2614
|
|
|
2601
2615
|
let messageData = await connectionClient.fetchOne(message.uid, fields, { uid: true });
|
|
2602
2616
|
|
|
2617
|
+
if (!messageData || !messageData.uid) {
|
|
2618
|
+
//TODO: support partial responses
|
|
2619
|
+
this.logger.debug({ msg: 'Partial FETCH response', code: 'partial_fetch', query: { range: message.uid, fields, opts: { uid: true } } });
|
|
2620
|
+
return false;
|
|
2621
|
+
}
|
|
2622
|
+
|
|
2603
2623
|
// Mark as seen if requested
|
|
2604
|
-
if (options.markAsSeen &&
|
|
2624
|
+
if (options.markAsSeen && !messageData.flags?.has('\\Seen')) {
|
|
2605
2625
|
try {
|
|
2606
2626
|
let res = await connectionClient.messageFlagsAdd(message.uid, ['\\Seen'], { uid: true });
|
|
2607
|
-
|
|
2627
|
+
// Mirror the flag locally so the returned message reflects it. A
|
|
2628
|
+
// caller-provided field list can leave FLAGS out and a server can
|
|
2629
|
+
// answer the FETCH without them - in that case there is nothing to
|
|
2630
|
+
// mirror into, and inventing a set here would report the message as
|
|
2631
|
+
// carrying \Seen and no other flag
|
|
2632
|
+
if (res && messageData.flags) {
|
|
2608
2633
|
messageData.flags.add('\\Seen');
|
|
2609
2634
|
}
|
|
2610
2635
|
} catch (err) {
|
|
@@ -2612,12 +2637,6 @@ class Mailbox {
|
|
|
2612
2637
|
}
|
|
2613
2638
|
}
|
|
2614
2639
|
|
|
2615
|
-
if (!messageData || !messageData.uid) {
|
|
2616
|
-
//TODO: support partial responses
|
|
2617
|
-
this.logger.debug({ msg: 'Partial FETCH response', code: 'partial_fetch', query: { range: message.uid, fields, opts: { uid: true } } });
|
|
2618
|
-
return false;
|
|
2619
|
-
}
|
|
2620
|
-
|
|
2621
2640
|
messageInfo = await this.getMessageInfo(messageData, true);
|
|
2622
2641
|
} finally {
|
|
2623
2642
|
if (lock) {
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
const { ImapFlow } = require('imapflow');
|
|
4
4
|
const { backOff } = require('exponential-backoff');
|
|
5
5
|
const { ReconnectBackoff } = require('../reconnect-backoff');
|
|
6
|
+
const { detachImapClient } = require('./detach-client');
|
|
6
7
|
const EventEmitter = require('events').EventEmitter;
|
|
7
8
|
|
|
8
9
|
const MAX_WAIT_DELAY = 450; //ms
|
|
@@ -178,10 +179,7 @@ class Subconnection extends EventEmitter {
|
|
|
178
179
|
let prevImapClient = this.imapClient;
|
|
179
180
|
prevImapClient.disabled = true;
|
|
180
181
|
try {
|
|
181
|
-
prevImapClient.
|
|
182
|
-
prevImapClient.once('error', err => {
|
|
183
|
-
this.logger.warn({ msg: 'IMAP connection error', previous: true, account: this.account, err });
|
|
184
|
-
});
|
|
182
|
+
detachImapClient(prevImapClient, this.logger, { type: 'subConnection', previous: true, account: this.account });
|
|
185
183
|
prevImapClient.close();
|
|
186
184
|
} catch (err) {
|
|
187
185
|
this.logger.warn({ msg: 'IMAP close error', err });
|
|
@@ -275,7 +275,9 @@ class SyncOperations {
|
|
|
275
275
|
|
|
276
276
|
// Record every message currently on the server as already-seen, without queuing notifications
|
|
277
277
|
for await (let messageData of imapClient.fetch('1:*', fields, { uid: true })) {
|
|
278
|
-
|
|
278
|
+
// A server can answer a FETCH without the fields it was asked for, and
|
|
279
|
+
// everything below this point dereferences the flag set
|
|
280
|
+
if (!messageData || !messageData.uid || !messageData.flags) {
|
|
279
281
|
continue;
|
|
280
282
|
}
|
|
281
283
|
// ignore Recent flag
|
|
@@ -378,7 +380,7 @@ class SyncOperations {
|
|
|
378
380
|
|
|
379
381
|
// Fetch all messages in range
|
|
380
382
|
for await (let messageData of imapClient.fetch(range, fields, opts)) {
|
|
381
|
-
if (!messageData || !messageData.uid) {
|
|
383
|
+
if (!messageData || !messageData.uid || (fields.flags && !messageData.flags)) {
|
|
382
384
|
//TODO: support partial responses
|
|
383
385
|
this.logger.debug({ msg: 'Partial FETCH response', code: 'partial_fetch', query: { range, fields, opts } });
|
|
384
386
|
continue;
|
|
@@ -539,7 +541,7 @@ class SyncOperations {
|
|
|
539
541
|
try {
|
|
540
542
|
// Fetch and process each message
|
|
541
543
|
for await (let messageData of imapClient.fetch(range, fields, opts)) {
|
|
542
|
-
if (!messageData || !messageData.uid) {
|
|
544
|
+
if (!messageData || !messageData.uid || (fields.flags && !messageData.flags)) {
|
|
543
545
|
//TODO: support partial responses
|
|
544
546
|
this.logger.debug({ msg: 'Partial FETCH response', code: 'partial_fetch', query: { range, fields, opts } });
|
|
545
547
|
continue;
|
|
@@ -13,6 +13,8 @@ const { BaseClient } = require('./base-client');
|
|
|
13
13
|
const { oauth2Apps } = require('../oauth2-apps');
|
|
14
14
|
|
|
15
15
|
const { Subconnection } = require('./imap/subconnection');
|
|
16
|
+
const { decodeStoredListing, diffMailboxListing, buildStoredListingObject } = require('./imap/listing-diff');
|
|
17
|
+
const { detachImapClient } = require('./imap/detach-client');
|
|
16
18
|
const { ReconnectBackoff } = require('./reconnect-backoff');
|
|
17
19
|
|
|
18
20
|
const {
|
|
@@ -396,7 +398,7 @@ class IMAPClient extends BaseClient {
|
|
|
396
398
|
}
|
|
397
399
|
|
|
398
400
|
this.commandClient = null;
|
|
399
|
-
commandClient.
|
|
401
|
+
detachImapClient(commandClient, this.logger, { type: 'commandClient', cid: commandCid, account: this.account });
|
|
400
402
|
commandClient = null;
|
|
401
403
|
});
|
|
402
404
|
|
|
@@ -637,15 +639,6 @@ class IMAPClient extends BaseClient {
|
|
|
637
639
|
|
|
638
640
|
// Get mailbox listing from server
|
|
639
641
|
let listing = await connectionClient.list(options);
|
|
640
|
-
if (!listing.length) {
|
|
641
|
-
// server bug, the list can never be empty
|
|
642
|
-
if (this.imapClient) {
|
|
643
|
-
this.imapClient.close();
|
|
644
|
-
}
|
|
645
|
-
let error = new Error('Server bug: empty mailbox listing');
|
|
646
|
-
error.code = 'ServerBug';
|
|
647
|
-
throw error;
|
|
648
|
-
}
|
|
649
642
|
|
|
650
643
|
// Extract delimiter from INBOX (most reliable source)
|
|
651
644
|
let inboxData = (listing || []).find(entry => /^INBOX$/i.test(entry.path));
|
|
@@ -661,70 +654,60 @@ class IMAPClient extends BaseClient {
|
|
|
661
654
|
return mailbox;
|
|
662
655
|
});
|
|
663
656
|
|
|
664
|
-
|
|
657
|
+
// Checked AFTER filtering, not just on the raw response: INBOX always exists and is
|
|
658
|
+
// always selectable, so a listing with nothing selectable left is the same server bug as
|
|
659
|
+
// an empty one. Treating it as real would clear every stored folder below - wiping each
|
|
660
|
+
// folder's message index and firing a mailboxDeleted webhook per folder - on a listing
|
|
661
|
+
// that only failed to report a single selectable folder for one pass.
|
|
662
|
+
if (!listing.length) {
|
|
663
|
+
// server bug, the list can never be empty
|
|
664
|
+
if (this.imapClient) {
|
|
665
|
+
this.imapClient.close();
|
|
666
|
+
}
|
|
667
|
+
let error = new Error('Server bug: empty mailbox listing');
|
|
668
|
+
error.code = 'ServerBug';
|
|
669
|
+
throw error;
|
|
670
|
+
}
|
|
665
671
|
|
|
666
|
-
// compare listing for new / deleted / renamed folders
|
|
667
|
-
|
|
672
|
+
// compare listing for new / deleted / renamed folders (see lib/email-client/imap/listing-diff.js)
|
|
673
|
+
const { entries: storedListing, corruptPaths } = decodeStoredListing(await this.redis.hgetallBuffer(this.getMailboxListKey()));
|
|
668
674
|
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
.
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
// compare listings to detect changes
|
|
687
|
-
for (let mailbox of listing) {
|
|
688
|
-
let existingMailbox = storedListingMap.get(normalizePath(mailbox.path));
|
|
689
|
-
if (!existingMailbox) {
|
|
690
|
-
// found new!
|
|
691
|
-
mailbox.isNew = true;
|
|
692
|
-
hasChanges = true;
|
|
693
|
-
} else if (
|
|
694
|
-
existingMailbox.delimiter !== mailbox.delimiter ||
|
|
695
|
-
existingMailbox.specialUseSource !== mailbox.specialUseSource ||
|
|
696
|
-
existingMailbox.noInferiors !== mailbox.noInferiors
|
|
697
|
-
) {
|
|
698
|
-
hasChanges = true;
|
|
675
|
+
// Flags entries of `listing` as isNew as a side effect - that one-shot flag is what makes
|
|
676
|
+
// processListing() emit mailboxNew, so it has to ride along on the returned listing.
|
|
677
|
+
let { hasChanges, deletedEntries } = diffMailboxListing(listing, storedListing);
|
|
678
|
+
|
|
679
|
+
if (corruptPaths.length) {
|
|
680
|
+
// An unusable stored value is not part of the comparison at all, so it can not be
|
|
681
|
+
// reported as deleted either. Forcing the rewrite is the only thing that clears it.
|
|
682
|
+
//
|
|
683
|
+
// This is also where the corruption is escalated, rather than on the read path in
|
|
684
|
+
// Account.getMailboxListing(): here it is reported once per occurrence with every
|
|
685
|
+
// affected folder in hand, while the read path would report the same condition again
|
|
686
|
+
// on every /v1/account/{account}/mailboxes request until the next sync repairs it.
|
|
687
|
+
this.logger.error({ msg: 'Purging corrupt stored mailbox listing entries', account: this.account, paths: corruptPaths });
|
|
688
|
+
if (logger.notifyError) {
|
|
689
|
+
let corruptErr = new Error('Corrupt stored mailbox listing entries');
|
|
690
|
+
logger.notifyError(corruptErr, { user: this.account, meta: { paths: corruptPaths.join(', ') } });
|
|
699
691
|
}
|
|
692
|
+
hasChanges = true;
|
|
700
693
|
}
|
|
701
694
|
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
if (!listingPathSet.has(normalizePath(entry.path))) {
|
|
706
|
-
// found deleted!
|
|
707
|
-
await this.clearMailboxEntry(entry);
|
|
708
|
-
hasChanges = true;
|
|
709
|
-
}
|
|
695
|
+
for (let entry of deletedEntries) {
|
|
696
|
+
// found deleted!
|
|
697
|
+
await this.clearMailboxEntry(entry);
|
|
710
698
|
}
|
|
711
699
|
|
|
712
700
|
// on changes store updated listing
|
|
713
701
|
if (hasChanges) {
|
|
714
|
-
//
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
listingObject[normalizePath(entry.path)] = msgpack.encode(mailbox);
|
|
724
|
-
});
|
|
725
|
-
|
|
726
|
-
// Atomic update of the mailbox listing
|
|
727
|
-
await this.redis.multi().del(this.getMailboxListKey()).hmset(this.getMailboxListKey(), listingObject).exec();
|
|
702
|
+
// Atomic update of the mailbox listing. HMSET refuses an empty hash, and that error
|
|
703
|
+
// aborts the entire MULTI including the DEL, so the write is only queued when there
|
|
704
|
+
// is something to write.
|
|
705
|
+
let listingObject = buildStoredListingObject(listing);
|
|
706
|
+
let updateListing = this.redis.multi().del(this.getMailboxListKey());
|
|
707
|
+
if (Object.keys(listingObject).length) {
|
|
708
|
+
updateListing = updateListing.hmset(this.getMailboxListKey(), listingObject);
|
|
709
|
+
}
|
|
710
|
+
await updateListing.exec();
|
|
728
711
|
}
|
|
729
712
|
|
|
730
713
|
return listing;
|
|
@@ -1501,15 +1484,8 @@ class IMAPClient extends BaseClient {
|
|
|
1501
1484
|
let prevImapClient = this.imapClient;
|
|
1502
1485
|
prevImapClient.disabled = true;
|
|
1503
1486
|
try {
|
|
1504
|
-
prevImapClient.
|
|
1505
|
-
|
|
1506
|
-
const prevImapErrorHandler = err => {
|
|
1507
|
-
this.logger.warn({ msg: 'IMAP connection error', type: 'imapClient', previous: true, account: this.account, err });
|
|
1508
|
-
};
|
|
1509
|
-
|
|
1510
|
-
prevImapClient.once('error', prevImapErrorHandler);
|
|
1487
|
+
detachImapClient(prevImapClient, this.logger, { type: 'imapClient', previous: true, account: this.account });
|
|
1511
1488
|
prevImapClient.close();
|
|
1512
|
-
prevImapClient.removeListener('error', prevImapErrorHandler);
|
|
1513
1489
|
|
|
1514
1490
|
if (this.commandClient) {
|
|
1515
1491
|
this.logger.debug({ msg: 'Clearing previous command connection' });
|
|
@@ -2585,17 +2561,7 @@ class IMAPClient extends BaseClient {
|
|
|
2585
2561
|
*/
|
|
2586
2562
|
async getSpecialUseMailbox(specialUse) {
|
|
2587
2563
|
let storedListing = await this.redis.hgetallBuffer(this.getMailboxListKey());
|
|
2588
|
-
return
|
|
2589
|
-
.map(path => {
|
|
2590
|
-
try {
|
|
2591
|
-
return msgpack.decode(storedListing[path]);
|
|
2592
|
-
} catch (err) {
|
|
2593
|
-
// should not happen
|
|
2594
|
-
}
|
|
2595
|
-
return false;
|
|
2596
|
-
})
|
|
2597
|
-
.filter(entry => entry)
|
|
2598
|
-
.find(entry => entry.specialUse === specialUse);
|
|
2564
|
+
return decodeStoredListing(storedListing).entries.find(entry => entry.specialUse === specialUse);
|
|
2599
2565
|
}
|
|
2600
2566
|
|
|
2601
2567
|
/**
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// How a submitted message is put on the wire for Microsoft Graph /sendMail.
|
|
4
|
+
//
|
|
5
|
+
// Extracted from OutlookClient.submitMessage() because it is the part that has to be exactly
|
|
6
|
+
// right and can not be reached from a test otherwise (submitMessage needs a live account, an
|
|
7
|
+
// OAuth app and a Graph endpoint). The encoding is not obvious and Graph is unforgiving about
|
|
8
|
+
// it: the raw branch must post base64 as text/plain (a JSON body is rejected outright), while
|
|
9
|
+
// the structured branch must post a plain object so Graph honours an explicit from address.
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Builds the raw-MIME /sendMail request.
|
|
13
|
+
*
|
|
14
|
+
* @param {Buffer} raw - Complete RFC822 message
|
|
15
|
+
* @param {String} oauth2UserPath - Graph user path ("me" or "users/<id>")
|
|
16
|
+
* @returns {Object} { path, body, options } for OutlookOauth.request()
|
|
17
|
+
*/
|
|
18
|
+
function buildRawSendMailRequest(raw, oauth2UserPath) {
|
|
19
|
+
return {
|
|
20
|
+
path: `/${oauth2UserPath}/sendMail`,
|
|
21
|
+
// Graph expects the MIME message base64 encoded in the request body itself
|
|
22
|
+
body: Buffer.from(raw.toString('base64')),
|
|
23
|
+
options: {
|
|
24
|
+
// NOT application/json - Graph rejects a base64 MIME body sent as JSON
|
|
25
|
+
contentType: 'text/plain',
|
|
26
|
+
// sendMail answers 202 Accepted with an empty body
|
|
27
|
+
returnText: true
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Builds the structured (JSON) /sendMail request used when the from address has to be honoured.
|
|
34
|
+
*
|
|
35
|
+
* @param {Object} messagePayload - Graph message object
|
|
36
|
+
* @param {String} oauth2UserPath - Graph user path ("me" or "users/<id>")
|
|
37
|
+
* @returns {Object} { path, body, options } for OutlookOauth.request()
|
|
38
|
+
*/
|
|
39
|
+
function buildStructuredSendMailRequest(messagePayload, oauth2UserPath) {
|
|
40
|
+
return {
|
|
41
|
+
path: `/${oauth2UserPath}/sendMail`,
|
|
42
|
+
body: { message: messagePayload },
|
|
43
|
+
// No contentType: an object body is serialized as application/json
|
|
44
|
+
options: { returnText: true }
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
module.exports = { buildRawSendMailRequest, buildStructuredSendMailRequest };
|
|
@@ -16,6 +16,7 @@ const { detectMimeType, detectExtension } = require('nodemailer/lib/mime-funcs/m
|
|
|
16
16
|
|
|
17
17
|
// Import Graph API module for MS Graph API handling
|
|
18
18
|
const graphApi = require('./outlook/graph-api');
|
|
19
|
+
const { buildRawSendMailRequest, buildStructuredSendMailRequest } = require('./outlook/send-request');
|
|
19
20
|
|
|
20
21
|
const {
|
|
21
22
|
REDIS_PREFIX,
|
|
@@ -2457,16 +2458,8 @@ class OutlookClient extends BaseClient {
|
|
|
2457
2458
|
// Send via Graph API using structured message format
|
|
2458
2459
|
// This ensures the 'from' field is properly respected for shared mailboxes
|
|
2459
2460
|
// Note: sendMail returns 202 Accepted with empty body
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
'post',
|
|
2463
|
-
{
|
|
2464
|
-
message: messagePayload
|
|
2465
|
-
},
|
|
2466
|
-
{
|
|
2467
|
-
returnText: true
|
|
2468
|
-
}
|
|
2469
|
-
);
|
|
2461
|
+
let sendRequest = buildStructuredSendMailRequest(messagePayload, this.oauth2UserPath);
|
|
2462
|
+
await this.request(sendRequest.path, 'post', sendRequest.body, sendRequest.options);
|
|
2470
2463
|
} catch (err) {
|
|
2471
2464
|
switch (err.oauthRequest?.status) {
|
|
2472
2465
|
case 400: {
|
|
@@ -2492,10 +2485,8 @@ class OutlookClient extends BaseClient {
|
|
|
2492
2485
|
// Use raw MIME format (default)
|
|
2493
2486
|
// Preserves calendar invites and special MIME types, but ignores from field
|
|
2494
2487
|
try {
|
|
2495
|
-
|
|
2496
|
-
|
|
2497
|
-
returnText: true
|
|
2498
|
-
});
|
|
2488
|
+
let sendRequest = buildRawSendMailRequest(raw, this.oauth2UserPath);
|
|
2489
|
+
await this.request(sendRequest.path, 'post', sendRequest.body, sendRequest.options);
|
|
2499
2490
|
} catch (err) {
|
|
2500
2491
|
switch (err.oauthRequest?.status) {
|
|
2501
2492
|
case 400: {
|