diff --git a/docs/design/ja/task-productivity-contacts.md b/docs/design/ja/task-productivity-contacts.md index 60e7da5..a771c88 100644 --- a/docs/design/ja/task-productivity-contacts.md +++ b/docs/design/ja/task-productivity-contacts.md @@ -20,7 +20,17 @@ captureは省略した項目に本人の既定値を適用する。日付の空 Webの新規作成・クイック追加・サブタスク追加でも既定値を読む。サブタスクのプロジェクト・優先度・bucketは親の所属を優先する。既存汎用items APIとCalDAVではクライアント指定を尊重し、新しいcaptureの既定値を暗黙適用しない。 -同じrequest_id・同じ要求内容の再送は既存タスクを返す。最初の保存後に既定値やタスクが更新されても作り直さない。同じrequest_idで別内容を送ると412。要求のreceiptとタスクRevisionは同一トランザクションで保存する。API入力は32 KiBまで、件名300 UTF-8 bytes、メモ16 KiBまで。 +同じrequest_id・同じ要求内容の再送は既存タスクを返す。最初の保存後に既定値やタスクが更新されても作り直さない。同じrequest_idで別内容を送ると412。要求のreceiptとタスクRevisionは同一トランザクションで保存する。captureのJSON入力は2 MiBまで、その他の作成・更新は32 KiBまで。件名300 UTF-8 bytes、メモ16 KiBまで。 + +### メール本文のWiki保存 + +v0.8.0ではcaptureに省略可能な `wiki_body` と `wiki_prefix` を追加する。wiki_bodyを省略した従来クライアントの契約・receipt hashは変えない。本文を指定した場合、親Pathは要求のwiki_prefix、個人preferencesのwiki_prefix、選択プロジェクトの関連WikiとNotesSubpath(未設定はtasks)の順に解決する。いずれもなければ422 `wiki_prefix_required`。空の明示指定も拒否する。個人設定はDomain・本人単位で、既存preferences JSONに保存するため新規schema migrationは不要。 + +本文は256 KiBまでのUTF-8テキスト。H1に件名、preに本文をHTMLエスケープして格納し、波括弧も実体参照にしてマクロを実行しない。Pathは `親Path/安全化した件名--request_id`。既存Pathとの競合は409で、既存ページへ追記・上書きしない。新規Wikiのobject/path/revisionとactivity outbox、タスクのPageUUID、capture receiptは同一DBトランザクションで保存する。rollback時に未参照のcontent-addressed Documentファイルが残る可能性は既存DocumentStoreと同様だが、公開可能なページや片方だけのタスクは残さない。 + +保存前に対象Pathへのread/create/write権限を検証する。個人タスクの所有権はWikiへの権限を付与しない。Wiki本文の公開範囲は保存先のACLに従うため、個人設定UIに注意を表示する。再送は既定値の再解決より前にreceiptを確認し、既定Pathの変更で重複作成しない。成功応答はitemとpage_pathを返し、タスクの既存メモ操作から関連Wikiへ移動できる。 + +個人設定の既定プロジェクトは保存済みUUIDを選択状態・既定選択状態の双方に反映する。最初の100候補にない場合も本人のitemを取得して名前を表示し、取得できない場合はUUIDを保持して別項目の保存で意図せず解除しない。CLIの本文抽出・再送・NeoMutt設定は独立pages-cliリポジトリを正本とする。 calendar-captureも同じUIDを再利用し、再送で既存予定を上書きしない。時刻は `2026-11-02T09:00` のドメイン時刻、またはオフセット付きRFC 3339。終了は開始後、最大31日。予定内容を取得する前にPersonalLocal制約をロック内で検査する。外部取り込みカレンダーの内容を読まない。 diff --git a/docs/operations/releases/2026-11-06-mail-wiki.md b/docs/operations/releases/2026-11-06-mail-wiki.md new file mode 100644 index 0000000..398f9cb --- /dev/null +++ b/docs/operations/releases/2026-11-06-mail-wiki.md @@ -0,0 +1,29 @@ +# メール本文のWiki保存 + +PagesWiki v0.8.0とpages-cli v0.3.0で、NeoMuttからのメール取り込みを関連Wikiページ付きタスク作成へ拡張する。サーバーを先に更新し、その後CLIを更新する。 + +## 利用方法と互換性 + +個人設定 `/-/sys/config?panel=organizer` に「メール本文の既定Wiki親Path」を追加した。既定プロジェクトも現在の選択を表示し、候補の先頭100件にない場合は個別取得する。 + +```sh +pages task preferences --wiki-prefix /YOUR/MAIL/PATH +pages task from-mail < message.eml +``` + +メール本文は保存先Wikiの公開範囲・閲覧権限に従う。非公開を保証する特別なPath名はないため、保存先の権限を先に確認する。タスクのメモ閲覧・編集から本文ページを開ける。タスクとWikiは同一トランザクションで保存する。 + +CLIの本文非送信は `--metadata-only`。旧 `--include-body` は既定動作の別名として残す。旧版で取り込み済みのメールに異なる内容を再送すると412となり、本文追加のために既存ページを暗黙上書きしない。 + +サーバーの新しいcaptureフィールドは任意で、従来CLIの要求hashを維持する。schema migrationはなく、WikiPrefixは既存preferences JSONに追加する。旧版へ切り戻す場合、新CLIのWiki保存を停止する。旧サーバーで個人設定を再保存すると新フィールドを落とす可能性があるので注意する。 + +## ソースと検証 + +独立CLIの対象コミットは `cde94ed`。サーバー内の `pages-cli/pages` と `test_productivity.py` はこのソースに明示更新した結合テスト用スナップショットで、配布元ではない。 + +- CLI単体テスト41件、合成HTTP経由のログイン・メール取り込み・連絡先・ログアウト。 +- GoのメールWiki作成、既定Path・明示Path・プロジェクト配下、再送、権限拒否、衝突、本文上限、トランザクションのロールバック。 +- 独立CLIスナップショットとGo APIの結合テスト。作成したページがタスクのメモ操作から開けることを確認。 +- Chromiumのデスクトップ・スマホ幅で、現在のプロジェクト選択・フォームリセット・Wiki Path保存を確認。デスクトップでは候補の先頭ページにないプロジェクトも検証。 + +配布・本番切替結果は確認後に追記する。外部取り込みカレンダーや実メール本文を検証のために読まない。 diff --git a/internal/server/productivity_browser_test.go b/internal/server/productivity_browser_test.go index be22417..c7a47ee 100644 --- a/internal/server/productivity_browser_test.go +++ b/internal/server/productivity_browser_test.go @@ -23,6 +23,20 @@ func TestProductivityChromium(t *testing.T) { for _, size := range sizes { t.Run(view+"-"+size, func(t *testing.T) { s, token, domain, actor := taskFixture(t) + if view == "settings" { + project, err := s.store.SaveTask(context.Background(), domain, actor, newTaskRecord("project", "Saved default project"), 0) + if err != nil { + t.Fatal(err) + } + p, err := s.store.TaskPreferences(context.Background(), domain, actor) + if err != nil { + t.Fatal(err) + } + p.ProjectUUID = project.UUID + if _, err = s.store.SaveTaskPreferences(context.Background(), domain, actor, p, time.Now()); err != nil { + t.Fatal(err) + } + } if size == "english" { if err := s.store.SetConfigInDomain(domain, "locale", "en"); err != nil { t.Fatal(err) @@ -50,6 +64,11 @@ func TestProductivityChromium(t *testing.T) { path := map[string]string{"contacts": "/v/plan/contacts", "settings": "/-/sys/config?panel=organizer", "report": "/v/plan/dashboard", "tasks": "/v/plan/tasks", "home": "/", "editor": "/v/plan/task/-/new"}[view] server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { r.Header.Set("Authorization", "Bearer "+token) + // Simulate a saved project outside the first project list page. + if view == "settings" && size == "desktop" && r.URL.Path == "/_/api/v1/extension/organizer.tasks/items" && r.URL.Query().Get("kind") == "project" { + writeEventJSON(w, 200, map[string]any{"items": []any{}, "next": "fixture"}) + return + } if r.Method == "GET" && r.URL.RequestURI() == path { rec := httptest.NewRecorder() s.withAuthorization(s.mux).ServeHTTP(rec, r) diff --git a/internal/server/task_capture_wiki_test.go b/internal/server/task_capture_wiki_test.go new file mode 100644 index 0000000..6fd38ec --- /dev/null +++ b/internal/server/task_capture_wiki_test.go @@ -0,0 +1,173 @@ +package server + +import ( + "context" + "database/sql" + "encoding/json" + "errors" + "strings" + "testing" + "time" + + "github.com/google/uuid" + "pageswiki/internal/store" +) + +func TestTaskMailWikiCapture(t *testing.T) { + s, token, domain, actor := taskFixture(t) + ctx := context.Background() + p, err := s.store.TaskPreferences(ctx, domain, actor) + if err != nil { + t.Fatal(err) + } + for _, bad := range []string{"mail", "/a/../b", "/_/api", "/v/blog", "/a?x", "/a//b", "/a\nb"} { + p.WikiPrefix = bad + if w := taskRequest(s, token, "PUT", "preferences", p, ""); w.Code != 422 { + t.Fatal(bad, w.Code) + } + } + p.WikiPrefix = "/private/mail" + if _, err = s.store.SaveTaskPreferences(ctx, domain, actor, p, time.Now()); err != nil { + t.Fatal(err) + } + id := uuid.NewString() + input := map[string]any{"request_id": id, "title": "Mail / {{children}}", "notes": "From: sender@example.invalid", "wiki_body": "Hello\n {{children}}"} + w := taskRequest(s, token, "POST", "capture", input, "") + if w.Code != 201 { + t.Fatal(w.Code, w.Body) + } + var response struct { + Item store.TaskRecord `json:"item"` + Path string `json:"page_path"` + } + if err = json.Unmarshal(w.Body.Bytes(), &response); err != nil { + t.Fatal(err) + } + target := store.TaskCaptureWikiPath(p.WikiPrefix, input["title"].(string), id) + if response.Item.PageUUID == "" || response.Path != target || strings.Contains(response.Item.Notes, "bad()") { + t.Fatal(w.Body) + } + rev, err := s.store.GetLatestRevision(response.Item.PageUUID) + if err != nil || !strings.Contains(rev.Body, "<script>bad()</script>") || strings.Contains(rev.Body, "{{children}}") { + t.Fatal(rev, err) + } + // A committed retry remains stable even if the preference changes. + p, err = s.store.TaskPreferences(ctx, domain, actor) + if err != nil { + t.Fatal(err) + } + p.WikiPrefix = "/elsewhere" + if _, err = s.store.SaveTaskPreferences(ctx, domain, actor, p, time.Now()); err != nil { + t.Fatal(err) + } + if w = taskRequest(s, token, "POST", "capture", input, ""); w.Code != 201 || !strings.Contains(w.Body.String(), response.Item.PageUUID) { + t.Fatal(w.Code, w.Body) + } + input["wiki_body"] = "different" + if w = taskRequest(s, token, "POST", "capture", input, ""); w.Code != 412 { + t.Fatal(w.Code, w.Body) + } + input["request_id"] = uuid.NewString() + input["wiki_prefix"] = "/explicit" + if w = taskRequest(s, token, "POST", "capture", input, ""); w.Code != 201 || !strings.Contains(w.Body.String(), "/explicit/") { + t.Fatal(w.Code, w.Body) + } +} + +func TestTaskMailWikiRollbackAndAccess(t *testing.T) { + s, token, domain, actor := taskFixture(t) + ctx := context.Background() + id := uuid.NewString() + title := "Mail" + input := map[string]any{"request_id": id, "title": title, "wiki_body": "Mail body"} + if w := taskRequest(s, token, "POST", "capture", input, ""); w.Code != 422 || !strings.Contains(w.Body.String(), "wiki_prefix_required") { + t.Fatal(w.Code, w.Body) + } + input["wiki_prefix"] = "/mail" + target := store.TaskCaptureWikiPath("/mail", title, id) + absent := func() { + t.Helper() + if _, e := s.store.ResolvePathInDomain(domain, target); !errors.Is(e, sql.ErrNoRows) { + t.Fatal("orphan Wiki", e) + } + if _, e := s.store.GetTask(ctx, domain, actor, id); !errors.Is(e, sql.ErrNoRows) { + t.Fatal("orphan task", e) + } + } + // Failure after document insert must roll back the entire capture, including receipt. + if _, e := s.store.DB.Exec(`CREATE TRIGGER reject_capture BEFORE INSERT ON organizer_changes BEGIN SELECT RAISE(ABORT,'fixture'); END`); e != nil { + t.Fatal(e) + } + if w := taskRequest(s, token, "POST", "capture", input, ""); w.Code != 500 { + t.Fatal(w.Code, w.Body) + } + absent() + if _, e := s.store.DB.Exec(`DROP TRIGGER reject_capture`); e != nil { + t.Fatal(e) + } + for _, body := range []string{strings.Repeat("x", 256<<10+1), "bad\x00text"} { + input["wiki_body"] = body + if w := taskRequest(s, token, "POST", "capture", input, ""); w.Code != 422 { + t.Fatal(w.Code) + } + absent() + } + input["wiki_body"] = "Mail body" + other, e := s.store.CreatePasswordAccount(domain, "mailother", "long fixture password", "Other", "") + if e != nil { + t.Fatal(e) + } + otherToken, e := s.store.CreateAuthSession(domain, other.UUID) + if e != nil { + t.Fatal(e) + } + if w := taskRequest(s, otherToken, "POST", "capture", input, ""); w.Code != 403 { + t.Fatal("Wiki ACL", w.Code, w.Body) + } + absent() + existing, e := s.store.CreatePageAtPathInDomain(domain, target) + if e != nil { + t.Fatal(e) + } + if w := taskRequest(s, token, "POST", "capture", input, ""); w.Code != 409 { + t.Fatal("collision", w.Code, w.Body) + } + if got, e := s.store.ResolvePathInDomain(domain, target); e != nil || got != existing { + t.Fatal("overwrite", e) + } + if _, e = s.store.GetTask(ctx, domain, actor, id); !errors.Is(e, sql.ErrNoRows) { + t.Fatal("orphan collision task", e) + } + input["request_id"] = uuid.NewString() + if w := taskRequest(s, token, "POST", "capture", input, ""); w.Code != 201 { + t.Fatal(w.Code, w.Body) + } +} + +func TestTaskMailWikiProjectFallback(t *testing.T) { + s, token, domain, actor := taskFixture(t) + ctx := context.Background() + root, e := s.store.CreatePageAtPathInDomain(domain, "/projects/app") + if e != nil { + t.Fatal(e) + } + project := newTaskRecord("project", "App") + project.PageUUID = root + project.NotesSubpath = "mail" + project, e = s.store.SaveTask(ctx, domain, actor, project, 0) + if e != nil { + t.Fatal(e) + } + p, e := s.store.TaskPreferences(ctx, domain, actor) + if e != nil { + t.Fatal(e) + } + p.ProjectUUID = project.UUID + if _, e = s.store.SaveTaskPreferences(ctx, domain, actor, p, time.Now()); e != nil { + t.Fatal(e) + } + input := map[string]any{"request_id": uuid.NewString(), "title": "Project mail", "wiki_body": "Body"} + if w := taskRequest(s, token, "POST", "capture", input, ""); w.Code != 201 || !strings.Contains(w.Body.String(), "/projects/app/mail/") { + t.Fatal(w.Code, w.Body) + } +} diff --git a/internal/server/task_productivity.go b/internal/server/task_productivity.go index d201e22..3bb8ff5 100644 --- a/internal/server/task_productivity.go +++ b/internal/server/task_productivity.go @@ -2,8 +2,10 @@ package server import ( "crypto/sha256" + "database/sql" "encoding/hex" "encoding/json" + "errors" "io" "net/http" "strings" @@ -15,7 +17,11 @@ import ( ) func taskJSON(w http.ResponseWriter, r *http.Request, output any) bool { - d := json.NewDecoder(http.MaxBytesReader(w, r.Body, 32768)) + return taskJSONLimit(w, r, output, 32768) +} + +func taskJSONLimit(w http.ResponseWriter, r *http.Request, output any, limit int64) bool { + d := json.NewDecoder(http.MaxBytesReader(w, r.Body, limit)) d.DisallowUnknownFields() if d.Decode(output) != nil || d.Decode(&struct{}{}) != io.EOF { taskError(w, store.ErrTaskInvalid) @@ -61,6 +67,8 @@ type taskCaptureInput struct { ReviewOn *string `json:"review_on"` Estimate *int `json:"estimate_minutes"` Complexity *int `json:"complexity"` + WikiBody *string `json:"wiki_body,omitempty"` + WikiPrefix *string `json:"wiki_prefix,omitempty"` } func (s *Server) handleTaskProductivity(w http.ResponseWriter, r *http.Request, actor string, active bool, kind string) { @@ -181,13 +189,23 @@ func (s *Server) handleTaskProductivity(w http.ResponseWriter, r *http.Request, case "capture": if r.Method == "POST" { var input taskCaptureInput - if !taskJSON(w, r, &input) { + if !taskJSONLimit(w, r, &input, 2<<20) { return } if id, err := uuid.Parse(input.RequestID); err != nil || id.String() != input.RequestID { taskError(w, store.ErrTaskInvalid) return } + raw, _ := json.Marshal(input) + hash := sha256.Sum256(raw) + requestHash := hex.EncodeToString(hash[:]) + if old, e := s.store.CapturedTask(r.Context(), domain, actor, input.RequestID, requestHash); e == nil { + s.taskResponse(w, r, old, 201) + return + } else if !errors.Is(e, sql.ErrNoRows) { + taskError(w, e) + return + } p, err := s.store.TaskPreferences(r.Context(), domain, actor) if err != nil { taskError(w, err) @@ -211,9 +229,49 @@ func (s *Server) handleTaskProductivity(w http.ResponseWriter, r *http.Request, item.Complexity = *input.Complexity } applyTaskReminderDefaults(&item, p) - raw, _ := json.Marshal(input) - hash := sha256.Sum256(raw) - item, err = s.store.CaptureTask(r.Context(), domain, actor, input.RequestID, hex.EncodeToString(hash[:]), item) + target, body := "", "" + if input.WikiBody != nil { + prefix := p.WikiPrefix + if input.WikiPrefix != nil { + prefix = *input.WikiPrefix + } + if prefix == "" && input.WikiPrefix == nil && item.ProjectUUID != "" { + project, e := s.store.GetTask(r.Context(), domain, actor, item.ProjectUUID) + if e != nil && !errors.Is(e, sql.ErrNoRows) { + taskError(w, e) + return + } + if e == nil && project.Kind == "project" && !project.Archived { + if base := s.taskPagePath(r, project.PageUUID); base != "" { + sub := project.NotesSubpath + if sub == "" { + sub = "tasks" + } + prefix = strings.TrimRight(base, "/") + "/" + sub + } + } + } + if prefix == "" { + writeEventJSON(w, 422, map[string]string{"error": "wiki_prefix_required"}) + return + } + if !store.ValidTaskWikiPrefix(prefix) { + taskError(w, store.ErrTaskInvalid) + return + } + target = store.TaskCaptureWikiPath(prefix, item.Title, input.RequestID) + for _, capability := range []string{"read", "create", "write"} { + if s.authorizePathRequest(r, capability, target) != nil { + taskError(w, store.ErrAccessDenied) + return + } + } + body = *input.WikiBody + } else if input.WikiPrefix != nil { + taskError(w, store.ErrTaskInvalid) + return + } + item, err = s.store.CaptureTaskWithWiki(r.Context(), domain, actor, input.RequestID, requestHash, item, target, body) if err != nil { taskError(w, err) return diff --git a/internal/server/testdata/productivity-browser.js b/internal/server/testdata/productivity-browser.js index 3b7d7d1..468659f 100644 --- a/internal/server/testdata/productivity-browser.js +++ b/internal/server/testdata/productivity-browser.js @@ -31,7 +31,12 @@ window.addEventListener('load',async()=>{const wait=async(fn)=>{for(let i=0;i<12 const exported=await fetch('/_/api/v1/extension/organizer.contacts/export.vcf').then(r=>r.text());if(!exported.includes('ブラウザ連絡先'))throw Error('Export missing'); document.querySelector('#tasks-dav-open').click();await wait(()=>document.querySelector('#tasks-dav-dialog').open);const dav=document.querySelector('#tasks-dav-dialog');if(getComputedStyle(dav.querySelector('.dav-compat-toggle')).display!=='none')throw Error('CalDAV-only compatibility visible');fill(dav.querySelector('form>input'),'Browser contact client');dav.querySelector('form').requestSubmit();await wait(()=>!dav.querySelector('section').hidden);const credentials=await fetch('/_/api/v1/caldav-credentials').then(r=>r.json());if(credentials.items.length!==1||credentials.items[0].scope!=='contacts')throw Error('Wrong credential scope');dav.querySelector('tbody button').click();await wait(()=>!dav.querySelector('tbody button'));dav.querySelector('header button').click(); }else if(path==='/-/sys/config'){ + await wait(()=>document.querySelector('[name=project_uuid] option:checked')?.textContent==='Saved default project'); + const savedProject=document.querySelector('[name=project_uuid]').value; + document.querySelector('.productivity-settings').reset();if(document.querySelector('[name=project_uuid]').value!==savedProject)throw Error('Default project reset lost selection'); + fill(document.querySelector('[name=wiki_prefix]'),'/private/mail'); await wait(()=>document.querySelector('[name=due_days]'));fill(document.querySelector('[name=due_days]'),'2');document.querySelector('[name=notify_due]').click();document.querySelector('[name=notify_planned]').click();fill(document.querySelector('[name=reminder_time]'),'08:45');document.querySelector('.productivity-settings').requestSubmit();await wait(()=>document.querySelector('#productivity-status').textContent.includes('保存'));const d=await fetch('/_/api/v1/extension/organizer.tasks/preferences').then(r=>r.json());if(d.preferences.due_days!==2||!d.preferences.notify_due||!d.preferences.notify_planned||d.preferences.reminder_time!=='08:45'||!d.defaults.notify_due||d.defaults.notify_planned)throw Error('Preferences missing'); + if(d.preferences.project_uuid!==savedProject||d.preferences.wiki_prefix!=='/private/mail')throw Error('Wiki destination or existing default project lost'); }else if(path==='/v/plan/dashboard'){ await wait(()=>document.querySelectorAll('.productivity-day').length>300);if(!document.querySelector('.productivity-day[data-level="2"]'))throw Error('Seven-point completion missing'); if(document.querySelectorAll('[data-widget]').length!==6)throw Error('Widget registry missing'); diff --git a/internal/store/task_capture_wiki.go b/internal/store/task_capture_wiki.go new file mode 100644 index 0000000..21380dd --- /dev/null +++ b/internal/store/task_capture_wiki.go @@ -0,0 +1,80 @@ +package store + +import ( + "context" + "database/sql" + "encoding/json" + "html" + "path" + "strings" + "time" + "unicode" + "unicode/utf8" + + "github.com/google/uuid" +) + +func ValidTaskWikiPrefix(p string) bool { + if p == "" { + return true + } + if len(p) > 500 || !utf8.ValidString(p) || !strings.HasPrefix(p, "/") || path.Clean(p) != p || strings.ContainsAny(p, "\\?#%:") || strings.IndexFunc(p, unicode.IsControl) >= 0 { + return false + } + for _, part := range strings.Split(p, "/") { + if part == "_" || part == ".." || part == "." { + return false + } + } + first := strings.Split(strings.TrimPrefix(p, "/"), "/")[0] + switch first { + case "-", "v", "e", "o", "r", "static", "raw": + return false + } + return true +} + +// A request UUID suffix separates equal subjects and makes the target stable. +func TaskCaptureWikiPath(prefix, title, request string) string { + item := TaskRecord{Title: title, UUID: request} + name := path.Base(TaskNotePath("", TaskRecord{}, item)) + return strings.TrimRight(prefix, "/") + "/" + name + "--" + request +} + +func (s *Store) createCapturedTaskWiki(ctx context.Context, tx *sql.Tx, domain, actor string, item *TaskRecord, target, body string) error { + if target == "" || !ValidTaskWikiPrefix(path.Dir(target)) || path.Clean(target) != target || item.PageUUID != "" || len(body) > 256<<10 || !utf8.ValidString(body) || strings.IndexByte(body, 0) >= 0 { + return ErrTaskInvalid + } + var exists bool + if err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM paths WHERE domain_uuid=? AND path=?)`, domain, target).Scan(&exists); err != nil { + return err + } + if exists { + return ErrTaskNoteExists + } + item.PageUUID = uuid.NewString() + if _, err := tx.ExecContext(ctx, `INSERT INTO objects(uuid,kind,created_at) VALUES(?,'page',strftime('%s','now'))`, item.PageUUID); err != nil { + return err + } + if _, err := tx.ExecContext(ctx, `INSERT INTO paths(domain_uuid,path,object_uuid,created_at) VALUES(?,?,?,strftime('%s','now'))`, domain, target, item.PageUUID); err != nil { + return err + } + // Mail is data, never HTML or executable wiki macros (including {{...}}). + escape := func(v string) string { + return strings.NewReplacer("{", "{", "}", "}").Replace(html.EscapeString(v)) + } + content := "
" + escape(body) + "\n" + now := time.Now() + rev := &Revision{UUID: uuid.NewString(), ObjectUUID: item.PageUUID, DocumentHash: revisionHash(content), Language: "und", ContentType: "text/html; charset=utf-8", Body: content, Message: "Capture mail as task Wiki", CreatedAt: now.Unix(), CreatedAtMS: now.UnixMilli()} + if err := s.persistDocument(rev.DocumentHash, rev.Body); err != nil { + return err + } + if err := insertRevision(tx, rev, nil); err != nil { + return err + } + if err := s.appendRevisionActivityOutboxIfAvailable(tx, rev); err != nil { + return err + } + payload, _ := json.Marshal(map[string]string{"path": target, "object_uuid": item.PageUUID}) + return s.appendActivityOutboxIfAvailable(tx, ActivityOutboxEvent{DomainUUID: domain, ActorUUID: actor, EventType: "page.created", TargetKind: "page", TargetUUID: item.PageUUID, Payload: string(payload), IdempotencyKey: "page.created:" + item.PageUUID}) +} diff --git a/internal/store/task_productivity.go b/internal/store/task_productivity.go index 897fbd7..b9c4bc8 100644 --- a/internal/store/task_productivity.go +++ b/internal/store/task_productivity.go @@ -33,6 +33,7 @@ var taskProductivityMigrations = []db.SQLMigration{{Version: 1, Name: "task_capt type TaskPreferences struct { Version int64 `json:"version"` ProjectUUID string `json:"project_uuid"` + WikiPrefix string `json:"wiki_prefix"` AssigneeUUID string `json:"assignee_uuid"` DueDays *int `json:"due_days"` StartDays *int `json:"start_days"` @@ -89,6 +90,9 @@ func (s *Store) SaveTaskPreferences(ctx context.Context, domain, actor string, p if s.ReadOnly { return p, ErrReadOnly } + if !ValidTaskWikiPrefix(p.WikiPrefix) { + return p, ErrTaskInvalid + } // Older clients do not send this field. Preserve their default clock. if p.ReminderTime == "" { p.ReminderTime = "09:00" @@ -193,6 +197,12 @@ func (s *Store) CapturedTask(ctx context.Context, domain, actor, request, hash s } func (s *Store) CaptureTask(ctx context.Context, domain, actor, request, hash string, item TaskRecord) (TaskRecord, error) { + return s.CaptureTaskWithWiki(ctx, domain, actor, request, hash, item, "", "") +} + +// CaptureTaskWithWiki commits the task, receipt and optional Wiki page together. +// The HTTP caller must authorize read/create/write for the exact target path. +func (s *Store) CaptureTaskWithWiki(ctx context.Context, domain, actor, request, hash string, item TaskRecord, target, body string) (TaskRecord, error) { if _, err := uuid.Parse(request); err != nil { return TaskRecord{}, ErrTaskInvalid } @@ -204,10 +214,18 @@ func (s *Store) CaptureTask(ctx context.Context, domain, actor, request, hash st return old, err } item.UUID = request - out, err := s.saveTask(ctx, domain, actor, item, 0, func(tx *sql.Tx, _ *TaskRecord, _ TaskRecord) error { + out, err := s.saveTask(ctx, domain, actor, item, 0, func(tx *sql.Tx, current *TaskRecord, _ TaskRecord) error { + if target != "" { + if e := s.createCapturedTaskWiki(ctx, tx, domain, actor, current, target, body); e != nil { + return e + } + } _, e := tx.ExecContext(ctx, `INSERT INTO organizer_capture_receipts VALUES(?,?,?,?,?)`, domain, actor, request, hash, item.UUID) return e }) + if err == nil && target != "" { + s.InvalidatePathCaches() + } if err != nil { if old, e := s.CapturedTask(ctx, domain, actor, request, hash); e == nil { return old, nil diff --git a/pages-cli/pages b/pages-cli/pages index b3c5a2f..e82ce20 100755 --- a/pages-cli/pages +++ b/pages-cli/pages @@ -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 @@ -17,6 +18,7 @@ import posixpath import re import secrets import shlex +import ssl import stat import subprocess import sys @@ -28,6 +30,7 @@ import urllib.parse as url import urllib.request as http import webbrowser +VERSION = '0.3.0' class Failure(Exception): def __init__(self, message, status=0, payload=None): @@ -74,14 +77,23 @@ class NoRedirect(http.HTTPRedirectHandler): class Client: - def __init__(self, base, token='', lang='auto'): + def __init__(self, base, token='', lang='auto', *, insecure=False): self.base, self.token, self.lang = origin(base), token, lang - self.opener = http.build_opener(NoRedirect()) + self.insecure = insecure + handlers = [NoRedirect()] + if insecure: + # This client's TLS policy only; never change Python's global defaults. + context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) + context.check_hostname = False + context.verify_mode = ssl.CERT_NONE + context.set_alpn_protocols(['http/1.1']) + handlers.append(http.HTTPSHandler(context=context)) + self.opener = http.build_opener(*handlers) def request(self, route, method='GET', data=None, form=None, limit=4 << 20, etag=None, content_type=None): if not route.startswith('/') or route.startswith('//'): raise Failure('Invalid API route.') - headers = {'Accept': 'application/json', 'User-Agent': 'pages-cli/0.1'} + headers = {'Accept': 'application/json', 'User-Agent': 'pages-cli/' + VERSION} if etag is not None: headers['If-Match'] = etag if self.token: @@ -469,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): @@ -497,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)) @@ -507,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)) @@ -516,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','')} @@ -531,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 @@ -543,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 @@ -670,7 +710,7 @@ def execute(client, profile, words): route = addr[5:] if addr.startswith('path:') else '/o/' + addr if addr.startswith('uuid:') else '/r/view/' + addr[4:] webbrowser.open(client.base + url.quote(route, safe='/:'), new=2) elif cmd == 'edit': - editing = Client(client.base, client.token, language(args.lang)) if args.lang else client + editing = Client(client.base, client.token, language(args.lang), insecure=client.insecure) if args.lang else client editor(editing, addr, args.format) elif cmd == 'put': state = client.snapshot(addr) @@ -739,13 +779,16 @@ def shell(client, profile): def main(argv=None): parser = argparse.ArgumentParser(prog='pages', description=__doc__, epilog='Commands: configure, login, logout, status, shell, ls, cd, pwd, cat, view, get, put, edit, search, open, task, contacts. Use pages COMMAND --help.') + parser.add_argument('--version', action='version', version='pages-cli ' + VERSION) parser.add_argument('--profile', help='Use another configured connection for this invocation') parser.add_argument('--lang', help='auto or an exact language tag for this invocation') + parser.add_argument('-k', '--insecure', action='store_true', help='Skip TLS certificate and hostname verification for this invocation only (unsafe; place before COMMAND)') parser.add_argument('command', nargs='?', default='shell') parser.add_argument('args', nargs=argparse.REMAINDER) args = parser.parse_args(argv) if args.command == 'configure': if args.profile or args.lang: parser.error('Use pages configure set --profile NAME --lang TAG.') + if args.insecure: parser.error('--insecure is invocation-only; configure does not connect or save a TLS bypass.') configure(args.args); return if args.command == 'help': parser.print_help(); return @@ -767,10 +810,12 @@ def main(argv=None): lang = language(args.lang or spec.get('lang', 'auto')) # Hold the profile lock before releasing the configuration lock. profile = stack.enter_context(Profile(name).locked()) + if args.insecure: + print('WARNING: --insecure disables TLS certificate and hostname verification for this invocation. Credentials and data can be intercepted; this option is not saved.', file=sys.stderr) if args.command == 'login': if not login_args.oidc and not spec.get('username'): raise Failure('Set your account with pages configure --username NAME, or use pages login --oidc.') if profile.data.get('token'): raise Failure('Log out this profile before replacing its session.') - client = Client(spec['origin'], lang=lang) + client = Client(spec['origin'], lang=lang, insecure=args.insecure) if login_args.oidc: expires, username = client.login_oidc(login_args.no_browser, spec.get('username', '')) else: @@ -799,7 +844,7 @@ def main(argv=None): if args.command != 'logout' and (profile.data.get('origin') != spec['origin'] or profile.data.get('configured_username', profile.data.get('username')) != spec.get('username', '')): raise Failure('Session and configured identity differ. Log out before reconnecting.') if args.command not in ('logout', 'status') and profile.data.get('expires', 0) <= time.time(): raise Failure('Session expired; log out and log in again.') - client = Client(profile.data['origin'], profile.data['token'], lang) + client = Client(profile.data['origin'], profile.data['token'], lang, insecure=args.insecure) if args.command == 'shell': shell(client, profile) else: execute(client, profile, [args.command] + args.args) diff --git a/pages-cli/test_productivity.py b/pages-cli/test_productivity.py index b1c708a..2306bf0 100644 --- a/pages-cli/test_productivity.py +++ b/pages-cli/test_productivity.py @@ -11,13 +11,43 @@ m=runpy.run_path(str(Path(__file__).with_name('pages'))) class ProductivityTests(unittest.TestCase): def test_mail_privacy_and_long_subject_override(self): raw=b'Subject: Test\r\nFrom: Person
Hello
World & friends
' + body=m['task_mail'](raw)[3] + self.assertIn('Hello\n',body);self.assertIn('World & friends',body) + for value in ('secret','evil','https://'):self.assertNotIn(value,body) + for body in (b'a'*(256<<10),b'bad\0text'): + with self.assertRaises(m['Failure']):m['task_mail'](b'Subject: Test\r\n\r\n'+body) + + def test_mail_payload_wiki_and_metadata_only(self): + from unittest.mock import patch + raw=b'Subject: Test\r\nMessage-ID:HTML alternate
',subtype='html') + msg.add_attachment(b'ATTACHMENT',maintype='application',subtype='octet-stream',filename='secret.bin') + body=m['task_mail'](msg.as_bytes())[3] + self.assertIn('Plain body',body);self.assertNotIn('alternate',body);self.assertNotIn('ATTACHMENT',body) + def test_capture_retains_request_until_explicit_retry(self): calls=[] def send(route,**kwargs): @@ -42,3 +72,12 @@ class ProductivityTests(unittest.TestCase): for cmd in [('task','--help'),('contacts','--help'),('task','add','--help')]: with contextlib.redirect_stdout(io.StringIO()),self.assertRaises(SystemExit) as raised:m['main'](list(cmd)) self.assertEqual(raised.exception.code,0) + + def test_neomutt_query_leading_dash_is_data(self): + calls=[] + def query(route): + calls.append(route) + return {'items':[{'name':'Person','emails':['person@example.invalid'],'organization':''}]} + with contextlib.redirect_stdout(io.StringIO()): + m['contacts'](SimpleNamespace(json=query),['query','--neomutt','--','--unexpected']) + self.assertIn('q=--unexpected',calls[0]) diff --git a/pages-cli/test_task_integration.py b/pages-cli/test_task_integration.py index 8d1dd0a..7a6211d 100644 --- a/pages-cli/test_task_integration.py +++ b/pages-cli/test_task_integration.py @@ -5,6 +5,8 @@ import json from pathlib import Path import runpy import sys +from types import SimpleNamespace +from unittest.mock import patch def run(): m=runpy.run_path(str(Path(__file__).with_name('pages'))) @@ -26,6 +28,16 @@ def run(): task('done',ident) 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: