aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Ser <contact@emersion.fr>2019-12-03 11:36:53 +0100
committerSimon Ser <contact@emersion.fr>2019-12-03 11:36:53 +0100
commited50cef3cf806c09762e75cf10fb022ce19d3a1b (patch)
tree6ad9b054c883055b25c123c4c9d4db0d546b4370
parent4cbe2fde9dad377dfedbf57b5e77df78e4e5b417 (diff)
downloadalps-ed50cef3cf806c09762e75cf10fb022ce19d3a1b.tar.gz
alps-ed50cef3cf806c09762e75cf10fb022ce19d3a1b.zip
Linkify message part tree
-rw-r--r--imap.go52
-rw-r--r--public/message.html8
2 files changed, 52 insertions, 8 deletions
diff --git a/imap.go b/imap.go
index 93eede3..18220b5 100644
--- a/imap.go
+++ b/imap.go
@@ -137,6 +137,52 @@ func (msg *imapMessage) TextPartName() string {
return strings.Join(l, ".")
}
+type IMAPPartNode struct {
+ Path []int
+ MIMEType string
+ Children []IMAPPartNode
+}
+
+func (node *IMAPPartNode) PathString() string {
+ l := make([]string, len(node.Path))
+ for i, partNum := range node.Path {
+ l[i] = strconv.Itoa(partNum)
+ }
+
+ return strings.Join(l, ".")
+}
+
+func imapPartTree(bs *imap.BodyStructure, path []int) *IMAPPartNode {
+ if !strings.EqualFold(bs.MIMEType, "multipart") && len(path) == 0 {
+ path = []int{1}
+ }
+
+ node := &IMAPPartNode{
+ Path: path,
+ MIMEType: strings.ToLower(bs.MIMEType + "/" + bs.MIMESubType),
+ Children: make([]IMAPPartNode, len(bs.Parts)),
+ }
+
+ for i, part := range bs.Parts {
+ num := i + 1
+
+ partPath := append([]int(nil), path...)
+ partPath = append(partPath, num)
+
+ node.Children[i] = *imapPartTree(part, partPath)
+ }
+
+ return node
+}
+
+func (msg *imapMessage) PartTree() *IMAPPartNode {
+ if msg.BodyStructure == nil {
+ return nil
+ }
+
+ return imapPartTree(msg.BodyStructure, nil)
+}
+
func listMessages(conn *imapclient.Client, mboxName string) ([]imapMessage, error) {
if err := ensureMailboxSelected(conn, mboxName); err != nil {
return nil, err
@@ -179,9 +225,7 @@ func listMessages(conn *imapclient.Client, mboxName string) ([]imapMessage, erro
return msgs, nil
}
-var _ = message.Read
-
-func getMessage(conn *imapclient.Client, mboxName string, uid uint32, partPath []int) (*imap.Message, string, error) {
+func getMessage(conn *imapclient.Client, mboxName string, uid uint32, partPath []int) (*imapMessage, string, error) {
if err := ensureMailboxSelected(conn, mboxName); err != nil {
return nil, "", err
}
@@ -232,5 +276,5 @@ func getMessage(conn *imapclient.Client, mboxName string, uid uint32, partPath [
return nil, "", err
}
- return msg, string(b), nil
+ return &imapMessage{msg}, string(b), nil
}
diff --git a/public/message.html b/public/message.html
index 047e78f..18646f0 100644
--- a/public/message.html
+++ b/public/message.html
@@ -7,17 +7,17 @@
<h2>{{.Message.Envelope.Subject}}</h2>
{{define "message-part"}}
- {{.MIMEType}}/{{.MIMESubType}}
- {{if gt (len .Parts) 0}}
+ <a href="?part={{.PathString}}">{{.MIMEType}}</a>
+ {{if gt (len .Children) 0}}
<ul>
- {{range .Parts}}
+ {{range .Children}}
<li>{{template "message-part" .}}</li>
{{end}}
</ul>
{{end}}
{{end}}
-{{template "message-part" .Message.BodyStructure}}
+{{template "message-part" .Message.PartTree}}
{{if .Body}}
<pre>{{.Body}}</pre>