mirror of
https://github.com/hanzoai/mail.git
synced 2026-09-23 23:53:02 +00:00
Fork of github.com/mjl-/mox at 5656ca6d1b
(upstream v0.0.15 plus 55 commits).
Rename the module path in go.mod and the import paths in 268 Go files. Also
update the five scripts that "go install" the module (genwebsite.sh,
docker-release.sh, apidiff.sh, test-upgrade.sh, Dockerfile.release), which
would otherwise build upstream rather than this fork.
Add NOTICE recording the upstream project, author, license, and fork point,
and stating which license covers which files: MIT for the mox source and our
changes, MPL-2.0 for publicsuffix/public_suffix_list.txt alone, and the
vendored dependencies' own licenses under licenses/. Embed NOTICE so
"mail licenses" prints the lineage from the running binary. LICENSE.MIT,
LICENSE.MPLv2.0 and the licenses/ tree are preserved verbatim; no code is
relicensed.
The mox command name, config keys, data directory layout and website content
are unchanged. LLM.md records the fork point, the rename, what was left alone,
and the steps to stand the server up.
Co-authored-by: Hanzo Dev <[email protected]>
266 lines
4.7 KiB
Go
266 lines
4.7 KiB
Go
package imapserver
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
|
|
"github.com/hanzoai/mail/mlog"
|
|
)
|
|
|
|
type token interface {
|
|
pack(c *conn) string
|
|
xwriteTo(c *conn, xw io.Writer) // Writes to xw panic on error.
|
|
}
|
|
|
|
type bare string
|
|
|
|
func (t bare) pack(c *conn) string {
|
|
return string(t)
|
|
}
|
|
|
|
func (t bare) xwriteTo(c *conn, xw io.Writer) {
|
|
xw.Write([]byte(t.pack(c)))
|
|
}
|
|
|
|
type niltoken struct{}
|
|
|
|
var nilt niltoken
|
|
|
|
func (t niltoken) pack(c *conn) string {
|
|
return "NIL"
|
|
}
|
|
|
|
func (t niltoken) xwriteTo(c *conn, xw io.Writer) {
|
|
xw.Write([]byte(t.pack(c)))
|
|
}
|
|
|
|
func nilOrString(s *string) token {
|
|
if s == nil {
|
|
return nilt
|
|
}
|
|
return string0(*s)
|
|
}
|
|
|
|
type string0 string
|
|
|
|
// ../rfc/9051:7081
|
|
// ../rfc/9051:6856 ../rfc/6855:153
|
|
func (t string0) pack(c *conn) string {
|
|
var r strings.Builder
|
|
r.WriteString(`"`)
|
|
for _, ch := range t {
|
|
if ch == '\x00' || ch == '\r' || ch == '\n' || ch > 0x7f && !c.utf8strings() {
|
|
return syncliteral(t).pack(c)
|
|
}
|
|
if ch == '\\' || ch == '"' {
|
|
r.WriteString(`\`)
|
|
}
|
|
r.WriteString(string(ch))
|
|
}
|
|
r.WriteString(`"`)
|
|
return r.String()
|
|
}
|
|
|
|
func (t string0) xwriteTo(c *conn, xw io.Writer) {
|
|
xw.Write([]byte(t.pack(c)))
|
|
}
|
|
|
|
type dquote string
|
|
|
|
func (t dquote) pack(c *conn) string {
|
|
var r strings.Builder
|
|
r.WriteString(`"`)
|
|
for _, c := range t {
|
|
if c == '\\' || c == '"' {
|
|
r.WriteString(`\`)
|
|
}
|
|
r.WriteString(string(c))
|
|
}
|
|
r.WriteString(`"`)
|
|
return r.String()
|
|
}
|
|
|
|
func (t dquote) xwriteTo(c *conn, xw io.Writer) {
|
|
xw.Write([]byte(t.pack(c)))
|
|
}
|
|
|
|
type syncliteral string
|
|
|
|
func (t syncliteral) pack(c *conn) string {
|
|
return fmt.Sprintf("{%d}\r\n", len(t)) + string(t)
|
|
}
|
|
|
|
func (t syncliteral) xwriteTo(c *conn, xw io.Writer) {
|
|
fmt.Fprintf(xw, "{%d}\r\n", len(t))
|
|
xw.Write([]byte(t))
|
|
}
|
|
|
|
// data from reader with known size.
|
|
type readerSizeSyncliteral struct {
|
|
r io.Reader
|
|
size int64
|
|
lit8 bool
|
|
}
|
|
|
|
func (t readerSizeSyncliteral) pack(c *conn) string {
|
|
buf, err := io.ReadAll(t.r)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
var lit string
|
|
if t.lit8 {
|
|
lit = "~"
|
|
}
|
|
return fmt.Sprintf("%s{%d}\r\n", lit, t.size) + string(buf)
|
|
}
|
|
|
|
func (t readerSizeSyncliteral) xwriteTo(c *conn, xw io.Writer) {
|
|
var lit string
|
|
if t.lit8 {
|
|
lit = "~"
|
|
}
|
|
fmt.Fprintf(xw, "%s{%d}\r\n", lit, t.size)
|
|
defer c.xtracewrite(mlog.LevelTracedata)()
|
|
if _, err := io.Copy(xw, io.LimitReader(t.r, t.size)); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// data from reader without known size.
|
|
type readerSyncliteral struct {
|
|
r io.Reader
|
|
}
|
|
|
|
func (t readerSyncliteral) pack(c *conn) string {
|
|
buf, err := io.ReadAll(t.r)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return fmt.Sprintf("{%d}\r\n", len(buf)) + string(buf)
|
|
}
|
|
|
|
func (t readerSyncliteral) xwriteTo(c *conn, xw io.Writer) {
|
|
buf, err := io.ReadAll(t.r)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Fprintf(xw, "{%d}\r\n", len(buf))
|
|
defer c.xtracewrite(mlog.LevelTracedata)()
|
|
xw.Write(buf)
|
|
}
|
|
|
|
// list with tokens space-separated
|
|
type listspace []token
|
|
|
|
func (t listspace) pack(c *conn) string {
|
|
var s strings.Builder
|
|
s.WriteString("(")
|
|
for i, e := range t {
|
|
if i > 0 {
|
|
s.WriteString(" ")
|
|
}
|
|
s.WriteString(e.pack(c))
|
|
}
|
|
s.WriteString(")")
|
|
return s.String()
|
|
}
|
|
|
|
func (t listspace) xwriteTo(c *conn, xw io.Writer) {
|
|
fmt.Fprint(xw, "(")
|
|
for i, e := range t {
|
|
if i > 0 {
|
|
fmt.Fprint(xw, " ")
|
|
}
|
|
e.xwriteTo(c, xw)
|
|
}
|
|
fmt.Fprint(xw, ")")
|
|
}
|
|
|
|
// concatenate tokens space-separated
|
|
type concatspace []token
|
|
|
|
func (t concatspace) pack(c *conn) string {
|
|
var s strings.Builder
|
|
for i, e := range t {
|
|
if i > 0 {
|
|
s.WriteString(" ")
|
|
}
|
|
s.WriteString(e.pack(c))
|
|
}
|
|
return s.String()
|
|
}
|
|
|
|
func (t concatspace) xwriteTo(c *conn, xw io.Writer) {
|
|
for i, e := range t {
|
|
if i > 0 {
|
|
fmt.Fprint(xw, " ")
|
|
}
|
|
e.xwriteTo(c, xw)
|
|
}
|
|
}
|
|
|
|
// Concatenated tokens, no spaces or list syntax.
|
|
type concat []token
|
|
|
|
func (t concat) pack(c *conn) string {
|
|
var s strings.Builder
|
|
for _, e := range t {
|
|
s.WriteString(e.pack(c))
|
|
}
|
|
return s.String()
|
|
}
|
|
|
|
func (t concat) xwriteTo(c *conn, xw io.Writer) {
|
|
for _, e := range t {
|
|
e.xwriteTo(c, xw)
|
|
}
|
|
}
|
|
|
|
type astring string
|
|
|
|
func (t astring) pack(c *conn) string {
|
|
if len(t) == 0 {
|
|
return string0(t).pack(c)
|
|
}
|
|
next:
|
|
for _, ch := range t {
|
|
for _, x := range atomChar {
|
|
if ch == x {
|
|
continue next
|
|
}
|
|
}
|
|
return string0(t).pack(c)
|
|
}
|
|
return string(t)
|
|
}
|
|
|
|
func (t astring) xwriteTo(c *conn, xw io.Writer) {
|
|
xw.Write([]byte(t.pack(c)))
|
|
}
|
|
|
|
// mailbox with utf7 encoding if connection requires it, or utf8 otherwise.
|
|
type mailboxt string
|
|
|
|
func (t mailboxt) pack(c *conn) string {
|
|
s := string(t)
|
|
if !c.utf8strings() {
|
|
s = utf7encode(s)
|
|
}
|
|
return astring(s).pack(c)
|
|
}
|
|
|
|
func (t mailboxt) xwriteTo(c *conn, xw io.Writer) {
|
|
xw.Write([]byte(t.pack(c)))
|
|
}
|
|
|
|
type number uint32
|
|
|
|
func (t number) pack(c *conn) string {
|
|
return fmt.Sprintf("%d", t)
|
|
}
|
|
|
|
func (t number) xwriteTo(c *conn, xw io.Writer) {
|
|
xw.Write([]byte(t.pack(c)))
|
|
}
|