imapkit 4.3.1 → 5.0.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/README.md +187 -67
- package/bin/help.txt +20 -5
- package/bin/imapkit.js +64 -24
- package/dist/cjs/commands/append.js +1 -1
- package/dist/cjs/commands/create.js +2 -2
- package/dist/cjs/commands/delete.js +2 -14
- package/dist/cjs/commands/handlers/fetch.js +2 -1
- package/dist/cjs/commands/handlers/flags.d.ts +3 -3
- package/dist/cjs/commands/handlers/flags.js +3 -3
- package/dist/cjs/commands/handlers/store.js +7 -7
- package/dist/cjs/commands/list.js +17 -3
- package/dist/cjs/commands/rename.js +6 -46
- package/dist/cjs/commands/select.js +1 -0
- package/dist/cjs/commands/subscribe.js +7 -8
- package/dist/cjs/commands/unsubscribe.js +3 -9
- package/dist/cjs/control.d.ts +425 -0
- package/dist/cjs/control.js +637 -0
- package/dist/cjs/dates.d.ts +9 -1
- package/dist/cjs/dates.js +20 -0
- package/dist/cjs/envelope.d.ts +1 -1
- package/dist/cjs/envelope.js +26 -1
- package/dist/cjs/index.d.ts +9 -1
- package/dist/cjs/index.js +9 -2
- package/dist/cjs/load-plugins.d.ts +3 -1
- package/dist/cjs/load-plugins.js +28 -4
- package/dist/cjs/plugins/acl.js +109 -42
- package/dist/cjs/plugins/binary.js +2 -1
- package/dist/cjs/plugins/condstore.js +7 -0
- package/dist/cjs/plugins/context-search.js +2 -2
- package/dist/cjs/plugins/create-special-use.d.ts +0 -5
- package/dist/cjs/plugins/create-special-use.js +11 -1
- package/dist/cjs/plugins/imap4rev2.js +5 -0
- package/dist/cjs/plugins/index.js +1 -3
- package/dist/cjs/plugins/metadata-server.d.ts +6 -2
- package/dist/cjs/plugins/metadata-server.js +3 -1
- package/dist/cjs/plugins/metadata.d.ts +5 -1
- package/dist/cjs/plugins/metadata.js +143 -57
- package/dist/cjs/plugins/objectid.js +10 -0
- package/dist/cjs/plugins/quota.js +50 -2
- package/dist/cjs/plugins/savedate.js +1 -1
- package/dist/cjs/plugins/special-use.d.ts +0 -8
- package/dist/cjs/plugins/special-use.js +23 -0
- package/dist/cjs/quirks.d.ts +28 -0
- package/dist/cjs/quirks.js +125 -0
- package/dist/cjs/random.d.ts +10 -0
- package/dist/cjs/random.js +22 -0
- package/dist/cjs/rest.d.ts +54 -0
- package/dist/cjs/rest.js +567 -0
- package/dist/cjs/script.d.ts +33 -4
- package/dist/cjs/script.js +64 -4
- package/dist/cjs/server.d.ts +199 -9
- package/dist/cjs/server.js +603 -227
- package/dist/cjs/smtp-listener.d.ts +3 -2
- package/dist/cjs/smtp-listener.js +10 -6
- package/dist/cjs/storage-schema.d.ts +139 -0
- package/dist/cjs/storage-schema.js +222 -0
- package/dist/cjs/store-operations.d.ts +171 -0
- package/dist/cjs/store-operations.js +425 -0
- package/dist/cjs/types.d.ts +26 -1
- package/dist/esm/commands/append.js +1 -1
- package/dist/esm/commands/create.js +2 -2
- package/dist/esm/commands/delete.js +2 -14
- package/dist/esm/commands/handlers/fetch.js +2 -1
- package/dist/esm/commands/handlers/flags.d.ts +3 -3
- package/dist/esm/commands/handlers/flags.js +3 -3
- package/dist/esm/commands/handlers/store.js +7 -7
- package/dist/esm/commands/list.js +17 -3
- package/dist/esm/commands/rename.js +6 -46
- package/dist/esm/commands/select.js +1 -0
- package/dist/esm/commands/subscribe.js +7 -8
- package/dist/esm/commands/unsubscribe.js +3 -9
- package/dist/esm/control.d.ts +425 -0
- package/dist/esm/control.js +630 -0
- package/dist/esm/dates.d.ts +9 -1
- package/dist/esm/dates.js +20 -1
- package/dist/esm/envelope.d.ts +1 -1
- package/dist/esm/envelope.js +26 -1
- package/dist/esm/index.d.ts +9 -1
- package/dist/esm/index.js +5 -2
- package/dist/esm/load-plugins.d.ts +3 -1
- package/dist/esm/load-plugins.js +28 -4
- package/dist/esm/plugins/acl.js +109 -42
- package/dist/esm/plugins/binary.js +2 -1
- package/dist/esm/plugins/condstore.js +7 -0
- package/dist/esm/plugins/context-search.js +2 -2
- package/dist/esm/plugins/create-special-use.d.ts +0 -5
- package/dist/esm/plugins/create-special-use.js +8 -1
- package/dist/esm/plugins/imap4rev2.js +5 -0
- package/dist/esm/plugins/index.js +1 -3
- package/dist/esm/plugins/metadata-server.d.ts +6 -2
- package/dist/esm/plugins/metadata-server.js +3 -1
- package/dist/esm/plugins/metadata.d.ts +5 -1
- package/dist/esm/plugins/metadata.js +143 -57
- package/dist/esm/plugins/objectid.js +10 -0
- package/dist/esm/plugins/quota.js +50 -2
- package/dist/esm/plugins/savedate.js +1 -1
- package/dist/esm/plugins/special-use.d.ts +0 -8
- package/dist/esm/plugins/special-use.js +23 -0
- package/dist/esm/quirks.d.ts +28 -0
- package/dist/esm/quirks.js +121 -0
- package/dist/esm/random.d.ts +10 -0
- package/dist/esm/random.js +19 -0
- package/dist/esm/rest.d.ts +54 -0
- package/dist/esm/rest.js +559 -0
- package/dist/esm/script.d.ts +33 -4
- package/dist/esm/script.js +63 -4
- package/dist/esm/server.d.ts +199 -9
- package/dist/esm/server.js +606 -230
- package/dist/esm/smtp-listener.d.ts +3 -2
- package/dist/esm/smtp-listener.js +10 -6
- package/dist/esm/storage-schema.d.ts +139 -0
- package/dist/esm/storage-schema.js +218 -0
- package/dist/esm/store-operations.d.ts +171 -0
- package/dist/esm/store-operations.js +410 -0
- package/dist/esm/types.d.ts +26 -1
- package/dist/plugin-help.json +3 -16
- package/package.json +11 -3
- package/dist/cjs/plugins/xtoybird.d.ts +0 -2
- package/dist/cjs/plugins/xtoybird.js +0 -236
- package/dist/esm/plugins/xtoybird.d.ts +0 -2
- package/dist/esm/plugins/xtoybird.js +0 -218
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { storeError } from '../store-operations.js';
|
|
1
2
|
import { states } from '../command-states.js';
|
|
2
3
|
import { isNumber } from '../numbers.js';
|
|
3
4
|
import { isAstring } from '../arguments.js';
|
|
@@ -10,6 +11,7 @@ import { registerEnable, isEnabled } from './enable.js';
|
|
|
10
11
|
* @help default 65536), "metadataMaxEntries" (per mailbox and for the
|
|
11
12
|
* @help server, default 100). "metadataPrivate": false turns /private
|
|
12
13
|
* @help entries off. ENABLE METADATA turns on unsolicited METADATA responses
|
|
14
|
+
* @help (loads the ENABLE plugin, RFC 5464 section 4.1)
|
|
13
15
|
*/
|
|
14
16
|
// Default limits, RFC 5464 section 4.1 requires at least 1024 octets and 10 entries
|
|
15
17
|
const DEFAULT_MAX_SIZE = 64 * 1024;
|
|
@@ -308,6 +310,60 @@ function setup(server, mailboxes) {
|
|
|
308
310
|
return callback();
|
|
309
311
|
}, { states: states.AUTHENTICATED });
|
|
310
312
|
// RFC 5464 section 4.3: setmetadata = "SETMETADATA" SP mailbox SP entry-values
|
|
313
|
+
/**
|
|
314
|
+
* Stores the changes of SETMETADATA (or of the control API) when all of them pass the checks, and tells the
|
|
315
|
+
* sessions that used ENABLE METADATA about the changed entries. The session that made the change
|
|
316
|
+
* (`server.activeConnection`) is not told, and without one (the control API) the read-only server entries can
|
|
317
|
+
* be set too
|
|
318
|
+
*
|
|
319
|
+
* @param {Object} holder Mailbox object or state.server
|
|
320
|
+
* @param {Map} changes Lower case entry name to the new value, null removes the entry
|
|
321
|
+
* @return {Object|null} `{ text, code }` when a change fails, nothing is changed then
|
|
322
|
+
*/
|
|
323
|
+
const storeChanges = (holder, changes) => {
|
|
324
|
+
const origin = server.activeConnection;
|
|
325
|
+
const entries = getEntries(holder);
|
|
326
|
+
const computed = getComputedEntries(holder);
|
|
327
|
+
// changes go to a copy that replaces the entries once every check passed
|
|
328
|
+
const next = Object.assign(Object.create(null), entries);
|
|
329
|
+
const changed = [];
|
|
330
|
+
for (const [name, value] of changes) {
|
|
331
|
+
if ((!holder.path && origin && READ_ONLY_SERVER_ENTRIES.has(name)) || name in computed) {
|
|
332
|
+
return { text: 'The ' + name + ' entry is read-only', code: 'CANNOT' };
|
|
333
|
+
}
|
|
334
|
+
if (!state.allowPrivate && name.startsWith('/private/')) {
|
|
335
|
+
return { text: 'Private annotations are not supported', code: ['METADATA', 'NOPRIVATE'] };
|
|
336
|
+
}
|
|
337
|
+
if (value !== null && value.length > state.maxSize) {
|
|
338
|
+
return { text: 'Value of ' + name + ' is too large', code: ['METADATA', 'MAXSIZE', state.maxSize] };
|
|
339
|
+
}
|
|
340
|
+
if ((entries[name] ?? null) !== value) {
|
|
341
|
+
changed.push(name);
|
|
342
|
+
}
|
|
343
|
+
if (value === null) {
|
|
344
|
+
delete next[name];
|
|
345
|
+
}
|
|
346
|
+
else {
|
|
347
|
+
next[name] = value;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
// only adding entries can fail, even when the storage already holds more than the limit
|
|
351
|
+
const count = Object.keys(next).length;
|
|
352
|
+
if (count > state.maxEntries && count > Object.keys(entries).length) {
|
|
353
|
+
return { text: 'Too many annotations', code: ['METADATA', 'TOOMANY'] };
|
|
354
|
+
}
|
|
355
|
+
setEntries(holder, next);
|
|
356
|
+
if (changed.length) {
|
|
357
|
+
// RFC 5464 section 4.4.2: unsolicited responses only list the entry names. Section 4.1:
|
|
358
|
+
// only sessions that used ENABLE METADATA (or METADATA-SERVER) get them
|
|
359
|
+
server.notify({
|
|
360
|
+
tag: '*',
|
|
361
|
+
command: 'METADATA',
|
|
362
|
+
attributes: [mailboxAttribute(holder.path)].concat(changed.map(name => ({ type: 'ATOM', value: name })))
|
|
363
|
+
}, false, origin, isMetadataEnabled);
|
|
364
|
+
}
|
|
365
|
+
return null;
|
|
366
|
+
};
|
|
311
367
|
server.setCommandHandler('SETMETADATA', (connection, parsed, data, callback) => {
|
|
312
368
|
const attrs = parsed.attributes || [];
|
|
313
369
|
if (attrs.length !== 2 || !isAstring(attrs[0]) || !Array.isArray(attrs[1])) {
|
|
@@ -343,79 +399,109 @@ function setup(server, mailboxes) {
|
|
|
343
399
|
if (!holder) {
|
|
344
400
|
return callback();
|
|
345
401
|
}
|
|
346
|
-
const
|
|
347
|
-
|
|
348
|
-
const fail = (text, code) => {
|
|
402
|
+
const failure = storeChanges(holder, changes);
|
|
403
|
+
if (failure) {
|
|
349
404
|
// RFC 5464 section 4.3: when one entry fails, no entry is changed
|
|
350
|
-
connection.sendStatus(parsed, data, 'NO', text, code);
|
|
405
|
+
connection.sendStatus(parsed, data, 'NO', failure.text, failure.code);
|
|
351
406
|
return callback();
|
|
352
|
-
};
|
|
353
|
-
// changes go to a copy that replaces the entries once every check passed
|
|
354
|
-
const next = Object.assign(Object.create(null), entries);
|
|
355
|
-
const changed = [];
|
|
356
|
-
for (const [name, value] of changes) {
|
|
357
|
-
if ((!holder.path && READ_ONLY_SERVER_ENTRIES.has(name)) || name in computed) {
|
|
358
|
-
return fail('The ' + name + ' entry is read-only', 'CANNOT');
|
|
359
|
-
}
|
|
360
|
-
if (!state.allowPrivate && name.startsWith('/private/')) {
|
|
361
|
-
return fail('Private annotations are not supported', ['METADATA', 'NOPRIVATE']);
|
|
362
|
-
}
|
|
363
|
-
if (value !== null && value.length > state.maxSize) {
|
|
364
|
-
return fail('Value of ' + name + ' is too large', ['METADATA', 'MAXSIZE', state.maxSize]);
|
|
365
|
-
}
|
|
366
|
-
if ((entries[name] ?? null) !== value) {
|
|
367
|
-
changed.push(name);
|
|
368
|
-
}
|
|
369
|
-
if (value === null) {
|
|
370
|
-
delete next[name];
|
|
371
|
-
}
|
|
372
|
-
else {
|
|
373
|
-
next[name] = value;
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
// only adding entries can fail, even when the storage already holds more than the limit
|
|
377
|
-
const count = Object.keys(next).length;
|
|
378
|
-
if (count > state.maxEntries && count > Object.keys(entries).length) {
|
|
379
|
-
return fail('Too many annotations', ['METADATA', 'TOOMANY']);
|
|
380
|
-
}
|
|
381
|
-
setEntries(holder, next);
|
|
382
|
-
if (changed.length) {
|
|
383
|
-
// RFC 5464 section 4.4.2: unsolicited responses only list the entry names. Section 4.1:
|
|
384
|
-
// only sessions that used ENABLE METADATA (or METADATA-SERVER) get them
|
|
385
|
-
server.notify({
|
|
386
|
-
tag: '*',
|
|
387
|
-
command: 'METADATA',
|
|
388
|
-
attributes: [mailboxAttribute(holder.path)].concat(changed.map(name => ({ type: 'ATOM', value: name })))
|
|
389
|
-
}, false, connection, isMetadataEnabled);
|
|
390
407
|
}
|
|
391
408
|
connection.sendStatus(parsed, data, 'OK', 'SETMETADATA completed');
|
|
392
409
|
return callback();
|
|
393
410
|
},
|
|
394
411
|
// RFC 5464 section 5: values can be literal8, with or without BINARY
|
|
395
412
|
{ states: states.AUTHENTICATED, mailboxArguments: [0], literal8: true });
|
|
396
|
-
server.
|
|
397
|
-
if (
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
if (target && target !== extra && extra.metadata) {
|
|
405
|
-
setEntries(target, Object.assign(Object.create(null), getEntries(extra)));
|
|
413
|
+
server.on('mailbox', (event) => {
|
|
414
|
+
if (event.type === 'rename' && event.oldPath === 'INBOX') {
|
|
415
|
+
// RFC 5464 section 4.1: renaming INBOX copies its annotations, INBOX keeps them. Other mailboxes are
|
|
416
|
+
// moved as objects, so their annotations follow without any help
|
|
417
|
+
const inbox = server.getMailbox('INBOX');
|
|
418
|
+
const target = server.getMailbox(event.path);
|
|
419
|
+
if (inbox && target && target !== inbox && inbox.metadata) {
|
|
420
|
+
setEntries(target, Object.assign(Object.create(null), getEntries(inbox)));
|
|
406
421
|
}
|
|
407
422
|
}
|
|
408
|
-
else if (
|
|
409
|
-
// RFC 5464 section 4.1: a mailbox created later with the same name must not inherit the
|
|
410
|
-
//
|
|
411
|
-
const placeholder =
|
|
412
|
-
if (placeholder) {
|
|
423
|
+
else if (event.type === 'delete') {
|
|
424
|
+
// RFC 5464 section 4.1: a mailbox created later with the same name must not inherit the annotations.
|
|
425
|
+
// A mailbox with children stays as a \Noselect placeholder
|
|
426
|
+
const placeholder = server.getMailbox(event.path);
|
|
427
|
+
if (placeholder && placeholder !== event.mailbox) {
|
|
413
428
|
delete placeholder.metadata;
|
|
414
429
|
}
|
|
415
430
|
}
|
|
416
431
|
});
|
|
432
|
+
// Control API (README "Control API"): the stored annotations of a mailbox, or of the server for "". Values are
|
|
433
|
+
// UTF-8 text, the protocol keeps them as octets
|
|
434
|
+
// control.reset() restores the server annotations of the options, the mailboxes come with their storage
|
|
435
|
+
server.on('reset', () => {
|
|
436
|
+
state.server = { path: '', metadata: options.metadata };
|
|
437
|
+
});
|
|
438
|
+
const controlHolder = (path) => {
|
|
439
|
+
if (path === '') {
|
|
440
|
+
return state.server;
|
|
441
|
+
}
|
|
442
|
+
if (!state.mailboxes) {
|
|
443
|
+
throw storeError('Mailbox annotations need the METADATA plugin, METADATA-SERVER has server annotations only', 'INVALID');
|
|
444
|
+
}
|
|
445
|
+
return server.control.requireMailbox(path);
|
|
446
|
+
};
|
|
447
|
+
const getMetadataOperation = (path) => {
|
|
448
|
+
const entries = getEntries(controlHolder(path));
|
|
449
|
+
const result = {};
|
|
450
|
+
Object.keys(entries).forEach(name => {
|
|
451
|
+
result[name] = Buffer.from(entries[name], 'binary').toString('utf-8');
|
|
452
|
+
});
|
|
453
|
+
return result;
|
|
454
|
+
};
|
|
455
|
+
const setMetadataOperation = (path, values) => {
|
|
456
|
+
const holder = controlHolder(path);
|
|
457
|
+
if (!values || typeof values !== 'object' || Array.isArray(values)) {
|
|
458
|
+
throw storeError('setMetadata expects an object of entry names and values (null removes an entry)', 'INVALID');
|
|
459
|
+
}
|
|
460
|
+
const changes = new Map();
|
|
461
|
+
Object.keys(values).forEach(name => {
|
|
462
|
+
const value = values[name];
|
|
463
|
+
const error = checkEntryName(name, true);
|
|
464
|
+
if (error) {
|
|
465
|
+
throw storeError(error, 'INVALID');
|
|
466
|
+
}
|
|
467
|
+
if (value !== null && typeof value !== 'string') {
|
|
468
|
+
throw storeError('Value of ' + name + ' must be a string or null', 'INVALID');
|
|
469
|
+
}
|
|
470
|
+
changes.set(name.toLowerCase(), value === null ? null : Buffer.from(value, 'utf-8').toString('binary'));
|
|
471
|
+
});
|
|
472
|
+
const failure = storeChanges(holder, changes);
|
|
473
|
+
if (failure) {
|
|
474
|
+
throw storeError(failure.text, typeof failure.code === 'string' ? failure.code : 'INVALID');
|
|
475
|
+
}
|
|
476
|
+
return getMetadataOperation(path);
|
|
477
|
+
};
|
|
478
|
+
server.control.register('getMetadata', getMetadataOperation, [
|
|
479
|
+
{ method: 'GET', path: '/v1/metadata', summary: 'Server annotations (METADATA plugins)', handler: () => server.control.getMetadata('') },
|
|
480
|
+
{
|
|
481
|
+
method: 'GET',
|
|
482
|
+
path: '/v1/mailboxes/{path}/metadata',
|
|
483
|
+
summary: 'Mailbox annotations (METADATA plugin)',
|
|
484
|
+
handler: ({ params }) => server.control.getMetadata(params.path)
|
|
485
|
+
}
|
|
486
|
+
]);
|
|
487
|
+
server.control.register('setMetadata', setMetadataOperation, [
|
|
488
|
+
{
|
|
489
|
+
method: 'PUT',
|
|
490
|
+
path: '/v1/metadata',
|
|
491
|
+
summary: 'Sets server annotations ({ "/shared/comment": "text" }, null removes one, METADATA plugins)',
|
|
492
|
+
handler: ({ body }) => server.control.setMetadata('', body)
|
|
493
|
+
},
|
|
494
|
+
{
|
|
495
|
+
method: 'PUT',
|
|
496
|
+
path: '/v1/mailboxes/{path}/metadata',
|
|
497
|
+
summary: 'Sets mailbox annotations (METADATA plugin)',
|
|
498
|
+
handler: ({ params, body }) => server.control.setMetadata(params.path, body)
|
|
499
|
+
}
|
|
500
|
+
]);
|
|
417
501
|
}
|
|
418
502
|
export default function metadataPlugin(server) {
|
|
419
503
|
setup(server, true);
|
|
420
504
|
}
|
|
505
|
+
// RFC 5464 section 4.1: a server that sends unsolicited METADATA responses "MUST support the ENABLE command"
|
|
506
|
+
metadataPlugin.requires = ['ENABLE'];
|
|
421
507
|
export { setup };
|
|
@@ -95,6 +95,16 @@ export default function objectidPlugin(server) {
|
|
|
95
95
|
}
|
|
96
96
|
getMailboxId(mailbox);
|
|
97
97
|
});
|
|
98
|
+
// Control API (README "Control API"): the object ids in getMailbox() and getMessage()
|
|
99
|
+
server.control.mailboxInfoHandlers.push((mailbox, info) => {
|
|
100
|
+
if (mailbox.flags.indexOf('\\Noselect') < 0) {
|
|
101
|
+
info.mailboxId = getMailboxId(mailbox);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
server.control.messageInfoHandlers.push((message, info) => {
|
|
105
|
+
info.emailId = message.EMAILID;
|
|
106
|
+
info.threadId = message.THREADID;
|
|
107
|
+
});
|
|
98
108
|
server.messageHandlers.push((server, message, mailbox) => {
|
|
99
109
|
const owner = 'a message of mailbox ' + mailbox.path;
|
|
100
110
|
if (message.EMAILID) {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { storeError } from '../store-operations.js';
|
|
1
2
|
import { states } from '../command-states.js';
|
|
2
3
|
import { normalizeLineBreaks } from '../mimeparser.js';
|
|
3
4
|
import { MAX_NUMBER64, isNumber } from '../numbers.js';
|
|
@@ -29,6 +30,8 @@ export default function quotaPlugin(server) {
|
|
|
29
30
|
soft: !!config.soft,
|
|
30
31
|
limits: {}
|
|
31
32
|
};
|
|
33
|
+
// the limits of the quota option, control.reset() restores them
|
|
34
|
+
const initialLimits = {};
|
|
32
35
|
RESOURCES.forEach(resource => {
|
|
33
36
|
if (config[resource] === undefined || config[resource] === null) {
|
|
34
37
|
return;
|
|
@@ -37,8 +40,13 @@ export default function quotaPlugin(server) {
|
|
|
37
40
|
if (!Number.isSafeInteger(limit) || limit < 0) {
|
|
38
41
|
throw new Error('Invalid quota limit for ' + resource + ': ' + JSON.stringify(config[resource]));
|
|
39
42
|
}
|
|
40
|
-
|
|
43
|
+
initialLimits[resource] = limit;
|
|
41
44
|
});
|
|
45
|
+
const restoreLimits = () => {
|
|
46
|
+
root.limits = Object.assign({}, initialLimits);
|
|
47
|
+
};
|
|
48
|
+
restoreLimits();
|
|
49
|
+
server.on('reset', restoreLimits);
|
|
42
50
|
// RFC 9208 sections 3.1.1 and 5: every supported resource is advertised, SETQUOTA needs QUOTASET
|
|
43
51
|
server.registerCapability('QUOTA');
|
|
44
52
|
RESOURCES.forEach(resource => server.registerCapability('QUOTA=RES-' + resource));
|
|
@@ -111,7 +119,7 @@ export default function quotaPlugin(server) {
|
|
|
111
119
|
}
|
|
112
120
|
const added = { MESSAGE: messages.length, bytes: totalSize(messages) };
|
|
113
121
|
// RFC 8508 section 3.4: REPLACE counts only the net usage, the replaced message is removed
|
|
114
|
-
if (options.replaced && connection.selectedMailbox && inRoot(connection.selectedMailbox)) {
|
|
122
|
+
if (options.replaced && connection && connection.selectedMailbox && inRoot(connection.selectedMailbox)) {
|
|
115
123
|
added.MESSAGE--;
|
|
116
124
|
added.bytes -= totalSize([options.replaced]);
|
|
117
125
|
}
|
|
@@ -236,4 +244,44 @@ export default function quotaPlugin(server) {
|
|
|
236
244
|
}
|
|
237
245
|
});
|
|
238
246
|
server.statusHandlers['DELETED-STORAGE'] = (connection, mailbox) => totalSize(mailbox.messages.filter(message => message.flags.indexOf('\\Deleted') >= 0));
|
|
247
|
+
// Control API (README "Control API"): the quota root with its limits and usage, STORAGE in units of 1024 octets
|
|
248
|
+
const getQuotaOperation = () => {
|
|
249
|
+
const usage = getUsage();
|
|
250
|
+
return {
|
|
251
|
+
root: root.name,
|
|
252
|
+
limits: Object.assign({}, root.limits),
|
|
253
|
+
usage: { STORAGE: Math.ceil(usage.bytes / 1024), MESSAGE: usage.MESSAGE, MAILBOX: usage.MAILBOX }
|
|
254
|
+
};
|
|
255
|
+
};
|
|
256
|
+
// the new limits replace all earlier ones, like SETQUOTA (RFC 9208 section 4.1.3)
|
|
257
|
+
const setQuotaOperation = (limits) => {
|
|
258
|
+
if (!limits || typeof limits !== 'object' || Array.isArray(limits)) {
|
|
259
|
+
throw storeError('setQuota expects an object of resource limits, e.g. { STORAGE: 1024 }', 'INVALID');
|
|
260
|
+
}
|
|
261
|
+
const next = {};
|
|
262
|
+
Object.keys(limits).forEach(name => {
|
|
263
|
+
const resource = name.toUpperCase();
|
|
264
|
+
const value = limits[name];
|
|
265
|
+
if (RESOURCES.indexOf(resource) < 0) {
|
|
266
|
+
throw storeError('Unknown quota resource ' + name + ', expected ' + RESOURCES.join(', '), 'INVALID');
|
|
267
|
+
}
|
|
268
|
+
if (typeof value !== 'number' || !Number.isSafeInteger(value) || value < 0) {
|
|
269
|
+
throw storeError('Limit of ' + resource + ' must be a non-negative integer', 'INVALID');
|
|
270
|
+
}
|
|
271
|
+
next[resource] = value;
|
|
272
|
+
});
|
|
273
|
+
root.limits = next;
|
|
274
|
+
return getQuotaOperation();
|
|
275
|
+
};
|
|
276
|
+
server.control.register('getQuota', getQuotaOperation, [
|
|
277
|
+
{ method: 'GET', path: '/v1/quota', summary: 'Quota root, limits and usage (QUOTA plugin)', handler: () => server.control.getQuota() }
|
|
278
|
+
]);
|
|
279
|
+
server.control.register('setQuota', setQuotaOperation, [
|
|
280
|
+
{
|
|
281
|
+
method: 'PUT',
|
|
282
|
+
path: '/v1/quota',
|
|
283
|
+
summary: 'Replaces the quota limits ({ STORAGE, MESSAGE, MAILBOX }, QUOTA plugin)',
|
|
284
|
+
handler: ({ body }) => server.control.setQuota(body)
|
|
285
|
+
}
|
|
286
|
+
]);
|
|
239
287
|
}
|
|
@@ -25,7 +25,7 @@ export default function savedatePlugin(server) {
|
|
|
25
25
|
delete message.SAVEDATE;
|
|
26
26
|
return;
|
|
27
27
|
}
|
|
28
|
-
const savedate = server.normalizeDateTime(message.SAVEDATE ||
|
|
28
|
+
const savedate = server.normalizeDateTime(message.SAVEDATE || server.now());
|
|
29
29
|
if (!server.validateInternalDate(savedate)) {
|
|
30
30
|
throw new Error('Invalid SAVEDATE value ' + JSON.stringify(savedate) + ' in mailbox ' + mailbox.path);
|
|
31
31
|
}
|
|
@@ -1,10 +1,2 @@
|
|
|
1
1
|
import type { IMAPServer } from '../types.js';
|
|
2
|
-
/**
|
|
3
|
-
* @help Enables SPECIAL-USE [RFC6154] capability
|
|
4
|
-
* @help Mailboxes need to have a "special-use"
|
|
5
|
-
* @help property (String or Array) that will be used
|
|
6
|
-
* @help as extra flag for LIST and LSUB responses.
|
|
7
|
-
* @help With LIST-EXTENDED the SPECIAL-USE selection and
|
|
8
|
-
* @help return options work with the other LIST options
|
|
9
|
-
*/
|
|
10
2
|
export default function specialUsePlugin(server: IMAPServer): void;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { getListExtensions } from '../list-extensions.js';
|
|
2
|
+
import { storeError } from '../store-operations.js';
|
|
2
3
|
/**
|
|
3
4
|
* @help Enables SPECIAL-USE [RFC6154] capability
|
|
4
5
|
* @help Mailboxes need to have a "special-use"
|
|
@@ -7,6 +8,8 @@ import { getListExtensions } from '../list-extensions.js';
|
|
|
7
8
|
* @help With LIST-EXTENDED the SPECIAL-USE selection and
|
|
8
9
|
* @help return options work with the other LIST options
|
|
9
10
|
*/
|
|
11
|
+
// RFC 6154 section 2 and RFC 8457 (\\Important)
|
|
12
|
+
const SPECIAL_USES = new Set(['\\All', '\\Archive', '\\Drafts', '\\Flagged', '\\Junk', '\\Sent', '\\Trash', '\\Important']);
|
|
10
13
|
export default function specialUsePlugin(server) {
|
|
11
14
|
// Register capability
|
|
12
15
|
server.registerCapability('SPECIAL-USE');
|
|
@@ -77,4 +80,24 @@ export default function specialUsePlugin(server) {
|
|
|
77
80
|
}
|
|
78
81
|
}
|
|
79
82
|
});
|
|
83
|
+
// Control API (README "Control API"): the special-use attributes of a mailbox (RFC 6154 section 2)
|
|
84
|
+
server.control.mailboxInfoHandlers.push((mailbox, info) => {
|
|
85
|
+
info.specialUse = getSpecialUse(mailbox);
|
|
86
|
+
});
|
|
87
|
+
const setSpecialUseOperation = (path, uses) => {
|
|
88
|
+
const mailbox = server.control.requireMailbox(path, true);
|
|
89
|
+
if (!Array.isArray(uses) || uses.some(use => typeof use !== 'string' || !SPECIAL_USES.has(use))) {
|
|
90
|
+
throw storeError('Special-use attributes must be a list of ' + [...SPECIAL_USES].join(', '), 'INVALID');
|
|
91
|
+
}
|
|
92
|
+
mailbox['special-use'] = [...new Set(uses)];
|
|
93
|
+
return server.control.getMailbox(mailbox.path);
|
|
94
|
+
};
|
|
95
|
+
server.control.register('setSpecialUse', setSpecialUseOperation, [
|
|
96
|
+
{
|
|
97
|
+
method: 'PUT',
|
|
98
|
+
path: '/v1/mailboxes/{path}/special-use',
|
|
99
|
+
summary: 'Sets the special-use attributes of a mailbox ({ specialUse: ["\\Sent"] }, SPECIAL-USE plugin)',
|
|
100
|
+
handler: ({ params, body }) => server.control.setSpecialUse(params.path, body.specialUse)
|
|
101
|
+
}
|
|
102
|
+
]);
|
|
80
103
|
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Quirk presets (#87): named sets of script rules (and plugins to leave out) that make the server behave like a known
|
|
3
|
+
* real server, so a client test reproduces the bug of that server in every run, without the server itself. The
|
|
4
|
+
* `quirks` option turns them on, the rules come after the rules of the `script` option. The probabilistic ones use
|
|
5
|
+
* the random numbers of the `scriptSeed` option, so a run can be repeated. The presets are exported as data, copy
|
|
6
|
+
* and adjust one as script rules when it does not fit
|
|
7
|
+
*/
|
|
8
|
+
import type { ScriptRule } from './script.js';
|
|
9
|
+
/** A quirk preset */
|
|
10
|
+
export interface Quirk {
|
|
11
|
+
description: string;
|
|
12
|
+
/** script rules, see README "Scripted faults" */
|
|
13
|
+
rules?: ScriptRule[] | undefined;
|
|
14
|
+
/** plugins that are not loaded even when the `plugins` option lists them */
|
|
15
|
+
removePlugins?: string[] | undefined;
|
|
16
|
+
}
|
|
17
|
+
export declare const quirks: Record<string, Quirk>;
|
|
18
|
+
/**
|
|
19
|
+
* Resolves the `quirks` option
|
|
20
|
+
*
|
|
21
|
+
* @param {Array|String} names Quirk names
|
|
22
|
+
* @return {Object} `{ rules, removePlugins }` of all of them, removePlugins maps a plugin name to the quirk that removes it
|
|
23
|
+
* @throws {Error} for an unknown name
|
|
24
|
+
*/
|
|
25
|
+
export declare function resolveQuirks(names: string[] | string | null | undefined): {
|
|
26
|
+
rules: ScriptRule[];
|
|
27
|
+
removePlugins: Map<string, string>;
|
|
28
|
+
};
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Quirk presets (#87): named sets of script rules (and plugins to leave out) that make the server behave like a known
|
|
3
|
+
* real server, so a client test reproduces the bug of that server in every run, without the server itself. The
|
|
4
|
+
* `quirks` option turns them on, the rules come after the rules of the `script` option. The probabilistic ones use
|
|
5
|
+
* the random numbers of the `scriptSeed` option, so a run can be repeated. The presets are exported as data, copy
|
|
6
|
+
* and adjust one as script rules when it does not fit
|
|
7
|
+
*/
|
|
8
|
+
const FETCH_COMMANDS = ['FETCH', 'UID FETCH'];
|
|
9
|
+
/**
|
|
10
|
+
* The part a body section belongs to: "2.MIME", "2.HEADER" and "2" all belong to part 2, "HEADER" and "TEXT" to the
|
|
11
|
+
* message itself
|
|
12
|
+
*
|
|
13
|
+
* @param {Array} section Section of a BODY item, its first entry is the section spec
|
|
14
|
+
* @return {String} part path, "" for the message
|
|
15
|
+
*/
|
|
16
|
+
function partPath(section) {
|
|
17
|
+
const spec = String((section[0] && section[0].value) || '').toUpperCase();
|
|
18
|
+
return spec.replace(/(^|\.)(MIME|HEADER(\.FIELDS(\.NOT)?)?|TEXT)$/, '');
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Changes the BODY[...] values of an untagged FETCH response
|
|
22
|
+
*
|
|
23
|
+
* @param {Object} response Untagged FETCH response
|
|
24
|
+
* @param {Function} change `(name, value, index)` returns the new value
|
|
25
|
+
* @return {Object|undefined} the changed response, nothing for other responses
|
|
26
|
+
*/
|
|
27
|
+
function changeBodyValues(response, change) {
|
|
28
|
+
const items = response.attributes && response.attributes[2];
|
|
29
|
+
if (!Array.isArray(items) || !response.attributes[1] || response.attributes[1].value !== 'FETCH') {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
const changed = items.slice();
|
|
33
|
+
for (let i = 0; i < changed.length - 1; i += 2) {
|
|
34
|
+
const name = changed[i];
|
|
35
|
+
if (name && name.type === 'ATOM' && String(name.value).toUpperCase() === 'BODY' && Array.isArray(name.section)) {
|
|
36
|
+
changed[i + 1] = change(name, changed[i + 1]);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return Object.assign({}, response, { attributes: [response.attributes[0], response.attributes[1], changed] });
|
|
40
|
+
}
|
|
41
|
+
export const quirks = {
|
|
42
|
+
'james-fetchgroup': {
|
|
43
|
+
description: 'Apache James FetchGroup: only the first section asked for a part in one FETCH is answered, later ones for the same ' +
|
|
44
|
+
'part come back empty. BODY[2.MIME] BODY[2] gives a zero-length body, the reverse order loses the headers',
|
|
45
|
+
rules: [
|
|
46
|
+
{
|
|
47
|
+
on: 'response',
|
|
48
|
+
command: FETCH_COMMANDS,
|
|
49
|
+
untagged: true,
|
|
50
|
+
mutate: response => {
|
|
51
|
+
const seen = new Set();
|
|
52
|
+
return changeBodyValues(response, (name, value) => {
|
|
53
|
+
const path = partPath(name.section);
|
|
54
|
+
if (seen.has(path)) {
|
|
55
|
+
return { type: 'LITERAL', value: '' };
|
|
56
|
+
}
|
|
57
|
+
seen.add(path);
|
|
58
|
+
return value;
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
]
|
|
63
|
+
},
|
|
64
|
+
'james-late-fetch': {
|
|
65
|
+
description: 'Apache James: now and then (1 in 4) a FETCH response comes after the tagged OK of its command',
|
|
66
|
+
rules: [{ on: 'response', command: FETCH_COMMANDS, untagged: true, chance: 0.25, defer: 'tagged' }]
|
|
67
|
+
},
|
|
68
|
+
'yahoo-quoted-sections': {
|
|
69
|
+
description: 'Yahoo: short body sections (up to 100 octets without line breaks) are quoted strings instead of literals',
|
|
70
|
+
rules: [
|
|
71
|
+
{
|
|
72
|
+
on: 'response',
|
|
73
|
+
command: FETCH_COMMANDS,
|
|
74
|
+
untagged: true,
|
|
75
|
+
mutate: response => changeBodyValues(response, (name, value) => value && value.type === 'LITERAL' && typeof value.value === 'string' && value.value.length <= 100 && /^[\x20-\x7e]*$/.test(value.value)
|
|
76
|
+
? { type: 'STRING', value: value.value }
|
|
77
|
+
: value)
|
|
78
|
+
}
|
|
79
|
+
]
|
|
80
|
+
},
|
|
81
|
+
'm365-throttle': {
|
|
82
|
+
description: 'Microsoft 365: 1 in 10 commands (not LOGOUT) is refused with "BAD Request is throttled. Suggested Backoff Time: 1000 milliseconds"',
|
|
83
|
+
rules: [
|
|
84
|
+
{
|
|
85
|
+
on: 'command',
|
|
86
|
+
chance: 0.1,
|
|
87
|
+
when: context => context.command !== 'LOGOUT',
|
|
88
|
+
send: '$TAG BAD Request is throttled. Suggested Backoff Time: 1000 milliseconds\r\n'
|
|
89
|
+
}
|
|
90
|
+
]
|
|
91
|
+
},
|
|
92
|
+
'no-uidplus': {
|
|
93
|
+
description: 'A server without UIDPLUS: no APPENDUID, COPYUID or UID EXPUNGE, even when the plugins list it',
|
|
94
|
+
removePlugins: ['UIDPLUS']
|
|
95
|
+
},
|
|
96
|
+
'no-move': {
|
|
97
|
+
description: 'A server without MOVE, clients have to COPY, STORE \\Deleted and EXPUNGE',
|
|
98
|
+
removePlugins: ['MOVE']
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* Resolves the `quirks` option
|
|
103
|
+
*
|
|
104
|
+
* @param {Array|String} names Quirk names
|
|
105
|
+
* @return {Object} `{ rules, removePlugins }` of all of them, removePlugins maps a plugin name to the quirk that removes it
|
|
106
|
+
* @throws {Error} for an unknown name
|
|
107
|
+
*/
|
|
108
|
+
export function resolveQuirks(names) {
|
|
109
|
+
const rules = [];
|
|
110
|
+
const removePlugins = new Map();
|
|
111
|
+
[].concat(names || []).forEach(name => {
|
|
112
|
+
const key = String(name).toLowerCase();
|
|
113
|
+
const quirk = Object.hasOwn(quirks, key) ? quirks[key] : null;
|
|
114
|
+
if (!quirk) {
|
|
115
|
+
throw new Error('Unknown quirk "' + name + '". Available quirks: ' + Object.keys(quirks).join(', '));
|
|
116
|
+
}
|
|
117
|
+
rules.push(...(quirk.rules || []));
|
|
118
|
+
(quirk.removePlugins || []).forEach(plugin => removePlugins.set(plugin, key));
|
|
119
|
+
});
|
|
120
|
+
return { rules, removePlugins };
|
|
121
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Seeded random numbers, so that randomized test behavior (UID shuffles, script rule chances) can be repeated
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* A small seeded random number generator (mulberry32), for repeatable "shuffle" orders
|
|
6
|
+
*
|
|
7
|
+
* @param {Number} seed Seed
|
|
8
|
+
* @return {Function} returns numbers from 0 (inclusive) to 1 (exclusive)
|
|
9
|
+
*/
|
|
10
|
+
export declare function seededRandom(seed: number): () => number;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Seeded random numbers, so that randomized test behavior (UID shuffles, script rule chances) can be repeated
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* A small seeded random number generator (mulberry32), for repeatable "shuffle" orders
|
|
6
|
+
*
|
|
7
|
+
* @param {Number} seed Seed
|
|
8
|
+
* @return {Function} returns numbers from 0 (inclusive) to 1 (exclusive)
|
|
9
|
+
*/
|
|
10
|
+
export function seededRandom(seed) {
|
|
11
|
+
let state = seed >>> 0;
|
|
12
|
+
return () => {
|
|
13
|
+
state = (state + 0x6d2b79f5) >>> 0;
|
|
14
|
+
let t = state;
|
|
15
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
16
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
17
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The REST API: the control API over HTTP, for tests written in any language. Off unless the `rest` option (or
|
|
3
|
+
* `--rest-port`) turns it on. It is full control over the server, including the mail of every user, so it listens
|
|
4
|
+
* on 127.0.0.1 by default, needs a bearer token on any other address, sends no CORS headers, takes JSON bodies
|
|
5
|
+
* only (a browser can not send them cross-origin without a CORS preflight) and, without a token, only answers
|
|
6
|
+
* requests for a loopback host name (DNS rebinding). Built on node:http only.
|
|
7
|
+
*/
|
|
8
|
+
import http from 'node:http';
|
|
9
|
+
import type { IMAPServer } from './types.js';
|
|
10
|
+
import type { ControlRoute as Route } from './control.js';
|
|
11
|
+
/** The HTTP server of the REST API, `endStreams()` ends the open event streams */
|
|
12
|
+
type RestServer = http.Server & {
|
|
13
|
+
endStreams?: () => void;
|
|
14
|
+
};
|
|
15
|
+
/** Options of the REST API, the `rest` server option */
|
|
16
|
+
interface RestOptions {
|
|
17
|
+
port?: number | undefined;
|
|
18
|
+
/** address to listen on, 127.0.0.1 by default. Any other than a loopback address needs a token */
|
|
19
|
+
host?: string | undefined;
|
|
20
|
+
/** bearer token that every request must send in `Authorization: Bearer <token>` */
|
|
21
|
+
token?: string | undefined;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Checks if a host name or address is a loopback one
|
|
25
|
+
*
|
|
26
|
+
* @param {String} host Host name or address, can be in brackets
|
|
27
|
+
* @return {Boolean} true for localhost, 127.0.0.0/8 and ::1
|
|
28
|
+
*/
|
|
29
|
+
declare function isLoopback(host: string): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Builds the route table of a server
|
|
32
|
+
*
|
|
33
|
+
* @param {Object} server IMAP server
|
|
34
|
+
* @return {Array} routes
|
|
35
|
+
*/
|
|
36
|
+
declare function getRoutes(server: IMAPServer, streams?: Set<http.ServerResponse>): Route[];
|
|
37
|
+
/**
|
|
38
|
+
* Describes the routes as an OpenAPI 3.1 document
|
|
39
|
+
*
|
|
40
|
+
* @param {Array} routes Route table
|
|
41
|
+
* @return {Object} OpenAPI document
|
|
42
|
+
*/
|
|
43
|
+
declare function openApi(routes: Route[]): Record<string, unknown>;
|
|
44
|
+
/**
|
|
45
|
+
* Creates the HTTP server of the REST API, it does not listen yet
|
|
46
|
+
*
|
|
47
|
+
* @param {Object} server IMAP server
|
|
48
|
+
* @param {Object} options `{ host, token }`
|
|
49
|
+
* @return {Object} HTTP server
|
|
50
|
+
* @throws {Error} for a host that is not a loopback address without a token
|
|
51
|
+
*/
|
|
52
|
+
declare function createRestServer(server: IMAPServer, options: RestOptions): RestServer;
|
|
53
|
+
export { createRestServer, isLoopback, getRoutes, openApi };
|
|
54
|
+
export type { RestOptions, RestServer };
|