feat: capture mail bodies in linked Wiki pages
Test and release pages-cli / linux (push) Successful in 31s
Test and release pages-cli / macos (push) Skipped

This commit is contained in:
Anya Tokugawa committed 2026-11-06 08:33:54 +09:00
1 parent 59ab4c5b26
commit cde94ed936
8 files changed
+101 -24

No files matched your search

+42 -13
View File
@@ -10,6 +10,7 @@ import difflib
import fcntl
import getpass
import hashlib
from html.parser import HTMLParser
import json
import os
from pathlib import Path
@@ -29,7 +30,7 @@ import urllib.parse as url
import urllib.request as http
import webbrowser
VERSION = '0.2.0'
VERSION = '0.3.0'
class Failure(Exception):
def __init__(self, message, status=0, payload=None):
@@ -480,21 +481,42 @@ def task_project(client, value):
return matches[0]
def task_mail(raw, include_body=False, override_title=None):
class MailText(HTMLParser):
"""Extract inert text; never load links, images, styles or attachments."""
def __init__(self):
super().__init__(convert_charrefs=True)
self.parts, self.hidden = [], []
def handle_starttag(self, tag, attrs):
if tag in ('script','style','head'): self.hidden.append(tag)
if not self.hidden and tag in ('br','p','div','li','tr','h1','h2','h3','pre'): self.parts.append('\n')
def handle_endtag(self, tag):
if self.hidden and tag == self.hidden[-1]: self.hidden.pop()
if not self.hidden and tag in ('p','div','li','tr','h1','h2','h3','pre'): self.parts.append('\n')
def handle_data(self, data):
if not self.hidden: self.parts.append(data)
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)'
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
if include_body:
part = message.get_body(preferencelist=('plain',))
content = ''
part = message.get_body(preferencelist=('plain','html'))
if part:
content = part.get_content()
try: content = part.get_content()
except (LookupError, UnicodeError, ValueError) as e: raise Failure('Mail body encoding is not supported; convert to UTF-8 first.') from e
if not isinstance(content,str): raise Failure('Mail body is not readable text.')
notes += '\n\n' + content
if len(notes.encode()) > 16384: raise Failure('Mail notes exceed 16 KiB; omit --include-body.')
return title, notes, clean(message.get('Message-ID')) or hashlib.sha256(raw).hexdigest()
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
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
def tasks(client, profile, words):
@@ -508,7 +530,9 @@ def tasks(client, profile, words):
p=sub.add_parser(name)
if name=='add':p.add_argument('title')
elif name=='update':p.add_argument('uuid',type=task_uuid);p.add_argument('--title')
else:p.add_argument('--title');p.add_argument('--include-body',action='store_true')
else:
p.add_argument('--title');p.add_argument('--wiki-prefix',help='Wiki parent Path (otherwise use personal default or project Wiki subtree)')
body=p.add_mutually_exclusive_group();body.add_argument('--include-body',action='store_true',help='Save body to Wiki (default; retained for compatibility)');body.add_argument('--metadata-only',action='store_true',help='Do not upload body or create a Wiki document')
p.add_argument('--notes');p.add_argument('--project');p.add_argument('--assignee')
p.add_argument('--due');p.add_argument('--start-on');p.add_argument('--planned');p.add_argument('--review')
p.add_argument('--estimate',type=int);p.add_argument('--complexity',type=int,choices=range(1,6))
@@ -518,7 +542,7 @@ def tasks(client, profile, words):
sub.add_parser('stop');sub.add_parser('status')
p=sub.add_parser('retry');p.add_argument('request_id',type=task_uuid)
p=sub.add_parser('report');p.add_argument('--from',dest='start');p.add_argument('--to',dest='end')
p=sub.add_parser('preferences');p.add_argument('--project');p.add_argument('--assignee');p.add_argument('--due-days',type=int);p.add_argument('--start-days',type=int);p.add_argument('--planned-days',type=int);p.add_argument('--review-days',type=int);p.add_argument('--estimate',type=int);p.add_argument('--complexity',type=int);p.add_argument('--digest',choices=['on','off']);p.add_argument('--at');p.add_argument('--timezone')
p=sub.add_parser('preferences');p.add_argument('--wiki-prefix');p.add_argument('--project');p.add_argument('--assignee');p.add_argument('--due-days',type=int);p.add_argument('--start-days',type=int);p.add_argument('--planned-days',type=int);p.add_argument('--review-days',type=int);p.add_argument('--estimate',type=int);p.add_argument('--complexity',type=int);p.add_argument('--digest',choices=['on','off']);p.add_argument('--at');p.add_argument('--timezone')
for child in sub.choices.values():child.add_argument('--json',action='store_true')
a=parser.parse_args(words);action=a.action
def emit(data): print(json.dumps(data,ensure_ascii=False,indent=2))
@@ -527,8 +551,10 @@ def tasks(client, profile, words):
if len(pending)>=20 and payload['request_id'] not in pending:raise Failure('20 pending captures retained. Retry them before adding more.')
pending[payload['request_id']]=payload;profile.save()
try: result=client.json(TASK_API+'capture',method='POST',data=payload)
except Failure:
print('Draft retained; retry with: pages task retry '+payload['request_id'],file=sys.stderr);raise
except Failure as e:
print('Draft retained; retry with: pages task retry '+payload['request_id'],file=sys.stderr)
if e.payload.get('error') == 'wiki_prefix_required': raise Failure('Set a private Wiki destination first: pages task preferences --wiki-prefix /YOUR/MAIL/PATH',e.status) from e
raise
del pending[payload['request_id']];profile.save();return result
if action in ('list','projects'):
query={'kind':'project' if action=='projects' else 'task','view':getattr(a,'view','all'),'limit':100,'q':getattr(a,'q',''),'before':getattr(a,'before','')}
@@ -542,7 +568,7 @@ def tasks(client, profile, words):
if action=='show':emit(client.json(TASK_API+'items/'+a.uuid));return
if action=='preferences':
data=client.json(TASK_API+'preferences');p=data['preferences'];changed=False
for attr,key in [('project','project_uuid'),('assignee','assignee_uuid'),('due_days','due_days'),('start_days','start_days'),('planned_days','planned_days'),('review_days','review_days'),('estimate','estimate_minutes'),('complexity','complexity'),('at','digest_time'),('timezone','timezone')]:
for attr,key in [('wiki_prefix','wiki_prefix'),('project','project_uuid'),('assignee','assignee_uuid'),('due_days','due_days'),('start_days','start_days'),('planned_days','planned_days'),('review_days','review_days'),('estimate','estimate_minutes'),('complexity','complexity'),('at','digest_time'),('timezone','timezone')]:
value=getattr(a,attr)
if value is not None:p[key]=task_project(client,value) if attr=='project' else value;changed=True
if a.digest:p['digest_enabled']=a.digest=='on';changed=True
@@ -554,8 +580,11 @@ def tasks(client, profile, words):
if action in ('add','from-mail'):
payload={'request_id':getattr(a,'request_id',None) or str(uuid.uuid4()),'title':a.title,'notes':a.notes or ''}
if action=='from-mail':
title,notes,message_id=task_mail(sys.stdin.buffer.read((2<<20)+1),a.include_body,a.title)
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)
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 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