From ebd81d17865771f3f1cdeb8e3f5681f4ab4224cc Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 09:36:46 -0500 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9C=A8=20inbox:=20mail-in=20files=20as?= =?UTF-8?q?=20email=20items=20with=20an=20owner=20approval=20queue=20(Bett?= =?UTF-8?q?y=20C2,=20part=205)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit POST /api/inbox/email takes the Markdown, audio and image attachments hive-mx received for hive@, relayed and signed by the hub, and spools them as items of a new built-in email source under connector-state/email/items (0700/0600, 256 MiB, ids stable per message and file so a retried delivery dedupes). A DMARC-aligned sender on the calendar email allowlist gets normal items; anyone else's carry AwaitingApproval: Route (except Knowledge only), auto-routing rules and transcription skip them until the owner approves (POST /api/inbox/email/{id}/approve); decline deletes, and unapproved items expire after 14 days. Markdown is the item text wrapped as untrusted; audio goes through the same transcription path as voice items via a new AudioOpener (read from the spool, no link); images are stored attachments served by GET /api/inbox/email/{id}/file with the sniffed type, nosniff and a sandbox CSP. The Inbox shows the sender, an awaiting-approval pill with Approve/Decline, Open file and image thumbnails (static/index.html regenerated). openapi.json, api-reference and inbox-voice-sources.md. Part of #18391, #18395. Signed-off-by: Andy Anderson --- changelog.d/added-18395-mail-in-files.md | 1 + dashboard/openapi.json | 191 ++++++++++ src/cmd/hive/boot_inbox.go | 3 + src/docs/api-reference/knowledge.md | 4 + src/docs/inbox-voice-sources.md | 35 ++ src/pkg/dashboard/api_inbox.go | 1 + src/pkg/dashboard/api_inbox_email.go | 144 ++++++++ src/pkg/dashboard/api_inbox_email_test.go | 135 +++++++ src/pkg/dashboard/static-src/css/inbox.css | 1 + src/pkg/dashboard/static-src/js/inbox.js | 38 +- src/pkg/dashboard/static/index.html | 39 +- src/pkg/inbox/email_source.go | 403 +++++++++++++++++++++ src/pkg/inbox/email_source_test.go | 295 +++++++++++++++ src/pkg/inbox/inbox.go | 15 + src/pkg/inbox/service.go | 30 +- src/pkg/inbox/transcription.go | 41 ++- 16 files changed, 1362 insertions(+), 14 deletions(-) create mode 100644 changelog.d/added-18395-mail-in-files.md create mode 100644 src/pkg/dashboard/api_inbox_email.go create mode 100644 src/pkg/dashboard/api_inbox_email_test.go create mode 100644 src/pkg/inbox/email_source.go create mode 100644 src/pkg/inbox/email_source_test.go diff --git a/changelog.d/added-18395-mail-in-files.md b/changelog.d/added-18395-mail-in-files.md new file mode 100644 index 0000000000..47ca83a6a0 --- /dev/null +++ b/changelog.d/added-18395-mail-in-files.md @@ -0,0 +1 @@ +- Mail Markdown, audio and image files to `hive@` and they land in the Inbox as email items: from an allowlisted calendar email they are routable, shareable and transcribed like voice items; from anyone else they wait for the owner's Approve or Decline. diff --git a/dashboard/openapi.json b/dashboard/openapi.json index bf04b17dda..424fa03e25 100644 --- a/dashboard/openapi.json +++ b/dashboard/openapi.json @@ -39289,6 +39289,197 @@ } } }, + "/api/inbox/email": { + "post": { + "tags": [ + "Inbox" + ], + "summary": "Receive mail-in files (hub relay)", + "description": "Markdown, audio and image attachments hive-mx received for hive@. A sender whose From address passed DMARC alignment and is a proven calendar email of a user with access becomes normal Inbox items; anyone else's items await the owner's approval. Relay signature: X-Hive-Relay-Timestamp, X-Hive-Relay-Signature-256 (HMAC-SHA256 with this hive's relay key over timestamp.delivery.body) and X-Hive-Relay-Delivery; no session.", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "description": "hive-mx delivery (pkg/mailin/wire.Delivery) of kind files: id, kind, recipient, message_id, received_at, auth, files {title, attachments[] {name, kind, content_type, size, sha256, data}}" + } + } + } + }, + "responses": { + "200": { + "description": "Stored", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "items": { + "type": "array", + "items": { + "type": "string" + } + }, + "awaiting_approval": { + "type": "boolean" + } + } + } + } + } + }, + "400": { + "description": "Invalid delivery" + }, + "401": { + "description": "Relay signature rejected" + }, + "409": { + "description": "Mail-in files are off or unavailable" + }, + "413": { + "description": "Delivery too large" + }, + "503": { + "description": "This hive holds no relay key" + }, + "507": { + "description": "The email spool is full" + } + } + } + }, + "/api/inbox/email/{id}/approve": { + "post": { + "tags": [ + "Inbox" + ], + "summary": "Approve a mailed-in item", + "description": "Lets an item from outside the allowlist be shared, routed and transcribed. Owner only. Audited as inbox-email-approve.", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + }, + "description": "Email item id" + } + ], + "responses": { + "200": { + "description": "The item", + "content": { + "application/json": { + "schema": { + "type": "object", + "description": "Inbox item" + } + } + } + }, + "403": { + "description": "Owner access required" + }, + "404": { + "description": "No such email item" + }, + "503": { + "description": "Mail-in files are not available" + } + } + } + }, + "/api/inbox/email/{id}/decline": { + "post": { + "tags": [ + "Inbox" + ], + "summary": "Decline a mailed-in item", + "description": "Deletes the item and its file. Owner only. Audited as inbox-email-decline.", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + }, + "description": "Email item id" + } + ], + "responses": { + "200": { + "description": "Deleted", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "declined": { + "type": "boolean" + } + } + } + } + } + }, + "403": { + "description": "Owner access required" + }, + "404": { + "description": "No such email item" + }, + "503": { + "description": "Mail-in files are not available" + } + } + } + }, + "/api/inbox/email/{id}/file": { + "get": { + "tags": [ + "Inbox" + ], + "summary": "Download a mailed-in file", + "description": "The stored attachment with its sniffed type (Markdown as text/plain), inline for images and as a download otherwise, X-Content-Type-Options nosniff and a sandbox Content-Security-Policy. Owner or read-write.", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + }, + "description": "Email item id" + } + ], + "responses": { + "200": { + "description": "The file", + "content": { + "application/octet-stream": { + "schema": { + "type": "string", + "format": "binary" + } + } + } + }, + "403": { + "description": "Owner or read-write access required" + }, + "404": { + "description": "No such email item" + }, + "503": { + "description": "Mail-in files are not available" + } + } + } + }, "/api/inbox/route": { "post": { "tags": [ diff --git a/src/cmd/hive/boot_inbox.go b/src/cmd/hive/boot_inbox.go index 2ef3a03936..ad3eaa831c 100644 --- a/src/cmd/hive/boot_inbox.go +++ b/src/cmd/hive/boot_inbox.go @@ -66,6 +66,9 @@ func (b *boot) bootInbox(baseDir string) { Fetcher: &inbox.LinkFetcher{UserAgent: "hive-inbox/" + version}, } svc.EnableUploads(uploads) + // Files mailed to hive@ through hive-mx (#18395) are spooled on + // this spoke's data volume (0700 dirs, 0600 files). + svc.EnableEmail(&inbox.EmailSource{Dir: filepath.Join(baseDir, "connector-state", "email", "items")}) b.startInboxUploadRetention(uploads) // Dashboard recordings (● Record): audio under connector-state/recordings // until inbox.recording.retention_days, facts in the personal layer. diff --git a/src/docs/api-reference/knowledge.md b/src/docs/api-reference/knowledge.md index e1526c6180..979d9e8172 100644 --- a/src/docs/api-reference/knowledge.md +++ b/src/docs/api-reference/knowledge.md @@ -68,6 +68,10 @@ Part of the [Dashboard REST API reference](../api-reference.md); see that page f | `POST` | `/api/knowledge/connectors/{name}/auth/complete` | Owner only | Finish a sign-in from pasted material on a hosted hive: `{redirect_url}` (Plaud: the `localhost:8199/auth/callback?…` URL the browser landed on, forwarded in-pod while the login is pending; Zoom, Google Meet and Webex: the registered redirect URL with `code` and `state`, exchanged for tokens kept in the connector token store) or `{tokens}` (Plaud only: contents of `~/.plaud/tokens-mcp.json`); see [inbox-voice-sources.md](../inbox-voice-sources.md) | `pkg/dashboard/api_inbox.go:32` | | `GET` | `/api/inbox` | Dashboard auth/session | Inbox items (voice recordings, dictations and meeting recordings keyed by `(source, id)`) with per-destination route state, source connection status, candidate repos and auto-routing settings; see [inbox-voice-sources.md](../inbox-voice-sources.md) | `pkg/dashboard/api_inbox.go:24` | | `POST` | `/api/inbox/route` | Owner only | Route one item: `{source, item_id, destination: spek_rfc\|dibs\|dismiss, repo?}` (202 queued; 400 unconfigured destination, 404 unknown item, 409 already queued/running) | `pkg/dashboard/api_inbox.go:25` | +| `POST` | `/api/inbox/email` | Hub relay signature | Mail-in files hive-mx received for `hive@` ([#18395](https://github.com/hivecommons/hive/issues/18395)): Markdown, audio and image attachments become `email` Inbox items under `connector-state/email/items`. A sender who passed DMARC alignment and is a proven calendar email of a user with access gets normal items; anyone else's items carry `awaiting_approval` and cannot be shared, routed or transcribed until approved. `200 {items, awaiting_approval}`; `409` off (`meetings.email.files_enabled`); `507` spool full (256 MiB); `401` bad relay signature | `pkg/dashboard/api_inbox_email.go:21` | +| `POST` | `/api/inbox/email/{id}/approve` | Owner only | Approve a mailed-in item from outside the allowlist. Audited as `inbox-email-approve` | `pkg/dashboard/api_inbox_email.go:22` | +| `POST` | `/api/inbox/email/{id}/decline` | Owner only | Delete a mailed-in item and its file. Unapproved items are also deleted after 14 days. Audited as `inbox-email-decline` | `pkg/dashboard/api_inbox_email.go:23` | +| `GET` | `/api/inbox/email/{id}/file` | Owner or read-write | The stored file with its sniffed type (Markdown as `text/plain`), inline for images, `nosniff` and a sandbox CSP | `pkg/dashboard/api_inbox_email.go:24` | | `POST` | `/api/inbox/dismiss` | Owner only | Bulk "Knowledge only": `{items: [{source, item_id}], undo?}` | `pkg/dashboard/api_inbox.go:26` | | `GET` | `/api/inbox/settings` | Dashboard auth/session | Auto-routing rules, default RFC repo, dibs URL, destination readiness | `pkg/dashboard/api_inbox.go:27` | | `POST` | `/api/inbox/settings` | Owner only | Update `inbox.rules` / `inbox.spek.repo` / `inbox.dibs.url` in the runtime config (omitted fields unchanged) | `pkg/dashboard/api_inbox.go:28` | diff --git a/src/docs/inbox-voice-sources.md b/src/docs/inbox-voice-sources.md index c28e637212..fee324e6da 100644 --- a/src/docs/inbox-voice-sources.md +++ b/src/docs/inbox-voice-sources.md @@ -419,6 +419,41 @@ for a conversation Otter has no transcript for ([inbox-transcription.md](inbox-transcription.md)). Setup is in [knowledge-connectors.md](knowledge-connectors.md#otterai-conversations-type-otter). +## Files mailed to hive@ (email) + +Mail sent to `hive@` with Markdown (`.md`), +audio (`.m4a`, `.mp3`, `.wav`, `.ogg`, `.webm`) or image (`.png`, `.jpg`, +`.gif`, `.webp`) attachments reaches the cluster's receive-only mail +receiver, hive-mx, which keeps only those attachments (type checked by +content, at most 10 files and 2 MB per message) and the subject, and the hub +relays them to the hive ([#18395](https://github.com/hivecommons/hive/issues/18395); +how mail reaches hive-mx is in [meetings.md](meetings.md#inviting-betty-by-email)). +Each attachment becomes one `email` item (✉️), titled with the subject (and +the file name when a mail carries several), spooled on the spoke's data +volume under `connector-state/email/items` (0700 directories, 0600 files, +256 MiB in all). + +- **Who lands directly:** a sender whose `From` address passed DMARC + alignment and is a proven calendar email of a user with owner, read-write + or read access (see + [Proving calendar emails](meetings.md#proving-calendar-emails)). Their + items are like any other Inbox item: routable to spek or dibs, shareable, + matched by auto-routing rules (`source: email`). +- **Everyone else** waits: the item shows **awaiting approval** with + **✓ Approve** and **✕ Decline** for the owner. Until approved it cannot be + shared, routed or transcribed, and rules skip it; approving lets the rules + apply on the next sweep. Declining deletes it; unapproved items are + deleted after 14 days. +- **Markdown** is the item's text, marked as untrusted data written by the + sender. **Audio** goes through the same transcription path as voice items + ([inbox-transcription.md](inbox-transcription.md)), read from the spool + instead of a download link; auto transcription applies when + `inbox.transcription.sources` includes `email`. **Images** are stored + as attachments of the item and shown as a thumbnail; **📎 Open file** + downloads any of them (owner and read-write). +- Nothing in a mail is executed or followed. Turn mail-in files off with + `meetings.email.files_enabled: false`; senders then get a bounce. + ## Configuration ```yaml diff --git a/src/pkg/dashboard/api_inbox.go b/src/pkg/dashboard/api_inbox.go index 4804da6e95..c787d6c2a0 100644 --- a/src/pkg/dashboard/api_inbox.go +++ b/src/pkg/dashboard/api_inbox.go @@ -34,6 +34,7 @@ func (s *Server) registerInboxRoutes() { s.mux.HandleFunc("GET /api/inbox/transcriptions", s.handleInboxTranscriptions) s.registerInboxUploadRoutes() s.registerInboxSpekRunner() + s.registerInboxEmailRoutes() } // inboxSourceReadTimeout bounds one read that may spawn the source's MCP diff --git a/src/pkg/dashboard/api_inbox_email.go b/src/pkg/dashboard/api_inbox_email.go new file mode 100644 index 0000000000..cc846b110b --- /dev/null +++ b/src/pkg/dashboard/api_inbox_email.go @@ -0,0 +1,144 @@ +package dashboard + +import ( + "errors" + "io" + "mime" + "net/http" + "strconv" + + "github.com/hivecommons/hive/pkg/inbox" + "github.com/hivecommons/hive/pkg/mailin/wire" +) + +// Mail-in files (hivecommons/hive#18395): Markdown, audio and image +// attachments mailed to hive@ become Inbox items of the email source. +// +// - POST /api/inbox/email relay-signed, from the hub +// - POST /api/inbox/email/{id}/approve|decline owner: the approval queue +// - GET /api/inbox/email/{id}/file the stored file +func (s *Server) registerInboxEmailRoutes() { + s.mux.HandleFunc("POST /api/inbox/email", s.handleInboxEmail) + s.mux.HandleFunc("POST /api/inbox/email/{id}/approve", s.handleInboxEmailApprove) + s.mux.HandleFunc("POST /api/inbox/email/{id}/decline", s.handleInboxEmailDecline) + s.mux.HandleFunc("GET /api/inbox/email/{id}/file", s.handleInboxEmailFile) +} + +func (s *Server) inboxEmailSource(w http.ResponseWriter, code int) (*inbox.EmailSource, bool) { + if s.deps != nil && s.deps.Inbox != nil { + if e := s.deps.Inbox.Email(); e != nil { + return e, true + } + } + jsonError(w, "mail-in files are not available on this hive", code) + return nil, false +} + +func (s *Server) handleInboxEmail(w http.ResponseWriter, r *http.Request) { + d, ok := s.readRelayedDelivery(w, r, wire.KindFiles) + if !ok { + return + } + // 409 is permanent for the sender (hive-mx bounces the mail). + if !s.meetingsEmailConfig().FilesOn() { + jsonError(w, "this hive does not accept mail-in files", http.StatusConflict) + return + } + src, ok := s.inboxEmailSource(w, http.StatusConflict) + if !ok { + return + } + trusted := s.mailTrusted(d.Auth) + items, err := src.Add(d, trusted) + switch { + case errors.Is(err, inbox.ErrEmailQuota): + jsonError(w, err.Error(), http.StatusInsufficientStorage) + return + case err != nil: + jsonError(w, "could not store the mail-in files", http.StatusInternalServerError) + return + } + ids := make([]string, 0, len(items)) + for _, it := range items { + ids = append(ids, it.ID) + } + s.webhookLogger().Info("mail-in files received", "delivery", d.ID, "files", len(ids), "trusted", trusted) + jsonResponse(w, map[string]any{"items": ids, "awaiting_approval": !trusted}) +} + +func (s *Server) handleInboxEmailApprove(w http.ResponseWriter, r *http.Request) { + if !requireOwnerRole(w, r) { + return + } + src, ok := s.inboxEmailSource(w, http.StatusServiceUnavailable) + if !ok { + return + } + it, err := src.Approve(r.PathValue("id"), requestUser(r)) + if err != nil { + inboxEmailError(w, err) + return + } + s.auditFromRequest(r, "inbox-email-approve", it.ID, "") + jsonResponse(w, it) +} + +func (s *Server) handleInboxEmailDecline(w http.ResponseWriter, r *http.Request) { + if !requireOwnerRole(w, r) { + return + } + src, ok := s.inboxEmailSource(w, http.StatusServiceUnavailable) + if !ok { + return + } + id := r.PathValue("id") + if err := src.Decline(id); err != nil { + inboxEmailError(w, err) + return + } + s.auditFromRequest(r, "inbox-email-decline", id, "") + jsonResponse(w, map[string]any{"declined": true}) +} + +func inboxEmailError(w http.ResponseWriter, err error) { + if errors.Is(err, inbox.ErrEmailNotFound) { + jsonError(w, "no such email item", http.StatusNotFound) + return + } + jsonError(w, "email item unavailable", http.StatusInternalServerError) +} + +// handleInboxEmailFile serves a stored attachment for the Inbox reader: an +// image inline, anything else as a download. The type is the one sniffed +// on receipt, never the sender's claim, and the response may not run script. +func (s *Server) handleInboxEmailFile(w http.ResponseWriter, r *http.Request) { + if !requireInboxPlaybackRole(w, r) { + return + } + src, ok := s.inboxEmailSource(w, http.StatusServiceUnavailable) + if !ok { + return + } + rc, m, err := src.Open(r.PathValue("id")) + if err != nil { + inboxEmailError(w, err) + return + } + defer rc.Close() + disposition := "attachment" + if m.Kind == wire.AttachImage { + disposition = "inline" + } + ct := m.ContentType + if m.Kind == wire.AttachMarkdown { + ct = "text/plain; charset=utf-8" + } + h := w.Header() + h.Set("Content-Type", ct) + h.Set("Content-Length", strconv.FormatInt(m.Size, 10)) + h.Set("Content-Disposition", mime.FormatMediaType(disposition, map[string]string{"filename": m.Filename})) + h.Set("X-Content-Type-Options", "nosniff") + h.Set("Content-Security-Policy", "default-src 'none'; sandbox") + h.Set("Cache-Control", "private, no-store") + _, _ = io.Copy(w, rc) +} diff --git a/src/pkg/dashboard/api_inbox_email_test.go b/src/pkg/dashboard/api_inbox_email_test.go new file mode 100644 index 0000000000..e15542f86e --- /dev/null +++ b/src/pkg/dashboard/api_inbox_email_test.go @@ -0,0 +1,135 @@ +package dashboard + +import ( + "encoding/json" + "io" + "net/http" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/inbox" + "github.com/hivecommons/hive/pkg/mailin/wire" +) + +func mailFilesServer(t *testing.T) (*Server, *inbox.EmailSource) { + t.Helper() + s, st := mailServer(t) + proveFor(t, st, "bob", "bob@example.com") + svc := inbox.NewService(config.InboxConfig{}, inbox.NewStore(filepath.Join(t.TempDir(), "inbox.json")), nil, nil, nil) + src := &inbox.EmailSource{Dir: filepath.Join(t.TempDir(), "items")} + svc.EnableEmail(src) + s.deps.Inbox = svc + return s, src +} + +func filesDelivery(from string, aligned bool, files ...wire.Attachment) wire.Delivery { + return wire.Delivery{Kind: wire.KindFiles, MessageID: "m-" + from, Recipient: "hive@acme.hive.hivecommons.dev", + Auth: wire.Auth{From: from, Aligned: aligned}, Files: &wire.Files{Title: "notes", Attachments: files}} +} + +func TestInboxEmailReceive(t *testing.T) { + s, src := mailFilesServer(t) + md := wire.Attachment{Name: "n.md", Kind: wire.AttachMarkdown, ContentType: "text/markdown", Data: []byte("# hi")} + img := wire.Attachment{Name: "p.png", Kind: wire.AttachImage, ContentType: "image/png", Data: []byte("\x89PNG\r\n\x1a\nx")} + + rec := relayPost(s, inboxEmailPath, filesDelivery("bob@example.com", true, md), testMailRelayKey) + var out struct { + Items []string `json:"items"` + Awaiting bool `json:"awaiting_approval"` + } + _ = json.Unmarshal(rec.Body.Bytes(), &out) + if rec.Code != http.StatusOK || len(out.Items) != 1 || out.Awaiting { + t.Fatalf("allowlisted sender: %d %s", rec.Code, rec.Body) + } + rec = relayPost(s, inboxEmailPath, filesDelivery("stranger@example.org", true, img), testMailRelayKey) + _ = json.Unmarshal(rec.Body.Bytes(), &out) + if rec.Code != http.StatusOK || !out.Awaiting { + t.Fatalf("outsider: %d %s", rec.Code, rec.Body) + } + pendingID := out.Items[0] + + // Only the owner approves or declines. + if rec := serve(s, userReq(http.MethodPost, "/api/inbox/email/"+pendingID+"/approve", "bob", "read-write", "{}")); rec.Code != http.StatusForbidden { + t.Fatalf("non-owner approve: %d", rec.Code) + } + if rec := serve(s, userReq(http.MethodPost, "/api/inbox/email/"+pendingID+"/approve", "alice", "owner", "{}")); rec.Code != http.StatusOK || strings.Contains(rec.Body.String(), `"awaiting_approval":true`) { + t.Fatalf("approve: %d %s", rec.Code, rec.Body) + } + if rec := serve(s, userReq(http.MethodPost, "/api/inbox/email/"+strings.Repeat("a", 24)+"/approve", "alice", "owner", "{}")); rec.Code != http.StatusNotFound { + t.Fatalf("approve unknown: %d", rec.Code) + } + + // The file is served with its sniffed type and no script. + rec = serve(s, userReq(http.MethodGet, "/api/inbox/email/"+pendingID+"/file", "bob", "read-write", "")) + body, _ := io.ReadAll(rec.Body) + if rec.Code != http.StatusOK || rec.Header().Get("Content-Type") != "image/png" || !strings.HasPrefix(rec.Header().Get("Content-Disposition"), "inline") || + rec.Header().Get("X-Content-Type-Options") != "nosniff" || !strings.Contains(rec.Header().Get("Content-Security-Policy"), "sandbox") || string(body) != string(img.Data) { + t.Fatalf("file: %d %v", rec.Code, rec.Header()) + } + mdItems, _ := src.List(t.Context()) + var mdID string + for _, it := range mdItems { + if it.UploadKind == wire.AttachMarkdown { + mdID = it.ID + } + } + rec = serve(s, userReq(http.MethodGet, "/api/inbox/email/"+mdID+"/file", "alice", "owner", "")) + if rec.Header().Get("Content-Type") != "text/plain; charset=utf-8" || !strings.HasPrefix(rec.Header().Get("Content-Disposition"), "attachment") { + t.Fatalf("markdown file headers %v", rec.Header()) + } + if rec := serve(s, userReq(http.MethodGet, "/api/inbox/email/"+mdID+"/file", "carol", "read", "")); rec.Code != http.StatusForbidden { + t.Fatalf("reader fetched a file: %d", rec.Code) + } + if rec := serve(s, userReq(http.MethodGet, "/api/inbox/email/zz/file", "alice", "owner", "")); rec.Code != http.StatusNotFound { + t.Fatalf("unknown file: %d", rec.Code) + } + + if rec := serve(s, userReq(http.MethodPost, "/api/inbox/email/"+pendingID+"/decline", "alice", "owner", "{}")); rec.Code != http.StatusOK { + t.Fatalf("decline: %d", rec.Code) + } + if rec := serve(s, userReq(http.MethodPost, "/api/inbox/email/"+pendingID+"/decline", "alice", "owner", "{}")); rec.Code != http.StatusNotFound { + t.Fatalf("decline again: %d", rec.Code) + } + if rec := serve(s, userReq(http.MethodPost, "/api/inbox/email/"+pendingID+"/decline", "bob", "read-write", "{}")); rec.Code != http.StatusForbidden { + t.Fatalf("non-owner decline: %d", rec.Code) + } +} + +func TestInboxEmailUnavailable(t *testing.T) { + s, _ := mailFilesServer(t) + src := &inbox.EmailSource{Dir: filepath.Join(t.TempDir(), "q"), QuotaBytes: 1} + s.deps.Inbox.EnableEmail(src) + big := wire.Attachment{Name: "n.md", Kind: wire.AttachMarkdown, Data: []byte("too big")} + if rec := relayPost(s, inboxEmailPath, filesDelivery("bob@example.com", true, big), testMailRelayKey); rec.Code != http.StatusInsufficientStorage { + t.Fatalf("quota: %d", rec.Code) + } + t.Setenv("HIVE_MEETINGS_EMAIL_FILES_ENABLED", "off") + if rec := relayPost(s, inboxEmailPath, filesDelivery("bob@example.com", true, big), testMailRelayKey); rec.Code != http.StatusConflict { + t.Fatalf("files off: %d", rec.Code) + } + t.Setenv("HIVE_MEETINGS_EMAIL_FILES_ENABLED", "") + s.deps.Inbox.EnableEmail(nil) + if rec := relayPost(s, inboxEmailPath, filesDelivery("bob@example.com", true, big), testMailRelayKey); rec.Code != http.StatusConflict { + t.Fatalf("no source: %d", rec.Code) + } + for _, p := range []string{"/approve", "/decline"} { + if rec := serve(s, userReq(http.MethodPost, "/api/inbox/email/"+strings.Repeat("a", 24)+p, "alice", "owner", "{}")); rec.Code != http.StatusServiceUnavailable { + t.Errorf("%s without source: %d", p, rec.Code) + } + } + if rec := serve(s, userReq(http.MethodGet, "/api/inbox/email/x/file", "alice", "owner", "")); rec.Code != http.StatusServiceUnavailable { + t.Errorf("file without source: %d", rec.Code) + } + // A blocked spool directory is a server error. + blocked := filepath.Join(t.TempDir(), "file") + if err := os.WriteFile(blocked, nil, 0o600); err != nil { + t.Fatal(err) + } + s.deps.Inbox.EnableEmail(&inbox.EmailSource{Dir: filepath.Join(blocked, "items")}) + if rec := relayPost(s, inboxEmailPath, filesDelivery("bob@example.com", true, big), testMailRelayKey); rec.Code != http.StatusInternalServerError { + t.Fatalf("blocked spool: %d", rec.Code) + } +} diff --git a/src/pkg/dashboard/static-src/css/inbox.css b/src/pkg/dashboard/static-src/css/inbox.css index d7fd17d50c..cf526b9d8c 100644 --- a/src/pkg/dashboard/static-src/css/inbox.css +++ b/src/pkg/dashboard/static-src/css/inbox.css @@ -15,6 +15,7 @@ .inbox-more { padding: 0; font-size: var(--fs-xs); } .inbox-pending { margin-top: var(--sp-2); font-style: italic; } .inbox-media:not(:empty) { margin-top: var(--sp-2); } + .inbox-email-image { display: block; max-width: min(100%, 320px); max-height: 240px; margin-top: var(--sp-2); border-radius: var(--radius-sm, 4px); } .inbox-audio { width: 100%; max-width: 520px; display: block; } .inbox-transcript { margin-top: var(--sp-2); } .inbox-transcript-text { white-space: pre-wrap; font-family: inherit; font-size: var(--fs-sm); max-height: 24em; overflow: auto; margin: var(--sp-1) 0 0; padding: var(--sp-2); border: 1px solid var(--border); border-radius: var(--r); background: var(--bg); } diff --git a/src/pkg/dashboard/static-src/js/inbox.js b/src/pkg/dashboard/static-src/js/inbox.js index 0aa6c434a0..ab54ce4fab 100644 --- a/src/pkg/dashboard/static-src/js/inbox.js +++ b/src/pkg/dashboard/static-src/js/inbox.js @@ -4,7 +4,7 @@ // dibs happens inside the hive via POST /api/inbox/route. const INBOX_POLL_MS = 60000; const INBOX_BUSY_POLL_MS = 5000; - const INBOX_SOURCE_ICONS = { plaud: '🎙️', wisprflow: '🗣️', zoom: '📹', teams: '👥', discord: '🎧', 'google-meet': '🎥', webex: '🎬', 'slack-huddles': '💬', 'fireflies': '🪲', 'fathom': '🌊', 'limitless': '📿', 'granola': '🥣', 'otter': '🦦', 'file-drop': '📎', webhook: '🔗', 'hive-recording': '⏺️', upload: '📎' }; + const INBOX_SOURCE_ICONS = { plaud: '🎙️', wisprflow: '🗣️', zoom: '📹', teams: '👥', discord: '🎧', 'google-meet': '🎥', webex: '🎬', 'slack-huddles': '💬', 'fireflies': '🪲', 'fathom': '🌊', 'limitless': '📿', 'granola': '🥣', 'otter': '🦦', 'file-drop': '📎', webhook: '🔗', 'hive-recording': '⏺️', upload: '📎', email: '✉️' }; const INBOX_DEST_LABELS = { spek_rfc: 'RFC', dibs: 'dibs' }; // Summaries longer than this many characters start collapsed. const INBOX_SUMMARY_COLLAPSE_CHARS = 280; @@ -39,8 +39,11 @@ let _inboxPointerInside = false; function inboxSourceIcon(src) { return INBOX_SOURCE_ICONS[src] || '📥'; } - // Uploaded documents have no audio; uploaded audio/video does. - function inboxItemHasAudio(it) { return !it || it.source !== 'upload' || it.upload_kind === 'audio' || it.upload_kind === 'video'; } + // Uploaded documents have no audio; uploaded audio/video does. Mailed-in + // audio (source email) is opened as a file, not streamed. + function inboxItemHasAudio(it) { return !it || (it.source !== 'upload' && it.source !== 'email') || (it.source === 'upload' && (it.upload_kind === 'audio' || it.upload_kind === 'video')); } + // Mailed-in files (source email) are opened from the hive itself: GET /api/inbox/email/{id}/file. + function inboxEmailFileUrl(it) { return '/api/inbox/email/' + encodeURIComponent(it.id) + '/file'; } // The role is known once /api/role settled (answered, or retries gave up: // then it is undefined and every gate treats the viewer as read-only). // Before that the Inbox renders a neutral loading state, never a guessed 'read'. @@ -399,11 +402,14 @@ // added them ("upload · notes.md · added 2h ago by ana"). const meta = it.source === 'upload' ? [escapeHtml(it.origin || 'upload'), when ? 'added ' + when : '', it.added_by ? 'by ' + escapeHtml(it.added_by) : '', dur, factLink].filter(Boolean).join(' · ') - : [escapeHtml(it.source), when ? 'recorded ' + when : '', it.recorded_by ? 'by ' + escapeHtml(it.recorded_by) : '', dur, factLink].filter(Boolean).join(' · '); + : it.source === 'email' + ? ['email', it.sender ? 'from ' + escapeHtml(it.sender) : '', when ? 'received ' + when : '', escapeHtml(it.upload_kind || '')].filter(Boolean).join(' · ') + : [escapeHtml(it.source), when ? 'recorded ' + when : '', it.recorded_by ? 'by ' + escapeHtml(it.recorded_by) : '', dur, factLink].filter(Boolean).join(' · '); const routes = it.routes || {}; let pills = ''; for (const d of Object.keys(routes)) pills += inboxStatePill(d, routes[d]) + ' '; if (it.dismissed) pills += 'knowledge only'; + if (it.awaiting_approval) pills += 'awaiting approval '; pills += inboxSharePillsHtml(it, key); for (const d of Object.keys(it.share_intents || {})) { const si = it.share_intents[d]; @@ -416,7 +422,13 @@ if (it.audio_expired) actions += 'audio expired (kept ' + (Number(it.audio_retention_days) || 5) + ' days)'; else if (inboxViewerCanPlay() && inboxItemHasAudio(it)) actions += ''; actions += ''; - actions += ''; + if (it.source === 'email' && inboxViewerCanPlay()) actions += '📎 Open file'; + if (it.awaiting_approval) { + if (owner) actions += '' + + ''; + } else { + actions += ''; + } return '
' + (inboxCanShare(window._hiveRole) ? '' : '') + '
' + inboxSourceIcon(it.source) + '
' + @@ -424,6 +436,7 @@ '
' + meta + '
' + inboxSummaryHtml(it, key) + '
' + + (it.source === 'email' && it.upload_kind === 'image' && inboxViewerCanPlay() ? '' + escapeHtml(it.title || 'mailed-in image') + '' : '') + (pills ? '
' + pills + '
' : '') + inboxTranscriptionHtml(it, key) + '
' + actions + '
' + @@ -769,6 +782,21 @@ if (!r.ok) throw new Error(data.error || ('HTTP ' + r.status)); return data; } + // Approval queue for mailed-in files from outside the allowlist (#18395). + async function inboxEmailAct(id, action) { + try { + const r = await fetch('/api/inbox/email/' + encodeURIComponent(id) + '/' + action, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: '{}' }); + const data = await r.json().catch(() => ({})); + if (!r.ok) throw new Error(data.error || ('HTTP ' + r.status)); + showToast(action === 'approve' ? 'Approved' : 'Declined and deleted', 'success'); + } catch (e) { + showToast('Could not ' + action + ': ' + e.message, 'error'); + } + await fetchInbox(); + } + function inboxEmailApprove(id) { return inboxEmailAct(id, 'approve'); } + function inboxEmailDecline(id) { return inboxEmailAct(id, 'decline'); } + async function inboxDismiss(src, id) { try { const d = await inboxPostDismiss([{ source: src, item_id: id }], false); showToast(d && d.waiting_transcript ? 'Transcribing, then → Knowledge' : 'Kept as a Knowledge fact only', 'success'); await fetchInbox(); } catch (e) { showToast('Dismiss failed: ' + e.message, 'error'); } diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index 57d5970a2e..014ae5e347 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -6087,6 +6087,7 @@ .inbox-more { padding: 0; font-size: var(--fs-xs); } .inbox-pending { margin-top: var(--sp-2); font-style: italic; } .inbox-media:not(:empty) { margin-top: var(--sp-2); } + .inbox-email-image { display: block; max-width: min(100%, 320px); max-height: 240px; margin-top: var(--sp-2); border-radius: var(--radius-sm, 4px); } .inbox-audio { width: 100%; max-width: 520px; display: block; } .inbox-transcript { margin-top: var(--sp-2); } .inbox-transcript-text { white-space: pre-wrap; font-family: inherit; font-size: var(--fs-sm); max-height: 24em; overflow: auto; margin: var(--sp-1) 0 0; padding: var(--sp-2); border: 1px solid var(--border); border-radius: var(--r); background: var(--bg); } @@ -47739,7 +47740,7 @@ // dibs happens inside the hive via POST /api/inbox/route. const INBOX_POLL_MS = 60000; const INBOX_BUSY_POLL_MS = 5000; - const INBOX_SOURCE_ICONS = { plaud: '🎙️', wisprflow: '🗣️', zoom: '📹', teams: '👥', discord: '🎧', 'google-meet': '🎥', webex: '🎬', 'slack-huddles': '💬', 'fireflies': '🪲', 'fathom': '🌊', 'limitless': '📿', 'granola': '🥣', 'otter': '🦦', 'file-drop': '📎', webhook: '🔗', 'hive-recording': '⏺️', upload: '📎' }; + const INBOX_SOURCE_ICONS = { plaud: '🎙️', wisprflow: '🗣️', zoom: '📹', teams: '👥', discord: '🎧', 'google-meet': '🎥', webex: '🎬', 'slack-huddles': '💬', 'fireflies': '🪲', 'fathom': '🌊', 'limitless': '📿', 'granola': '🥣', 'otter': '🦦', 'file-drop': '📎', webhook: '🔗', 'hive-recording': '⏺️', upload: '📎', email: '✉️' }; const INBOX_DEST_LABELS = { spek_rfc: 'RFC', dibs: 'dibs' }; // Summaries longer than this many characters start collapsed. const INBOX_SUMMARY_COLLAPSE_CHARS = 280; @@ -47774,8 +47775,11 @@ let _inboxPointerInside = false; function inboxSourceIcon(src) { return INBOX_SOURCE_ICONS[src] || '📥'; } - // Uploaded documents have no audio; uploaded audio/video does. - function inboxItemHasAudio(it) { return !it || it.source !== 'upload' || it.upload_kind === 'audio' || it.upload_kind === 'video'; } + // Uploaded documents have no audio; uploaded audio/video does. Mailed-in + // audio (source email) is opened as a file, not streamed. + function inboxItemHasAudio(it) { return !it || (it.source !== 'upload' && it.source !== 'email') || (it.source === 'upload' && (it.upload_kind === 'audio' || it.upload_kind === 'video')); } + // Mailed-in files (source email) are opened from the hive itself: GET /api/inbox/email/{id}/file. + function inboxEmailFileUrl(it) { return '/api/inbox/email/' + encodeURIComponent(it.id) + '/file'; } // The role is known once /api/role settled (answered, or retries gave up: // then it is undefined and every gate treats the viewer as read-only). // Before that the Inbox renders a neutral loading state, never a guessed 'read'. @@ -48134,11 +48138,14 @@ // added them ("upload · notes.md · added 2h ago by ana"). const meta = it.source === 'upload' ? [escapeHtml(it.origin || 'upload'), when ? 'added ' + when : '', it.added_by ? 'by ' + escapeHtml(it.added_by) : '', dur, factLink].filter(Boolean).join(' · ') - : [escapeHtml(it.source), when ? 'recorded ' + when : '', it.recorded_by ? 'by ' + escapeHtml(it.recorded_by) : '', dur, factLink].filter(Boolean).join(' · '); + : it.source === 'email' + ? ['email', it.sender ? 'from ' + escapeHtml(it.sender) : '', when ? 'received ' + when : '', escapeHtml(it.upload_kind || '')].filter(Boolean).join(' · ') + : [escapeHtml(it.source), when ? 'recorded ' + when : '', it.recorded_by ? 'by ' + escapeHtml(it.recorded_by) : '', dur, factLink].filter(Boolean).join(' · '); const routes = it.routes || {}; let pills = ''; for (const d of Object.keys(routes)) pills += inboxStatePill(d, routes[d]) + ' '; if (it.dismissed) pills += 'knowledge only'; + if (it.awaiting_approval) pills += 'awaiting approval '; pills += inboxSharePillsHtml(it, key); for (const d of Object.keys(it.share_intents || {})) { const si = it.share_intents[d]; @@ -48151,7 +48158,13 @@ if (it.audio_expired) actions += 'audio expired (kept ' + (Number(it.audio_retention_days) || 5) + ' days)'; else if (inboxViewerCanPlay() && inboxItemHasAudio(it)) actions += ''; actions += ''; - actions += ''; + if (it.source === 'email' && inboxViewerCanPlay()) actions += '📎 Open file'; + if (it.awaiting_approval) { + if (owner) actions += '' + + ''; + } else { + actions += ''; + } return '
' + (inboxCanShare(window._hiveRole) ? '' : '') + '
' + inboxSourceIcon(it.source) + '
' + @@ -48159,6 +48172,7 @@ '
' + meta + '
' + inboxSummaryHtml(it, key) + '
' + + (it.source === 'email' && it.upload_kind === 'image' && inboxViewerCanPlay() ? '' + escapeHtml(it.title || 'mailed-in image') + '' : '') + (pills ? '
' + pills + '
' : '') + inboxTranscriptionHtml(it, key) + '
' + actions + '
' + @@ -48504,6 +48518,21 @@ if (!r.ok) throw new Error(data.error || ('HTTP ' + r.status)); return data; } + // Approval queue for mailed-in files from outside the allowlist (#18395). + async function inboxEmailAct(id, action) { + try { + const r = await fetch('/api/inbox/email/' + encodeURIComponent(id) + '/' + action, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: '{}' }); + const data = await r.json().catch(() => ({})); + if (!r.ok) throw new Error(data.error || ('HTTP ' + r.status)); + showToast(action === 'approve' ? 'Approved' : 'Declined and deleted', 'success'); + } catch (e) { + showToast('Could not ' + action + ': ' + e.message, 'error'); + } + await fetchInbox(); + } + function inboxEmailApprove(id) { return inboxEmailAct(id, 'approve'); } + function inboxEmailDecline(id) { return inboxEmailAct(id, 'decline'); } + async function inboxDismiss(src, id) { try { const d = await inboxPostDismiss([{ source: src, item_id: id }], false); showToast(d && d.waiting_transcript ? 'Transcribing, then → Knowledge' : 'Kept as a Knowledge fact only', 'success'); await fetchInbox(); } catch (e) { showToast('Dismiss failed: ' + e.message, 'error'); } diff --git a/src/pkg/inbox/email_source.go b/src/pkg/inbox/email_source.go new file mode 100644 index 0000000000..96c258a7e0 --- /dev/null +++ b/src/pkg/inbox/email_source.go @@ -0,0 +1,403 @@ +package inbox + +import ( + "bytes" + "context" + "crypto/sha256" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "io" + "os" + "path/filepath" + "sort" + "strings" + "sync" + "time" + + "github.com/hivecommons/hive/pkg/mailin/wire" +) + +// EmailType is the Inbox source for mail-in files: Markdown, audio and image +// attachments mailed to hive@ through hive-mx (hivecommons/hive#18395, +// design src/docs/design/email-inbox.md). It is push, not a connector: the +// dashboard's relay-signed POST /api/inbox/email adds items to a spool under +// connector-state/email/items, and the source lists that spool. +// +// Items from a sender on the hive's email allowlist (DMARC aligned) are +// normal items. Every other item is AwaitingApproval: it is listed, but it +// cannot be routed or transcribed and is skipped by auto-routing rules until +// the owner approves it (or declines, which deletes it). +const EmailType = "email" + +// Email spool limits. +const ( + // DefaultEmailQuotaBytes bounds the whole spool. + DefaultEmailQuotaBytes = 256 << 20 + // EmailPendingRetention is how long an unapproved item is kept. + EmailPendingRetention = 14 * 24 * time.Hour + emailIDHexLen = 24 + emailMetaFile = "meta.json" + emailDataFile = "data" + emailDirMode = 0o700 + emailFileMode = 0o600 + // emailTitleSep joins the subject and the file name of a multi-file mail. + emailTitleSep = " · " +) + +// Errors the email source returns. +var ( + ErrEmailQuota = errors.New("the email spool is full") + ErrAwaitingApproval = errors.New("this item is awaiting the owner's approval") + ErrEmailNotFound = errors.New("no such email item") +) + +// EmailMeta is one spooled attachment. +type EmailMeta struct { + ID string `json:"id"` + Title string `json:"title"` + Filename string `json:"filename"` + Kind string `json:"kind"` + ContentType string `json:"content_type"` + Size int64 `json:"size"` + SHA256 string `json:"sha256"` + From string `json:"from"` + Aligned bool `json:"aligned"` + Approved bool `json:"approved"` + ApprovedBy string `json:"approved_by,omitempty"` + MessageID string `json:"message_id,omitempty"` + DeliveryID string `json:"delivery_id,omitempty"` + ReceivedAt time.Time `json:"received_at"` +} + +// EmailSource is the email spool as an Inbox source. Safe for concurrent use. +type EmailSource struct { + Dir string + QuotaBytes int64 + Now func() time.Time + + mu sync.Mutex +} + +// Type implements Source. +func (e *EmailSource) Type() string { return EmailType } + +// Builtin implements BuiltinSource: listed without being configured. +func (e *EmailSource) Builtin() bool { return true } + +func (e *EmailSource) now() time.Time { + if e.Now != nil { + return e.Now().UTC() + } + return time.Now().UTC() +} + +func (e *EmailSource) quota() int64 { + if e.QuotaBytes > 0 { + return e.QuotaBytes + } + return DefaultEmailQuotaBytes +} + +func (e *EmailSource) itemDir(id string) string { return filepath.Join(e.Dir, id) } + +func validEmailID(id string) bool { + if len(id) != emailIDHexLen { + return false + } + _, err := hex.DecodeString(id) + return err == nil +} + +// emailItemID is stable per message and file, so a retried delivery of the +// same mail lands on the same item. +func emailItemID(d wire.Delivery, a wire.Attachment) string { + key := d.MessageID + if key == "" { + key = d.ID + } + sum := sha256.Sum256([]byte(key + "\x00" + d.Recipient + "\x00" + a.SHA256 + "\x00" + a.Name)) + return hex.EncodeToString(sum[:])[:emailIDHexLen] +} + +func (e *EmailSource) readMeta(id string) (EmailMeta, error) { + var m EmailMeta + b, err := os.ReadFile(filepath.Join(e.itemDir(id), emailMetaFile)) + if errors.Is(err, os.ErrNotExist) { + return m, ErrEmailNotFound + } + if err != nil { + return m, err + } + if err := json.Unmarshal(b, &m); err != nil { + return m, err + } + return m, nil +} + +func (e *EmailSource) writeMeta(m EmailMeta) error { + b, err := json.MarshalIndent(m, "", " ") + if err != nil { + return err + } + path := filepath.Join(e.itemDir(m.ID), emailMetaFile) + tmp := path + ".tmp" + if err := os.WriteFile(tmp, b, emailFileMode); err != nil { + return err + } + return os.Rename(tmp, path) +} + +// metas lists the spool, newest first. +func (e *EmailSource) metas() ([]EmailMeta, error) { + entries, err := os.ReadDir(e.Dir) + if errors.Is(err, os.ErrNotExist) { + return nil, nil + } + if err != nil { + return nil, err + } + var out []EmailMeta + for _, de := range entries { + if !de.IsDir() || !validEmailID(de.Name()) { + continue + } + m, err := e.readMeta(de.Name()) + if err != nil { + continue + } + out = append(out, m) + } + sort.Slice(out, func(i, j int) bool { return out[i].ReceivedAt.After(out[j].ReceivedAt) }) + return out, nil +} + +func (e *EmailSource) usageLocked() int64 { + ms, _ := e.metas() + var n int64 + for _, m := range ms { + n += m.Size + } + return n +} + +// pruneLocked deletes unapproved items past EmailPendingRetention. +func (e *EmailSource) pruneLocked(now time.Time) { + ms, _ := e.metas() + for _, m := range ms { + if !m.Approved && now.Sub(m.ReceivedAt) > EmailPendingRetention { + _ = os.RemoveAll(e.itemDir(m.ID)) + } + } +} + +func emailTitle(subject string, a wire.Attachment, many bool) string { + subject = strings.TrimSpace(subject) + switch { + case subject == "": + return a.Name + case many: + return truncateRunes(subject+emailTitleSep+a.Name, wire.MaxTitleRunes) + } + return subject +} + +// Add spools one delivery's attachments. trusted marks a sender on the +// allowlist with DMARC alignment; everyone else's items await approval. +// It returns the new or already present items. +func (e *EmailSource) Add(d wire.Delivery, trusted bool) ([]Item, error) { + if d.Files == nil || len(d.Files.Attachments) == 0 { + return nil, nil + } + e.mu.Lock() + defer e.mu.Unlock() + now := e.now() + e.pruneLocked(now) + if err := os.MkdirAll(e.Dir, emailDirMode); err != nil { + return nil, err + } + var incoming int64 + for _, a := range d.Files.Attachments { + incoming += int64(len(a.Data)) + } + if e.usageLocked()+incoming > e.quota() { + return nil, ErrEmailQuota + } + many := len(d.Files.Attachments) > 1 + var out []Item + for _, a := range d.Files.Attachments { + if len(a.Data) == 0 { + continue + } + sum := sha256.Sum256(a.Data) + a.SHA256 = hex.EncodeToString(sum[:]) + id := emailItemID(d, a) + if m, err := e.readMeta(id); err == nil { + out = append(out, e.item(m)) + continue + } + m := EmailMeta{ + ID: id, Title: emailTitle(d.Files.Title, a, many), Filename: CleanUploadName(a.Name), + Kind: a.Kind, ContentType: a.ContentType, Size: int64(len(a.Data)), SHA256: a.SHA256, + From: d.Auth.From, Aligned: d.Auth.Aligned, Approved: trusted, + MessageID: d.MessageID, DeliveryID: d.ID, ReceivedAt: now, + } + if trusted { + m.ApprovedBy = "allowlist" + } + dir := e.itemDir(id) + if err := os.MkdirAll(dir, emailDirMode); err != nil { + return out, err + } + if err := os.WriteFile(filepath.Join(dir, emailDataFile), a.Data, emailFileMode); err != nil { + _ = os.RemoveAll(dir) + return out, err + } + if err := e.writeMeta(m); err != nil { + _ = os.RemoveAll(dir) + return out, err + } + out = append(out, e.item(m)) + } + return out, nil +} + +// Approve lets an awaiting item be routed and transcribed. +func (e *EmailSource) Approve(id, by string) (Item, error) { + if !validEmailID(id) { + return Item{}, ErrEmailNotFound + } + e.mu.Lock() + defer e.mu.Unlock() + m, err := e.readMeta(id) + if err != nil { + return Item{}, err + } + if !m.Approved { + m.Approved, m.ApprovedBy = true, by + if err := e.writeMeta(m); err != nil { + return Item{}, err + } + } + return e.item(m), nil +} + +// Decline deletes an item and its file. +func (e *EmailSource) Decline(id string) error { + if !validEmailID(id) { + return ErrEmailNotFound + } + e.mu.Lock() + defer e.mu.Unlock() + if _, err := e.readMeta(id); err != nil { + return err + } + return os.RemoveAll(e.itemDir(id)) +} + +// Open returns an item's file and its metadata. +func (e *EmailSource) Open(id string) (io.ReadCloser, EmailMeta, error) { + if !validEmailID(id) { + return nil, EmailMeta{}, ErrEmailNotFound + } + m, err := e.readMeta(id) + if err != nil { + return nil, m, err + } + f, err := os.Open(filepath.Join(e.itemDir(id), emailDataFile)) + if err != nil { + return nil, m, err + } + return f, m, nil +} + +func (e *EmailSource) item(m EmailMeta) Item { + it := Item{ + Source: EmailType, Connector: EmailType, ID: m.ID, Title: firstNonEmpty(m.Title, m.Filename, m.ID), + RecordedAt: m.ReceivedAt, Tags: []string{EmailType, m.Kind}, Origin: "email · " + m.From, + AddedBy: m.From, UploadKind: m.Kind, AwaitingApproval: !m.Approved, Sender: m.From, + } + if m.Kind == wire.AttachAudio { + // Audio has no text until it is transcribed. + it.Pending = true + } + if m.Kind == wire.AttachMarkdown { + if b, err := os.ReadFile(filepath.Join(e.itemDir(m.ID), emailDataFile)); err == nil { + it.Excerpt = truncateRunes(strings.TrimSpace(string(b)), SummaryRunes) + } + } + return it +} + +// List implements Source. +func (e *EmailSource) List(context.Context) ([]Item, error) { + ms, err := e.metas() + if err != nil { + return nil, err + } + out := make([]Item, 0, len(ms)) + for _, m := range ms { + out = append(out, e.item(m)) + } + return out, nil +} + +// EmailProvenance is where an email item came from. +func EmailProvenance(m EmailMeta) string { + return "email://" + m.ID + "/" + m.Filename +} + +// Fetch implements Source. Markdown is returned as the item text (wrapped as +// untrusted data); an image or audio file is described, its bytes served by +// the dashboard; audio text arrives through transcription. +func (e *EmailSource) Fetch(_ context.Context, it Item) (Content, error) { + if !validEmailID(it.ID) { + return Content{}, ErrEmailNotFound + } + m, err := e.readMeta(it.ID) + if err != nil { + return Content{}, err + } + c := Content{Title: firstNonEmpty(m.Title, m.Filename), Provenance: EmailProvenance(m), TranscriptLabel: "Email attachment"} + header := fmt.Sprintf("- **From:** %s (%s)\n- **File:** %s (%s, %s)\n", m.From, alignedLabel(m.Aligned), m.Filename, m.ContentType, humanBytes(m.Size)) + switch m.Kind { + case wire.AttachMarkdown: + b, err := os.ReadFile(filepath.Join(e.itemDir(m.ID), emailDataFile)) + if err != nil { + return Content{}, err + } + text := string(bytes.ToValidUTF8(b, nil)) + c.Markdown = header + "\n" + WrapUntrusted(EmailType, m.ID, strings.TrimSpace(text)) + case wire.AttachAudio: + c.Pending = true + c.Markdown = header + "\nAn audio file; its text appears here once it is transcribed." + default: + c.Markdown = header + "\nAn image, stored as an attachment of this item." + } + return c, nil +} + +func alignedLabel(aligned bool) string { + if aligned { + return "DMARC aligned" + } + return "not authenticated" +} + +// OpenAudio implements AudioOpener for audio items. +func (e *EmailSource) OpenAudio(_ context.Context, it Item) (io.ReadCloser, string, int64, error) { + rc, m, err := e.Open(it.ID) + if err != nil { + return nil, "", 0, err + } + if m.Kind != wire.AttachAudio { + _ = rc.Close() + return nil, "", 0, ErrNoAudio + } + mime := audioMIMEByExt[strings.ToLower(filepath.Ext(m.Filename))] + if mime == "" { + mime = m.ContentType + } + return rc, mime, m.Size, nil +} diff --git a/src/pkg/inbox/email_source_test.go b/src/pkg/inbox/email_source_test.go new file mode 100644 index 0000000000..219a80e054 --- /dev/null +++ b/src/pkg/inbox/email_source_test.go @@ -0,0 +1,295 @@ +package inbox + +import ( + "context" + "errors" + "io" + "os" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/mailin/wire" + "github.com/hivecommons/hive/pkg/transcribe" +) + +var ( + emailMD = []byte("# Notes\n\nShip it.\n") + emailPNG = []byte("\x89PNG\r\n\x1a\nfake") + emailAudio = []byte("ID3\x04fake-mp3") +) + +func emailDelivery(msgID string, files ...wire.Attachment) wire.Delivery { + return wire.Delivery{ID: "d-" + msgID, Kind: wire.KindFiles, Recipient: "hive@acme.example", MessageID: msgID, + Auth: wire.Auth{From: "ada@example.com", Aligned: true}, Files: &wire.Files{Title: "Tuesday notes", Attachments: files}} +} + +func att(name, kind, ct string, data []byte) wire.Attachment { + return wire.Attachment{Name: name, Kind: kind, ContentType: ct, Size: len(data), Data: data} +} + +func newEmailSource(t *testing.T) (*EmailSource, *time.Time) { + t.Helper() + now := time.Date(2026, 11, 16, 12, 0, 0, 0, time.UTC) + return &EmailSource{Dir: filepath.Join(t.TempDir(), "email", "items"), Now: func() time.Time { return now }}, &now +} + +func TestEmailSourceAddListFetch(t *testing.T) { + e, _ := newEmailSource(t) + if e.Type() != EmailType || !e.Builtin() { + t.Fatal("type") + } + if items, err := e.List(context.Background()); err != nil || len(items) != 0 { + t.Fatalf("empty spool: %v %v", items, err) + } + d := emailDelivery("m1@example.com", + att("notes.md", wire.AttachMarkdown, "text/markdown", emailMD), + att("shot.png", wire.AttachImage, "image/png", emailPNG), + att("memo.mp3", wire.AttachAudio, "audio/mpeg", emailAudio), + att("empty.md", wire.AttachMarkdown, "text/markdown", nil)) + items, err := e.Add(d, true) + if err != nil || len(items) != 3 { + t.Fatalf("add: %d %v", len(items), err) + } + md := items[0] + if md.Source != EmailType || md.AwaitingApproval || md.Sender != "ada@example.com" || md.Title != "Tuesday notes · notes.md" || !strings.Contains(md.Excerpt, "Ship it") || md.Pending { + t.Fatalf("markdown item %+v", md) + } + if !items[2].Pending || items[2].UploadKind != wire.AttachAudio { + t.Fatalf("audio item %+v", items[2]) + } + // A retried delivery of the same mail lands on the same items. + again, err := e.Add(d, false) + if err != nil || len(again) != 3 || again[0].ID != md.ID || again[0].AwaitingApproval { + t.Fatalf("retry: %+v %v", again, err) + } + listed, _ := e.List(context.Background()) + if len(listed) != 3 { + t.Fatalf("listed %d", len(listed)) + } + + c, err := e.Fetch(context.Background(), md) + if err != nil || !strings.Contains(c.Markdown, "Ship it") || !strings.Contains(c.Markdown, "DMARC aligned") || c.Provenance != "email://"+md.ID+"/notes.md" { + t.Fatalf("markdown content %+v %v", c, err) + } + if c, _ := e.Fetch(context.Background(), items[1]); !strings.Contains(c.Markdown, "An image") { + t.Fatalf("image content %+v", c) + } + if c, _ := e.Fetch(context.Background(), items[2]); !c.Pending { + t.Fatalf("audio content %+v", c) + } + if _, err := e.Fetch(context.Background(), Item{ID: "../../etc"}); !errors.Is(err, ErrEmailNotFound) { + t.Fatalf("bad id: %v", err) + } + if _, err := e.Fetch(context.Background(), Item{ID: strings.Repeat("a", emailIDHexLen)}); !errors.Is(err, ErrEmailNotFound) { + t.Fatalf("missing id: %v", err) + } + + // Files on disk are private. + st, err := os.Stat(filepath.Join(e.Dir, md.ID, emailDataFile)) + if err != nil || st.Mode().Perm() != emailFileMode { + t.Fatalf("data mode %v %v", st, err) + } + + // Nothing to add. + if out, err := e.Add(wire.Delivery{}, true); err != nil || out != nil { + t.Fatalf("no files: %v %v", out, err) + } +} + +func TestEmailSourceApprovalAndDecline(t *testing.T) { + e, now := newEmailSource(t) + d := emailDelivery("m2", att("a.md", wire.AttachMarkdown, "text/markdown", emailMD)) + d.Files.Title = "" + d.Auth = wire.Auth{From: "stranger@example.org"} + items, err := e.Add(d, false) + if err != nil || !items[0].AwaitingApproval || items[0].Title != "a.md" { + t.Fatalf("untrusted add %+v %v", items, err) + } + if c, _ := e.Fetch(context.Background(), items[0]); !strings.Contains(c.Markdown, "not authenticated") { + t.Fatalf("untrusted content %q", c.Markdown) + } + ok, err := e.Approve(items[0].ID, "owner") + if err != nil || ok.AwaitingApproval { + t.Fatalf("approve %+v %v", ok, err) + } + if again, err := e.Approve(items[0].ID, "owner"); err != nil || again.AwaitingApproval { + t.Fatalf("approve twice %+v %v", again, err) + } + if _, err := e.Approve("nope", "owner"); !errors.Is(err, ErrEmailNotFound) { + t.Fatalf("approve bad id: %v", err) + } + if _, err := e.Approve(strings.Repeat("b", emailIDHexLen), "owner"); !errors.Is(err, ErrEmailNotFound) { + t.Fatalf("approve missing: %v", err) + } + + // Declining deletes; unapproved items expire. + d2 := emailDelivery("m3", att("b.png", wire.AttachImage, "image/png", emailPNG)) + pending, _ := e.Add(d2, false) + if err := e.Decline(pending[0].ID); err != nil { + t.Fatal(err) + } + if err := e.Decline(pending[0].ID); !errors.Is(err, ErrEmailNotFound) { + t.Fatalf("decline twice: %v", err) + } + if err := e.Decline("x"); !errors.Is(err, ErrEmailNotFound) { + t.Fatalf("decline bad id: %v", err) + } + stale, _ := e.Add(emailDelivery("m4", att("c.png", wire.AttachImage, "image/png", emailPNG)), false) + *now = now.Add(EmailPendingRetention + time.Hour) + _, _ = e.Add(emailDelivery("m5", att("d.md", wire.AttachMarkdown, "text/markdown", emailMD)), true) + if _, err := e.readMeta(stale[0].ID); !errors.Is(err, ErrEmailNotFound) { + t.Fatalf("stale pending item kept: %v", err) + } + if _, err := e.readMeta(items[0].ID); err != nil { + t.Fatalf("approved item pruned: %v", err) + } +} + +func TestEmailSourceQuotaAndOpen(t *testing.T) { + e, _ := newEmailSource(t) + e.QuotaBytes = int64(len(emailMD) + 1) + if _, err := e.Add(emailDelivery("q1", att("a.md", wire.AttachMarkdown, "text/markdown", emailMD)), true); err != nil { + t.Fatal(err) + } + if _, err := e.Add(emailDelivery("q2", att("b.md", wire.AttachMarkdown, "text/markdown", emailMD)), true); !errors.Is(err, ErrEmailQuota) { + t.Fatalf("quota: %v", err) + } + if (&EmailSource{}).quota() != DefaultEmailQuotaBytes { + t.Fatal("default quota") + } + + e2, _ := newEmailSource(t) + items, _ := e2.Add(emailDelivery("o1", att("v.webm", wire.AttachAudio, "audio/webm", emailAudio), att("p.png", wire.AttachImage, "image/png", emailPNG)), true) + rc, mime, size, err := e2.OpenAudio(context.Background(), items[0]) + if err != nil || mime != "audio/webm" || size != int64(len(emailAudio)) { + t.Fatalf("open audio %q %d %v", mime, size, err) + } + b, _ := io.ReadAll(rc) + _ = rc.Close() + if string(b) != string(emailAudio) { + t.Fatal("audio bytes") + } + if _, _, _, err := e2.OpenAudio(context.Background(), items[1]); !errors.Is(err, ErrNoAudio) { + t.Fatalf("image as audio: %v", err) + } + if _, _, _, err := e2.OpenAudio(context.Background(), Item{ID: "zz"}); err == nil { + t.Fatal("bad id opened") + } + if _, _, err := e2.Open(strings.Repeat("c", emailIDHexLen)); !errors.Is(err, ErrEmailNotFound) { + t.Fatalf("open missing: %v", err) + } + // A wav keeps its MIME by extension. + items, _ = e2.Add(emailDelivery("o2", att("r.wav", wire.AttachAudio, "audio/wave", emailAudio)), true) + if _, mime, _, _ := e2.OpenAudio(context.Background(), items[0]); mime != "audio/wav" { + t.Fatalf("wav mime %q", mime) + } + // An unknown audio extension falls back to the sniffed type. + items, _ = e2.Add(emailDelivery("o3", att("noext", wire.AttachAudio, "audio/ogg", emailAudio)), true) + if _, mime, _, _ := e2.OpenAudio(context.Background(), items[0]); mime != "audio/ogg" { + t.Fatalf("fallback mime %q", mime) + } + // A corrupt meta file is skipped by List. + if err := os.WriteFile(filepath.Join(e2.Dir, items[0].ID, emailMetaFile), []byte("{"), 0o600); err != nil { + t.Fatal(err) + } + listed, _ := e2.List(context.Background()) + for _, it := range listed { + if it.ID == items[0].ID { + t.Fatal("corrupt item listed") + } + } +} + +func TestEmailItemsGatedInService(t *testing.T) { + e, _ := newEmailSource(t) + dest := &contentDest{} + svc := NewService(config.InboxConfig{Rules: []config.InboxRule{{Source: EmailType, Destination: DestDibs}}}, NewStore(filepath.Join(t.TempDir(), "inbox.json")), nil, []Destination{dest}, nil) + svc.Run = func(fn func()) { fn() } + svc.EnableEmail(e) + if svc.Email() != e || !svc.HasSource(EmailType) { + t.Fatal("email source not enabled") + } + svc.SetSources(nil) + if !svc.HasSource(EmailType) { + t.Fatal("SetSources dropped the email source") + } + pending, _ := e.Add(emailDelivery("g1", att("x.md", wire.AttachMarkdown, "text/markdown", emailMD)), false) + ctx := context.Background() + it, ok, _ := svc.Find(ctx, EmailType, pending[0].ID) + if !ok || !it.AwaitingApproval { + t.Fatalf("find %+v", it) + } + if _, err := svc.Route(ctx, it, Request{Destination: DestDibs}); !errors.Is(err, ErrAwaitingApproval) { + t.Fatalf("route an unapproved item: %v", err) + } + if n, _ := svc.Sweep(ctx); n != 0 || svc.Store.Seen(it.Key()) { + t.Fatalf("sweep routed or marked an unapproved item: %d", n) + } + if _, err := e.Approve(it.ID, "owner"); err != nil { + t.Fatal(err) + } + if n, _ := svc.Sweep(ctx); n != 1 { + t.Fatalf("approved item not auto-routed: %d", n) + } + svc.Wait() + if !strings.Contains(dest.got.Markdown, "Ship it") { + t.Fatalf("routed content %+v", dest.got) + } + // Dismissing ("Knowledge only") stays allowed while awaiting approval. + other, _ := e.Add(emailDelivery("g2", att("y.md", wire.AttachMarkdown, "text/markdown", emailMD)), false) + oit, _, _ := svc.Find(ctx, EmailType, other[0].ID) + if _, err := svc.Route(ctx, oit, Request{Destination: DestDismiss}); err != nil { + t.Fatalf("dismiss an unapproved item: %v", err) + } + var nilSvc Service + if nilSvc.Email() != nil { + t.Fatal("zero service has an email source") + } +} + +func TestEmailAudioTranscribesFromTheSpool(t *testing.T) { + e, _ := newEmailSource(t) + svc := NewService(config.InboxConfig{Transcription: activeTx()}, NewStore(filepath.Join(t.TempDir(), "inbox.json")), nil, nil, nil) + svc.Run = func(fn func()) { fn() } + store := NewTranscriptStore(filepath.Join(t.TempDir(), "transcripts")) + svc.EnableTranscription(store) + fake := &transcribe.Fake{Result: transcribe.Result{Segments: []transcribe.Segment{{Text: "hello from email"}}, Duration: time.Second}} + svc.NewTranscriber = func(config.InboxTranscription) (transcribe.Transcriber, error) { return fake, nil } + svc.TranscribeBackoff = func(int) time.Duration { return 0 } + svc.EnableEmail(e) + ctx := context.Background() + + pending, _ := e.Add(emailDelivery("a1", att("memo.m4a", wire.AttachAudio, "audio/mp4", emailAudio)), false) + it, _, _ := svc.Find(ctx, EmailType, pending[0].ID) + if it.Transcribable { + t.Fatal("an unapproved recording must not be transcribable") + } + if _, err := e.Approve(it.ID, "owner"); err != nil { + t.Fatal(err) + } + it, _, _ = svc.Find(ctx, EmailType, it.ID) + if !it.Transcribable { + t.Fatalf("approved audio not transcribable: %+v", it) + } + if _, err := svc.Transcribe(ctx, it, TranscribeOptions{}); err != nil { + t.Fatal(err) + } + rec, ok := store.Get(EmailType, it.ID) + if !ok || rec.Status != TranscriptionDone || !strings.Contains(rec.Text, "hello from email") || fake.Bytes() != int64(len(emailAudio)) { + t.Fatalf("transcript %+v", rec) + } + c, err := svc.Content(ctx, it) + if err != nil || !strings.Contains(c.Transcript, "hello from email") { + t.Fatalf("content %+v %v", c, err) + } + + // Markdown has no audio. + md, _ := e.Add(emailDelivery("a3", att("n.md", wire.AttachMarkdown, "text/markdown", emailMD)), true) + mit, _, _ := svc.Find(ctx, EmailType, md[0].ID) + if svc.hasAudio(mit) || mit.Transcribable { + t.Fatal("markdown has no audio") + } +} diff --git a/src/pkg/inbox/inbox.go b/src/pkg/inbox/inbox.go index fb3b631351..ad46a451bc 100644 --- a/src/pkg/inbox/inbox.go +++ b/src/pkg/inbox/inbox.go @@ -6,6 +6,7 @@ package inbox import ( "context" "errors" + "io" "time" ) @@ -91,6 +92,12 @@ type Item struct { AudioExpired bool `json:"audio_expired,omitempty"` // AudioRetentionDays is how long a hive recording's audio is kept. AudioRetentionDays int `json:"audio_retention_days,omitempty"` + // AwaitingApproval marks an item from outside the hive's allowlist (the + // email source, #18395): it cannot be routed, shared or transcribed until + // the owner approves it. + AwaitingApproval bool `json:"awaiting_approval,omitempty"` + // Sender is the From address of an emailed item. + Sender string `json:"sender,omitempty"` } // Key returns the stable "source/id" key used by the state store. @@ -153,6 +160,14 @@ type AudioSource interface { // ErrNoAudio is returned when an item's source offers no audio. var ErrNoAudio = errors.New("no audio is available for this item") +// AudioOpener is optionally implemented by sources that hold an item's audio +// themselves (the email spool) instead of minting a download link: the +// transcriber reads it directly. It returns the audio, its MIME type and its +// size. +type AudioOpener interface { + OpenAudio(ctx context.Context, it Item) (io.ReadCloser, string, int64, error) +} + // SummaryRunes bounds Item.Summary in listings. const SummaryRunes = 2000 diff --git a/src/pkg/inbox/service.go b/src/pkg/inbox/service.go index 82a59303e6..6aa8335573 100644 --- a/src/pkg/inbox/service.go +++ b/src/pkg/inbox/service.go @@ -104,6 +104,9 @@ type Service struct { // recordings is the dashboard recording source (see recordingsource.go); // nil until EnableRecordings. recordings *RecordingSource + // email holds files mailed to hive@ (email_source.go); nil until + // EnableEmail. + email *EmailSource } // NewService builds a service for cfg with the given sources/destinations. @@ -138,6 +141,9 @@ func (s *Service) SetSources(sources []Source) { next[RecordingType] = s.recordings } } + if s.email != nil { + next[EmailType] = s.email + } s.sources = next s.mu.Unlock() } @@ -156,6 +162,23 @@ func (s *Service) EnableUploads(u *UploadSource) { } } +// EnableEmail registers the email spool source (#18395). +func (s *Service) EnableEmail(e *EmailSource) { + s.mu.Lock() + defer s.mu.Unlock() + s.email = e + if e != nil { + s.sources[EmailType] = e + } +} + +// Email returns the email source, or nil when it is not enabled. +func (s *Service) Email() *EmailSource { + s.mu.RLock() + defer s.mu.RUnlock() + return s.email +} + // Uploads returns the upload source, or nil when uploads are not enabled. func (s *Service) Uploads() *UploadSource { s.mu.RLock() @@ -364,6 +387,9 @@ var ErrUnknownDestination = errors.New("unknown destination") // Route queues it for dest and runs the delivery in the background. The // returned state is the queued state; callers poll Items for progress. func (s *Service) Route(ctx context.Context, it Item, req Request) (RouteState, error) { + if it.AwaitingApproval && req.Destination != DestDismiss { + return RouteState{}, ErrAwaitingApproval + } if req.Destination == DestDismiss { if req.ShareID != "" { ref := refFromResult(it, DestDismiss, Result{}) @@ -481,7 +507,9 @@ func (s *Service) Sweep(ctx context.Context) (routed int, err error) { items, lerr := s.Items(ctx) rules := s.Config().Rules for _, it := range items { - if s.Store.Seen(it.Key()) { + if s.Store.Seen(it.Key()) || it.AwaitingApproval { + // An unapproved item is not marked seen: once the owner + // approves it, the rules apply on the next sweep. continue } rule, ok := FirstRule(rules, it) diff --git a/src/pkg/inbox/transcription.go b/src/pkg/inbox/transcription.go index 943ba8306a..9a2a62400c 100644 --- a/src/pkg/inbox/transcription.go +++ b/src/pkg/inbox/transcription.go @@ -196,8 +196,14 @@ func (s *Service) transcriber(t config.InboxTranscription) (transcribe.Transcrib // hasAudio reports whether the item's source can mint audio links. func (s *Service) hasAudio(it Item) bool { - _, ok := s.source(it.Source).(AudioSource) - return ok && !it.AudioExpired + src := s.source(it.Source) + if _, ok := src.(AudioSource); ok { + return !it.AudioExpired + } + if _, ok := src.(AudioOpener); ok { + return it.Pending + } + return false } // Reason codes for Item.TranscribeUnavailable. no_key means the resolved @@ -259,7 +265,7 @@ func (s *Service) applyTranscripts(items []Item) { } } busy := ok && (rec.Status == TranscriptionQueued || rec.Status == TranscriptionRunning || rec.Status == TranscriptionDone) - it.Transcribable = active && it.Pending && !busy && s.hasAudio(*it) + it.Transcribable = active && it.Pending && !busy && !it.AwaitingApproval && s.hasAudio(*it) if it.Pending && !busy && s.hasAudio(*it) { if !blockedKnown { blocked, blockedKnown = s.transcribeBlockedReason(txCfg, store), true @@ -504,6 +510,35 @@ func (s *Service) transcribeItem(ctx context.Context, it Item, cfg config.InboxT return tr.Transcribe(ctx, audio) } +// transcribeOpened transcribes audio a source holds itself (AudioOpener). +func (s *Service) transcribeOpened(ctx context.Context, tr transcribe.Transcriber, op AudioOpener, it Item, cfg config.InboxTranscription, rec *TranscriptRecord, job *txJob) (transcribe.Result, error) { + probe, mime, size, err := op.OpenAudio(ctx, it) + if err != nil { + return transcribe.Result{}, err + } + _ = probe.Close() + maxBytes := cfg.EffectiveMaxBytes() + if maxBytes > 0 && size > maxBytes { + return transcribe.Result{}, transcribe.ErrTooLarge + } + rec.AudioSize = size + s.Logger.Info("inbox: transcribing", "item", it.Key(), "provider", tr.Name(), "audio_host", "local") + audio := transcribe.Audio{ + MIME: mime, + Duration: time.Duration(it.DurationMS) * time.Millisecond, + Lang: cfg.EffectiveLanguage(), + Progress: func(p transcribe.Progress) { s.txProgress(job, p) }, + Open: func(ctx context.Context) (io.ReadCloser, error) { + rc, _, _, err := op.OpenAudio(ctx, it) + if err != nil { + return nil, err + } + return readCloser{Reader: transcribe.LimitReader(rc, maxBytes), Closer: rc}, nil + }, + } + return tr.Transcribe(ctx, audio) +} + type readCloser struct { io.Reader io.Closer From 1d08952c5d4893887f502ed96ff8fc3b4a7e0cd0 Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 14:21:43 -0500 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=90=9B=20inbox:=20the=20email=20sourc?= =?UTF-8?q?e=20implements=20v5's=20AudioOpener=20(LocalAudio)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Andy Anderson --- src/pkg/inbox/email_source.go | 21 ++++++++++++++------- src/pkg/inbox/email_source_test.go | 25 ++++++++++++++----------- src/pkg/inbox/inbox.go | 9 --------- src/pkg/inbox/transcription.go | 29 ----------------------------- 4 files changed, 28 insertions(+), 56 deletions(-) diff --git a/src/pkg/inbox/email_source.go b/src/pkg/inbox/email_source.go index 96c258a7e0..d238adf14d 100644 --- a/src/pkg/inbox/email_source.go +++ b/src/pkg/inbox/email_source.go @@ -385,19 +385,26 @@ func alignedLabel(aligned bool) string { return "not authenticated" } -// OpenAudio implements AudioOpener for audio items. -func (e *EmailSource) OpenAudio(_ context.Context, it Item) (io.ReadCloser, string, int64, error) { - rc, m, err := e.Open(it.ID) +// OpenAudio implements AudioOpener for audio items: the transcriber reads +// the spooled file directly. +func (e *EmailSource) OpenAudio(_ context.Context, it Item) (LocalAudio, error) { + if !validEmailID(it.ID) { + return LocalAudio{}, ErrEmailNotFound + } + m, err := e.readMeta(it.ID) if err != nil { - return nil, "", 0, err + return LocalAudio{}, err } if m.Kind != wire.AttachAudio { - _ = rc.Close() - return nil, "", 0, ErrNoAudio + return LocalAudio{}, ErrNoAudio + } + f, err := os.Open(filepath.Join(e.itemDir(it.ID), emailDataFile)) + if err != nil { + return LocalAudio{}, err } mime := audioMIMEByExt[strings.ToLower(filepath.Ext(m.Filename))] if mime == "" { mime = m.ContentType } - return rc, mime, m.Size, nil + return LocalAudio{File: f, Name: m.Filename, MIME: mime, Size: m.Size, ModTime: m.ReceivedAt}, nil } diff --git a/src/pkg/inbox/email_source_test.go b/src/pkg/inbox/email_source_test.go index 219a80e054..a56d067253 100644 --- a/src/pkg/inbox/email_source_test.go +++ b/src/pkg/inbox/email_source_test.go @@ -163,19 +163,22 @@ func TestEmailSourceQuotaAndOpen(t *testing.T) { e2, _ := newEmailSource(t) items, _ := e2.Add(emailDelivery("o1", att("v.webm", wire.AttachAudio, "audio/webm", emailAudio), att("p.png", wire.AttachImage, "image/png", emailPNG)), true) - rc, mime, size, err := e2.OpenAudio(context.Background(), items[0]) - if err != nil || mime != "audio/webm" || size != int64(len(emailAudio)) { - t.Fatalf("open audio %q %d %v", mime, size, err) + la, err := e2.OpenAudio(context.Background(), items[0]) + if err != nil || la.MIME != "audio/webm" || la.Size != int64(len(emailAudio)) || la.Name != "v.webm" { + t.Fatalf("open audio %+v %v", la, err) } - b, _ := io.ReadAll(rc) - _ = rc.Close() + b, _ := io.ReadAll(la.File) + _ = la.File.Close() if string(b) != string(emailAudio) { t.Fatal("audio bytes") } - if _, _, _, err := e2.OpenAudio(context.Background(), items[1]); !errors.Is(err, ErrNoAudio) { + if _, err := e2.OpenAudio(context.Background(), items[1]); !errors.Is(err, ErrNoAudio) { t.Fatalf("image as audio: %v", err) } - if _, _, _, err := e2.OpenAudio(context.Background(), Item{ID: "zz"}); err == nil { + if _, err := e2.OpenAudio(context.Background(), Item{ID: "zz"}); err == nil { + t.Fatal("bad id opened") + } + if _, err := e2.OpenAudio(context.Background(), Item{ID: strings.Repeat("d", emailIDHexLen)}); err == nil { t.Fatal("bad id opened") } if _, _, err := e2.Open(strings.Repeat("c", emailIDHexLen)); !errors.Is(err, ErrEmailNotFound) { @@ -183,13 +186,13 @@ func TestEmailSourceQuotaAndOpen(t *testing.T) { } // A wav keeps its MIME by extension. items, _ = e2.Add(emailDelivery("o2", att("r.wav", wire.AttachAudio, "audio/wave", emailAudio)), true) - if _, mime, _, _ := e2.OpenAudio(context.Background(), items[0]); mime != "audio/wav" { - t.Fatalf("wav mime %q", mime) + if la, _ := e2.OpenAudio(context.Background(), items[0]); la.MIME != "audio/wav" { + t.Fatalf("wav mime %q", la.MIME) } // An unknown audio extension falls back to the sniffed type. items, _ = e2.Add(emailDelivery("o3", att("noext", wire.AttachAudio, "audio/ogg", emailAudio)), true) - if _, mime, _, _ := e2.OpenAudio(context.Background(), items[0]); mime != "audio/ogg" { - t.Fatalf("fallback mime %q", mime) + if la, _ := e2.OpenAudio(context.Background(), items[0]); la.MIME != "audio/ogg" { + t.Fatalf("fallback mime %q", la.MIME) } // A corrupt meta file is skipped by List. if err := os.WriteFile(filepath.Join(e2.Dir, items[0].ID, emailMetaFile), []byte("{"), 0o600); err != nil { diff --git a/src/pkg/inbox/inbox.go b/src/pkg/inbox/inbox.go index ad46a451bc..1d6cd03103 100644 --- a/src/pkg/inbox/inbox.go +++ b/src/pkg/inbox/inbox.go @@ -6,7 +6,6 @@ package inbox import ( "context" "errors" - "io" "time" ) @@ -160,14 +159,6 @@ type AudioSource interface { // ErrNoAudio is returned when an item's source offers no audio. var ErrNoAudio = errors.New("no audio is available for this item") -// AudioOpener is optionally implemented by sources that hold an item's audio -// themselves (the email spool) instead of minting a download link: the -// transcriber reads it directly. It returns the audio, its MIME type and its -// size. -type AudioOpener interface { - OpenAudio(ctx context.Context, it Item) (io.ReadCloser, string, int64, error) -} - // SummaryRunes bounds Item.Summary in listings. const SummaryRunes = 2000 diff --git a/src/pkg/inbox/transcription.go b/src/pkg/inbox/transcription.go index 9a2a62400c..2d07ee2736 100644 --- a/src/pkg/inbox/transcription.go +++ b/src/pkg/inbox/transcription.go @@ -510,35 +510,6 @@ func (s *Service) transcribeItem(ctx context.Context, it Item, cfg config.InboxT return tr.Transcribe(ctx, audio) } -// transcribeOpened transcribes audio a source holds itself (AudioOpener). -func (s *Service) transcribeOpened(ctx context.Context, tr transcribe.Transcriber, op AudioOpener, it Item, cfg config.InboxTranscription, rec *TranscriptRecord, job *txJob) (transcribe.Result, error) { - probe, mime, size, err := op.OpenAudio(ctx, it) - if err != nil { - return transcribe.Result{}, err - } - _ = probe.Close() - maxBytes := cfg.EffectiveMaxBytes() - if maxBytes > 0 && size > maxBytes { - return transcribe.Result{}, transcribe.ErrTooLarge - } - rec.AudioSize = size - s.Logger.Info("inbox: transcribing", "item", it.Key(), "provider", tr.Name(), "audio_host", "local") - audio := transcribe.Audio{ - MIME: mime, - Duration: time.Duration(it.DurationMS) * time.Millisecond, - Lang: cfg.EffectiveLanguage(), - Progress: func(p transcribe.Progress) { s.txProgress(job, p) }, - Open: func(ctx context.Context) (io.ReadCloser, error) { - rc, _, _, err := op.OpenAudio(ctx, it) - if err != nil { - return nil, err - } - return readCloser{Reader: transcribe.LimitReader(rc, maxBytes), Closer: rc}, nil - }, - } - return tr.Transcribe(ctx, audio) -} - type readCloser struct { io.Reader io.Closer From 61aaff569ead5ea3ddc4362c30e3fadcaaf17536 Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 14:36:01 -0500 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=8C=B1=20inbox:=20the=20email=20auto-?= =?UTF-8?q?route=20test=20rule=20needs=20a=20tag=20to=20match?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Andy Anderson --- src/pkg/inbox/email_source_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pkg/inbox/email_source_test.go b/src/pkg/inbox/email_source_test.go index a56d067253..6754e10345 100644 --- a/src/pkg/inbox/email_source_test.go +++ b/src/pkg/inbox/email_source_test.go @@ -209,7 +209,7 @@ func TestEmailSourceQuotaAndOpen(t *testing.T) { func TestEmailItemsGatedInService(t *testing.T) { e, _ := newEmailSource(t) dest := &contentDest{} - svc := NewService(config.InboxConfig{Rules: []config.InboxRule{{Source: EmailType, Destination: DestDibs}}}, NewStore(filepath.Join(t.TempDir(), "inbox.json")), nil, []Destination{dest}, nil) + svc := NewService(config.InboxConfig{Rules: []config.InboxRule{{Source: EmailType, Tag: EmailType, Destination: DestDibs}}}, NewStore(filepath.Join(t.TempDir(), "inbox.json")), nil, []Destination{dest}, nil) svc.Run = func(fn func()) { fn() } svc.EnableEmail(e) if svc.Email() != e || !svc.HasSource(EmailType) {