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{})
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Archive) Aliases() []string {
|
2019-06-27 19:33:11 +02:00
|
|
|
return []string{"archive"}
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Archive) Complete(aerc *widgets.Aerc, args []string) []string {
|
2019-06-27 19:33:11 +02:00
|
|
|
return nil
|
2019-06-08 19:41:56 +02:00
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +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-14 09:42:24 +02:00
|
|
|
store := widget.Store()
|
|
|
|
if store == nil {
|
|
|
|
return errors.New("Cannot perform action. Messages still loading")
|
|
|
|
}
|
2019-07-10 02:04:21 +02:00
|
|
|
msg, err := widget.SelectedMessage()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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
|
|
|
|
}
|