Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var SteamUser = require('steam-user');
- var fs = require('fs');
- var crypto = require('crypto');
- var SteamTotp = require('steam-totp');
- var TradeOfferManager = require('steam-tradeoffer-manager');
- var SteamCommunity = require('steamcommunity');
- var Steam = require('steam-client');
- function xdBot(options) {
- this.cookies_ = null;
- this.account_name_ = options && options.account_name ? options.account_name : "";
- this.password_ = options && options.password ? options.password : "";
- this.shared_secret_ = options && options.shared_secret ? options.shared_secret : "";
- this.identity_secret_ = options && options.identity_secret ? options.identity_secret : "";
- this.confirmation_checking_interval_ = options && options.confirmation_checking_interval ? options.confirmation_checking_interval : 30000;
- this.trade_poll_data_file_ = options && options.trade_poll_data_file ? options.trade_poll_data_file : "poll_data.json";
- this.local_bind_address_ = options && options.local_bind_address ? options.local_bind_address : "0.0.0.0";
- this.m_client_ = new Steam.CMClient(Steam.EConnectionProtocol.TCP);
- this.m_client_.bind(this.local_bind_address_);
- this.steam_client_ = new SteamUser(this.m_client_);
- this.save_poll_data_ = true;
- this.steam_community_ = new SteamCommunity({
- localAddress: this.local_bind_address_
- });
- this.trade_offer_manager_ = new TradeOfferManager({
- "steam": this.steam_client_, // Polling every 30 seconds is fine since we get notifications from Steam
- "community": this.steam_community_,
- //"domain": "example.com", // Our domain is example.com
- "language": "en" // We want English item descriptions
- });
- this.callback_hash_ = {};
- this.registerHandlers();
- }
- xdBot.prototype = {
- constructor: xdBot,
- };
- xdBot.prototype.setSavePollData = function (value) {
- this.save_poll_data_ = value;
- };
- xdBot.prototype.isConnected = function () {
- return this.m_client_.connected;
- };
- xdBot.prototype.isLoggedOn = function () {
- return this.m_client_.loggedOn;
- };
- xdBot.prototype.registerHandlers = function () {
- var self = this;
- this.trade_offer_manager_.on("pollData", function (data) {
- if (self.save_poll_data_) {
- fs.writeFile(self.trade_poll_data_file_, JSON.stringify(data), function () { });
- }
- });
- this.steam_community_.on("debug", function (data) {
- self.log("[Steam Community Debug]: " + data);
- });
- this.steam_community_.on("sessionExpired", function (err) {
- self.log("steam community session expired");
- self.steam_community_.stopConfirmationChecker();
- self.log("relogging to the community...");
- if (self.isConnected() && self.isLoggedOn()) {
- try {
- self.steam_client_.webLogOn();
- }
- catch (e) {
- console.log("caught exception: ", e);
- }
- }
- else {
- }
- });
- this.steam_community_.on("confKeyNeeded", function (tag, callback) {
- self.log("conf key needed...");
- var time = Math.floor(Date.now() / 1000);
- callback(null, time, SteamTotp.getConfirmationKey(self.identity_secret_, time, tag));
- });
- this.steam_community_.on("newConfirmation", this.onNewConfirmation.bind(this));
- this.steam_client_.on("disconnected", function (eresult, msg) {
- self.log("steam client disconnected: " + msg);
- });
- this.steam_client_.on('loggedOn', function (details) {
- if (details.eresult === 1) {
- self.log("Logged into Steam!");
- }
- else {
- self.log("error login into steam");
- }
- });
- this.steam_client_.on("webSession", function (sessionID, cookies) {
- self.log("websession response!");
- self.cookies_ = cookies;
- self.trade_offer_manager_.setCookies(cookies, function (err) {
- if (err) {
- self.log("error setting cookies");
- process.exit(1); // Fatal error since we couldn't get our API key
- return;
- }
- self.steam_community_.startConfirmationChecker(self.confirmation_checking_interval_); // Checks and accepts confirmations every 30 seconds
- self.emit("ready", [self.cookies_]);
- });
- });
- this.steam_client_.on("error", function (err) {
- self.emit("steamUserError", [err]);
- });
- }
- xdBot.prototype.on = function (event, cb) {
- if (!this.callback_hash_.hasOwnProperty(event)) {
- this.callback_hash_[event] = [];
- }
- this.callback_hash_[event].push(cb);
- };
- xdBot.prototype.emit = function (event, args) {
- var cb_arr = this.callback_hash_.hasOwnProperty(event) ? this.callback_hash_[event] : [];
- for (var i = 0; i < cb_arr.length; ++i) {
- typeof cb_arr[i] === "function" && cb_arr[i].apply(this, args);
- }
- };
- xdBot.prototype.loadTradePollData = function (cb) {
- var self = this;
- fs.readFile(this.trade_poll_data_file_, function read(err, data) {
- if (!err) {
- try {
- self.trade_offer_manager_.pollData = JSON.parse(data);
- }
- catch (e) {
- err = e;
- }
- }
- return typeof cb === "function" && cb(err);
- });
- };
- xdBot.prototype.start = function () {
- var self = this;
- this.loadTradePollData(function (err) {
- self.log(err ? "could not load poll data from file" : "poll data loaded from file");
- self.steam_client_.logOn({
- "accountName": self.account_name_,
- "password": self.password_,
- "twoFactorCode": SteamTotp.getAuthCode(self.shared_secret_)
- });
- });
- };
- // xdBot.prototype.start = function (require_polldata = false, cb = null) {
- // var self = this;
- // this.loadTradePollData(function (err) {
- // self.log(err ? "could not load poll data from file" : "poll data loaded from file");
- // if (!require_polldata || !err) {
- // self.steam_client_.logOn({
- // "accountName": self.account_name_,
- // "password": self.password_,
- // "twoFactorCode": SteamTotp.getAuthCode(self.shared_secret_)
- // });
- // return typeof cb === "function" && cb(null);
- // }
- // else {
- // self.emit("startupFailed", [err]);
- // return typeof cb === "function" && cb(err);
- // }
- // });
- // };
- xdBot.prototype.onLoginSuccess = function () {
- //this.log("WE LOGGED IN!");
- };
- xdBot.prototype.log = function (obj) {
- this.emit("debug", [obj]);
- };
- xdBot.prototype.getTradeOfferManager = function () {
- return this.trade_offer_manager_;
- };
- xdBot.prototype.getSteamCommunity = function () {
- return this.steam_community_;
- };
- xdBot.prototype.checkConfirmations = function () {
- this.steam_community_.checkConfirmations();
- };
- xdBot.prototype.onNewConfirmation = function (confirmation) {
- /*
- var self = this;
- var time = SteamTotp.time();
- var confKey = SteamTotp.getConfirmationKey(this.identity_secret_, time, "allow");
- confirmation.respond(time, confKey, true, function (err) {
- if (err) {
- self.log("Cannot accept confirmation: " + err.message);
- } else {
- self.log("Confirmation accepted successfully");
- }
- });
- */
- this.emit("newConfirmation", [confirmation]);
- };
- xdBot.prototype.acceptConfirmation = function (confirmation, cb) {
- var time = SteamTotp.time();
- var confKey = SteamTotp.getConfirmationKey(this.identity_secret_, time, "allow");
- confirmation.respond(time, confKey, true, function (err) {
- typeof cb === "function" && cb(err);
- });
- };
- xdBot.prototype.declineConfirmation = function (confirmation, cb) {
- var time = SteamTotp.time();
- var confKey = SteamTotp.getConfirmationKey(this.identity_secret_, time, "cancel");
- confirmation.respond(time, confKey, false, function (err) {
- typeof cb === "function" && cb(err);
- });
- };
- module.exports = xdBot;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement