Update vendor/

This commit is contained in:
2018-06-28 01:09:56 +01:00
parent 3e5ab5bb0a
commit 21c6e571d8
108 changed files with 121110 additions and 1144 deletions

View File

@@ -24,7 +24,7 @@ func (cmd *Noop) Handle(conn Conn) error {
ctx := conn.Context()
if ctx.Mailbox != nil {
// If a mailbox is selected, NOOP can be used to poll for server updates
if mbox, ok := ctx.Mailbox.(backend.UpdaterMailbox); ok {
if mbox, ok := ctx.Mailbox.(backend.MailboxPoller); ok {
return mbox.Poll()
}
}
@@ -38,7 +38,7 @@ type Logout struct {
func (cmd *Logout) Handle(conn Conn) error {
res := &imap.StatusResp{
Type: imap.StatusBye,
Type: imap.StatusRespBye,
Info: "Closing connection",
}

View File

@@ -29,10 +29,9 @@ func (cmd *Select) Handle(conn Conn) error {
return err
}
items := []string{
imap.MailboxFlags, imap.MailboxPermanentFlags,
imap.MailboxMessages, imap.MailboxRecent, imap.MailboxUnseen,
imap.MailboxUidNext, imap.MailboxUidValidity,
items := []imap.StatusItem{
imap.StatusMessages, imap.StatusRecent, imap.StatusUnseen,
imap.StatusUidNext, imap.StatusUidValidity,
}
status, err := mbox.Status(items)
@@ -48,12 +47,12 @@ func (cmd *Select) Handle(conn Conn) error {
return err
}
code := imap.CodeReadWrite
var code imap.StatusRespCode = imap.CodeReadWrite
if ctx.MailboxReadOnly {
code = imap.CodeReadOnly
}
return ErrStatusResp(&imap.StatusResp{
Type: imap.StatusOk,
Type: imap.StatusRespOk,
Code: code,
})
}
@@ -208,7 +207,7 @@ func (cmd *Status) Handle(conn Conn) error {
}
// Only keep items thqat have been requested
items := make(map[string]interface{})
items := make(map[imap.StatusItem]interface{})
for _, k := range cmd.Items {
items[k] = status.Items[k]
}
@@ -231,7 +230,7 @@ func (cmd *Append) Handle(conn Conn) error {
mbox, err := ctx.User.GetMailbox(cmd.Mailbox)
if err == backend.ErrNoSuchMailbox {
return ErrStatusResp(&imap.StatusResp{
Type: imap.StatusNo,
Type: imap.StatusRespNo,
Code: imap.CodeTryCreate,
Info: err.Error(),
})
@@ -246,10 +245,13 @@ func (cmd *Append) Handle(conn Conn) error {
// If APPEND targets the currently selected mailbox, send an untagged EXISTS
// Do this only if the backend doesn't send updates itself
if conn.Server().Updates == nil && ctx.Mailbox != nil && ctx.Mailbox.Name() == mbox.Name() {
status, err := mbox.Status([]string{imap.MailboxMessages})
status, err := mbox.Status([]imap.StatusItem{imap.StatusMessages})
if err != nil {
return err
}
status.Flags = nil
status.PermanentFlags = nil
status.UnseenSeqNum = 0
res := &responses.Select{Mailbox: status}
if err := conn.WriteResp(res); err != nil {

View File

@@ -36,7 +36,7 @@ func (cmd *StartTLS) Handle(conn Conn) error {
// Send an OK status response to let the client know that the TLS handshake
// can begin
return ErrStatusResp(&imap.StatusResp{
Type: imap.StatusOk,
Type: imap.StatusRespOk,
Info: "Begin TLS negotiation now",
})
}
@@ -62,7 +62,7 @@ func (cmd *StartTLS) Upgrade(conn Conn) error {
func afterAuthStatus(conn Conn) error {
return ErrStatusResp(&imap.StatusResp{
Type: imap.StatusOk,
Type: imap.StatusRespOk,
Code: imap.CodeCapability,
Arguments: imap.FormatStringList(conn.Capabilities()),
})

View File

@@ -2,7 +2,6 @@ package server
import (
"errors"
"strings"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/commands"
@@ -53,12 +52,8 @@ func (cmd *Close) Handle(conn Conn) error {
ctx.Mailbox = nil
ctx.MailboxReadOnly = false
if err := mailbox.Expunge(); err != nil {
return err
}
// No need to send expunge updates here, since the mailbox is already unselected
return nil
return mailbox.Expunge()
}
type Expunge struct {
@@ -206,15 +201,10 @@ func (cmd *Store) handle(uid bool, conn Conn) error {
return ErrMailboxReadOnly
}
itemStr := cmd.Item
silent := strings.HasSuffix(itemStr, imap.SilentOp)
if silent {
itemStr = strings.TrimSuffix(itemStr, imap.SilentOp)
}
item := imap.FlagsOp(itemStr)
if item != imap.SetFlags && item != imap.AddFlags && item != imap.RemoveFlags {
return errors.New("Unsupported STORE operation")
// Only flags operations are supported
op, silent, err := imap.ParseFlagsOp(cmd.Item)
if err != nil {
return err
}
flagsList, ok := cmd.Value.([]interface{})
@@ -233,7 +223,7 @@ func (cmd *Store) handle(uid bool, conn Conn) error {
// from receiving them
// TODO: find a better way to do this, without conn.silent
*conn.silent() = silent
err = ctx.Mailbox.UpdateMessagesFlags(uid, cmd.SeqSet, item, flags)
err = ctx.Mailbox.UpdateMessagesFlags(uid, cmd.SeqSet, op, flags)
*conn.silent() = false
if err != nil {
return err
@@ -244,7 +234,7 @@ func (cmd *Store) handle(uid bool, conn Conn) error {
if conn.Server().Updates == nil && !silent {
inner := &Fetch{}
inner.SeqSet = cmd.SeqSet
inner.Items = []string{"FLAGS"}
inner.Items = []imap.FetchItem{imap.FetchFlags}
if uid {
inner.Items = append(inner.Items, "UID")
}
@@ -307,7 +297,7 @@ func (cmd *Uid) Handle(conn Conn) error {
}
return ErrStatusResp(&imap.StatusResp{
Type: imap.StatusOk,
Info: imap.Uid + " " + inner.Name + " completed",
Type: imap.StatusRespOk,
Info: "UID " + inner.Name + " completed",
})
}

View File

@@ -36,7 +36,7 @@ type Conn interface {
setTLSConn(*tls.Conn)
silent() *bool // TODO: remove this
serve() error
serve(Conn) error
commandHandler(cmd *imap.Command) (hdlr Handler, err error)
}
@@ -52,17 +52,21 @@ type Context struct {
MailboxReadOnly bool
// Responses to send to the client.
Responses chan<- imap.WriterTo
// Closed when the client is logged out.
LoggedOut <-chan struct{}
}
type conn struct {
*imap.Conn
conn Conn // With extensions overrides
s *Server
ctx *Context
l sync.Locker
tlsConn *tls.Conn
continues chan bool
responses chan imap.WriterTo
loggedOut chan struct{}
silentVal bool
}
@@ -73,6 +77,7 @@ func newConn(s *Server, c net.Conn) *conn {
w := imap.NewWriter(nil)
responses := make(chan imap.WriterTo)
loggedOut := make(chan struct{})
tlsConn, _ := c.(*tls.Conn)
@@ -84,10 +89,12 @@ func newConn(s *Server, c net.Conn) *conn {
ctx: &Context{
State: imap.ConnectingState,
Responses: responses,
LoggedOut: loggedOut,
},
tlsConn: tlsConn,
continues: continues,
responses: responses,
loggedOut: loggedOut,
}
if s.Debug != nil {
@@ -149,14 +156,7 @@ func (c *conn) Close() error {
c.ctx.User.Logout()
}
if err := c.Conn.Close(); err != nil {
return err
}
close(c.continues)
c.ctx.State = imap.LogoutState
return nil
return c.Conn.Close()
}
func (c *conn) Capabilities() []string {
@@ -187,8 +187,8 @@ func (c *conn) send() {
// Send continuation requests
go func() {
for range c.continues {
res := &imap.ContinuationResp{Info: "send literal"}
if err := res.WriteTo(c.Writer); err != nil {
resp := &imap.ContinuationReq{Info: "send literal"}
if err := resp.WriteTo(c.Writer); err != nil {
c.Server().ErrorLog.Println("cannot send continuation request: ", err)
} else if err := c.Writer.Flush(); err != nil {
c.Server().ErrorLog.Println("cannot flush connection: ", err)
@@ -199,19 +199,22 @@ func (c *conn) send() {
// Send responses
for {
// Get a response that needs to be sent
res := <-c.responses
select {
case res := <-c.responses:
// Request to send the response
c.l.Lock()
// Request to send the response
c.l.Lock()
// Send the response
if err := res.WriteTo(c.Writer); err != nil {
c.Server().ErrorLog.Println("cannot send response: ", err)
} else if err := c.Writer.Flush(); err != nil {
c.Server().ErrorLog.Println("cannot flush connection: ", err)
}
// Send the response
if err := res.WriteTo(c.Writer); err != nil {
c.Server().ErrorLog.Println("cannot send response: ", err)
} else if err := c.Writer.Flush(); err != nil {
c.Server().ErrorLog.Println("cannot flush connection: ", err)
c.l.Unlock()
case <-c.loggedOut:
return
}
c.l.Unlock()
}
}
@@ -225,7 +228,7 @@ func (c *conn) greet() error {
}
greeting := &imap.StatusResp{
Type: imap.StatusOk,
Type: imap.StatusRespOk,
Code: imap.CodeCapability,
Arguments: args,
Info: "IMAP4rev1 Service Ready",
@@ -262,7 +265,15 @@ func (c *conn) silent() *bool {
return &c.silentVal
}
func (c *conn) serve() error {
func (c *conn) serve(conn Conn) error {
c.conn = conn
defer func() {
c.ctx.State = imap.LogoutState
close(c.continues)
close(c.loggedOut)
}()
// Send greeting
if err := c.greet(); err != nil {
return err
@@ -286,7 +297,7 @@ func (c *conn) serve() error {
if err != nil {
if imap.IsParseError(err) {
res = &imap.StatusResp{
Type: imap.StatusBad,
Type: imap.StatusRespBad,
Info: err.Error(),
}
} else {
@@ -298,7 +309,7 @@ func (c *conn) serve() error {
if err := cmd.Parse(fields); err != nil {
res = &imap.StatusResp{
Tag: cmd.Tag,
Type: imap.StatusBad,
Type: imap.StatusRespBad,
Info: err.Error(),
}
} else {
@@ -307,7 +318,7 @@ func (c *conn) serve() error {
if err != nil {
res = &imap.StatusResp{
Tag: cmd.Tag,
Type: imap.StatusBad,
Type: imap.StatusRespBad,
Info: err.Error(),
}
}
@@ -323,8 +334,8 @@ func (c *conn) serve() error {
continue
}
if up != nil && res.Type == imap.StatusOk {
if err := up.Upgrade(c); err != nil {
if up != nil && res.Type == imap.StatusRespOk {
if err := up.Upgrade(c.conn); err != nil {
c.s.ErrorLog.Println("cannot upgrade connection:", err)
return err
}
@@ -356,24 +367,24 @@ func (c *conn) handleCommand(cmd *imap.Command) (res *imap.StatusResp, up Upgrad
c.l.Unlock()
defer c.l.Lock()
hdlrErr := hdlr.Handle(c)
hdlrErr := hdlr.Handle(c.conn)
if statusErr, ok := hdlrErr.(*errStatusResp); ok {
res = statusErr.resp
} else if hdlrErr != nil {
res = &imap.StatusResp{
Type: imap.StatusNo,
Type: imap.StatusRespNo,
Info: hdlrErr.Error(),
}
} else {
res = &imap.StatusResp{
Type: imap.StatusOk,
Type: imap.StatusRespOk,
}
}
if res != nil {
res.Tag = cmd.Tag
if res.Type == imap.StatusOk && res.Info == "" {
if res.Type == imap.StatusRespOk && res.Info == "" {
res.Info = cmd.Name + " completed"
}
}

View File

@@ -45,7 +45,7 @@ type Upgrader interface {
type HandlerFactory func() Handler
// A function that creates SASL servers.
type SaslServerFactory func(conn Conn) sasl.Server
type SASLServerFactory func(conn Conn) sasl.Server
// An IMAP extension.
type Extension interface {
@@ -94,7 +94,7 @@ type Server struct {
conns map[Conn]struct{}
commands map[string]HandlerFactory
auths map[string]SaslServerFactory
auths map[string]SASLServerFactory
extensions []Extension
// TCP address to listen on.
@@ -104,7 +104,7 @@ type Server struct {
// This server's backend.
Backend backend.Backend
// Backend updates that will be sent to connected clients.
Updates <-chan interface{}
Updates <-chan backend.Update
// Automatically logout clients after a duration. To do not logout users
// automatically, set this to zero. The duration MUST be at least
// MinAutoLogout (as stated in RFC 3501 section 5.4).
@@ -132,7 +132,7 @@ func New(bkd backend.Backend) *Server {
ErrorLog: log.New(os.Stderr, "imap/server: ", log.LstdFlags),
}
s.auths = map[string]SaslServerFactory{
s.auths = map[string]SASLServerFactory{
sasl.Plain: func(conn Conn) sasl.Server {
return sasl.NewPlainServer(func(identity, username, password string) error {
if identity != "" && identity != username {
@@ -153,42 +153,42 @@ func New(bkd backend.Backend) *Server {
}
s.commands = map[string]HandlerFactory{
imap.Noop: func() Handler { return &Noop{} },
imap.Capability: func() Handler { return &Capability{} },
imap.Logout: func() Handler { return &Logout{} },
"NOOP": func() Handler { return &Noop{} },
"CAPABILITY": func() Handler { return &Capability{} },
"LOGOUT": func() Handler { return &Logout{} },
imap.StartTLS: func() Handler { return &StartTLS{} },
imap.Login: func() Handler { return &Login{} },
imap.Authenticate: func() Handler { return &Authenticate{} },
"STARTTLS": func() Handler { return &StartTLS{} },
"LOGIN": func() Handler { return &Login{} },
"AUTHENTICATE": func() Handler { return &Authenticate{} },
imap.Select: func() Handler { return &Select{} },
imap.Examine: func() Handler {
"SELECT": func() Handler { return &Select{} },
"EXAMINE": func() Handler {
hdlr := &Select{}
hdlr.ReadOnly = true
return hdlr
},
imap.Create: func() Handler { return &Create{} },
imap.Delete: func() Handler { return &Delete{} },
imap.Rename: func() Handler { return &Rename{} },
imap.Subscribe: func() Handler { return &Subscribe{} },
imap.Unsubscribe: func() Handler { return &Unsubscribe{} },
imap.List: func() Handler { return &List{} },
imap.Lsub: func() Handler {
"CREATE": func() Handler { return &Create{} },
"DELETE": func() Handler { return &Delete{} },
"RENAME": func() Handler { return &Rename{} },
"SUBSCRIBE": func() Handler { return &Subscribe{} },
"UNSUBSCRIBE": func() Handler { return &Unsubscribe{} },
"LIST": func() Handler { return &List{} },
"LSUB": func() Handler {
hdlr := &List{}
hdlr.Subscribed = true
return hdlr
},
imap.Status: func() Handler { return &Status{} },
imap.Append: func() Handler { return &Append{} },
"STATUS": func() Handler { return &Status{} },
"APPEND": func() Handler { return &Append{} },
imap.Check: func() Handler { return &Check{} },
imap.Close: func() Handler { return &Close{} },
imap.Expunge: func() Handler { return &Expunge{} },
imap.Search: func() Handler { return &Search{} },
imap.Fetch: func() Handler { return &Fetch{} },
imap.Store: func() Handler { return &Store{} },
imap.Copy: func() Handler { return &Copy{} },
imap.Uid: func() Handler { return &Uid{} },
"CHECK": func() Handler { return &Check{} },
"CLOSE": func() Handler { return &Close{} },
"EXPUNGE": func() Handler { return &Expunge{} },
"SEARCH": func() Handler { return &Search{} },
"FETCH": func() Handler { return &Fetch{} },
"STORE": func() Handler { return &Store{} },
"COPY": func() Handler { return &Copy{} },
"UID": func() Handler { return &Uid{} },
}
return s
@@ -207,7 +207,11 @@ func (s *Server) Serve(l net.Listener) error {
delete(s.listeners, l)
}()
go s.listenUpdates()
updater, ok := s.Backend.(backend.BackendUpdater)
if ok {
s.Updates = updater.Updates()
go s.listenUpdates()
}
for {
c, err := l.Accept()
@@ -274,7 +278,7 @@ func (s *Server) serveConn(conn Conn) error {
delete(s.conns, conn)
}()
return conn.serve()
return conn.serve(conn)
}
// Get a command handler factory for the provided command name.
@@ -289,49 +293,32 @@ func (s *Server) Command(name string) HandlerFactory {
return s.commands[name]
}
func (s *Server) listenUpdates() (err error) {
updater, ok := s.Backend.(backend.Updater)
if !ok {
return
}
s.Updates = updater.Updates()
func (s *Server) listenUpdates() {
for {
item := <-s.Updates
update := <-s.Updates
var (
update *backend.Update
res imap.WriterTo
)
switch item := item.(type) {
var res imap.WriterTo
switch update := update.(type) {
case *backend.StatusUpdate:
update = &item.Update
res = item.StatusResp
res = update.StatusResp
case *backend.MailboxUpdate:
update = &item.Update
res = &responses.Select{Mailbox: item.MailboxStatus}
res = &responses.Select{Mailbox: update.MailboxStatus}
case *backend.MessageUpdate:
update = &item.Update
ch := make(chan *imap.Message, 1)
ch <- item.Message
ch <- update.Message
close(ch)
res = &responses.Fetch{Messages: ch}
case *backend.ExpungeUpdate:
update = &item.Update
ch := make(chan uint32, 1)
ch <- item.SeqNum
ch <- update.SeqNum
close(ch)
res = &responses.Expunge{SeqNums: ch}
default:
s.ErrorLog.Printf("unhandled update: %T\n", item)
s.ErrorLog.Printf("unhandled update: %T\n", update)
}
if update == nil || res == nil {
if res == nil {
continue
}
@@ -341,10 +328,10 @@ func (s *Server) listenUpdates() (err error) {
for conn := range s.conns {
ctx := conn.Context()
if update.Username != "" && (ctx.User == nil || ctx.User.Username() != update.Username) {
if update.Username() != "" && (ctx.User == nil || ctx.User.Username() != update.Username()) {
continue
}
if update.Mailbox != "" && (ctx.Mailbox == nil || ctx.Mailbox.Name() != update.Mailbox) {
if update.Mailbox() != "" && (ctx.Mailbox == nil || ctx.Mailbox.Name() != update.Mailbox()) {
continue
}
if *conn.silent() {
@@ -374,12 +361,11 @@ func (s *Server) listenUpdates() (err error) {
for done := 0; done < wait; done++ {
<-sends
}
close(sends)
backend.DoneUpdate(update)
close(update.Done())
}()
} else {
backend.DoneUpdate(update)
close(update.Done())
}
}
}
@@ -421,6 +407,6 @@ func (s *Server) Enable(extensions ...Extension) {
//
// This function should not be called directly, it must only be used by
// libraries implementing extensions of the IMAP protocol.
func (s *Server) EnableAuth(name string, f SaslServerFactory) {
func (s *Server) EnableAuth(name string, f SASLServerFactory) {
s.auths[name] = f
}