fix: normalize mailing-list subjects and preserve long Japanese titles
This commit is contained in:
1 parent
cde94ed936
commit
541c2bd069
5 files changed
+43
-9
No files matched your search
+2
-2
@@ -18,8 +18,8 @@ rpm/debは初期配布の必須条件にせず、LinuxはELFを提供する。Py
|
||||
|
||||
```sh
|
||||
# ダウンロード先で実行。手元にあるファイルをチェックする。
|
||||
sha256sum --ignore-missing -c pages-cli-v0.3.0-linux-amd64-SHA256SUMS
|
||||
tar -xzf pages-cli-v0.3.0-linux-amd64.tar.gz
|
||||
sha256sum --ignore-missing -c pages-cli-v0.3.1-linux-amd64-SHA256SUMS
|
||||
tar -xzf pages-cli-v0.3.1-linux-amd64.tar.gz
|
||||
mkdir -p ~/.local/bin
|
||||
install -m 755 pages ~/.local/bin/pages
|
||||
pages --version
|
||||
|
||||
+4
-2
@@ -26,9 +26,11 @@ v0.3.0からは件名をタスク名、From・Date・Message-IDをメモにし
|
||||
|
||||
本文の公開範囲は保存先Wikiの権限に従う。タスクが個人用でも、公開Pathに保存した本文は公開され得るため、メール用には閲覧者を制限したPathを指定する。設定例の `/private/mail` という名前自体が非公開を保証するものではない。
|
||||
|
||||
作成Pathは `親Path/安全化した件名--request_id`。同名件名を区別し、既存ページを上書きしない。タスクのメモ閲覧・編集ボタンから本文ページを開ける。タスク・Wiki・再送receiptは同一トランザクションで保存し、途中失敗で片方だけを残さない。UUIDで紐づくので、その後Wikiを移動しても追従する。
|
||||
PagesWiki v0.8.1以降の作成Pathは `親Path/mail-request_id`。request_idは接続先・本人・Message-IDから生成するUUIDなので、末尾は件名や文字コードに依存しないASCIIになる。Message-ID自体をURLへ露出せず、同じ件名でも別メールなら区別する。Message-IDがなければメール原文のSHA-256を識別に使う。既存ページは移動・上書きせず、従来形式で保存済みの要求をretryしても元のページを返す。タスクのメモ閲覧・編集ボタンから本文ページを開ける。タスク・Wiki・再送receiptは同一トランザクションで保存し、途中失敗で片方だけを残さない。UUIDで紐づくので、その後Wikiを移動しても追従する。
|
||||
|
||||
plain textを優先し、HTMLしかない場合はテキストを抽出する。添付・外部画像・CSS・JS・外部URLは取得せず、本文中のHTMLやWikiマクロも実行しない。原文EMLやHTMLレイアウトの保存機能ではない。入力1通2 MiB、Wiki本文(メタデータ込み)256 KiB、タスクメモ16 KiBまで。上限超過は切り捨てず拒否する。件名300 UTF-8 bytes超は `--title '短い件名'` を指定する。旧 `--include-body` は既定動作の別名として残す。以前の本文非送信動作には `--metadata-only` を使う。
|
||||
CLI v0.3.1ではMIMEヘッダを復号した件名の先頭から `[list 123]`・`[list:123]`・`[list#123]` のような連番を除去する。Re:/Fwd:は維持し、その後の連番も除去する。`[重要]`、件名の途中の括弧、`--title` で明示した件名は変更しない。自動件名が300 UTF-8 bytesを超える場合は文字境界で省略して末尾に「…」を付ける。除去・省略・手動指定で変わった元のSubjectはWiki本文に残す。手動件名の上限超過は拒否する。
|
||||
|
||||
plain textを優先し、HTMLしかない場合はテキストを抽出する。添付・外部画像・CSS・JS・外部URLは取得せず、本文中のHTMLやWikiマクロも実行しない。原文EMLやHTMLレイアウトの保存機能ではない。入力1通2 MiB、Wiki本文(メタデータ込み)256 KiB、タスクメモ16 KiBまで。本文・メモの上限超過は切り捨てず拒否する。旧 `--include-body` は既定動作の別名として残す。以前の本文非送信動作には `--metadata-only` を使う。
|
||||
|
||||
同じ接続・本人・Message-IDから同じrequest_idを作るため、同じメールの再取り込みで重複しない。Message-IDなしはメール全体のSHA-256を使う。同じIDで本文・指定Path・オプションを変えると412となり、以前のページを上書きしない。旧版でメタデータのみ取り込み済みのメールに本文を追加する場合は、既存の関連Wikiを編集する。同じメールを別の独立タスクにもしたい場合は `task add` を使う。
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
+19
-1
@@ -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]
|
||||
|
||||
Reference in new issue
Block a user