| 1 |
package lib |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
|
| 6 |
"git.sr.ht/~rjarry/aerc/models" |
| 7 |
) |
| 8 |
|
| 9 |
// FindMIMEPart finds the first message part with the provided MIME type. |
| 10 |
// FindMIMEPart recurses inside multipart containers. |
| 11 |
func FindMIMEPart(mime string, bs *models.BodyStructure, path []int) []int { |
| 12 |
for i, part := range bs.Parts { |
| 13 |
cur := append(path, i+1) //nolint:gocritic // intentional append to different slice |
| 14 |
if part.FullMIMEType() == mime { |
| 15 |
return cur |
| 16 |
} |
| 17 |
if strings.ToLower(part.MIMEType) == "multipart" { |
| 18 |
if path := FindMIMEPart(mime, part, cur); path != nil { |
| 19 |
return path |
| 20 |
} |
| 21 |
} |
| 22 |
} |
| 23 |
return nil |
| 24 |
} |
| 25 |
|
| 26 |
func FindPlaintext(bs *models.BodyStructure, path []int) []int { |
| 27 |
return FindMIMEPart("text/plain", bs, path) |
| 28 |
} |
| 29 |
|
| 30 |
func FindCalendartext(bs *models.BodyStructure, path []int) []int { |
| 31 |
return FindMIMEPart("text/calendar", bs, path) |
| 32 |
} |
| 33 |
|
| 34 |
func FindFirstNonMultipart(bs *models.BodyStructure, path []int) []int { |
| 35 |
for i, part := range bs.Parts { |
| 36 |
cur := append(path, i+1) //nolint:gocritic // intentional append to different slice |
| 37 |
mimetype := strings.ToLower(part.MIMEType) |
| 38 |
if mimetype != "multipart" { |
| 39 |
return cur |
| 40 |
} else if mimetype == "multipart" { |
| 41 |
if path := FindFirstNonMultipart(part, cur); path != nil { |
| 42 |
return path |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
return nil |
| 47 |
} |
| 48 |
|
| 49 |
func FindAllNonMultipart(bs *models.BodyStructure, path []int, pathlist [][]int) [][]int { |
| 50 |
for i, part := range bs.Parts { |
| 51 |
cur := append(path, i+1) //nolint:gocritic // intentional append to different slice |
| 52 |
mimetype := strings.ToLower(part.MIMEType) |
| 53 |
if mimetype != "multipart" { |
| 54 |
tmp := make([]int, len(cur)) |
| 55 |
copy(tmp, cur) |
| 56 |
pathlist = append(pathlist, tmp) |
| 57 |
} else if mimetype == "multipart" { |
| 58 |
if sub := FindAllNonMultipart(part, cur, nil); len(sub) > 0 { |
| 59 |
pathlist = append(pathlist, sub...) |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
return pathlist |
| 64 |
} |
| 65 |
|
| 66 |
func EqualParts(a []int, b []int) bool { |
| 67 |
if len(a) != len(b) { |
| 68 |
return false |
| 69 |
} |
| 70 |
for i := range a { |
| 71 |
if a[i] != b[i] { |
| 72 |
return false |
| 73 |
} |
| 74 |
} |
| 75 |
return true |
| 76 |
} |