internal/imapc/open.go

35 lines · 790 bytes

 1package imapc
 2
 3import (
 4	"time"
 5
 6	"gitbay.org/gitbay/internal/config"
 7)
 8
 9// Open connects to the configured mailbox, logs in with the password
10// file's password, and opens the mailbox: read-only (EXAMINE) or for
11// setting flags (SELECT). It returns the number of messages in it.
12func Open(in config.MailInbound, readOnly bool, timeout time.Duration) (*Client, int, error) {
13	pass, err := in.Password()
14	if err != nil {
15		return nil, 0, err
16	}
17	c, err := Dial(in.Addr(), in.TLS == "starttls", timeout)
18	if err != nil {
19		return nil, 0, err
20	}
21	if err := c.Login(in.User, pass); err != nil {
22		c.Close()
23		return nil, 0, err
24	}
25	open := c.Select
26	if readOnly {
27		open = c.Examine
28	}
29	n, err := open(in.MailboxName())
30	if err != nil {
31		c.Close()
32		return nil, 0, err
33	}
34	return c, n, nil
35}