|
| 1 | +package notify |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/url" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + xbmcDefaultPort = 8080 |
| 13 | + xbmcRemoteProtocol = 2 |
| 14 | + kodiRemoteProtocol = 6 |
| 15 | + xbmcImageSize = "128x128" |
| 16 | + xbmcJSONRPCPath = "/jsonrpc" |
| 17 | + xbmcNotifyTypeInfo = "info" |
| 18 | + xbmcNotifyTypeWarn = "warning" |
| 19 | + xbmcNotifyTypeError = "error" |
| 20 | +) |
| 21 | + |
| 22 | +type XBMCTarget struct { |
| 23 | + host string |
| 24 | + port int |
| 25 | + secure bool |
| 26 | + user string |
| 27 | + password string |
| 28 | + protocol int |
| 29 | + includeImage bool |
| 30 | + duration int |
| 31 | +} |
| 32 | + |
| 33 | +func NewXBMCTarget(target *ParsedURL) (*XBMCTarget, error) { |
| 34 | + if target == nil { |
| 35 | + return nil, fmt.Errorf("missing target") |
| 36 | + } |
| 37 | + |
| 38 | + host := strings.TrimSpace(target.Host) |
| 39 | + if host == "" { |
| 40 | + return nil, fmt.Errorf("missing host") |
| 41 | + } |
| 42 | + |
| 43 | + schema := strings.ToLower(strings.TrimSpace(target.Scheme)) |
| 44 | + protocol := kodiRemoteProtocol |
| 45 | + if strings.HasPrefix(schema, "xbmc") { |
| 46 | + protocol = xbmcRemoteProtocol |
| 47 | + } |
| 48 | + |
| 49 | + secure := strings.HasSuffix(schema, "s") |
| 50 | + port := target.Port |
| 51 | + if !target.HasPort && protocol == xbmcRemoteProtocol { |
| 52 | + port = xbmcDefaultPort |
| 53 | + } |
| 54 | + |
| 55 | + includeImage := parseBoolWithDefault(target.Query["image"], true) |
| 56 | + duration := 12 |
| 57 | + if raw := strings.TrimSpace(target.Query["duration"]); raw != "" { |
| 58 | + if parsed, err := strconv.Atoi(raw); err == nil { |
| 59 | + if parsed < 0 { |
| 60 | + parsed = -parsed |
| 61 | + } |
| 62 | + duration = parsed |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return &XBMCTarget{ |
| 67 | + host: host, |
| 68 | + port: port, |
| 69 | + secure: secure, |
| 70 | + user: strings.TrimSpace(target.User), |
| 71 | + password: target.Password, |
| 72 | + protocol: protocol, |
| 73 | + includeImage: includeImage, |
| 74 | + duration: duration, |
| 75 | + }, nil |
| 76 | +} |
| 77 | + |
| 78 | +func (x *XBMCTarget) BuildRequest(body, title string, notifyType NotifyType) (RequestSpec, error) { |
| 79 | + payload := map[string]any{ |
| 80 | + "jsonrpc": "2.0", |
| 81 | + "method": "GUI.ShowNotification", |
| 82 | + "params": map[string]any{ |
| 83 | + "title": title, |
| 84 | + "message": body, |
| 85 | + "displaytime": int(x.duration * 1000), |
| 86 | + }, |
| 87 | + "id": 1, |
| 88 | + } |
| 89 | + |
| 90 | + if x.includeImage { |
| 91 | + imageURL := appriseImageURL(notifyType, xbmcImageSize) |
| 92 | + if imageURL != "" { |
| 93 | + payload["params"].(map[string]any)["image"] = imageURL |
| 94 | + if x.protocol != xbmcRemoteProtocol { |
| 95 | + switch notifyType { |
| 96 | + case NotifyFailure: |
| 97 | + payload["type"] = xbmcNotifyTypeError |
| 98 | + case NotifyWarning: |
| 99 | + payload["type"] = xbmcNotifyTypeWarn |
| 100 | + default: |
| 101 | + payload["type"] = xbmcNotifyTypeInfo |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + data, err := json.Marshal(payload) |
| 108 | + if err != nil { |
| 109 | + return RequestSpec{}, err |
| 110 | + } |
| 111 | + |
| 112 | + headers := map[string]string{ |
| 113 | + "User-Agent": "Apprise", |
| 114 | + "Content-Type": "application/json", |
| 115 | + } |
| 116 | + if x.user != "" { |
| 117 | + headers["Authorization"] = basicAuthHeader(x.user, x.password) |
| 118 | + } |
| 119 | + |
| 120 | + return RequestSpec{ |
| 121 | + Method: "POST", |
| 122 | + URL: x.notifyURL(), |
| 123 | + Headers: headers, |
| 124 | + Body: string(data), |
| 125 | + }, nil |
| 126 | +} |
| 127 | + |
| 128 | +func (x *XBMCTarget) Send(body, title string, notifyType NotifyType) error { |
| 129 | + spec, err := x.BuildRequest(body, title, notifyType) |
| 130 | + if err != nil { |
| 131 | + return err |
| 132 | + } |
| 133 | + |
| 134 | + return SendRequest(spec) |
| 135 | +} |
| 136 | + |
| 137 | +func (x *XBMCTarget) notifyURL() string { |
| 138 | + scheme := "http" |
| 139 | + if x.secure { |
| 140 | + scheme = "https" |
| 141 | + } |
| 142 | + |
| 143 | + host := x.host |
| 144 | + if x.port > 0 { |
| 145 | + host = host + ":" + strconv.Itoa(x.port) |
| 146 | + } |
| 147 | + |
| 148 | + u := url.URL{ |
| 149 | + Scheme: scheme, |
| 150 | + Host: host, |
| 151 | + Path: xbmcJSONRPCPath, |
| 152 | + } |
| 153 | + |
| 154 | + return u.String() |
| 155 | +} |
0 commit comments