Created
December 10, 2011 03:52
-
-
Save max-mapper/1454545 to your computer and use it in GitHub Desktop.
foursquare irc bridge thingy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var crypto = require('crypto'), | |
fs = require("fs"), | |
https = require('https'), | |
http = require("http"), | |
sys = require('sys'), | |
request = require('request'), | |
url = require('url'), | |
querystring = require('querystring'), | |
tako = require('./tako'), | |
qs = require('querystring'), | |
irc = require('./IRC/lib/irc'); | |
var bot = new irc({ server: 'irc.freenode.net'}); | |
bot.connect(function () { | |
bot.nick('nerdtracker5000'); | |
bot.join('#nerdtracker'); | |
}); | |
var options = { | |
baseURL: "https://outmapp.in", | |
ca: fs.readFileSync('sub.class1.server.ca.pem'), | |
key: fs.readFileSync('ssl.key'), | |
cert: fs.readFileSync('ssl.crt'), | |
"fsqID": "", | |
"fsqSecret": "" | |
}; | |
var t = tako() | |
t.middle('json') | |
t.route('/', function(req, resp) { | |
resp.end('go to /nerdtracker/login to register') | |
}) | |
t.route('/nerdtracker/push', function(req, resp) { | |
req.on('data', function (data) { | |
var checkin = JSON.parse(qs.parse(data.toString()).checkin) | |
var msg = checkin.user.firstName + ' just checked in at ' + checkin.venue.name | |
if (checkin.venue && checkin.venue.location && checkin.venue.location.address) msg = msg + ' (' + checkin.venue.location.address + ')' | |
if (checkin.shout) msg = msg + ': ' + checkin.shout | |
bot.privmsg('#nerdtracker', msg) | |
}) | |
req.on('end', function() { | |
resp.end('thanks foursquare') | |
}) | |
}) | |
t | |
.route('/nerdtracker/login', function (req, resp) { | |
var u = 'https://foursquare.com/oauth2/authenticate' | |
+ '?client_id=' + options.fsqID | |
+ '&response_type=code' | |
+ '&redirect_uri=' + options.baseURL + '/nerdtracker/callback' | |
; | |
resp.statusCode = 302 | |
resp.setHeader('location', u) | |
resp.end() | |
}) | |
t | |
.route('/nerdtracker/callback', function (req, resp) { | |
var u = 'https://foursquare.com/oauth2/access_token' | |
+ '?client_id=' + options.fsqID | |
+ '&client_secret=' + options.fsqSecret | |
+ '&grant_type=authorization_code' | |
+ '&redirect_uri=' + options.baseURL + '/nerdtracker/callback' | |
+ '&code=' + req.qs.code | |
; | |
request.get({url:u}, function (e, r, body) { | |
resp.statusCode = 200 | |
var dataz = JSON.parse(body) | |
dataz.registration_success = true | |
dataz.all_done = true | |
bot.privmsg('#nerdtracker', 'a new person just registered!') | |
resp.end(JSON.stringify(dataz)) | |
}) | |
}) | |
t.listen(function(handler) { | |
return https.createServer(options, handler) | |
}, 443) |
ya, its @mikeals unreleased streaming web framework (codenamed tako). you pass it a server and a port and it listens for you. the arity of http.createServer and https.createServer is different so this is the most generic abstraction I could think of
o/ @mikeal sent me. :)
That's fine, but why not:
t.createServer(function(handler) {
return https.createServer(options, handler)
})
t.listen(443)
it will eventually just be t.createServer().listen(port, cb)
+1 for not breaking the server convention.
npm ERR! 404 'tako' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it
Please publish this thing. I want to use it for some stuff.
Which IRC lib is this using? irc = require('./IRC/lib/irc');
it's and old version of https://github.com/gf3/IRC-js
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Care to explain that wacky listen method?