2019-03-21 03:23:38 +00:00
|
|
|
package imap
|
|
|
|
|
|
|
|
import (
|
2020-01-24 11:10:00 +00:00
|
|
|
"fmt"
|
2022-06-20 19:49:05 +00:00
|
|
|
|
2019-03-21 03:23:38 +00:00
|
|
|
"github.com/emersion/go-imap"
|
|
|
|
|
2022-03-22 08:52:27 +00:00
|
|
|
"git.sr.ht/~rjarry/aerc/logging"
|
2021-11-05 09:19:46 +00:00
|
|
|
"git.sr.ht/~rjarry/aerc/worker/types"
|
2019-03-21 03:23:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (imapw *IMAPWorker) handleDeleteMessages(msg *types.DeleteMessages) {
|
|
|
|
item := imap.FormatFlagsOp(imap.AddFlags, true)
|
|
|
|
flags := []interface{}{imap.DeletedFlag}
|
2019-07-08 02:43:57 +00:00
|
|
|
uids := toSeqSet(msg.Uids)
|
|
|
|
if err := imapw.client.UidStore(uids, item, flags, nil); err != nil {
|
2019-03-21 03:23:38 +00:00
|
|
|
imapw.worker.PostMessage(&types.Error{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Error: err,
|
|
|
|
}, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var deleted []uint32
|
|
|
|
ch := make(chan uint32)
|
|
|
|
done := make(chan interface{})
|
|
|
|
go func() {
|
2022-03-22 08:52:27 +00:00
|
|
|
defer logging.PanicHandler()
|
|
|
|
|
2019-03-21 03:23:38 +00:00
|
|
|
for seqNum := range ch {
|
2022-06-20 19:49:05 +00:00
|
|
|
if uid, found := imapw.seqMap.Pop(seqNum); !found {
|
2022-07-19 20:31:51 +00:00
|
|
|
logging.Errorf("handleDeleteMessages unknown seqnum: %d", seqNum)
|
2022-06-20 19:49:05 +00:00
|
|
|
} else {
|
|
|
|
deleted = append(deleted, uid)
|
|
|
|
}
|
2019-03-21 03:23:38 +00:00
|
|
|
}
|
|
|
|
done <- nil
|
|
|
|
}()
|
|
|
|
if err := imapw.client.Expunge(ch); err != nil {
|
|
|
|
imapw.worker.PostMessage(&types.Error{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Error: err,
|
|
|
|
}, nil)
|
|
|
|
} else {
|
|
|
|
<-done
|
|
|
|
imapw.worker.PostMessage(&types.MessagesDeleted{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Uids: deleted,
|
|
|
|
}, nil)
|
2022-03-18 08:53:02 +00:00
|
|
|
imapw.worker.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil)
|
2019-03-21 03:23:38 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-09 18:55:34 +00:00
|
|
|
|
2020-05-25 14:59:48 +00: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:
|
2022-07-19 20:31:51 +00:00
|
|
|
err := fmt.Errorf("handleAnsweredMessages: %w", m.Error)
|
|
|
|
logging.Errorf("could not fetch headers: %v", err)
|
2020-05-25 14:59:48 +00:00
|
|
|
emitErr(err)
|
|
|
|
case *types.Done:
|
2022-03-18 08:53:02 +00:00
|
|
|
imapw.worker.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil)
|
2020-05-25 14:59:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-05 14:29:52 +00:00
|
|
|
func (imapw *IMAPWorker) handleFlagMessages(msg *types.FlagMessages) {
|
|
|
|
flags := []interface{}{flagToImap[msg.Flag]}
|
2019-06-09 18:55:34 +00:00
|
|
|
item := imap.FormatFlagsOp(imap.AddFlags, true)
|
2020-07-05 14:29:52 +00:00
|
|
|
if !msg.Enable {
|
2019-06-09 18:55:34 +00:00
|
|
|
item = imap.FormatFlagsOp(imap.RemoveFlags, true)
|
|
|
|
}
|
2019-07-08 02:43:57 +00:00
|
|
|
uids := toSeqSet(msg.Uids)
|
2020-01-24 11:10:00 +00:00
|
|
|
emitErr := func(err error) {
|
2019-06-09 18:55:34 +00:00
|
|
|
imapw.worker.PostMessage(&types.Error{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Error: err,
|
|
|
|
}, nil)
|
2020-01-24 11:10:00 +00:00
|
|
|
}
|
|
|
|
if err := imapw.client.UidStore(uids, item, flags, nil); err != nil {
|
|
|
|
emitErr(err)
|
2019-06-09 18:55:34 +00:00
|
|
|
return
|
|
|
|
}
|
2020-01-24 11:10:00 +00:00
|
|
|
imapw.worker.PostAction(&types.FetchMessageHeaders{
|
|
|
|
Uids: msg.Uids,
|
|
|
|
}, func(_msg types.WorkerMessage) {
|
|
|
|
switch m := _msg.(type) {
|
|
|
|
case *types.Error:
|
2022-07-19 20:31:51 +00:00
|
|
|
err := fmt.Errorf("handleFlagMessages: %w", m.Error)
|
|
|
|
logging.Errorf("could not fetch headers: %v", err)
|
2020-01-24 11:10:00 +00:00
|
|
|
emitErr(err)
|
|
|
|
case *types.Done:
|
2022-03-18 08:53:02 +00:00
|
|
|
imapw.worker.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil)
|
2020-01-24 11:10:00 +00:00
|
|
|
}
|
|
|
|
})
|
2019-06-09 18:55:34 +00:00
|
|
|
}
|