-
Notifications
You must be signed in to change notification settings - Fork 34
/
utils.js
125 lines (101 loc) · 3.85 KB
/
utils.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
var hostName = require("os").hostname();
var twilio = require('twilio');
var fromText = "", toText = "";
var twilioSID = "";
var twilioAuthToken = "";
var exec = require('child_process').exec;
var client = null;// = new twilio.RestClient(twilioSID, twilioAuthToken);
var ipSent = false;
var getNetworkIPs = (function (callback) {
var ignoreRE = /^(127\.0\.0\.1|::1|fe80(:1)?::1(%.*)?)$/i;
var cached;
var command;
var filterRE;
switch (process.platform) {
case 'win32':
//case 'win64': // TODO: test
command = 'ipconfig';
filterRE = /\bIPv[46][^:\r\n]+:\s*([^\s]+)/g;
break;
case 'darwin':
command = 'ifconfig';
filterRE = /\binet\s+([^\s]+)/g;
// filterRE = /\binet6\s+([^\s]+)/g; // IPv6
break;
default:
command = 'ifconfig';
filterRE = /\binet\b[^:]+:\s*([^\s]+)/g;
// filterRE = /\binet6[^:]+:\s*([^\s]+)/g; // IPv6
break;
}
console.log('running: ' + command);
exec(command, function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
} else {
cached = [];
var ip;
var matches = stdout.match(filterRE) || [];
for (var i = 0; i < matches.length; i++) {
ip = matches[i].replace(filterRE, '$1')
if (!ignoreRE.test(ip)) {
cached.push(ip);
}
}
}
callback(error, cached[0]);
});
});
exports.InitializeTwilio = function (to, from, sid, token, deviceName, port) {
if (to != null && from != null && sid != null && token != null && to != '' && from != '' && sid != '' && token != '') {
console.log("initializing twilio: " + to + ", " + from + ", " + sid + ", " + token + ", " + deviceName);
toText = to.toString();
fromText = from.toString();
twilioSID = sid.toString();
twilioAuthToken = token.toString();
if (deviceName != null)
hostName = deviceName;
client = new twilio(twilioSID, twilioAuthToken);
if (!ipSent) {
ipSent = true;
console.log('sending ip address');
getNetworkIPs(function (error, ip) {
if (error) {
console.log('ip error:', error);
exports.sendText("ip error: " + error);
} else {
console.log("ip: " + ip);
exports.sendText("ip: " + ip + ":" + port);
}
}, false);
}
//exports.sendText("Twilio initialized");
}
}
exports.sendText = function (msg) {
if (client == null) {
console.log("Twilio client not initialized");
} else {
client.messages.create({
to: toText,
from: fromText,
body: hostName + " " + msg
}, function (error, message) {
// The HTTP request to Twilio will run asynchronously. This callback
// function will be called when a response is received from Twilio
// The "error" variable will contain error information, if any.
// If the request was successful, this value will be "falsy"
if (!error) {
// The second argument to the callback will contain the information
// sent back by Twilio for the request. In this case, it is the
// information about the text messsage you just sent:
console.log('Success! The SID for this SMS message is:');
console.log(message.sid);
console.log('Message sent on:');
console.log(message.dateCreated);
} else {
console.log('Oops! There was an error: ' + JSON.stringify(error));
}
});
}
}