forked from alienscience/imapsrv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailstore.go
121 lines (105 loc) · 3.29 KB
/
mailstore.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package imapsrv
import (
"log"
)
// Mailbox represents an IMAP mailbox
type Mailbox struct {
Name string // The name of the mailbox
Path []string // Full mailbox path
Id int64 // The id of the mailbox
Flags uint8 // Mailbox flags
}
// Mailbox flags
const (
// Noinferiors indicates it is not possible for any child levels of hierarchy to exist
// under this name; no child levels exist now and none can be
// created in the future.
Noinferiors = 1 << iota
// Noselect indicates it is not possible to use this name as a selectable mailbox.
Noselect
// Marked indicates that the mailbox has been marked "interesting" by the server;
// the mailbox probably contains messages that have been added since
// the last time the mailbox was selected
Marked
// Unmarked indicates the mailbox does not contain any additional messages since the
// last time the mailbox was selected.
Unmarked
)
var mailboxFlags = map[uint8]string{
Noinferiors: "Noinferiors",
Noselect: "Noselect",
Marked: "Marked",
Unmarked: "Unmarked",
}
// Mailstore is a service responsible for I/O with the actual e-mails
type Mailstore interface {
// GetMailbox gets IMAP mailbox information
// Returns nil if the mailbox does not exist
GetMailbox(path []string) (*Mailbox, error)
// GetMailboxes gets a list of mailboxes at the given path
GetMailboxes(path []string) ([]*Mailbox, error)
// FirstUnseen gets the sequence number of the first unseen message in an IMAP mailbox
FirstUnseen(mbox int64) (int64, error)
// TotalMessages gets the total number of messages in an IMAP mailbox
TotalMessages(mbox int64) (int64, error)
// RecentMessages gets the total number of unread messages in an IMAP mailbox
RecentMessages(mbox int64) (int64, error)
// NextUid gets the next available uid in an IMAP mailbox
NextUid(mbox int64) (int64, error)
}
// DummyMailstore is used for demonstrating the IMAP server
type dummyMailstore struct {
}
// GetMailbox gets mailbox information
func (m *dummyMailstore) GetMailbox(path []string) (*Mailbox, error) {
return &Mailbox{
Name: "inbox",
Path: []string{"inbox"},
Id: 1,
}, nil
}
// GetMailboxes gets a list of mailboxes at the given path
func (m *dummyMailstore) GetMailboxes(path []string) ([]*Mailbox, error) {
log.Printf("GetMailboxes %v", path)
if len(path) == 0 {
// Root
return []*Mailbox{
{
Name: "inbox",
Path: []string{"inbox"},
Id: 1,
},
{
Name: "spam",
Path: []string{"spam"},
Id: 2,
},
}, nil
} else if len(path) == 1 && path[0] == "inbox" {
return []*Mailbox{
{
Name: "starred",
Path: []string{"inbox", "stared"},
Id: 3,
},
}, nil
} else {
return []*Mailbox{}, nil
}
}
// FirstUnseen gets the sequence number of the first unseen message in an IMAP mailbox
func (m *dummyMailstore) FirstUnseen(mbox int64) (int64, error) {
return 4, nil
}
// TotalMessages gets the total number of messages in an IMAP mailbox
func (m *dummyMailstore) TotalMessages(mbox int64) (int64, error) {
return 8, nil
}
// RecentMessages gets the total number of unread messages in an IMAP mailbox
func (m *dummyMailstore) RecentMessages(mbox int64) (int64, error) {
return 4, nil
}
// DummyMailstore gets the next available uid in an IMAP mailbox
func (m *dummyMailstore) NextUid(mbox int64) (int64, error) {
return 9, nil
}