2019-03-21 04:23:38 +01:00
|
|
|
package imap
|
|
|
|
|
|
|
|
import (
|
2020-01-24 12:10:00 +01:00
|
|
|
"fmt"
|
2019-03-21 04:23:38 +01:00
|
|
|
"github.com/emersion/go-imap"
|
|
|
|
|
2019-05-18 02:57:10 +02:00
|
|
|
"git.sr.ht/~sircmpwn/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
|
|
|
|
}
|
|
|
|
var deleted []uint32
|
|
|
|
ch := make(chan uint32)
|
|
|
|
done := make(chan interface{})
|
|
|
|
go func() {
|
|
|
|
for seqNum := range ch {
|
|
|
|
i := seqNum - 1
|
|
|
|
deleted = append(deleted, imapw.seqMap[i])
|
|
|
|
imapw.seqMap = append(imapw.seqMap[:i], imapw.seqMap[i+1:]...)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
imapw.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil)
|
|
|
|
}
|
|
|
|
}
|
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: %v", m.Error)
|
|
|
|
imapw.worker.Logger.Printf("could not fetch headers: %s", err)
|
|
|
|
emitErr(err)
|
|
|
|
case *types.Done:
|
|
|
|
imapw.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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:
|
2020-07-05 16:29:52 +02:00
|
|
|
err := fmt.Errorf("handleFlagMessages: %v", m.Error)
|
2020-01-24 12:10:00 +01:00
|
|
|
imapw.worker.Logger.Printf("could not fetch headers: %s", err)
|
|
|
|
emitErr(err)
|
|
|
|
case *types.Done:
|
|
|
|
imapw.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil)
|
|
|
|
}
|
|
|
|
})
|
2019-06-09 20:55:34 +02:00
|
|
|
}
|