2016-09-20 14:39:09 +00:00
|
|
|
var providers = require('./providers.js'),
|
|
|
|
carriers = require('./carriers.js'),
|
|
|
|
spawn = require('child_process').spawn,
|
2016-11-27 05:57:45 +00:00
|
|
|
StringDecoder = require('string_decoder').StringDecoder,
|
|
|
|
config = require('./config.js');
|
2014-10-06 05:37:34 +00:00
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
/*
|
|
|
|
General purpose logging function, gated by a configurable
|
|
|
|
value.
|
|
|
|
*/
|
|
|
|
function output() {
|
2016-11-27 05:57:45 +00:00
|
|
|
if (config.debugEnabled) {
|
2014-10-06 05:37:34 +00:00
|
|
|
return console.log.apply(this, arguments);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
/* 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
|
2016-09-20 14:39:09 +00:00
|
|
|
carrier - carrier to use (may be null)
|
2014-10-06 05:37:34 +00:00
|
|
|
region - region to use (defaults to US)
|
|
|
|
cb - function(err), provides err messages
|
|
|
|
*/
|
2015-11-28 18:49:03 +00:00
|
|
|
function sendText(phone, message, carrier, region, cb) {
|
2014-10-06 05:37:34 +00:00
|
|
|
output('txting phone', phone, ':', message);
|
|
|
|
|
|
|
|
region = region || 'us';
|
|
|
|
|
2015-11-28 18:49:03 +00:00
|
|
|
var providers_list;
|
2016-09-20 14:39:09 +00:00
|
|
|
if (carrier !== null) {
|
2015-11-28 18:49:03 +00:00
|
|
|
providers_list = carriers[carrier];
|
2016-09-20 14:39:09 +00:00
|
|
|
} else {
|
|
|
|
providers_list = providers[region];
|
2015-11-28 18:49:03 +00:00
|
|
|
}
|
2014-10-06 05:37:34 +00:00
|
|
|
|
2015-12-03 07:36:39 +00:00
|
|
|
var emails = providers_list.map(function(provider) {
|
|
|
|
return provider.replace('%s', phone);
|
|
|
|
}).join(',');
|
2016-11-27 05:57:45 +00:00
|
|
|
var args = ['-s', 'txt', '-e', 'set from=' + config.fromAddress,
|
2015-12-03 07:36:39 +00:00
|
|
|
'-e', 'set use_from=yes', '-e', 'set envelope_from=yes', '-b', emails];
|
|
|
|
var child = spawn('mutt', args);
|
|
|
|
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));
|
|
|
|
});
|
|
|
|
child.on('error', function(data) {
|
2016-05-26 21:26:38 +00:00
|
|
|
output('mutt failed', {email: emails, data: decoder.write(data)});
|
2014-10-06 05:37:34 +00:00
|
|
|
cb(false);
|
|
|
|
});
|
2015-12-03 07:36:39 +00:00
|
|
|
child.on('exit', function(code, signal) {
|
|
|
|
cb(false);
|
2014-10-06 05:37:34 +00:00
|
|
|
});
|
2016-09-20 14:39:09 +00:00
|
|
|
output(message);
|
2015-12-03 07:36:39 +00:00
|
|
|
child.stdin.write(message);
|
|
|
|
child.stdin.end();
|
2014-10-06 05:37:34 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 05:57:45 +00:00
|
|
|
//----------------------------------------------------------------
|
|
|
|
/* Overrides default config
|
|
|
|
|
|
|
|
Takes a new configuration object, which is
|
|
|
|
used to override the defaults
|
|
|
|
|
|
|
|
Params:
|
|
|
|
obj - object of config properties to be overriden
|
|
|
|
*/
|
|
|
|
function setConfig(obj) {
|
2016-12-03 02:20:40 +00:00
|
|
|
config = Object.assign(config, obj);
|
2016-11-27 05:57:45 +00:00
|
|
|
}
|
|
|
|
|
2014-10-06 05:37:34 +00:00
|
|
|
module.exports = {
|
|
|
|
send: sendText, // Send a text message
|
2016-11-27 05:57:45 +00:00
|
|
|
config: setConfig // Override default config
|
2014-10-06 05:37:34 +00:00
|
|
|
};
|