| 1 | package mattermost |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "crypto/tls" |
| 7 | "fmt" |
| 8 | "net/http" |
| 9 | "net/url" |
| 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 sends notifications to a pre-configured Mattermost channel or user. |
| 18 | type Service struct { |
| 19 | standard.Standard |
| 20 | |
| 21 | Config *Config |
| 22 | pkr format.PropKeyResolver |
| 23 | httpClient types.HTTPClient |
| 24 | } |
| 25 | |
| 26 | // defaultHTTPTimeout is the default timeout for HTTP requests. |
| 27 | const defaultHTTPTimeout = 10 * time.Second |
| 28 | |
| 29 | // GetID returns the service identifier. |
| 30 | func (s *Service) GetID() string { |
| 31 | return Scheme |
| 32 | } |
| 33 | |
| 34 | // Initialize configures the service with a URL and logger. |
| 35 | func (s *Service) Initialize(serviceURL *url.URL, logger types.StdLogger) error { |
| 36 | s.SetLogger(logger) |
| 37 | s.Config = &Config{} |
| 38 | s.pkr = format.NewPropKeyResolver(s.Config) |
| 39 | |
| 40 | err := s.Config.setURL(&s.pkr, serviceURL) |
| 41 | if err != nil { |
| 42 | return err |
| 43 | } |
| 44 | |
| 45 | var transport *http.Transport |
| 46 | if s.Config.DisableTLS { |
| 47 | transport = &http.Transport{} // Plain HTTP |
| 48 | } else { |
| 49 | transport = &http.Transport{ |
| 50 | TLSClientConfig: &tls.Config{ |
| 51 | InsecureSkipVerify: false, // Explicitly safe when TLS is enabled |
| 52 | MinVersion: tls.VersionTLS12, // Enforce TLS 1.2 or higher |
| 53 | }, |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | s.httpClient = &http.Client{Transport: transport} |
| 58 | |
| 59 | return nil |
| 60 | } |
| 61 | |
| 62 | // Send delivers a notification message to Mattermost. |
| 63 | func (s *Service) Send(message string, params *types.Params) error { |
| 64 | config := s.Config |
| 65 | serviceURL := buildURL(config) |
| 66 | |
| 67 | if err := s.pkr.UpdateConfigFromParams(config, params); err != nil { |
| 68 | return fmt.Errorf("updating config from params: %w", err) |
| 69 | } |
| 70 | |
| 71 | json, err := CreateJSONPayload(config, message, params) |
| 72 | if err != nil { |
| 73 | return fmt.Errorf("creating JSON payload: %w", err) |
| 74 | } |
| 75 | |
| 76 | ctx, cancel := context.WithTimeout( |
| 77 | context.Background(), |
| 78 | defaultHTTPTimeout, |
| 79 | ) |
| 80 | defer cancel() |
| 81 | |
| 82 | req, err := http.NewRequestWithContext( |
| 83 | ctx, |
| 84 | http.MethodPost, |
| 85 | serviceURL.String(), |
| 86 | bytes.NewReader(json), |
| 87 | ) |
| 88 | if err != nil { |
| 89 | return fmt.Errorf("creating POST request: %w", err) |
| 90 | } |
| 91 | |
| 92 | req.Header.Set("Content-Type", "application/json") |
| 93 | |
| 94 | client := s.httpClient |
| 95 | if client == nil { |
| 96 | transport := &http.Transport{ |
| 97 | TLSClientConfig: &tls.Config{ |
| 98 | InsecureSkipVerify: false, |
| 99 | MinVersion: tls.VersionTLS12, |
| 100 | }, |
| 101 | } |
| 102 | client = &http.Client{Transport: transport} |
| 103 | } |
| 104 | |
| 105 | res, err := client.Do(req) |
| 106 | if err != nil { |
| 107 | return fmt.Errorf("executing POST request to Mattermost API: %w", err) |
| 108 | } |
| 109 | |
| 110 | defer func() { _ = res.Body.Close() }() |
| 111 | |
| 112 | if res.StatusCode != http.StatusOK { |
| 113 | return fmt.Errorf("%w: %s", ErrSendFailed, res.Status) |
| 114 | } |
| 115 | |
| 116 | return nil |
| 117 | } |
| 118 | |
| 119 | // SetHTTPClient sets a custom HTTP client for the service. |
| 120 | func (s *Service) SetHTTPClient(client types.HTTPClient) { |
| 121 | s.httpClient = client |
| 122 | } |
| 123 | |
| 124 | // buildURL constructs the API URL for Mattermost based on the Config. |
| 125 | // Returns a *url.URL that is valid by construction. |
| 126 | func buildURL(config *Config) *url.URL { |
| 127 | scheme := "https" |
| 128 | if config.DisableTLS { |
| 129 | scheme = "http" |
| 130 | } |
| 131 | |
| 132 | return &url.URL{ |
| 133 | Scheme: scheme, |
| 134 | Host: config.Host, |
| 135 | Path: "/hooks/" + config.Token, |
| 136 | } |
| 137 | } |