summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDaniel Bridges <bridges2@gmail.com>2019-07-09 17:04:21 -0700
committerDrew DeVault <sir@cmpwn.com>2019-07-10 13:21:38 -0400
commit217e85a55d0c1047bef1e2bc41783ccd4629bfc1 (patch)
tree5e06a60ccc27e59e4eef63315cea5065b5c3e8c6
parent3f30c27bb31f26ca639b7a87d54a7c522bbe8be0 (diff)
downloadaerc-217e85a55d0c1047bef1e2bc41783ccd4629bfc1.tar.gz
Fix crashes when operating on empty folder (#216)
-rw-r--r--commands/msg/archive.go5
-rw-r--r--commands/msg/copy.go5
-rw-r--r--commands/msg/delete.go6
-rw-r--r--commands/msg/move.go5
-rw-r--r--commands/msg/pipe.go5
-rw-r--r--commands/msg/read.go5
-rw-r--r--commands/msg/reply.go5
-rw-r--r--commands/msg/unsubscribe.go6
-rw-r--r--widgets/account.go8
-rw-r--r--widgets/msgviewer.go8
-rw-r--r--widgets/providesmessage.go2
11 files changed, 45 insertions, 15 deletions
diff --git a/commands/msg/archive.go b/commands/msg/archive.go
index 40fb48b..fe391d2 100644
--- a/commands/msg/archive.go
+++ b/commands/msg/archive.go
@@ -41,7 +41,10 @@ func (_ Archive) Execute(aerc *widgets.Aerc, args []string) error {
 	if acct == nil {
 		return errors.New("No account selected")
 	}
-	msg := widget.SelectedMessage()
+	msg, err := widget.SelectedMessage()
+	if err != nil {
+		return err
+	}
 	store := widget.Store()
 	archiveDir := acct.AccountConfig().Archive
 	store.Next()
diff --git a/commands/msg/copy.go b/commands/msg/copy.go
index 4d65d24..48bccd4 100644
--- a/commands/msg/copy.go
+++ b/commands/msg/copy.go
@@ -44,7 +44,10 @@ func (_ Copy) Execute(aerc *widgets.Aerc, args []string) error {
 	}
 
 	widget := aerc.SelectedTab().(widgets.ProvidesMessage)
-	msg := widget.SelectedMessage()
+	msg, err := widget.SelectedMessage()
+	if err != nil {
+		return err
+	}
 	store := widget.Store()
 	store.Copy([]uint32{msg.Uid}, args[optind], createParents, func(
 		msg types.WorkerMessage) {
diff --git a/commands/msg/delete.go b/commands/msg/delete.go
index 104e1e1..36b8d8c 100644
--- a/commands/msg/delete.go
+++ b/commands/msg/delete.go
@@ -35,10 +35,10 @@ func (_ Delete) Execute(aerc *widgets.Aerc, args []string) error {
 		return errors.New("No account selected")
 	}
 	store := widget.Store()
-	if len(store.Uids) == 0 {
-		return errors.New("No message selected")
+	msg, err := widget.SelectedMessage()
+	if err != nil {
+		return err
 	}
-	msg := widget.SelectedMessage()
 	_, isMsgView := widget.(*widgets.MessageViewer)
 	if isMsgView {
 		aerc.RemoveTab(widget)
diff --git a/commands/msg/move.go b/commands/msg/move.go
index c98157e..8b19a10 100644
--- a/commands/msg/move.go
+++ b/commands/msg/move.go
@@ -49,7 +49,10 @@ func (_ Move) Execute(aerc *widgets.Aerc, args []string) error {
 	if acct == nil {
 		return errors.New("No account selected")
 	}
-	msg := widget.SelectedMessage()
+	msg, err := widget.SelectedMessage()
+	if err != nil {
+		return err
+	}
 	store := widget.Store()
 	_, isMsgView := widget.(*widgets.MessageViewer)
 	if isMsgView {
diff --git a/commands/msg/pipe.go b/commands/msg/pipe.go
index 9f4a009..821f359 100644
--- a/commands/msg/pipe.go
+++ b/commands/msg/pipe.go
@@ -112,7 +112,10 @@ func (_ Pipe) Execute(aerc *widgets.Aerc, args []string) error {
 
 	if pipeFull {
 		store := provider.Store()
-		msg := provider.SelectedMessage()
+		msg, err := provider.SelectedMessage()
+		if err != nil {
+			return err
+		}
 		store.FetchFull([]uint32{msg.Uid}, func(reader io.Reader) {
 			if background {
 				doExec(reader)
diff --git a/commands/msg/read.go b/commands/msg/read.go
index db463f1..30c6822 100644
--- a/commands/msg/read.go
+++ b/commands/msg/read.go
@@ -30,7 +30,10 @@ func (_ Read) Execute(aerc *widgets.Aerc, args []string) error {
 	}
 
 	widget := aerc.SelectedTab().(widgets.ProvidesMessage)
-	msg := widget.SelectedMessage()
+	msg, err := widget.SelectedMessage()
+	if err != nil {
+		return err
+	}
 	store := widget.Store()
 	store.Read([]uint32{msg.Uid}, args[0] == "read", func(
 		msg types.WorkerMessage) {
diff --git a/commands/msg/reply.go b/commands/msg/reply.go
index 68c2089..5295ee6 100644
--- a/commands/msg/reply.go
+++ b/commands/msg/reply.go
@@ -60,7 +60,10 @@ func (_ reply) Execute(aerc *widgets.Aerc, args []string) error {
 	conf := acct.AccountConfig()
 	us, _ := gomail.ParseAddress(conf.From)
 	store := widget.Store()
-	msg := widget.SelectedMessage()
+	msg, err := widget.SelectedMessage()
+	if err != nil {
+		return err
+	}
 	acct.Logger().Println("Replying to email " + msg.Envelope.MessageId)
 
 	var (
diff --git a/commands/msg/unsubscribe.go b/commands/msg/unsubscribe.go
index d4a7e9a..720ff43 100644
--- a/commands/msg/unsubscribe.go
+++ b/commands/msg/unsubscribe.go
@@ -34,7 +34,11 @@ func (Unsubscribe) Execute(aerc *widgets.Aerc, args []string) error {
 		return errors.New("Usage: unsubscribe")
 	}
 	widget := aerc.SelectedTab().(widgets.ProvidesMessage)
-	headers := widget.SelectedMessage().RFC822Headers
+	msg, err := widget.SelectedMessage()
+	if err != nil {
+		return err
+	}
+	headers := msg.RFC822Headers
 	if !headers.Has("list-unsubscribe") {
 		return errors.New("No List-Unsubscribe header found")
 	}
diff --git a/widgets/account.go b/widgets/account.go
index e08a253..981a143 100644
--- a/widgets/account.go
+++ b/widgets/account.go
@@ -1,6 +1,7 @@
 package widgets
 
 import (
+	"errors"
 	"fmt"
 	"log"
 
@@ -170,8 +171,11 @@ func (acct *AccountView) SelectedAccount() *AccountView {
 	return acct
 }
 
-func (acct *AccountView) SelectedMessage() *models.MessageInfo {
-	return acct.msglist.Selected()
+func (acct *AccountView) SelectedMessage() (*models.MessageInfo, error) {
+	if len(acct.msglist.Store().Uids) == 0 {
+		return nil, errors.New("no message selected")
+	}
+	return acct.msglist.Selected(), nil
 }
 
 func (acct *AccountView) SelectedMessagePart() *PartInfo {
diff --git a/widgets/msgviewer.go b/widgets/msgviewer.go
index f6bef2d..f15fbae 100644
--- a/widgets/msgviewer.go
+++ b/widgets/msgviewer.go
@@ -2,6 +2,7 @@ package widgets
 
 import (
 	"bufio"
+	"errors"
 	"fmt"
 	"io"
 	"os/exec"
@@ -211,8 +212,11 @@ func (mv *MessageViewer) SelectedAccount() *AccountView {
 	return mv.acct
 }
 
-func (mv *MessageViewer) SelectedMessage() *models.MessageInfo {
-	return mv.msg
+func (mv *MessageViewer) SelectedMessage() (*models.MessageInfo, error) {
+	if mv.msg == nil {
+		return nil, errors.New("no message selected")
+	}
+	return mv.msg, nil
 }
 
 func (mv *MessageViewer) ToggleHeaders() {
diff --git a/widgets/providesmessage.go b/widgets/providesmessage.go
index a1cfaa8..34b9011 100644
--- a/widgets/providesmessage.go
+++ b/widgets/providesmessage.go
@@ -17,6 +17,6 @@ type ProvidesMessage interface {
 	ui.Drawable
 	Store() *lib.MessageStore
 	SelectedAccount() *AccountView
-	SelectedMessage() *models.MessageInfo
+	SelectedMessage() (*models.MessageInfo, error)
 	SelectedMessagePart() *PartInfo
 }