aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.eu>2021-02-28 15:20:03 +0100
committerRené 'Necoro' Neumann <necoro@necoro.eu>2021-02-28 15:20:03 +0100
commitf22c45ab83a83297feaaa727accf2f93a124a336 (patch)
tree0b9a8174a6162628e8b1296ade0a6b24d045bf93
parent0182cd0c2b1ca8a9bfa7f6725c44620d24dfe5ba (diff)
downloadfeed2imap-go-f22c45ab83a83297feaaa727accf2f93a124a336.tar.gz
feed2imap-go-f22c45ab83a83297feaaa727accf2f93a124a336.tar.bz2
feed2imap-go-f22c45ab83a83297feaaa727accf2f93a124a336.zip
Restructure
-rw-r--r--internal/imap/client.go49
-rw-r--r--internal/imap/imap.go54
2 files changed, 49 insertions, 54 deletions
diff --git a/internal/imap/client.go b/internal/imap/client.go
index c916a64..ebcd7b3 100644
--- a/internal/imap/client.go
+++ b/internal/imap/client.go
@@ -1,11 +1,15 @@
package imap
import (
+ "fmt"
+ "net"
"sync/atomic"
+ "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"
)
@@ -26,6 +30,49 @@ type Client struct {
connChannel chan *connection
}
+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
+ }
+
+ 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.stopCommander()
@@ -62,7 +109,7 @@ func (cl *Client) createConnection(c *imapClient.Client) *connection {
return conn
}
-func NewClient() *Client {
+func newClient() *Client {
return &Client{
mailboxes: NewMailboxes(),
connChannel: make(chan *connection, 0),
diff --git a/internal/imap/imap.go b/internal/imap/imap.go
index 51687ff..a6995af 100644
--- a/internal/imap/imap.go
+++ b/internal/imap/imap.go
@@ -2,68 +2,16 @@ package imap
import (
"fmt"
- "net"
"strings"
- "time"
-
- imapClient "github.com/emersion/go-imap/client"
"github.com/Necoro/feed2imap-go/pkg/config"
"github.com/Necoro/feed2imap-go/pkg/log"
)
-var dialer imapClient.Dialer
-
-func init() {
- dialer = &net.Dialer{Timeout: 30 * time.Second}
-}
-
-func newImapClient(url config.Url) (*imapClient.Client, error) {
- var (
- 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 c, nil
-}
-
-func (cl *Client) connect(url config.Url) (*connection, error) {
- c, err := newImapClient(url)
- if err != nil {
- return nil, err
- }
-
- 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 Connect(url config.Url) (*Client, error) {
var err error
- client := NewClient()
+ client := newClient()
client.host = url.Host
defer func() {
if err != nil {