diff options
author | Simon Ser <contact@emersion.fr> | 2020-01-28 11:15:10 +0100 |
---|---|---|
committer | Simon Ser <contact@emersion.fr> | 2020-01-28 11:15:10 +0100 |
commit | 50046b62ac61a985f82bfc22b4e7b39b334d030c (patch) | |
tree | 89647c65c0971b164e56b608a23d2c2301a336cb /plugins | |
parent | b325933a8bc68fa99bc79982938f6936e9a6de8e (diff) | |
download | alps-50046b62ac61a985f82bfc22b4e7b39b334d030c.tar.gz alps-50046b62ac61a985f82bfc22b4e7b39b334d030c.zip |
plugins/base: use BodyStructure.Walk instead of custom logic
Diffstat (limited to 'plugins')
-rwxr-xr-x | plugins/base/imap.go | 66 |
1 files changed, 20 insertions, 46 deletions
diff --git a/plugins/base/imap.go b/plugins/base/imap.go index 280a06b..1b41265 100755 --- a/plugins/base/imap.go +++ b/plugins/base/imap.go @@ -107,66 +107,40 @@ type IMAPMessage struct { *imap.Message } -func textPartPath(bs *imap.BodyStructure) ([]int, bool) { - if bs.Disposition != "" && !strings.EqualFold(bs.Disposition, "inline") { - return nil, false - } - - if strings.EqualFold(bs.MIMEType, "text") { - return []int{1}, true - } - - if !strings.EqualFold(bs.MIMEType, "multipart") { - return nil, false +func (msg *IMAPMessage) TextPartName() string { + if msg.BodyStructure == nil { + return "" } - textPartNum := -1 - for i, part := range bs.Parts { - num := i + 1 - - if strings.EqualFold(part.MIMEType, "multipart") { - if subpath, ok := textPartPath(part); ok { - return append([]int{num}, subpath...), true - } - } + var best []int + isTextPlain := false + msg.BodyStructure.Walk(func(path []int, part *imap.BodyStructure) bool { if !strings.EqualFold(part.MIMEType, "text") { - continue + return true + } + if part.Disposition != "" && !strings.EqualFold(part.Disposition, "inline") { + return true } - var pick bool switch strings.ToLower(part.MIMESubType) { case "plain": - pick = true + isTextPlain = true + best = path case "html": - pick = textPartNum < 0 - } - - if pick { - textPartNum = num + if !isTextPlain { + best = path + } } - } - - if textPartNum > 0 { - return []int{textPartNum}, true - } - return nil, false -} - -func (msg *IMAPMessage) TextPartName() string { - if msg.BodyStructure == nil { - return "" - } - - path, ok := textPartPath(msg.BodyStructure) - if !ok { + return true + }) + if best == nil { return "" } - l := make([]string, len(path)) - for i, partNum := range path { + l := make([]string, len(best)) + for i, partNum := range best { l[i] = strconv.Itoa(partNum) } - return strings.Join(l, ".") } |