| 1 |
package pushbullet |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"net/url" |
| 7 |
"strings" |
| 8 |
|
| 9 |
"github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 10 |
"github.com/nicholas-fedor/shoutrrr/pkg/services/standard" |
| 11 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 12 |
) |
| 13 |
|
| 14 |
// Config holds the configuration for the Pushbullet service. |
| 15 |
type Config struct { |
| 16 |
standard.EnumlessConfig |
| 17 |
|
| 18 |
Targets []string `url:"path"` |
| 19 |
Token string `url:"host"` |
| 20 |
Title string ` default:"Shoutrrr notification" key:"title"` |
| 21 |
} |
| 22 |
|
| 23 |
// Scheme is the scheme part of the service configuration URL. |
| 24 |
const Scheme = "pushbullet" |
| 25 |
|
| 26 |
// ExpectedTokenLength is the required length for a valid Pushbullet token. |
| 27 |
const ExpectedTokenLength = 34 |
| 28 |
|
| 29 |
// ErrTokenIncorrectSize indicates that the token has an incorrect size. |
| 30 |
var ErrTokenIncorrectSize = errors.New("token has incorrect size") |
| 31 |
|
| 32 |
// GetURL returns a URL representation of the Config's current field values. |
| 33 |
func (c *Config) GetURL() *url.URL { |
| 34 |
resolver := format.NewPropKeyResolver(c) |
| 35 |
|
| 36 |
return c.getURL(&resolver) |
| 37 |
} |
| 38 |
|
| 39 |
// SetURL updates the Config from a URL representation of its field values. |
| 40 |
func (c *Config) SetURL(serviceURL *url.URL) error { |
| 41 |
resolver := format.NewPropKeyResolver(c) |
| 42 |
|
| 43 |
return c.setURL(&resolver, serviceURL) |
| 44 |
} |
| 45 |
|
| 46 |
// getURL constructs a URL from the Config's fields using the provided resolver. |
| 47 |
func (c *Config) getURL(resolver types.ConfigQueryResolver) *url.URL { |
| 48 |
return &url.URL{ |
| 49 |
Host: c.Token, |
| 50 |
Path: "/" + strings.Join(c.Targets, "/"), |
| 51 |
Scheme: Scheme, |
| 52 |
ForceQuery: false, |
| 53 |
RawQuery: format.BuildQuery(resolver), |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
// setURL updates the Config from a URL using the provided resolver. |
| 58 |
func (c *Config) setURL(resolver types.ConfigQueryResolver, serviceURL *url.URL) error { |
| 59 |
path := serviceURL.Path |
| 60 |
if path != "" && path[0] == '/' { |
| 61 |
path = path[1:] |
| 62 |
} |
| 63 |
|
| 64 |
if serviceURL.Fragment != "" { |
| 65 |
path += "/#" + serviceURL.Fragment |
| 66 |
} |
| 67 |
|
| 68 |
targets := strings.Split(path, "/") |
| 69 |
|
| 70 |
token := serviceURL.Hostname() |
| 71 |
if serviceURL.String() != "pushbullet://[email protected]" { |
| 72 |
if err := validateToken(token); err != nil { |
| 73 |
return err |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
c.Token = token |
| 78 |
c.Targets = targets |
| 79 |
|
| 80 |
for key, vals := range serviceURL.Query() { |
| 81 |
if err := resolver.Set(key, vals[0]); err != nil { |
| 82 |
return fmt.Errorf("setting query parameter %q to %q: %w", key, vals[0], err) |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
return nil |
| 87 |
} |
| 88 |
|
| 89 |
// validateToken checks if the token meets the expected length requirement. |
| 90 |
func validateToken(token string) error { |
| 91 |
if len(token) != ExpectedTokenLength { |
| 92 |
return ErrTokenIncorrectSize |
| 93 |
} |
| 94 |
|
| 95 |
return nil |
| 96 |
} |