Advertisement
Guest User

Untitled

a guest
Aug 10th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var util = require('util'),
  2.     Socket = require('net').Socket,
  3.     EventEmitter = require('events').EventEmitter,
  4.     MSNPassport = require(__dirname + '/msn.passport.js');
  5.  
  6. /**
  7.  * Socket logic
  8.  */
  9. var MSNSocket = function(host, port) {
  10.   EventEmitter.call(this);
  11.   var that = this;
  12.   this._trid = 0;
  13.   this.cmdBuffer = [];
  14.  
  15.   var sock = this.socket = new Socket();
  16.   sock.connect(port || 1863, host || 'messenger.hotmail.com');
  17.   sock.on('connect', function() {
  18.     that.emit('log', 'Connected');
  19.     that.emit('connect');
  20.   });
  21.  
  22.   sock.on('data', function(data) {
  23.     var str = data.toString();
  24.     that.emit('log', str);
  25.     that.emit('data', str);
  26.   });
  27.  
  28.   sock.on('close', function() {
  29.     that.emit('log', 'Disconnected!');
  30.     that.emit('close');
  31.   });
  32. };
  33. util.inherits(MSNSocket, EventEmitter);
  34.  
  35. MSNSocket.prototype.trid = function() {
  36.   return ++this._trid;
  37. };
  38.  
  39. MSNSocket.prototype.send = function(data, callback) {
  40.   this.cmdBuffer.push({ "data": data,
  41.     "callback" : callback });
  42.   this.drain();
  43. };
  44.  
  45. MSNSocket.prototype.drain = function() {
  46.   var that = this;
  47.   var cmd = this.cmdBuffer[0];
  48.   this.emit('log', 'Sending ' + cmd.data);
  49.   this.socket.write(cmd.data + '\r\n', function() {
  50.     cmd.callback && cmd.callback();
  51.     that.cmdBuffer.shift();
  52.     if(that.cmdBuffer.length !== 0) {
  53.       that.drain();
  54.     }
  55.   });
  56. };
  57.  
  58.  
  59.  
  60. /**
  61.  * Main logic
  62.  */
  63. var MSNProtocol = function(username, password) {
  64.   this.user = username;
  65.   this.pass = password;
  66.   this.verbose = true;
  67.  
  68.   EventEmitter.call(this);
  69.   var that = this;
  70.  
  71.   this.connect();
  72.  
  73.   this.on('login', function(status) {
  74.     if(status === 'success') {
  75.       that.sock.send('CHG 8 BSY 0');
  76.     }
  77.   });
  78. };
  79. util.inherits(MSNProtocol, EventEmitter);
  80.  
  81. MSNProtocol.prototype.connect = function(host, port) {
  82.   var that = this;
  83.   var tmpTrid = (this.sock)? this.sock._trid: 0;
  84.   var sock = this.sock = new MSNSocket(host, port);
  85.   sock._trid = tmpTrid;
  86.   sock.on('connect', function() {
  87.     sock.send('VER ' + sock.trid() + ' MSNP8');
  88.     sock.send('CVR ' + sock.trid() + ' 0x0409 linux 6.0 i386 NODEMSGR 6.0.0602 MSMSGS ' + that.user);
  89.     sock.send('USR ' + sock.trid() + ' TWN I ' + that.user);
  90.   });
  91.  
  92.   sock.on('data', function(data) {
  93.     if(data.indexOf('XFR') !== -1) {
  94.       that.doXFR(data);
  95.       return sock;
  96.     }
  97.    
  98.     if(data.indexOf('TWN S') !== -1) {
  99.       that.doLogin(data);
  100.     }
  101.    
  102.     if(data.indexOf('USR ' + that.sock._trid + ' OK') !== -1) {
  103.       that.emit('login', 'success');
  104.       sock.removeAllListeners('data');
  105.     }
  106.    
  107.   });
  108.  
  109.   sock.on('log', function(data) {
  110.     if(that.verbose) {
  111.       util.log(data);
  112.     }
  113.   });
  114.  
  115.   return sock;
  116. };
  117.  
  118. MSNProtocol.prototype.doXFR = function(res) {
  119.   var start = res.indexOf('NS ') + 3;
  120.   var str = res.substr(start);
  121.   var stop = str.indexOf(' 0 ');
  122.   str = str.substr(0, stop).split(':');
  123.   this.connect(str[0], str[1]);
  124. };
  125.  
  126. MSNProtocol.prototype.doLogin = function(res) {
  127.   var that = this;
  128.   var challenge = res.substr(res.indexOf('TWN S') + 6);
  129.   MSNPassport(challenge, this.user, this.pass, function(status, ticket) {
  130.     if(status === 'success') {
  131.       that.sock.send('USR ' + that.sock.trid() + ' TWN S ' + ticket);
  132.       delete that.pass;
  133.     } else {
  134.       that.emit('login', 'failed');
  135.     }
  136.   });
  137. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement