it's all working

This commit is contained in:
typpo 2012-04-07 05:00:44 -04:00
parent 29792f6bfd
commit f3fc683a6b
3 changed files with 83 additions and 20 deletions

65
app.js
View File

@ -1,6 +1,7 @@
var express = require('express') var express = require('express')
, app = express.createServer() , app = express.createServer()
, nodemailer = require('nodemailer') , nodemailer = require('nodemailer')
, _ = require('underscore')
/* /*
, amazonses = require('amazon-ses') , amazonses = require('amazon-ses')
*/ */
@ -8,6 +9,7 @@ var express = require('express')
, spawn = require('child_process').spawn , spawn = require('child_process').spawn
, Stream = require('stream') , Stream = require('stream')
, config = require('./config.js') , config = require('./config.js')
, providers = require('./providers.js').list
/* /*
var SendGrid = require('sendgrid').SendGrid; var SendGrid = require('sendgrid').SendGrid;
@ -41,24 +43,51 @@ app.get('/', function(req, res) {
app.post('/text', function(req, res) { app.post('/text', function(req, res) {
var keystr = req.connection.remoteAddress + '_' + dateStr(); var keystr = req.connection.remoteAddress + '_' + dateStr();
var number = stripPhone(req.body.number);
if (number.length < 9 || number.length > 10) {
res.send({success:false,message:'Invalid phone number.'});
return;
}
redis.incr('phone:' + number, function(err, num) {
if (err) {
res.send({success:false,message:'Could not validate phone# quota.'});
return;
}
setTimeout(function() {
redis.decr('phone:' + number, function(err, num) {
if (err) {
console.log('*** WARNING failed to decr ' + number);
}
});
}, 1000*60*3);
if (num > 3) {
res.send({success:false,message:'Exceeded quota for this phone number.'});
return;
}
// now check against ip quota
redis.incr(keystr, function(err, num) { redis.incr(keystr, function(err, num) {
if (err) { if (err) {
res.send({success:false,message:'Could not validate IP quota.'}); res.send({success:false,message:'Could not validate IP quota.'});
return; return;
} }
if (num > 500) {
res.send({success:false,message:'Exceeded quota for this IP address.'});
return;
}
if (num < 501) {
sendText(req.body.number, req.body.message, function(err) { sendText(req.body.number, req.body.message, function(err) {
if (err) if (err)
res.send({success:false,message:'Communication with SMS gateway failed.'}); res.send({success:false,message:'Communication with SMS gateway failed.'});
else else
res.send({success:true}); res.send({success:true});
}); });
}
else {
res.send({success:false,message:'Exceeded quota.'});
}
}); });
});
}); });
function dateStr() { function dateStr() {
@ -69,27 +98,37 @@ function dateStr() {
return mm + '/' + dd + '/' + yyyy; return mm + '/' + dd + '/' + yyyy;
} }
function validatePhone() { function stripPhone(phone) {
return phone.replace(/\D/g, '');
}
function validatePhone(phone) {
} }
function sendText(phone, message, cb) { function sendText(phone, message, cb) {
console.log('txting phone', phone);
var actual_phone = phone.replace(/\D/g, '');
console.log('txting phone', actual_phone);
console.log('msg', message); console.log('msg', message);
//var child = process.createChildProcess('sendmail', ['-f"txt@textbelt.com"', '"9147727429@vtext.com"']);
var child = spawn('sendmail', ['-f', 'txt@textbelt.com', '9147727429@vtext.com']); var done = _.after(providers.length, function() {
cb(false);
});
for (var i=0; i < providers.length; i++) {
var provider = providers[i];
var email = provider.replace('%s', phone);
var child = spawn('sendmail', ['-f', 'txt@textbelt.com', email]);
child.stdout.on('data', console.log); child.stdout.on('data', console.log);
child.stderr.on('data', console.log); child.stderr.on('data', console.log);
child.on('error', console.log); child.on('error', console.log);
child.on('exit', function(code, signal) { child.on('exit', function(code, signal) {
cb(code !== 0); done();
}); });
child.stdin.write('testiiii', 'binary'); child.stdin.write(message + '\n.');
child.stdin.end(); child.stdin.end();
}
/* /*

View File

@ -7,6 +7,7 @@
, "redis-url": "latest" , "redis-url": "latest"
, "amazon-ses": "latest" , "amazon-ses": "latest"
, "sendgrid": "latest" , "sendgrid": "latest"
, "underscore": "latest"
} }
, "devDependencies": { , "devDependencies": {
} }

23
providers.js Normal file
View File

@ -0,0 +1,23 @@
module.exports = {
list: [
'%s@message.alltel.com',
'%s@paging.acswireless.com',
'%s@txt.att.net',
'%s@myboostmobile.com',
'%s@mobile.celloneusa.com',
'%s@sms.edgewireless.com',
'%s@messaging.sprintpcs.com',
'%s@tmomail.net',
'%s@mymetropcs.com',
'%s@messaging.nextel.com',
'%s@mobile.celloneusa.com',
'%s@qwestmp.com',
'%s@pcs.rogers.com',
'%s@msg.telus.com',
//'%s@email.uscc.net',
'%s@vtext.com',
'%s@vmobl.com',
],
};