1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
package server
import (
"bufio"
"errors"
"strings"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/backend"
"github.com/emersion/go-imap/commands"
"github.com/emersion/go-imap/responses"
)
// imap errors in Authenticated state.
var (
ErrNotAuthenticated = errors.New("Not authenticated")
)
type Select struct {
commands.Select
}
func (cmd *Select) Handle(conn Conn) error {
ctx := conn.Context()
// As per RFC1730#6.3.1,
// The SELECT command automatically deselects any
// currently selected mailbox before attempting the new selection.
// Consequently, if a mailbox is selected and a SELECT command that
// fails is attempted, no mailbox is selected.
// For example, some clients (e.g. Apple Mail) perform SELECT "" when the
// server doesn't announce the UNSELECT capability.
ctx.Mailbox = nil
ctx.MailboxReadOnly = false
if ctx.User == nil {
return ErrNotAuthenticated
}
mbox, err := ctx.User.GetMailbox(cmd.Mailbox)
if err != nil {
return err
}
items := []imap.StatusItem{
imap.StatusMessages, imap.StatusRecent, imap.StatusUnseen,
imap.StatusUidNext, imap.StatusUidValidity,
}
status, err := mbox.Status(items)
if err != nil {
return err
}
ctx.Mailbox = mbox
ctx.MailboxReadOnly = cmd.ReadOnly || status.ReadOnly
res := &responses.Select{Mailbox: status}
if err := conn.WriteResp(res); err != nil {
return err
}
var code imap.StatusRespCode = imap.CodeReadWrite
if ctx.MailboxReadOnly {
code = imap.CodeReadOnly
}
return ErrStatusResp(&imap.StatusResp{
Type: imap.StatusRespOk,
Code: code,
})
}
type Create struct {
commands.Create
}
func (cmd *Create) Handle(conn Conn) error {
ctx := conn.Context()
if ctx.User == nil {
return ErrNotAuthenticated
}
return ctx.User.CreateMailbox(cmd.Mailbox)
}
type Delete struct {
commands.Delete
}
func (cmd *Delete) Handle(conn Conn) error {
ctx := conn.Context()
if ctx.User == nil {
return ErrNotAuthenticated
}
return ctx.User.DeleteMailbox(cmd.Mailbox)
}
type Rename struct {
commands.Rename
}
func (cmd *Rename) Handle(conn Conn) error {
ctx := conn.Context()
if ctx.User == nil {
return ErrNotAuthenticated
}
return ctx.User.RenameMailbox(cmd.Existing, cmd.New)
}
type Subscribe struct {
commands.Subscribe
}
func (cmd *Subscribe) Handle(conn Conn) error {
ctx := conn.Context()
if ctx.User == nil {
return ErrNotAuthenticated
}
mbox, err := ctx.User.GetMailbox(cmd.Mailbox)
if err != nil {
return err
}
return mbox.SetSubscribed(true)
}
type Unsubscribe struct {
commands.Unsubscribe
}
func (cmd *Unsubscribe) Handle(conn Conn) error {
ctx := conn.Context()
if ctx.User == nil {
return ErrNotAuthenticated
}
mbox, err := ctx.User.GetMailbox(cmd.Mailbox)
if err != nil {
return err
}
return mbox.SetSubscribed(false)
}
type List struct {
commands.List
}
func (cmd *List) Handle(conn Conn) error {
ctx := conn.Context()
if ctx.User == nil {
return ErrNotAuthenticated
}
ch := make(chan *imap.MailboxInfo)
res := &responses.List{Mailboxes: ch, Subscribed: cmd.Subscribed}
done := make(chan error, 1)
go (func() {
done <- conn.WriteResp(res)
// Make sure to drain the channel.
for range ch {
}
})()
mailboxes, err := ctx.User.ListMailboxes(cmd.Subscribed)
if err != nil {
// Close channel to signal end of results
close(ch)
return err
}
for _, mbox := range mailboxes {
info, err := mbox.Info()
if err != nil {
// Close channel to signal end of results
close(ch)
return err
}
// An empty ("" string) mailbox name argument is a special request to return
// the hierarchy delimiter and the root name of the name given in the
// reference.
if cmd.Mailbox == "" {
ch <- &imap.MailboxInfo{
Attributes: []string{imap.NoSelectAttr},
Delimiter: info.Delimiter,
Name: info.Delimiter,
}
break
}
if info.Match(cmd.Reference, cmd.Mailbox) {
ch <- info
}
}
// Close channel to signal end of results
close(ch)
return <-done
}
type Status struct {
commands.Status
}
func (cmd *Status) Handle(conn Conn) error {
ctx := conn.Context()
if ctx.User == nil {
return ErrNotAuthenticated
}
mbox, err := ctx.User.GetMailbox(cmd.Mailbox)
if err != nil {
return err
}
status, err := mbox.Status(cmd.Items)
if err != nil {
return err
}
// Only keep items thqat have been requested
items := make(map[imap.StatusItem]interface{})
for _, k := range cmd.Items {
items[k] = status.Items[k]
}
status.Items = items
res := &responses.Status{Mailbox: status}
return conn.WriteResp(res)
}
type Append struct {
commands.Append
}
func (cmd *Append) Handle(conn Conn) error {
ctx := conn.Context()
if ctx.User == nil {
return ErrNotAuthenticated
}
mbox, err := ctx.User.GetMailbox(cmd.Mailbox)
if err == backend.ErrNoSuchMailbox {
return ErrStatusResp(&imap.StatusResp{
Type: imap.StatusRespNo,
Code: imap.CodeTryCreate,
Info: err.Error(),
})
} else if err != nil {
return err
}
if err := mbox.CreateMessage(cmd.Flags, cmd.Date, cmd.Message); err != nil {
if err == backend.ErrTooBig {
return ErrStatusResp(&imap.StatusResp{
Type: imap.StatusRespNo,
Code: "TOOBIG",
Info: "Message size exceeding limit",
})
}
return err
}
// 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([]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 {
return err
}
}
return nil
}
type Unselect struct {
commands.Unselect
}
func (cmd *Unselect) Handle(conn Conn) error {
ctx := conn.Context()
if ctx.Mailbox == nil {
return ErrNoMailboxSelected
}
ctx.Mailbox = nil
ctx.MailboxReadOnly = false
return nil
}
type Idle struct {
commands.Idle
}
func (cmd *Idle) Handle(conn Conn) error {
cont := &imap.ContinuationReq{Info: "idling"}
if err := conn.WriteResp(cont); err != nil {
return err
}
// Wait for DONE
scanner := bufio.NewScanner(conn)
scanner.Scan()
if err := scanner.Err(); err != nil {
return err
}
if strings.ToUpper(scanner.Text()) != "DONE" {
return errors.New("Expected DONE")
}
return nil
}
|