mirror of
https://github.com/mjl-/mox.git
synced 2025-07-12 17:04:39 +03:00
add webmail
it was far down on the roadmap, but implemented earlier, because it's interesting, and to help prepare for a jmap implementation. for jmap we need to implement more client-like functionality than with just imap. internal data structures need to change. jmap has lots of other requirements, so it's already a big project. by implementing a webmail now, some of the required data structure changes become clear and can be made now, so the later jmap implementation can do things similarly to the webmail code. the webmail frontend and webmail are written together, making their interface/api much smaller and simpler than jmap. one of the internal changes is that we now keep track of per-mailbox total/unread/unseen/deleted message counts and mailbox sizes. keeping this data consistent after any change to the stored messages (through the code base) is tricky, so mox now has a consistency check that verifies the counts are correct, which runs only during tests, each time an internal account reference is closed. we have a few more internal "changes" that are propagated for the webmail frontend (that imap doesn't have a way to propagate on a connection), like changes to the special-use flags on mailboxes, and used keywords in a mailbox. more changes that will be required have revealed themselves while implementing the webmail, and will be implemented next. the webmail user interface is modeled after the mail clients i use or have used: thunderbird, macos mail, mutt; and webmails i normally only use for testing: gmail, proton, yahoo, outlook. a somewhat technical user is assumed, but still the goal is to make this webmail client easy to use for everyone. the user interface looks like most other mail clients: a list of mailboxes, a search bar, a message list view, and message details. there is a top/bottom and a left/right layout for the list/message view, default is automatic based on screen size. the panes can be resized by the user. buttons for actions are just text, not icons. clicking a button briefly shows the shortcut for the action in the bottom right, helping with learning to operate quickly. any text that is underdotted has a title attribute that causes more information to be displayed, e.g. what a button does or a field is about. to highlight potential phishing attempts, any text (anywhere in the webclient) that switches unicode "blocks" (a rough approximation to (language) scripts) within a word is underlined orange. multiple messages can be selected with familiar ui interaction: clicking while holding control and/or shift keys. keyboard navigation works with arrows/page up/down and home/end keys, and also with a few basic vi-like keys for list/message navigation. we prefer showing the text instead of html (with inlined images only) version of a message. html messages are shown in an iframe served from an endpoint with CSP headers to prevent dangerous resources (scripts, external images) from being loaded. the html is also sanitized, with javascript removed. a user can choose to load external resources (e.g. images for tracking purposes). the frontend is just (strict) typescript, no external frameworks. all incoming/outgoing data is typechecked, both the api request parameters and response types, and the data coming in over SSE. the types and checking code are generated with sherpats, which uses the api definitions generated by sherpadoc based on the Go code. so types from the backend are automatically propagated to the frontend. since there is no framework to automatically propagate properties and rerender components, changes coming in over the SSE connection are propagated explicitly with regular function calls. the ui is separated into "views", each with a "root" dom element that is added to the visible document. these views have additional functions for getting changes propagated, often resulting in the view updating its (internal) ui state (dom). we keep the frontend compilation simple, it's just a few typescript files that get compiled (combined and types stripped) into a single js file, no additional runtime code needed or complicated build processes used. the webmail is served is served from a compressed, cachable html file that includes style and the javascript, currently just over 225kb uncompressed, under 60kb compressed (not minified, including comments). we include the generated js files in the repository, to keep Go's easily buildable self-contained binaries. authentication is basic http, as with the account and admin pages. most data comes in over one long-term SSE connection to the backend. api requests signal which mailbox/search/messages are requested over the SSE connection. fetching individual messages, and making changes, are done through api calls. the operations are similar to imap, so some code has been moved from package imapserver to package store. the future jmap implementation will benefit from these changes too. more functionality will probably be moved to the store package in the future. the quickstart enables webmail on the internal listener by default (for new installs). users can enable it on the public listener if they want to. mox localserve enables it too. to enable webmail on existing installs, add settings like the following to the listeners in mox.conf, similar to AccountHTTP(S): WebmailHTTP: Enabled: true WebmailHTTPS: Enabled: true special thanks to liesbeth, gerben, andrii for early user feedback. there is plenty still to do, see the list at the top of webmail/webmail.ts. feedback welcome as always.
This commit is contained in:
119
message/authresults.go
Normal file
119
message/authresults.go
Normal file
@ -0,0 +1,119 @@
|
||||
package message
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// ../rfc/8601:577
|
||||
|
||||
// Authentication-Results header, see RFC 8601.
|
||||
type AuthResults struct {
|
||||
Hostname string
|
||||
Comment string // If not empty, header comment without "()", added after Hostname.
|
||||
Methods []AuthMethod
|
||||
}
|
||||
|
||||
// ../rfc/8601:598
|
||||
|
||||
// AuthMethod is a result for one authentication method.
|
||||
//
|
||||
// Example encoding in the header: "spf=pass smtp.mailfrom=example.net".
|
||||
type AuthMethod struct {
|
||||
// E.g. "dkim", "spf", "iprev", "auth".
|
||||
Method string
|
||||
Result string // Each method has a set of known values, e.g. "pass", "temperror", etc.
|
||||
Comment string // Optional, message header comment.
|
||||
Reason string // Optional.
|
||||
Props []AuthProp
|
||||
}
|
||||
|
||||
// ../rfc/8601:606
|
||||
|
||||
// AuthProp describes properties for an authentication method.
|
||||
// Each method has a set of known properties.
|
||||
// Encoded in the header as "type.property=value", e.g. "smtp.mailfrom=example.net"
|
||||
// for spf.
|
||||
type AuthProp struct {
|
||||
// Valid values maintained at https://www.iana.org/assignments/email-auth/email-auth.xhtml
|
||||
Type string
|
||||
Property string
|
||||
Value string
|
||||
// Whether value is address-like (localpart@domain, or domain). Or another value,
|
||||
// which is subject to escaping.
|
||||
IsAddrLike bool
|
||||
Comment string // If not empty, header comment withtout "()", added after Value.
|
||||
}
|
||||
|
||||
// MakeAuthProp is a convenient way to make an AuthProp.
|
||||
func MakeAuthProp(typ, property, value string, isAddrLike bool, Comment string) AuthProp {
|
||||
return AuthProp{typ, property, value, isAddrLike, Comment}
|
||||
}
|
||||
|
||||
// todo future: we could store fields as dns.Domain, and when we encode as non-ascii also add the ascii version as a comment.
|
||||
|
||||
// Header returns an Authentication-Results header, possibly spanning multiple
|
||||
// lines, always ending in crlf.
|
||||
func (h AuthResults) Header() string {
|
||||
// Escaping of values: ../rfc/8601:684 ../rfc/2045:661
|
||||
|
||||
optComment := func(s string) string {
|
||||
if s != "" {
|
||||
return " (" + s + ")"
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
w := &HeaderWriter{}
|
||||
w.Add("", "Authentication-Results:"+optComment(h.Comment)+" "+value(h.Hostname)+";")
|
||||
for i, m := range h.Methods {
|
||||
tokens := []string{}
|
||||
addf := func(format string, args ...any) {
|
||||
s := fmt.Sprintf(format, args...)
|
||||
tokens = append(tokens, s)
|
||||
}
|
||||
addf("%s=%s", m.Method, m.Result)
|
||||
if m.Comment != "" && (m.Reason != "" || len(m.Props) > 0) {
|
||||
addf("(%s)", m.Comment)
|
||||
}
|
||||
if m.Reason != "" {
|
||||
addf("reason=%s", value(m.Reason))
|
||||
}
|
||||
for _, p := range m.Props {
|
||||
v := p.Value
|
||||
if !p.IsAddrLike {
|
||||
v = value(v)
|
||||
}
|
||||
addf("%s.%s=%s%s", p.Type, p.Property, v, optComment(p.Comment))
|
||||
}
|
||||
for j, t := range tokens {
|
||||
if j == len(tokens)-1 && i < len(h.Methods)-1 {
|
||||
t += ";"
|
||||
}
|
||||
w.Add(" ", t)
|
||||
}
|
||||
}
|
||||
return w.String()
|
||||
}
|
||||
|
||||
func value(s string) string {
|
||||
quote := s == ""
|
||||
for _, c := range s {
|
||||
// utf-8 does not have to be quoted. ../rfc/6532:242
|
||||
if c == '"' || c == '\\' || c <= ' ' || c == 0x7f {
|
||||
quote = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !quote {
|
||||
return s
|
||||
}
|
||||
r := `"`
|
||||
for _, c := range s {
|
||||
if c == '"' || c == '\\' {
|
||||
r += "\\"
|
||||
}
|
||||
r += string(c)
|
||||
}
|
||||
r += `"`
|
||||
return r
|
||||
}
|
26
message/authresults_test.go
Normal file
26
message/authresults_test.go
Normal file
@ -0,0 +1,26 @@
|
||||
package message
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mjl-/mox/dns"
|
||||
)
|
||||
|
||||
func TestAuthResults(t *testing.T) {
|
||||
dom, err := dns.ParseDomain("møx.example")
|
||||
if err != nil {
|
||||
t.Fatalf("parsing domain: %v", err)
|
||||
}
|
||||
authRes := AuthResults{
|
||||
Hostname: dom.XName(true),
|
||||
Comment: dom.ASCIIExtra(true),
|
||||
Methods: []AuthMethod{
|
||||
{"dkim", "pass", "", "", []AuthProp{{"header", "d", dom.XName(true), true, dom.ASCIIExtra(true)}}},
|
||||
},
|
||||
}
|
||||
s := authRes.Header()
|
||||
const exp = "Authentication-Results: (xn--mx-lka.example) møx.example; dkim=pass\r\n\theader.d=møx.example (xn--mx-lka.example)\r\n"
|
||||
if s != exp {
|
||||
t.Fatalf("got %q, expected %q", s, exp)
|
||||
}
|
||||
}
|
21
message/hdrcmtdomain.go
Normal file
21
message/hdrcmtdomain.go
Normal file
@ -0,0 +1,21 @@
|
||||
package message
|
||||
|
||||
import (
|
||||
"github.com/mjl-/mox/dns"
|
||||
)
|
||||
|
||||
// HeaderCommentDomain returns domain name optionally followed by a message
|
||||
// header comment with ascii-only name.
|
||||
//
|
||||
// The comment is only present when smtputf8 is true and the domain name is unicode.
|
||||
//
|
||||
// Caller should make sure the comment is allowed in the syntax. E.g. for Received,
|
||||
// it is often allowed before the next field, so make sure such a next field is
|
||||
// present.
|
||||
func HeaderCommentDomain(domain dns.Domain, smtputf8 bool) string {
|
||||
s := domain.XName(smtputf8)
|
||||
if smtputf8 && domain.Unicode != "" {
|
||||
s += " (" + domain.ASCII + ")"
|
||||
}
|
||||
return s
|
||||
}
|
@ -85,6 +85,10 @@ type Part struct {
|
||||
bound []byte // Only set if valid multipart with boundary, includes leading --, excludes \r\n.
|
||||
}
|
||||
|
||||
// todo: have all Content* fields in Part?
|
||||
// todo: make Address contain a type Localpart and dns.Domain?
|
||||
// todo: if we ever make a major change and reparse all parts, switch to lower-case values if not too troublesome.
|
||||
|
||||
// Envelope holds the basic/common message headers as used in IMAP4.
|
||||
type Envelope struct {
|
||||
Date time.Time
|
||||
|
17
message/qp.go
Normal file
17
message/qp.go
Normal file
@ -0,0 +1,17 @@
|
||||
package message
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// NeedsQuotedPrintable returns whether text should be encoded with
|
||||
// quoted-printable. If not, it can be included as 7bit or 8bit encoding.
|
||||
func NeedsQuotedPrintable(text string) bool {
|
||||
// ../rfc/2045:1025
|
||||
for _, line := range strings.Split(text, "\r\n") {
|
||||
if len(line) > 78 || strings.Contains(line, "\r") || strings.Contains(line, "\n") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
46
message/tlsrecv.go
Normal file
46
message/tlsrecv.go
Normal file
@ -0,0 +1,46 @@
|
||||
package message
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
|
||||
"github.com/mjl-/mox/mlog"
|
||||
)
|
||||
|
||||
// TLSReceivedComment returns a comment about TLS of the connection for use in a Receive header.
|
||||
func TLSReceivedComment(log *mlog.Log, cs tls.ConnectionState) []string {
|
||||
// todo future: we could use the "tls" clause for the Received header as specified in ../rfc/8314:496. however, the text implies it is only for submission, not regular smtp. and it cannot specify the tls version. for now, not worth the trouble.
|
||||
|
||||
// Comments from other mail servers:
|
||||
// gmail.com: (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128)
|
||||
// yahoo.com: (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256)
|
||||
// proton.me: (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested)
|
||||
// outlook.com: (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384)
|
||||
|
||||
var l []string
|
||||
add := func(s string) {
|
||||
l = append(l, s)
|
||||
}
|
||||
|
||||
versions := map[uint16]string{
|
||||
tls.VersionTLS10: "TLS1.0",
|
||||
tls.VersionTLS11: "TLS1.1",
|
||||
tls.VersionTLS12: "TLS1.2",
|
||||
tls.VersionTLS13: "TLS1.3",
|
||||
}
|
||||
|
||||
if version, ok := versions[cs.Version]; ok {
|
||||
add(version)
|
||||
} else {
|
||||
log.Info("unknown tls version identifier", mlog.Field("version", cs.Version))
|
||||
add(fmt.Sprintf("TLS identifier %x", cs.Version))
|
||||
}
|
||||
|
||||
add(tls.CipherSuiteName(cs.CipherSuite))
|
||||
|
||||
// Make it a comment.
|
||||
l[0] = "(" + l[0]
|
||||
l[len(l)-1] = l[len(l)-1] + ")"
|
||||
|
||||
return l
|
||||
}
|
Reference in New Issue
Block a user