| 1 | package zulipscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/ |
| 2 | |
| 3 | import ( |
| 4 | "contextscip-go gomod github.com/golang/go/src . context/" |
| 5 | "encoding/jsonscip-go gomod github.com/golang/go/src . `encoding/json`/" |
| 6 | "fmtscip-go gomod github.com/golang/go/src . fmt/" |
| 7 | "ioscip-go gomod github.com/golang/go/src . io/" |
| 8 | "net/httpscip-go gomod github.com/golang/go/src . `net/http`/" |
| 9 | "net/urlscip-go gomod github.com/golang/go/src . `net/url`/" |
| 10 | "regexpscip-go gomod github.com/golang/go/src . regexp/" |
| 11 | "strconvscip-go gomod github.com/golang/go/src . strconv/" |
| 12 | "stringsscip-go gomod github.com/golang/go/src . strings/" |
| 13 | "syncscip-go gomod github.com/golang/go/src . sync/" |
| 14 | "timescip-go gomod github.com/golang/go/src . time/" |
| 15 | |
| 16 | "github.com/nicholas-fedor/shoutrrr/pkg/formatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/" |
| 17 | "github.com/nicholas-fedor/shoutrrr/pkg/services/standardscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/standard`/" |
| 18 | "github.com/nicholas-fedor/shoutrrr/pkg/typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/" |
| 19 | ) |
| 20 | |
| 21 | // HTTPClient defines the interface for HTTP operations. |
| 22 | type HTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/HTTPClient# interface { |
| 23 | Doscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/HTTPClient#Do.(req *httpscip-go gomod github.com/golang/go/src . `net/http`/.Requestscip-go gomod github.com/golang/go/src . `net/http`/Request#) (*httpscip-go gomod github.com/golang/go/src . `net/http`/.Responsescip-go gomod github.com/golang/go/src . `net/http`/Response#, error) |
| 24 | } |
| 25 | |
| 26 | // DefaultHTTPClient is the default implementation using http.DefaultClient with timeout. |
| 27 | type DefaultHTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/DefaultHTTPClient# struct { |
| 28 | clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/DefaultHTTPClient#client. *httpscip-go gomod github.com/golang/go/src . `net/http`/.Clientscip-go gomod github.com/golang/go/src . `net/http`/Client# |
| 29 | } |
| 30 | |
| 31 | // registerResponse represents the relevant fields from the Zulip register endpoint. |
| 32 | type registerResponsescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/registerResponse# struct { |
| 33 | MaxMessageLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/registerResponse#MaxMessageLength. int `json:"max_message_length"` |
| 34 | MaxTopicLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/registerResponse#MaxTopicLength. int `json:"max_topic_length"` |
| 35 | } |
| 36 | |
| 37 | // Service sends notifications to a pre-configured Zulip channel or user. |
| 38 | type Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service# struct { |
| 39 | standardscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/standard`/.Standardscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#Standard. |
| 40 | |
| 41 | Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#Config. *Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Config# |
| 42 | pkrscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#pkr. formatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/.PropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver# |
| 43 | HTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#HTTPClient. HTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/HTTPClient# |
| 44 | |
| 45 | muscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#mu. syncscip-go gomod github.com/golang/go/src . sync/.Oncescip-go gomod github.com/golang/go/src . sync/Once# |
| 46 | contentMaxSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#contentMaxSize. int |
| 47 | topicMaxLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#topicMaxLength. int |
| 48 | } |
| 49 | |
| 50 | // defaultHTTPTimeout is the default timeout for HTTP requests to Zulip. |
| 51 | const defaultHTTPTimeoutscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/defaultHTTPTimeout. = 30 * timescip-go gomod github.com/golang/go/src . time/.Secondscip-go gomod github.com/golang/go/src . time/Second. |
| 52 | |
| 53 | // contentMaxSize defines the default maximum allowed message size in bytes. |
| 54 | const contentMaxSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/contentMaxSize. = 10000 |
| 55 | |
| 56 | // topicMaxLength defines the default maximum allowed topic length in characters. |
| 57 | const topicMaxLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/topicMaxLength. = 60 |
| 58 | |
| 59 | // registerTimeout is the timeout for the register API call. |
| 60 | const registerTimeoutscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/registerTimeout. = 10 * timescip-go gomod github.com/golang/go/src . time/.Secondscip-go gomod github.com/golang/go/src . time/Second. |
| 61 | |
| 62 | // maxErrorBodySize limits how much of a non-OK response body we read to prevent |
| 63 | // memory exhaustion from huge error bodies returned by the server. |
| 64 | const maxErrorBodySizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/maxErrorBodySize. = 4 * 1024 |
| 65 | |
| 66 | // hostValidator ensures the host is a valid hostname or domain, |
| 67 | // optionally followed by a colon and port number (1-65535). |
| 68 | var hostValidatorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/hostValidator. = regexpscip-go gomod github.com/golang/go/src . regexp/.MustCompilescip-go gomod github.com/golang/go/src . regexp/MustCompile().( |
| 69 | `^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}(:[0-9]{1,5})?$`, |
| 70 | ) |
| 71 | |
| 72 | // validateHost checks that the host string is a valid hostname with optional port (1-65535). |
| 73 | func validateHostscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/validateHost().(host string) error { |
| 74 | if !hostValidatorscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/hostValidator..MatchStringscip-go gomod github.com/golang/go/src . regexp/Regexp#MatchString().(host) { |
| 75 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%w: %q", ErrInvalidHostscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/ErrInvalidHost., host) |
| 76 | } |
| 77 | |
| 78 | if _, portStr, ok := stringsscip-go gomod github.com/golang/go/src . strings/.Cutscip-go gomod github.com/golang/go/src . strings/Cut().(host, ":"); ok && portStr != "" { |
| 79 | port, parseErr := strconvscip-go gomod github.com/golang/go/src . strconv/.Atoiscip-go gomod github.com/golang/go/src . strconv/Atoi().(portStr) |
| 80 | if parseErr != nil || port < 1 || port > 65535 { |
| 81 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%w: %q", ErrInvalidHostscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/ErrInvalidHost., host) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return nil |
| 86 | } |
| 87 | |
| 88 | func NewDefaultHTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/NewDefaultHTTPClient().() *DefaultHTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/DefaultHTTPClient# { |
| 89 | return &DefaultHTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/DefaultHTTPClient#{ |
| 90 | clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/DefaultHTTPClient#client.: &httpscip-go gomod github.com/golang/go/src . `net/http`/.Clientscip-go gomod github.com/golang/go/src . `net/http`/Client#{ |
| 91 | Timeoutscip-go gomod github.com/golang/go/src . `net/http`/Client#Timeout.: defaultHTTPTimeoutscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/defaultHTTPTimeout., |
| 92 | }, |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // Do performs the HTTP request. |
| 97 | func (c *DefaultHTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/DefaultHTTPClient#) Doscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/DefaultHTTPClient#Do().(req *httpscip-go gomod github.com/golang/go/src . `net/http`/.Requestscip-go gomod github.com/golang/go/src . `net/http`/Request#) (*httpscip-go gomod github.com/golang/go/src . `net/http`/.Responsescip-go gomod github.com/golang/go/src . `net/http`/Response#, error) { |
| 98 | resp, err := c.clientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/DefaultHTTPClient#client..Doscip-go gomod github.com/golang/go/src . `net/http`/Client#Do().(req) |
| 99 | if err != nil { |
| 100 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("performing HTTP request: %w", err) |
| 101 | } |
| 102 | |
| 103 | return resp, nil |
| 104 | } |
| 105 | |
| 106 | // GetID returns the identifier for this service. |
| 107 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#) GetIDscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#GetID().() string { |
| 108 | return Schemescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Scheme. |
| 109 | } |
| 110 | |
| 111 | // Initialize configures the service with a URL and logger. |
| 112 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#) Initializescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#Initialize().(serviceURL *urlscip-go gomod github.com/golang/go/src . `net/url`/.URLscip-go gomod github.com/golang/go/src . `net/url`/URL#, logger typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.StdLoggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/StdLogger#) error { |
| 113 | s.SetLoggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/standard`/Logger#SetLogger().(logger) |
| 114 | s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#Config. = &Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Config#{} |
| 115 | |
| 116 | s.pkrscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#pkr. = formatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/.NewPropKeyResolverscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/NewPropKeyResolver().(s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#Config.) |
| 117 | if s.HTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#HTTPClient. == nil { |
| 118 | s.HTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#HTTPClient. = NewDefaultHTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/NewDefaultHTTPClient().() |
| 119 | } |
| 120 | |
| 121 | s.contentMaxSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#contentMaxSize. = contentMaxSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/contentMaxSize. |
| 122 | s.topicMaxLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#topicMaxLength. = topicMaxLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/topicMaxLength. |
| 123 | |
| 124 | if err := s.pkrscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#pkr..SetDefaultPropsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#SetDefaultProps().(s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#Config.); err != nil { |
| 125 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("setting default properties: %w", err) |
| 126 | } |
| 127 | |
| 128 | if err := s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#Config..SetURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Config#SetURL().(serviceURL); err != nil { |
| 129 | return err |
| 130 | } |
| 131 | |
| 132 | return nil |
| 133 | } |
| 134 | |
| 135 | // Send delivers a notification message to Zulip. |
| 136 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#) Sendscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#Send().(message string, params *typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.Paramsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/Params#) error { |
| 137 | return s.SendWithContextscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#SendWithContext().(contextscip-go gomod github.com/golang/go/src . context/.Backgroundscip-go gomod github.com/golang/go/src . context/Background().(), message, params) |
| 138 | } |
| 139 | |
| 140 | // SendWithContext delivers a notification message to Zulip with context support. |
| 141 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#) SendWithContextscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#SendWithContext().(ctx contextscip-go gomod github.com/golang/go/src . context/.Contextscip-go gomod github.com/golang/go/src . context/Context#, message string, params *typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.Paramsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/Params#) error { |
| 142 | // Clone the config to avoid modifying the original for this send operation. |
| 143 | config := s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#Config..Clonescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Config#Clone().() |
| 144 | |
| 145 | // Use PropKeyResolver for consistent param handling |
| 146 | if err := s.pkrscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#pkr..UpdateConfigFromParamsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/format`/PropKeyResolver#UpdateConfigFromParams().(config, params); err != nil { |
| 147 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("updating config from params: %w", err) |
| 148 | } |
| 149 | |
| 150 | // If no topic is set, fall back to using the title as the topic |
| 151 | // for backward compatibility with tools that only provide a title. |
| 152 | if config.Topicscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Config#Topic. == "" && config.Titlescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Config#Title. != "" { |
| 153 | config.Topicscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Config#Topic. = config.Titlescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Config#Title. |
| 154 | config.Titlescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Config#Title. = "" |
| 155 | } |
| 156 | |
| 157 | if err := ValidateMessageTypescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/ValidateMessageType().(config.Typescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Config#Type.); err != nil { |
| 158 | return err |
| 159 | } |
| 160 | |
| 161 | if err := validateHostscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/validateHost().(config.Hostscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Config#Host.); err != nil { |
| 162 | return err |
| 163 | } |
| 164 | |
| 165 | s.muscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#mu..Doscip-go gomod github.com/golang/go/src . sync/Once#Do().(func() { s.fetchLimitsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#fetchLimits().(ctx) }) |
| 166 | |
| 167 | topicLength := len([]rune(config.Topicscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Config#Topic.)) |
| 168 | if topicLength > s.topicMaxLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#topicMaxLength. { |
| 169 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().( |
| 170 | "%w: %d characters, got %d", |
| 171 | ErrTopicTooLongscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/ErrTopicTooLong., |
| 172 | s.topicMaxLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#topicMaxLength., |
| 173 | topicLength, |
| 174 | ) |
| 175 | } |
| 176 | |
| 177 | // Prepend title to message content when both topic and title are set. |
| 178 | if config.Titlescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Config#Title. != "" { |
| 179 | message = fmtscip-go gomod github.com/golang/go/src . fmt/.Sprintfscip-go gomod github.com/golang/go/src . fmt/Sprintf().("%s\n\n%s", config.Titlescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Config#Title., message) |
| 180 | } |
| 181 | |
| 182 | messageSize := len(message) |
| 183 | if messageSize > s.contentMaxSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#contentMaxSize. { |
| 184 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().( |
| 185 | "%w: %d bytes, got %d bytes", |
| 186 | ErrMessageTooLongscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/ErrMessageTooLong., |
| 187 | s.contentMaxSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#contentMaxSize., |
| 188 | messageSize, |
| 189 | ) |
| 190 | } |
| 191 | |
| 192 | if config.Typescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Config#Type. == MessageTypeDirectscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/MessageTypeDirect. { |
| 193 | recipients := config.Toscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Config#To. |
| 194 | if recipients == "" && config.Streamscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Config#Stream. != "" { |
| 195 | recipients = config.Streamscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Config#Stream. |
| 196 | } |
| 197 | |
| 198 | if recipients == "" { |
| 199 | return ErrMissingRecipientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/ErrMissingRecipient. |
| 200 | } |
| 201 | } else if config.Streamscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Config#Stream. == "" { |
| 202 | return ErrMissingRecipientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/ErrMissingRecipient. |
| 203 | } |
| 204 | |
| 205 | return s.doSendscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#doSend().(ctx, config, message) |
| 206 | } |
| 207 | |
| 208 | // SetHTTPClient sets a custom HTTP client for the service. |
| 209 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#) SetHTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#SetHTTPClient().(client typesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/.HTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/types`/HTTPClient#) { |
| 210 | s.HTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#HTTPClient. = client |
| 211 | } |
| 212 | |
| 213 | // doSend sends the notification to Zulip using the configured API URL. |
| 214 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#) doSendscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#doSend().(ctx contextscip-go gomod github.com/golang/go/src . context/.Contextscip-go gomod github.com/golang/go/src . context/Context#, config *Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Config#, message string) error { |
| 215 | apiURL := s.getAPIURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#getAPIURL().(config) |
| 216 | |
| 217 | payload := CreatePayloadscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/CreatePayload().(config, message) |
| 218 | |
| 219 | req, err := httpscip-go gomod github.com/golang/go/src . `net/http`/.NewRequestWithContextscip-go gomod github.com/golang/go/src . `net/http`/NewRequestWithContext().( |
| 220 | ctx, |
| 221 | httpscip-go gomod github.com/golang/go/src . `net/http`/.MethodPostscip-go gomod github.com/golang/go/src . `net/http`/MethodPost., |
| 222 | apiURL, |
| 223 | stringsscip-go gomod github.com/golang/go/src . strings/.NewReaderscip-go gomod github.com/golang/go/src . strings/NewReader().(payload.Encodescip-go gomod github.com/golang/go/src . `net/url`/Values#Encode().()), |
| 224 | ) |
| 225 | if err != nil { |
| 226 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("creating request: %w", err) |
| 227 | } |
| 228 | |
| 229 | req.Headerscip-go gomod github.com/golang/go/src . `net/http`/Request#Header..Setscip-go gomod github.com/golang/go/src . `net/http`/Header#Set().("Content-Type", "application/x-www-form-urlencoded") |
| 230 | |
| 231 | res, err := s.HTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#HTTPClient..Doscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/HTTPClient#Do.(req) |
| 232 | if err != nil { |
| 233 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("making HTTP POST request: %w", err) |
| 234 | } |
| 235 | |
| 236 | defer func() { _ = res.Bodyscip-go gomod github.com/golang/go/src . `net/http`/Response#Body..Closescip-go gomod github.com/golang/go/src . io/Closer#Close().() }() |
| 237 | |
| 238 | if res.StatusCodescip-go gomod github.com/golang/go/src . `net/http`/Response#StatusCode. != httpscip-go gomod github.com/golang/go/src . `net/http`/.StatusOKscip-go gomod github.com/golang/go/src . `net/http`/StatusOK. { |
| 239 | body, readErr := ioscip-go gomod github.com/golang/go/src . io/.ReadAllscip-go gomod github.com/golang/go/src . io/ReadAll().(ioscip-go gomod github.com/golang/go/src . io/.LimitReaderscip-go gomod github.com/golang/go/src . io/LimitReader().(res.Bodyscip-go gomod github.com/golang/go/src . `net/http`/Response#Body., maxErrorBodySizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/maxErrorBodySize.+1)) |
| 240 | if readErr == nil && len(body) > 0 { |
| 241 | trunc := "" |
| 242 | |
| 243 | if len(body) > maxErrorBodySizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/maxErrorBodySize. { |
| 244 | body = body[:maxErrorBodySizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/maxErrorBodySize.] |
| 245 | trunc = " [truncated]" |
| 246 | } |
| 247 | |
| 248 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("failed to send zulip message: %w: %d, body: %s%s", ErrResponseStatusFailurescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/ErrResponseStatusFailure., res.StatusCodescip-go gomod github.com/golang/go/src . `net/http`/Response#StatusCode., string(body), trunc) |
| 249 | } |
| 250 | |
| 251 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("failed to send zulip message: %w: %d", ErrResponseStatusFailurescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/ErrResponseStatusFailure., res.StatusCodescip-go gomod github.com/golang/go/src . `net/http`/Response#StatusCode.) |
| 252 | } |
| 253 | |
| 254 | return nil |
| 255 | } |
| 256 | |
| 257 | // fetchLimits calls the Zulip register endpoint to fetch server-side size limits. |
| 258 | // Uses defaults if the call fails. |
| 259 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#) fetchLimitsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#fetchLimits().(ctx contextscip-go gomod github.com/golang/go/src . context/.Contextscip-go gomod github.com/golang/go/src . context/Context#) { |
| 260 | if err := validateHostscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/validateHost().(s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#Config..Hostscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Config#Host.); err != nil { |
| 261 | return |
| 262 | } |
| 263 | |
| 264 | registerURL := s.getRegisterURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#getRegisterURL().() |
| 265 | |
| 266 | ctx, cancel := contextscip-go gomod github.com/golang/go/src . context/.WithTimeoutscip-go gomod github.com/golang/go/src . context/WithTimeout().(ctx, registerTimeoutscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/registerTimeout.) |
| 267 | defer cancel() |
| 268 | |
| 269 | form := urlscip-go gomod github.com/golang/go/src . `net/url`/.Valuesscip-go gomod github.com/golang/go/src . `net/url`/Values#{} |
| 270 | form.Setscip-go gomod github.com/golang/go/src . `net/url`/Values#Set().("fetch_event_types", `["realm"]`) |
| 271 | |
| 272 | req, err := httpscip-go gomod github.com/golang/go/src . `net/http`/.NewRequestWithContextscip-go gomod github.com/golang/go/src . `net/http`/NewRequestWithContext().(ctx, httpscip-go gomod github.com/golang/go/src . `net/http`/.MethodPostscip-go gomod github.com/golang/go/src . `net/http`/MethodPost., registerURL, stringsscip-go gomod github.com/golang/go/src . strings/.NewReaderscip-go gomod github.com/golang/go/src . strings/NewReader().(form.Encodescip-go gomod github.com/golang/go/src . `net/url`/Values#Encode().())) |
| 273 | if err != nil { |
| 274 | return |
| 275 | } |
| 276 | |
| 277 | req.Headerscip-go gomod github.com/golang/go/src . `net/http`/Request#Header..Setscip-go gomod github.com/golang/go/src . `net/http`/Header#Set().("Content-Type", "application/x-www-form-urlencoded") |
| 278 | |
| 279 | res, err := s.HTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#HTTPClient..Doscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/HTTPClient#Do.(req) |
| 280 | if err != nil { |
| 281 | return |
| 282 | } |
| 283 | |
| 284 | defer func() { _ = res.Bodyscip-go gomod github.com/golang/go/src . `net/http`/Response#Body..Closescip-go gomod github.com/golang/go/src . io/Closer#Close().() }() |
| 285 | |
| 286 | if res.StatusCodescip-go gomod github.com/golang/go/src . `net/http`/Response#StatusCode. != httpscip-go gomod github.com/golang/go/src . `net/http`/.StatusOKscip-go gomod github.com/golang/go/src . `net/http`/StatusOK. { |
| 287 | return |
| 288 | } |
| 289 | |
| 290 | var reg registerResponsescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/registerResponse# |
| 291 | if err := jsonscip-go gomod github.com/golang/go/src . `encoding/json`/.NewDecoderscip-go gomod github.com/golang/go/src . `encoding/json`/NewDecoder().(res.Bodyscip-go gomod github.com/golang/go/src . `net/http`/Response#Body.).Decodescip-go gomod github.com/golang/go/src . `encoding/json`/Decoder#Decode().(®); err != nil { |
| 292 | return |
| 293 | } |
| 294 | |
| 295 | if reg.MaxMessageLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/registerResponse#MaxMessageLength. > 0 { |
| 296 | s.contentMaxSizescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#contentMaxSize. = reg.MaxMessageLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/registerResponse#MaxMessageLength. |
| 297 | } |
| 298 | |
| 299 | if reg.MaxTopicLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/registerResponse#MaxTopicLength. > 0 { |
| 300 | s.topicMaxLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#topicMaxLength. = reg.MaxTopicLengthscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/registerResponse#MaxTopicLength. |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | // getAPIURL constructs the API URL for Zulip based on the Config. |
| 305 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#) getAPIURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#getAPIURL().(config *Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Config#) string { |
| 306 | return (&urlscip-go gomod github.com/golang/go/src . `net/url`/.URLscip-go gomod github.com/golang/go/src . `net/url`/URL#{ |
| 307 | Userscip-go gomod github.com/golang/go/src . `net/url`/URL#User.: urlscip-go gomod github.com/golang/go/src . `net/url`/.UserPasswordscip-go gomod github.com/golang/go/src . `net/url`/UserPassword().(config.BotMailscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Config#BotMail., config.BotKeyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Config#BotKey.), |
| 308 | Hostscip-go gomod github.com/golang/go/src . `net/url`/URL#Host.: config.Hostscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Config#Host., |
| 309 | Pathscip-go gomod github.com/golang/go/src . `net/url`/URL#Path.: "/api/v1/messages", |
| 310 | Schemescip-go gomod github.com/golang/go/src . `net/url`/URL#Scheme.: "https", |
| 311 | }).Stringscip-go gomod github.com/golang/go/src . `net/url`/URL#String().() |
| 312 | } |
| 313 | |
| 314 | // getRegisterURL constructs the register API URL from the service config. |
| 315 | func (s *Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#) getRegisterURLscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#getRegisterURL().() string { |
| 316 | return (&urlscip-go gomod github.com/golang/go/src . `net/url`/.URLscip-go gomod github.com/golang/go/src . `net/url`/URL#{ |
| 317 | Userscip-go gomod github.com/golang/go/src . `net/url`/URL#User.: urlscip-go gomod github.com/golang/go/src . `net/url`/.UserPasswordscip-go gomod github.com/golang/go/src . `net/url`/UserPassword().(s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#Config..BotMailscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Config#BotMail., s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#Config..BotKeyscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Config#BotKey.), |
| 318 | Hostscip-go gomod github.com/golang/go/src . `net/url`/URL#Host.: s.Configscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Service#Config..Hostscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/chat/zulip`/Config#Host., |
| 319 | Pathscip-go gomod github.com/golang/go/src . `net/url`/URL#Path.: "/api/v1/register", |
| 320 | Schemescip-go gomod github.com/golang/go/src . `net/url`/URL#Scheme.: "https", |
| 321 | }).Stringscip-go gomod github.com/golang/go/src . `net/url`/URL#String().() |
| 322 | } |