aboutsummaryrefslogtreecommitdiff
path: root/internal/imap/imap.go
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.eu>2020-04-23 20:10:05 +0200
committerRené 'Necoro' Neumann <necoro@necoro.eu>2020-04-23 20:10:05 +0200
commit6bd87a567ef481b922f6baec2b475ec376c45443 (patch)
treed13c1995234acd3a07d16dafb009aa53ba05a7cb /internal/imap/imap.go
parent7bfe97f3e72279a17c16803d64a16beb55944332 (diff)
downloadfeed2imap-go-6bd87a567ef481b922f6baec2b475ec376c45443.tar.gz
feed2imap-go-6bd87a567ef481b922f6baec2b475ec376c45443.tar.bz2
feed2imap-go-6bd87a567ef481b922f6baec2b475ec376c45443.zip
Concurrent imap
Diffstat (limited to 'internal/imap/imap.go')
-rw-r--r--internal/imap/imap.go82
1 files changed, 48 insertions, 34 deletions
diff --git a/internal/imap/imap.go b/internal/imap/imap.go
index 4965ca5..52c61f0 100644
--- a/internal/imap/imap.go
+++ b/internal/imap/imap.go
@@ -56,51 +56,34 @@ func sanitizeUrl(url *url.URL) {
setDefaultPort(url)
}
-func Connect(url *url.URL) (*Client, error) {
- var c *imapClient.Client
- var err error
-
- sanitizeUrl(url)
-
- forceTls := forceTLS(url)
-
+func newImapClient(url *url.URL, forceTls bool) (*imapClient.Client,error) {
if forceTls {
- c, err = imapClient.DialTLS(url.Host, nil)
+ c, err := imapClient.DialTLS(url.Host, nil)
if err != nil {
return nil, fmt.Errorf("connecting (TLS) to %s: %w", url.Host, err)
}
log.Print("Connected to ", url.Host, " (TLS)")
+ return c, nil
} else {
- c, err = imapClient.Dial(url.Host)
+ c, err := imapClient.Dial(url.Host)
if err != nil {
return nil, fmt.Errorf("connecting to %s: %w", url.Host, err)
}
+ return c, nil
}
+}
- var client = Client{c: c, host: url.Host, mailboxes: mailboxes{}}
+func (client *Client) connect(url *url.URL, forceTls bool) (*connection, error) {
+ c, err := newImapClient(url, forceTls)
+ if err != nil {
+ return nil, err
+ }
- defer func() {
- if err != nil {
- client.Disconnect()
- }
- }()
+ conn := client.createConnection(c)
if !forceTls {
- var hasStartTls bool // explicit to avoid shadowing err
-
- hasStartTls, err = c.SupportStartTLS()
- if err != nil {
- return nil, fmt.Errorf("checking for starttls for %s: %w", url.Host, err)
- }
-
- if hasStartTls {
- if err = c.StartTLS(nil); err != nil {
- return nil, fmt.Errorf("enabling starttls for %s: %w", url.Host, err)
- }
-
- log.Print("Connected to ", url.Host, " (STARTTLS)")
- } else {
- log.Print("Connected to ", url.Host, " (Plain)")
+ if err = conn.startTls(); err != nil {
+ return nil, err
}
}
@@ -109,9 +92,33 @@ func Connect(url *url.URL) (*Client, error) {
return nil, fmt.Errorf("login to %s: %w", url.Host, err)
}
- if err = client.fetchDelimiter(); err != nil {
+ return conn, nil
+}
+
+func Connect(url *url.URL) (*Client, error) {
+ var err error
+
+ sanitizeUrl(url)
+ forceTls := forceTLS(url)
+
+ client := NewClient()
+ client.host = url.Host
+ defer func() {
+ if err != nil {
+ client.Disconnect()
+ }
+ }()
+
+ var conn *connection // the main connection
+ if conn, err = client.connect(url, forceTls); err != nil {
+ return nil, err
+ }
+
+ delim, err := conn.fetchDelimiter()
+ if err != nil {
return nil, fmt.Errorf("fetching delimiter: %w", err)
}
+ client.delimiter = delim
toplevel := url.Path
if toplevel[0] == '/' {
@@ -121,11 +128,18 @@ func Connect(url *url.URL) (*Client, error) {
log.Printf("Determined '%s' as toplevel, with '%s' as delimiter", client.toplevel, client.delimiter)
- if err = client.ensureFolder(client.toplevel); err != nil {
+ if err = conn.ensureFolder(client.toplevel); err != nil {
return nil, err
}
+ // the other connections
+ for i := 1; i < len(client.connections); i++ {
+ if _, err := client.connect(url, forceTls); err != nil { // explicitly new var 'err', b/c these are now harmless
+ log.Warnf("connecting #%d: %s", i, err)
+ }
+ }
+
client.startCommander()
- return &client, nil
+ return client, nil
}