Advertisement
Guest User

Untitled

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