diff options
author | Koni Marti <koni.marti@gmail.com> | 2022-01-19 13:18:09 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2022-01-19 17:34:42 +0100 |
commit | 1ace50a6b927fde7ef3205001a7acd91e04ac2d7 (patch) | |
tree | d635cea128d58542be369e8f269ebd2224f4ac09 /worker | |
parent | 022bf1a11fa65c81dd5c7ded2a205a94bb449a7a (diff) | |
download | aerc-1ace50a6b927fde7ef3205001a7acd91e04ac2d7.tar.gz |
imap: emits connection error on logout
implements a new connection error message. This allows the worker to emit a
connection-related error message to the ui when the imap client closes the
loggedOut channel.
Signed-off-by: Koni Marti <koni.marti@gmail.com>
Diffstat (limited to 'worker')
-rw-r--r-- | worker/imap/worker.go | 29 | ||||
-rw-r--r-- | worker/types/messages.go | 5 |
2 files changed, 34 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 diff --git a/worker/types/messages.go b/worker/types/messages.go index fb701bdc..a5eae8d4 100644 --- a/worker/types/messages.go +++ b/worker/types/messages.go @@ -48,6 +48,11 @@ type Error struct { Error error } +type ConnError struct { + Message + Error error +} + type Unsupported struct { Message } |