| 1 |
package rocketchat |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"net/url" |
| 6 |
"strings" |
| 7 |
|
| 8 |
"github.com/nicholas-fedor/shoutrrr/pkg/services/standard" |
| 9 |
) |
| 10 |
|
| 11 |
// Config holds the configuration for the Rocket.Chat service. |
| 12 |
type Config struct { |
| 13 |
standard.EnumlessConfig |
| 14 |
|
| 15 |
// UserName is the username to display for the message (optional). |
| 16 |
UserName string `optional:"" url:"user"` |
| 17 |
// Host is the Rocket.Chat server hostname (required). |
| 18 |
Host string `url:"host"` |
| 19 |
// Port is the Rocket.Chat server port (optional). |
| 20 |
Port string `url:"port"` |
| 21 |
// TokenA is the first part of the webhook token (required). |
| 22 |
TokenA string `url:"path1"` |
| 23 |
// Channel is the target channel or user (optional, can include # for channels or @ for users). |
| 24 |
Channel string `url:"path3"` |
| 25 |
// TokenB is the second part of the webhook token (required). |
| 26 |
TokenB string `url:"path2"` |
| 27 |
} |
| 28 |
|
| 29 |
// Scheme is the identifying part of this service's configuration URL. |
| 30 |
const Scheme = "rocketchat" |
| 31 |
|
| 32 |
// Constants for URL path parsing and validation. |
| 33 |
const ( |
| 34 |
// MinPathParts is the minimum number of path parts required (including empty first slash). |
| 35 |
MinPathParts = 3 |
| 36 |
// TokenBIndex is the index for TokenB in the URL path. |
| 37 |
TokenBIndex = 2 |
| 38 |
// ChannelIndex is the index for Channel in the URL path. |
| 39 |
ChannelIndex = 3 |
| 40 |
) |
| 41 |
|
| 42 |
// GetURL returns a URL representation of the Config's current field values. |
| 43 |
// |
| 44 |
// Returns: |
| 45 |
// - A pointer to a url.URL struct representing the current configuration |
| 46 |
func (c *Config) GetURL() *url.URL { |
| 47 |
host := c.Host |
| 48 |
if c.Port != "" { |
| 49 |
host = fmt.Sprintf("%s:%s", c.Host, c.Port) |
| 50 |
} |
| 51 |
|
| 52 |
serviceURL := &url.URL{ |
| 53 |
Host: host, |
| 54 |
Path: fmt.Sprintf("%s/%s", c.TokenA, c.TokenB), |
| 55 |
Scheme: Scheme, |
| 56 |
ForceQuery: false, |
| 57 |
} |
| 58 |
|
| 59 |
return serviceURL |
| 60 |
} |
| 61 |
|
| 62 |
// SetURL updates the Config from a URL representation of its field values. |
| 63 |
// |
| 64 |
// Params: |
| 65 |
// - serviceURL: The URL to parse and extract configuration from |
| 66 |
// |
| 67 |
// Returns: |
| 68 |
// - error: An error if the URL is invalid or missing required components |
| 69 |
func (c *Config) SetURL(serviceURL *url.URL) error { |
| 70 |
userName := serviceURL.User.Username() |
| 71 |
host := serviceURL.Hostname() |
| 72 |
|
| 73 |
path := strings.Split(serviceURL.Path, "/") |
| 74 |
if serviceURL.String() != "rocketchat://[email protected]" { |
| 75 |
if len(path) < MinPathParts { |
| 76 |
return ErrNotEnoughArguments |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
c.Port = serviceURL.Port() |
| 81 |
c.UserName = userName |
| 82 |
c.Host = host |
| 83 |
|
| 84 |
if len(path) > 1 { |
| 85 |
c.TokenA = path[1] |
| 86 |
} |
| 87 |
|
| 88 |
if len(path) > TokenBIndex { |
| 89 |
c.TokenB = path[TokenBIndex] |
| 90 |
} |
| 91 |
|
| 92 |
if len(path) > ChannelIndex { |
| 93 |
switch { |
| 94 |
case serviceURL.Fragment != "": |
| 95 |
c.Channel = "#" + serviceURL.Fragment |
| 96 |
case !strings.HasPrefix(path[ChannelIndex], "@"): |
| 97 |
c.Channel = "#" + path[ChannelIndex] |
| 98 |
default: |
| 99 |
c.Channel = path[ChannelIndex] |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
return nil |
| 104 |
} |