This commit give aerc the ability to read commands through a FIFO
(named pipe). A command line flag is added (`-f`) which specifies the
path of the FIFO to read commands from. If no -f option is specified,
aerc doesn't read from any FIFO.
Again, please get rid of the FIFO, we already have a socket.
There's no need for two control handles.
The socket code is in lib/socket.go
So far it's pretty simple, if you connect and send a message with mailto:$url
it'll start the mailto handler.
We could introduce a new verb, say execute, again delimited from the command with
":" so that the parsing stays easy.
By that time, you'd also want to respond with either result: ok or result: error
depending on the outcome I guess, so we might need to tweak the mailto response
a bit to keep consistent.
Cheers,
Reto
---
aerc.go | 22 +++++++++++++++++++++-
widgets/aerc.go | 22 ++++++++++++++++++++++
2 files changed, 43 insertions(+), 1 deletion(-)
diff --git a/aerc.go b/aerc.go
index 33acb43..573931d 100644
--- a/aerc.go
+++ b/aerc.go
@@ -95,14 +95,17 @@ func usage() {
}
func main() {
- opts, optind, err := getopt.Getopts(os.Args, "v")
+ opts, optind, err := getopt.Getopts(os.Args, "f:v")
if err != nil {
log.Print(err)
usage()
return
}
+ fifopath := ""
for _, opt := range opts {
switch opt.Option {
+ case 'f':
+ fifopath = opt.Value
case 'v':
fmt.Println("aerc " + Version)
return
@@ -162,6 +165,23 @@ func main() {
return getCompletions(aerc, cmd)
}, &commands.CmdHistory)
+ if len(fifopath) > 0 {
+ // check that fifo exists
+ fileinfo, err := os.Stat(fifopath)
+ if err != nil && os.IsNotExist(err) {
+ fmt.Fprintf(os.Stderr, "Fatal: Provided FIFO path doesn't exist\n")
+ os.Exit(1)
+ } else if err != nil {
+ fmt.Fprintf(os.Stderr, "Fatal: Error getting FIFO status: %v\n", err)
+ os.Exit(1)
+ } else if fileinfo.Mode() & os.ModeNamedPipe == 0 {
+ fmt.Fprintf(os.Stderr, "Fatal: File isn't a FIFO\n")
+ os.Exit(1)
+ }
+
+ go aerc.ListenFifo(fifopath)
+ }
+
ui, err = libui.Initialize(aerc)
if err != nil {
panic(err)
diff --git a/widgets/aerc.go b/widgets/aerc.go
index 6df0c95..3b92ebe 100644
--- a/widgets/aerc.go
+++ b/widgets/aerc.go
@@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io"
+ "io/ioutil"
"log"
"net/url"
"strings"
@@ -424,6 +425,27 @@ func (aerc *Aerc) focus(item ui.Interactive) {
}
}
+func (aerc *Aerc) ListenFifo(fifopath string) {
+ // read+exec loop
+ for {
+ data, err := ioutil.ReadFile(fifopath)
+ if err != nil {
+ aerc.PushError(err.Error())
+ continue
+ }
+
+ cmd := string(data)
+ parts, err := shlex.Split(cmd)
+ if err != nil {
+ aerc.PushError(err.Error())
+ }
+ err = aerc.cmd(parts)
+ if err != nil {
+ aerc.PushError(err.Error())
+ }
+ }
+}
+
func (aerc *Aerc) BeginExCommand(cmd string) {
previous := aerc.focused
exline := NewExLine(aerc.conf, cmd, func(cmd string) {
--
2.31.0
Hi Sebastian,
On Mon, Mar 29, 2021 at 01:51:12PM -0400, Sebastian wrote: