summary refs log tree commit diff stats
path: root/worker
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2019-05-13 20:16:55 -0400
committerDrew DeVault <sir@cmpwn.com>2019-05-13 20:16:55 -0400
commit026e8a17ca40955652949584388b38566fef66e7 (patch)
treea6430ec16308dc35ddeb1bc7f381a27124427739 /worker
parentbb46b2b7e15ba839475973ae44d5a833c6f2b265 (diff)
downloadaerc-026e8a17ca40955652949584388b38566fef66e7.tar.gz
Handle incoming emails gracefully
Diffstat (limited to 'worker')
-rw-r--r--worker/imap/open.go3
-rw-r--r--worker/imap/worker.go23
2 files changed, 23 insertions, 3 deletions
diff --git a/worker/imap/open.go b/worker/imap/open.go
index dc5d6d1..ab54e88 100644
--- a/worker/imap/open.go
+++ b/worker/imap/open.go
@@ -17,6 +17,9 @@ func (imapw *IMAPWorker) handleOpenDirectory(msg *types.OpenDirectory) {
 		}, nil)
 	} else {
 		imapw.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil)
+		if imapw.idleStop == nil {
+			imapw.idleStop = make(chan struct{})
+		}
 	}
 }
 
diff --git a/worker/imap/worker.go b/worker/imap/worker.go
index 4354ab9..3d4555f 100644
--- a/worker/imap/worker.go
+++ b/worker/imap/worker.go
@@ -18,7 +18,7 @@ var errUnsupported = fmt.Errorf("unsupported command")
 
 type imapClient struct {
 	*client.Client
-	*idle.IdleClient
+	idle *idle.IdleClient
 }
 
 type IMAPWorker struct {
@@ -30,6 +30,8 @@ type IMAPWorker struct {
 	}
 
 	client   *imapClient
+	idleStop chan struct{}
+	idleDone chan error
 	selected imap.MailboxStatus
 	updates  chan client.Update
 	worker   *types.Worker
@@ -39,8 +41,9 @@ type IMAPWorker struct {
 
 func NewIMAPWorker(worker *types.Worker) *IMAPWorker {
 	return &IMAPWorker{
-		updates: make(chan client.Update, 50),
-		worker:  worker,
+		idleDone: make(chan error),
+		updates:  make(chan client.Update, 50),
+		worker:   worker,
 	}
 }
 
@@ -80,6 +83,13 @@ func (w *IMAPWorker) verifyPeerCert(msg types.WorkerMessage) func(
 }
 
 func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error {
+	if w.idleStop != nil {
+		close(w.idleStop)
+		if err := <-w.idleDone; err != nil {
+			w.worker.PostMessage(&types.Error{Error: err}, nil)
+		}
+	}
+
 	switch msg := msg.(type) {
 	case *types.Unsupported:
 		// No-op
@@ -167,6 +177,13 @@ func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error {
 	default:
 		return errUnsupported
 	}
+
+	if w.idleStop != nil {
+		w.idleStop = make(chan struct{})
+		go func() {
+			w.idleDone <- w.client.idle.IdleWithFallback(w.idleStop, 0)
+		}()
+	}
 	return nil
 }
 
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262