aboutsummaryrefslogtreecommitdiffstats
path: root/lib/parse/header.go
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2023-03-11 13:26:58 +0900
committerRobin Jarry <robin@jarry.cc>2023-03-26 21:02:18 +0200
commit94a763920f5ee8b54b8ba7f819be373d80a4805d (patch)
treeccff19f3bb7c35dc02af49f04fe6adfce251498f /lib/parse/header.go
parentc09b17a930cc58c13dd5dd2ba8e7a261833eb05a (diff)
downloadaerc-94a763920f5ee8b54b8ba7f819be373d80a4805d.tar.gz
parse msg-id lists more liberally
Some user agents deliberately generate non-standard message identifier lists in In-Reply-To and References headers. Instead of failing silently, aerc now falls back to a desperate parser scavaging whatever looking like <id-left@id-right>. As the more liberal parser being substituted with, References header are now stored for IMAP not only when there's a parsing error. Fixes: 31d2f5be3cec ("message-info: add explicit References field") Signed-off-by: Nguyễn Gia Phong <mcsinyx@disroot.org> Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'lib/parse/header.go')
-rw-r--r--lib/parse/header.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/lib/parse/header.go b/lib/parse/header.go
new file mode 100644
index 00000000..361b3af0
--- /dev/null
+++ b/lib/parse/header.go
@@ -0,0 +1,42 @@
+package parse
+
+import (
+ "strings"
+
+ "git.sr.ht/~rjarry/aerc/log"
+ "github.com/emersion/go-message/mail"
+)
+
+// MsgIDList parses a list of message identifiers. It returns message
+// identifiers without angle brackets. If the header field is missing,
+// it returns nil.
+//
+// This can be used on In-Reply-To and References header fields.
+// If the field does not conform to RFC 5322, fall back
+// to greedily parsing a subsequence of the original field.
+func MsgIDList(h *mail.Header, key string) []string {
+ l, err := h.MsgIDList(key)
+ if err == nil {
+ return l
+ }
+ log.Errorf("%s: %s", err, h.Get(key))
+
+ // Expensive, fix your peer's MUA instead!
+ var list []string
+ header := &mail.Header{Header: h.Header.Copy()}
+ value := header.Get(key)
+ for err != nil && len(value) > 0 {
+ // Skip parsed IDs
+ if len(l) > 0 {
+ last := "<" + l[len(l)-1] + ">"
+ value = value[strings.Index(value, last)+len(last):]
+ list = append(list, l...)
+ }
+
+ // Skip a character until some IDs can be parsed
+ value = value[1:]
+ header.Set(key, value)
+ l, err = header.MsgIDList(key)
+ }
+ return append(list, l...)
+}