]> git.giacomolli.uk - onechat/blob - terminal_image.go
Log image and message sends
[onechat] / terminal_image.go
1 package main
2
3 import "strings"
4
5 // terminalImageRequest reads /image with one path and an optional caption.
6 // Single and double quotes permit spaces in a path, with no shell expansion. An
7 // invalid path argument becomes an empty path for the daemon to refuse.
8 func terminalImageRequest(line string) (SocketRequest, bool) {
9 rest, found := strings.CutPrefix(strings.TrimLeft(line, " \t"), "/image")
10 if !found || (rest != "" && rest[0] != ' ' && rest[0] != '\t') {
11 return SocketRequest{}, false
12 }
13
14 request := SocketRequest{Type: "image"}
15 rest = strings.TrimLeft(rest, " \t")
16 if rest == "" {
17 return request, true
18 }
19
20 if rest[0] == '\'' || rest[0] == '"' {
21 end := strings.IndexByte(rest[1:], rest[0])
22 if end < 0 {
23 return request, true
24 }
25
26 request.Path = rest[1 : 1+end]
27 rest = rest[end+2:]
28 if rest != "" && rest[0] != ' ' && rest[0] != '\t' {
29 request.Path = ""
30 return request, true
31 }
32 } else {
33 end := strings.IndexAny(rest, " \t")
34 if end < 0 {
35 request.Path = rest
36 return request, true
37 }
38
39 request.Path = rest[:end]
40 rest = rest[end:]
41 }
42
43 request.Text = strings.TrimLeft(rest, " \t")
44
45 return request, true
46 }