feat: support mailing-list and monthly mail capture paths
Build and publish container images / images (push) Successful in 11m59s
Release container images / release (push) Successful in 22s

This commit is contained in:
Anya Tokugawa committed 2026-11-06 09:51:45 +09:00
1 parent 543814cbd6
commit 1a451023f0
7 files changed
+165 -5

No files matched your search

+27 -3
View File
@@ -3,6 +3,8 @@
import argparse
import email.policy
import email.parser
import email.utils
import datetime
import uuid
import base64
import contextlib
@@ -30,7 +32,7 @@ import urllib.parse as url
import urllib.request as http
import webbrowser
VERSION = '0.3.1'
VERSION = '0.4.0'
class Failure(Exception):
def __init__(self, message, status=0, payload=None):
@@ -506,6 +508,19 @@ def mail_subject(value):
value = match.group('reply') + value[match.end():]
def task_mail_suffix(raw, request_id, now=None):
message = email.parser.BytesParser(policy=email.policy.default).parsebytes(raw)
subject = ' '.join(safe_text(str(message.get('Subject') or '')).split())
marker = re.match(r'^(?:(?:re|fw|fwd)\s*:\s*)*\[([A-Za-z0-9][A-Za-z0-9._-]{0,99})[\s:#]+([0-9]{1,20})\]',subject,re.IGNORECASE)
if marker:
return marker[1]+'/'+str(int(marker[2]))
try: date = email.utils.parsedate_to_datetime(str(message.get('Date') or ''))
except (ValueError, TypeError, OverflowError): date = now or datetime.datetime.now(datetime.timezone.utc)
# Preserve the month in the sender's Date timezone, not the receiving host's.
ident = request_id if ' '.join(safe_text(str(message.get('Message-ID') or '')).split()) else hashlib.sha256(raw).hexdigest()
return f'{date.year:04d}/{date.month:02d}/{ident}'
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)
@@ -563,6 +578,7 @@ def tasks(client, profile, words):
def capture(payload):
pending=profile.data.setdefault('task_outbox',{})
if len(pending)>=20 and payload['request_id'] not in pending:raise Failure('20 pending captures retained. Retry them before adding more.')
if payload['request_id'] in pending and pending[payload['request_id']] != payload:raise Failure('A different draft with this request ID is retained. Use pages task retry '+payload['request_id'])
pending[payload['request_id']]=payload;profile.save()
try: result=client.json(TASK_API+'capture',method='POST',data=payload)
except Failure as e:
@@ -595,13 +611,21 @@ def tasks(client, profile, words):
payload={'request_id':getattr(a,'request_id',None) or str(uuid.uuid4()),'title':a.title,'notes':a.notes or ''}
if action=='from-mail':
if a.metadata_only and a.wiki_prefix is not None: raise Failure('--wiki-prefix cannot be combined with --metadata-only.')
title,notes,message_id,wiki_body=task_mail(sys.stdin.buffer.read((2<<20)+1),not a.metadata_only,a.title)
raw=sys.stdin.buffer.read((2<<20)+1)
title,notes,message_id,wiki_body=task_mail(raw,not a.metadata_only,a.title)
payload.update(title=a.title or title,notes=a.notes if a.notes is not None else notes,request_id=str(uuid.uuid5(uuid.NAMESPACE_URL,client.base+'\n'+profile.data.get('username','')+'\n'+message_id)))
if wiki_body is not None: payload['wiki_body']=wiki_body
if wiki_body is not None:
payload['wiki_body']=wiki_body
payload['wiki_suffix']=task_mail_suffix(raw,payload['request_id'])
if a.wiki_prefix is not None: payload['wiki_prefix']=a.wiki_prefix
for attr,key in [('project','project_uuid'),('assignee','assignee_uuid'),('due','due_on'),('start_on','start_on'),('planned','planned_on'),('review','review_on'),('estimate','estimate_minutes'),('complexity','complexity')]:
value=getattr(a,attr)
if value is not None:payload[key]=task_project(client,value) if attr=='project' else value
# A retry of the same pending mail must not drift across a month boundary
# when Date was missing. Other changed options still fail closed.
old=profile.data.get('task_outbox',{}).get(payload['request_id'])
if action=='from-mail' and old and {k:v for k,v in old.items() if k!='wiki_suffix'}=={k:v for k,v in payload.items() if k!='wiki_suffix'}:
payload=old.copy()
emit(capture(payload));return
if action=='project-add':
emit(client.json(TASK_API+'items',method='POST',data={'uuid':str(uuid.uuid4()),'kind':'project','title':a.title,'notes':a.notes,'status':'todo','bucket':'inbox','priority':1}));return
+33
View File
@@ -9,6 +9,39 @@ import unittest
m=runpy.run_path(str(Path(__file__).with_name('pages')))
class ProductivityTests(unittest.TestCase):
def test_mail_path_priority_and_dates(self):
import datetime,hashlib
ident='06cb3a24-61f8-4a9f-a0bf-89eea9632f25'
suffix=m['task_mail_suffix'];now=datetime.datetime(2030,2,1,tzinfo=datetime.timezone.utc)
for marker in ('[hogehoge 00123]','Re: [hogehoge:123]','Fwd: [hogehoge#123]'):
raw=('Subject: '+marker+' Title\r\n\r\nbody').encode()
self.assertEqual(suffix(raw,ident,now),'hogehoge/123')
raw=b'Subject: Plain\r\nDate: Sat, 31 Jan 2026 23:30:00 -0800\r\nMessage-ID: <id@example.invalid>\r\n\r\nbody'
self.assertEqual(suffix(raw,ident,now),'2026/01/'+ident)
noid=raw.replace(b'Message-ID: <id@example.invalid>\r\n',b'')
self.assertEqual(suffix(noid,ident,now),'2026/01/'+hashlib.sha256(noid).hexdigest())
for date in (b'',b'Date: broken\r\n'):
raw=b'Subject: [../unsafe 123] Title\r\n'+date+b'\r\nbody'
self.assertEqual(suffix(raw,ident,now),'2030/02/'+hashlib.sha256(raw).hexdigest())
def test_pending_mail_month_and_options_are_preserved(self):
from unittest.mock import patch
raw=b'Subject: Test\r\nMessage-ID: <same@example.invalid>\r\n\r\nbody'
calls=[]
def send(route,**kw):
calls.append(kw['data'].copy())
raise m['Failure']('offline')
client=SimpleNamespace(base='https://example.invalid',json=send)
profile=SimpleNamespace(data={'username':'test'},save=lambda:None)
for month in ('2030/01','2030/02'):
with patch.dict(m['tasks'].__globals__,task_mail_suffix=lambda raw,ident:month+'/'+ident),patch('sys.stdin',SimpleNamespace(buffer=io.BytesIO(raw))),contextlib.redirect_stderr(io.StringIO()),self.assertRaises(m['Failure']):
m['tasks'](client,profile,['from-mail'])
self.assertEqual(calls[0],calls[1])
with patch('sys.stdin',SimpleNamespace(buffer=io.BytesIO(raw))),self.assertRaises(m['Failure']):
m['tasks'](client,profile,['from-mail','--wiki-prefix','/changed'])
self.assertEqual(len(calls),2)
self.assertEqual(next(iter(profile.data['task_outbox'].values())),calls[0])
def test_mail_privacy_and_long_subject_override(self):
raw=b'Subject: Test\r\nFrom: Person <person@example.invalid>\r\nMessage-ID: <id@example.invalid>\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nPRIVATE BODY'
title,notes,ident,body=m['task_mail'](raw)
+9 -1
View File
@@ -34,7 +34,7 @@ def run():
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']=='/private/mail/mail-'+captured['item']['uuid']
assert captured['item']['page_uuid'] and captured['page_path']=='/private/mail/hogehoge/123'
assert captured['item']['title']=='日本語の件名と依頼'
assert 'Private mail body' not in captured['item']['notes']
with patch('sys.stdin',SimpleNamespace(buffer=io.BytesIO(mail))):
@@ -43,6 +43,14 @@ def run():
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()
message.replace_header('Subject','連番なしの日本語');message.replace_header('Message-ID','<other@example.invalid>');message['Date']='Sat, 31 Jan 2026 23:30:00 -0800'
with patch('sys.stdin',SimpleNamespace(buffer=io.BytesIO(message.as_bytes()))):dated=task('from-mail')
assert dated['page_path']=='/private/mail/2026/01/'+dated['item']['uuid']
del message['Message-ID']
import hashlib
raw_mail=message.as_bytes()
with patch('sys.stdin',SimpleNamespace(buffer=io.BytesIO(raw_mail))):hashed=task('from-mail')
assert hashed['page_path']=='/private/mail/2026/01/'+hashlib.sha256(raw_mail).hexdigest()
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()