2019-06-09 20:55:34 +02:00
|
|
|
package msg
|
|
|
|
|
|
|
|
import (
|
2020-07-17 17:50:24 +02:00
|
|
|
"fmt"
|
2019-12-22 12:05:28 +01:00
|
|
|
"sync"
|
2020-05-28 16:32:32 +02:00
|
|
|
"time"
|
2019-06-09 20:55:34 +02:00
|
|
|
|
2019-07-15 19:51:07 +02:00
|
|
|
"git.sr.ht/~sircmpwn/getopt"
|
|
|
|
|
2019-12-22 12:05:28 +01:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/lib"
|
2019-07-15 19:51:07 +02:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/models"
|
2019-06-09 20:55:34 +02:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/widgets"
|
|
|
|
"git.sr.ht/~sircmpwn/aerc/worker/types"
|
|
|
|
)
|
|
|
|
|
2020-07-05 16:29:52 +02:00
|
|
|
type FlagMsg struct{}
|
2019-06-27 19:33:11 +02:00
|
|
|
|
2019-06-09 20:55:34 +02:00
|
|
|
func init() {
|
2020-07-05 16:29:52 +02:00
|
|
|
register(FlagMsg{})
|
2019-06-27 19:33:11 +02:00
|
|
|
}
|
|
|
|
|
2020-07-05 16:29:52 +02:00
|
|
|
func (FlagMsg) Aliases() []string {
|
|
|
|
return []string{"flag", "unflag", "read", "unread"}
|
2019-06-27 19:33:11 +02:00
|
|
|
}
|
|
|
|
|
2020-07-05 16:29:52 +02:00
|
|
|
func (FlagMsg) Complete(aerc *widgets.Aerc, args []string) []string {
|
2019-06-27 19:33:11 +02:00
|
|
|
return nil
|
2019-06-09 20:55:34 +02:00
|
|
|
}
|
|
|
|
|
2020-07-05 16:29:52 +02:00
|
|
|
// If this was called as 'flag' or 'unflag', without the toggle (-t)
|
|
|
|
// option, then it will flag the corresponding messages with the given
|
|
|
|
// flag. If the toggle option was given, it will individually toggle
|
|
|
|
// the given flag for the corresponding messages.
|
|
|
|
//
|
|
|
|
// If this was called as 'read' or 'unread', it has the same effect as
|
|
|
|
// 'flag' or 'unflag', respectively, but the 'Seen' flag is affected.
|
|
|
|
func (FlagMsg) Execute(aerc *widgets.Aerc, args []string) error {
|
|
|
|
|
|
|
|
// The flag to change
|
|
|
|
var flag models.Flag
|
|
|
|
// User-readable name of the flag to change
|
|
|
|
var flagName string
|
|
|
|
// Whether to toggle the flag (true) or to enable/disable it (false)
|
|
|
|
var toggle bool
|
|
|
|
// Whether to enable (true) or disable (false) the flag
|
|
|
|
enable := (args[0] == "read" || args[0] == "flag")
|
|
|
|
// User-readable name for the action being performed
|
|
|
|
var actionName string
|
|
|
|
// Getopt option string, varies by command name
|
|
|
|
var getoptString string
|
|
|
|
// Help message to provide on parsing failure
|
|
|
|
var helpMessage string
|
|
|
|
// Used during parsing to prevent choosing a flag muliple times
|
|
|
|
// A default flag will be used if this is false
|
|
|
|
flagChosen := false
|
|
|
|
|
|
|
|
if args[0] == "read" || args[0] == "unread" {
|
|
|
|
flag = models.SeenFlag
|
|
|
|
flagName = "read"
|
|
|
|
getoptString = "t"
|
|
|
|
helpMessage = "Usage: " + args[0] + " [-t]"
|
|
|
|
} else { // 'flag' / 'unflag'
|
|
|
|
flag = models.FlaggedFlag
|
|
|
|
flagName = "flagged"
|
|
|
|
getoptString = "tax:"
|
|
|
|
helpMessage = "Usage: " + args[0] + " [-t] [-a | -x <flag>]"
|
|
|
|
}
|
|
|
|
|
|
|
|
opts, optind, err := getopt.Getopts(args, getoptString)
|
2019-07-15 19:51:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
|
|
switch opt.Option {
|
|
|
|
case 't':
|
|
|
|
toggle = true
|
2020-07-05 16:29:52 +02:00
|
|
|
case 'a':
|
|
|
|
if flagChosen {
|
|
|
|
return fmt.Errorf("Cannot choose a flag multiple times! " + helpMessage)
|
|
|
|
}
|
|
|
|
flag = models.AnsweredFlag
|
|
|
|
flagName = "answered"
|
|
|
|
flagChosen = true
|
|
|
|
case 'x':
|
|
|
|
if flagChosen {
|
|
|
|
return fmt.Errorf("Cannot choose a flag multiple times! " + helpMessage)
|
|
|
|
}
|
|
|
|
// TODO: Support all flags?
|
|
|
|
switch opt.Value {
|
|
|
|
case "Seen":
|
|
|
|
flag = models.SeenFlag
|
|
|
|
flagName = "seen"
|
|
|
|
case "Answered":
|
|
|
|
flag = models.AnsweredFlag
|
|
|
|
flagName = "answered"
|
|
|
|
case "Flagged":
|
|
|
|
flag = models.FlaggedFlag
|
|
|
|
flagName = "flagged"
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unknown / Prohibited flag \"%v\"", opt.Value)
|
|
|
|
}
|
|
|
|
flagChosen = true
|
2019-07-15 19:51:07 +02:00
|
|
|
}
|
2019-06-09 20:55:34 +02:00
|
|
|
}
|
2020-07-05 16:29:52 +02:00
|
|
|
if toggle {
|
|
|
|
actionName = "Toggling"
|
|
|
|
} else if enable {
|
|
|
|
actionName = "Setting"
|
|
|
|
} else {
|
|
|
|
actionName = "Unsetting"
|
|
|
|
}
|
|
|
|
if optind != len(args) {
|
|
|
|
// Any non-option arguments: Error
|
|
|
|
return fmt.Errorf(helpMessage)
|
|
|
|
}
|
2019-06-09 20:55:34 +02:00
|
|
|
|
2019-12-22 12:05:28 +01:00
|
|
|
h := newHelper(aerc)
|
|
|
|
store, err := h.store()
|
2019-07-10 02:04:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-22 12:05:28 +01:00
|
|
|
|
2020-07-05 16:29:52 +02:00
|
|
|
// UIDs of messages to enable or disable the flag for.
|
|
|
|
var toEnable []uint32
|
|
|
|
var toDisable []uint32
|
2019-12-22 12:05:28 +01:00
|
|
|
|
2020-07-05 16:29:52 +02:00
|
|
|
if toggle {
|
|
|
|
// If toggling, split messages into those that need to
|
|
|
|
// be enabled / disabled.
|
|
|
|
msgs, err := h.messages()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, m := range msgs {
|
|
|
|
var enabled bool
|
|
|
|
for _, mFlag := range m.Flags {
|
|
|
|
if mFlag == flag {
|
|
|
|
enabled = true
|
|
|
|
break
|
|
|
|
}
|
2019-07-15 19:51:07 +02:00
|
|
|
}
|
2020-07-05 16:29:52 +02:00
|
|
|
if enabled {
|
|
|
|
toDisable = append(toDisable, m.Uid)
|
|
|
|
} else {
|
|
|
|
toEnable = append(toEnable, m.Uid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
msgUids, err := h.markedOrSelectedUids()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-07-15 19:51:07 +02:00
|
|
|
}
|
2020-07-05 16:29:52 +02:00
|
|
|
if enable {
|
|
|
|
toEnable = msgUids
|
2019-12-22 12:05:28 +01:00
|
|
|
} else {
|
2020-07-05 16:29:52 +02:00
|
|
|
toDisable = msgUids
|
2019-12-22 12:05:28 +01:00
|
|
|
}
|
2019-07-15 19:51:07 +02:00
|
|
|
}
|
2019-06-09 20:55:34 +02:00
|
|
|
|
2020-07-05 16:29:52 +02:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
success := true
|
|
|
|
|
|
|
|
if len(toEnable) != 0 {
|
|
|
|
submitFlagChange(aerc, store, toEnable, flag, true, &wg, &success)
|
|
|
|
}
|
|
|
|
if len(toDisable) != 0 {
|
|
|
|
submitFlagChange(aerc, store, toDisable, flag, false, &wg, &success)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to do flagging in the background, else we block the main thread
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
if success {
|
2020-07-17 17:50:24 +02:00
|
|
|
aerc.PushStatus(actionName+" flag '"+flagName+"' successful", 10*time.Second)
|
2019-06-09 20:55:34 +02:00
|
|
|
}
|
2020-07-05 16:29:52 +02:00
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
2019-12-22 12:05:28 +01:00
|
|
|
}
|
|
|
|
|
2020-07-05 16:29:52 +02:00
|
|
|
func submitFlagChange(aerc *widgets.Aerc, store *lib.MessageStore,
|
|
|
|
uids []uint32, flag models.Flag, newState bool,
|
|
|
|
wg *sync.WaitGroup, success *bool) {
|
|
|
|
store.Flag(uids, flag, newState, func(msg types.WorkerMessage) {
|
2019-12-22 12:05:28 +01:00
|
|
|
wg.Add(1)
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case *types.Done:
|
|
|
|
wg.Done()
|
|
|
|
case *types.Error:
|
2020-05-28 16:32:42 +02:00
|
|
|
aerc.PushError(" " + msg.Error.Error())
|
2019-12-22 12:05:28 +01:00
|
|
|
*success = false
|
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|