aboutsummaryrefslogtreecommitdiffstats
path: root/worker/lib/headers.go
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2023-10-17 14:40:08 +0200
committerRobin Jarry <robin@jarry.cc>2023-10-28 19:24:55 +0200
commit57088312fdd8e602a084bd5736a0e22a34be9ec0 (patch)
tree8c5544262cf8c1772ec661748cfa4d5491ff4c77 /worker/lib/headers.go
parent591659b52867cb118d1f82d41693a02123935e0c (diff)
downloadaerc-57088312fdd8e602a084bd5736a0e22a34be9ec0.tar.gz
worker: move shared code to lib
Avoid importing code from worker/lib into lib. It should only be the other way around. Move the message parsing code used by maildir, notmuch, mbox and the eml viewer into a lib/rfc822 package. Adapt imports accordingly. Signed-off-by: Robin Jarry <robin@jarry.cc> Reviewed-by: Koni Marti <koni.marti@gmail.com> Tested-by: Moritz Poldrack <moritz@poldrack.dev> Tested-by: Inwit <inwit@sindominio.net>
Diffstat (limited to 'worker/lib/headers.go')
-rw-r--r--worker/lib/headers.go29
1 files changed, 29 insertions, 0 deletions
diff --git a/worker/lib/headers.go b/worker/lib/headers.go
new file mode 100644
index 00000000..391d1d2a
--- /dev/null
+++ b/worker/lib/headers.go
@@ -0,0 +1,29 @@
+package lib
+
+import (
+ "strings"
+
+ "github.com/emersion/go-message/mail"
+)
+
+// LimitHeaders returns a new Header with the specified headers included or
+// excluded
+func LimitHeaders(hdr *mail.Header, fields []string, exclude bool) *mail.Header {
+ fieldMap := make(map[string]struct{}, len(fields))
+ for _, f := range fields {
+ fieldMap[strings.ToLower(f)] = struct{}{}
+ }
+ nh := &mail.Header{}
+ curFields := hdr.Fields()
+ for curFields.Next() {
+ key := strings.ToLower(curFields.Key())
+ _, present := fieldMap[key]
+ // XOR exclude and present. When they are equal, it means we
+ // should not add the header to the new header struct
+ if exclude == present {
+ continue
+ }
+ nh.Add(key, curFields.Value())
+ }
+ return nh
+}