From 6271d455eb7d72ca2942e3721721be54fed27779 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sun, 9 Jun 2019 14:55:34 -0400 Subject: Add :read and :unread commands --- commands/msg/read.go | 38 ++++++++++++++++++++++++++++++++++++++ lib/msgstore.go | 14 ++++++++++++++ worker/imap/flags.go | 17 +++++++++++++++++ worker/imap/worker.go | 2 ++ worker/types/messages.go | 7 +++++++ 5 files changed, 78 insertions(+) create mode 100644 commands/msg/read.go diff --git a/commands/msg/read.go b/commands/msg/read.go new file mode 100644 index 0000000..9844797 --- /dev/null +++ b/commands/msg/read.go @@ -0,0 +1,38 @@ +package msg + +import ( + "errors" + "time" + + "github.com/gdamore/tcell" + + "git.sr.ht/~sircmpwn/aerc/widgets" + "git.sr.ht/~sircmpwn/aerc/worker/types" +) + +func init() { + register("read", Read) + register("unread", Read) +} + +func Read(aerc *widgets.Aerc, args []string) error { + if len(args) != 1 { + return errors.New("Usage: " + args[0]) + } + + widget := aerc.SelectedTab().(widgets.ProvidesMessage) + msg := widget.SelectedMessage() + store := widget.Store() + store.Read([]uint32{msg.Uid}, args[0] == "read", func( + msg types.WorkerMessage) { + + switch msg := msg.(type) { + case *types.Done: + aerc.PushStatus("Messages updated.", 10*time.Second) + case *types.Error: + aerc.PushStatus(" "+msg.Error.Error(), 10*time.Second). + Color(tcell.ColorDefault, tcell.ColorRed) + } + }) + return nil +} diff --git a/lib/msgstore.go b/lib/msgstore.go index 56725e3..5b4d540 100644 --- a/lib/msgstore.go +++ b/lib/msgstore.go @@ -265,3 +265,17 @@ func (store *MessageStore) Move(uids []uint32, dest string, createDest bool, store.update() } + +func (store *MessageStore) Read(uids []uint32, read bool, + cb func(msg types.WorkerMessage)) { + + var set imap.SeqSet + for _, uid := range uids { + set.AddNum(uid) + } + + store.worker.PostAction(&types.ReadMessages{ + Read: read, + Uids: set, + }, cb) +} diff --git a/worker/imap/flags.go b/worker/imap/flags.go index bd368c1..0122d8e 100644 --- a/worker/imap/flags.go +++ b/worker/imap/flags.go @@ -41,3 +41,20 @@ func (imapw *IMAPWorker) handleDeleteMessages(msg *types.DeleteMessages) { imapw.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil) } } + +func (imapw *IMAPWorker) handleReadMessages(msg *types.ReadMessages) { + item := imap.FormatFlagsOp(imap.AddFlags, true) + flags := []interface{}{imap.SeenFlag} + if !msg.Read { + item = imap.FormatFlagsOp(imap.RemoveFlags, true) + flags = []interface{}{imap.SeenFlag} + } + if err := imapw.client.UidStore(&msg.Uids, item, flags, nil); err != nil { + imapw.worker.PostMessage(&types.Error{ + Message: types.RespondTo(msg), + Error: err, + }, nil) + return + } + imapw.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil) +} diff --git a/worker/imap/worker.go b/worker/imap/worker.go index 373072b..d978755 100644 --- a/worker/imap/worker.go +++ b/worker/imap/worker.go @@ -137,6 +137,8 @@ func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error { w.handleFetchFullMessages(msg) case *types.DeleteMessages: w.handleDeleteMessages(msg) + case *types.ReadMessages: + w.handleReadMessages(msg) case *types.CopyMessages: w.handleCopyMessages(msg) case *types.AppendMessage: diff --git a/worker/types/messages.go b/worker/types/messages.go index 0d81c4f..fa4b4e4 100644 --- a/worker/types/messages.go +++ b/worker/types/messages.go @@ -108,6 +108,13 @@ type DeleteMessages struct { Uids imap.SeqSet } +// Marks messages as read or unread +type ReadMessages struct { + Message + Read bool + Uids imap.SeqSet +} + type CopyMessages struct { Message Destination string -- cgit v1.2.3