| 1 |
package matrix |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"net/url" |
| 6 |
|
| 7 |
"github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 8 |
"github.com/nicholas-fedor/shoutrrr/pkg/services/standard" |
| 9 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 10 |
) |
| 11 |
|
| 12 |
// Config is the configuration for the matrix service. |
| 13 |
type Config struct { |
| 14 |
standard.EnumlessConfig |
| 15 |
|
| 16 |
User string `desc:"Username or empty when using access token" optional:"" url:"user"` |
| 17 |
|
| 18 |
Password string `desc:"Password or access token" url:"password"` |
| 19 |
DisableTLS bool ` default:"No" key:"disableTLS"` |
| 20 |
Host string ` url:"host"` |
| 21 |
Rooms []string `desc:"Room aliases, or with ! prefix, room IDs" key:"rooms,room" optional:""` |
| 22 |
Title string ` default:"" key:"title"` |
| 23 |
} |
| 24 |
|
| 25 |
// GetURL returns a URL representation of it's current field values. |
| 26 |
func (c *Config) GetURL() *url.URL { |
| 27 |
resolver := format.NewPropKeyResolver(c) |
| 28 |
|
| 29 |
return c.getURL(&resolver) |
| 30 |
} |
| 31 |
|
| 32 |
// SetURL updates a ServiceConfig from a URL representation of it's field values. |
| 33 |
func (c *Config) SetURL(serviceURL *url.URL) error { |
| 34 |
resolver := format.NewPropKeyResolver(c) |
| 35 |
|
| 36 |
return c.setURL(&resolver, serviceURL) |
| 37 |
} |
| 38 |
|
| 39 |
func (c *Config) getURL(resolver types.ConfigQueryResolver) *url.URL { |
| 40 |
return &url.URL{ |
| 41 |
User: url.UserPassword(c.User, c.Password), |
| 42 |
Host: c.Host, |
| 43 |
Scheme: Scheme, |
| 44 |
ForceQuery: true, |
| 45 |
RawQuery: format.BuildQuery(resolver), |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
func (c *Config) setURL(resolver types.ConfigQueryResolver, serviceURL *url.URL) error { |
| 50 |
// Skip credential validation and query parameter parsing for dummy URLs |
| 51 |
// used in docs generation. Query params (rooms, title, disableTLS) are |
| 52 |
// intentionally ignored for dummy.com to avoid validation errors. |
| 53 |
if serviceURL.Hostname() == "dummy.com" { |
| 54 |
c.User = serviceURL.User.Username() |
| 55 |
if password, ok := serviceURL.User.Password(); ok { |
| 56 |
c.Password = password |
| 57 |
} |
| 58 |
|
| 59 |
c.Host = serviceURL.Host |
| 60 |
|
| 61 |
return nil |
| 62 |
} |
| 63 |
|
| 64 |
c.User = serviceURL.User.Username() |
| 65 |
|
| 66 |
password, ok := serviceURL.User.Password() |
| 67 |
if !ok { |
| 68 |
return ErrMissingCredentials |
| 69 |
} |
| 70 |
|
| 71 |
c.Password = password |
| 72 |
c.Host = serviceURL.Host |
| 73 |
|
| 74 |
if c.Host == "" { |
| 75 |
return ErrMissingHost |
| 76 |
} |
| 77 |
|
| 78 |
for key, vals := range serviceURL.Query() { |
| 79 |
if err := resolver.Set(key, vals[0]); err != nil { |
| 80 |
return fmt.Errorf("setting query parameter %q to %q: %w", key, vals[0], err) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
for r, room := range c.Rooms { |
| 85 |
// If room does not begin with a '#' let's prepend it |
| 86 |
if room != "" && room[0] != '#' && room[0] != '!' { |
| 87 |
c.Rooms[r] = "#" + room |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
return nil |
| 92 |
} |