| 1 | package pushover |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "net/url" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | "time" |
| 11 | |
| 12 | "github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 13 | "github.com/nicholas-fedor/shoutrrr/pkg/services/standard" |
| 14 | "github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 15 | ) |
| 16 | |
| 17 | // Service provides the Pushover notification service. |
| 18 | type Service struct { |
| 19 | standard.Standard |
| 20 | |
| 21 | Config *Config |
| 22 | pkr format.PropKeyResolver |
| 23 | Client *http.Client |
| 24 | httpClient types.HTTPClient |
| 25 | } |
| 26 | |
| 27 | // hookURL is the Pushover API endpoint for sending messages. |
| 28 | const ( |
| 29 | hookURL = "https://api.pushover.net/1/messages.json" |
| 30 | contentType = "application/x-www-form-urlencoded" |
| 31 | defaultHTTPTimeout = 10 * time.Second // defaultHTTPTimeout is the default timeout for HTTP requests. |
| 32 | ) |
| 33 | |
| 34 | // GetID returns the service identifier. |
| 35 | func (s *Service) GetID() string { |
| 36 | return Scheme |
| 37 | } |
| 38 | |
| 39 | // Initialize configures the service with a URL and logger. |
| 40 | func (s *Service) Initialize(serviceURL *url.URL, logger types.StdLogger) error { |
| 41 | s.SetLogger(logger) |
| 42 | s.Config = &Config{} |
| 43 | |
| 44 | s.pkr = format.NewPropKeyResolver(s.Config) |
| 45 | if s.Client == nil { |
| 46 | s.Client = &http.Client{ |
| 47 | Timeout: defaultHTTPTimeout, |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | if err := s.Config.setURL(&s.pkr, serviceURL); err != nil { |
| 52 | return err |
| 53 | } |
| 54 | |
| 55 | return nil |
| 56 | } |
| 57 | |
| 58 | // Send delivers a notification message to Pushover. |
| 59 | func (s *Service) Send(message string, params *types.Params) error { |
| 60 | config := s.Config |
| 61 | if err := s.pkr.UpdateConfigFromParams(config, params); err != nil { |
| 62 | return fmt.Errorf("updating config from params: %w", err) |
| 63 | } |
| 64 | |
| 65 | if _, err := parseEncryptionKey(config.EncryptionKey); err != nil { |
| 66 | return err |
| 67 | } |
| 68 | |
| 69 | device := strings.Join(config.Devices, ",") |
| 70 | if err := s.sendToDevice(device, message, config); err != nil { |
| 71 | return fmt.Errorf("failed to send notifications to pushover devices: %w", err) |
| 72 | } |
| 73 | |
| 74 | return nil |
| 75 | } |
| 76 | |
| 77 | // SetHTTPClient sets a custom HTTP client for the service. |
| 78 | func (s *Service) SetHTTPClient(client types.HTTPClient) { |
| 79 | s.httpClient = client |
| 80 | } |
| 81 | |
| 82 | // httpClientOrDefault returns the custom client or the default Client. |
| 83 | func (s *Service) httpClientOrDefault() types.HTTPClient { |
| 84 | if s.httpClient != nil { |
| 85 | return s.httpClient |
| 86 | } |
| 87 | |
| 88 | return s.Client |
| 89 | } |
| 90 | |
| 91 | // sendToDevice sends a notification to a specific Pushover device. |
| 92 | func (s *Service) sendToDevice(device, message string, config *Config) error { |
| 93 | key, err := parseEncryptionKey(config.EncryptionKey) |
| 94 | if err != nil { |
| 95 | return err |
| 96 | } |
| 97 | |
| 98 | if key != nil { |
| 99 | message, err = encryptField(message, key) |
| 100 | if err != nil { |
| 101 | return err |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | data := url.Values{} |
| 106 | data.Set("device", device) |
| 107 | data.Set("user", config.User) |
| 108 | data.Set("token", config.Token) |
| 109 | data.Set("message", message) |
| 110 | |
| 111 | if config.Title != "" { |
| 112 | title := config.Title |
| 113 | if key != nil { |
| 114 | title, err = encryptField(title, key) |
| 115 | if err != nil { |
| 116 | return err |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | data.Set("title", title) |
| 121 | } |
| 122 | |
| 123 | if key != nil { |
| 124 | data.Set("encrypted", "1") |
| 125 | } |
| 126 | |
| 127 | if config.Priority >= -2 && config.Priority <= 1 { |
| 128 | data.Set("priority", strconv.FormatInt(int64(config.Priority), 10)) |
| 129 | } |
| 130 | |
| 131 | ctx, cancel := context.WithTimeout(context.Background(), defaultHTTPTimeout) |
| 132 | defer cancel() |
| 133 | |
| 134 | req, err := http.NewRequestWithContext( |
| 135 | ctx, |
| 136 | http.MethodPost, |
| 137 | hookURL, |
| 138 | strings.NewReader(data.Encode()), |
| 139 | ) |
| 140 | if err != nil { |
| 141 | return fmt.Errorf("creating request: %w", err) |
| 142 | } |
| 143 | |
| 144 | req.Header.Set("Content-Type", contentType) |
| 145 | |
| 146 | client := s.httpClientOrDefault() |
| 147 | |
| 148 | res, err := client.Do(req) |
| 149 | if err != nil { |
| 150 | return fmt.Errorf("sending request to Pushover API: %w", err) |
| 151 | } |
| 152 | |
| 153 | defer func() { _ = res.Body.Close() }() |
| 154 | |
| 155 | if res.StatusCode != http.StatusOK { |
| 156 | return fmt.Errorf("%w: %q, response status %q", ErrSendFailed, device, res.Status) |
| 157 | } |
| 158 | |
| 159 | return nil |
| 160 | } |