Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var SteamUser = require('steam-user');
  2. var fs = require('fs');
  3. var crypto = require('crypto');
  4. var SteamTotp = require('steam-totp');
  5. var TradeOfferManager = require('steam-tradeoffer-manager');
  6. var SteamCommunity = require('steamcommunity');
  7.  
  8. var Steam = require('steam-client');
  9.  
  10.  
  11. function xdBot(options) {
  12.     this.cookies_ = null;
  13.  
  14.     this.account_name_ = options && options.account_name ? options.account_name : "";
  15.     this.password_ = options && options.password ? options.password : "";
  16.  
  17.     this.shared_secret_ = options && options.shared_secret ? options.shared_secret : "";
  18.     this.identity_secret_ = options && options.identity_secret ? options.identity_secret : "";
  19.  
  20.     this.confirmation_checking_interval_ = options && options.confirmation_checking_interval ? options.confirmation_checking_interval : 30000;
  21.  
  22.     this.trade_poll_data_file_ = options && options.trade_poll_data_file ? options.trade_poll_data_file : "poll_data.json";
  23.  
  24.     this.local_bind_address_ = options && options.local_bind_address ? options.local_bind_address : "0.0.0.0";
  25.  
  26.     this.m_client_ = new Steam.CMClient(Steam.EConnectionProtocol.TCP);
  27.     this.m_client_.bind(this.local_bind_address_);
  28.  
  29.     this.steam_client_ = new SteamUser(this.m_client_);
  30.  
  31.     this.save_poll_data_ = true;
  32.  
  33.     this.steam_community_ = new SteamCommunity({
  34.         localAddress: this.local_bind_address_
  35.     });
  36.  
  37.     this.trade_offer_manager_ = new TradeOfferManager({
  38.         "steam": this.steam_client_, // Polling every 30 seconds is fine since we get notifications from Steam
  39.         "community": this.steam_community_,
  40.         //"domain": "example.com", // Our domain is example.com
  41.         "language": "en" // We want English item descriptions
  42.     });
  43.  
  44.     this.callback_hash_ = {};
  45.  
  46.     this.registerHandlers();
  47. }
  48.  
  49. xdBot.prototype = {
  50.     constructor: xdBot,
  51. };
  52.  
  53. xdBot.prototype.setSavePollData = function (value) {
  54.     this.save_poll_data_ = value;
  55. };
  56.  
  57. xdBot.prototype.isConnected = function () {
  58.     return this.m_client_.connected;
  59. };
  60.  
  61. xdBot.prototype.isLoggedOn = function () {
  62.     return this.m_client_.loggedOn;
  63. };
  64.  
  65. xdBot.prototype.registerHandlers = function () {
  66.     var self = this;
  67.  
  68.     this.trade_offer_manager_.on("pollData", function (data) {
  69.         if (self.save_poll_data_) {
  70.             fs.writeFile(self.trade_poll_data_file_, JSON.stringify(data), function () { });
  71.         }
  72.     });
  73.  
  74.     this.steam_community_.on("debug", function (data) {
  75.         self.log("[Steam Community Debug]: " + data);
  76.     });
  77.  
  78.     this.steam_community_.on("sessionExpired", function (err) {
  79.         self.log("steam community session expired");
  80.  
  81.         self.steam_community_.stopConfirmationChecker();
  82.  
  83.         self.log("relogging to the community...");
  84.  
  85.         if (self.isConnected() && self.isLoggedOn()) {
  86.             try {
  87.                 self.steam_client_.webLogOn();
  88.             }
  89.             catch (e) {
  90.                 console.log("caught exception: ", e);
  91.             }
  92.         }
  93.         else {
  94.  
  95.         }
  96.     });
  97.  
  98.     this.steam_community_.on("confKeyNeeded", function (tag, callback) {
  99.         self.log("conf key needed...");
  100.         var time = Math.floor(Date.now() / 1000);
  101.         callback(null, time, SteamTotp.getConfirmationKey(self.identity_secret_, time, tag));
  102.     });
  103.  
  104.     this.steam_community_.on("newConfirmation", this.onNewConfirmation.bind(this));
  105.  
  106.     this.steam_client_.on("disconnected", function (eresult, msg) {
  107.         self.log("steam client disconnected: " + msg);
  108.     });
  109.  
  110.     this.steam_client_.on('loggedOn', function (details) {
  111.         if (details.eresult === 1) {
  112.             self.log("Logged into Steam!");
  113.         }
  114.         else {
  115.             self.log("error login into steam");
  116.         }
  117.     });
  118.  
  119.     this.steam_client_.on("webSession", function (sessionID, cookies) {
  120.         self.log("websession response!");
  121.         self.cookies_ = cookies;
  122.         self.trade_offer_manager_.setCookies(cookies, function (err) {
  123.             if (err) {
  124.                 self.log("error setting cookies");
  125.                 process.exit(1); // Fatal error since we couldn't get our API key
  126.                 return;
  127.             }
  128.             self.steam_community_.startConfirmationChecker(self.confirmation_checking_interval_); // Checks and accepts confirmations every 30 seconds
  129.  
  130.             self.emit("ready", [self.cookies_]);
  131.         });
  132.     });
  133.  
  134.     this.steam_client_.on("error", function (err) {
  135.         self.emit("steamUserError", [err]);
  136.     });
  137. }
  138.  
  139. xdBot.prototype.on = function (event, cb) {
  140.     if (!this.callback_hash_.hasOwnProperty(event)) {
  141.         this.callback_hash_[event] = [];
  142.     }
  143.  
  144.     this.callback_hash_[event].push(cb);
  145. };
  146.  
  147. xdBot.prototype.emit = function (event, args) {
  148.     var cb_arr = this.callback_hash_.hasOwnProperty(event) ? this.callback_hash_[event] : [];
  149.  
  150.     for (var i = 0; i < cb_arr.length; ++i) {
  151.         typeof cb_arr[i] === "function" && cb_arr[i].apply(this, args);
  152.     }
  153. };
  154.  
  155. xdBot.prototype.loadTradePollData = function (cb) {
  156.     var self = this;
  157.     fs.readFile(this.trade_poll_data_file_, function read(err, data) {
  158.         if (!err) {
  159.             try {
  160.                 self.trade_offer_manager_.pollData = JSON.parse(data);
  161.             }
  162.             catch (e) {
  163.                 err = e;
  164.             }
  165.         }
  166.  
  167.         return typeof cb === "function" && cb(err);
  168.     });
  169. };
  170.  
  171. xdBot.prototype.start = function () {
  172.     var self = this;
  173.  
  174.     this.loadTradePollData(function (err) {
  175.         self.log(err ? "could not load poll data from file" : "poll data loaded from file");
  176.         self.steam_client_.logOn({
  177.             "accountName": self.account_name_,
  178.             "password": self.password_,
  179.             "twoFactorCode": SteamTotp.getAuthCode(self.shared_secret_)
  180.         });
  181.     });
  182. };
  183.  
  184. // xdBot.prototype.start = function (require_polldata = false, cb = null) {
  185. //     var self = this;
  186.  
  187. //     this.loadTradePollData(function (err) {
  188. //         self.log(err ? "could not load poll data from file" : "poll data loaded from file");
  189.  
  190. //         if (!require_polldata || !err) {
  191. //             self.steam_client_.logOn({
  192. //                 "accountName": self.account_name_,
  193. //                 "password": self.password_,
  194. //                 "twoFactorCode": SteamTotp.getAuthCode(self.shared_secret_)
  195. //             });
  196.  
  197. //             return typeof cb === "function" && cb(null);
  198. //         }
  199. //         else {
  200. //             self.emit("startupFailed", [err]);
  201. //             return typeof cb === "function" && cb(err);
  202. //         }
  203. //     });
  204. // };
  205.  
  206.  
  207.  
  208. xdBot.prototype.onLoginSuccess = function () {
  209.     //this.log("WE LOGGED IN!");
  210. };
  211.  
  212. xdBot.prototype.log = function (obj) {
  213.     this.emit("debug", [obj]);
  214. };
  215.  
  216. xdBot.prototype.getTradeOfferManager = function () {
  217.     return this.trade_offer_manager_;
  218. };
  219.  
  220. xdBot.prototype.getSteamCommunity = function () {
  221.     return this.steam_community_;
  222. };
  223.  
  224. xdBot.prototype.checkConfirmations = function () {
  225.     this.steam_community_.checkConfirmations();
  226. };
  227.  
  228. xdBot.prototype.onNewConfirmation = function (confirmation) {
  229.     /*
  230.     var self = this;
  231.  
  232.     var time = SteamTotp.time();
  233.     var confKey = SteamTotp.getConfirmationKey(this.identity_secret_, time, "allow");
  234.     confirmation.respond(time, confKey, true, function (err) {
  235.         if (err) {
  236.             self.log("Cannot accept confirmation: " + err.message);
  237.         } else {
  238.             self.log("Confirmation accepted successfully");
  239.         }
  240.     });
  241.     */
  242.  
  243.     this.emit("newConfirmation", [confirmation]);
  244. };
  245.  
  246. xdBot.prototype.acceptConfirmation = function (confirmation, cb) {
  247.     var time = SteamTotp.time();
  248.     var confKey = SteamTotp.getConfirmationKey(this.identity_secret_, time, "allow");
  249.     confirmation.respond(time, confKey, true, function (err) {
  250.         typeof cb === "function" && cb(err);
  251.     });
  252. };
  253.  
  254. xdBot.prototype.declineConfirmation = function (confirmation, cb) {
  255.     var time = SteamTotp.time();
  256.     var confKey = SteamTotp.getConfirmationKey(this.identity_secret_, time, "cancel");
  257.     confirmation.respond(time, confKey, false, function (err) {
  258.         typeof cb === "function" && cb(err);
  259.     });
  260. };
  261.  
  262.  
  263. module.exports = xdBot;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement