package main import "strings" // terminalImageRequest reads /image with one path and an optional caption. // Single and double quotes permit spaces in a path, with no shell expansion. An // invalid path argument becomes an empty path for the daemon to refuse. func terminalImageRequest(line string) (SocketRequest, bool) { rest, found := strings.CutPrefix(strings.TrimLeft(line, " \t"), "/image") if !found || (rest != "" && rest[0] != ' ' && rest[0] != '\t') { return SocketRequest{}, false } request := SocketRequest{Type: "image"} rest = strings.TrimLeft(rest, " \t") if rest == "" { return request, true } if rest[0] == '\'' || rest[0] == '"' { end := strings.IndexByte(rest[1:], rest[0]) if end < 0 { return request, true } request.Path = rest[1 : 1+end] rest = rest[end+2:] if rest != "" && rest[0] != ' ' && rest[0] != '\t' { request.Path = "" return request, true } } else { end := strings.IndexAny(rest, " \t") if end < 0 { request.Path = rest return request, true } request.Path = rest[:end] rest = rest[end:] } request.Text = strings.TrimLeft(rest, " \t") return request, true }