Skip to content

Commit 22e254a

Browse files
authored
Sync master with main (#3)
1 parent b71832c commit 22e254a

File tree

3 files changed

+35
-30
lines changed

3 files changed

+35
-30
lines changed

backend/memory/backend.go

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ package memory
33

44
import (
55
"errors"
6-
"time"
7-
6+
"fmt"
87
"github.com/emersion/go-imap"
98
"github.com/emersion/go-imap/backend"
9+
"sync"
1010
)
1111

1212
type Backend struct {
13-
users map[string]*User
13+
users map[string]*User
14+
userLock sync.Mutex
1415
}
1516

1617
func (be *Backend) Login(_ *imap.ConnInfo, username, password string) (backend.User, error) {
@@ -22,35 +23,36 @@ func (be *Backend) Login(_ *imap.ConnInfo, username, password string) (backend.U
2223
return nil, errors.New("Bad username or password")
2324
}
2425

25-
func New() *Backend {
26-
user := &User{username: "username", password: "password"}
27-
28-
body := "From: contact@example.org\r\n" +
29-
"To: contact@example.org\r\n" +
30-
"Subject: A little message, just for you\r\n" +
31-
"Date: Wed, 11 May 2016 14:31:59 +0000\r\n" +
32-
"Message-ID: <0000000@localhost/>\r\n" +
33-
"Content-Type: text/plain\r\n" +
34-
"\r\n" +
35-
"Hi there :)"
36-
37-
user.mailboxes = map[string]*Mailbox{
38-
"INBOX": {
39-
name: "INBOX",
40-
user: user,
41-
Messages: []*Message{
42-
{
43-
Uid: 6,
44-
Date: time.Now(),
45-
Flags: []string{"\\Seen"},
46-
Size: uint32(len(body)),
47-
Body: []byte(body),
48-
},
49-
},
50-
},
26+
func (be *Backend) AddUser(username, password string) (*User, error) {
27+
if _, ok := be.users[username]; ok {
28+
return nil, fmt.Errorf("user %s already exists", username)
29+
}
30+
31+
user := &User{
32+
username: username,
33+
password: password,
5134
}
5235

36+
be.userLock.Lock()
37+
defer be.userLock.Unlock()
38+
be.users[username] = user
39+
return user, nil
40+
}
41+
42+
func (u *User) SetMailboxes(mailboxes map[string]*Mailbox) {
43+
u.mailboxes = mailboxes
44+
}
45+
46+
func (be *Backend) GetUser(username string) (*User, error) {
47+
if user, ok := be.users[username]; ok {
48+
return user, nil
49+
}
50+
return nil, fmt.Errorf("no user exists with username %s", username)
51+
}
52+
53+
func New() *Backend {
5354
return &Backend{
54-
users: map[string]*User{user.username: user},
55+
users: map[string]*User{},
56+
userLock: sync.Mutex{},
5557
}
5658
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/emersion/go-imap
33
go 1.13
44

55
require (
6+
github.com/emersion/go-imap v1.2.1
67
github.com/emersion/go-message v0.15.0
78
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21
89
golang.org/x/text v0.3.7

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/emersion/go-imap v1.2.1 h1:+s9ZjMEjOB8NzZMVTM3cCenz2JrQIGGo5j1df19WjTA=
2+
github.com/emersion/go-imap v1.2.1/go.mod h1:Qlx1FSx2FTxjnjWpIlVNEuX+ylerZQNFE5NsmKFSejY=
13
github.com/emersion/go-message v0.15.0 h1:urgKGqt2JAc9NFJcgncQcohHdiYb803YTH9OQwHBHIY=
24
github.com/emersion/go-message v0.15.0/go.mod h1:wQUEfE+38+7EW8p8aZ96ptg6bAb1iwdgej19uXASlE4=
35
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 h1:OJyUGMJTzHTd1XQp98QTaHernxMYzRaOasRir9hUlFQ=

0 commit comments

Comments
 (0)