feat(mail-admin): unified inbox (#76) #3

Closed
eSlider wants to merge 1 commits from feat/unified-view#76 into main
11 changed files with 1029 additions and 2 deletions
Showing only changes of commit 1f88063412 - Show all commits
+31
View File
@@ -88,6 +88,37 @@ curl -u "$MAIL_ADMIN_USER:$MAIL_ADMIN_PASSWORD" https://mail.produktor.io/admin/
Credentials `MAIL_ADMIN_USER` / `MAIL_ADMIN_PASSWORD` are required in `.env`
(compose fails without them). The JSON API is at `/admin/api/accounts`.
## Web UI → Все письма
**https://mail.produktor.io/admin/messages** — unified inbox of **all**
mailboxes (info@, postmaster@, ano@, andriy.oblivantsev@, postman@) read
directly from the Maildir on disk: no IMAP, no user passwords. Each row shows
date, from, subject, size and links to the full message view (text/html
toggle + attachment list + headers). Messages are grouped by mailbox, sorted
newest first; the search box filters by subject/from client-side.
Read-only API (same Basic Auth):
| Endpoint | Description |
|----------|-------------|
| `GET /admin/api/messages` | Unified list, newest first. Filters: `?mailbox=` (full address or localpart), `?q=` (subject/from, case-insensitive), `?limit=` (default 100, max 500) |
| `GET /admin/api/messages/<mailbox>/<filename>` | Full message: decoded headers + unfolded `text/plain`/`text/html` bodies + `attachments` (filename/size/cid) |
```bash
curl -u "$MAIL_ADMIN_USER:$MAIL_ADMIN_PASSWORD" \
"https://mail.produktor.io/admin/api/messages?limit=5"
curl -u "$MAIL_ADMIN_USER:$MAIL_ADMIN_PASSWORD" \
"https://mail.produktor.io/admin/api/messages/ano@produktor.io/<filename>"
```
Layout assumption: `data/mail-data/<domain>/<localpart>/{cur,new}/` (docker
-mailserver default Maildir; message files in `cur/` + `new/`, `tmp/` is
transient and ignored). Mailbox and filename are validated strictly and
resolved against the real directory listing — path traversal and symlink
escape from `mail-data/` are impossible. MIME multipart bodies are unfolded
recursively with the Go stdlib (`net/mail`, `mime/multipart`,
`mime/quotedprintable`); nothing is ever written or deleted.
## Reverse proxy (NPM)
`mail.produktor.io` is a proxy host in Nginx Proxy Manager (`provider` container,
+393
View File
@@ -0,0 +1,393 @@
package main
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"io"
"io/fs"
"mime"
"mime/multipart"
"mime/quotedprintable"
"net/mail"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"time"
)
// errBadMailbox marks a mailbox identifier that fails shape validation; it is
// mapped to HTTP 400, never to a filesystem lookup.
var errBadMailbox = errors.New("invalid mailbox")
// mailboxRe accepts only the characters that can appear in a real mailbox
// address: lowercase letters, digits, dot, underscore, dash and one "@".
// Anything else (slashes, "..", uppercase, control chars) is rejected before
// it can reach the filesystem.
var mailboxRe = regexp.MustCompile(`^[a-z0-9._-]+@[a-z0-9.-]+$`)
// splitMailbox validates the full address shape and returns local/domain.
func splitMailbox(mailbox string) (local, domain string, err error) {
if !mailboxRe.MatchString(mailbox) {
return "", "", fmt.Errorf("%w: %q", errBadMailbox, mailbox)
}
local, domain, _ = strings.Cut(mailbox, "@")
return local, domain, nil
}
// messageSummary is one row of the unified inbox list.
type messageSummary struct {
Mailbox string `json:"mailbox"`
Filename string `json:"filename"`
MessageID string `json:"message_id,omitempty"`
From string `json:"from,omitempty"`
To string `json:"to,omitempty"`
Subject string `json:"subject,omitempty"`
Date time.Time `json:"date"`
Size int64 `json:"size"`
Path string `json:"path"` // maildir-relative path hint
when time.Time // sort key (Date header or file mtime), not serialized
}
// messagesResponse is the JSON payload of GET /api/messages.
type messagesResponse struct {
Updated time.Time `json:"updated"`
Count int `json:"count"`
Messages []messageSummary `json:"messages"`
}
// listMessages returns the unified inbox: every message file in cur/ and new/
// of every mailbox under root (data/mail-data/<domain>/<localpart>), newest
// first. mailboxFilter matches the full address or the bare localpart; query
// is a case-insensitive substring match on subject and from.
func listMessages(root, mailboxFilter, query string, limit int) ([]messageSummary, error) {
filter := strings.ToLower(strings.TrimSpace(query))
var out []messageSummary
err := walkMailboxes(root, func(local, domain string) error {
mailbox := local + "@" + domain
if mailboxFilter != "" && mailbox != mailboxFilter && local != mailboxFilter {
return nil
}
for _, sub := range []string{"cur", "new"} {
dir := filepath.Join(root, domain, local, sub)
entries, err := os.ReadDir(dir)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
continue // no cur/ or new/ yet
}
return err
}
for _, e := range entries {
if e.IsDir() {
continue
}
info, err := e.Info()
if err != nil {
return err
}
if !info.Mode().IsRegular() {
continue
}
path := filepath.Join(dir, e.Name())
s, err := summarizeMessage(root, path, mailbox, e.Name(), sub, info)
if err != nil {
continue // unparseable file (dovecot index, lock): skip
}
if filter != "" &&
!strings.Contains(strings.ToLower(s.Subject), filter) &&
!strings.Contains(strings.ToLower(s.From), filter) {
continue
}
out = append(out, s)
}
}
return nil
})
if err != nil {
return nil, err
}
sort.Slice(out, func(i, j int) bool { return out[i].when.After(out[j].when) })
if limit > 0 && len(out) > limit {
out = out[:limit]
}
return out, nil
}
// walkMailboxes visits every <domain>/<localpart> maildir under root.
func walkMailboxes(root string, fn func(local, domain string) error) error {
domains, err := os.ReadDir(root)
if err != nil {
return err
}
for _, d := range domains {
if !d.IsDir() {
continue
}
users, err := os.ReadDir(filepath.Join(root, d.Name()))
if err != nil {
continue
}
for _, u := range users {
if u.IsDir() {
if err := fn(u.Name(), d.Name()); err != nil {
return err
}
}
}
}
return nil
}
// summarizeMessage reads the headers of one message file. Files that do not
// parse as RFC 5322 (empty, binary index leftovers) still appear in the list
// with filename and size only.
func summarizeMessage(root, path, mailbox, filename, sub string, info fs.FileInfo) (messageSummary, error) {
s := messageSummary{
Mailbox: mailbox,
Filename: filename,
Size: info.Size(),
when: info.ModTime(),
}
if rel, err := filepath.Rel(root, path); err == nil {
s.Path = rel
}
f, err := os.Open(path)
if err != nil {
return s, err
}
defer f.Close()
msg, err := mail.ReadMessage(f)
if err != nil {
return s, nil // not a message: keep the row, no headers
}
h := msg.Header
s.MessageID = decodeHeader(h.Get("Message-Id"))
s.From = headerAddress(h.Get("From"))
s.To = headerAddress(h.Get("To"))
s.Subject = decodeHeader(h.Get("Subject"))
if d, err := mail.ParseDate(h.Get("Date")); err == nil {
s.Date, s.when = d, d
}
return s, nil
}
// headerAddress extracts the first address from an RFC 5322 address header,
// falling back to the raw value when parsing fails.
func headerAddress(v string) string {
v = strings.TrimSpace(v)
if v == "" {
return ""
}
if addrs, err := mail.ParseAddressList(v); err == nil && len(addrs) > 0 {
return addrs[0].String()
}
return v
}
// attachmentInfo describes one MIME attachment part.
type attachmentInfo struct {
Filename string `json:"filename,omitempty"`
Size int64 `json:"size"`
CID string `json:"cid,omitempty"`
}
// headerMap keeps header values per name (multiple values allowed, e.g.
// Received) with a convenience Getter like net/http.Header.
type headerMap map[string][]string
func (h headerMap) Get(key string) string {
if v := h[key]; len(v) > 0 {
return v[0]
}
return ""
}
// messageDetail is the JSON payload of GET /api/messages/<mailbox>/<filename>:
// decoded headers, the unfolded text/plain and text/html bodies and a list of
// attachments. Read-only: the underlying message file is never modified.
type messageDetail struct {
Mailbox string `json:"mailbox"`
Filename string `json:"filename"`
Size int64 `json:"size"`
Headers headerMap `json:"headers"`
Text string `json:"text"`
HTML string `json:"html"`
Attachments []attachmentInfo `json:"attachments"`
}
// Subject, From and To expose decoded display values from the header map
// (headers are stored decoded by parseMessage).
func (d messageDetail) Subject() string { return d.Headers.Get("Subject") }
func (d messageDetail) From() string { return headerAddress(d.Headers.Get("From")) }
func (d messageDetail) To() string { return headerAddress(d.Headers.Get("To")) }
// readMessage resolves mailbox+filename against the real listing (a request
// filename can never build a path of its own) and parses the message. It
// returns fs.ErrNotExist when the file is not in cur/ or new/ of the mailbox.
func readMessage(root, mailbox, filename string) (messageDetail, error) {
local, domain, err := splitMailbox(mailbox)
if err != nil {
return messageDetail{}, err
}
full := ""
for _, sub := range []string{"cur", "new"} {
dir := filepath.Join(root, domain, local, sub)
entries, err := os.ReadDir(dir)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
continue
}
return messageDetail{}, err
}
for _, e := range entries {
if !e.IsDir() && e.Name() == filename {
full = filepath.Join(dir, e.Name())
break
}
}
}
if full == "" {
return messageDetail{}, os.ErrNotExist
}
info, err := os.Stat(full)
if err != nil {
return messageDetail{}, err
}
data, err := os.ReadFile(full)
if err != nil {
return messageDetail{}, err
}
d, err := parseMessage(data, mailbox, filename)
if err != nil {
return messageDetail{}, err
}
d.Size = info.Size()
return d, nil
}
// parseMessage splits a raw message into decoded headers, text/html bodies
// and attachment metadata. multipart bodies are unfolded recursively.
func parseMessage(data []byte, mailbox, filename string) (messageDetail, error) {
msg, err := mail.ReadMessage(bytes.NewReader(data))
if err != nil {
return messageDetail{}, err
}
d := messageDetail{
Mailbox: mailbox,
Filename: filename,
Headers: headerMap{},
}
for k, vs := range msg.Header {
decoded := make([]string, len(vs))
for i, v := range vs {
decoded[i] = decodeHeader(v)
}
d.Headers[k] = decoded
}
mediatype, params, _ := mime.ParseMediaType(msg.Header.Get("Content-Type"))
if mediatype == "" {
mediatype = "text/plain"
}
if strings.HasPrefix(mediatype, "multipart/") {
if err := parseMultipart(msg.Body, params["boundary"], &d); err != nil {
return messageDetail{}, err
}
return d, nil
}
body, err := io.ReadAll(decodeTransfer(msg.Header.Get("Content-Transfer-Encoding"), msg.Body))
if err != nil {
return messageDetail{}, err
}
addPart(msg.Header, mediatype, body, &d)
return d, nil
}
// parseMultipart walks one multipart/* body, recursing into nested
// multipart parts (mixed/alternative/related).
func parseMultipart(r io.Reader, boundary string, d *messageDetail) error {
mr := multipart.NewReader(r, boundary)
for {
part, err := mr.NextPart()
if err == io.EOF {
return nil
}
if err != nil {
return err
}
mediatype, params, _ := mime.ParseMediaType(part.Header.Get("Content-Type"))
if mediatype == "" {
mediatype = "text/plain"
}
if strings.HasPrefix(mediatype, "multipart/") {
if err := parseMultipart(part, params["boundary"], d); err != nil {
return err
}
part.Close()
continue
}
data, err := io.ReadAll(decodeTransfer(part.Header.Get("Content-Transfer-Encoding"), part))
if err != nil {
part.Close()
return err
}
addPart(part.Header, mediatype, data, d)
part.Close()
}
}
// addPart dispatches one leaf MIME part: text/plain and text/html feed the
// body views, everything else becomes an attachment entry.
func addPart(h interface{ Get(string) string }, mediatype string, data []byte, d *messageDetail) {
switch mediatype {
case "text/plain":
d.Text += string(data)
case "text/html":
d.HTML += string(data)
default:
a := attachmentInfo{Size: int64(len(data))}
if cd := h.Get("Content-Disposition"); cd != "" {
if _, p, err := mime.ParseMediaType(cd); err == nil {
a.Filename = p["filename"]
}
}
if a.Filename == "" {
if _, p, err := mime.ParseMediaType(h.Get("Content-Type")); err == nil {
a.Filename = p["name"]
}
}
if cid := h.Get("Content-Id"); cid != "" {
a.CID = strings.Trim(cid, "<>")
}
d.Attachments = append(d.Attachments, a)
}
}
// decodeTransfer wraps a part body with the decoder for its
// Content-Transfer-Encoding; base64 and quoted-printable are the only
// encodings that need one, everything else is read verbatim.
func decodeTransfer(enc string, r io.Reader) io.Reader {
switch strings.ToLower(strings.TrimSpace(enc)) {
case "base64":
return base64.NewDecoder(base64.StdEncoding, r)
case "quoted-printable":
return quotedprintable.NewReader(r)
default:
return r
}
}
// wordDecoder decodes RFC 2047 encoded words in header values.
var wordDecoder = &mime.WordDecoder{}
func decodeHeader(v string) string {
if v == "" {
return ""
}
if d, err := wordDecoder.DecodeHeader(v); err == nil {
return d
}
return v
}
+278
View File
@@ -0,0 +1,278 @@
package main
import (
"encoding/json"
"errors"
"io/fs"
"net/http"
"strings"
"testing"
)
// Fixture additions (testdata/mail/produktor.io):
//
// ano/cur/2.multipart — multipart/mixed: alternative (text/plain+text/html)
// + image/png attachment (cid logo-cid-1), RFC 2047 encoded subject.
// ano/cur/3.searchme — simple message with subject/from search targets.
//
// Combined with the pre-existing fixtures the unified inbox contains:
// info 3 (cur 1.fixture1, 2.fixture2 + new 3.fixture3), ano 3, postmaster 1.
func TestListMessagesCounts(t *testing.T) {
msgs, err := listMessages("testdata/mail", "", "", 500)
if err != nil {
t.Fatal(err)
}
if len(msgs) != 7 {
t.Fatalf("got %d messages, want 7 (info 3 + ano 3 + postmaster 1)", len(msgs))
}
byMailbox := map[string]int{}
for _, m := range msgs {
byMailbox[m.Mailbox]++
if m.Size <= 0 {
t.Errorf("%s/%s: size = %d, want > 0", m.Mailbox, m.Filename, m.Size)
}
if m.Filename == "" {
t.Error("empty filename in listing")
}
}
want := map[string]int{
"info@produktor.io": 3,
"ano@produktor.io": 3,
"postmaster@produktor.io": 1,
}
for mb, n := range want {
if byMailbox[mb] != n {
t.Errorf("mailbox %s: got %d messages, want %d", mb, byMailbox[mb], n)
}
}
}
func TestListMessagesSortNewestFirst(t *testing.T) {
msgs, err := listMessages("testdata/mail", "", "", 500)
if err != nil {
t.Fatal(err)
}
// 3.searchme (Date 11:00 +0100) is newer than 2.multipart (10:00 +0100);
// messages without a parseable Date fall back to file mtime.
if msgs[0].Filename != "3.searchme" {
t.Errorf("first = %s/%s, want ano/3.searchme", msgs[0].Mailbox, msgs[0].Filename)
}
if msgs[1].Filename != "2.multipart" {
t.Errorf("second = %s/%s, want ano/2.multipart", msgs[1].Mailbox, msgs[1].Filename)
}
}
func TestListMessagesMailboxFilter(t *testing.T) {
msgs, err := listMessages("testdata/mail", "info@produktor.io", "", 500)
if err != nil {
t.Fatal(err)
}
if len(msgs) != 3 {
t.Fatalf("got %d, want 3 for info@produktor.io", len(msgs))
}
for _, m := range msgs {
if m.Mailbox != "info@produktor.io" {
t.Errorf("mailbox = %q, want info@produktor.io", m.Mailbox)
}
}
// localpart-only filter is accepted too
msgs, err = listMessages("testdata/mail", "postmaster", "", 500)
if err != nil {
t.Fatal(err)
}
if len(msgs) != 1 {
t.Fatalf("localpart filter: got %d, want 1", len(msgs))
}
}
func TestListMessagesQuery(t *testing.T) {
msgs, err := listMessages("testdata/mail", "", "acme", 500)
if err != nil {
t.Fatal(err)
}
if len(msgs) != 1 || msgs[0].Filename != "3.searchme" {
t.Fatalf("q=acme: got %+v, want 1 hit (3.searchme)", msgs)
}
// case-insensitive on subject
msgs, err = listMessages("testdata/mail", "", "INVOICE", 500)
if err != nil {
t.Fatal(err)
}
if len(msgs) != 1 || msgs[0].Subject != "Invoice #42 from Acme Corp" {
t.Fatalf("q=INVOICE: got %+v, want 3.searchme", msgs)
}
msgs, err = listMessages("testdata/mail", "", "no such term", 500)
if err != nil {
t.Fatal(err)
}
if len(msgs) != 0 {
t.Fatalf("q=no-match: got %d, want 0", len(msgs))
}
}
func TestListMessagesLimit(t *testing.T) {
msgs, err := listMessages("testdata/mail", "", "", 2)
if err != nil {
t.Fatal(err)
}
if len(msgs) != 2 {
t.Fatalf("limit 2: got %d, want 2", len(msgs))
}
}
func TestReadMessageMultipart(t *testing.T) {
d, err := readMessage("testdata/mail", "ano@produktor.io", "2.multipart")
if err != nil {
t.Fatal(err)
}
if got := d.Headers.Get("Subject"); !strings.Contains(got, "Multipart fixture with") {
t.Errorf("Subject header = %q, want RFC 2047 decoded", got)
}
if !strings.Contains(d.Text, "hello plain body") {
t.Errorf("Text = %q, want plain body", d.Text)
}
if !strings.Contains(d.HTML, "<b>html</b>") {
t.Errorf("HTML = %q, want html body", d.HTML)
}
if len(d.Attachments) != 1 {
t.Fatalf("attachments = %d, want 1", len(d.Attachments))
}
a := d.Attachments[0]
if a.Filename != "logo.png" || a.CID != "logo-cid-1" || a.Size != 8 {
t.Errorf("attachment = %+v, want logo.png cid=logo-cid-1 size=8", a)
}
if d.Headers.Get("From") == "" || d.Headers.Get("To") == "" || d.Headers.Get("Date") == "" {
t.Error("expected from/to/date headers")
}
}
func TestReadMessageSimple(t *testing.T) {
d, err := readMessage("testdata/mail", "ano@produktor.io", "3.searchme")
if err != nil {
t.Fatal(err)
}
if d.Subject() != "Invoice #42 from Acme Corp" {
t.Errorf("subject = %q", d.Subject())
}
if !strings.Contains(d.Text, "Please pay") {
t.Errorf("Text = %q, want body", d.Text)
}
if d.HTML != "" {
t.Errorf("HTML = %q, want empty for plain message", d.HTML)
}
if len(d.Attachments) != 0 {
t.Errorf("attachments = %d, want 0", len(d.Attachments))
}
}
func TestReadMessagePathSafety(t *testing.T) {
cases := []struct{ mailbox, filename string }{
{"../../etc", "passwd"},
{"ano@produktor.io", "../../etc/passwd"},
{"ano@produktor.io", "..%2F1.fixtureano"},
{"ano@produktor.io", "nonexistent"},
{"ANO@produktor.io", "1.fixtureano"}, // uppercase rejected
{"ano..@produktor.io", "1.fixtureano"},
{"ano@produktor.io/../info", "1.fixture1"},
}
for _, c := range cases {
if _, err := readMessage("testdata/mail", c.mailbox, c.filename); err == nil {
t.Errorf("readMessage(%q, %q): expected error, got nil", c.mailbox, c.filename)
} else if !errors.Is(err, fs.ErrNotExist) && !errors.Is(err, errBadMailbox) {
t.Errorf("readMessage(%q, %q): err = %v, want errBadMailbox or fs.ErrNotExist", c.mailbox, c.filename, err)
}
}
}
func TestAPIMessages(t *testing.T) {
h := testServer(t).handler()
rec := doAuth(t, h, "/admin/api/messages", "bot", "s3cret")
if rec.Code != http.StatusOK {
t.Fatalf("code = %d, want 200: %s", rec.Code, rec.Body.String())
}
var resp messagesResponse
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
t.Fatal(err)
}
if resp.Count != 7 || len(resp.Messages) != 7 {
t.Fatalf("count = %d len = %d, want 7", resp.Count, len(resp.Messages))
}
if resp.Messages[0].Filename != "3.searchme" {
t.Errorf("first = %+v, want 3.searchme (newest first)", resp.Messages[0])
}
rec = doAuth(t, h, "/admin/api/messages?mailbox=postmaster@produktor.io", "bot", "s3cret")
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
t.Fatal(err)
}
if resp.Count != 1 || resp.Messages[0].Mailbox != "postmaster@produktor.io" {
t.Errorf("mailbox filter: count = %d, want 1 postmaster", resp.Count)
}
rec = doAuth(t, h, "/admin/api/messages?q=acme", "bot", "s3cret")
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
t.Fatal(err)
}
if resp.Count != 1 {
t.Errorf("q filter: count = %d, want 1", resp.Count)
}
rec = doAuth(t, h, "/admin/api/messages?limit=2", "bot", "s3cret")
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
t.Fatal(err)
}
if len(resp.Messages) != 2 {
t.Errorf("limit=2: got %d, want 2", len(resp.Messages))
}
}
func TestAPIMessageDetail(t *testing.T) {
h := testServer(t).handler()
rec := doAuth(t, h, "/admin/api/messages/ano@produktor.io/2.multipart", "bot", "s3cret")
if rec.Code != http.StatusOK {
t.Fatalf("code = %d, want 200: %s", rec.Code, rec.Body.String())
}
var d messageDetail
if err := json.Unmarshal(rec.Body.Bytes(), &d); err != nil {
t.Fatal(err)
}
if !strings.Contains(d.Text, "hello plain body") || !strings.Contains(d.HTML, "<b>html</b>") {
t.Errorf("detail body: text=%q html=%q", d.Text, d.HTML)
}
if len(d.Attachments) != 1 || d.Attachments[0].Filename != "logo.png" {
t.Errorf("attachments = %+v, want logo.png", d.Attachments)
}
}
func TestAPIMessageDetailUnsafe(t *testing.T) {
h := testServer(t).handler()
for _, p := range []string{
"/admin/api/messages/..%2F..%2Fetc/passwd",
"/admin/api/messages/ano@produktor.io/..%2F..%2Fetc/passwd",
"/admin/api/messages/ANO@produktor.io/1.fixtureano",
"/admin/api/messages/ano@produktor.io/nonexistent",
"/admin/api/messages/ano@produktor.io/1.fixtureano/extra",
} {
rec := doAuth(t, h, p, "bot", "s3cret")
if rec.Code == http.StatusOK {
t.Errorf("%s: code = %d, want 4xx", p, rec.Code)
}
}
}
func TestMessagesPagesRender(t *testing.T) {
h := testServer(t).handler()
rec := doAuth(t, h, "/admin/messages", "bot", "s3cret")
body := rec.Body.String()
if !strings.Contains(body, "All messages") || !strings.Contains(body, "ano@produktor.io") {
t.Error("messages page missing title/mailbox grouping")
}
rec = doAuth(t, h, "/admin/messages", "bot", "s3cret")
rec = doAuth(t, h, "/admin/message/ano@produktor.io/2.multipart", "bot", "s3cret")
body = rec.Body.String()
if rec.Code != http.StatusOK || !strings.Contains(body, "hello plain body") {
t.Errorf("message page: code = %d, body missing text: %s", rec.Code, body[:min(len(body), 200)])
}
}
+280 -1
View File
@@ -8,6 +8,7 @@ import (
"io/fs"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"sort"
@@ -43,6 +44,8 @@ type accountsResponse struct {
var templateFuncs = template.FuncMap{
"humanBytes": humanBytes,
"formatTime": func(t time.Time) string { return t.UTC().Format("2006-01-02 15:04 MST") },
"urlPath": url.PathEscape, // safe embedding of mailbox/filename in a URL path segment
"lower": strings.ToLower,
}
var indexTmpl = template.Must(template.New("index").Funcs(templateFuncs).Parse(`<!DOCTYPE html>
@@ -64,7 +67,7 @@ a { color: #3b82f6; }
</head>
<body>
<h1>Mail accounts — mail.produktor.io</h1>
<p class="meta">Source: <code>config/postfix-accounts.cf</code> (read-only). Updated {{.Updated | formatTime}} · <a href="{{.Base}}api/accounts">JSON</a></p>
<p class="meta">Source: <code>config/postfix-accounts.cf</code> (read-only). Updated {{.Updated | formatTime}} · <a href="{{.Base}}api/accounts">JSON</a> · <a href="{{.Base}}messages">All messages</a></p>
<table>
<thead><tr><th>Address</th><th class="num">INBOX</th><th class="num">Total</th><th class="num">Storage</th><th>Quota</th><th>Last message</th></tr></thead>
<tbody>
@@ -127,8 +130,16 @@ func (s *server) route(w http.ResponseWriter, r *http.Request) {
switch {
case path == "/" || path == "/index.html":
s.handleIndex(w, r)
case path == "/messages":
s.handleMessagesPage(w, r)
case strings.HasPrefix(path, "/message/"):
s.handleMessagePage(w, r, strings.TrimPrefix(path, "/message/"))
case path == "/api/accounts":
s.handleAPI(w, r)
case path == "/api/messages":
s.handleMessagesAPI(w, r)
case strings.HasPrefix(path, "/api/messages/"):
s.handleMessageAPI(w, r, strings.TrimPrefix(path, "/api/messages/"))
default:
http.NotFound(w, r)
}
@@ -226,3 +237,271 @@ func (s *server) handleAPI(w http.ResponseWriter, r *http.Request) {
enc.SetIndent("", " ")
enc.Encode(resp)
}
// parseLimit turns a limit query value into an int within [1, max];
// empty/invalid values fall back to def.
func parseLimit(v string, def, max int) int {
if v == "" {
return def
}
n, err := strconv.Atoi(v)
if err != nil || n < 1 {
return def
}
if n > max {
return max
}
return n
}
// splitPath2 splits the remainder of a two-segment route into exactly two
// non-empty parts; anything else (traversal, extra segments) fails.
func splitPath2(rest string) (a, b string, ok bool) {
parts := strings.Split(rest, "/")
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
return "", "", false
}
return parts[0], parts[1], true
}
func (s *server) handleMessagesAPI(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
msgs, err := listMessages(s.maildirRoot, q.Get("mailbox"), q.Get("q"), parseLimit(q.Get("limit"), 100, 500))
if err != nil {
log.Printf("listMessages: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
resp := messagesResponse{Updated: time.Now().UTC(), Count: len(msgs), Messages: msgs}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
enc.Encode(resp)
}
func (s *server) handleMessageAPI(w http.ResponseWriter, r *http.Request, rest string) {
mailbox, filename, ok := splitPath2(rest)
if !ok {
http.NotFound(w, r)
return
}
d, err := readMessage(s.maildirRoot, mailbox, filename)
switch {
case errors.Is(err, errBadMailbox):
http.Error(w, "invalid mailbox", http.StatusBadRequest)
case errors.Is(err, fs.ErrNotExist):
http.NotFound(w, r)
case err != nil:
log.Printf("readMessage(%s, %s): %v", mailbox, filename, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
default:
w.Header().Set("Content-Type", "application/json; charset=utf-8")
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
enc.Encode(d)
}
}
// messagesGroup renders one mailbox section of the unified inbox page.
type messagesGroup struct {
Mailbox string
Count int
Messages []messageSummary
}
var messagesTmpl = template.Must(template.New("messages").Funcs(templateFuncs).Parse(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>All messages — mail.produktor.io</title>
<style>
:root { color-scheme: light dark; }
body { font: 14px/1.5 system-ui, sans-serif; margin: 2rem auto; max-width: 64rem; padding: 0 1rem; }
table { border-collapse: collapse; width: 100%; }
th, td { text-align: left; padding: .45rem .6rem; border-bottom: 1px solid #4446; }
th { border-bottom-width: 2px; }
td.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
tr.mb td { background: #3b82f61a; font-weight: 600; border-top: 2px solid #4446; }
a { color: #3b82f6; }
.meta { color: #888; margin: .5rem 0 1rem; }
#q { width: 100%; box-sizing: border-box; padding: .5rem .6rem; font: inherit; margin-bottom: 1rem; }
</style>
</head>
<body>
<h1>All messages — mail.produktor.io</h1>
<p class="meta"><a href="{{.Base}}">Accounts</a> · <a href="{{.Base}}api/messages">JSON</a> · updated {{.Updated | formatTime}} · read-only view of the Maildir on disk (no IMAP)</p>
<input id="q" placeholder="Filter by subject / from…" autofocus>
<table id="msgs">
<thead><tr><th>Date</th><th>From</th><th>Subject</th><th class="num">Size</th></tr></thead>
<tbody>
{{range .Groups}}
<tr class="mb"><td colspan="4">{{.Mailbox}} · {{.Count}}</td></tr>
{{range .Messages}}
<tr class="row" data-s="{{lower .Subject}} {{lower .From}}">
<td>{{if .Date.IsZero}}—{{else}}{{.Date | formatTime}}{{end}}</td>
<td>{{.From}}</td>
<td><a href="{{$.Base}}message/{{urlPath .Mailbox}}/{{urlPath .Filename}}">{{if .Subject}}{{.Subject}}{{else}}<i>(no subject)</i>{{end}}</a></td>
<td class="num">{{.Size | humanBytes}}</td>
</tr>
{{end}}
{{end}}
</tbody>
</table>
<script>
const q = document.getElementById('q');
q.addEventListener('input', () => {
const t = q.value.trim().toLowerCase();
for (const row of document.querySelectorAll('#msgs tr.row')) {
row.style.display = row.dataset.s.includes(t) ? '' : 'none';
}
for (const g of document.querySelectorAll('#msgs tr.mb')) {
let any = false;
for (let n = g.nextElementSibling; n && !n.classList.contains('mb'); n = n.nextElementSibling) {
if (n.style.display !== 'none') any = true;
}
g.style.display = any ? '' : 'none';
}
});
</script>
</body>
</html>`))
func (s *server) handleMessagesPage(w http.ResponseWriter, r *http.Request) {
msgs, err := listMessages(s.maildirRoot, "", "", 500)
if err != nil {
log.Printf("listMessages: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var groups []messagesGroup
for _, m := range msgs {
if len(groups) == 0 || groups[len(groups)-1].Mailbox != m.Mailbox {
groups = append(groups, messagesGroup{Mailbox: m.Mailbox})
}
g := &groups[len(groups)-1]
g.Count++
g.Messages = append(g.Messages, m)
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if err := messagesTmpl.Execute(w, struct {
Updated time.Time
Base string
Groups []messagesGroup
}{time.Now().UTC(), s.basePath + "/", groups}); err != nil {
log.Printf("render messages: %v", err)
}
}
// headerPair is one row of the header table on the message page.
type headerPair struct {
Name, Value string
}
// preferredHeaderOrder lists the most useful headers first; the rest follow
// sorted alphabetically.
var preferredHeaderOrder = []string{"From", "To", "Subject", "Date", "Message-Id", "Mime-Version", "Content-Type"}
func orderedHeaders(h headerMap) []headerPair {
seen := map[string]bool{}
var rows []headerPair
for _, name := range preferredHeaderOrder {
if v := h.Get(name); v != "" {
rows = append(rows, headerPair{name, v})
seen[name] = true
}
}
var rest []string
for name := range h {
if !seen[name] {
rest = append(rest, name)
}
}
sort.Strings(rest)
for _, name := range rest {
rows = append(rows, headerPair{name, h.Get(name)})
}
return rows
}
var messageTmpl = template.Must(template.New("message").Funcs(templateFuncs).Parse(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{if .Msg.Subject}}{{.Msg.Subject}}{{else}}Message{{end}} — mail.produktor.io</title>
<style>
:root { color-scheme: light dark; }
body { font: 14px/1.5 system-ui, sans-serif; margin: 2rem auto; max-width: 56rem; padding: 0 1rem; }
a { color: #3b82f6; }
.meta { color: #888; margin: .5rem 0 1rem; }
h1 { margin-bottom: .25rem; }
pre { background: #00000022; border: 1px solid #4446; padding: .8rem; white-space: pre-wrap; word-break: break-word; }
table.headers { border-collapse: collapse; width: 100%; }
table.headers th, table.headers td { text-align: left; vertical-align: top; padding: .3rem .6rem; border-bottom: 1px solid #4446; }
table.headers th { width: 12rem; color: #888; font-weight: 600; }
.tabs { margin: 1rem 0 .5rem; }
.tabs button { font: inherit; padding: .35rem .9rem; cursor: pointer; border: 1px solid #4446; background: #00000022; }
.tabs button.on { background: #3b82f6; color: #fff; }
</style>
</head>
<body>
<p class="meta"><a href="{{.Base}}messages">← All messages</a></p>
<h1>{{if .Msg.Subject}}{{.Msg.Subject}}{{else}}<i>(no subject)</i>{{end}}</h1>
<p class="meta">{{.Msg.From}} → {{.Msg.To}} · {{.Mailbox}} · {{.Size | humanBytes}}</p>
<div class="tabs"><button id="tab-text" class="on">text</button><button id="tab-html">html</button></div>
{{if .Msg.Text}}<pre id="view-text">{{.Msg.Text}}</pre>{{else}}<pre id="view-text"><i>(no text part)</i></pre>{{end}}
{{if .Msg.HTML}}<pre id="view-html" hidden>{{.Msg.HTML}}</pre>{{else}}<pre id="view-html" hidden><i>(no html part)</i></pre>{{end}}
<h2>Attachments</h2>
<ul>
{{range .Msg.Attachments}}<li>{{if .Filename}}{{.Filename}}{{else}}<i>(unnamed)</i>{{end}} · {{.Size | humanBytes}}{{if .CID}} · cid: {{.CID}}{{end}}</li>{{else}}<li>none</li>{{end}}
</ul>
<h2>Headers</h2>
<table class="headers">
{{range .Rows}}<tr><th>{{.Name}}</th><td>{{.Value}}</td></tr>{{end}}
</table>
<script>
const text = document.getElementById('view-text');
const html = document.getElementById('view-html');
const bt = document.getElementById('tab-text');
const bh = document.getElementById('tab-html');
function show(which) {
text.hidden = which !== 'text';
html.hidden = which !== 'html';
bt.classList.toggle('on', which === 'text');
bh.classList.toggle('on', which === 'html');
}
bt.addEventListener('click', () => show('text'));
bh.addEventListener('click', () => show('html'));
</script>
</body>
</html>`))
func (s *server) handleMessagePage(w http.ResponseWriter, r *http.Request, rest string) {
mailbox, filename, ok := splitPath2(rest)
if !ok {
http.NotFound(w, r)
return
}
d, err := readMessage(s.maildirRoot, mailbox, filename)
switch {
case errors.Is(err, errBadMailbox):
http.Error(w, "invalid mailbox", http.StatusBadRequest)
case errors.Is(err, fs.ErrNotExist):
http.NotFound(w, r)
case err != nil:
log.Printf("readMessage(%s, %s): %v", mailbox, filename, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if err := messageTmpl.Execute(w, struct {
Base string
Mailbox string
Size int64
Msg messageDetail
Rows []headerPair
}{s.basePath + "/", mailbox, d.Size, d, orderedHeaders(d.Headers)}); err != nil {
log.Printf("render message: %v", err)
}
}
+1
View File
@@ -1,3 +1,4 @@
Subject: ano msg
Date: Tue, 01 Sep 2026 09:00:00 +0100
y
+33
View File
@@ -0,0 +1,33 @@
Return-Path: <sender@example.org>
From: Sender Name <sender@example.org>
To: ano@produktor.io
Subject: =?UTF-8?B?TXVsdGlwYXJ0IGZpeHR1cmUgd2l0aCDDpHR0YWNobWVudA==?=
Date: Tue, 01 Sep 2026 10:00:00 +0100
Message-ID: <multipart-fixture-1@example.org>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="----=_fixture_76"
------=_fixture_76
Content-Type: multipart/alternative; boundary="----=_fixture_alt"
------=_fixture_alt
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
hello plain body
------=_fixture_alt
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
<html><body><p>hello <b>html</b> body</p></body></html>
------=_fixture_alt--
------=_fixture_76
Content-Type: image/png; name="logo.png"
Content-Disposition: attachment; filename="logo.png"
Content-ID: <logo-cid-1>
Content-Transfer-Encoding: base64
iVBORw0KGgo=
------=_fixture_76--
+8
View File
@@ -0,0 +1,8 @@
Return-Path: <billing@acme.example>
From: Acme Billing <billing@acme.example>
To: ano@produktor.io
Subject: Invoice #42 from Acme Corp
Date: Tue, 01 Sep 2026 11:00:00 +0100
Message-ID: <invoice-42@acme.example>
Please pay the attached invoice.
+1
View File
@@ -1,4 +1,5 @@
Return-Path: <test@example.org>
Subject: fixture one
Date: Mon, 31 Aug 2026 09:00:00 +0100
body
+1
View File
@@ -1,4 +1,5 @@
Return-Path: <test@example.org>
Subject: fixture two
Date: Mon, 31 Aug 2026 09:05:00 +0100
body body
+1
View File
@@ -1,4 +1,5 @@
Return-Path: <test@example.org>
Subject: fixture three
Date: Mon, 31 Aug 2026 09:10:00 +0100
body
@@ -1,3 +1,4 @@
Subject: pm msg
Date: Mon, 31 Aug 2026 10:00:00 +0100
z