1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
diff --git a/CHANGELOG.md b/CHANGELOG.md
index dd8e3259c317b1d85832928aa0dccf6ee55971d1..4551909e9c44a868ce22021ca75ba5cacd9923d3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -61,6 +61,7 @@ ```
*.default=true
*.normal=true
```
+- Openers commands are not executed in with `sh -c`.
### Deprecated
diff --git a/commands/msg/unsubscribe.go b/commands/msg/unsubscribe.go
index a489b3b9de74aee8fd1894c5360f276c68e65353..6fef16f5b142b23c8cabace01bd259b5b237237b 100644
--- a/commands/msg/unsubscribe.go
+++ b/commands/msg/unsubscribe.go
@@ -180,7 +180,7 @@ case "Yes":
go func() {
defer log.PanicHandler()
mime := fmt.Sprintf("x-scheme-handler/%s", u.Scheme)
- if err := lib.XDGOpenMime(u.String(), mime, nil); err != nil {
+ if err := lib.XDGOpenMime(u.String(), mime, ""); err != nil {
app.PushError("Unsubscribe:" + err.Error())
}
}()
diff --git a/commands/msgview/open-link.go b/commands/msgview/open-link.go
index ad2a7cc28fe4e730456abd8a253cd39f6cbe0a4b..55aee08c1a3ccae67fa2873264d79708b086fb6f 100644
--- a/commands/msgview/open-link.go
+++ b/commands/msgview/open-link.go
@@ -12,7 +12,7 @@ )
type OpenLink struct {
Url *url.URL `opt:"url" action:"ParseUrl"`
- Cmd []string `opt:"..." required:"false"`
+ Cmd string `opt:"..." required:"false"`
}
func init() {
diff --git a/commands/msgview/open.go b/commands/msgview/open.go
index bab46bd715546e9c76a65b16966505a20fb10f96..0d6db5b0142a3b751f3f796fe213873efee2a70d 100644
--- a/commands/msgview/open.go
+++ b/commands/msgview/open.go
@@ -13,8 +13,8 @@ "git.sr.ht/~rjarry/aerc/log"
)
type Open struct {
- Delete bool `opt:"-d"`
- Cmd []string `opt:"..." required:"false"`
+ Delete bool `opt:"-d"`
+ Cmd string `opt:"..." required:"false"`
}
func init() {
diff --git a/config/aerc.conf b/config/aerc.conf
index 18c6decceefb4da0132d455edf19ba99fa069365..06cfb2d1e8c8ff2bc893ba836ed1084f4546ddb9 100644
--- a/config/aerc.conf
+++ b/config/aerc.conf
@@ -548,9 +548,10 @@ # Openers allow you to specify the command to use for the :open and :open-link
# actions on a per-MIME-type basis. The :open-link URL scheme is used to
# determine the MIME type as follows: x-scheme-handler/<scheme>.
#
-# {} is expanded as the temporary filename to be opened. If it is not
-# encountered in the command, the temporary filename will be appened to the end
-# of the command.
+# {} is expanded as the temporary filename or URL to be opened with proper
+# shell quoting. If it is not encountered in the command, the filename/URL will
+# be appended to the end of the command. The command will then be executed with
+# `sh -c`.
#
# Like [filters], openers support basic shell globbing. The first opener which
# matches the part's MIME type (or URL scheme handler MIME type) will be used,
@@ -558,7 +559,7 @@ # so order them from most to least specific.
#
# Examples:
# x-scheme-handler/irc=hexchat
-# x-scheme-handler/http*=firefox
+# x-scheme-handler/http*=printf '%s' {} | wl-copy
# text/html=surf -dfgms
# text/plain=gvim {} +125
# message/rfc822=thunderbird
diff --git a/config/openers.go b/config/openers.go
index e246ea64fcce50f5db1dc6fb9098e2e2ee13c6f3..31d99e9c1a8dd2fe93024ed92edf3a08102051e7 100644
--- a/config/openers.go
+++ b/config/openers.go
@@ -1,17 +1,15 @@
package config
import (
- "fmt"
"strings"
"git.sr.ht/~rjarry/aerc/log"
"github.com/go-ini/ini"
- "github.com/google/shlex"
)
type Opener struct {
Mime string
- Args []string
+ Args string
}
var Openers []Opener
@@ -24,14 +22,7 @@ }
for _, key := range openers.Keys() {
mime := strings.ToLower(key.Name())
- if args, err := shlex.Split(key.Value()); err != nil {
- return err
- } else {
- if len(args) == 0 {
- return fmt.Errorf("opener command empty for %s", mime)
- }
- Openers = append(Openers, Opener{Mime: mime, Args: args})
- }
+ Openers = append(Openers, Opener{Mime: mime, Args: key.Value()})
}
out:
diff --git a/doc/aerc-config.5.scd b/doc/aerc-config.5.scd
index 0e86e56bb859191938dd771030924b14581478ae..a45e244394e5e1704ec3100f5cb9179548b431a9 100644
--- a/doc/aerc-config.5.scd
+++ b/doc/aerc-config.5.scd
@@ -900,9 +900,10 @@ actions on a per-MIME-type basis. The *:open-link* URL scheme is used to
determine the MIME type as follows: _x-scheme-handler/<scheme>_. They are
configured in the *[openers]* section of _aerc.conf_.
-_{}_ is expanded as the temporary filename or URL to be opened. If it is not
-encountered in the command, the filename/URL will be appened to the end of the
-command. Environment variables are also expanded. Tilde is not expanded.
+_{}_ is expanded as the temporary filename or URL to be opened with proper shell
+quoting. If it is not encountered in the command, the filename/URL will be
+appended to the end of the command. The command will then be executed with
+_sh -c_.
Like *[filters]*, openers support basic shell globbing. The first opener which
matches the part's MIME type (or URL scheme handler MIME type) will be used, so
@@ -913,7 +914,7 @@
```
[openers]
x-scheme-handler/irc=hexchat
-x-scheme-handler/http\*=firefox
+x-scheme-handler/http\*=printf '%s' {} | wl-copy
text/html=surf -dfgms
text/plain=gvim {} +125
message/rfc822=thunderbird
diff --git a/lib/open.go b/lib/open.go
index b60c416519c542aa4a6136167b93fed1caae991b..21bee748126823389deb1e37a1ddb89adc0dcb69 100644
--- a/lib/open.go
+++ b/lib/open.go
@@ -6,19 +6,21 @@ "os/exec"
"runtime"
"strings"
+ "git.sr.ht/~rjarry/go-opt"
+ "github.com/danwakefield/fnmatch"
+
"git.sr.ht/~rjarry/aerc/config"
"git.sr.ht/~rjarry/aerc/log"
- "github.com/danwakefield/fnmatch"
)
func XDGOpenMime(
- uri string, mimeType string, args []string,
+ uri string, mimeType string, args string,
) error {
if len(args) == 0 {
// no explicit command provided, lookup opener from mime type
for _, o := range config.Openers {
if fnmatch.Match(o.Mime, mimeType, 0) {
- args = append(args, o.Args...)
+ args = o.Args
break
}
}
@@ -26,28 +28,24 @@ }
if len(args) == 0 {
// no opener defined in config, fallback to default
if runtime.GOOS == "darwin" {
- args = append(args, "open")
+ args = "open"
} else {
- args = append(args, "xdg-open")
+ args = "xdg-open"
}
}
- i := 0
- for ; i < len(args); i++ {
- if strings.Contains(args[i], "{}") {
- break
- }
- }
- if i < len(args) {
+ // Escape URI special characters
+ uri = opt.QuoteArg(uri)
+ if strings.Contains(args, "{}") {
// found {} placeholder in args, replace with uri
- args[i] = strings.Replace(args[i], "{}", uri, 1)
+ args = strings.Replace(args, "{}", uri, 1)
} else {
// no {} placeholder in args, add uri at the end
- args = append(args, uri)
+ args = args + " " + uri
}
log.Tracef("running command: %v", args)
- cmd := exec.Command(args[0], args[1:]...)
+ cmd := exec.Command("sh", "-c", args)
out, err := cmd.CombinedOutput()
log.Debugf("command: %v exited. err=%v out=%s", args, err, out)
if err != nil {
|