-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
381 lines (312 loc) · 11.6 KB
/
index.js
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
var ws = require('ws')
, fs = require('fs')
, querystring = require('querystring')
, https = require('https')
, Notification = require('node-notifier')
, path = require('path')
/**
* Handles everything for showing Pushover notifications, just call #connect()
*
* @param {Object} settings Instance configuration
* @param {String} settings.deviceId The device id for your Pushover notification stream
* @param {String} settings.secret The secret for your Pushover notification stream
* @param {String} [settings.imageCache=null] Path to the image cache directory, used for app icons
* @param {String} [settings.wsHost='wss://client.pushover.net/push'] Pushover websocket host to connect to
* @param {String} [settings.iconHost='client.pushover.net'] Pushover icon host
* @param {String} [settings.apiHost='api.pushover.net'] Pushover API host
* @param {String} [settings.apiPath='/1'] Pushover API version, mostly
* @param {Number} [settings.keepAliveTimeout=60000] Time to wait for a keep alive message before considering the
* connection dead, also used for connection attempt rate limiting
* @param {Object} [settings.notifier=Notification] Notification subsystem to use, mostly here for test support
* @param {Object} [settings.https=https] https lib to use, mostly here for test support
* @param {Object} [settings.logger=console] logger to use, mostly here for test support
*
* @constructor
*/
var Client = function (settings) {
this.settings = settings
this.settings.wsHost = settings.wsHost || 'wss://client.pushover.net/push'
this.settings.iconHost = settings.iconHost || 'client.pushover.net'
this.settings.apiHost = settings.apiHost || 'api.pushover.net'
this.settings.apiPath = settings.apiPath || '/1'
this.settings.keepAliveTimeout = settings.keepAliveTimeout || 60000
this.notifier = new Notification()
this.https = settings.https || https
this.logger = settings.logger || console
}
module.exports = Client
/**
* Handles the websocket connection
* Sets up triggered message refreshing as well as an initial refresh to ensure we haven't missed anything
*/
Client.prototype.connect = function () {
var self = this
if (self.wsClient) {
return
}
var wsClient = new ws(self.settings.wsHost)
self._lastConnection = Date.now()
wsClient.on('open', function () {
self.refreshMessages()
self.logger.log('Websocket client connected, waiting for new messages')
self.resetKeepAlive()
wsClient.send('login:' + self.settings.deviceId + ':' + self.settings.secret + '\n')
})
wsClient.on('message', function (event) {
var message = event.toString('utf8')
//New message
if (message === '!') {
self.logger.log('Got new message event')
return self.refreshMessages()
//Keep alive message
} else if (message === '#') {
self.resetKeepAlive()
return
}
self.logger.error('Unknown message:', message)
self.reconnect()
})
wsClient.on('error', function (error) {
self.logger.error('Websocket connection error')
self.logger.error(error.stack || error)
self.reconnect()
})
wsClient.on('close', function () {
self.logger.log('Websocket connection closed, reconnecting')
self.reconnect()
})
self.wsClient = wsClient
}
/**
* Resets the websocket client termination timer
* If the timer isn't reset in time the websocket client is reconnected
*/
Client.prototype.resetKeepAlive = function () {
var self = this
clearTimeout(self._keepAlive)
self._keepAlive = setTimeout(function () {
self.logger.error('Did not receive a keep alive message in time, closing connection')
self.reconnect()
}, self.settings.keepAliveTimeout)
}
/**
* Handles clearing the old websocket client and reconnecting
* Avoids spamming the websocket server
*/
Client.prototype.reconnect = function () {
var self = this
clearTimeout(self._keepAlive)
try {
self.wsClient.removeAllListeners()
self.wsClient.terminate()
self.wsClient = null
} catch (e) {}
self._reconnect = setTimeout(function () {
clearTimeout(self._reconnect)
self.connect()
}, self.settings.keepAliveTimeout - (Date.now() - self._lastConnection))
}
/**
* Makes an https request to Pushover to get all messages we haven't seen yet
* Notifications will be generated for any new messages
*/
Client.prototype.refreshMessages = function () {
var self = this
self.logger.log('Refreshing messages')
var options = {
host: self.settings.apiHost
, method: 'GET'
, path: self.settings.apiPath + '/messages.json?' + querystring.stringify({
secret: self.settings.secret
, device_id: self.settings.deviceId
})
}
var request = self.https.request(options, function (response) {
var finalData = ''
response.on('data', function (data) {
finalData += data.toString()
})
response.on('end', function () {
if (response.statusCode !== 200) {
self.logger.error('Error while refreshing messages')
self.logger.error(finalData)
return
}
try {
var payload = JSON.parse(finalData)
self.notify(payload.messages)
} catch (error) {
self.logger.error('Failed to parse message payload')
self.logger.error(error.stack || error)
}
})
})
request.on('error', function (error) {
self.logger.error('Error while refreshing messages')
self.logger.error(error.stack || error)
})
request.end()
}
/**
* Takes a list of message, prepares them, and sends to the notify subsystem
* After all notifications are processed updateHead is called to clear them from Pushover for the configured deviceId
*
* @param {Client~PushoverMessage[]} messages A list of pushover message objects
*/
Client.prototype.notify = function (messages) {
var self = this,
lastMessage,
useMessages = messages
var next = function () {
var message = useMessages.shift(),
icon
if (!message) {
self.updateHead(lastMessage)
return
}
lastMessage = message
if (message.icon) {
icon = message.icon + '.png'
} else if (message.aid === 1) {
icon = 'pushover.png'
} else {
icon = 'default.png'
}
try {
self.fetchImage(icon, function (imageFile) {
var payload = {appIcon: imageFile}
payload.title = message.title || message.app
if (message.message) {
payload.message = message.message
}
self.logger.log('Sending notification for', message.id)
try {
self.notifier.notify(payload, function (error) {
if (error) {
self.logger.error('Returned error while trying to send the notification')
self.logger.error(error.stack || error)
}
})
} catch (error) {
self.logger.error('Caught error while trying to send the notification')
self.logger.error(error.stack || error)
next()
return
}
next()
})
} catch (error) {
self.logger.error('Caught error while trying to fetch the image')
self.logger.error(error.stack || error)
next()
}
}
next()
}
/**
* Fetches an image from Pushover and stuffs it in a cache dir
* If the image already exists in the cache dir the fetch is skipped
*
* @param {String} imageName The name of the image, from the message object
* @param {Client~FetchCallback} callback A function to call once this has completed, the image path is provided or false if no
* image could be fetched
*/
Client.prototype.fetchImage = function (imageName, callback) {
var self = this
if (!self.settings.imageCache) {
return callback(false)
}
var imageFile = path.join(self.settings.imageCache, imageName)
if (fs.existsSync(imageFile)) {
return callback(imageFile)
}
self.logger.log('Caching image for', imageName)
var options = {
host: self.settings.iconHost
, method: 'GET'
, path: '/icons/' + imageName
}
var request = self.https.request(options, function (response) {
try {
response.pipe(fs.createWriteStream(imageFile))
} catch (error) {
self.logger.error('FS error while caching image', imageName)
self.logger.error(error.stack || error)
return callback(false)
}
response.on('end', function () {
if (response.statusCode !== 200) {
self.logger.error('HTTP error while caching image', imageName, 'statusCode:', response.statusCode)
return callback(false)
}
callback(imageFile)
})
})
request.on('error', function (error) {
self.logger.error('Request error while caching image', imageName)
self.logger.error(error.stack || error)
callback(false)
})
request.end()
}
/**
* Updates the last seen message with Pushover
* Any messages below this id will *not* be re-synced
*
* @param {Client~PushoverMessage} message The last message received from an update
*/
Client.prototype.updateHead = function (message) {
var self = this
if (!message) {
return
}
self.logger.log('Updating head position to', message.id)
var options = {
host: self.settings.apiHost
, method: 'POST'
, path: self.settings.apiPath + '/devices/' + self.settings.deviceId + '/update_highest_message.json'
}
var request = self.https.request(options, function (response) {
var finalData = ''
response.on('data', function (data) {
finalData += data.toString()
})
response.on('end', function () {
if (response.statusCode !== 200) {
self.logger.error('Error while updating head')
self.logger.error(finalData)
}
})
})
request.on('error', function (error) {
self.logger.error('Error while refreshing messages')
self.logger.error(error.stack || error)
})
request.write(querystring.stringify({
secret: self.settings.secret
, message: message.id
}) + '\n')
request.end()
}
/**
* A Pushover message
* Contains everything needed to prepare and display a notification
*
* @typedef {Object} Client~PushoverMessage
*
* @property {Number} id Unique ID of the message
* @property {String} message Actual message to be displayed
* @property {String} app Name of the app that send the message
* @property {Number} aid Id of the app that sent the message
* @property {String} icon Name of the icon for the app that sent the message.
* Seems to always be a png on Pushovers servers
* @property {Number} date Unix time stamp representing the date the message was sent
* @property {Number} priority Message priority
* @property {Number} acked Whether or not the message has been acked by some other client
* @property {Number} umid No idea
*/
/**
* @callback Client~FetchCallback
*
* @param {String|boolean} Either the path to the image on disk or false if no image could be provided
*/