Debian Codegraph @

pkg/services/chat/signal/signal_config.go

raw
1 package signal
2
3 import (
4 "fmt"
5 "net"
6 "net/url"
7 "strconv"
8 "strings"
9
10 "github.com/nicholas-fedor/shoutrrr/pkg/format"
11 "github.com/nicholas-fedor/shoutrrr/pkg/types"
12 )
13
14 // Config holds settings for the Signal notification service.
15 type Config struct {
16 Host string `default:"localhost" desc:"Signal REST API server hostname or IP" key:"host"`
17 Port int `default:"8080" desc:"Signal REST API server port" key:"port"`
18 User string ` desc:"Username for HTTP Basic Auth" key:"user" optional:""`
19 Password string ` desc:"Password for HTTP Basic Auth" key:"password" optional:"" sensitive:"true"`
20 Token string ` desc:"API token for Bearer authentication" key:"token,apikey" optional:"" sensitive:"true"`
21 Source string ` desc:"Source phone number (with country code)" key:"source"`
22 Recipients []string ` desc:"Recipient phone numbers, group IDs, or u: usernames" key:"recipients,to"`
23 Title string ` desc:"Optional title prepended to the message body" key:"title" optional:""`
24 Attachments string ` desc:"Comma-separated raw base64 attachments; a data: value is one URI" key:"attachments" optional:""`
25 TextMode textMode `default:"None" desc:"Message text mode (None omits text_mode; Styled enables markup)" key:"textmode,text_mode"`
26 DisableTLS bool `default:"No" desc:"Disable TLS for Signal REST API connection" key:"disabletls"`
27 SkipTLSVerify bool `default:"No" desc:"Skip TLS certificate verification" key:"skiptlsverify"`
28 NotifySelf bool `default:"Yes" desc:"Notify the sender's devices. When No, sends notify_self=false." key:"notifyself"`
29 }
30
31 const (
32 // Scheme identifies this service in configuration URLs.
33 Scheme = "signal"
34 // minPathParts is the minimum number of path parts required (source + at least one recipient).
35 minPathParts = 2
36 )
37
38 // Enums returns the fields that use an EnumFormatter for their values.
39 //
40 // Returns:
41 // - A map of config field names to their enum formatters.
42 func (*Config) Enums() map[string]types.EnumFormatter {
43 return map[string]types.EnumFormatter{
44 "TextMode": TextModes.Enum,
45 }
46 }
47
48 // GetURL generates a URL from the current configuration values.
49 //
50 // Returns:
51 // - *url.URL: the generated URL representing the configuration.
52 func (c *Config) GetURL() *url.URL {
53 resolver := format.NewPropKeyResolver(c)
54
55 return c.getURL(&resolver)
56 }
57
58 // SetURL updates the configuration from a URL representation.
59 //
60 // Parameters:
61 // - serviceURL: the URL to parse configuration from
62 //
63 // Returns:
64 // - error: if parsing fails, nil otherwise
65 func (c *Config) SetURL(serviceURL *url.URL) error {
66 resolver := format.NewPropKeyResolver(c)
67
68 return c.setURL(&resolver, serviceURL)
69 }
70
71 // getURL constructs a URL from the Config's fields using the provided resolver.
72 //
73 // Parameters:
74 // - resolver: the configuration query resolver for property resolution
75 //
76 // Returns:
77 // - *url.URL: the constructed URL
78 func (c *Config) getURL(resolver types.ConfigQueryResolver) *url.URL {
79 recipients := strings.Join(c.Recipients, "/")
80
81 result := &url.URL{
82 Scheme: Scheme,
83 Host: net.JoinHostPort(c.Host, strconv.Itoa(c.Port)),
84 Path: fmt.Sprintf("/%s/%s", c.Source, recipients),
85 RawQuery: format.BuildQuery(resolver),
86 }
87
88 if c.User != "" {
89 if c.Password != "" {
90 result.User = url.UserPassword(c.User, c.Password)
91 } else {
92 result.User = url.User(c.User)
93 }
94 }
95
96 return result
97 }
98
99 // parseAuth extracts user and password from the URL.
100 //
101 // Parameters:
102 // - serviceURL: the URL to extract authentication from
103 func (c *Config) parseAuth(serviceURL *url.URL) {
104 if serviceURL.User != nil {
105 c.User = serviceURL.User.Username()
106 if password, ok := serviceURL.User.Password(); ok {
107 c.Password = password
108 }
109 }
110 }
111
112 // parseHostPort extracts host and port from the URL.
113 //
114 // Parameters:
115 // - serviceURL: the URL to extract host and port from
116 func (c *Config) parseHostPort(serviceURL *url.URL) {
117 host := serviceURL.Hostname()
118 if host == "" {
119 host = serviceURL.Host
120 }
121
122 c.Host = host
123
124 portStr := serviceURL.Port()
125 if portStr == "" {
126 portStr = "8080"
127 }
128
129 if port, err := strconv.Atoi(portStr); err == nil {
130 c.Port = port
131 }
132 }
133
134 // parsePath extracts source phone number and recipients from the URL path.
135 //
136 // Parameters:
137 // - serviceURL: the URL to extract path from
138 //
139 // Returns:
140 // - error: if parsing fails, nil otherwise
141 func (c *Config) parsePath(serviceURL *url.URL) error {
142 pathParts := strings.Split(strings.Trim(serviceURL.Path, "/"), "/")
143 if len(pathParts) < minPathParts {
144 return ErrNoRecipients
145 }
146
147 source := pathParts[0]
148 if !isValidPhoneNumber(source) {
149 return fmt.Errorf("%w: %s", ErrInvalidPhoneNumber, source)
150 }
151
152 c.Source = source
153
154 recipients, err := parseRecipients(pathParts[1:])
155 if err != nil {
156 return err
157 }
158
159 c.Recipients = recipients
160
161 return nil
162 }
163
164 // parseQuery processes query parameters using the resolver.
165 //
166 // Parameters:
167 // - resolver: the configuration query resolver
168 // - serviceURL: the URL containing query parameters
169 //
170 // Returns:
171 // - error: if parsing fails, nil otherwise
172 func (c *Config) parseQuery(resolver types.ConfigQueryResolver, serviceURL *url.URL) error {
173 for key, vals := range serviceURL.Query() {
174 if err := resolver.Set(key, vals[0]); err != nil {
175 return fmt.Errorf("setting config property %q from URL query: %w", key, err)
176 }
177 }
178
179 return nil
180 }
181
182 // setURL updates the Config from a URL using the provided resolver.
183 //
184 // Parameters:
185 // - resolver: the configuration query resolver
186 // - serviceURL: the URL to parse
187 //
188 // Returns:
189 // - error: if parsing fails, nil otherwise
190 func (c *Config) setURL(resolver types.ConfigQueryResolver, serviceURL *url.URL) error {
191 if serviceURL.String() == "signal://[email protected]" {
192 c.Host = "localhost"
193 c.Port = 8080
194 c.Source = "+1234567890"
195 c.Recipients = []string{"+0987654321"}
196 c.DisableTLS = false
197
198 return nil
199 }
200
201 c.parseAuth(serviceURL)
202 c.parseHostPort(serviceURL)
203
204 if err := c.parsePath(serviceURL); err != nil {
205 return err
206 }
207
208 if err := c.parseQuery(resolver, serviceURL); err != nil {
209 return err
210 }
211
212 return nil
213 }