Debian Codegraph @

pkg/services/push/ntfy/ntfy_config.go

raw
1 package ntfy
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/types"
11 )
12
13 // Config holds the configuration for the Ntfy service.
14 type Config struct {
15 Title string `default:"" desc:"Message title" key:"title"`
16 Host string `default:"ntfy.sh" desc:"Server hostname and port" url:"host"`
17 Topic string ` desc:"Target topic name" url:"path" required:""`
18 Password string ` desc:"Auth password" url:"password" optional:""`
19 Username string ` desc:"Auth username" url:"user" optional:""`
20 Scheme string `default:"https" desc:"Server protocol, http or https" key:"scheme"`
21 Tags []string ` desc:"List of tags that may or not map to emojis" key:"tags" optional:""`
22 Priority priority `default:"default" desc:"Message priority with 1=min, 3=default and 5=max" key:"priority"`
23 Actions []string ` desc:"Custom user action buttons for notifications, see https://docs.ntfy.sh/publish/#action-buttons" key:"actions" optional:"" sep:";"`
24 Click string ` desc:"Website opened when notification is clicked" key:"click" optional:""`
25 Attach string ` desc:"URL of an attachment, see attach via URL" key:"attach" optional:""`
26 Filename string ` desc:"File name of the attachment" key:"filename" optional:""`
27 Delay string ` desc:"Timestamp or duration for delayed delivery, see https://docs.ntfy.sh/publish/#scheduled-delivery" key:"delay,at,in" optional:""`
28 Email string ` desc:"E-mail address for e-mail notifications" key:"email" optional:""`
29 Icon string ` desc:"URL to use as notification icon" key:"icon" optional:""`
30 Markdown bool `default:"no" desc:"Enable Markdown formatting, see https://docs.ntfy.sh/publish/#markdown-formatting" key:"markdown"`
31 Cache bool `default:"yes" desc:"Cache messages" key:"cache"`
32 Firebase bool `default:"yes" desc:"Send to firebase" key:"firebase"`
33 DisableTLSVerification bool `default:"no" desc:"Disable TLS certificate verification" key:"disabletlsverification"`
34 DisableTLS bool `default:"no" desc:"Disable TLS entirely (force HTTP scheme)" key:"disabletls"`
35 }
36
37 // Scheme is the identifying part of this service's configuration URL.
38 const (
39 Scheme = "ntfy"
40 )
41
42 // ErrTopicRequired indicates that the topic is missing from the config URL.
43 var ErrTopicRequired = errors.New("topic is required")
44
45 // Enums returns the fields that use an EnumFormatter for their values.
46 func (*Config) Enums() map[string]types.EnumFormatter {
47 return map[string]types.EnumFormatter{
48 "Priority": Priority.Enum,
49 }
50 }
51
52 // GetAPIURL constructs the API URL for the Ntfy service based on the configuration.
53 func (c *Config) GetAPIURL() string {
54 path := c.Topic
55 if !strings.HasPrefix(c.Topic, "/") {
56 path = "/" + path
57 }
58
59 apiURL := url.URL{
60 Scheme: c.Scheme,
61 Host: c.Host,
62 Path: path,
63 }
64
65 return apiURL.String()
66 }
67
68 // GetURL returns a URL representation of the Config's current field values.
69 func (c *Config) GetURL() *url.URL {
70 resolver := format.NewPropKeyResolver(c)
71
72 return c.getURL(&resolver)
73 }
74
75 // QueryFields returns the list of query parameter names for the Config struct.
76 func (c *Config) QueryFields() []string {
77 return format.GetConfigQueryResolver(c).QueryFields()
78 }
79
80 // SetURL updates the Config from a URL representation of its field values.
81 func (c *Config) SetURL(serviceURL *url.URL) error {
82 resolver := format.NewPropKeyResolver(c)
83
84 return c.setURL(&resolver, serviceURL)
85 }
86
87 // getURL constructs a URL from the Config's fields using the provided resolver.
88 func (c *Config) getURL(resolver types.ConfigQueryResolver) *url.URL {
89 path := c.Topic
90 if !strings.HasPrefix(c.Topic, "/") {
91 path = "/" + path
92 }
93
94 result := &url.URL{
95 Host: c.Host,
96 Scheme: Scheme,
97 ForceQuery: true,
98 Path: path,
99 RawQuery: format.BuildQuery(resolver),
100 }
101 if c.Username != "" {
102 if c.Password != "" {
103 result.User = url.UserPassword(c.Username, c.Password)
104 } else {
105 result.User = url.User(c.Username)
106 }
107 }
108
109 return result
110 }
111
112 // setURL updates the Config from a URL using the provided resolver.
113 func (c *Config) setURL(resolver types.ConfigQueryResolver, serviceURL *url.URL) error {
114 if serviceURL.User != nil {
115 password, _ := serviceURL.User.Password()
116 c.Password = password
117 c.Username = serviceURL.User.Username()
118 } else {
119 c.Password = ""
120 c.Username = ""
121 }
122
123 c.Host = serviceURL.Host
124 c.Topic = strings.TrimPrefix(serviceURL.Path, "/")
125
126 serviceURL.RawQuery = strings.ReplaceAll(serviceURL.RawQuery, ";", "%3b")
127 for key, vals := range serviceURL.Query() {
128 if err := resolver.Set(key, vals[0]); err != nil {
129 return fmt.Errorf("setting query parameter %q to %q: %w", key, vals[0], err)
130 }
131 }
132
133 if serviceURL.String() != "ntfy://[email protected]" {
134 if c.Topic == "" {
135 return ErrTopicRequired
136 }
137 }
138
139 return nil
140 }