-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcontact.js
143 lines (129 loc) · 4.78 KB
/
contact.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
var parser = require("xml2js").Parser({explicitRoot: false, explicitArray: false, mergeAttrs: true});
var axios = require('axios')
https = require('https');
var PNU = require('google-libphonenumber').PhoneNumberUtil;
var phoneUtil = PNU.getInstance();
const httpclient = axios.create({
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
});
module.exports = function(RED) {
RED.httpAdmin.get('/fritzbox/phonebook/regioncodes', function(req, res, next) {
res.end(JSON.stringify(phoneUtil.getSupportedRegions()));
});
function FritzBoxContact(n) {
RED.nodes.createNode(this,n);
var node = this;
node.topic = n.topic;
node.phonebook = n.phonebook || 0;
node.ccode = n.ccode;
node.config = RED.nodes.getNode(n.device);
var statusupdate = function(status) {
node.status = status;
};
node.config.on('statusUpdate', statusupdate);
var simpleSearch = function(contacts, number) {
var result = [];
contacts.forEach(function(contact) {
if(Array.isArray(contact.telephony.number)) {
contact.telephony.number.forEach(function(number) {
if(number._.includes(number)) {
result.push(contact);
}
});
} else {
if(contact.telephony.number._.includes(number)) {
result.push(contact);
}
}
});
return result;
}
var queryphonebook = function(phonenumber) {
// Query the phonebook
return node.config.fritzbox.services["urn:dslforum-org:service:X_AVM-DE_OnTel:1"].actions.GetPhonebook({'NewPhonebookID': node.phonebook})
.then(function(url) {
// Follow the phonebook link and parse the XML
return httpclient.get(url.NewPhonebookURL);
}).then(function(result) {
return parser.parseStringPromise(result.data)
}).then(function(result) {
// Parse the incoming number
var inNumber;
try {
inNumber = phoneUtil.parse(phonenumber, node.ccode);
} catch(e) {
node.warn(`The provided number ${phonenumber} is not valid for region ${node.ccode}: ${e}`);
return [];
}
if(!phoneUtil.isValidNumber(inNumber)) {
return simpleSearch(result.phonebook.contact, phonenumber);
}
// Search the phonebook for the number
var contacts = [];
result.phonebook.contact.forEach(function(contact) {
function matchNumber(number) {
if (number._.startsWith('**')) return;
if (number._.includes('@')) return;
try {
var numberDE = phoneUtil.parse(number._, node.ccode);
if(phoneUtil.isValidNumber(numberDE) && phoneUtil.isNumberMatch(inNumber, numberDE) === PNU.MatchType.EXACT_MATCH) {
contacts.push(contact);
}
} catch(e) {
node.warn(`The invalid phonebook number ${number._} for region ${node.ccode} will be ignored: ${e}`);
}
};
if(Array.isArray(contact.telephony.number)) {
contact.telephony.number.forEach(matchNumber);
} else {
matchNumber(contact.telephony.number);
}
});
return contacts;
})
};
node.on('input', function(msg) {
if(node.config.state === "ready" && node.config.fritzbox) {
if(msg.payload !== null &&
typeof msg.payload === 'object' &&
!Array.isArray(msg.payload) &&
msg.payload.callee !== undefined &&
msg.payload.caller !== undefined ) {
var caller = queryphonebook(msg.payload.caller).then(function(contacts) {
msg.payload.caller_contacts = contacts;
});
var callee = queryphonebook(msg.payload.callee).then(function(contacts) {
msg.payload.callee_contacts = contacts;
});
Promise.all([caller, callee]).then(function() {
node.send(msg);
}).catch(function(e) {
node.warn(e);
node.send(msg);
});
} else if (msg.payload !== null && typeof msg.payload === 'string') {
queryphonebook(msg.payload).then(function(contacts) {
msg.payload = contacts;
node.send(msg);
}).catch(function(e) {
node.warn(e);
msg.payload = [];
node.send(msg);
});
} else {
node.warn("Invalid input");
node.send(msg);
}
} else {
node.error("Device not ready.", msg);
node.config.reinit();
}
});
node.on('close', function() {
node.config.removeListener('statusUpdate', statusupdate);
});
}
RED.nodes.registerType("fritzbox-contact", FritzBoxContact);
};