2019-03-21 04:23:38 +01:00
|
|
|
package imap
|
|
|
|
|
|
|
|
import (
|
2020-01-24 12:10:00 +01:00
|
|
|
"fmt"
|
2022-06-20 21:49:05 +02:00
|
|
|
|
2019-03-21 04:23:38 +01:00
|
|
|
"github.com/emersion/go-imap"
|
|
|
|
|
2022-03-22 09:52:27 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/logging"
|
2021-11-05 10:19:46 +01:00
|
|
|
"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}
|
2019-07-08 04:43:57 +02:00
|
|
|
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
|
|
|
|
}
|
2022-07-26 14:22:19 +02:00
|
|
|
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 {
|
2022-03-18 09:53:02 +01:00
|
|
|
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:
|
2022-07-19 22:31:51 +02:00
|
|
|
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:
|
2022-03-18 09:53:02 +01:00
|
|
|
imapw.worker.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil)
|
2020-05-25 16:59:48 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-05 16:29:52 +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)
|
2020-07-05 16:29:52 +02:00
|
|
|
if !msg.Enable {
|
2019-06-09 20:55:34 +02:00
|
|
|
item = imap.FormatFlagsOp(imap.RemoveFlags, true)
|
|
|
|
}
|
2019-07-08 04:43:57 +02:00
|
|
|
uids := toSeqSet(msg.Uids)
|
2020-01-24 12:10:00 +01:00
|
|
|
emitErr := func(err error) {
|
2019-06-09 20:55:34 +02:00
|
|
|
imapw.worker.PostMessage(&types.Error{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Error: err,
|
|
|
|
}, nil)
|
2020-01-24 12:10:00 +01:00
|
|
|
}
|
|
|
|
if err := imapw.client.UidStore(uids, item, flags, nil); err != nil {
|
|
|
|
emitErr(err)
|
2019-06-09 20:55:34 +02:00
|
|
|
return
|
|
|
|
}
|
2020-01-24 12:10:00 +01:00
|
|
|
imapw.worker.PostAction(&types.FetchMessageHeaders{
|
|
|
|
Uids: msg.Uids,
|
|
|
|
}, func(_msg types.WorkerMessage) {
|
|
|
|
switch m := _msg.(type) {
|
|
|
|
case *types.Error:
|
2022-07-19 22:31:51 +02:00
|
|
|
err := fmt.Errorf("handleFlagMessages: %w", m.Error)
|
|
|
|
logging.Errorf("could not fetch headers: %v", err)
|
2020-01-24 12:10:00 +01:00
|
|
|
emitErr(err)
|
|
|
|
case *types.Done:
|
2022-03-18 09:53:02 +01:00
|
|
|
imapw.worker.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil)
|
2020-01-24 12:10:00 +01:00
|
|
|
}
|
|
|
|
})
|
2019-06-09 20:55:34 +02:00
|
|
|
}
|