mox/message/parseheaderfields.go

79 lines
2 KiB
Go
Raw Permalink Normal View History

implement message threading in backend and webmail 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.
2023-09-13 09:51:50 +03:00
package message
import (
"bytes"
"fmt"
"net/mail"
"net/textproto"
)
// ParseHeaderFields parses only the header fields in "fields" from the complete
// header buffer "header". It uses "scratch" as temporary space, which can be
// reused across calls, potentially saving lots of unneeded allocations when only a
// few headers are needed and/or many messages are parsed.
implement message threading in backend and webmail 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.
2023-09-13 09:51:50 +03:00
func ParseHeaderFields(header []byte, scratch []byte, fields [][]byte) (textproto.MIMEHeader, error) {
// todo: should not use mail.ReadMessage, it allocates a bufio.Reader. should implement header parsing ourselves.
// Gather the raw lines for the fields, with continuations, without the other
// headers. Put them in a byte slice and only parse those headers. For now, use
// mail.ReadMessage without letting it do allocations for all headers.
scratch = scratch[:0]
var keepcontinuation bool
for len(header) > 0 {
if header[0] == ' ' || header[0] == '\t' {
// Continuation.
i := bytes.IndexByte(header, '\n')
if i < 0 {
i = len(header)
} else {
i++
}
if keepcontinuation {
scratch = append(scratch, header[:i]...)
}
header = header[i:]
continue
}
i := bytes.IndexByte(header, ':')
if i < 0 || i > 0 && (header[i-1] == ' ' || header[i-1] == '\t') {
i = bytes.IndexByte(header, '\n')
if i < 0 {
break
}
header = header[i+1:]
keepcontinuation = false
continue
}
k := header[:i]
keepcontinuation = false
for _, f := range fields {
if bytes.EqualFold(k, f) {
keepcontinuation = true
break
}
}
i = bytes.IndexByte(header, '\n')
if i < 0 {
i = len(header)
} else {
i++
}
if keepcontinuation {
scratch = append(scratch, header[:i]...)
}
header = header[i:]
}
if len(scratch) == 0 {
return nil, nil
}
scratch = append(scratch, "\r\n"...)
msg, err := mail.ReadMessage(bytes.NewReader(scratch))
if err != nil {
return nil, fmt.Errorf("reading message header")
}
return textproto.MIMEHeader(msg.Header), nil
}