-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
117 lines (95 loc) · 2.57 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
'use strict';
/*
# Respsonse Format
Outbound:
Date;CALL;ConnectionId;Extension;CallerId;CalledPhoneNumber;
Inbound:
Date;RING;ConnectionId;CallerId;CalledPhoneNumber;
Connected:
Date;CONNECT;ConnectionId;Extension;Number;
Disconnected:
Date;DISCONNECT;ConnectionID;DurationInSeconds;
*/
var net = require('net');
var events = require('events');
var CallMonitor = function (host, port) {
var self = this;
this.call = {};
port = port || 1012;
function fritzboxDateToUnix(string) {
var d = string.match(/[0-9]{2}/g);
var result = '';
result += '20' + d[2] + '-' + d[1] + '-' + d[0];
result += ' ' + d[3] + ':' + d[4] + ':' + d[5];
return Math.floor(new Date(result).getTime() / 1000);
}
function parseMessage(buffer) {
var message = buffer.toString()
.toLowerCase()
.replace(/[\n\r]$/, '')
.replace(/;$/, '')
.split(';');
message[0] = fritzboxDateToUnix(message[0]);
return message;
}
var client = net.createConnection(port, host);
client.addListener('error', function (error) {
self.emit('error', error);
});
client.addListener('data', function (chunk) {
var data = parseMessage(chunk);
if (data[1] === 'ring') {
self.call[data[2]] = {
type: 'inbound',
start: data[0],
caller: data[3],
called: data[4]
};
self.emit('inbound', {
time: data[0],
caller: data[3],
called: data[4]
});
return;
}
if (data[1] === 'call') {
self.call[data[2]] = {
type: 'outbound',
start: data[0],
extension: data[3],
caller: data[4],
called: data[5]
};
self.emit('outbound', {
time: data[0],
extension: data[3],
caller: data[4],
called: data[5]
});
return;
}
if (data[1] === 'connect') {
self.call[data[2]]['connect'] = data[0];
self.emit('connected', {
time: data[0],
extension: self.call[data[2]]['extension'],
caller: self.call[data[2]]['caller'],
called: self.call[data[2]]['called']
});
return;
}
if (data[1] === 'disconnect') {
self.call[data[2]].disconnect = data[0];
self.call[data[2]].duration = parseInt(data[3], 10);
var call = self.call[data[2]];
delete(self.call[data[2]]);
self.emit('disconnected', call);
return;
}
});
client.addListener('end', function () {
client.end();
});
};
CallMonitor.prototype = new events.EventEmitter();
module.exports = CallMonitor;