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