aerc/worker/imap/flags.go

95 lines
2.5 KiB
Go
Raw Normal View History

2019-03-21 04:23:38 +01:00
package imap
import (
"fmt"
2019-03-21 04:23:38 +01:00
"github.com/emersion/go-imap"
"git.sr.ht/~rjarry/aerc/logging"
"git.sr.ht/~rjarry/aerc/worker/types"
2019-03-21 04:23:38 +01:00
)
func (imapw *IMAPWorker) handleDeleteMessages(msg *types.DeleteMessages) {
item := imap.FormatFlagsOp(imap.AddFlags, true)
flags := []interface{}{imap.DeletedFlag}
uids := toSeqSet(msg.Uids)
if err := imapw.client.UidStore(uids, item, flags, nil); err != nil {
2019-03-21 04:23:38 +01:00
imapw.worker.PostMessage(&types.Error{
Message: types.RespondTo(msg),
Error: err,
}, nil)
return
}
if err := imapw.client.Expunge(nil); err != nil {
2019-03-21 04:23:38 +01:00
imapw.worker.PostMessage(&types.Error{
Message: types.RespondTo(msg),
Error: err,
}, nil)
} else {
imapw.worker.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil)
2019-03-21 04:23:38 +01:00
}
}
2019-06-09 20:55:34 +02:00
2020-05-25 16:59:48 +02:00
func (imapw *IMAPWorker) handleAnsweredMessages(msg *types.AnsweredMessages) {
item := imap.FormatFlagsOp(imap.AddFlags, true)
flags := []interface{}{imap.AnsweredFlag}
if !msg.Answered {
item = imap.FormatFlagsOp(imap.RemoveFlags, true)
flags = []interface{}{imap.AnsweredFlag}
}
uids := toSeqSet(msg.Uids)
emitErr := func(err error) {
imapw.worker.PostMessage(&types.Error{
Message: types.RespondTo(msg),
Error: err,
}, nil)
}
if err := imapw.client.UidStore(uids, item, flags, nil); err != nil {
emitErr(err)
return
}
imapw.worker.PostAction(&types.FetchMessageHeaders{
Uids: msg.Uids,
}, func(_msg types.WorkerMessage) {
switch m := _msg.(type) {
case *types.Error:
err := fmt.Errorf("handleAnsweredMessages: %w", m.Error)
logging.Errorf("could not fetch headers: %v", err)
2020-05-25 16:59:48 +02:00
emitErr(err)
case *types.Done:
imapw.worker.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil)
2020-05-25 16:59:48 +02:00
}
})
}
func (imapw *IMAPWorker) handleFlagMessages(msg *types.FlagMessages) {
flags := []interface{}{flagToImap[msg.Flag]}
2019-06-09 20:55:34 +02:00
item := imap.FormatFlagsOp(imap.AddFlags, true)
if !msg.Enable {
2019-06-09 20:55:34 +02:00
item = imap.FormatFlagsOp(imap.RemoveFlags, true)
}
uids := toSeqSet(msg.Uids)
emitErr := func(err error) {
2019-06-09 20:55:34 +02:00
imapw.worker.PostMessage(&types.Error{
Message: types.RespondTo(msg),
Error: err,
}, nil)
}
if err := imapw.client.UidStore(uids, item, flags, nil); err != nil {
emitErr(err)
2019-06-09 20:55:34 +02:00
return
}
imapw.worker.PostAction(&types.FetchMessageHeaders{
Uids: msg.Uids,
}, func(_msg types.WorkerMessage) {
switch m := _msg.(type) {
case *types.Error:
err := fmt.Errorf("handleFlagMessages: %w", m.Error)
logging.Errorf("could not fetch headers: %v", err)
emitErr(err)
case *types.Done:
imapw.worker.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil)
}
})
2019-06-09 20:55:34 +02:00
}