-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader-voda.js
162 lines (149 loc) · 5.18 KB
/
reader-voda.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
//
// PagerMon - reader.js
// 2017-06-04
// Author: Dave McKenzie
//
// Description: Takes output of multimon-ng and pushes to PagerMon server
//
// Usage: Invoke via a shell script, ideally
// If not, just pipe multimon's output to it
//
// Example: reader.sh
//
// CONFIG
// create config file if it does not exist, and set defaults
var fs = require('fs');
var conf_defaults = require('./config/default.json');
var conf_file = './config/config-voda.json';
if( ! fs.existsSync(conf_file) ) {
fs.writeFileSync( conf_file, JSON.stringify(conf_defaults,null, 2) );
console.log('created config file - set your api key in '+conf_file);
return;
}
// load the config file
var nconf = require('nconf');
nconf.file({file: conf_file});
nconf.load();
var hostname = nconf.get('hostname');
var apikey = nconf.get('apikey');
var identifier = nconf.get('identifier');
var frequency = process.argv[2];
var sendFunctionCode = nconf.get('sendFunctionCode') || false;
var uri = hostname+"/api/messages";
var http = require('http');
var request = require('request');
require('request').debug = true;
var rp = require('request-promise-native');
var moment = require('moment');
var colors = require('colors/safe');
colors.setTheme({
success: ['green', 'bold'],
error: ['red', 'bold', 'bgwhite']
});
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
terminal: true
});
var frag = {};
rl.on('line', (line) => {
//console.log(`Received: ${line.trim()}`);
var time = moment().format("YYYY-MM-DD HH:mm:ss");
var datetime = moment().unix();
var address;
var message;
var trimMessage;
// TODO: pad address with zeros for better address matching
// if (line.indexOf('POCSAG512: Address:') > -1) {
if (/^POCSAG(\d+): Address: /.test(line) ) {
address = line.match(/POCSAG(\d+): Address:(.*?)Function/)[2].trim();
if (sendFunctionCode) {
address += line.match(/POCSAG(\d+): Address:(.*?)Function: (\d)/)[3];
}
if (line.indexOf('Alpha:') > -1) {
message = line.match(/Alpha:(.*?)$/)[1].trim();
trimMessage = message.replace(/<[A-Za-z]{3}>/g,'');
} else if (line.indexOf('Numeric:') > -1) {
message = line.match(/Numeric:(.*?)$/)[1].trim();
trimMessage = message.replace(/<[A-Za-z]{3}>/g,'');
} else {
message = false;
trimMessage = '';
}
} else if (line.indexOf('FLEX: ') > -1) {
address = line.match(/FLEX:.*?\[(\d*?)\] /)[1].trim();
if (line.match( /( ALN | GPN | NUM)/ )) {
if (line.match( / [0-9]{4}\/[0-9]\/F\/. / )) {
// message is fragmented, hold onto it for next line
frag[address] = line.match(/FLEX:.*?\[\d*\] ... (.*?)$/)[1].trim();
message = false;
trimMessage = '';
} else if (line.match( / [0-9]{4}\/[0-9]\/C\/. / )) {
// message is a completion of the last fragmented message
message = line.match(/FLEX:.*?\[\d*\] ... (.*?)$/)[1].trim();
trimMessage = frag[address]+message;
delete frag[address];
} else if (line.match( / [0-9]{4}\/[0-9]\/K\/. / )) {
// message is a full message
message = line.match(/FLEX:.*?\[\d*\] ... (.*?)$/)[1].trim();
trimMessage = message;
} else {
// message doesn't have the KFC flags, treat as full message
message = line.match(/FLEX:.*?\[\d*\] ... (.*?)$/)[1].trim();
trimMessage = message;
}
}
} else {
address = '';
message = false;
trimMessage = '';
}
// filter out most false hits
// if too much junk data, make sure '-p' option isn't enabled in multimon
if ((address.length > 2 && message) && ((trimMessage.match(/E\d{11}/g)) || (trimMessage.match(/N\d{11}/g)) || (trimMessage.match(/J\d{11}/g)) || (trimMessage.match('Part 2 of 2')) || (trimMessage.match('Branch for cover')))) {
var padAddress = padDigits(address,7);
console.log(colors.red(time+': ')+colors.yellow(frequency+': ')+colors.blue(padAddress+': ')+colors.success(trimMessage));
// now send the message
var form = {
address: padAddress,
message: trimMessage,
datetime: datetime,
source: identifier
};
setTimeout(sendPage, 0, form, 0);
} else {
console.log(colors.red(time+': ')+colors.yellow(frequency+': ')+colors.grey(line));
}
}).on('close', () => {
console.log('Input died!');
});
var sendPage = function(message,retries) {
var options = {
method: 'POST',
uri: uri,
headers: {
'X-Requested-With': 'XMLHttpRequest',
'User-Agent': 'PagerMon reader.js',
apikey: apikey
},
form: message
};
rp(options)
.then(function (body) {
// console.log(colors.success('Message delivered. ID: '+body));
})
.catch(function (err) {
console.log(colors.yellow('Message failed to deliver. '+err));
if (retries < 10) {
var retryTime = Math.pow(2, retries) * 1000;
retries++;
console.log(colors.yellow(`Retrying in ${retryTime} ms`));
setTimeout(sendPage, retryTime, message, retries);
} else {
console.log(colors.yellow('Message failed to deliver after 10 retries, giving up'));
}
});
};
var padDigits = function(number, digits) {
return Array(Math.max(digits - String(number).length + 1, 0)).join(0) + number;
};