package render import ( "fmt" "io" "strings" "time" "tangled.org/matilde.at/km/internal/mail" ) // Attachment describes one attachment of a message. type Attachment struct { PartID string `json:"partId"` BlobID string `json:"blobId"` Name string `json:"name"` Type string `json:"type"` Size int64 `json:"size"` Inline bool `json:"inline"` CID string `json:"cid"` } // Attachments lists the attachments of e. An inline part with a content id // counts as an attachment and is marked inline. func Attachments(e *mail.Email) []Attachment { out := make([]Attachment, 0, len(e.Attachments)) for _, a := range e.Attachments { out = append(out, Attachment{ PartID: a.PartID, BlobID: a.BlobID, Name: a.Name, Type: a.Type, Size: a.Size, Inline: strings.EqualFold(a.Disposition, "inline") || a.CID != "", CID: a.CID, }) } return out } // TextBody returns the message body as plain text: the text/plain parts, or // HTML parts converted to text. func TextBody(e *mail.Email) string { var parts []string for _, p := range e.TextBody { v, ok := e.BodyValues[p.PartID] if !ok { continue } s := v.Value if strings.EqualFold(p.Type, "text/html") { s = HTMLToText(s) } parts = append(parts, strings.TrimRight(s, "\r\n \t")) } return strings.Join(parts, "\n\n") } // HTMLBody returns the values of the HTML body parts, concatenated. func HTMLBody(e *mail.Email) string { var b strings.Builder for _, p := range e.HTMLBody { b.WriteString(e.BodyValues[p.PartID].Value) } return b.String() } func addrList(as []mail.Address) string { s := make([]string, len(as)) for i, a := range as { s[i] = a.String() } return strings.Join(s, ", ") } // fullDate is the date shown in an expanded message header. func (p *Printer) fullDate(t time.Time) string { if !p.IsTTY { return p.Date(t) } return t.Local().Format("Mon, 2 Jan 2006 15:04") } // Message writes one expanded message: header, attachments, body. func (p *Printer) Message(w io.Writer, e *mail.Email) { hdr := func(name, val string) { if val != "" { fmt.Fprintf(w, "%s %s\n", p.Bold(name+":"), val) } } hdr("From", addrList(e.From)) hdr("To", addrList(e.To)) hdr("Cc", addrList(e.Cc)) hdr("Date", p.fullDate(e.ReceivedAt)) hdr("ID", e.ID) if e.Unread() { if p.IsTTY { fmt.Fprintln(w, p.Cyan("● unread")) } else { hdr("Flags", "unread") } } if atts := Attachments(e); len(atts) > 0 { fmt.Fprintln(w, p.Bold("Attachments:")) for _, a := range atts { name := a.Name if name == "" { name = "(unnamed)" } line := fmt.Sprintf(" %s %s %s", name, a.Type, Size(a.Size)) if a.Inline { line += " (inline)" } fmt.Fprintln(w, line) } } fmt.Fprintln(w) if body := TextBody(e); body != "" { fmt.Fprintln(w, body) } else { fmt.Fprintln(w, p.Dim("(no text body)")) } } // collapsedLine is a one-line summary of an older, read message. func (p *Printer) collapsedLine(e *mail.Email) string { from := "(unknown)" if len(e.From) > 0 { from = e.From[0].Short() } preview := strings.Join(strings.Fields(e.Preview), " ") if p.IsTTY { line := fmt.Sprintf("%s %s %s %s", from, p.Dim(p.Date(e.ReceivedAt)), p.Dim(e.ID), preview) return Truncate(line, p.Width) } return fmt.Sprintf("%s %s %s %s", from, p.Date(e.ReceivedAt), e.ID, preview) } // Thread writes a thread: a subject header, then each message. Read // messages before the last collapse to one line; unread messages and the // last message are expanded. func (p *Printer) Thread(w io.Writer, subject string, emails []mail.Email) { if subject == "" { subject = "(no subject)" } fmt.Fprintln(w, p.Bold(subject)) fmt.Fprintln(w, p.Dim(fmt.Sprintf("%d message%s", len(emails), plural(len(emails))))) rule := "----" if p.IsTTY { n := p.Width if n <= 0 || n > 72 { n = 72 } rule = p.Dim(strings.Repeat("─", n)) } for i := range emails { e := &emails[i] fmt.Fprintln(w) if i < len(emails)-1 && !e.Unread() { fmt.Fprintln(w, p.collapsedLine(e)) continue } fmt.Fprintln(w, rule) p.Message(w, e) } } func plural(n int) string { if n == 1 { return "" } return "s" }