-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver_session.go
103 lines (90 loc) · 2.36 KB
/
server_session.go
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
// Copyright 2014 The imapsrv Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the imapsrv.LICENSE file.
package imap
import (
"fmt"
"log"
)
// IMAP session states
type state int
const (
notAuthenticated = iota
authenticated
selected
)
// An IMAP mailbox
type Mailbox struct {
Name string // The name of the mailbox
Id int64 // The id of the mailbox
}
// An IMAP session
type session struct {
// The client id
id int
// The state of the session
st state
// The currently selected mailbox (if st == selected)
mailbox *Mailbox
// IMAP configuration
config *ServerConfig
}
// Create a new IMAP session
func createSession(id int, config *ServerConfig) *session {
return &session{
id: id,
st: notAuthenticated,
config: config}
}
// Log a message with session information
func (s *session) log(info ...interface{}) {
preamble := fmt.Sprintf("IMAP (%d) ", s.id)
message := []interface{}{preamble}
message = append(message, info...)
log.Print(message...)
}
// Select a mailbox - returns true if the mailbox exists
func (s *session) selectMailbox(name string) (bool, error) {
for _, mailstore := range s.config.Mailstores {
// Lookup the mailbox
mbox, err := mailstore.GetMailbox(name)
if err != nil {
return false, err
}
if mbox == nil {
return false, nil
}
// Make note of the mailbox
s.mailbox = mbox
break
}
return true, nil
}
// Add mailbox information to the given response
func (s *session) addMailboxInfo(resp *serverResponse) error {
for _, mailstore := range s.config.Mailstores {
// Get the mailbox information from the mailstore
firstUnseen, err := mailstore.FirstUnseen(s.mailbox.Id)
if err != nil {
return err
}
totalMessages, err := mailstore.TotalMessages(s.mailbox.Id)
if err != nil {
return err
}
recentMessages, err := mailstore.RecentMessages(s.mailbox.Id)
if err != nil {
return err
}
nextUid, err := mailstore.NextUid(s.mailbox.Id)
if err != nil {
return err
}
resp.extra(fmt.Sprint(totalMessages, " EXISTS"))
resp.extra(fmt.Sprint(recentMessages, " RECENT"))
resp.extra(fmt.Sprintf("OK [UNSEEN %d] Message %d is first unseen", firstUnseen, firstUnseen))
resp.extra(fmt.Sprintf("OK [UIDVALIDITY %d] UIDs valid", s.mailbox.Id))
resp.extra(fmt.Sprintf("OK [UIDNEXT %d] Predicted next UID", nextUid))
}
return nil
}