| 1 | package ifttt |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "net/url" |
| 7 | |
| 8 | "github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 9 | "github.com/nicholas-fedor/shoutrrr/pkg/services/standard" |
| 10 | "github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 11 | ) |
| 12 | |
| 13 | // Config holds settings for the IFTTT notification service. |
| 14 | type Config struct { |
| 15 | standard.EnumlessConfig |
| 16 | |
| 17 | WebHookID string `required:"true" url:"host"` |
| 18 | Events []string `required:"true" key:"events"` |
| 19 | Value1 string ` key:"value1" optional:""` |
| 20 | Value2 string ` key:"value2" optional:""` |
| 21 | Value3 string ` key:"value3" optional:""` |
| 22 | UseMessageAsValue uint8 ` key:"messagevalue" default:"2" desc:"Sets the corresponding value field to the notification message"` |
| 23 | UseTitleAsValue uint8 ` key:"titlevalue" default:"0" desc:"Sets the corresponding value field to the notification title"` |
| 24 | Title string ` key:"title" default:"" desc:"Notification title, optionally set by the sender"` |
| 25 | } |
| 26 | |
| 27 | const ( |
| 28 | Scheme = "ifttt" // Scheme identifies this service in configuration URLs. |
| 29 | DefaultMessageValue = 2 // Default value field (1-3) for the notification message |
| 30 | DisabledValue = 0 // Value to disable title assignment |
| 31 | MinValueField = 1 // Minimum valid value field (Value1) |
| 32 | MaxValueField = 3 // Maximum valid value field (Value3) |
| 33 | MinLength = 1 // Minimum length for required fields like Events and WebHookID |
| 34 | ) |
| 35 | |
| 36 | var ( |
| 37 | ErrInvalidMessageValue = errors.New( |
| 38 | "invalid value for messagevalue: only values 1-3 are supported", |
| 39 | ) |
| 40 | ErrInvalidTitleValue = errors.New( |
| 41 | "invalid value for titlevalue: only values 1-3 or 0 (for disabling) are supported", |
| 42 | ) |
| 43 | ErrTitleMessageConflict = errors.New("titlevalue cannot use the same number as messagevalue") |
| 44 | ErrMissingEvents = errors.New("events missing from config URL") |
| 45 | ErrMissingWebhookID = errors.New("webhook ID missing from config URL") |
| 46 | ) |
| 47 | |
| 48 | // GetURL generates a URL from the current configuration values. |
| 49 | func (c *Config) GetURL() *url.URL { |
| 50 | resolver := format.NewPropKeyResolver(c) |
| 51 | |
| 52 | return c.getURL(&resolver) |
| 53 | } |
| 54 | |
| 55 | // SetURL updates the configuration from a URL representation. |
| 56 | func (c *Config) SetURL(serviceURL *url.URL) error { |
| 57 | resolver := format.NewPropKeyResolver(c) |
| 58 | |
| 59 | return c.setURL(&resolver, serviceURL) |
| 60 | } |
| 61 | |
| 62 | func (c *Config) getURL(resolver types.ConfigQueryResolver) *url.URL { |
| 63 | return &url.URL{ |
| 64 | Host: c.WebHookID, |
| 65 | Path: "/", |
| 66 | Scheme: Scheme, |
| 67 | RawQuery: format.BuildQuery(resolver), |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func (c *Config) setURL(resolver types.ConfigQueryResolver, serviceURL *url.URL) error { |
| 72 | if c.UseMessageAsValue == DisabledValue { |
| 73 | c.UseMessageAsValue = DefaultMessageValue |
| 74 | } |
| 75 | |
| 76 | c.WebHookID = serviceURL.Hostname() |
| 77 | |
| 78 | for key, vals := range serviceURL.Query() { |
| 79 | if err := resolver.Set(key, vals[0]); err != nil { |
| 80 | return fmt.Errorf("setting config property %q from URL query: %w", key, err) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if c.UseMessageAsValue > MaxValueField || c.UseMessageAsValue < MinValueField { |
| 85 | return ErrInvalidMessageValue |
| 86 | } |
| 87 | |
| 88 | if c.UseTitleAsValue > MaxValueField { |
| 89 | return ErrInvalidTitleValue |
| 90 | } |
| 91 | |
| 92 | if c.UseTitleAsValue != DisabledValue && |
| 93 | c.UseTitleAsValue == c.UseMessageAsValue { |
| 94 | return ErrTitleMessageConflict |
| 95 | } |
| 96 | |
| 97 | if serviceURL.String() != "ifttt://[email protected]" { |
| 98 | if len(c.Events) < MinLength { |
| 99 | return ErrMissingEvents |
| 100 | } |
| 101 | |
| 102 | if len(c.WebHookID) < MinLength { |
| 103 | return ErrMissingWebhookID |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return nil |
| 108 | } |