2024-09-09 03:11:50 +09:00
|
|
|
package dbus
|
|
|
|
|
2024-09-09 21:13:00 +09:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-09-09 03:11:50 +09:00
|
|
|
type Config struct {
|
2024-09-09 13:27:31 +09:00
|
|
|
// See set 'see' policy for NAME (--see=NAME)
|
|
|
|
See []string `json:"see"`
|
|
|
|
// Talk set 'talk' policy for NAME (--talk=NAME)
|
2024-09-09 03:11:50 +09:00
|
|
|
Talk []string `json:"talk"`
|
2024-09-09 13:27:31 +09:00
|
|
|
// Own set 'own' policy for NAME (--own=NAME)
|
|
|
|
Own []string `json:"own"`
|
|
|
|
|
|
|
|
// Call set RULE for calls on NAME (--call=NAME=RULE)
|
|
|
|
Call map[string]string `json:"call"`
|
|
|
|
// Broadcast set RULE for broadcasts from NAME (--broadcast=NAME=RULE)
|
|
|
|
Broadcast map[string]string `json:"broadcast"`
|
2024-09-09 03:11:50 +09:00
|
|
|
|
|
|
|
Log bool `json:"log,omitempty"`
|
|
|
|
Filter bool `json:"filter"`
|
|
|
|
}
|
|
|
|
|
2024-09-09 21:13:00 +09:00
|
|
|
func (c *Config) Args(bus [2]string) (args []string) {
|
2024-09-09 13:27:31 +09:00
|
|
|
argc := 2 + len(c.See) + len(c.Talk) + len(c.Own) + len(c.Call) + len(c.Broadcast)
|
2024-09-09 03:11:50 +09:00
|
|
|
if c.Log {
|
|
|
|
argc++
|
|
|
|
}
|
|
|
|
if c.Filter {
|
|
|
|
argc++
|
|
|
|
}
|
|
|
|
|
|
|
|
args = make([]string, 0, argc)
|
2024-09-09 21:13:00 +09:00
|
|
|
args = append(args, bus[0], bus[1])
|
2024-09-11 16:35:23 +09:00
|
|
|
if c.Filter {
|
|
|
|
args = append(args, "--filter")
|
|
|
|
}
|
2024-09-09 03:11:50 +09:00
|
|
|
for _, name := range c.See {
|
|
|
|
args = append(args, "--see="+name)
|
|
|
|
}
|
|
|
|
for _, name := range c.Talk {
|
|
|
|
args = append(args, "--talk="+name)
|
|
|
|
}
|
|
|
|
for _, name := range c.Own {
|
|
|
|
args = append(args, "--own="+name)
|
|
|
|
}
|
2024-09-09 13:27:31 +09:00
|
|
|
for name, rule := range c.Call {
|
|
|
|
args = append(args, "--call="+name+"="+rule)
|
|
|
|
}
|
|
|
|
for name, rule := range c.Broadcast {
|
|
|
|
args = append(args, "--broadcast="+name+"="+rule)
|
|
|
|
}
|
2024-09-09 03:11:50 +09:00
|
|
|
if c.Log {
|
|
|
|
args = append(args, "--log")
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-09 21:13:00 +09:00
|
|
|
func (c *Config) buildSeal(seal *strings.Builder, bus [2]string) error {
|
|
|
|
for _, arg := range c.Args(bus) {
|
|
|
|
// reject argument strings containing null
|
|
|
|
for _, b := range arg {
|
|
|
|
if b == '\x00' {
|
|
|
|
return errors.New("argument contains null")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// write null terminated argument
|
|
|
|
seal.WriteString(arg)
|
|
|
|
seal.WriteByte('\x00')
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-09 03:11:50 +09:00
|
|
|
// NewConfig returns a reference to a Config struct with optional defaults.
|
|
|
|
// If id is an empty string own defaults are omitted.
|
|
|
|
func NewConfig(id string, defaults, mpris bool) (c *Config) {
|
2024-09-09 19:20:37 +09:00
|
|
|
c = &Config{
|
|
|
|
Call: make(map[string]string),
|
|
|
|
Broadcast: make(map[string]string),
|
|
|
|
|
|
|
|
Filter: true,
|
|
|
|
}
|
2024-09-09 03:11:50 +09:00
|
|
|
|
|
|
|
if defaults {
|
2024-09-09 19:20:37 +09:00
|
|
|
c.Talk = []string{"org.freedesktop.DBus", "org.freedesktop.Notifications"}
|
|
|
|
|
|
|
|
c.Call["org.freedesktop.portal.*"] = "*"
|
|
|
|
c.Broadcast["org.freedesktop.portal.*"] = "@/org/freedesktop/portal/*"
|
2024-09-09 03:11:50 +09:00
|
|
|
|
|
|
|
if id != "" {
|
2024-09-09 19:20:37 +09:00
|
|
|
c.Own = []string{id + ".*"}
|
2024-09-09 03:11:50 +09:00
|
|
|
if mpris {
|
2024-09-09 19:20:37 +09:00
|
|
|
c.Own = append(c.Own, "org.mpris.MediaPlayer2."+id+".*")
|
2024-09-09 03:11:50 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|