1
0
Fork 0

Adding upstream version 0.8.9.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-05-22 10:16:14 +02:00
parent 3b2c48b5e4
commit c0c4addb85
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
285 changed files with 25880 additions and 0 deletions

View file

@ -0,0 +1,114 @@
package pushover
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/nicholas-fedor/shoutrrr/pkg/format"
"github.com/nicholas-fedor/shoutrrr/pkg/services/standard"
"github.com/nicholas-fedor/shoutrrr/pkg/types"
)
// hookURL is the Pushover API endpoint for sending messages.
const (
hookURL = "https://api.pushover.net/1/messages.json"
contentType = "application/x-www-form-urlencoded"
defaultHTTPTimeout = 10 * time.Second // defaultHTTPTimeout is the default timeout for HTTP requests.
)
// ErrSendFailed indicates a failure in sending the notification to a Pushover device.
var ErrSendFailed = errors.New("failed to send notification to pushover device")
// Service provides the Pushover notification service.
type Service struct {
standard.Standard
Config *Config
pkr format.PropKeyResolver
Client *http.Client
}
// Send delivers a notification message to Pushover.
func (service *Service) Send(message string, params *types.Params) error {
config := service.Config
if err := service.pkr.UpdateConfigFromParams(config, params); err != nil {
return fmt.Errorf("updating config from params: %w", err)
}
device := strings.Join(config.Devices, ",")
if err := service.sendToDevice(device, message, config); err != nil {
return fmt.Errorf("failed to send notifications to pushover devices: %w", err)
}
return nil
}
// sendToDevice sends a notification to a specific Pushover device.
func (service *Service) sendToDevice(device string, message string, config *Config) error {
data := url.Values{}
data.Set("device", device)
data.Set("user", config.User)
data.Set("token", config.Token)
data.Set("message", message)
if len(config.Title) > 0 {
data.Set("title", config.Title)
}
if config.Priority >= -2 && config.Priority <= 1 {
data.Set("priority", strconv.FormatInt(int64(config.Priority), 10))
}
ctx, cancel := context.WithTimeout(context.Background(), defaultHTTPTimeout)
defer cancel()
req, err := http.NewRequestWithContext(
ctx,
http.MethodPost,
hookURL,
strings.NewReader(data.Encode()),
)
if err != nil {
return fmt.Errorf("creating request: %w", err)
}
req.Header.Set("Content-Type", contentType)
res, err := service.Client.Do(req)
if err != nil {
return fmt.Errorf("sending request to Pushover API: %w", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return fmt.Errorf("%w: %q, response status %q", ErrSendFailed, device, res.Status)
}
return nil
}
// Initialize configures the service with a URL and logger.
func (service *Service) Initialize(configURL *url.URL, logger types.StdLogger) error {
service.SetLogger(logger)
service.Config = &Config{}
service.pkr = format.NewPropKeyResolver(service.Config)
service.Client = &http.Client{
Timeout: defaultHTTPTimeout,
}
if err := service.Config.setURL(&service.pkr, configURL); err != nil {
return err
}
return nil
}
// GetID returns the service identifier.
func (service *Service) GetID() string {
return Scheme
}

View file

@ -0,0 +1,83 @@
package pushover
import (
"errors"
"fmt"
"net/url"
"github.com/nicholas-fedor/shoutrrr/pkg/format"
"github.com/nicholas-fedor/shoutrrr/pkg/types"
)
// Scheme is the identifying part of this service's configuration URL.
const Scheme = "pushover"
// Static errors for configuration validation.
var (
ErrUserMissing = errors.New("user missing from config URL")
ErrTokenMissing = errors.New("token missing from config URL")
)
// Config for the Pushover notification service.
type Config struct {
Token string `desc:"API Token/Key" url:"pass"`
User string `desc:"User Key" url:"host"`
Devices []string ` key:"devices" optional:""`
Priority int8 ` key:"priority" default:"0"`
Title string ` key:"title" optional:""`
}
// Enums returns the fields that should use a corresponding EnumFormatter to Print/Parse their values.
func (config *Config) Enums() map[string]types.EnumFormatter {
return map[string]types.EnumFormatter{}
}
// GetURL returns a URL representation of its current field values.
func (config *Config) GetURL() *url.URL {
resolver := format.NewPropKeyResolver(config)
return config.getURL(&resolver)
}
// SetURL updates the Config from a URL representation of its field values.
func (config *Config) SetURL(url *url.URL) error {
resolver := format.NewPropKeyResolver(config)
return config.setURL(&resolver, url)
}
// setURL updates the Config from a URL using the provided resolver.
func (config *Config) setURL(resolver types.ConfigQueryResolver, url *url.URL) error {
password, _ := url.User.Password()
config.User = url.Host
config.Token = password
for key, vals := range url.Query() {
if err := resolver.Set(key, vals[0]); err != nil {
return fmt.Errorf("setting query parameter %q to %q: %w", key, vals[0], err)
}
}
if url.String() != "pushover://dummy@dummy.com" {
if len(config.User) < 1 {
return ErrUserMissing
}
if len(config.Token) < 1 {
return ErrTokenMissing
}
}
return nil
}
// getURL constructs a URL from the Config's fields using the provided resolver.
func (config *Config) getURL(resolver types.ConfigQueryResolver) *url.URL {
return &url.URL{
User: url.UserPassword("Token", config.Token),
Host: config.User,
Scheme: Scheme,
ForceQuery: true,
RawQuery: format.BuildQuery(resolver),
}
}

View file

@ -0,0 +1,11 @@
package pushover
// ErrorMessage for error events within the pushover service.
type ErrorMessage string
const (
// UserMissing should be used when a config URL is missing a user.
UserMissing ErrorMessage = "user missing from config URL"
// TokenMissing should be used when a config URL is missing a token.
TokenMissing ErrorMessage = "token missing from config URL"
)

View file

@ -0,0 +1,197 @@
package pushover_test
import (
"errors"
"log"
"net/url"
"os"
"testing"
"github.com/jarcoal/httpmock"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
"github.com/nicholas-fedor/shoutrrr/internal/testutils"
"github.com/nicholas-fedor/shoutrrr/pkg/format"
"github.com/nicholas-fedor/shoutrrr/pkg/services/pushover"
)
const hookURL = "https://api.pushover.net/1/messages.json"
func TestPushover(t *testing.T) {
gomega.RegisterFailHandler(ginkgo.Fail)
ginkgo.RunSpecs(t, "Pushover Suite")
}
var (
service *pushover.Service
config *pushover.Config
keyResolver format.PropKeyResolver
envPushoverURL *url.URL
logger *log.Logger
_ = ginkgo.BeforeSuite(func() {
service = &pushover.Service{}
logger = log.New(ginkgo.GinkgoWriter, "Test", log.LstdFlags)
envPushoverURL, _ = url.Parse(os.Getenv("SHOUTRRR_PUSHOVER_URL"))
})
)
var _ = ginkgo.Describe("the pushover service", func() {
ginkgo.When("running integration tests", func() {
ginkgo.It("should work", func() {
if envPushoverURL.String() == "" {
return
}
serviceURL, _ := url.Parse(envPushoverURL.String())
err := service.Initialize(serviceURL, testutils.TestLogger())
gomega.Expect(err).NotTo(gomega.HaveOccurred())
err = service.Send("this is an integration test", nil)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
})
ginkgo.It("returns the correct service identifier", func() {
gomega.Expect(service.GetID()).To(gomega.Equal("pushover"))
})
})
})
var _ = ginkgo.Describe("the pushover config", func() {
ginkgo.BeforeEach(func() {
config = &pushover.Config{}
keyResolver = format.NewPropKeyResolver(config)
})
ginkgo.When("updating it using an url", func() {
ginkgo.It("should update the username using the host part of the url", func() {
url := createURL("simme", "dummy")
err := config.SetURL(url)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(config.User).To(gomega.Equal("simme"))
})
ginkgo.It("should update the token using the password part of the url", func() {
url := createURL("dummy", "TestToken")
err := config.SetURL(url)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(config.Token).To(gomega.Equal("TestToken"))
})
ginkgo.It("should error if supplied with an empty username", func() {
url := createURL("", "token")
expectErrorMessageGivenURL(pushover.UserMissing, url)
})
ginkgo.It("should error if supplied with an empty token", func() {
url := createURL("user", "")
expectErrorMessageGivenURL(pushover.TokenMissing, url)
})
})
ginkgo.When("getting the current config", func() {
ginkgo.It("should return the config that is currently set as an url", func() {
config.User = "simme"
config.Token = "test-token"
url := config.GetURL()
password, _ := url.User.Password()
gomega.Expect(url.Host).To(gomega.Equal(config.User))
gomega.Expect(password).To(gomega.Equal(config.Token))
gomega.Expect(url.Scheme).To(gomega.Equal("pushover"))
})
})
ginkgo.When("setting a config key", func() {
ginkgo.It("should split it by commas if the key is devices", func() {
err := keyResolver.Set("devices", "a,b,c,d")
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(config.Devices).To(gomega.Equal([]string{"a", "b", "c", "d"}))
})
ginkgo.It("should update priority when a valid number is supplied", func() {
err := keyResolver.Set("priority", "1")
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(config.Priority).To(gomega.Equal(int8(1)))
})
ginkgo.It("should update priority when a negative number is supplied", func() {
gomega.Expect(keyResolver.Set("priority", "-1")).To(gomega.Succeed())
gomega.Expect(config.Priority).To(gomega.BeEquivalentTo(-1))
gomega.Expect(keyResolver.Set("priority", "-2")).To(gomega.Succeed())
gomega.Expect(config.Priority).To(gomega.BeEquivalentTo(-2))
})
ginkgo.It("should update the title when it is supplied", func() {
err := keyResolver.Set("title", "new title")
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(config.Title).To(gomega.Equal("new title"))
})
ginkgo.It("should return an error if priority is not a number", func() {
err := keyResolver.Set("priority", "super-duper")
gomega.Expect(err).To(gomega.HaveOccurred())
})
ginkgo.It("should return an error if the key is not recognized", func() {
err := keyResolver.Set("devicey", "a,b,c,d")
gomega.Expect(err).To(gomega.HaveOccurred())
})
})
ginkgo.When("getting a config key", func() {
ginkgo.It("should join it with commas if the key is devices", func() {
config.Devices = []string{"a", "b", "c"}
value, err := keyResolver.Get("devices")
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(value).To(gomega.Equal("a,b,c"))
})
ginkgo.It("should return an error if the key is not recognized", func() {
_, err := keyResolver.Get("devicey")
gomega.Expect(err).To(gomega.HaveOccurred())
})
})
ginkgo.When("listing the query fields", func() {
ginkgo.It("should return the keys \"devices\",\"priority\",\"title\"", func() {
fields := keyResolver.QueryFields()
gomega.Expect(fields).To(gomega.Equal([]string{"devices", "priority", "title"}))
})
})
ginkgo.Describe("sending the payload", func() {
ginkgo.BeforeEach(func() {
httpmock.Activate()
})
ginkgo.AfterEach(func() {
httpmock.DeactivateAndReset()
})
ginkgo.It("should not report an error if the server accepts the payload", func() {
serviceURL, err := url.Parse("pushover://:apptoken@usertoken")
gomega.Expect(err).NotTo(gomega.HaveOccurred())
err = service.Initialize(serviceURL, logger)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
httpmock.RegisterResponder("POST", hookURL, httpmock.NewStringResponder(200, ""))
err = service.Send("Message", nil)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
})
ginkgo.It("should not panic if an error occurs when sending the payload", func() {
serviceURL, err := url.Parse("pushover://:apptoken@usertoken")
gomega.Expect(err).NotTo(gomega.HaveOccurred())
err = service.Initialize(serviceURL, logger)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
httpmock.RegisterResponder(
"POST",
hookURL,
httpmock.NewErrorResponder(errors.New("dummy error")),
)
err = service.Send("Message", nil)
gomega.Expect(err).To(gomega.HaveOccurred())
})
})
})
func createURL(username string, token string) *url.URL {
return &url.URL{
User: url.UserPassword("Token", token),
Host: username,
}
}
func expectErrorMessageGivenURL(msg pushover.ErrorMessage, url *url.URL) {
err := config.SetURL(url)
gomega.Expect(err).To(gomega.HaveOccurred())
gomega.Expect(err.Error()).To(gomega.Equal(string(msg)))
}