fix: use subject-independent identities for captured mail Wiki paths
This commit is contained in:
1 parent
0b6343aff0
commit
3262f88005
7 files changed
+91
-11
No files matched your search
+17
-3
@@ -30,7 +30,7 @@ import urllib.parse as url
|
||||
import urllib.request as http
|
||||
import webbrowser
|
||||
|
||||
VERSION = '0.3.0'
|
||||
VERSION = '0.3.1'
|
||||
|
||||
class Failure(Exception):
|
||||
def __init__(self, message, status=0, payload=None):
|
||||
@@ -496,11 +496,24 @@ class MailText(HTMLParser):
|
||||
if not self.hidden: self.parts.append(data)
|
||||
|
||||
|
||||
def mail_subject(value):
|
||||
# Remove only leading mailing-list sequence markers, preserving meaningful
|
||||
# tags such as [Important], and any Re:/Fwd: prefix before the marker.
|
||||
marker = re.compile(r'^(?P<reply>(?:(?:re|fw|fwd)\s*:\s*)*)\[[^\[\]\r\n]+[\s:#]\s*[0-9]+\]\s*', re.IGNORECASE)
|
||||
while True:
|
||||
match = marker.match(value)
|
||||
if not match: return value.strip()
|
||||
value = match.group('reply') + value[match.end():]
|
||||
|
||||
|
||||
def task_mail(raw, include_body=True, override_title=None):
|
||||
if len(raw) > 2 << 20: raise Failure('Mail input exceeds 2 MiB; pipe one message at a time.')
|
||||
message = email.parser.BytesParser(policy=email.policy.default).parsebytes(raw)
|
||||
clean = lambda value: ' '.join(safe_text(str(value or '')).split())
|
||||
title = override_title or clean(message.get('Subject')) or '(No subject)'
|
||||
subject = clean(message.get('Subject'))
|
||||
title = override_title or mail_subject(subject) or '(No subject)'
|
||||
if not override_title and len(title.encode()) > 300:
|
||||
title = title.encode()[:297].decode('utf-8',errors='ignore').rstrip() + '…'
|
||||
if len(title.encode()) > 300: raise Failure('Mail subject exceeds 300 UTF-8 bytes; use --title.')
|
||||
notes = '\n'.join(key+': '+clean(message.get(key)) for key in ('From','Date','Message-ID') if message.get(key))
|
||||
wiki_body = None
|
||||
@@ -513,7 +526,8 @@ def task_mail(raw, include_body=True, override_title=None):
|
||||
if not isinstance(content,str): raise Failure('Mail body is not readable text.')
|
||||
if part.get_content_type() == 'text/html':
|
||||
parser = MailText();parser.feed(content);parser.close();content = ''.join(parser.parts).strip()
|
||||
wiki_body = notes + '\n\n' + content
|
||||
# Retain the original subject when its mailing-list marker was removed.
|
||||
wiki_body = (('Subject: '+subject+'\n') if title != subject else '') + notes + '\n\n' + content
|
||||
if '\0' in wiki_body or len(wiki_body.encode()) > 256 << 10: raise Failure('Mail Wiki body must be valid text of at most 256 KiB; nothing was uploaded.')
|
||||
if len(notes.encode()) > 16384: raise Failure('Mail metadata exceeds 16 KiB.')
|
||||
return title, notes, clean(message.get('Message-ID')) or hashlib.sha256(raw).hexdigest(), wiki_body
|
||||
|
||||
Reference in new issue
Block a user