emailengine-app 2.84.1 → 2.85.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/.dockerignore +6 -0
- package/.ncurc.js +5 -3
- package/.prettierignore +5 -1
- package/.prettierrc.js +2 -0
- package/.release-please-manifest.json +1 -1
- package/CHANGELOG.md +61 -0
- package/Dockerfile +8 -2
- package/README.md +1 -1
- package/bin/emailengine.js +444 -403
- package/copy-static-files.sh +39 -14
- package/data/google-crawlers.json +1 -1
- package/data/public_suffix_list.dat +16562 -0
- package/docker-compose.yml +1 -1
- package/eslint.config.js +81 -99
- package/lib/account/own-account-key.js +42 -0
- package/lib/account-assignment.js +1 -33
- package/lib/account.js +68 -24
- package/lib/add-trackers.js +11 -4
- package/lib/api-reference/format.js +4 -3
- package/lib/api-routes/account-routes.js +244 -26
- package/lib/api-routes/blocklist-routes.js +23 -2
- package/lib/api-routes/delivery-test-routes.js +26 -3
- package/lib/api-routes/export-routes.js +28 -2
- package/lib/api-routes/gateway-routes.js +37 -5
- package/lib/api-routes/mailbox-routes.js +41 -2
- package/lib/api-routes/message-routes.js +104 -4
- package/lib/api-routes/mint-bounds.js +11 -0
- package/lib/api-routes/oauth2-app-routes.js +48 -15
- package/lib/api-routes/outbox-routes.js +45 -11
- package/lib/api-routes/permission-map.js +12 -1
- package/lib/api-routes/route-helpers.js +125 -3
- package/lib/api-routes/settings-routes.js +23 -2
- package/lib/api-routes/submit-routes.js +338 -309
- package/lib/api-routes/template-routes.js +38 -2
- package/lib/api-routes/token-routes.js +142 -96
- package/lib/api-routes/webhook-route-routes.js +13 -2
- package/lib/auth-token.js +23 -10
- package/lib/bounce-detect.js +414 -210
- package/lib/consts.js +39 -4
- package/lib/create-imap-client.js +41 -2
- package/lib/email-client/base-client.js +30 -41
- package/lib/email-client/gmail-client.js +30 -18
- package/lib/email-client/imap/mailbox.js +13 -23
- package/lib/email-client/imap/response-metrics.js +75 -0
- package/lib/email-client/imap/subconnection.js +1 -1
- package/lib/email-client/imap-client.js +6 -3
- package/lib/email-client/message-builder.js +7 -2
- package/lib/email-client/notification-handler.js +1 -2
- package/lib/email-client/outlook/graph-api.js +17 -2
- package/lib/email-client/outlook-client.js +6 -3
- package/lib/email-client/smtp-pool-manager.js +6 -0
- package/lib/encrypt.js +9 -0
- package/lib/export.js +3 -1
- package/lib/gateway.js +2 -3
- package/lib/get-raw-email.js +4 -2
- package/lib/hapi-request-logger.js +107 -0
- package/lib/imap-proxy-auth.js +14 -1
- package/lib/imapproxy/imap-core/README.md +6 -5
- package/lib/imapproxy/imap-core/lib/commands/authenticate-plain.js +1 -0
- package/lib/imapproxy/imap-core/lib/commands/login.js +1 -0
- package/lib/imapproxy/imap-core/lib/imap-command.js +86 -43
- package/lib/imapproxy/imap-core/lib/imap-stream.js +58 -13
- package/lib/imapproxy/imap-core/lib/indexer/indexer.js +3 -1
- package/lib/mcp/listen.js +10 -1
- package/lib/mcp/tools.js +37 -8
- package/lib/notify-attachments.js +17 -0
- package/lib/oauth/external-account-config.js +3 -1
- package/lib/oauth/gmail.js +21 -48
- package/lib/oauth/mail-ru.js +5 -38
- package/lib/oauth/outlook-mailbox.js +35 -0
- package/lib/oauth/outlook.js +26 -44
- package/lib/oauth/state.js +24 -0
- package/lib/oauth2-apps.js +1 -1
- package/lib/pbkdf2-phc.js +19 -2
- package/lib/privileged-settings.js +6 -3
- package/lib/redis-scan-delete.js +15 -1
- package/lib/schemas.js +79 -33
- package/lib/settings.js +13 -1
- package/lib/smtp-auth.js +23 -0
- package/lib/sso.js +1 -1
- package/lib/swagger-options.js +1 -1
- package/lib/thread-diagnostics.js +46 -0
- package/lib/timezones.js +109 -0
- package/lib/token-permission-view.js +3 -3
- package/lib/tools.js +285 -66
- package/lib/totp.js +2 -5
- package/lib/translations.js +1 -2
- package/lib/ui-routes/account-routes.js +491 -491
- package/lib/ui-routes/account-setup.js +99 -0
- package/lib/ui-routes/admin-config-routes.js +98 -48
- package/lib/ui-routes/admin-entities-routes.js +113 -31
- package/lib/ui-routes/auth-routes.js +137 -62
- package/lib/ui-routes/connection-security-form.js +74 -0
- package/lib/ui-routes/form-lists.js +46 -0
- package/lib/ui-routes/internals-routes.js +5 -0
- package/lib/ui-routes/mcp-consent-routes.js +5 -1
- package/lib/ui-routes/oauth-config-routes.js +133 -100
- package/lib/ui-routes/route-helpers.js +19 -3
- package/lib/ui-routes/sign-in-helpers.js +59 -0
- package/lib/ui-routes/tls-config-routes.js +58 -15
- package/lib/ui-routes/unsubscribe-routes.js +6 -3
- package/lib/utils/back-off.js +82 -0
- package/lib/utils/map-concurrent.js +36 -0
- package/lib/utils/murmurhash3.js +66 -0
- package/lib/utils/network.js +28 -0
- package/lib/utils/provider-id.js +27 -0
- package/lib/utils/public-suffix.js +122 -0
- package/lib/utils/random-id.js +26 -0
- package/lib/utils/require-tls.js +33 -0
- package/lib/utils/version.js +74 -0
- package/lib/web-safe-html.js +5 -6
- package/lib/webhook-penalty.js +75 -0
- package/lib/webhook-routing.js +49 -2
- package/lib/webhooks.js +41 -6
- package/package.json +37 -39
- package/sbom.json +7980 -1
- package/server.js +659 -455
- package/static/js/ace/ace.js +1 -1
- package/static/js/ace/mode-handlebars.js +1 -1
- package/static/js/ace/mode-html.js +1 -1
- package/static/js/ace/mode-javascript.js +1 -1
- package/static/js/ace/mode-markdown.js +1 -1
- package/static/js/ace/worker-html.js +1 -1
- package/static/js/ace/worker-javascript.js +1 -1
- package/static/js/ace/worker-json.js +1 -1
- package/static/js/ee-client.js +5 -0
- package/static/licenses.html +191 -1016
- package/translations/messages.pot +70 -71
- package/views/account/security.hbs +10 -0
- package/views/accounts/account.hbs +11 -11
- package/views/accounts/edit.hbs +6 -2
- package/views/config/ai.hbs +1 -1
- package/views/config/security.hbs +2 -2
- package/views/config/service.hbs +3 -0
- package/views/internals/index.hbs +8 -0
- package/views/partials/gateway_form.hbs +3 -1
- package/views/partials/webhook_form.hbs +1 -1
- package/views/suppression-lists/index.hbs +1 -1
- package/workers/api.js +780 -585
- package/workers/export.js +1 -1
- package/workers/submit.js +1 -1
- package/workers/webhooks.js +54 -34
- package/config/test.toml +0 -46
- package/install.sh +0 -428
- package/setup-production.sh +0 -215
- package/systemd/emailengine.service +0 -97
- package/systemd/nginx-proxy.conf +0 -77
package/lib/bounce-detect.js
CHANGED
|
@@ -38,6 +38,12 @@ const DELAY_NOTICE_RE =
|
|
|
38
38
|
// "I'm not going to try again"), so a 4.x.x status on its own does not make a notice a delay
|
|
39
39
|
const GAVE_UP_RE = /permanent error|given up|not going to try again|retry timeout exceeded|will not be retried|could not be delivered/i;
|
|
40
40
|
|
|
41
|
+
// The first run of non-whitespace after a marker line, where a bounce names the recipient. A
|
|
42
|
+
// newline is whitespace, so one `\s*` is the whole skip; the `\s*\n*\s*` it replaces was three
|
|
43
|
+
// overlapping quantifiers whose combinations grew with the fourth power of a newline run. Anchored,
|
|
44
|
+
// so a text that ends in whitespace fails once rather than once per start position.
|
|
45
|
+
const NEXT_TOKEN = /^\s*(\S+)/;
|
|
46
|
+
|
|
41
47
|
/**
|
|
42
48
|
* Tells whether a notice the text heuristics matched reports a delay rather than a failure
|
|
43
49
|
*
|
|
@@ -307,54 +313,65 @@ function parseDeliveryHeaders(entries) {
|
|
|
307
313
|
}
|
|
308
314
|
|
|
309
315
|
/**
|
|
310
|
-
*
|
|
316
|
+
* Finds the first attachment of a content type, compared case-insensitively per RFC 2045
|
|
311
317
|
*
|
|
312
|
-
* @
|
|
313
|
-
* @param {
|
|
314
|
-
* @
|
|
315
|
-
* @returns {Promise<Object>} Bounce detection results
|
|
316
|
-
* @returns {string} [returns.recipient] - Failed recipient email address
|
|
317
|
-
* @returns {string} [returns.action] - Bounce action (typically 'failed')
|
|
318
|
-
* @returns {Object} [returns.response] - SMTP response details
|
|
319
|
-
* @returns {string} [returns.response.message] - The error message from the remote server
|
|
320
|
-
* @returns {string} [returns.response.status] - Enhanced status code (X.X.X format)
|
|
321
|
-
* @returns {string} [returns.response.source] - Source of the diagnostic code
|
|
322
|
-
* @returns {string} [returns.mta] - Mail server that reported the failure
|
|
323
|
-
* @returns {string} [returns.queueId] - Queue ID from the sending MTA
|
|
324
|
-
* @returns {string} [returns.messageId] - Original message ID that bounced
|
|
325
|
-
* @returns {Object} [returns.messageHeaders] - Headers from the original message
|
|
318
|
+
* @param {Object[]} attachments - Parsed attachments
|
|
319
|
+
* @param {string} contentType - Lowercase content type to look for
|
|
320
|
+
* @returns {Object|undefined} The first matching attachment
|
|
326
321
|
*/
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
return {};
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
let parsed;
|
|
322
|
+
function findAttachmentByType(attachments, contentType) {
|
|
323
|
+
return attachments.find(attachment => attachment.contentType?.toLowerCase() === contentType);
|
|
324
|
+
}
|
|
334
325
|
|
|
326
|
+
/**
|
|
327
|
+
* Parses the bounce unless the caller already did
|
|
328
|
+
*
|
|
329
|
+
* @param {Stream|Buffer|string|Object} sourceStream - Email source, or an object carrying `parsed`
|
|
330
|
+
* @returns {Promise<Object>} Parsed message
|
|
331
|
+
*/
|
|
332
|
+
async function loadParsedMessage(sourceStream) {
|
|
335
333
|
// Use pre-parsed email if available to avoid parsing twice
|
|
336
334
|
if (sourceStream.parsed) {
|
|
337
|
-
|
|
338
|
-
} else {
|
|
339
|
-
// Parse the email, keeping delivery status attachments intact
|
|
340
|
-
parsed = await parseMessage(sourceStream, PARSER_OPTIONS);
|
|
335
|
+
return sourceStream.parsed;
|
|
341
336
|
}
|
|
337
|
+
// Parse the email, keeping delivery status attachments intact
|
|
338
|
+
return await parseMessage(sourceStream, PARSER_OPTIONS);
|
|
339
|
+
}
|
|
342
340
|
|
|
343
|
-
|
|
344
|
-
|
|
341
|
+
/**
|
|
342
|
+
* Builds the state one bounceDetect() run works on
|
|
343
|
+
*
|
|
344
|
+
* Every reader below updates this object rather than closing over shared variables: `result` is
|
|
345
|
+
* the detection result being filled in, `parsedMessageHeaders` the headers of the bounced message
|
|
346
|
+
* once some part has supplied them.
|
|
347
|
+
*
|
|
348
|
+
* @param {Object} parsed - Parsed bounce message
|
|
349
|
+
* @returns {Object} Detection context
|
|
350
|
+
*/
|
|
351
|
+
function createDetectContext(parsed) {
|
|
345
352
|
// Look for standard bounce attachments as defined in RFC 3464
|
|
346
|
-
//
|
|
353
|
+
// A message without attachments has no attachments array
|
|
347
354
|
const attachments = parsed.attachments || [];
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
355
|
+
return {
|
|
356
|
+
parsed,
|
|
357
|
+
attachments,
|
|
358
|
+
result: {},
|
|
359
|
+
deliveryStatus: findAttachmentByType(attachments, 'message/delivery-status'),
|
|
360
|
+
messageHeaders: findAttachmentByType(attachments, 'text/rfc822-headers'),
|
|
361
|
+
originalMessage: findAttachmentByType(attachments, 'message/rfc822'),
|
|
362
|
+
// Zoho Mail uses non-standard content type for original message
|
|
363
|
+
zohoOriginalMessage: findAttachmentByType(attachments, 'text/rfc822'),
|
|
364
|
+
parsedMessageHeaders: undefined
|
|
365
|
+
};
|
|
366
|
+
}
|
|
356
367
|
|
|
357
|
-
|
|
368
|
+
/**
|
|
369
|
+
* Handles the Exchange/Outlook specific header for failed recipients
|
|
370
|
+
*
|
|
371
|
+
* @param {Object} ctx - Detection context
|
|
372
|
+
*/
|
|
373
|
+
function readFailedRecipientsHeader(ctx) {
|
|
374
|
+
let { parsed, result } = ctx;
|
|
358
375
|
if (parsed.headers && parsed.headers.has('x-failed-recipients')) {
|
|
359
376
|
// Parse comma and space separated list of failed recipients
|
|
360
377
|
let list = []
|
|
@@ -369,9 +386,16 @@ async function bounceDetect(sourceStream) {
|
|
|
369
386
|
result.recipient = list[0];
|
|
370
387
|
}
|
|
371
388
|
}
|
|
389
|
+
}
|
|
372
390
|
|
|
373
|
-
|
|
374
|
-
|
|
391
|
+
/**
|
|
392
|
+
* Special handling for Amazon WorkMail bounces
|
|
393
|
+
* WorkMail doesn't use standard delivery-status attachments
|
|
394
|
+
*
|
|
395
|
+
* @param {Object} ctx - Detection context
|
|
396
|
+
*/
|
|
397
|
+
function readWorkMailReport(ctx) {
|
|
398
|
+
let { parsed } = ctx;
|
|
375
399
|
if (parsed.headers && parsed.headers.has('x-mailer')) {
|
|
376
400
|
let xMailer = []
|
|
377
401
|
.concat(parsed.headers.get('x-mailer') || '')
|
|
@@ -391,137 +415,187 @@ async function bounceDetect(sourceStream) {
|
|
|
391
415
|
if (splitPos >= 0) {
|
|
392
416
|
let bounceDetails = parsed.text.substr(splitPos + splitMarker.length).trim();
|
|
393
417
|
// Parse the technical report section as if it were a delivery-status
|
|
394
|
-
|
|
418
|
+
Object.assign(ctx.result, parseDeliveryStatus(bounceDetails) || {});
|
|
395
419
|
}
|
|
396
420
|
}
|
|
397
421
|
}
|
|
422
|
+
}
|
|
398
423
|
|
|
424
|
+
/**
|
|
425
|
+
* Processes the standard delivery-status attachment and the RFC822 headers attachment
|
|
426
|
+
* (the latter contains headers of the original message)
|
|
427
|
+
*
|
|
428
|
+
* @param {Object} ctx - Detection context
|
|
429
|
+
*/
|
|
430
|
+
function readReportParts(ctx) {
|
|
399
431
|
// Process standard delivery-status attachment
|
|
400
|
-
if (deliveryStatus) {
|
|
401
|
-
|
|
432
|
+
if (ctx.deliveryStatus) {
|
|
433
|
+
Object.assign(ctx.result, parseDeliveryStatus(ctx.deliveryStatus.content) || {});
|
|
402
434
|
}
|
|
403
435
|
|
|
404
436
|
// Process RFC822 headers attachment (contains headers of the original message)
|
|
405
|
-
if (messageHeaders) {
|
|
406
|
-
parsedMessageHeaders = decodeHeaders(messageHeaders.content);
|
|
407
|
-
|
|
437
|
+
if (ctx.messageHeaders) {
|
|
438
|
+
ctx.parsedMessageHeaders = decodeHeaders(ctx.messageHeaders.content);
|
|
439
|
+
Object.assign(ctx.result, parseDeliveryHeaders(ctx.parsedMessageHeaders));
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* Builds a headers object from headerLines for consistent access
|
|
445
|
+
*
|
|
446
|
+
* @param {Object} parsedOriginal - Parsed original message
|
|
447
|
+
* @returns {Object} Header keys mapped to arrays of raw values
|
|
448
|
+
*/
|
|
449
|
+
function collectOriginalHeaders(parsedOriginal) {
|
|
450
|
+
let headers = {};
|
|
451
|
+
|
|
452
|
+
if (parsedOriginal.headerLines && Array.isArray(parsedOriginal.headerLines)) {
|
|
453
|
+
parsedOriginal.headerLines.forEach(entry => {
|
|
454
|
+
// Preserve the original header value (after the key and colon).
|
|
455
|
+
// pushHeaderValue() skips names that resolve to an Object.prototype member,
|
|
456
|
+
// which a bare `!headers[key]` probe would have pushed onto, throwing out of
|
|
457
|
+
// the whole block and losing every header of the bounced message
|
|
458
|
+
pushHeaderValue(headers, entry.key, entry.line.substring(entry.key.length + 1).trim());
|
|
459
|
+
});
|
|
408
460
|
}
|
|
409
461
|
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
try {
|
|
413
|
-
// Parse the original message to extract its headers
|
|
414
|
-
let parsedOriginal = await parseMessage(originalMessage.content, PARSER_OPTIONS);
|
|
415
|
-
if (parsedOriginal) {
|
|
416
|
-
// Build headers object from headerLines for consistent access
|
|
417
|
-
let headers = {};
|
|
418
|
-
|
|
419
|
-
if (parsedOriginal.headerLines && Array.isArray(parsedOriginal.headerLines)) {
|
|
420
|
-
parsedOriginal.headerLines.forEach(entry => {
|
|
421
|
-
// Preserve the original header value (after the key and colon).
|
|
422
|
-
// pushHeaderValue() skips names that resolve to an Object.prototype member,
|
|
423
|
-
// which a bare `!headers[key]` probe would have pushed onto, throwing out of
|
|
424
|
-
// the whole block and losing every header of the bounced message
|
|
425
|
-
pushHeaderValue(headers, entry.key, entry.line.substring(entry.key.length + 1).trim());
|
|
426
|
-
});
|
|
427
|
-
}
|
|
462
|
+
return headers;
|
|
463
|
+
}
|
|
428
464
|
|
|
429
|
-
|
|
465
|
+
/**
|
|
466
|
+
* Applies a parsed original message attachment to the detection context
|
|
467
|
+
*
|
|
468
|
+
* @param {Object} ctx - Detection context
|
|
469
|
+
* @param {Object} parsedOriginal - Parsed message/rfc822 attachment
|
|
470
|
+
*/
|
|
471
|
+
function applyOriginalMessage(ctx, parsedOriginal) {
|
|
472
|
+
ctx.parsedMessageHeaders = collectOriginalHeaders(parsedOriginal);
|
|
430
473
|
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
474
|
+
// Extract Message-ID if not already found
|
|
475
|
+
if (!ctx.result.messageId && parsedOriginal.messageId) {
|
|
476
|
+
ctx.result.messageId = parsedOriginal.messageId;
|
|
477
|
+
}
|
|
435
478
|
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
479
|
+
// Some bounces include delivery-status within the original message attachment
|
|
480
|
+
const originalAttachments = parsedOriginal.attachments || [];
|
|
481
|
+
if (!ctx.deliveryStatus) {
|
|
482
|
+
let deliveryStatus = findAttachmentByType(originalAttachments, 'message/delivery-status');
|
|
483
|
+
if (deliveryStatus) {
|
|
484
|
+
Object.assign(ctx.result, parseDeliveryStatus(deliveryStatus.content) || {});
|
|
485
|
+
}
|
|
486
|
+
}
|
|
444
487
|
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
} catch (E) {
|
|
455
|
-
// Failed to parse original message attachment
|
|
456
|
-
// Continue processing as this is not critical
|
|
457
|
-
// TODO: Consider logging this error for debugging
|
|
488
|
+
// Similarly for headers attachment
|
|
489
|
+
if (!ctx.messageHeaders) {
|
|
490
|
+
let messageHeaders = findAttachmentByType(originalAttachments, 'text/rfc822-headers');
|
|
491
|
+
if (messageHeaders) {
|
|
492
|
+
let headerEntries = decodeHeaders(messageHeaders.content);
|
|
493
|
+
Object.assign(ctx.result, parseDeliveryHeaders(headerEntries));
|
|
458
494
|
}
|
|
459
495
|
}
|
|
496
|
+
}
|
|
460
497
|
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
.toLowerCase()
|
|
476
|
-
: '';
|
|
477
|
-
// Check From header for Zoho domain
|
|
478
|
-
const fromHeader = parsed.headers.has('from')
|
|
479
|
-
? []
|
|
480
|
-
.concat(parsed.headers.get('from') || '')
|
|
481
|
-
.map(f => (f.address || f.text || f || '').toLowerCase())
|
|
482
|
-
.join(' ')
|
|
483
|
-
: '';
|
|
484
|
-
// Check User-Agent header
|
|
485
|
-
const userAgent = parsed.headers.has('user-agent')
|
|
486
|
-
? []
|
|
487
|
-
.concat(parsed.headers.get('user-agent') || '')
|
|
488
|
-
.join(' ')
|
|
489
|
-
.toLowerCase()
|
|
490
|
-
: '';
|
|
491
|
-
|
|
492
|
-
isZohoBounce = xMailer.includes('zoho') || fromHeader.includes('zoho') || userAgent.includes('zoho');
|
|
498
|
+
/**
|
|
499
|
+
* Processes the complete original message attachment
|
|
500
|
+
*
|
|
501
|
+
* @param {Object} ctx - Detection context
|
|
502
|
+
*/
|
|
503
|
+
async function readOriginalMessage(ctx) {
|
|
504
|
+
if (!ctx.originalMessage) {
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
507
|
+
try {
|
|
508
|
+
// Parse the original message to extract its headers
|
|
509
|
+
let parsedOriginal = await parseMessage(ctx.originalMessage.content, PARSER_OPTIONS);
|
|
510
|
+
if (parsedOriginal) {
|
|
511
|
+
applyOriginalMessage(ctx, parsedOriginal);
|
|
493
512
|
}
|
|
513
|
+
} catch (E) {
|
|
514
|
+
// Failed to parse original message attachment
|
|
515
|
+
// Continue processing as this is not critical
|
|
516
|
+
// TODO: Consider logging this error for debugging
|
|
517
|
+
}
|
|
518
|
+
}
|
|
494
519
|
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
520
|
+
/**
|
|
521
|
+
* Verifies a text/rfc822 attachment comes from an actual Zoho bounce to prevent false positives
|
|
522
|
+
*
|
|
523
|
+
* @param {Object} parsed - Parsed bounce message
|
|
524
|
+
* @returns {Boolean} True when the bounce headers carry a Zoho indicator
|
|
525
|
+
*/
|
|
526
|
+
function isZohoBounce(parsed) {
|
|
527
|
+
// Check for Zoho-specific indicators in the bounce email headers
|
|
528
|
+
if (!parsed.headers) {
|
|
529
|
+
return false;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
// Check X-Mailer header
|
|
533
|
+
const xMailer = parsed.headers.has('x-mailer')
|
|
534
|
+
? []
|
|
535
|
+
.concat(parsed.headers.get('x-mailer') || '')
|
|
536
|
+
.join(' ')
|
|
537
|
+
.toLowerCase()
|
|
538
|
+
: '';
|
|
539
|
+
// Check From header for Zoho domain
|
|
540
|
+
const fromHeader = parsed.headers.has('from')
|
|
541
|
+
? []
|
|
542
|
+
.concat(parsed.headers.get('from') || '')
|
|
543
|
+
.map(f => (f.address || f.text || f || '').toLowerCase())
|
|
544
|
+
.join(' ')
|
|
545
|
+
: '';
|
|
546
|
+
// Check User-Agent header
|
|
547
|
+
const userAgent = parsed.headers.has('user-agent')
|
|
548
|
+
? []
|
|
549
|
+
.concat(parsed.headers.get('user-agent') || '')
|
|
550
|
+
.join(' ')
|
|
551
|
+
.toLowerCase()
|
|
552
|
+
: '';
|
|
553
|
+
|
|
554
|
+
return xMailer.includes('zoho') || fromHeader.includes('zoho') || userAgent.includes('zoho');
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* Handles Zoho's non-standard bounce format
|
|
559
|
+
* Zoho uses text/rfc822 content type (instead of standard message/rfc822)
|
|
560
|
+
* and includes original message headers in a non-standard format
|
|
561
|
+
*
|
|
562
|
+
* @param {Object} ctx - Detection context
|
|
563
|
+
*/
|
|
564
|
+
async function readZohoOriginalMessage(ctx) {
|
|
565
|
+
if (!ctx.zohoOriginalMessage || !isZohoBounce(ctx.parsed)) {
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
568
|
+
try {
|
|
569
|
+
let parsedOriginal = await parseMessage(ctx.zohoOriginalMessage.content, PARSER_OPTIONS);
|
|
570
|
+
// Zoho sometimes includes headers in the text body with no actual header structure
|
|
571
|
+
if (parsedOriginal && parsedOriginal.text && (!parsedOriginal.headerLines || !parsedOriginal.headerLines.filter(h => h.key).length)) {
|
|
572
|
+
// Remove leading spaces that indicate header continuation
|
|
573
|
+
let headerContent = capHeaderBlock(parsedOriginal.text).replace(/^ (?=[^ ])/gm, '');
|
|
574
|
+
|
|
575
|
+
// Additional validation: content should look like email headers
|
|
576
|
+
// Check for common header patterns (Received:, Message-ID:, Date:, From:)
|
|
577
|
+
if (/^\s*(Received|Message-ID|Date|From):/im.test(headerContent)) {
|
|
578
|
+
let headers = decodeHeaders(headerContent);
|
|
579
|
+
if (headers && headers['message-id']) {
|
|
580
|
+
ctx.parsedMessageHeaders = headers;
|
|
581
|
+
Object.assign(ctx.result, parseDeliveryHeaders(headers));
|
|
512
582
|
}
|
|
513
|
-
} catch (E) {
|
|
514
|
-
// Failed to parse Zoho attachment
|
|
515
|
-
// Continue processing as this is not critical
|
|
516
583
|
}
|
|
517
584
|
}
|
|
585
|
+
} catch (E) {
|
|
586
|
+
// Failed to parse Zoho attachment
|
|
587
|
+
// Continue processing as this is not critical
|
|
518
588
|
}
|
|
589
|
+
}
|
|
519
590
|
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
591
|
+
/**
|
|
592
|
+
* Extracts the text content the pattern matching runs over
|
|
593
|
+
*
|
|
594
|
+
* @param {Object} parsed - Parsed bounce message
|
|
595
|
+
* @param {Object[]} attachments - Its attachments
|
|
596
|
+
* @returns {string} Text, capped in length
|
|
597
|
+
*/
|
|
598
|
+
function extractHeuristicText(parsed, attachments) {
|
|
525
599
|
let text = (parsed.text || '').toString();
|
|
526
600
|
|
|
527
601
|
// Some bounces put the content in an attachment with no content-type
|
|
@@ -539,9 +613,29 @@ async function bounceDetect(sourceStream) {
|
|
|
539
613
|
text = text.substring(0, MAX_TEXT_LENGTH);
|
|
540
614
|
}
|
|
541
615
|
|
|
542
|
-
|
|
543
|
-
|
|
616
|
+
return text;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
/**
|
|
620
|
+
* Tries to extract an enhanced status code (X.X.X) from an SMTP reply, unless one is already set
|
|
621
|
+
*
|
|
622
|
+
* @param {Object} response - Response object to fill in
|
|
623
|
+
* @param {string} message - SMTP reply text
|
|
624
|
+
*/
|
|
625
|
+
function fillEnhancedStatus(response, message) {
|
|
626
|
+
if (!response.status) {
|
|
627
|
+
let statusMatch = message.match(/[45]\d{2}[\s-](\d+\.\d+\.\d+)[\s-]/);
|
|
628
|
+
if (statusMatch && statusMatch[1]) {
|
|
629
|
+
response.status = statusMatch[1];
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
}
|
|
544
633
|
|
|
634
|
+
// Pattern matching for non-standard bounce formats. Each heuristic below only fills gaps the
|
|
635
|
+
// structured parts (and the heuristics before it) left, and they run in this order.
|
|
636
|
+
|
|
637
|
+
function matchHostSaid(result, text) {
|
|
638
|
+
// Only proceed if we haven't found the recipient or response message yet
|
|
545
639
|
if (!result.recipient || !result.response || !result.response.message) {
|
|
546
640
|
/*
|
|
547
641
|
* Match pattern like:
|
|
@@ -549,8 +643,12 @@ async function bounceDetect(sourceStream) {
|
|
|
549
643
|
* 550 5.1.1 No such user (in reply to RCPT TO command)
|
|
550
644
|
*/
|
|
551
645
|
// The address classes exclude '<' and whitespace and are bounded, so a run of '<' or of
|
|
552
|
-
// unterminated addresses can not make every start position scan to the end of the text
|
|
553
|
-
|
|
646
|
+
// unterminated addresses can not make every start position scan to the end of the text.
|
|
647
|
+
// The whitespace between the host and `said:` is matched by one quantifier: Postfix writes
|
|
648
|
+
// the address right after the name (`host mx.example.com[203.0.113.4] said:`), and the
|
|
649
|
+
// `\s*(?:\[...\])?\s+` that allowed a space there had two adjacent whitespace runs that
|
|
650
|
+
// split a long run of spaces every possible way (50 KB of spaces took about a second)
|
|
651
|
+
let m = text.match(/<([^<>@\s]{1,256}@[^<>\s]{1,256})>:\s*host\s+([a-z0-9._-]+)(?:\[([^\]\s]{1,256})\])?\s+said:/im);
|
|
554
652
|
if (m) {
|
|
555
653
|
// Find the end of the error message (double newline or end of string)
|
|
556
654
|
let end = text.substr(m.index + m[0].length).match(/\r?\n\r?\n|$/);
|
|
@@ -572,16 +670,13 @@ async function bounceDetect(sourceStream) {
|
|
|
572
670
|
}
|
|
573
671
|
|
|
574
672
|
// Try to extract enhanced status code from the message
|
|
575
|
-
|
|
576
|
-
let statusMatch = message.match(/[45]\d{2}[\s-](\d+\.\d+\.\d+)[\s-]/);
|
|
577
|
-
if (statusMatch && statusMatch[1]) {
|
|
578
|
-
result.response.status = statusMatch[1];
|
|
579
|
-
}
|
|
580
|
-
}
|
|
673
|
+
fillEnhancedStatus(result.response, message);
|
|
581
674
|
}
|
|
582
675
|
}
|
|
583
676
|
}
|
|
677
|
+
}
|
|
584
678
|
|
|
679
|
+
function matchDeliveryFailedRecipient(result, text) {
|
|
585
680
|
if (!result.recipient) {
|
|
586
681
|
/*
|
|
587
682
|
* Match pattern like:
|
|
@@ -591,14 +686,18 @@ async function bounceDetect(sourceStream) {
|
|
|
591
686
|
// Use bounded quantifier to prevent ReDoS with nested [\s\w]* patterns
|
|
592
687
|
let m = text.match(/Delivery[^:]{0,100}failed[^:]{0,50}:/im);
|
|
593
688
|
if (m) {
|
|
594
|
-
// Look for email address on the next line
|
|
595
|
-
|
|
689
|
+
// Look for email address on the next line. One whitespace quantifier: `\s*\n*\s*` was
|
|
690
|
+
// three overlapping ones (a newline is whitespace), and a few hundred newlines after
|
|
691
|
+
// the marker took seconds of backtracking
|
|
692
|
+
let addrMatch = text.substring(m.index + m[0].length).match(NEXT_TOKEN);
|
|
596
693
|
if (addrMatch && addrMatch[1].indexOf('@') >= 0) {
|
|
597
694
|
result.recipient = addrMatch[1].trim();
|
|
598
695
|
}
|
|
599
696
|
}
|
|
600
697
|
}
|
|
698
|
+
}
|
|
601
699
|
|
|
700
|
+
function matchAffectedRecipients(result, text) {
|
|
602
701
|
if (!result.recipient) {
|
|
603
702
|
/*
|
|
604
703
|
* Match pattern like:
|
|
@@ -607,13 +706,15 @@ async function bounceDetect(sourceStream) {
|
|
|
607
706
|
*/
|
|
608
707
|
let m = text.match(/The following recipients were affected:/im);
|
|
609
708
|
if (m) {
|
|
610
|
-
let addrMatch = text.substr(m.index + m[0].length).match(
|
|
709
|
+
let addrMatch = text.substr(m.index + m[0].length).match(NEXT_TOKEN);
|
|
611
710
|
if (addrMatch && addrMatch[1].indexOf('@') >= 0) {
|
|
612
711
|
result.recipient = addrMatch[1].trim();
|
|
613
712
|
}
|
|
614
713
|
}
|
|
615
714
|
}
|
|
715
|
+
}
|
|
616
716
|
|
|
717
|
+
function matchLeadingSmtpReply(result, text) {
|
|
617
718
|
if (!result.response || !result.response.message) {
|
|
618
719
|
/*
|
|
619
720
|
* Match SMTP response at the beginning of a line
|
|
@@ -638,17 +739,14 @@ async function bounceDetect(sourceStream) {
|
|
|
638
739
|
result.response.message = result.response.message || message;
|
|
639
740
|
|
|
640
741
|
// Extract enhanced status code
|
|
641
|
-
|
|
642
|
-
let statusMatch = message.match(/[45]\d{2}[\s-](\d+\.\d+\.\d+)[\s-]/);
|
|
643
|
-
if (statusMatch && statusMatch[1]) {
|
|
644
|
-
result.response.status = statusMatch[1];
|
|
645
|
-
}
|
|
646
|
-
}
|
|
742
|
+
fillEnhancedStatus(result.response, message);
|
|
647
743
|
}
|
|
648
744
|
}
|
|
649
745
|
}
|
|
746
|
+
}
|
|
650
747
|
|
|
651
|
-
|
|
748
|
+
// Another non-standard format
|
|
749
|
+
function matchRecipientBlock(result, text) {
|
|
652
750
|
if (!result.recipient || !result.response || !result.response.message) {
|
|
653
751
|
/*
|
|
654
752
|
* Match pattern like:
|
|
@@ -675,12 +773,7 @@ async function bounceDetect(sourceStream) {
|
|
|
675
773
|
result.response.message = result.response.message || message;
|
|
676
774
|
|
|
677
775
|
// Try X.X.X format
|
|
678
|
-
|
|
679
|
-
let statusMatch = message.match(/[45]\d{2}[\s-](\d+\.\d+\.\d+)[\s-]/);
|
|
680
|
-
if (statusMatch && statusMatch[1]) {
|
|
681
|
-
result.response.status = statusMatch[1];
|
|
682
|
-
}
|
|
683
|
-
}
|
|
776
|
+
fillEnhancedStatus(result.response, message);
|
|
684
777
|
|
|
685
778
|
// Also try (#X.X.X) format
|
|
686
779
|
if (!result.response.status) {
|
|
@@ -692,7 +785,9 @@ async function bounceDetect(sourceStream) {
|
|
|
692
785
|
}
|
|
693
786
|
}
|
|
694
787
|
}
|
|
788
|
+
}
|
|
695
789
|
|
|
790
|
+
function matchUndeliverableMessageTo(result, text) {
|
|
696
791
|
if (!result.recipient) {
|
|
697
792
|
/*
|
|
698
793
|
* Match pattern like:
|
|
@@ -705,8 +800,10 @@ async function bounceDetect(sourceStream) {
|
|
|
705
800
|
result.recipient = m[1];
|
|
706
801
|
}
|
|
707
802
|
}
|
|
803
|
+
}
|
|
708
804
|
|
|
709
|
-
|
|
805
|
+
// Google's old bounce format (pre-2015 style)
|
|
806
|
+
function matchGoogleLegacy(result, text) {
|
|
710
807
|
if (!result.action || !result.response || !result.response.message) {
|
|
711
808
|
/*
|
|
712
809
|
* Match pattern like:
|
|
@@ -738,7 +835,9 @@ async function bounceDetect(sourceStream) {
|
|
|
738
835
|
}
|
|
739
836
|
}
|
|
740
837
|
}
|
|
838
|
+
}
|
|
741
839
|
|
|
840
|
+
function matchReasonForProblem(result, text) {
|
|
742
841
|
if (!result.response || !result.response.message) {
|
|
743
842
|
/*
|
|
744
843
|
* Match pattern like:
|
|
@@ -767,7 +866,9 @@ async function bounceDetect(sourceStream) {
|
|
|
767
866
|
}
|
|
768
867
|
}
|
|
769
868
|
}
|
|
869
|
+
}
|
|
770
870
|
|
|
871
|
+
function matchRemoteHostSaid(result, text) {
|
|
771
872
|
if (!result.recipient && (!result.response || !result.response.message)) {
|
|
772
873
|
/*
|
|
773
874
|
* Match pattern like:
|
|
@@ -788,8 +889,10 @@ async function bounceDetect(sourceStream) {
|
|
|
788
889
|
}
|
|
789
890
|
}
|
|
790
891
|
}
|
|
892
|
+
}
|
|
791
893
|
|
|
792
|
-
|
|
894
|
+
// Complex pattern for certain MTA formats (Postfix style)
|
|
895
|
+
function matchRecipientHostBlock(result, text) {
|
|
793
896
|
if (result.recipient && result.response && !result.response.message && /A message that you sent could not be delivered/.test(text)) {
|
|
794
897
|
/*
|
|
795
898
|
* Match pattern like:
|
|
@@ -818,8 +921,45 @@ async function bounceDetect(sourceStream) {
|
|
|
818
921
|
}
|
|
819
922
|
}
|
|
820
923
|
}
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
/**
|
|
927
|
+
* Collects the indented error lines that follow the recipient line in an Exim block
|
|
928
|
+
*
|
|
929
|
+
* @param {string} block - One blank-line separated block of the bounce text
|
|
930
|
+
* @param {string} recipient - Recipient address to look for
|
|
931
|
+
* @returns {string[]} Trimmed error lines, empty when none follow the recipient
|
|
932
|
+
*/
|
|
933
|
+
function collectEximMessageLines(block, recipient) {
|
|
934
|
+
// Split block into lines and find the recipient line
|
|
935
|
+
const lines = block.split(/\r?\n/);
|
|
936
|
+
let foundRecipient = false;
|
|
937
|
+
let messageLines = [];
|
|
938
|
+
|
|
939
|
+
for (const line of lines) {
|
|
940
|
+
if (!foundRecipient) {
|
|
941
|
+
// Look for line containing recipient email
|
|
942
|
+
if (line.includes(recipient)) {
|
|
943
|
+
foundRecipient = true;
|
|
944
|
+
}
|
|
945
|
+
} else if (/^\s{2,}/.test(line) && line.trim()) {
|
|
946
|
+
// After recipient line, collect indented lines as error message
|
|
947
|
+
// Error lines typically start with more indentation than the recipient
|
|
948
|
+
messageLines.push(line.trim());
|
|
949
|
+
} else if (line.trim() === '') {
|
|
950
|
+
// Empty line might separate multiple errors, stop here
|
|
951
|
+
break;
|
|
952
|
+
} else {
|
|
953
|
+
// Non-indented non-empty line means end of this recipient's block
|
|
954
|
+
break;
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
return messageLines;
|
|
959
|
+
}
|
|
821
960
|
|
|
822
|
-
|
|
961
|
+
// Exim-style bounce format
|
|
962
|
+
function matchEximFailedAddresses(result, text) {
|
|
823
963
|
if (result.recipient && (!result.response || !result.response.message) && /The following address\(?es\)? failed:/i.test(text)) {
|
|
824
964
|
/*
|
|
825
965
|
* Match pattern like (Exim MTA):
|
|
@@ -834,31 +974,7 @@ async function bounceDetect(sourceStream) {
|
|
|
834
974
|
for (const block of blocks) {
|
|
835
975
|
// Check if this block contains our recipient email
|
|
836
976
|
if (block.includes(result.recipient)) {
|
|
837
|
-
|
|
838
|
-
const lines = block.split(/\r?\n/);
|
|
839
|
-
let foundRecipient = false;
|
|
840
|
-
let messageLines = [];
|
|
841
|
-
|
|
842
|
-
for (const line of lines) {
|
|
843
|
-
if (!foundRecipient) {
|
|
844
|
-
// Look for line containing recipient email
|
|
845
|
-
if (line.includes(result.recipient)) {
|
|
846
|
-
foundRecipient = true;
|
|
847
|
-
}
|
|
848
|
-
} else {
|
|
849
|
-
// After recipient line, collect indented lines as error message
|
|
850
|
-
// Error lines typically start with more indentation than the recipient
|
|
851
|
-
if (/^\s{2,}/.test(line) && line.trim()) {
|
|
852
|
-
messageLines.push(line.trim());
|
|
853
|
-
} else if (line.trim() === '') {
|
|
854
|
-
// Empty line might separate multiple errors, stop here
|
|
855
|
-
break;
|
|
856
|
-
} else {
|
|
857
|
-
// Non-indented non-empty line means end of this recipient's block
|
|
858
|
-
break;
|
|
859
|
-
}
|
|
860
|
-
}
|
|
861
|
-
}
|
|
977
|
+
let messageLines = collectEximMessageLines(block, result.recipient);
|
|
862
978
|
|
|
863
979
|
if (messageLines.length > 0) {
|
|
864
980
|
const message = messageLines.join(' ').replace(/\s+/g, ' ').trim();
|
|
@@ -872,8 +988,10 @@ async function bounceDetect(sourceStream) {
|
|
|
872
988
|
}
|
|
873
989
|
}
|
|
874
990
|
}
|
|
991
|
+
}
|
|
875
992
|
|
|
876
|
-
|
|
993
|
+
// KDDI / Japanese carrier style bounce
|
|
994
|
+
function matchCouldNotBeDeliveredTo(result, text) {
|
|
877
995
|
if (!result.recipient) {
|
|
878
996
|
/*
|
|
879
997
|
* Match pattern like:
|
|
@@ -885,8 +1003,10 @@ async function bounceDetect(sourceStream) {
|
|
|
885
1003
|
result.action = result.action || 'failed';
|
|
886
1004
|
}
|
|
887
1005
|
}
|
|
1006
|
+
}
|
|
888
1007
|
|
|
889
|
-
|
|
1008
|
+
// X6 / Generic MTA style with "permanent errors"
|
|
1009
|
+
function matchPermanentErrors(result, text) {
|
|
890
1010
|
if (!result.recipient) {
|
|
891
1011
|
/*
|
|
892
1012
|
* Match pattern like:
|
|
@@ -898,8 +1018,10 @@ async function bounceDetect(sourceStream) {
|
|
|
898
1018
|
result.action = result.action || 'failed';
|
|
899
1019
|
}
|
|
900
1020
|
}
|
|
1021
|
+
}
|
|
901
1022
|
|
|
902
|
-
|
|
1023
|
+
// Extract error message from Verizon MMS / mobile carrier style
|
|
1024
|
+
function matchCarrierError(result, text) {
|
|
903
1025
|
if (!result.response || !result.response.message) {
|
|
904
1026
|
/*
|
|
905
1027
|
* Match pattern like:
|
|
@@ -914,8 +1036,10 @@ async function bounceDetect(sourceStream) {
|
|
|
914
1036
|
result.response.message = result.response.message || m[0].replace(/\s+/g, ' ').trim();
|
|
915
1037
|
}
|
|
916
1038
|
}
|
|
1039
|
+
}
|
|
917
1040
|
|
|
918
|
-
|
|
1041
|
+
// Apache James / Verizon text style with RCPT TO
|
|
1042
|
+
function matchRcptTo(result, text) {
|
|
919
1043
|
if (!result.recipient) {
|
|
920
1044
|
/*
|
|
921
1045
|
* Match pattern like:
|
|
@@ -926,8 +1050,10 @@ async function bounceDetect(sourceStream) {
|
|
|
926
1050
|
result.recipient = m[1].trim();
|
|
927
1051
|
}
|
|
928
1052
|
}
|
|
1053
|
+
}
|
|
929
1054
|
|
|
930
|
-
|
|
1055
|
+
// Generic error code extraction (550, 551, 552, etc.)
|
|
1056
|
+
function matchGenericSmtpCode(result, text) {
|
|
931
1057
|
if (!result.response || !result.response.message) {
|
|
932
1058
|
/*
|
|
933
1059
|
* Match pattern like:
|
|
@@ -949,9 +1075,37 @@ async function bounceDetect(sourceStream) {
|
|
|
949
1075
|
}
|
|
950
1076
|
}
|
|
951
1077
|
}
|
|
1078
|
+
}
|
|
952
1079
|
|
|
953
|
-
|
|
954
|
-
|
|
1080
|
+
// The text heuristics in the order they run
|
|
1081
|
+
const TEXT_HEURISTICS = [
|
|
1082
|
+
matchHostSaid,
|
|
1083
|
+
matchDeliveryFailedRecipient,
|
|
1084
|
+
matchAffectedRecipients,
|
|
1085
|
+
matchLeadingSmtpReply,
|
|
1086
|
+
matchRecipientBlock,
|
|
1087
|
+
matchUndeliverableMessageTo,
|
|
1088
|
+
matchGoogleLegacy,
|
|
1089
|
+
matchReasonForProblem,
|
|
1090
|
+
matchRemoteHostSaid,
|
|
1091
|
+
matchRecipientHostBlock,
|
|
1092
|
+
matchEximFailedAddresses,
|
|
1093
|
+
matchCouldNotBeDeliveredTo,
|
|
1094
|
+
matchPermanentErrors,
|
|
1095
|
+
matchCarrierError,
|
|
1096
|
+
matchRcptTo,
|
|
1097
|
+
matchGenericSmtpCode
|
|
1098
|
+
];
|
|
1099
|
+
|
|
1100
|
+
/**
|
|
1101
|
+
* Tries to find embedded message headers in the bounce body
|
|
1102
|
+
* Some MTAs include the original headers inline
|
|
1103
|
+
*
|
|
1104
|
+
* @param {Object} ctx - Detection context
|
|
1105
|
+
* @param {string} text - Bounce text
|
|
1106
|
+
*/
|
|
1107
|
+
function readEmbeddedHeaders(ctx, text) {
|
|
1108
|
+
let { result } = ctx;
|
|
955
1109
|
if (!result.messageId && text) {
|
|
956
1110
|
// Look for the start of email headers (Received or Return-Path)
|
|
957
1111
|
let m = text.match(/^(Received|Return-Path):/im);
|
|
@@ -965,12 +1119,62 @@ async function bounceDetect(sourceStream) {
|
|
|
965
1119
|
result.messageId = entries['message-id'][0].trim();
|
|
966
1120
|
// Only use these headers if we haven't found headers elsewhere
|
|
967
1121
|
if (!result.headers) {
|
|
968
|
-
parsedMessageHeaders = entries;
|
|
1122
|
+
ctx.parsedMessageHeaders = entries;
|
|
969
1123
|
}
|
|
970
1124
|
}
|
|
971
1125
|
}
|
|
972
1126
|
}
|
|
973
1127
|
}
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
/**
|
|
1131
|
+
* Main bounce detection function that analyzes email messages for bounce information
|
|
1132
|
+
*
|
|
1133
|
+
* @async
|
|
1134
|
+
* @param {Stream|Object} sourceStream - Email stream or object with parsed email
|
|
1135
|
+
* @param {Object} [sourceStream.parsed] - Pre-parsed email object (avoids re-parsing)
|
|
1136
|
+
* @returns {Promise<Object>} Bounce detection results
|
|
1137
|
+
* @returns {string} [returns.recipient] - Failed recipient email address
|
|
1138
|
+
* @returns {string} [returns.action] - Bounce action (typically 'failed')
|
|
1139
|
+
* @returns {Object} [returns.response] - SMTP response details
|
|
1140
|
+
* @returns {string} [returns.response.message] - The error message from the remote server
|
|
1141
|
+
* @returns {string} [returns.response.status] - Enhanced status code (X.X.X format)
|
|
1142
|
+
* @returns {string} [returns.response.source] - Source of the diagnostic code
|
|
1143
|
+
* @returns {string} [returns.mta] - Mail server that reported the failure
|
|
1144
|
+
* @returns {string} [returns.queueId] - Queue ID from the sending MTA
|
|
1145
|
+
* @returns {string} [returns.messageId] - Original message ID that bounced
|
|
1146
|
+
* @returns {Object} [returns.messageHeaders] - Headers from the original message
|
|
1147
|
+
*/
|
|
1148
|
+
async function bounceDetect(sourceStream) {
|
|
1149
|
+
// Validate input parameter
|
|
1150
|
+
if (!sourceStream) {
|
|
1151
|
+
return {};
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
let parsed = await loadParsedMessage(sourceStream);
|
|
1155
|
+
let ctx = createDetectContext(parsed);
|
|
1156
|
+
|
|
1157
|
+
// Structured sources first, in order of precedence
|
|
1158
|
+
readFailedRecipientsHeader(ctx);
|
|
1159
|
+
readWorkMailReport(ctx);
|
|
1160
|
+
readReportParts(ctx);
|
|
1161
|
+
await readOriginalMessage(ctx);
|
|
1162
|
+
await readZohoOriginalMessage(ctx);
|
|
1163
|
+
|
|
1164
|
+
let result = ctx.result;
|
|
1165
|
+
|
|
1166
|
+
// What the structured parts said, if anything; the text heuristics below only fill gaps, and
|
|
1167
|
+
// only a claim they made is reconsidered at the end
|
|
1168
|
+
let structuredAction = result.action;
|
|
1169
|
+
|
|
1170
|
+
// Extract text content for pattern matching
|
|
1171
|
+
let text = extractHeuristicText(parsed, ctx.attachments);
|
|
1172
|
+
|
|
1173
|
+
for (let heuristic of TEXT_HEURISTICS) {
|
|
1174
|
+
heuristic(result, text);
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
readEmbeddedHeaders(ctx, text);
|
|
974
1178
|
|
|
975
1179
|
// The heuristics above claim 'failed' for any recipient block or quoted reply they recognize,
|
|
976
1180
|
// but a delay notice has the same shape: a Sendmail-style "THIS IS A WARNING MESSAGE ONLY"
|
|
@@ -985,7 +1189,7 @@ async function bounceDetect(sourceStream) {
|
|
|
985
1189
|
}
|
|
986
1190
|
|
|
987
1191
|
// Include parsed message headers in the result
|
|
988
|
-
result.messageHeaders = parsedMessageHeaders || null;
|
|
1192
|
+
result.messageHeaders = ctx.parsedMessageHeaders || null;
|
|
989
1193
|
|
|
990
1194
|
return result;
|
|
991
1195
|
}
|