diff options
author | Koni Marti <koni.marti@gmail.com> | 2022-01-19 13:18:10 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2022-01-19 17:40:52 +0100 |
commit | beae17a6da37402d1c69dc76b476f55cbae982b8 (patch) | |
tree | 94309a87346dc320627a032bfce73f58bcc77412 /worker | |
parent | 1ace50a6b927fde7ef3205001a7acd91e04ac2d7 (diff) | |
download | aerc-beae17a6da37402d1c69dc76b476f55cbae982b8.zip |
imap: auto-reconnects on connection error
if the worker emits a connection error, the ui will automatically send back a
reconnect command. The worker then establishes a new connection. Auto-reconnect
is disabled when the user sends the disconnect command.
Fixes: https://todo.sr.ht/~rjarry/aerc/1
Signed-off-by: Koni Marti <koni.marti@gmail.com>
Diffstat (limited to 'worker')
-rw-r--r-- | worker/imap/worker.go | 26 | ||||
-rw-r--r-- | worker/types/messages.go | 4 |
2 files changed, 28 insertions, 2 deletions
diff --git a/worker/imap/worker.go b/worker/imap/worker.go index 6ad7ed2..ba53df2 100644 --- a/worker/imap/worker.go +++ b/worker/imap/worker.go @@ -55,8 +55,9 @@ type IMAPWorker struct { updates chan client.Update worker *types.Worker // Map of sequence numbers to UIDs, index 0 is seq number 1 - seqMap []uint32 - done chan struct{} + seqMap []uint32 + done chan struct{} + autoReconnect bool } func NewIMAPWorker(worker *types.Worker) (types.Backend, error) { @@ -186,6 +187,26 @@ func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error { w.startConnectionObserver() + w.autoReconnect = true + w.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil) + case *types.Reconnect: + if !w.autoReconnect { + reterr = fmt.Errorf("auto-reconnect is disabled; run connect to enable it") + break + } + c, err := w.connect() + if err != nil { + reterr = err + 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 { @@ -199,6 +220,7 @@ func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error { reterr = err break } + w.autoReconnect = false w.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil) case *types.ListDirectories: w.handleListDirectories(msg) diff --git a/worker/types/messages.go b/worker/types/messages.go index a5eae8d..d8f1f56 100644 --- a/worker/types/messages.go +++ b/worker/types/messages.go @@ -68,6 +68,10 @@ type Connect struct { Message } +type Reconnect struct { + Message +} + type Disconnect struct { Message } |