aboutsummaryrefslogtreecommitdiff
path: root/internal/imap/client.go
blob: 4a0a0d8ef6320f7fb6d7ae65825832353f3e3854 (plain)
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
122
123
124
125
126
127
128
129
130
131
package imap

import (
	"fmt"
	"net"
	"sync"
	"time"

	uidplus "github.com/emersion/go-imap-uidplus"
	imapClient "github.com/emersion/go-imap/client"

	"github.com/Necoro/feed2imap-go/pkg/config"
	"github.com/Necoro/feed2imap-go/pkg/log"
)

type connConf struct {
	host      string
	delimiter string
	toplevel  Folder
}

type Client struct {
	connConf
	mailboxes      *mailboxes
	commander      *commander
	connections    []*connection
	maxConnections int
	connChannel    chan *connection
	connLock       sync.Mutex
	disconnected   bool
}

var dialer imapClient.Dialer

func init() {
	dialer = &net.Dialer{Timeout: 30 * time.Second}
}

func newImapClient(url config.Url) (c *imapClient.Client, err error) {
	if url.ForceTLS() {
		if c, err = imapClient.DialWithDialerTLS(dialer, url.HostPort(), nil); err != nil {
			return nil, fmt.Errorf("connecting (TLS) to %s: %w", url.Host, err)
		}
		log.Print("Connected to ", url.HostPort(), " (TLS)")
	} else {
		if c, err = imapClient.DialWithDialer(dialer, url.HostPort()); err != nil {
			return nil, fmt.Errorf("connecting to %s: %w", url.Host, err)
		}
	}

	return
}

func (cl *Client) connect(url config.Url) (*connection, error) {
	c, err := newImapClient(url)
	if err != nil {
		return nil, err
	}

	cl.connLock.Lock()
	defer cl.connLock.Unlock()

	if cl.disconnected {
		return nil, nil
	}

	conn := cl.createConnection(c)

	if !url.ForceTLS() {
		if err = conn.startTls(); err != nil {
			return nil, err
		}
	}

	if err = c.Login(url.User, url.Password); err != nil {
		return nil, fmt.Errorf("login to %s: %w", url.Host, err)
	}

	cl.connChannel <- conn

	return conn, nil
}

func (cl *Client) Disconnect() {
	if cl != nil {
		cl.connLock.Lock()

		cl.stopCommander()
		close(cl.connChannel)

		connected := false
		for _, conn := range cl.connections {
			connected = conn.disconnect() || connected
		}

		if connected {
			log.Print("Disconnected from ", cl.host)
		}

		cl.disconnected = true

		cl.connLock.Unlock()
	}
}

func (cl *Client) createConnection(c *imapClient.Client) *connection {

	if len(cl.connections) == cl.maxConnections {
		panic("Too many connections")
	}

	client := &client{c, uidplus.NewClient(c)}

	conn := &connection{
		connConf:  &cl.connConf,
		mailboxes: cl.mailboxes,
		c:         client,
	}

	cl.connections = append(cl.connections, conn)
	return conn
}

func newClient(maxConnections int) *Client {
	return &Client{
		mailboxes:      NewMailboxes(),
		connChannel:    make(chan *connection, 0),
		connections:    make([]*connection, 0, maxConnections),
		maxConnections: maxConnections,
	}
}