go-tg-screenshot-bot
89 строк · 2.7 Кб
1package dbus2
3import (4"strconv"5"strings"6)
7
8// MatchOption specifies option for dbus routing match rule. Options can be constructed with WithMatch* helpers.
9// For full list of available options consult
10// https://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-routing-match-rules
11type MatchOption struct {12key string13value string14}
15
16func formatMatchOptions(options []MatchOption) string {17items := make([]string, 0, len(options))18for _, option := range options {19items = append(items, option.key+"='"+option.value+"'")20}21return strings.Join(items, ",")22}
23
24// WithMatchOption creates match option with given key and value
25func WithMatchOption(key, value string) MatchOption {26return MatchOption{key, value}27}
28
29// doesn't make sense to export this option because clients can only
30// subscribe to messages with signal type.
31func withMatchType(typ string) MatchOption {32return WithMatchOption("type", typ)33}
34
35// WithMatchSender sets sender match option.
36func WithMatchSender(sender string) MatchOption {37return WithMatchOption("sender", sender)38}
39
40// WithMatchSender sets interface match option.
41func WithMatchInterface(iface string) MatchOption {42return WithMatchOption("interface", iface)43}
44
45// WithMatchMember sets member match option.
46func WithMatchMember(member string) MatchOption {47return WithMatchOption("member", member)48}
49
50// WithMatchObjectPath creates match option that filters events based on given path
51func WithMatchObjectPath(path ObjectPath) MatchOption {52return WithMatchOption("path", string(path))53}
54
55// WithMatchPathNamespace sets path_namespace match option.
56func WithMatchPathNamespace(namespace ObjectPath) MatchOption {57return WithMatchOption("path_namespace", string(namespace))58}
59
60// WithMatchDestination sets destination match option.
61func WithMatchDestination(destination string) MatchOption {62return WithMatchOption("destination", destination)63}
64
65// WithMatchArg sets argN match option, range of N is 0 to 63.
66func WithMatchArg(argIdx int, value string) MatchOption {67if argIdx < 0 || argIdx > 63 {68panic("range of argument index is 0 to 63")69}70return WithMatchOption("arg"+strconv.Itoa(argIdx), value)71}
72
73// WithMatchArgPath sets argN path match option, range of N is 0 to 63.
74func WithMatchArgPath(argIdx int, path string) MatchOption {75if argIdx < 0 || argIdx > 63 {76panic("range of argument index is 0 to 63")77}78return WithMatchOption("arg"+strconv.Itoa(argIdx)+"path", path)79}
80
81// WithMatchArg0Namespace sets arg0namespace match option.
82func WithMatchArg0Namespace(arg0Namespace string) MatchOption {83return WithMatchOption("arg0namespace", arg0Namespace)84}
85
86// WithMatchEavesdrop sets eavesdrop match option.
87func WithMatchEavesdrop(eavesdrop bool) MatchOption {88return WithMatchOption("eavesdrop", strconv.FormatBool(eavesdrop))89}
90