2014-10-06 05:37:34 +00:00
|
|
|
var providers = require('./providers.js')
|
|
|
|
, _ = require('underscore')
|
|
|
|
, exec = require('child_process').exec
|
|
|
|
, spawn = require('child_process').spawn;
|
2015-11-06 08:24:25 +00:00
|
|
|
var StringDecoder = require('string_decoder').StringDecoder;
|
2014-10-06 05:37:34 +00:00
|
|
|
|
|
|
|
var debugEnabled = false;
|
2015-11-06 08:24:25 +00:00
|
|
|
|
|
|
|
// NOTE: Change this if you are self-hosting!
|
2015-03-11 06:51:52 +00:00
|
|
|
var fromAddress = 'foo@bar.com';
|
2014-10-06 05:37:34 +00:00
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
/*
|
|
|
|
General purpose logging function, gated by a configurable
|
|
|
|
value.
|
|
|
|
*/
|
|
|
|
function output() {
|
2014-10-23 06:51:58 +00:00
|
|
|
if (debugEnabled) {
|
2014-10-06 05:37:34 +00:00
|
|
|
return console.log.apply(this, arguments);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
/* Enable verbosity for the text module.
|
|
|
|
|
|
|
|
If enabled, logging functions will
|
|
|
|
print to stdout.
|
|
|
|
|
|
|
|
Params:
|
|
|
|
enable - bool
|
|
|
|
*/
|
|
|
|
function debug(enable) {
|
|
|
|
debugEnabled = enable;
|
|
|
|
return debugEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
/* Sends a text message
|
|
|
|
|
|
|
|
Will perform a region lookup (for providers), then
|
|
|
|
send a message to each.
|
|
|
|
|
|
|
|
Params:
|
|
|
|
phone - phone number to text
|
|
|
|
message - message to send
|
|
|
|
region - region to use (defaults to US)
|
|
|
|
cb - function(err), provides err messages
|
|
|
|
*/
|
|
|
|
function sendText(phone, message, region, cb) {
|
|
|
|
output('txting phone', phone, ':', message);
|
|
|
|
|
|
|
|
region = region || 'us';
|
|
|
|
|
|
|
|
var providers_list = providers[region];
|
|
|
|
|
|
|
|
var done = _.after(providers_list.length, function() {
|
|
|
|
cb(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
_.each(providers_list, function(provider) {
|
|
|
|
var email = provider.replace('%s', phone);
|
2015-11-06 08:24:25 +00:00
|
|
|
var child = spawn('mail', ['-s', 'txt', '-a', 'From:' + fromAddress, email]);
|
|
|
|
var decoder = new StringDecoder('utf8');
|
|
|
|
child.stdout.on('data', function(data) {
|
|
|
|
output(decoder.write(data));
|
|
|
|
});
|
|
|
|
child.stderr.on('data', function(data) {
|
|
|
|
output(decoder.write(data));
|
|
|
|
});
|
2014-10-06 05:37:34 +00:00
|
|
|
child.on('error', function(data) {
|
2015-11-06 08:24:25 +00:00
|
|
|
output('sendmail failed', {email: email, data: decoder.write(data)});
|
2014-10-06 05:37:34 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
child.on('exit', function(code, signal) {
|
|
|
|
done();
|
|
|
|
});
|
2015-11-06 08:24:25 +00:00
|
|
|
child.stdin.write(message + '\n');
|
2014-10-06 05:37:34 +00:00
|
|
|
child.stdin.end();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
send: sendText, // Send a text message
|
|
|
|
debug: debug // Enable or disable debug output
|
|
|
|
};
|