2020-06-19 17:58:08 +02:00
|
|
|
package lib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/models"
|
2020-06-19 17:58:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func FindPlaintext(bs *models.BodyStructure, path []int) []int {
|
|
|
|
for i, part := range bs.Parts {
|
2022-07-31 14:32:48 +02:00
|
|
|
cur := append(path, i+1) //nolint:gocritic // intentional append to different slice
|
2020-06-19 17:58:08 +02: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 07:36:07 +02:00
|
|
|
func FindCalendartext(bs *models.BodyStructure, path []int) []int {
|
|
|
|
for i, part := range bs.Parts {
|
2022-07-31 14:32:48 +02:00
|
|
|
cur := append(path, i+1) //nolint:gocritic // intentional append to different slice
|
2022-05-24 07:36:07 +02: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 17:58:08 +02:00
|
|
|
func FindFirstNonMultipart(bs *models.BodyStructure, path []int) []int {
|
|
|
|
for i, part := range bs.Parts {
|
2022-07-31 14:32:48 +02:00
|
|
|
cur := append(path, i+1) //nolint:gocritic // intentional append to different slice
|
2020-06-19 17:58:08 +02:00
|
|
|
mimetype := strings.ToLower(part.MIMEType)
|
|
|
|
if mimetype != "multipart" {
|
2021-02-26 22:01:18 +01:00
|
|
|
return cur
|
2020-06-19 17:58:08 +02:00
|
|
|
} else if mimetype == "multipart" {
|
|
|
|
if path := FindFirstNonMultipart(part, cur); path != nil {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-06-28 23:42:08 +02:00
|
|
|
|
|
|
|
func FindAllNonMultipart(bs *models.BodyStructure, path []int, pathlist [][]int) [][]int {
|
|
|
|
for i, part := range bs.Parts {
|
2022-07-31 14:32:48 +02:00
|
|
|
cur := append(path, i+1) //nolint:gocritic // intentional append to different slice
|
2022-06-28 23:42:08 +02:00
|
|
|
mimetype := strings.ToLower(part.MIMEType)
|
|
|
|
if mimetype != "multipart" {
|
2022-07-14 21:49:54 +02:00
|
|
|
tmp := make([]int, len(cur))
|
|
|
|
copy(tmp, cur)
|
|
|
|
pathlist = append(pathlist, tmp)
|
2022-06-28 23:42:08 +02: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
|
|
|
|
}
|