forked from postalsys/imapflow
/
list.ts
326 lines (275 loc) · 14 KB
/
list.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import { decodePath, encodePath, normalizePath } from '../tools.js';
import { specialUse } from '../special-use';
// Lists mailboxes from server
export const LIST = async (connection, reference, mailbox, options) => {
options = options || {};
const FLAG_SORT_ORDER = ['\\Inbox', '\\Flagged', '\\Sent', '\\Drafts', '\\All', '\\Archive', '\\Junk', '\\Trash'];
const SOURCE_SORT_ORDER = ['user', 'extension', 'name'];
let listCommand = connection.capabilities.has('XLIST') && !connection.capabilities.has('SPECIAL-USE') ? 'XLIST' : 'LIST';
let response;
try {
let entries = [];
let statusMap = new Map();
let returnArgs = [];
let statusQueryAttributes = [];
if (options.statusQuery) {
Object.keys(options.statusQuery || {}).forEach(key => {
if (!options.statusQuery[key]) {
return;
}
switch (key.toUpperCase()) {
case 'MESSAGES':
case 'RECENT':
case 'UIDNEXT':
case 'UIDVALIDITY':
case 'UNSEEN':
statusQueryAttributes.push({ type: 'ATOM', value: key.toUpperCase() });
break;
case 'HIGHESTMODSEQ':
if (connection.capabilities.has('CONDSTORE')) {
statusQueryAttributes.push({ type: 'ATOM', value: key.toUpperCase() });
}
break;
}
});
}
if (listCommand === 'LIST' && connection.capabilities.has('LIST-STATUS') && statusQueryAttributes.length) {
returnArgs.push({ type: 'ATOM', value: 'STATUS' }, statusQueryAttributes);
if (connection.capabilities.has('SPECIAL-USE')) {
returnArgs.push({ type: 'ATOM', value: 'SPECIAL-USE' });
}
}
let specialUseMatches = {};
let addSpecialUseMatch = (entry, type, source) => {
if (!specialUseMatches[type]) {
specialUseMatches[type] = [];
}
specialUseMatches[type].push({ entry, source });
};
let specialUseHints = {};
if (options.specialUseHints && typeof options.specialUseHints === 'object') {
for (let type of Object.keys(options.specialUseHints)) {
if (
['sent', 'junk', 'trash', 'drafts', 'archive'].includes(type) &&
options.specialUseHints[type] &&
typeof options.specialUseHints[type] === 'string'
) {
specialUseHints[normalizePath(connection, options.specialUseHints[type])] = `\\${type.replace(/^./, c => c.toUpperCase())}`;
}
}
}
let runList = async (reference, mailbox) => {
const cmdArgs = [encodePath(connection, reference), encodePath(connection, mailbox)];
if (returnArgs.length) {
cmdArgs.push({ type: 'ATOM', value: 'RETURN' }, returnArgs);
}
response = await connection.exec(listCommand, cmdArgs, {
untagged: {
[listCommand]: async untagged => {
if (!untagged.attributes || !untagged.attributes.length) {
return;
}
let entry = {
path: normalizePath(connection, decodePath(connection, (untagged.attributes[2] && untagged.attributes[2].value) || '')),
pathAsListed: (untagged.attributes[2] && untagged.attributes[2].value) || '',
flags: new Set(untagged.attributes[0].map(entry => entry.value)),
delimiter: untagged.attributes[1] && untagged.attributes[1].value,
listed: true
};
if (specialUseHints[entry.path]) {
addSpecialUseMatch(entry, specialUseHints[entry.path], 'user');
}
if (listCommand === 'XLIST' && entry.flags.has('\\Inbox')) {
// XLIST specific flag, ignore
entry.flags.delete('\\Inbox');
if (entry.path !== 'INBOX') {
// XLIST may use localised inbox name
addSpecialUseMatch(entry, '\\Inbox', 'extension');
}
}
if (entry.path.toUpperCase() === 'INBOX') {
addSpecialUseMatch(entry, '\\Inbox', 'name');
}
if (entry.delimiter && entry.path.charAt(0) === entry.delimiter) {
entry.path = entry.path.slice(1);
}
entry.parentPath = entry.delimiter && entry.path ? entry.path.substr(0, entry.path.lastIndexOf(entry.delimiter)) : '';
entry.parent = entry.delimiter ? entry.path.split(entry.delimiter) : [entry.path];
entry.name = entry.parent.pop();
let { flag: specialUseFlag, source: flagSource } = specialUse(
connection.capabilities.has('XLIST') || connection.capabilities.has('SPECIAL-USE'),
entry
);
if (specialUseFlag) {
addSpecialUseMatch(entry, specialUseFlag, flagSource);
}
entries.push(entry);
},
STATUS: async untagged => {
let statusPath = normalizePath(connection, decodePath(connection, (untagged.attributes[0] && untagged.attributes[0].value) || ''));
let statusList = untagged.attributes && Array.isArray(untagged.attributes[1]) ? untagged.attributes[1] : false;
if (!statusList || !statusPath) {
return;
}
let key;
let map = { path: statusPath };
statusList.forEach((entry, i) => {
if (i % 2 === 0) {
key = entry && typeof entry.value === 'string' ? entry.value : false;
return;
}
if (!key || !entry || typeof entry.value !== 'string') {
return;
}
let value = false;
switch (key.toUpperCase()) {
case 'MESSAGES':
key = 'messages';
value = !isNaN(entry.value) ? Number(entry.value) : false;
break;
case 'RECENT':
key = 'recent';
value = !isNaN(entry.value) ? Number(entry.value) : false;
break;
case 'UIDNEXT':
key = 'uidNext';
value = !isNaN(entry.value) ? Number(entry.value) : false;
break;
case 'UIDVALIDITY':
key = 'uidValidity';
value = !isNaN(entry.value) ? BigInt(entry.value) : false;
break;
case 'UNSEEN':
key = 'unseen';
value = !isNaN(entry.value) ? Number(entry.value) : false;
break;
case 'HIGHESTMODSEQ':
key = 'highestModseq';
value = !isNaN(entry.value) ? BigInt(entry.value) : false;
break;
}
if (value === false) {
return;
}
map[key] = value;
});
statusMap.set(statusPath, map);
}
}
});
response.next();
};
let normalizedReference = normalizePath(connection, reference || '');
await runList(normalizedReference, normalizePath(connection, mailbox || '', true));
if (options.listOnly) {
return entries;
}
if (normalizedReference && !specialUseMatches['\\Inbox']) {
// INBOX was most probably not included in the listing if namespace was used
await runList('', 'INBOX');
}
if (options.statusQuery) {
for (let entry of entries) {
if (!entry.flags.has('\\Noselect') && !entry.flags.has('\\NonExistent')) {
if (statusMap.has(entry.path)) {
entry.status = statusMap.get(entry.path);
} else if (!statusMap.size) {
// run STATUS command
try {
entry.status = await connection.run('STATUS', entry.path, options.statusQuery);
} catch (err) {
entry.status = { error: err };
}
}
}
}
}
response = await connection.exec(
'LSUB',
[encodePath(connection, normalizePath(connection, reference || '')), encodePath(connection, normalizePath(connection, mailbox || '', true))],
{
untagged: {
LSUB: async untagged => {
if (!untagged.attributes || !untagged.attributes.length) {
return;
}
let entry = {
path: normalizePath(connection, decodePath(connection, (untagged.attributes[2] && untagged.attributes[2].value) || '')),
pathAsListed: (untagged.attributes[2] && untagged.attributes[2].value) || '',
flags: new Set(untagged.attributes[0].map(entry => entry.value)),
delimiter: untagged.attributes[1] && untagged.attributes[1].value,
subscribed: true
};
if (entry.path.toUpperCase() === 'INBOX') {
addSpecialUseMatch(entry, '\\Inbox', 'name');
}
if (entry.delimiter && entry.path.charAt(0) === entry.delimiter) {
entry.path = entry.path.slice(1);
}
entry.parentPath = entry.delimiter && entry.path ? entry.path.substr(0, entry.path.lastIndexOf(entry.delimiter)) : '';
entry.parent = entry.delimiter ? entry.path.split(entry.delimiter) : [entry.path];
entry.name = entry.parent.pop();
let existing = entries.find(existing => existing.path === entry.path);
if (existing) {
existing.subscribed = true;
entry.flags.forEach(flag => existing.flags.add(flag));
} else {
// ignore non-listed folders
/*
let specialUseFlag = specialUse(connection.capabilities.has('XLIST') || connection.capabilities.has('SPECIAL-USE'), entry);
if (specialUseFlag && !flagsSeen.has(specialUseFlag)) {
entry.specialUse = specialUseFlag;
}
entries.push(entry);
*/
}
}
}
}
);
response.next();
for (let type of Object.keys(specialUseMatches)) {
let sortedEntries = specialUseMatches[type].sort((a, b) => {
let aSource = SOURCE_SORT_ORDER.indexOf(a.source);
let bSource = SOURCE_SORT_ORDER.indexOf(b.source);
if (aSource === bSource) {
return a.entry.path.localeCompare(b.entry.path);
}
return aSource - bSource;
});
if (!sortedEntries[0].entry.specialUse) {
sortedEntries[0].entry.specialUse = type;
sortedEntries[0].entry.specialUseSource = sortedEntries[0].source;
}
}
let inboxEntry = entries.find(entry => entry.specialUse === '\\Inbox');
if (inboxEntry && !inboxEntry.subscribed) {
// override server settings and make INBOX always as subscribed
inboxEntry.subscribed = true;
}
return entries.sort((a, b) => {
if (a.specialUse && !b.specialUse) {
return -1;
}
if (!a.specialUse && b.specialUse) {
return 1;
}
if (a.specialUse && b.specialUse) {
return FLAG_SORT_ORDER.indexOf(a.specialUse) - FLAG_SORT_ORDER.indexOf(b.specialUse);
}
let aList = [].concat(a.parent).concat(a.name);
let bList = [].concat(b.parent).concat(b.name);
for (let i = 0; i < aList.length; i++) {
let aPart = aList[i];
let bPart = bList[i];
if (aPart !== bPart) {
return aPart.localeCompare(bPart || '');
}
}
return a.path.localeCompare(b.path);
});
} catch (err) {
connection.log.warn({ msg: 'Failed to list folders', err, cid: connection.id });
throw err;
}
};