aboutsummaryrefslogtreecommitdiffstats
path: root/worker/imap/worker.go
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2021-12-06 23:52:25 +0100
committerRobin Jarry <robin@jarry.cc>2021-12-07 21:26:56 +0100
commitcc432eefd32dc8652651ca0966ba733c7c47e088 (patch)
treebfe5db5d25bc76d7703661ef4550e56b55996ffa /worker/imap/worker.go
parentabcd327359592ca7b552585a0f3837c930b126b4 (diff)
downloadaerc-cc432eefd32dc8652651ca0966ba733c7c47e088.tar.gz
imap: move connect procedure into a dedicated function
This will prepare for extra tcp connection options support and for automatic reconnect. No functional change. Signed-off-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'worker/imap/worker.go')
-rw-r--r--worker/imap/worker.go100
1 files changed, 55 insertions, 45 deletions
diff --git a/worker/imap/worker.go b/worker/imap/worker.go
index c427e60a..55469cf3 100644
--- a/worker/imap/worker.go
+++ b/worker/imap/worker.go
@@ -107,54 +107,11 @@ func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error {
w.config.user = u.User
w.config.folders = msg.Config.Folders
case *types.Connect:
- var (
- c *client.Client
- err error
- )
if w.client != nil && w.client.State() == imap.SelectedState {
return fmt.Errorf("Already connected")
}
- switch w.config.scheme {
- case "imap":
- c, err = client.Dial(w.config.addr)
- if err != nil {
- return err
- }
-
- if !w.config.insecure {
- if err := c.StartTLS(&tls.Config{}); err != nil {
- return err
- }
- }
- case "imaps":
- c, err = client.DialTLS(w.config.addr, &tls.Config{})
- if err != nil {
- return err
- }
- default:
- return fmt.Errorf("Unknown IMAP scheme %s", w.config.scheme)
- }
- c.ErrorLog = w.worker.Logger
-
- if w.config.user != nil {
- username := w.config.user.Username()
- password, hasPassword := w.config.user.Password()
- if !hasPassword {
- // TODO: ask password
- }
-
- if w.config.oauthBearer.Enabled {
- if err := w.config.oauthBearer.Authenticate(username, password, c); err != nil {
- return err
- }
- } else if err := c.Login(username, password); err != nil {
- return err
- }
- }
-
- c.SetDebug(w.worker.Logger.Writer())
-
- if _, err := c.Select(imap.InboxName, false); err != nil {
+ c, err := w.connect()
+ if err != nil {
return err
}
@@ -255,6 +212,59 @@ func (w *IMAPWorker) handleImapUpdate(update client.Update) {
}
}
+func (w *IMAPWorker) connect() (*client.Client, error) {
+ var (
+ c *client.Client
+ err error
+ )
+ switch w.config.scheme {
+ case "imap":
+ c, err = client.Dial(w.config.addr)
+ if err != nil {
+ return nil, err
+ }
+
+ if !w.config.insecure {
+ if err := c.StartTLS(&tls.Config{}); err != nil {
+ return nil, err
+ }
+ }
+ case "imaps":
+ c, err = client.DialTLS(w.config.addr, &tls.Config{})
+ if err != nil {
+ return nil, err
+ }
+ default:
+ return nil, fmt.Errorf("Unknown IMAP scheme %s", w.config.scheme)
+ }
+ c.ErrorLog = w.worker.Logger
+
+ if w.config.user != nil {
+ username := w.config.user.Username()
+ password, hasPassword := w.config.user.Password()
+ if !hasPassword {
+ // TODO: ask password
+ }
+
+ if w.config.oauthBearer.Enabled {
+ if err := w.config.oauthBearer.Authenticate(
+ username, password, c); err != nil {
+ return nil, err
+ }
+ } else if err := c.Login(username, password); err != nil {
+ return nil, err
+ }
+ }
+
+ c.SetDebug(w.worker.Logger.Writer())
+
+ if _, err := c.Select(imap.InboxName, false); err != nil {
+ return nil, err
+ }
+
+ return c, nil
+}
+
func (w *IMAPWorker) Run() {
for {
select {