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
|
||||
|
||||
@@ -16,9 +16,27 @@ class ProductivityTests(unittest.TestCase):
|
||||
self.assertIn('PRIVATE BODY',body)
|
||||
self.assertIsNone(m['task_mail'](raw,False)[3])
|
||||
raw=raw.replace(b'Subject: Test',b'Subject: '+b'a'*301)
|
||||
with self.assertRaises(m['Failure']):m['task_mail'](raw)
|
||||
self.assertLessEqual(len(m['task_mail'](raw)[0].encode()),300)
|
||||
with self.assertRaises(m['Failure']):m['task_mail'](raw,True,'x'*301)
|
||||
self.assertEqual(m['task_mail'](raw,False,'Override')[0],'Override')
|
||||
|
||||
def test_japanese_subject_and_mailing_list_markers(self):
|
||||
from email.message import EmailMessage
|
||||
from email.header import Header
|
||||
for encoding in ('utf-8','iso-2022-jp'):
|
||||
subject='Re: [hogehoge 00123] [team:456] 日本語の依頼 [重要]'
|
||||
raw=('Subject: '+Header(subject,encoding).encode()+'\r\nMessage-ID: <same@example.invalid>\r\n\r\nbody').encode('ascii')
|
||||
title,notes,ident,body=m['task_mail'](raw)
|
||||
self.assertEqual(title,'Re: 日本語の依頼 [重要]')
|
||||
self.assertEqual(ident,'<same@example.invalid>');self.assertIn('Subject: '+subject,body)
|
||||
for subject in ('[重要] 日本語','報告 [list 123]','[release 1.2] 更新'):
|
||||
self.assertEqual(m['mail_subject'](subject),subject)
|
||||
msg=EmailMessage();msg['Subject']='[list 123] '+('日本語'*100);msg.set_content('Body')
|
||||
title,_,_,body=m['task_mail'](msg.as_bytes())
|
||||
self.assertLessEqual(len(title.encode()),300);self.assertTrue(title.endswith('…'))
|
||||
self.assertIn('日本語'*100,body)
|
||||
self.assertEqual(m['task_mail'](msg.as_bytes(),True,'[list 123] 手動指定')[0],'[list 123] 手動指定')
|
||||
|
||||
def test_html_mail_and_limits(self):
|
||||
raw=b'Subject: HTML\r\nContent-Type: text/html; charset=utf-8\r\n\r\n<head><style>secret</style></head><p>Hello</p><script>evil()</script><img src="https://example.invalid/image"><p>World & friends</p>'
|
||||
body=m['task_mail'](raw)[3]
|
||||
|
||||
@@ -7,6 +7,7 @@ import runpy
|
||||
import sys
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import patch
|
||||
from email.message import EmailMessage
|
||||
|
||||
def run():
|
||||
m=runpy.run_path(str(Path(__file__).with_name('pages')))
|
||||
@@ -29,15 +30,19 @@ def run():
|
||||
report=task('report','--json');assert sum(d['points'] for d in report['days'])==7
|
||||
assert any(i['uuid']==ident for i in report['tasks'])
|
||||
task('preferences','--wiki-prefix','/private/mail')
|
||||
mail=b'Subject: CLI mail\r\nMessage-ID: <integration@example.invalid>\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nPrivate mail body {{children}}'
|
||||
message=EmailMessage();message['Subject']='[hogehoge 123] 日本語の件名と依頼';message['Message-ID']='<integration@example.invalid>';message.set_content('Private mail body {{children}}')
|
||||
mail=message.as_bytes()
|
||||
with patch('sys.stdin',SimpleNamespace(buffer=io.BytesIO(mail))):
|
||||
captured=task('from-mail')
|
||||
assert captured['item']['page_uuid'] and captured['page_path'].startswith('/private/mail/CLI mail--')
|
||||
assert captured['item']['page_uuid'] and captured['page_path']=='/private/mail/mail-'+captured['item']['uuid']
|
||||
assert captured['item']['title']=='日本語の件名と依頼'
|
||||
assert 'Private mail body' not in captured['item']['notes']
|
||||
with patch('sys.stdin',SimpleNamespace(buffer=io.BytesIO(mail))):
|
||||
assert task('from-mail')['item']['page_uuid']==captured['item']['page_uuid']
|
||||
note=client.json(m['TASK_API']+'items/'+captured['item']['uuid']+'/note')
|
||||
assert note['linked'] and note['path']==captured['page_path']
|
||||
raw,_=client.raw('path:'+captured['page_path'])
|
||||
assert '日本語の件名と依頼' in raw.decode() and 'Private mail body' in raw.decode()
|
||||
path=Path(fixture['root'])/'person.vcf'
|
||||
path.write_text('BEGIN:VCARD\r\nVERSION:4.0\r\nUID:urn:test:cli\r\nFN:CLI Person\r\nEMAIL:cli@example.invalid\r\nEND:VCARD\r\n')
|
||||
output=io.StringIO()
|
||||
|
||||
Reference in new issue
Block a user