mirror of
https://github.com/mjl-/mox.git
synced 2024-12-25 16:03:48 +03:00
3fb41ff073
we match messages to their parents based on the "references" and "in-reply-to" headers (requiring the same base subject), and in absense of those headers we also by only base subject (against messages received max 4 weeks ago). we store a threadid with messages. all messages in a thread have the same threadid. messages also have a "thread parent ids", which holds all id's of parent messages up to the thread root. then there is "thread missing link", which is set when a referenced immediate parent wasn't found (but possibly earlier ancestors can still be found and will be in thread parent ids". threads can be muted: newly delivered messages are automatically marked as read/seen. threads can be marked as collapsed: if set, the webmail collapses the thread to a single item in the basic threading view (default is to expand threads). the muted and collapsed fields are copied from their parent on message delivery. the threading is implemented in the webmail. the non-threading mode still works as before. the new default threading mode "unread" automatically expands only the threads with at least one unread (not seen) meessage. the basic threading mode "on" expands all threads except when explicitly collapsed (as saved in the thread collapsed field). new shortcuts for navigation/interaction threads have been added, e.g. go to previous/next thread root, toggle collapse/expand of thread (or double click), toggle mute of thread. some previous shortcuts have changed, see the help for details. the message threading are added with an explicit account upgrade step, automatically started when an account is opened. the upgrade is done in the background because it will take too long for large mailboxes to block account operations. the upgrade takes two steps: 1. updating all message records in the database to add a normalized message-id and thread base subject (with "re:", "fwd:" and several other schemes stripped). 2. going through all messages in the database again, reading the "references" and "in-reply-to" headers from disk, and matching against their parents. this second step is also done at the end of each import of mbox/maildir mailboxes. new deliveries are matched immediately against other existing messages, currently no attempt is made to rematch previously delivered messages (which could be useful for related messages being delivered out of order). the threading is not yet exposed over imap.
74 lines
2.2 KiB
Go
74 lines
2.2 KiB
Go
package message
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/mjl-/mox/smtp"
|
|
)
|
|
|
|
// ReferencedIDs returns the Message-IDs referenced from the References header(s),
|
|
// with a fallback to the In-Reply-To header(s). The ids are canonicalized for
|
|
// thread-matching, like with MessageIDCanonical. Empty message-id's are skipped.
|
|
func ReferencedIDs(references []string, inReplyTo []string) ([]string, error) {
|
|
var refids []string // In thread-canonical form.
|
|
|
|
// parse and add 0 or 1 reference, returning the remaining refs string for a next attempt.
|
|
parse1 := func(refs string, one bool) string {
|
|
refs = strings.TrimLeft(refs, " \t\r\n")
|
|
if !strings.HasPrefix(refs, "<") {
|
|
// To make progress, we skip to next space or >.
|
|
i := strings.IndexAny(refs, " >")
|
|
if i < 0 {
|
|
return ""
|
|
}
|
|
return refs[i+1:]
|
|
}
|
|
refs = refs[1:]
|
|
// Look for the ending > or next <. If < is before >, this entry is truncated.
|
|
i := strings.IndexAny(refs, "<>")
|
|
if i < 0 {
|
|
return ""
|
|
}
|
|
if refs[i] == '<' {
|
|
// Truncated entry, we ignore it.
|
|
return refs[i:]
|
|
}
|
|
ref := strings.ToLower(refs[:i])
|
|
// Some MUAs wrap References line in the middle of message-id's, and others
|
|
// recombine them. Take out bare WSP in message-id's.
|
|
ref = strings.ReplaceAll(ref, " ", "")
|
|
ref = strings.ReplaceAll(ref, "\t", "")
|
|
refs = refs[i+1:]
|
|
// Canonicalize the quotedness of the message-id.
|
|
addr, err := smtp.ParseAddress(ref)
|
|
if err == nil {
|
|
// Leave the hostname form intact.
|
|
t := strings.Split(ref, "@")
|
|
ref = addr.Localpart.String() + "@" + t[len(t)-1]
|
|
}
|
|
// log.Errorx("assigning threads: bad reference in references header, using raw value", err, mlog.Field("msgid", mid), mlog.Field("reference", ref))
|
|
if ref != "" {
|
|
refids = append(refids, ref)
|
|
}
|
|
return refs
|
|
}
|
|
|
|
// References is the modern way (for a long time already) to reference ancestors.
|
|
// The direct parent is typically at the end of the list.
|
|
for _, refs := range references {
|
|
for refs != "" {
|
|
refs = parse1(refs, false)
|
|
}
|
|
}
|
|
// We only look at the In-Reply-To header if we didn't find any References.
|
|
if len(refids) == 0 {
|
|
for _, s := range inReplyTo {
|
|
parse1(s, true)
|
|
if len(refids) > 0 {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
return refids, nil
|
|
}
|