Advertisement
Guest User

Untitled

a guest
Jan 25th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. const _ = require('lodash');
  4. const EventEmitter = require('events').EventEmitter;
  5. const inherits = require('util').inherits;
  6. const request = require('co-request');
  7. const socketio = require('socket.io-client');
  8. const debug = require('debug')('instant:client');
  9. const debugtick = require('debug')('verbose:tick');
  10. const debugchat = require('debug')('instant:chat');
  11. const https = require('https');
  12.  
  13. https.globalAgent.options.rejectUnauthorized = false;
  14.  
  15. module.exports = JdClient;
  16.  
  17. function JdClient(username, password) {
  18.     EventEmitter.call(this);
  19.     let self = this;
  20.     // Save configuration and stuff.
  21.  
  22.     async () => {
  23.         self.username = null;
  24.         self.name = null;
  25.         self.balance = null;
  26.  
  27.         self.csrf = null;
  28.         self.inits = null;
  29.         self.uid = null;
  30.         self.invested = null;
  31.         self.balance = null;
  32.         self.max_profit = null;
  33.         self.cookie = null;
  34.         self.version = '0.1.5'
  35.         self.url = 'https://just-dice.com'
  36.  
  37.         self.credentials = {
  38.             hash: '',
  39.             username: username,
  40.             password: password,
  41.             code: ''
  42.         }
  43.  
  44.         self.cookie = yield login(self.credentials, self.url)
  45.         console.log('JD - Received cookie: ' + self.cookie);
  46.         console.log('JD - Setting up connection to %s', self.url);
  47.        
  48.         self.socket = socketio(self.url, {
  49.             multiplex: false,
  50.             agent: https.globalAgent,
  51.             extraHeaders: {
  52.                 origin: self.url,
  53.                 cookie: self.cookie
  54.             }
  55.         });
  56.  
  57.         self.socket.on('error', self.onError.bind(self));
  58.         self.socket.on('login_error', self.onError.bind(self));
  59.         self.socket.on('invest_error', self.onError.bind(self));
  60.         self.socket.on('divest_error', self.onError.bind(self));
  61.         self.socket.on('getver', self.onGetVer.bind(self));
  62.         self.socket.on('tip', self.onTip.bind(self));
  63.         self.socket.on('init', self.onInit.bind(self));
  64.         self.socket.on('set_hash', self.onSetHash.bind(self));
  65.         self.socket.on('balance', self.onBalance.bind(self));
  66.         self.socket.on('chat', self.onMsg.bind(self));
  67.  
  68.     });
  69. }
  70.  
  71. inherits(JdClient, EventEmitter);
  72.  
  73. JdClient.prototype.onBalance = function(data) {
  74.     console.log('JD - onBalance: ', data);
  75.     this.balance = data;
  76.     this.emit('balance', this.balance)
  77. };
  78.  
  79. JdClient.prototype.onGetVer = function(key) {
  80.     socket.emit('version', this.csrf, key, "jdbot:" + this.version);
  81.     console.log('JD - onGetVer: ', key);
  82. };
  83.  
  84. JdClient.prototype.onSetHash = function(hash) {
  85.     console.log('JD - onSetHash: ', hash);
  86. };
  87.  
  88. JdClient.prototype.onTip = function(from, name, amount, comment, ignored) {
  89.     console.log('JD - onTip: ', from, name, amount, comment, ignored);
  90. };
  91.  
  92. JdClient.prototype.onInit = function(data) {
  93.     this.invested = data.investment;
  94.     this.uid = data.uid;
  95.     this.name = data.name;
  96.     this.username = data.username;
  97.  
  98.     if (!this.inits++) {
  99.         this.csrf = data.csrf;
  100.         this.balance = data.balance;
  101.         this.emit('balance', this.balance)
  102.  
  103.         console.log('JD - connected as (' + this.uid + ') <' + this.name + '>');
  104.  
  105.     } else {
  106.         console.log('JD - reconnected');
  107.         this.csrf = data.csrf;
  108.     }
  109. };
  110.  
  111.  
  112. JdClient.prototype.onError = function(err) {
  113.     console.log('JD - onError: ', err);
  114. };
  115.  
  116. JdClient.prototype.onErr = function(err) {
  117.     console.log('JD - onErr: ', err);
  118. };
  119.  
  120.  
  121. JdClient.prototype.onDisconnect = function(data) {
  122.     console.log('JD - Client disconnected |', data, '|', typeof data);
  123.     this.emit('disconnect');
  124. };
  125.  
  126. JdClient.prototype.onMsg = function(message) {
  127.     let msg = {}
  128.     msg.site = 'JD'
  129.  
  130.     //setup message format for Instant and determine if message if private or public to
  131.     // determine how to respond to the user
  132.     if (message.match(/\[ \((.*?)\) <(.*?)> → \(1626506\) <rowan> \] (.*)/)) {
  133.         let isPrivateRegMatch = message.match(/\[ \((.*?)\) <(.*?)> → \(1626506\) <rowan> \] (.*)/)
  134.  
  135.         msg.type = 'private'
  136.         msg.uid = Number(isPrivateRegMatch[1])
  137.         msg.name = isPrivateRegMatch[2]
  138.         msg.message = isPrivateRegMatch[3].trim()
  139.     } else {
  140.         if (!message.match(/\((.*?)\) <(.*?)> (.*)/))
  141.             return
  142.  
  143.         let isPublicRegMatch = message.match(/\((.*?)\) <(.*?)> (.*)/)
  144.         msg.type = 'public'
  145.         msg.uid = Number(isPublicRegMatch[1])
  146.         msg.name = isPublicRegMatch[2]
  147.         msg.message = isPublicRegMatch[3].trim()
  148.     }
  149.  
  150.     this.emit('msg', msg);
  151. };
  152.  
  153. JdClient.prototype.say = function(uid, msg) {
  154.   if(msg.type === "private")
  155.     this.sayPrivate(msg.uid, msg)
  156.   else
  157.     this.sayPublic(msg)
  158. };
  159.  
  160. JdClient.prototype.sayPrivate = function(uid, msg) {
  161.     this.socket.emit('chat', this.csrf, '/msg ' + uid + ' ' + msg.message);
  162. };
  163.  
  164. JdClient.prototype.sayPublic = function(msg) {
  165.     this.socket.emit("chat", this.csrf, msg.message)
  166. };
  167.  
  168.  
  169. async function login(credentials, url) {
  170.     let jar = request.jar();
  171.  
  172.     let req = {
  173.         url: url,
  174.         jar: jar,
  175.         form: {}
  176.     }
  177.  
  178.     if (credentials.username) req.form.username = credentials.username;
  179.     if (credentials.password) req.form.password = credentials.password;
  180.     if (credentials.code) req.form.code = credentials.code;
  181.  
  182.     let res = yield request.post(req);
  183.     let cookie = jar.getCookieString(url);
  184.  
  185.     return cookie
  186.  
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement