Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var util = require('util'),
- Socket = require('net').Socket,
- EventEmitter = require('events').EventEmitter,
- MSNPassport = require(__dirname + '/msn.passport.js');
- /**
- * Socket logic
- */
- var MSNSocket = function(host, port) {
- EventEmitter.call(this);
- var that = this;
- this._trid = 0;
- this.cmdBuffer = [];
- var sock = this.socket = new Socket();
- sock.connect(port || 1863, host || 'messenger.hotmail.com');
- sock.on('connect', function() {
- that.emit('log', 'Connected');
- that.emit('connect');
- });
- sock.on('data', function(data) {
- var str = data.toString();
- that.emit('log', str);
- that.emit('data', str);
- });
- sock.on('close', function() {
- that.emit('log', 'Disconnected!');
- that.emit('close');
- });
- };
- util.inherits(MSNSocket, EventEmitter);
- MSNSocket.prototype.trid = function() {
- return ++this._trid;
- };
- MSNSocket.prototype.send = function(data, callback) {
- this.cmdBuffer.push({ "data": data,
- "callback" : callback });
- this.drain();
- };
- MSNSocket.prototype.drain = function() {
- var that = this;
- var cmd = this.cmdBuffer[0];
- this.emit('log', 'Sending ' + cmd.data);
- this.socket.write(cmd.data + '\r\n', function() {
- cmd.callback && cmd.callback();
- that.cmdBuffer.shift();
- if(that.cmdBuffer.length !== 0) {
- that.drain();
- }
- });
- };
- /**
- * Main logic
- */
- var MSNProtocol = function(username, password) {
- this.user = username;
- this.pass = password;
- this.verbose = true;
- EventEmitter.call(this);
- var that = this;
- this.connect();
- this.on('login', function(status) {
- if(status === 'success') {
- that.sock.send('CHG 8 BSY 0');
- }
- });
- };
- util.inherits(MSNProtocol, EventEmitter);
- MSNProtocol.prototype.connect = function(host, port) {
- var that = this;
- var tmpTrid = (this.sock)? this.sock._trid: 0;
- var sock = this.sock = new MSNSocket(host, port);
- sock._trid = tmpTrid;
- sock.on('connect', function() {
- sock.send('VER ' + sock.trid() + ' MSNP8');
- sock.send('CVR ' + sock.trid() + ' 0x0409 linux 6.0 i386 NODEMSGR 6.0.0602 MSMSGS ' + that.user);
- sock.send('USR ' + sock.trid() + ' TWN I ' + that.user);
- });
- sock.on('data', function(data) {
- if(data.indexOf('XFR') !== -1) {
- that.doXFR(data);
- return sock;
- }
- if(data.indexOf('TWN S') !== -1) {
- that.doLogin(data);
- }
- if(data.indexOf('USR ' + that.sock._trid + ' OK') !== -1) {
- that.emit('login', 'success');
- sock.removeAllListeners('data');
- }
- });
- sock.on('log', function(data) {
- if(that.verbose) {
- util.log(data);
- }
- });
- return sock;
- };
- MSNProtocol.prototype.doXFR = function(res) {
- var start = res.indexOf('NS ') + 3;
- var str = res.substr(start);
- var stop = str.indexOf(' 0 ');
- str = str.substr(0, stop).split(':');
- this.connect(str[0], str[1]);
- };
- MSNProtocol.prototype.doLogin = function(res) {
- var that = this;
- var challenge = res.substr(res.indexOf('TWN S') + 6);
- MSNPassport(challenge, this.user, this.pass, function(status, ticket) {
- if(status === 'success') {
- that.sock.send('USR ' + that.sock.trid() + ' TWN S ' + ticket);
- delete that.pass;
- } else {
- that.emit('login', 'failed');
- }
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement