aboutsummaryrefslogtreecommitdiffstats
path: root/commands
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2022-02-25 00:21:06 +0100
committerRobin Jarry <robin@jarry.cc>2022-02-25 13:56:53 +0100
commitc26d08103b327bd3f2470e542aa55ab354483347 (patch)
tree4c0567246916d676b71a4675c76a93a198add65f /commands
parent91ead11c47252ca8998fdad7b0770d6b8d07c987 (diff)
downloadaerc-c26d08103b327bd3f2470e542aa55ab354483347.tar.gz
aerc: always check SelectedAccount return value
aerc.SelectedAccount() is used in lots of places. Most of them without checking the return value. In some cases, the currently selected tab is not related to any account (widget.Terminal for example). This can lead to unexpected crashes when accessing account specific configuration. When possible, return an error when no account is currently selected. If no error can be returned, fallback to non-account specific configuration. Signed-off-by: Robin Jarry <robin@jarry.cc> Reviewed-by: Koni Marti <koni.marti@gmail.com>
Diffstat (limited to 'commands')
-rw-r--r--commands/account/compose.go3
-rw-r--r--commands/account/view.go3
-rw-r--r--commands/commands.go18
-rw-r--r--commands/compose/postpone.go6
-rw-r--r--commands/msg/unsubscribe.go3
-rw-r--r--commands/msgview/next.go5
6 files changed, 32 insertions, 6 deletions
diff --git a/commands/account/compose.go b/commands/account/compose.go
index d4128db6..1a62d0a3 100644
--- a/commands/account/compose.go
+++ b/commands/account/compose.go
@@ -30,6 +30,9 @@ func (Compose) Execute(aerc *widgets.Aerc, args []string) error {
return err
}
acct := aerc.SelectedAccount()
+ if acct == nil {
+ return errors.New("No account selected")
+ }
if template == "" {
template = aerc.Config().Templates.NewMessage
}
diff --git a/commands/account/view.go b/commands/account/view.go
index 4f59d947..53ad2678 100644
--- a/commands/account/view.go
+++ b/commands/account/view.go
@@ -26,6 +26,9 @@ func (ViewMessage) Execute(aerc *widgets.Aerc, args []string) error {
return errors.New("Usage: view-message")
}
acct := aerc.SelectedAccount()
+ if acct == nil {
+ return errors.New("No account selected")
+ }
if acct.Messages().Empty() {
return nil
}
diff --git a/commands/commands.go b/commands/commands.go
index cb5b63bf..70a77b92 100644
--- a/commands/commands.go
+++ b/commands/commands.go
@@ -117,11 +117,15 @@ func (cmds *Commands) GetCompletions(aerc *widgets.Aerc, cmd string) []string {
func GetFolders(aerc *widgets.Aerc, args []string) []string {
out := make([]string, 0)
+ acct := aerc.SelectedAccount()
+ if acct == nil {
+ return out
+ }
if len(args) == 0 {
- return aerc.SelectedAccount().Directories().List()
+ return acct.Directories().List()
}
- for _, dir := range aerc.SelectedAccount().Directories().List() {
- if foundInString(dir, args[0], aerc.SelectedAccount().UiConfig().FuzzyFolderComplete) {
+ for _, dir := range acct.Directories().List() {
+ if foundInString(dir, args[0], acct.UiConfig().FuzzyFolderComplete) {
out = append(out, dir)
}
}
@@ -144,8 +148,12 @@ func CompletionFromList(valid []string, args []string) []string {
}
func GetLabels(aerc *widgets.Aerc, args []string) []string {
+ acct := aerc.SelectedAccount()
+ if acct == nil {
+ return make([]string, 0)
+ }
if len(args) == 0 {
- return aerc.SelectedAccount().Labels()
+ return acct.Labels()
}
// + and - are used to denote tag addition / removal and need to be striped
@@ -165,7 +173,7 @@ func GetLabels(aerc *widgets.Aerc, args []string) []string {
trimmed := strings.TrimLeft(last, "+-")
out := make([]string, 0)
- for _, label := range aerc.SelectedAccount().Labels() {
+ for _, label := range acct.Labels() {
if hasCaseSmartPrefix(label, trimmed) {
var prev string
if len(others) > 0 {
diff --git a/commands/compose/postpone.go b/commands/compose/postpone.go
index f918b527..9462a9cd 100644
--- a/commands/compose/postpone.go
+++ b/commands/compose/postpone.go
@@ -31,6 +31,10 @@ func (Postpone) Execute(aerc *widgets.Aerc, args []string) error {
if len(args) != 1 {
return errors.New("Usage: postpone")
}
+ acct := aerc.SelectedAccount()
+ if acct == nil {
+ return errors.New("No account selected")
+ }
composer, _ := aerc.SelectedTab().(*widgets.Composer)
config := composer.Config()
tabName := aerc.TabNames()[aerc.SelectedTabIndex()]
@@ -48,7 +52,7 @@ func (Postpone) Execute(aerc *widgets.Aerc, args []string) error {
header.SetContentType("text/plain", map[string]string{"charset": "UTF-8"})
header.Set("Content-Transfer-Encoding", "quoted-printable")
worker := composer.Worker()
- dirs := aerc.SelectedAccount().Directories().List()
+ dirs := acct.Directories().List()
alreadyCreated := false
for _, dir := range dirs {
if dir == config.Postpone {
diff --git a/commands/msg/unsubscribe.go b/commands/msg/unsubscribe.go
index 3a69ff98..428e8728 100644
--- a/commands/msg/unsubscribe.go
+++ b/commands/msg/unsubscribe.go
@@ -89,6 +89,9 @@ func parseUnsubscribeMethods(header string) (methods []*url.URL) {
func unsubscribeMailto(aerc *widgets.Aerc, u *url.URL) error {
widget := aerc.SelectedTab().(widgets.ProvidesMessage)
acct := widget.SelectedAccount()
+ if acct == nil {
+ return errors.New("No account selected")
+ }
h := &mail.Header{}
h.SetSubject(u.Query().Get("subject"))
diff --git a/commands/msgview/next.go b/commands/msgview/next.go
index 4291a6a5..742bc665 100644
--- a/commands/msgview/next.go
+++ b/commands/msgview/next.go
@@ -1,6 +1,8 @@
package msgview
import (
+ "errors"
+
"git.sr.ht/~rjarry/aerc/commands/account"
"git.sr.ht/~rjarry/aerc/lib"
"git.sr.ht/~rjarry/aerc/widgets"
@@ -27,6 +29,9 @@ func (NextPrevMsg) Execute(aerc *widgets.Aerc, args []string) error {
}
mv, _ := aerc.SelectedTab().(*widgets.MessageViewer)
acct := mv.SelectedAccount()
+ if acct == nil {
+ return errors.New("No account selected")
+ }
store := mv.Store()
err = account.ExecuteNextPrevMessage(args, acct, pct, n)
if err != nil {