2012-04-05 05:21:56 +00:00
|
|
|
var express = require('express')
|
|
|
|
, app = express.createServer()
|
2012-04-07 09:00:44 +00:00
|
|
|
, _ = require('underscore')
|
2012-04-07 19:26:21 +00:00
|
|
|
, fs = require('fs')
|
2012-04-07 20:13:02 +00:00
|
|
|
, mixpanel = require('mixpanel')
|
2012-04-07 02:01:24 +00:00
|
|
|
, exec = require('child_process').exec
|
2012-04-07 02:30:43 +00:00
|
|
|
, spawn = require('child_process').spawn
|
|
|
|
, Stream = require('stream')
|
2012-04-06 22:33:12 +00:00
|
|
|
, config = require('./config.js')
|
2012-04-07 09:00:44 +00:00
|
|
|
, providers = require('./providers.js').list
|
2012-04-06 21:52:10 +00:00
|
|
|
|
2012-04-07 20:13:02 +00:00
|
|
|
var mpq = new mixpanel.Client('6e6e6b71ed5ada4504c52d915388d73d');
|
2012-04-07 02:01:24 +00:00
|
|
|
|
2012-04-06 21:52:10 +00:00
|
|
|
var redis;
|
|
|
|
if (process.env.NODE_ENV == 'production')
|
|
|
|
redis = require('redis-url').connect(process.env.REDISTOGO_URL);
|
|
|
|
else
|
|
|
|
redis = require('redis-url').connect();
|
2012-04-05 05:21:56 +00:00
|
|
|
|
|
|
|
// Express config
|
|
|
|
app.set('views', __dirname + '/views');
|
|
|
|
app.set('view engine', 'jade');
|
|
|
|
|
|
|
|
app.use(express.cookieParser());
|
|
|
|
app.use(express.static(__dirname + '/public'));
|
|
|
|
app.use(express.bodyParser());
|
|
|
|
|
|
|
|
// App
|
|
|
|
|
|
|
|
/* Homepage */
|
|
|
|
app.get('/', function(req, res) {
|
2012-04-07 19:26:21 +00:00
|
|
|
fs.readFile(__dirname + '/views/index.html', 'utf8', function(err, text){
|
|
|
|
res.send(text);
|
|
|
|
});
|
2012-04-05 05:21:56 +00:00
|
|
|
});
|
|
|
|
|
2012-04-06 19:44:51 +00:00
|
|
|
app.post('/text', function(req, res) {
|
2012-04-07 22:50:08 +00:00
|
|
|
if (!req.body.number || !req.body.message) {
|
|
|
|
mpq.track('incomplete request');
|
2012-04-09 18:48:17 +00:00
|
|
|
res.send({success:false,message:'Number and message parameters are required.'});
|
2012-04-07 22:50:08 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
2012-04-07 21:40:05 +00:00
|
|
|
mpq.track('text',
|
|
|
|
{number: req.body.number, message: req.body.message, ip: req.connection.remoteAddress});
|
2012-04-06 21:22:23 +00:00
|
|
|
|
2012-04-07 09:00:44 +00:00
|
|
|
var number = stripPhone(req.body.number);
|
|
|
|
if (number.length < 9 || number.length > 10) {
|
|
|
|
res.send({success:false,message:'Invalid phone number.'});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-07 19:54:52 +00:00
|
|
|
var ipkey = 'textbelt:ip:' + req.connection.remoteAddress + '_' + dateStr();
|
|
|
|
var phonekey = 'textbelt:phone:' + number;
|
|
|
|
|
|
|
|
redis.incr(phonekey, function(err, num) {
|
2012-04-06 21:22:23 +00:00
|
|
|
if (err) {
|
2012-04-07 20:13:02 +00:00
|
|
|
mpq.track('redis fail');
|
2012-04-07 09:00:44 +00:00
|
|
|
res.send({success:false,message:'Could not validate phone# quota.'});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(function() {
|
2012-04-07 19:54:52 +00:00
|
|
|
redis.decr(phonekey, function(err, num) {
|
2012-04-07 09:00:44 +00:00
|
|
|
if (err) {
|
2012-04-07 20:13:02 +00:00
|
|
|
mpq.track('failed to decr phone quota', {number: number});
|
2012-04-07 09:00:44 +00:00
|
|
|
console.log('*** WARNING failed to decr ' + number);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 1000*60*3);
|
|
|
|
if (num > 3) {
|
2012-04-07 20:13:02 +00:00
|
|
|
mpq.track('exceeded phone quota');
|
2012-04-07 09:00:44 +00:00
|
|
|
res.send({success:false,message:'Exceeded quota for this phone number.'});
|
2012-04-06 21:22:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-04-06 21:52:10 +00:00
|
|
|
|
2012-04-07 09:00:44 +00:00
|
|
|
// now check against ip quota
|
2012-04-07 19:54:52 +00:00
|
|
|
redis.incr(ipkey, function(err, num) {
|
2012-04-07 09:00:44 +00:00
|
|
|
if (err) {
|
2012-04-07 20:13:02 +00:00
|
|
|
mpq.track('redis fail');
|
2012-04-07 09:00:44 +00:00
|
|
|
res.send({success:false,message:'Could not validate IP quota.'});
|
|
|
|
return;
|
|
|
|
}
|
2012-04-07 22:50:08 +00:00
|
|
|
/*
|
2012-04-07 20:14:32 +00:00
|
|
|
if (num > 75) {
|
2012-04-07 20:13:02 +00:00
|
|
|
mpq.track('exceeded ip quota');
|
2012-04-07 09:00:44 +00:00
|
|
|
res.send({success:false,message:'Exceeded quota for this IP address.'});
|
|
|
|
return;
|
|
|
|
}
|
2012-04-07 22:50:08 +00:00
|
|
|
*/
|
2012-04-07 09:00:44 +00:00
|
|
|
|
2012-04-06 22:42:58 +00:00
|
|
|
sendText(req.body.number, req.body.message, function(err) {
|
2012-04-07 20:13:02 +00:00
|
|
|
if (err) {
|
|
|
|
mpq.track('sendText failed');
|
2012-04-06 22:42:58 +00:00
|
|
|
res.send({success:false,message:'Communication with SMS gateway failed.'});
|
2012-04-07 20:13:02 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
mpq.track('sendText success');
|
2012-04-06 21:22:23 +00:00
|
|
|
res.send({success:true});
|
2012-04-07 20:13:02 +00:00
|
|
|
}
|
2012-04-06 21:22:23 +00:00
|
|
|
});
|
2012-04-07 09:00:44 +00:00
|
|
|
});
|
|
|
|
|
2012-04-06 21:22:23 +00:00
|
|
|
});
|
2012-04-07 09:00:44 +00:00
|
|
|
|
2012-04-06 19:44:51 +00:00
|
|
|
});
|
|
|
|
|
2012-04-06 21:22:23 +00:00
|
|
|
function dateStr() {
|
|
|
|
var today = new Date();
|
|
|
|
var dd = today.getDate();
|
|
|
|
var mm = today.getMonth()+1;
|
|
|
|
var yyyy = today.getFullYear();
|
|
|
|
return mm + '/' + dd + '/' + yyyy;
|
|
|
|
}
|
|
|
|
|
2012-04-07 09:00:44 +00:00
|
|
|
function stripPhone(phone) {
|
2012-04-07 22:50:08 +00:00
|
|
|
return (phone+'').replace(/\D/g, '');
|
2012-04-07 09:00:44 +00:00
|
|
|
}
|
2012-04-07 02:01:24 +00:00
|
|
|
|
2012-04-06 22:42:58 +00:00
|
|
|
function sendText(phone, message, cb) {
|
2012-04-07 09:00:44 +00:00
|
|
|
console.log('txting phone', phone);
|
2012-04-07 02:30:43 +00:00
|
|
|
console.log('msg', message);
|
2012-04-07 02:01:24 +00:00
|
|
|
|
2012-04-07 09:00:44 +00:00
|
|
|
var done = _.after(providers.length, function() {
|
|
|
|
cb(false);
|
2012-04-07 02:30:43 +00:00
|
|
|
});
|
2012-04-07 09:00:44 +00:00
|
|
|
|
2012-04-07 19:54:52 +00:00
|
|
|
_.each(providers, function(provider) {
|
2012-04-07 09:00:44 +00:00
|
|
|
var email = provider.replace('%s', phone);
|
|
|
|
var child = spawn('sendmail', ['-f', 'txt@textbelt.com', email]);
|
|
|
|
child.stdout.on('data', console.log);
|
|
|
|
child.stderr.on('data', console.log);
|
2012-04-07 20:13:02 +00:00
|
|
|
child.on('error', function(data) {
|
|
|
|
mpq.track('sendmail failed', {email: email, data: data});
|
2012-04-07 19:54:52 +00:00
|
|
|
done();
|
|
|
|
});
|
2012-04-07 09:00:44 +00:00
|
|
|
child.on('exit', function(code, signal) {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
child.stdin.write(message + '\n.');
|
|
|
|
child.stdin.end();
|
2012-04-07 19:54:52 +00:00
|
|
|
});
|
2012-04-06 19:44:51 +00:00
|
|
|
}
|
2012-04-05 05:21:56 +00:00
|
|
|
|
2012-04-07 19:26:21 +00:00
|
|
|
var port = process.env.PORT || 9090;
|
2012-04-05 05:21:56 +00:00
|
|
|
app.listen(port, function() {
|
|
|
|
console.log('Listening on', port);
|
|
|
|
});
|