From de3255182263fb8203e761f483a17533c6a38a05 Mon Sep 17 00:00:00 2001 From: Sebastien Binet Date: Thu, 14 Dec 2023 16:20:36 +0100 Subject: lib: introduce FindMIMEPart, adapt Find{Plain,Calendar}text introduce a general FindMIMEPart function to find a message part with the given MIME type. reimplement FindPlaintext and FindCalendartext in terms of this new FindMIMEPart function. Signed-off-by: Sebastien Binet Acked-by: Robin Jarry --- lib/structure_helpers.go | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) (limited to 'lib') diff --git a/lib/structure_helpers.go b/lib/structure_helpers.go index 0189dc8a..7b5865a2 100644 --- a/lib/structure_helpers.go +++ b/lib/structure_helpers.go @@ -6,14 +6,16 @@ import ( "git.sr.ht/~rjarry/aerc/models" ) -func FindPlaintext(bs *models.BodyStructure, path []int) []int { +// FindMIMEPart finds the first message part with the provided MIME type. +// FindMIMEPart recurses inside multipart containers. +func FindMIMEPart(mime string, bs *models.BodyStructure, path []int) []int { for i, part := range bs.Parts { cur := append(path, i+1) //nolint:gocritic // intentional append to different slice - if part.FullMIMEType() == "text/plain" { + if part.FullMIMEType() == mime { return cur } if strings.ToLower(part.MIMEType) == "multipart" { - if path := FindPlaintext(part, cur); path != nil { + if path := FindMIMEPart(mime, part, cur); path != nil { return path } } @@ -21,19 +23,12 @@ func FindPlaintext(bs *models.BodyStructure, path []int) []int { return nil } +func FindPlaintext(bs *models.BodyStructure, path []int) []int { + return FindMIMEPart("text/plain", bs, path) +} + func FindCalendartext(bs *models.BodyStructure, path []int) []int { - for i, part := range bs.Parts { - cur := append(path, i+1) //nolint:gocritic // intentional append to different slice - if part.FullMIMEType() == "text/calendar" { - return cur - } - if strings.ToLower(part.MIMEType) == "multipart" { - if path := FindCalendartext(part, cur); path != nil { - return path - } - } - } - return nil + return FindMIMEPart("text/calendar", bs, path) } func FindFirstNonMultipart(bs *models.BodyStructure, path []int) []int { -- cgit