2019-06-08 19:41:56 +02:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2019-06-27 19:33:11 +02:00
|
|
|
type Archive struct{}
|
|
|
|
|
2019-06-08 19:41:56 +02:00
|
|
|
func init() {
|
2019-06-27 19:33:11 +02:00
|
|
|
register(Archive{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (_ Archive) Aliases() []string {
|
|
|
|
return []string{"archive"}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (_ Archive) Complete(aerc *widgets.Aerc, args []string) []string {
|
|
|
|
return nil
|
2019-06-08 19:41:56 +02:00
|
|
|
}
|
|
|
|
|
2019-06-27 19:33:11 +02:00
|
|
|
func (_ Archive) Execute(aerc *widgets.Aerc, args []string) error {
|
2019-06-08 19:41:56 +02:00
|
|
|
if len(args) != 2 {
|
|
|
|
return errors.New("Usage: archive <flat|year|month>")
|
|
|
|
}
|
2019-06-11 07:27:54 +02:00
|
|
|
widget := aerc.SelectedTab().(widgets.ProvidesMessage)
|
|
|
|
acct := widget.SelectedAccount()
|
2019-06-08 19:41:56 +02:00
|
|
|
if acct == nil {
|
|
|
|
return errors.New("No account selected")
|
|
|
|
}
|
2019-07-10 02:04:21 +02:00
|
|
|
msg, err := widget.SelectedMessage()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-11 07:27:54 +02:00
|
|
|
store := widget.Store()
|
2019-06-08 19:41:56 +02:00
|
|
|
archiveDir := acct.AccountConfig().Archive
|
2019-06-11 07:05:55 +02:00
|
|
|
store.Next()
|
|
|
|
acct.Messages().Scroll()
|
2019-06-08 19:41:56 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|