Add archive command

Adds an archive command that moves the current message into the folder
specified in the account config entry.

Supports three layouts at this point:

- flat: puts all messages next to each other
- year: creates a folder per year
- month: same as above, plus folders per month

This also adds a "-p" argument to "cp" and "mv" that works like
"--parents" on mkdir(1). We use this to auto-create the directories
for the archive layout.
This commit is contained in:
Robert Günzler 2019-06-08 19:41:56 +02:00 committed by Drew DeVault
parent 35f57321f8
commit acfe7d7625
12 changed files with 168 additions and 8 deletions

62
commands/msg/archive.go Normal file
View file

@ -0,0 +1,62 @@
package msg
import (
"errors"
"fmt"
"path"
"time"
"github.com/gdamore/tcell"
"git.sr.ht/~sircmpwn/aerc/widgets"
"git.sr.ht/~sircmpwn/aerc/worker/types"
)
const (
ARCHIVE_FLAT = "flat"
ARCHIVE_YEAR = "year"
ARCHIVE_MONTH = "month"
)
func init() {
register("archive", Archive)
}
func Archive(aerc *widgets.Aerc, args []string) error {
if len(args) != 2 {
return errors.New("Usage: archive <flat|year|month>")
}
acct := aerc.SelectedAccount()
if acct == nil {
return errors.New("No account selected")
}
msg := acct.Messages().Selected()
store := acct.Messages().Store()
archiveDir := acct.AccountConfig().Archive
acct.Messages().Next()
switch args[1] {
case ARCHIVE_MONTH:
archiveDir = path.Join(archiveDir,
fmt.Sprintf("%d", msg.Envelope.Date.Year()),
fmt.Sprintf("%02d", msg.Envelope.Date.Month()))
case ARCHIVE_YEAR:
archiveDir = path.Join(archiveDir, fmt.Sprintf("%v",
msg.Envelope.Date.Year()))
case ARCHIVE_FLAT:
// deliberately left blank
}
store.Move([]uint32{msg.Uid}, archiveDir, true, func(
msg types.WorkerMessage) {
switch msg := msg.(type) {
case *types.Done:
aerc.PushStatus("Messages archived.", 10*time.Second)
case *types.Error:
aerc.PushStatus(" "+msg.Error.Error(), 10*time.Second).
Color(tcell.ColorDefault, tcell.ColorRed)
}
})
return nil
}

View file

@ -4,6 +4,7 @@ import (
"errors" "errors"
"time" "time"
"git.sr.ht/~sircmpwn/getopt"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"git.sr.ht/~sircmpwn/aerc/widgets" "git.sr.ht/~sircmpwn/aerc/widgets"
@ -16,9 +17,23 @@ func init() {
} }
func Copy(aerc *widgets.Aerc, args []string) error { func Copy(aerc *widgets.Aerc, args []string) error {
if len(args) != 2 { opts, optind, err := getopt.Getopts(args[1:], "p")
return errors.New("Usage: mv <folder>") if err != nil {
return err
} }
if optind != len(args)-2 {
return errors.New("Usage: cp [-p] <folder>")
}
var (
createParents bool
)
for _, opt := range opts {
switch opt.Option {
case 'p':
createParents = true
}
}
widget := aerc.SelectedTab().(widgets.ProvidesMessage) widget := aerc.SelectedTab().(widgets.ProvidesMessage)
acct := widget.SelectedAccount() acct := widget.SelectedAccount()
if acct == nil { if acct == nil {
@ -26,7 +41,9 @@ func Copy(aerc *widgets.Aerc, args []string) error {
} }
msg := widget.SelectedMessage() msg := widget.SelectedMessage()
store := widget.Store() store := widget.Store()
store.Copy([]uint32{msg.Uid}, args[1], func(msg types.WorkerMessage) { store.Copy([]uint32{msg.Uid}, args[optind+1], createParents, func(
msg types.WorkerMessage) {
switch msg := msg.(type) { switch msg := msg.(type) {
case *types.Done: case *types.Done:
aerc.PushStatus("Messages copied.", 10*time.Second) aerc.PushStatus("Messages copied.", 10*time.Second)

View file

@ -4,6 +4,7 @@ import (
"errors" "errors"
"time" "time"
"git.sr.ht/~sircmpwn/getopt"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"git.sr.ht/~sircmpwn/aerc/widgets" "git.sr.ht/~sircmpwn/aerc/widgets"
@ -16,9 +17,23 @@ func init() {
} }
func Move(aerc *widgets.Aerc, args []string) error { func Move(aerc *widgets.Aerc, args []string) error {
if len(args) != 2 { opts, optind, err := getopt.Getopts(args[1:], "p")
return errors.New("Usage: mv <folder>") if err != nil {
return err
} }
if optind != len(args)-2 {
return errors.New("Usage: mv [-p] <folder>")
}
var (
createParents bool
)
for _, opt := range opts {
switch opt.Option {
case 'p':
createParents = true
}
}
widget := aerc.SelectedTab().(widgets.ProvidesMessage) widget := aerc.SelectedTab().(widgets.ProvidesMessage)
acct := widget.SelectedAccount() acct := widget.SelectedAccount()
if acct == nil { if acct == nil {
@ -31,7 +46,9 @@ func Move(aerc *widgets.Aerc, args []string) error {
aerc.RemoveTab(widget) aerc.RemoveTab(widget)
} }
acct.Messages().Next() acct.Messages().Next()
store.Move([]uint32{msg.Uid}, args[1], func(msg types.WorkerMessage) { store.Move([]uint32{msg.Uid}, args[optind+1], createParents, func(
msg types.WorkerMessage) {
switch msg := msg.(type) { switch msg := msg.(type) {
case *types.Done: case *types.Done:
aerc.PushStatus("Messages moved.", 10*time.Second) aerc.PushStatus("Messages moved.", 10*time.Second)

View file

@ -28,6 +28,7 @@ K = :prev-folder<Enter>
<Enter> = :view<Enter> <Enter> = :view<Enter>
d = :confirm 'Really delete this message?' ':delete-message<Enter>'<Enter> d = :confirm 'Really delete this message?' ':delete-message<Enter>'<Enter>
D = :delete<Enter> D = :delete<Enter>
A = :archive flat<Enter>
C = :compose<Enter> C = :compose<Enter>
@ -54,6 +55,7 @@ Rq = :reply -aq<Enter>
<C-k> = :prev-part<Enter> <C-k> = :prev-part<Enter>
<C-j> = :next-part<Enter> <C-j> = :next-part<Enter>
S = :save<space> S = :save<space>
A = :archive flat<Enter>
[compose] [compose]
# Keybindings used when the embedded terminal is not selected in the compose # Keybindings used when the embedded terminal is not selected in the compose

View file

@ -34,6 +34,7 @@ const (
) )
type AccountConfig struct { type AccountConfig struct {
Archive string
CopyTo string CopyTo string
Default string Default string
From string From string
@ -115,6 +116,7 @@ func loadAccountConfig(path string) ([]AccountConfig, error) {
} }
sec := file.Section(_sec) sec := file.Section(_sec)
account := AccountConfig{ account := AccountConfig{
Archive: "Archive",
Default: "INBOX", Default: "INBOX",
Name: _sec, Name: _sec,
Params: make(map[string]string), Params: make(map[string]string),
@ -137,6 +139,8 @@ func loadAccountConfig(path string) ([]AccountConfig, error) {
account.From = val account.From = val
} else if key == "copy-to" { } else if key == "copy-to" {
account.CopyTo = val account.CopyTo = val
} else if key == "archive" {
account.Archive = val
} else if key != "name" { } else if key != "name" {
account.Params[key] = val account.Params[key] = val
} }

View file

@ -106,6 +106,11 @@ Note that many of these configuration options are written for you, such as
*source* and *outgoing*, when you run the account configuration wizard *source* and *outgoing*, when you run the account configuration wizard
(*:new-account*). (*:new-account*).
*archive*
Specifies a folder to use as the destination of the *:archive* command.
Default: Archive
*copy-to* *copy-to*
Specifies a folder to copy sent mails to, usually "Sent". Specifies a folder to copy sent mails to, usually "Sent".

View file

@ -43,6 +43,15 @@ These commands work in any context.
## MESSAGE LIST COMMANDS ## MESSAGE LIST COMMANDS
*archive* <scheme>
Moves the selected message to the archive. The available schemes are:
*flat*: No special structure, all messages in the archive directory
*year*: Messages are stored in folders per year
*month*: Messages are stored in folders per year and subfolders per month
*cf* <folder> *cf* <folder>
Change the folder shown in the message list. Change the folder shown in the message list.

View file

@ -218,20 +218,27 @@ func (store *MessageStore) Delete(uids []uint32,
store.update() store.update()
} }
func (store *MessageStore) Copy(uids []uint32, dest string, func (store *MessageStore) Copy(uids []uint32, dest string, createDest bool,
cb func(msg types.WorkerMessage)) { cb func(msg types.WorkerMessage)) {
var set imap.SeqSet var set imap.SeqSet
for _, uid := range uids { for _, uid := range uids {
set.AddNum(uid) set.AddNum(uid)
} }
if createDest {
store.worker.PostAction(&types.CreateDirectory{
Directory: dest,
}, cb)
}
store.worker.PostAction(&types.CopyMessages{ store.worker.PostAction(&types.CopyMessages{
Destination: dest, Destination: dest,
Uids: set, Uids: set,
}, cb) }, cb)
} }
func (store *MessageStore) Move(uids []uint32, dest string, func (store *MessageStore) Move(uids []uint32, dest string, createDest bool,
cb func(msg types.WorkerMessage)) { cb func(msg types.WorkerMessage)) {
var set imap.SeqSet var set imap.SeqSet
@ -240,6 +247,12 @@ func (store *MessageStore) Move(uids []uint32, dest string,
store.Deleted[uid] = nil store.Deleted[uid] = nil
} }
if createDest {
store.worker.PostAction(&types.CreateDirectory{
Directory: dest,
}, cb)
}
store.worker.PostAction(&types.CopyMessages{ store.worker.PostAction(&types.CopyMessages{
Destination: dest, Destination: dest,
Uids: set, Uids: set,

View file

@ -185,6 +185,8 @@ func (acct *AccountView) onMessage(msg types.WorkerMessage) {
} else { } else {
acct.msglist.SetStore(nil) acct.msglist.SetStore(nil)
} }
case *types.CreateDirectory:
acct.dirlist.UpdateList(nil)
} }
case *types.DirectoryInfo: case *types.DirectoryInfo:
if store, ok := acct.msgStores[msg.Name]; ok { if store, ok := acct.msgStores[msg.Name]; ok {

22
worker/imap/create.go Normal file
View file

@ -0,0 +1,22 @@
package imap
import (
"strings"
"git.sr.ht/~sircmpwn/aerc/worker/types"
)
func (imapw *IMAPWorker) handleCreateDirectory(msg *types.CreateDirectory) {
if err := imapw.client.Create(msg.Directory); err != nil {
if strings.HasPrefix(err.Error(), "Mailbox already exists") {
// ignore "already exists" error
return
}
imapw.worker.PostMessage(&types.Error{
Message: types.RespondTo(msg),
Error: err,
}, nil)
} else {
imapw.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil)
}
}

View file

@ -127,6 +127,8 @@ func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error {
w.handleOpenDirectory(msg) w.handleOpenDirectory(msg)
case *types.FetchDirectoryContents: case *types.FetchDirectoryContents:
w.handleFetchDirectoryContents(msg) w.handleFetchDirectoryContents(msg)
case *types.CreateDirectory:
w.handleCreateDirectory(msg)
case *types.FetchMessageHeaders: case *types.FetchMessageHeaders:
w.handleFetchMessageHeaders(msg) w.handleFetchMessageHeaders(msg)
case *types.FetchMessageBodyPart: case *types.FetchMessageBodyPart:

View file

@ -82,6 +82,11 @@ type FetchDirectoryContents struct {
Message Message
} }
type CreateDirectory struct {
Message
Directory string
}
type FetchMessageHeaders struct { type FetchMessageHeaders struct {
Message Message
Uids imap.SeqSet Uids imap.SeqSet