-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
320 lines (274 loc) · 6.53 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package main
import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/gorilla/websocket"
"github.com/xmppo/go-xmpp"
)
type PushoverStreamReturn int
const (
PushoverOk PushoverStreamReturn = iota
PushoverError
PushoverReconnectRequest
PushoverReauthenticateRequest
PushoverEndRequest
)
type PushoverMessage struct {
ID int `json:"id"`
IDstr string `json:"id_str"`
UMID int `json:"umid"`
UMIDstr string `json:"umid_str"`
AID int `json:"aid"`
AIDstr string `json:"aid_str"`
App string `json:"app"`
Title string `json:"title"`
Message string `json:"message"`
Icon string `json:"icon"`
Date int `json:"date"`
QueuedDate int `json:"queued_date"`
DispatchedDate int `json:"dispatched_date"`
Priority int `json:"priority"`
Sound string `json:"sound"`
URL string `json:"url"`
URLTitle string `json:"url_title"`
Acked int `json:"acked"`
Receipt string `json:"receipt"`
HTML int `json:"html"`
}
type PushoverMessagesResponse struct {
Status int `json:"status"`
Request string `json:"request"`
Messages []PushoverMessage `json:"messages"`
}
var jabber *xmpp.Client
func main() {
var err error
xmppServer := os.Getenv("PTX_XMPP_SERVER")
xmppUser := os.Getenv("PTX_XMPP_USER")
xmppPassword := os.Getenv("PTX_XMPP_PASSWORD")
xmppTLS, err := strconv.ParseBool(os.Getenv("PTX_XMPP_TLS"))
if err != nil {
xmppTLS = true
}
xmppTarget := os.Getenv("PTX_XMPP_TARGET")
go func() {
if err := jabberConnect(
xmppServer,
xmppUser,
xmppPassword,
xmppTLS,
xmppTarget,
); err != nil {
log.Fatal(err)
}
}()
defer jabber.Close()
deviceId := os.Getenv("PTX_DEVICE_ID")
secret := os.Getenv("PTX_SECRET")
for {
status, err := pushoverStream(deviceId, secret, xmppTarget)
switch status {
case PushoverOk:
log.Println("pushover terminated normally, quitting")
os.Exit(0)
case PushoverError:
log.Printf("pushover error: %s", err)
os.Exit(int(status))
case PushoverReconnectRequest:
log.Println("pushover requested reconnect")
continue
case PushoverReauthenticateRequest:
log.Println("pushover requested re-auth, quitting")
os.Exit(int(status))
case PushoverEndRequest:
log.Println("pushover requested end, quitting")
os.Exit(int(status))
}
}
}
func jabberConnect(server, user, password string, useTLS bool, target string) error {
var err error
xmpp.DefaultConfig = &tls.Config{
ServerName: strings.Split(server, ":")[0],
InsecureSkipVerify: false,
}
jabberOpts := xmpp.Options{
Host: server,
User: user,
Password: password,
NoTLS: !useTLS,
Debug: false,
Session: true,
Status: "xa",
StatusMessage: "Pushing over ...",
}
for {
jabber, err = jabberOpts.NewClient()
if err != nil {
return err
}
jabber.Send(xmpp.Chat{
Remote: target,
Type: "chat",
Text: "Hello World!",
})
for {
chat, err := jabber.Recv()
if err != nil {
jabber.Close()
break
}
switch v := chat.(type) {
case xmpp.Presence:
log.Printf("%+v\n", v)
}
}
}
}
func pushoverStream(deviceId string, secret string, target string) (PushoverStreamReturn, error) {
u := url.URL{
Scheme: "wss",
Host: "client.pushover.net",
Path: "/push",
}
log.Printf("connecting to %s ...", u.String())
ws, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
return PushoverError, err
}
defer ws.Close()
ws.WriteMessage(
websocket.TextMessage,
[]byte(fmt.Sprintf(
"login:%s:%s\n",
deviceId,
secret,
)),
)
for {
_, push, err := ws.ReadMessage()
if err != nil {
return PushoverError, err
}
pushStr := string(push)
switch pushStr {
case "#":
// Keep-alive packet, no response needed.
continue
case "!":
// A new message has arrived; you should perform a sync.
msgs, err := pushoverGetMessages(deviceId, secret)
if err != nil {
log.Println(err)
}
for _, msg := range msgs {
jabber.Send(xmpp.Chat{
Remote: target,
Type: "chat",
Text: pushoverMessageToString(msg),
})
}
if err := pushoverDeleteMessages(deviceId, secret, msgs); err != nil {
log.Println(err)
}
case "R":
// Reload request; you should drop your connection and re-connect.
return PushoverReconnectRequest, nil
case "E":
// Error; a permanent problem occured and you should not automatically
// re-connect. Prompt the user to login again or re-enable the device.
return PushoverReauthenticateRequest, nil
case "A":
// Error; the device logged in from another session and this session is
// being closed. Do not automatically re-connect.
return PushoverEndRequest, nil
}
}
}
func pushoverGetMessages(deviceId, secret string) ([]PushoverMessage, error) {
var err error
u := "https://api.pushover.net/1/messages"
pushoverClient := http.Client{
Timeout: time.Second * 30,
}
req, err := http.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "pushover-to-xmpp")
q := req.URL.Query()
q.Add("device_id", deviceId)
q.Add("secret", secret)
req.URL.RawQuery = q.Encode()
res, err := pushoverClient.Do(req)
if err != nil {
return nil, err
}
if res.Body != nil {
defer res.Body.Close()
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
messagesResponse := PushoverMessagesResponse{}
err = json.Unmarshal(body, &messagesResponse)
if err != nil {
return nil, err
}
if messagesResponse.Status != 1 {
return nil, errors.New(fmt.Sprintf("Status was %d\n", messagesResponse.Status))
}
return messagesResponse.Messages, nil
}
func pushoverDeleteMessages(deviceId, secret string, msgs []PushoverMessage) error {
data := url.Values{
"secret": {secret},
"message": {msgs[len(msgs)-1].IDstr},
}
resp, err := http.PostForm(
"https://api.pushover.net/1/devices/"+
deviceId+"/update_highest_message.json",
data,
)
if err != nil {
return err
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return errors.New("Status code not 200")
}
return nil
}
func pushoverMessageToString(msg PushoverMessage) string {
var s string = ""
s = fmt.Sprintf(
"%s\n\n%s\n",
msg.Title,
msg.Message,
)
if msg.URLTitle != "" {
s = fmt.Sprintf(
"%s\n%s",
s,
msg.URLTitle,
)
}
if msg.URL != "" {
s = fmt.Sprintf(
"%s\n%s",
s,
msg.URL,
)
}
return s
}