Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
1,247 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,77 @@ | ||
from collections import deque | ||
from contextlib import closing | ||
|
||
import attr | ||
import regex as re | ||
import six | ||
from attr.validators import instance_of | ||
from six.moves import range | ||
|
||
from flanker.mime.message.headers import MimeHeaders | ||
from flanker.mime.message.headers.parsing import parse_stream | ||
|
||
|
||
_HEADERS = ('Action', | ||
'Content-Description', | ||
'Diagnostic-Code', | ||
'Final-Recipient', | ||
'Received', | ||
'Remote-Mta', | ||
'Reporting-Mta', | ||
'Status') | ||
|
||
_RE_STATUS = re.compile(r'\d\.\d+\.\d+', re.IGNORECASE) | ||
|
||
|
||
@attr.s(frozen=True) | ||
class Result(object): | ||
score = attr.ib(validator=instance_of(float)) | ||
status = attr.ib(validator=instance_of(six.text_type)) | ||
diagnostic_code = attr.ib(validator=instance_of(six.text_type)) | ||
notification = attr.ib(validator=instance_of(six.text_type)) | ||
|
||
|
||
def detect(message): | ||
headers = collect(message) | ||
return Result( | ||
score=len(headers) / float(len(HEADERS)), | ||
status=get_status(headers), | ||
notification=get_notification(message), | ||
diagnostic_code=headers.get('Diagnostic-Code')) | ||
headers = _collect_headers(message) | ||
return Result(score=len(headers) / float(len(_HEADERS)), | ||
status=_get_status(headers), | ||
diagnostic_code=headers.get('Diagnostic-Code', u''), | ||
notification=_get_notification(message)) | ||
|
||
|
||
def collect(message): | ||
def _collect_headers(message): | ||
collected = deque() | ||
for p in message.walk(with_self=True): | ||
for h in HEADERS: | ||
for h in _HEADERS: | ||
if h in p.headers: | ||
collected.append((h, p.headers[h])) | ||
if p.content_type.is_delivery_status(): | ||
collected += collect_from_status(p.body) | ||
collected += _collect_headers_from_status(p.body) | ||
|
||
return MimeHeaders(collected) | ||
|
||
|
||
def collect_from_status(body): | ||
def _collect_headers_from_status(body): | ||
out = deque() | ||
with closing(six.StringIO(body)) as stream: | ||
for i in range(3): | ||
out += parse_stream(stream) | ||
|
||
return out | ||
|
||
|
||
def get_status(headers): | ||
def _get_status(headers): | ||
for v in headers.getall('Status'): | ||
if RE_STATUS.match(v.strip()): | ||
if _RE_STATUS.match(v.strip()): | ||
return v | ||
|
||
return u'' | ||
|
||
def get_notification(message): | ||
|
||
def _get_notification(message): | ||
for part in message.walk(): | ||
content_desc = part.headers.get('Content-Description', '').lower() | ||
if content_desc == 'notification': | ||
return part.body | ||
|
||
return None | ||
|
||
|
||
HEADERS = ('Action', | ||
'Content-Description', | ||
'Diagnostic-Code', | ||
'Final-Recipient', | ||
'Received', | ||
'Remote-Mta', | ||
'Reporting-Mta', | ||
'Status') | ||
|
||
RE_STATUS = re.compile(r'\d\.\d+\.\d+', re.IGNORECASE) | ||
|
||
|
||
class Result(object): | ||
def __init__(self, score, status, notification, diagnostic_code): | ||
self.score = score | ||
self.status = status | ||
self.notification = notification | ||
self.diagnostic_code = diagnostic_code | ||
|
||
def __repr__(self): | ||
return (u'bounce.Result(status={}, score={}, notification={},' | ||
u' diag_code={})'.format(self.status, self.score, | ||
self.notification, | ||
self.diagnostic_code)) | ||
return u'' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.