summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorKoni Marti <koni.marti@gmail.com>2022-06-28 23:42:08 +0200
committerRobin Jarry <robin@jarry.cc>2022-07-02 17:53:06 +0200
commit60052c607011ab09fe204cf5adc0cc9e29b34cdd (patch)
treeda5bfd63f8aa074e4ab56353ddf32daef1c6a495 /lib
parent9d90b70b4edfd2af81ac686e16f8bfd9b6ebfa9c (diff)
downloadaerc-60052c607011ab09fe204cf5adc0cc9e29b34cdd.zip
forward: provide option to append all attachments
Append all non-multipart attachments with the -A flag. Rename the flag for forwarding a full message as an RFC2822 attachments to -F. Suggested-by: psykose Signed-off-by: Koni Marti <koni.marti@gmail.com> Tested-by: Tim Culverhouse <tim@timculverhouse.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/structure_helpers.go27
-rw-r--r--lib/structure_helpers_test.go46
2 files changed, 73 insertions, 0 deletions
diff --git a/lib/structure_helpers.go b/lib/structure_helpers.go
index ac6950a..1b4a6e6 100644
--- a/lib/structure_helpers.go
+++ b/lib/structure_helpers.go
@@ -52,3 +52,30 @@ func FindFirstNonMultipart(bs *models.BodyStructure, path []int) []int {
}
return nil
}
+
+func FindAllNonMultipart(bs *models.BodyStructure, path []int, pathlist [][]int) [][]int {
+ for i, part := range bs.Parts {
+ cur := append(path, i+1)
+ mimetype := strings.ToLower(part.MIMEType)
+ if mimetype != "multipart" {
+ pathlist = append(pathlist, cur)
+ } 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
+}
diff --git a/lib/structure_helpers_test.go b/lib/structure_helpers_test.go
new file mode 100644
index 0000000..f670735
--- /dev/null
+++ b/lib/structure_helpers_test.go
@@ -0,0 +1,46 @@
+package lib_test
+
+import (
+ "testing"
+
+ "git.sr.ht/~rjarry/aerc/lib"
+ "git.sr.ht/~rjarry/aerc/models"
+)
+
+func TestLib_FindAllNonMultipart(t *testing.T) {
+
+ testStructure := &models.BodyStructure{
+ MIMEType: "multipart",
+ Parts: []*models.BodyStructure{
+ &models.BodyStructure{},
+ &models.BodyStructure{
+ MIMEType: "multipart",
+ Parts: []*models.BodyStructure{
+ &models.BodyStructure{},
+ &models.BodyStructure{},
+ },
+ },
+ &models.BodyStructure{},
+ },
+ }
+
+ expected := [][]int{
+ []int{1},
+ []int{2, 1},
+ []int{2, 2},
+ []int{3},
+ }
+
+ parts := lib.FindAllNonMultipart(testStructure, nil, nil)
+
+ if len(expected) != len(parts) {
+ t.Errorf("incorrect dimensions; expected: %v, got: %v", expected, parts)
+ }
+
+ for i := 0; i < len(parts); i++ {
+ if !lib.EqualParts(expected[i], parts[i]) {
+ t.Errorf("incorrect values; expected: %v, got: %v", expected[i], parts[i])
+ }
+ }
+
+}