2020-06-19 15:58:08 +00:00
|
|
|
package lib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2021-11-05 09:19:46 +00:00
|
|
|
"git.sr.ht/~rjarry/aerc/models"
|
2020-06-19 15:58:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func FindPlaintext(bs *models.BodyStructure, path []int) []int {
|
|
|
|
for i, part := range bs.Parts {
|
2022-07-31 12:32:48 +00:00
|
|
|
cur := append(path, i+1) //nolint:gocritic // intentional append to different slice
|
2020-06-19 15:58:08 +00:00
|
|
|
if strings.ToLower(part.MIMEType) == "text" &&
|
|
|
|
strings.ToLower(part.MIMESubType) == "plain" {
|
|
|
|
return cur
|
|
|
|
}
|
|
|
|
if strings.ToLower(part.MIMEType) == "multipart" {
|
|
|
|
if path := FindPlaintext(part, cur); path != nil {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-24 05:36:07 +00:00
|
|
|
func FindCalendartext(bs *models.BodyStructure, path []int) []int {
|
|
|
|
for i, part := range bs.Parts {
|
2022-07-31 12:32:48 +00:00
|
|
|
cur := append(path, i+1) //nolint:gocritic // intentional append to different slice
|
2022-05-24 05:36:07 +00:00
|
|
|
if strings.ToLower(part.MIMEType) == "text" &&
|
|
|
|
strings.ToLower(part.MIMESubType) == "calendar" {
|
|
|
|
return cur
|
|
|
|
}
|
|
|
|
if strings.ToLower(part.MIMEType) == "multipart" {
|
|
|
|
if path := FindCalendartext(part, cur); path != nil {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-19 15:58:08 +00:00
|
|
|
func FindFirstNonMultipart(bs *models.BodyStructure, path []int) []int {
|
|
|
|
for i, part := range bs.Parts {
|
2022-07-31 12:32:48 +00:00
|
|
|
cur := append(path, i+1) //nolint:gocritic // intentional append to different slice
|
2020-06-19 15:58:08 +00:00
|
|
|
mimetype := strings.ToLower(part.MIMEType)
|
|
|
|
if mimetype != "multipart" {
|
2021-02-26 21:01:18 +00:00
|
|
|
return cur
|
2020-06-19 15:58:08 +00:00
|
|
|
} else if mimetype == "multipart" {
|
|
|
|
if path := FindFirstNonMultipart(part, cur); path != nil {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-06-28 21:42:08 +00:00
|
|
|
|
|
|
|
func FindAllNonMultipart(bs *models.BodyStructure, path []int, pathlist [][]int) [][]int {
|
|
|
|
for i, part := range bs.Parts {
|
2022-07-31 12:32:48 +00:00
|
|
|
cur := append(path, i+1) //nolint:gocritic // intentional append to different slice
|
2022-06-28 21:42:08 +00:00
|
|
|
mimetype := strings.ToLower(part.MIMEType)
|
|
|
|
if mimetype != "multipart" {
|
2022-07-14 19:49:54 +00:00
|
|
|
tmp := make([]int, len(cur))
|
|
|
|
copy(tmp, cur)
|
|
|
|
pathlist = append(pathlist, tmp)
|
2022-06-28 21:42:08 +00:00
|
|
|
} else if mimetype == "multipart" {
|
|
|
|
if sub := FindAllNonMultipart(part, cur, nil); len(sub) > 0 {
|
|
|
|
pathlist = append(pathlist, sub...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pathlist
|
|
|
|
}
|
|
|
|
|
|
|
|
func EqualParts(a []int, b []int) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := 0; i < len(a); i++ {
|
|
|
|
if a[i] != b[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|