commit 8ef218c1ec094744a00d469cd603a243fd0aaa81 Author: jangxx Date: Tue Jul 18 20:14:27 2017 +0200 initial commit, added basically all basic functionality diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..de8bda9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules +config.json +friends.txt diff --git a/config.example.json b/config.example.json new file mode 100644 index 0000000..bd3bfca --- /dev/null +++ b/config.example.json @@ -0,0 +1,13 @@ +{ + "pushover": { + "user": "", + "token": "" + }, + "ts-notify": { + "address": "localhost", + "port": 6000, + "friendly_name": "", + "apikey": "" + }, + "follow_file": "" +} diff --git a/index.js b/index.js new file mode 100644 index 0000000..a511bb7 --- /dev/null +++ b/index.js @@ -0,0 +1,63 @@ +const io = require('socket.io-client'); +const push = require('pushover-notifications'); +const util = require('util'); +const fs = require('fs'); + +const configjson = require('./config.example.json'); + +const ts_notify = configjson["ts-notify"]; + +var p = new push({ + user: configjson.pushover.user, + token: configjson.pushover.token +}); + +var address = "http://" + ts_notify.address + ":" + ts_notify.port; +var socket = io(address); + +socket.on('connect', function() { + util.log("Connected to " + address); + + socket.emit('apiaccess', ts_notify.apikey); +}); + +socket.on('event', function(evt) { + var friends = []; + if(configjson.follow_file != undefined || fs.existsSync(configjson.follow_file)) { + try { + var follow_file = fs.readFileSync(configjson.follow_file, {encoding: "UTF-8"}); + friends = follow_file.trim().split('\n'); + } catch(e) { + util.log("Error while reading friends file: " + err.message); + return; + } + } + + if(friends.indexOf(evt.uid) != -1) { + util.log(evt.name + " " + evt.status); + + var message = evt.name + " " + evt.status; + if (ts_notify.friendly_name != undefined || ts_notify.friendly_name != "") { + if (evt.status == "connected") { + message += " to "; + } else if (evt.status == "disconnected") { + message += " from "; + } else { + message += " at "; + } + message += ts_notify.friendly_name; + } + + p.send({ + message: message + }, function(err, result) { + if(err) { + util.log("Error while sending push: " + err.message); + } + }); + } +}); + +function htmlEntities(str) { + return String(str).replace(/&/g, '&').replace(//g, '>').replace(new RegExp('"', 'g'), '"'); +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..f22999c --- /dev/null +++ b/package.json @@ -0,0 +1,19 @@ +{ + "name": "ts-notify-pushover", + "version": "1.0.0", + "description": "Provides a link between the ts-notify-server and the Pushover service", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git@j4ns.de:git/ts-notify-pushover.git" + }, + "author": "jangxx (http://www.jangxx.com)", + "license": "ISC", + "dependencies": { + "pushover-notifications": "^0.2.4", + "socket.io-client": "^2.0.3" + } +}