aboutsummaryrefslogtreecommitdiffstats
path: root/worker/imap
diff options
context:
space:
mode:
Diffstat (limited to 'worker/imap')
-rw-r--r--worker/imap/worker.go29
1 files changed, 29 insertions, 0 deletions
diff --git a/worker/imap/worker.go b/worker/imap/worker.go
index 81d954fb..6ad7ed29 100644
--- a/worker/imap/worker.go
+++ b/worker/imap/worker.go
@@ -56,6 +56,7 @@ type IMAPWorker struct {
worker *types.Worker
// Map of sequence numbers to UIDs, index 0 is seq number 1
seqMap []uint32
+ done chan struct{}
}
func NewIMAPWorker(worker *types.Worker) (types.Backend, error) {
@@ -178,14 +179,22 @@ func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error {
break
}
+ w.stopConnectionObserver()
+
c.Updates = w.updates
w.client = &imapClient{c, sortthread.NewThreadClient(c), sortthread.NewSortClient(c)}
+
+ w.startConnectionObserver()
+
w.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil)
case *types.Disconnect:
if w.client == nil || w.client.State() != imap.SelectedState {
reterr = fmt.Errorf("Not connected")
break
}
+
+ w.stopConnectionObserver()
+
if err := w.client.Logout(); err != nil {
reterr = err
break
@@ -271,6 +280,26 @@ func (w *IMAPWorker) handleImapUpdate(update client.Update) {
}
}
+func (w *IMAPWorker) startConnectionObserver() {
+ go func() {
+ select {
+ case <-w.client.LoggedOut():
+ w.worker.PostMessage(&types.ConnError{
+ Error: fmt.Errorf("Logged Out"),
+ }, nil)
+ case <-w.done:
+ return
+ }
+ }()
+}
+
+func (w *IMAPWorker) stopConnectionObserver() {
+ if w.done != nil {
+ close(w.done)
+ }
+ w.done = make(chan struct{})
+}
+
func (w *IMAPWorker) connect() (*client.Client, error) {
var (
conn *net.TCPConn