summary refs log blame commit diff stats
path: root/commands/set.go
blob: f5366ff312187c2fb2943c6ec1a83d266f12ed5c (plain) (tree)




































































                                                                   
package commands

import (
	"errors"
	"strings"

	"git.sr.ht/~sircmpwn/aerc/widgets"

	"github.com/go-ini/ini"
)

type Set struct{}

func setUsage() string {
	return "set <category>.<option> <value>"
}

func init() {
	register(Set{})
}

func (_ Set) Aliases() []string {
	return []string{"set"}

}

func (_ Set) Complete(aerc *widgets.Aerc, args []string) []string {
	return nil
}

func SetCore(aerc *widgets.Aerc, args []string) error {
	if len(args) != 3 {
		return errors.New("Usage: " + setUsage())
	}

	config := aerc.Config()

	parameters := strings.Split(args[1], ".")

	if len(parameters) != 2 {
		return errors.New("Usage: " + setUsage())
	}

	category := parameters[0]
	option := parameters[1]
	value := args[2]

	new_file := ini.Empty()

	section, err := new_file.NewSection(category)

	if err != nil {
		return nil
	}

	if _, err := section.NewKey(option, value); err != nil {
		return err
	}

	if err := config.LoadConfig(new_file); err != nil {
		return err
	}

	return nil
}

func (_ Set) Execute(aerc *widgets.Aerc, args []string) error {
	return SetCore(aerc, args)
}
lag from the message. func (m Message) MarkRead(seen bool) error { flags, err := m.Flags() if err != nil { return fmt.Errorf("could not read previous flags: %v", err) } if seen { flags = append(flags, maildir.FlagSeen) return m.SetFlags(flags) } var newFlags []maildir.Flag for _, flag := range flags { if flag != maildir.FlagSeen { newFlags = append(newFlags, flag) } } return m.SetFlags(newFlags) } // Remove deletes the email immediately. func (m Message) Remove() error { return m.dir.Remove(m.key) } // MessageInfo populates a models.MessageInfo struct for the message. func (m Message) MessageInfo() (*models.MessageInfo, error) { return lib.MessageInfo(m) } // NewBodyPartReader creates a new io.Reader for the requested body part(s) of // the message. func (m Message) NewBodyPartReader(requestedParts []int) (io.Reader, error) { f, err := m.dir.Open(m.key) if err != nil { return nil, err } defer f.Close() msg, err := message.Read(f) if err != nil { return nil, fmt.Errorf("could not read message: %v", err) } return lib.FetchEntityPartReader(msg, requestedParts) } var maildirToFlag = map[maildir.Flag]models.Flag{ maildir.FlagReplied: models.AnsweredFlag, maildir.FlagSeen: models.SeenFlag, maildir.FlagTrashed: models.DeletedFlag, maildir.FlagFlagged: models.FlaggedFlag, // maildir.FlagDraft Flag = 'D' // maildir.FlagPassed Flag = 'P' } var flagToMaildir = map[models.Flag]maildir.Flag{ models.AnsweredFlag: maildir.FlagReplied, models.SeenFlag: maildir.FlagSeen, models.DeletedFlag: maildir.FlagTrashed, models.FlaggedFlag: maildir.FlagFlagged, // maildir.FlagDraft Flag = 'D' // maildir.FlagPassed Flag = 'P' } func translateMaildirFlags(maildirFlags []maildir.Flag) []models.Flag { var flags []models.Flag for _, maildirFlag := range maildirFlags { if flag, ok := maildirToFlag[maildirFlag]; ok { flags = append(flags, flag) } } return flags } func translateFlags(flags []models.Flag) []maildir.Flag { var maildirFlags []maildir.Flag for _, flag := range flags { if maildirFlag, ok := flagToMaildir[flag]; ok { maildirFlags = append(maildirFlags, maildirFlag) } } return maildirFlags } func (m Message) UID() uint32 { return m.uid } func (m Message) Labels() ([]string, error) { return nil, nil }