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>
This commit is contained in:
Koni Marti 2022-01-19 13:18:09 +01:00 committed by Robin Jarry
parent 022bf1a11f
commit 1ace50a6b9
3 changed files with 37 additions and 0 deletions

View file

@ -288,6 +288,9 @@ func (acct *AccountView) onMessage(msg types.WorkerMessage) {
}
case *types.LabelList:
acct.labels = msg.Labels
case *types.ConnError:
acct.logger.Printf("Connection error = %v", msg.Error)
acct.aerc.PushError(fmt.Sprintf("%v", msg.Error))
case *types.Error:
acct.logger.Printf("%v", msg.Error)
acct.aerc.PushError(fmt.Sprintf("%v", msg.Error))

View file

@ -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

View file

@ -48,6 +48,11 @@ type Error struct {
Error error
}
type ConnError struct {
Message
Error error
}
type Unsupported struct {
Message
}