Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { readFileSync } from 'node:fs';
const data = JSON.parse(readFileSync(new URL('./smtp-bounce-codes.json', import.meta.url), 'utf8'));
const enhancedCodes = new Map(data.enhanced.map(row => [row.code, row]));
const replyCodes = new Map(data.replies.map(row => [row.code, row]));
const enhancedPattern = /^[245]\.(?:0|[1-9][0-9]{0,2})\.(?:0|[1-9][0-9]{0,2})$/;
const enhancedSyntax = /[245]\.(?:0|[1-9][0-9]{0,2})\.(?:0|[1-9][0-9]{0,2})/.source;
const enhancedPattern = new RegExp('^' + enhancedSyntax + '$');
// Start a logical line at a quoted SMTP reply or DSN Status field.
const embeddedPattern = new RegExp(
/(?<![A-Za-z0-9_.])(?:[2345][0-9]{2}(?:[ \t]+|-[ \t]*)|status:[ \t]*)/.source +
enhancedSyntax + /(?![A-Za-z0-9_.])/.source, 'gi');
const replyPattern = /^([2345][0-9]{2})(?:[ \t-]|$)/;
const statusPattern = /^status:[ \t]*/i;
const candidatePattern = /^[0-9]+\./;
Expand All @@ -15,7 +20,7 @@ function scan(text) {
let reply = null, enhanced = null;
const warnings = [], messages = [];
const warn = message => { if (!warnings.includes(message)) warnings.push(message); };
for (const raw of text.split(/\r\n|\r|\n/)) {
for (const raw of text.replace(embeddedPattern, '\n$& ').split(/\r\n|\r|\n/)) {
const line = raw.replace(/^[ \t]+|[ \t]+$/g, '');
if (!line) continue;
const status = statusPattern.exec(line);
Expand All @@ -27,7 +32,7 @@ function scan(text) {
if (reply === null) reply = current;
else if (current !== reply) { warn('multiple-reply-codes'); continue; }
rest = ltrim(line.slice(match[0].length));
} else if (candidatePattern.test(line)) rest = line;
} else if (/^[0-9]\./.test(line)) rest = line;
else continue;
const token = rest.split(status ? /[ \t(]/ : /[ \t]/, 1)[0];
if (exactEnhanced(token)) {
Expand Down
12 changes: 9 additions & 3 deletions python/smtp_bounce_codes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
_DATA = json.loads(files(__package__).joinpath('smtp-bounce-codes.json').read_text(encoding='utf-8'))
_ENHANCED = {row['code']: row for row in _DATA['enhanced']}
_REPLIES = {row['code']: row for row in _DATA['replies']}
_ENHANCED_RE = re.compile(r'[245]\.(?:0|[1-9][0-9]{0,2})\.(?:0|[1-9][0-9]{0,2})')
_ENHANCED_SYNTAX = r'[245]\.(?:0|[1-9][0-9]{0,2})\.(?:0|[1-9][0-9]{0,2})'
_ENHANCED_RE = re.compile(_ENHANCED_SYNTAX)
# A quoted SMTP reply or DSN Status field starts a logical reply line.
_EMBEDDED_RE = re.compile(
r'(?<![A-Za-z0-9_.])(?:[2345][0-9]{2}(?:[ \t]+|-[ \t]*)|status:[ \t]*)'
+ _ENHANCED_SYNTAX + r'(?![A-Za-z0-9_.])', re.IGNORECASE | re.ASCII)
_REPLY_RE = re.compile(r'^([2345][0-9]{2})(?:[ \t-]|$)')
_STATUS_RE = re.compile(r'^status:[ \t]*', re.IGNORECASE | re.ASCII)
_CANDIDATE_RE = re.compile(r'^[0-9]+\.')
Expand All @@ -26,6 +31,7 @@ def warn(message):
if message not in warnings:
warnings.append(message)

text = _EMBEDDED_RE.sub(r'\n\g<0> ', text)
for raw in re.split(r'\r\n|\r|\n', text):
line = raw.strip(' \t')
if not line:
Expand All @@ -42,7 +48,7 @@ def warn(message):
warn('multiple-reply-codes')
continue
rest = line[match.end():].lstrip(' \t')
elif _CANDIDATE_RE.match(line):
elif re.match(r'^[0-9]\.', line):
rest = line
else:
continue
Expand All @@ -67,7 +73,7 @@ def warn(message):


def parse(text):
"""Extract the first reply and enhanced code from recognized line prefixes."""
"""Extract the first reply and enhanced code from SMTP replies and DSN status fields."""
return _scan(text)[0]


Expand Down
15 changes: 14 additions & 1 deletion tests/fixtures/lookup.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,20 @@
{"text":"550- 5.1.1 text","expected":{"reply":"550","enhanced":"5.1.1","warnings":[]}},
{"text":"Status: 5. 1.1","expected":{"reply":null,"enhanced":null,"warnings":["invalid-enhanced-code"]}},
{"text":"550 5.1000.1 invalid","expected":{"reply":"550","enhanced":null,"warnings":["invalid-enhanced-code"]}},
{"text":"5500 not a reply","expected":{"reply":null,"enhanced":null,"warnings":[]}}
{"text":"5500 not a reply","expected":{"reply":null,"enhanced":null,"warnings":[]}},
{"text":"host gmail-smtp-in.l.google.com[142.250.4.27] said: 550-5.1.1 The email account...","expected":{"reply":"550","enhanced":"5.1.1","warnings":[]}},
{"text":"Remote server returned '550 5.1.10 RESOLVER.ADR.RecipientNotFound; ...'","expected":{"reply":"550","enhanced":"5.1.10","warnings":[]}},
{"text":"Diagnostic-Code: smtp; 554 5.7.1 Service unavailable","expected":{"reply":"554","enhanced":"5.7.1","warnings":[]}},
{"text":"recipient report (Status: 4.2.2)","expected":{"reply":null,"enhanced":"4.2.2","warnings":[]}},
{"text":"remote said '550 5.1.1'","expected":{"reply":"550","enhanced":"5.1.1","warnings":[]}},
{"text":"550 response: remote said 550 5.1.1 unavailable","expected":{"reply":"550","enhanced":"5.1.1","warnings":[]}},
{"text":"142.250.4.27","expected":{"reply":null,"enhanced":null,"warnings":[]}},
{"text":"Postfix 3.4.2","expected":{"reply":null,"enhanced":null,"warnings":[]}},
{"text":"Version 5.1.1","expected":{"reply":null,"enhanced":null,"warnings":[]}},
{"text":"host 10.250 4.2.2","expected":{"reply":null,"enhanced":null,"warnings":[]}},
{"text":"remote 1550 5.1.1","expected":{"reply":null,"enhanced":null,"warnings":[]}},
{"text":"remote 550 5.1.1.2","expected":{"reply":null,"enhanced":null,"warnings":[]}},
{"text":"remote 550 5.1.1000","expected":{"reply":null,"enhanced":null,"warnings":[]}}
],
"lookup": [
{"code":"5.1.1","fields":{"code":"5.1.1","registry_code":"X.1.1","kind":"enhanced","class":5,"class_meaning":"permanent failure","retry":"no","title":"Bad destination mailbox address","fallback":false}},
Expand Down
Loading