Share quick settings menus and validated voice discovery
This commit is contained in:
1 parent
e7bd1d0043
commit
5ca137c879
5 files changed
+671
-28
No files matched your search
+119
-28
@@ -7,55 +7,146 @@ package speechdispatcher
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
type Voice struct{ Name, Language, Variant string }
|
||||
type Catalog map[string][]Voice
|
||||
|
||||
// Discover uses its own connection, so slow modules never hold up speech.
|
||||
// The empty module selects the server default.
|
||||
func Discover(ctx context.Context, socket string) (Catalog, error) {
|
||||
query, err := OpenQuery(ctx, socket, false, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
// Discovery is a complete catalog query. Modules lists the server's modules;
|
||||
// Voices holds each module's voices, keyed "" for the server default; Errors
|
||||
// records modules whose voice list could not be read.
|
||||
type Discovery struct {
|
||||
Modules []string
|
||||
Voices map[string][]Voice
|
||||
Errors map[string]error
|
||||
}
|
||||
|
||||
// ValidModule accepts module and language identifiers safe to send in SSIP.
|
||||
func ValidModule(module string) bool {
|
||||
for _, char := range module {
|
||||
if !(char >= 'a' && char <= 'z' || char >= 'A' && char <= 'Z' || char >= '0' && char <= '9' || char == '.' || char == '_' || char == '+' || char == '-') {
|
||||
return false
|
||||
}
|
||||
}
|
||||
defer query.Close()
|
||||
modules, err := query.Command(ctx, "LIST OUTPUT_MODULES")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return true
|
||||
}
|
||||
|
||||
// ValidSelection rejects identifiers that could break an SSIP command line.
|
||||
func ValidSelection(module, language, voice string) error {
|
||||
if !ValidModule(module) || !ValidModule(language) {
|
||||
return fmt.Errorf("invalid speech module or language")
|
||||
}
|
||||
catalog := Catalog{}
|
||||
for _, module := range append([]string{""}, modules...) {
|
||||
if strings.ContainsAny(module, "\r\n\x00") {
|
||||
if !utf8.ValidString(voice) || len(voice) > 1024 || voice != "" && strings.TrimSpace(voice) == "" {
|
||||
return fmt.Errorf("invalid speech voice")
|
||||
}
|
||||
for _, char := range voice {
|
||||
if unicode.IsControl(char) {
|
||||
return fmt.Errorf("invalid speech voice")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ParseVoices validates a LIST SYNTHESIS_VOICES reply. Some modules repeat
|
||||
// aliases; the synthesis name is the stable ID, so repeats are dropped.
|
||||
func ParseVoices(rows []string) ([]Voice, error) {
|
||||
voices := make([]Voice, 0, len(rows))
|
||||
seen := map[string]bool{}
|
||||
for _, row := range rows {
|
||||
fields := strings.Split(row, "\t")
|
||||
if len(fields) < 2 || len(fields) > 3 || fields[0] == "" || fields[1] == "" {
|
||||
return nil, fmt.Errorf("invalid speech voice list")
|
||||
}
|
||||
if err := ValidSelection("", fields[1], fields[0]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if seen[fields[0]] {
|
||||
continue
|
||||
}
|
||||
seen[fields[0]] = true
|
||||
voice := Voice{Name: fields[0], Language: fields[1]}
|
||||
if len(fields) == 3 {
|
||||
voice.Variant = fields[2]
|
||||
}
|
||||
voices = append(voices, voice)
|
||||
}
|
||||
return voices, nil
|
||||
}
|
||||
|
||||
// DiscoverQuery never speaks. Partial failures keep the modules that
|
||||
// succeeded and record the others in Errors. The caller bounds ctx.
|
||||
func DiscoverQuery(ctx context.Context, query *Query) (Discovery, error) {
|
||||
result := Discovery{Voices: map[string][]Voice{}, Errors: map[string]error{}}
|
||||
modules, err := query.Command(ctx, "LIST OUTPUT_MODULES")
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, module := range modules {
|
||||
if module == "" || !ValidModule(module) || seen[module] {
|
||||
return result, fmt.Errorf("invalid Speech Dispatcher module list")
|
||||
}
|
||||
seen[module] = true
|
||||
}
|
||||
result.Modules = modules
|
||||
// Query the server default before selecting any named module.
|
||||
for _, module := range append([]string{""}, modules...) {
|
||||
var rows []string
|
||||
var err error
|
||||
if module != "" {
|
||||
_, err = query.Command(ctx, "SET SELF OUTPUT_MODULE "+module)
|
||||
}
|
||||
if err == nil {
|
||||
rows, err = query.Command(ctx, "LIST SYNTHESIS_VOICES")
|
||||
}
|
||||
if err != nil {
|
||||
var rejected *Rejection
|
||||
if errors.As(err, &rejected) {
|
||||
err = nil
|
||||
if err == nil {
|
||||
voices, parseErr := ParseVoices(rows)
|
||||
if parseErr != nil {
|
||||
// The reply was fully consumed; subsequent modules remain safe.
|
||||
result.Errors[module] = parseErr
|
||||
continue
|
||||
}
|
||||
return catalog, err
|
||||
result.Voices[module] = voices
|
||||
}
|
||||
catalog[module] = nil
|
||||
for _, row := range rows {
|
||||
fields := strings.Split(row, "\t")
|
||||
if len(fields) < 2 {
|
||||
continue
|
||||
if err != nil {
|
||||
delete(result.Voices, module)
|
||||
result.Errors[module] = err
|
||||
// A rejected command ends a complete reply. A broken stream must
|
||||
// never be reused: a late reply could belong to the previous module.
|
||||
var rejected *Rejection
|
||||
if !errors.As(err, &rejected) {
|
||||
break
|
||||
}
|
||||
voice := Voice{Name: fields[0], Language: fields[1]}
|
||||
if len(fields) > 2 {
|
||||
voice.Variant = fields[2]
|
||||
}
|
||||
catalog[module] = append(catalog[module], voice)
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// Discover uses its own connection, so slow modules never hold up speech.
|
||||
// The empty module selects the server default. Modules whose voices could
|
||||
// not be read are omitted; a broken connection returns what was read.
|
||||
func Discover(ctx context.Context, socket string) (Catalog, error) {
|
||||
query, err := OpenQuery(ctx, socket, false, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer query.Close()
|
||||
discovery, err := DiscoverQuery(ctx, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
catalog := Catalog{}
|
||||
for module, voices := range discovery.Voices {
|
||||
catalog[module] = voices
|
||||
}
|
||||
for _, err := range discovery.Errors {
|
||||
var rejected *Rejection
|
||||
if !errors.As(err, &rejected) {
|
||||
return catalog, err
|
||||
}
|
||||
}
|
||||
return catalog, nil
|
||||
|
||||
@@ -41,3 +41,65 @@ func TestDiscoverVoicesAndOutputModule(t *testing.T) {
|
||||
t.Fatal("echo voice/module not configured")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseVoicesRejectsMalformedRowsAndKeepsNames(t *testing.T) {
|
||||
for _, row := range []string{"Missing tabs", "name\ten\tvariant\textra", "name\ten\rCANCEL ALL", "bad\x00name\ten"} {
|
||||
if _, err := ParseVoices([]string{row}); err == nil {
|
||||
t.Fatal("accepted malformed catalog row", row)
|
||||
}
|
||||
}
|
||||
voices, err := ParseVoices([]string{"Cherokee \tchr\tnone", "Alias\ten", "Alias\ten-US"})
|
||||
if err != nil || len(voices) != 2 || voices[0].Name != "Cherokee " || voices[1].Language != "en" {
|
||||
t.Fatal(voices, err)
|
||||
}
|
||||
if err := ValidSelection("espeak-ng", "chr", "Cherokee "); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ValidSelection("espeak-ng", "chr", " "); err == nil {
|
||||
t.Fatal("accepted whitespace-only name")
|
||||
}
|
||||
if err := ValidSelection("bad module", "", ""); err == nil {
|
||||
t.Fatal("accepted unsafe module")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiscoverRejectsUnsafeModuleList(t *testing.T) {
|
||||
path, _ := newSettingsServer(t, func(command string) string {
|
||||
if command == "LIST OUTPUT_MODULES" {
|
||||
return "200-good\r\n200-bad module\r\n200 OK\r\n"
|
||||
}
|
||||
return "200 OK\r\n"
|
||||
})
|
||||
if _, err := Discover(context.Background(), path); err == nil {
|
||||
t.Fatal("accepted an unsafe module name")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiscoveryContinuesAfterMalformedVoiceList(t *testing.T) {
|
||||
module := ""
|
||||
path, _ := newSettingsServer(t, func(command string) string {
|
||||
switch command {
|
||||
case "LIST OUTPUT_MODULES":
|
||||
return "200-broken\r\n200-good\r\n200 OK\r\n"
|
||||
case "SET SELF OUTPUT_MODULE broken":
|
||||
module = "broken"
|
||||
case "SET SELF OUTPUT_MODULE good":
|
||||
module = "good"
|
||||
case "LIST SYNTHESIS_VOICES":
|
||||
if module == "broken" {
|
||||
return "200-malformed\r\n200 OK\r\n"
|
||||
}
|
||||
return "200-Voice\ten\tnone\r\n200 OK\r\n"
|
||||
}
|
||||
return "200 OK\r\n"
|
||||
})
|
||||
query, err := OpenQuery(context.Background(), path, false, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer query.Close()
|
||||
catalog, err := DiscoverQuery(context.Background(), query)
|
||||
if err != nil || catalog.Errors["broken"] == nil || len(catalog.Voices["good"]) != 1 {
|
||||
t.Fatal(catalog, err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user