@@ -3,14 +3,15 @@ package memory
33
44import (
55 "errors"
6- "time"
7-
6+ "fmt"
87 "github.com/emersion/go-imap"
98 "github.com/emersion/go-imap/backend"
9+ "sync"
1010)
1111
1212type Backend struct {
13- users map [string ]* User
13+ users map [string ]* User
14+ userLock sync.Mutex
1415}
1516
1617func (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}
0 commit comments