diff --git a/components/confirmations.js b/components/confirmations.js index 66770cd..04681e1 100644 --- a/components/confirmations.js +++ b/components/confirmations.js @@ -1,7 +1,6 @@ var SteamCommunity = require('../index.js'); var Cheerio = require('cheerio'); var SteamTotp = require('steam-totp'); -var Async = require('async'); var CConfirmation = require('../classes/CConfirmation.js'); @@ -276,150 +275,3 @@ function request(community, url, key, time, tag, params, json, callback) { callback(null, body); }, "steamcommunity"); } - -// Confirmation checker - -/** - * Start automatically polling our confirmations for new ones. The `confKeyNeeded` event will be emitted when we need a confirmation key, or `newConfirmation` when we get a new confirmation - * @param {int} pollInterval - The interval, in milliseconds, at which we will poll for confirmations. This should probably be at least 10,000 to avoid rate-limits. - * @param {Buffer|string|null} [identitySecret=null] - Your identity_secret. If passed, all confirmations will be automatically accepted and nothing will be emitted. - */ -SteamCommunity.prototype.startConfirmationChecker = function(pollInterval, identitySecret) { - this._confirmationPollInterval = pollInterval; - this._knownConfirmations = this._knownConfirmations || {}; - this._confirmationKeys = this._confirmationKeys || {}; - this._identitySecret = identitySecret; - - if(this._confirmationTimer) { - clearTimeout(this._confirmationTimer); - } - - setTimeout(this.checkConfirmations.bind(this), 500); -}; - -/** - * Stop automatically polling our confirmations. - */ -SteamCommunity.prototype.stopConfirmationChecker = function() { - if(this._confirmationPollInterval) { - delete this._confirmationPollInterval; - } - - if(this._identitySecret) { - delete this._identitySecret; - } - - if(this._confirmationTimer) { - clearTimeout(this._confirmationTimer); - delete this._confirmationTimer; - } -}; - -/** - * Run the confirmation checker right now instead of waiting for the next poll. - * Useful to call right after you send/accept an offer that needs confirmation. - */ -SteamCommunity.prototype.checkConfirmations = function() { - if(this._confirmationTimer) { - clearTimeout(this._confirmationTimer); - delete this._confirmationTimer; - } - - var self = this; - if(!this._confirmationQueue) { - this._confirmationQueue = Async.queue(function(conf, callback) { - // Worker to process new confirmations - if(self._identitySecret) { - // We should accept this - self.emit('debug', "Accepting confirmation #" + conf.id); - var time = Math.floor(Date.now() / 1000); - conf.respond(time, SteamTotp.getConfirmationKey(self._identitySecret, time, "allow"), true, function(err) { - // If there was an error and it wasn't actually accepted, we'll pick it up again - if (!err) self.emit('confirmationAccepted', conf); - delete self._knownConfirmations[conf.id]; - setTimeout(callback, 1000); // Call the callback in 1 second, to make sure the time changes - }); - } else { - self.emit('newConfirmation', conf); - setTimeout(callback, 1000); // Call the callback in 1 second, to make sure the time changes - } - }, 1); - } - - this.emit('debug', 'Checking confirmations'); - - this._confirmationCheckerGetKey('conf', function(err, key) { - if(err) { - resetTimer(); - return; - } - - self.getConfirmations(key.time, key.key, function(err, confirmations) { - if(err) { - self.emit('debug', "Can't check confirmations: " + err.message); - resetTimer(); - return; - } - - var known = self._knownConfirmations; - - var newOnes = confirmations.filter(function(conf) { - return !known[conf.id]; - }); - - if(newOnes.length < 1) { - resetTimer(); - return; // No new ones - } - - // We have new confirmations! - newOnes.forEach(function(conf) { - self._knownConfirmations[conf.id] = conf; // Add it to our list of known confirmations - self._confirmationQueue.push(conf); - }); - - resetTimer(); - }); - }); - - function resetTimer() { - if(self._confirmationPollInterval) { - self._confirmationTimer = setTimeout(self.checkConfirmations.bind(self), self._confirmationPollInterval); - } - } -}; - -SteamCommunity.prototype._confirmationCheckerGetKey = function(tag, callback) { - if(this._identitySecret) { - if(tag == 'details') { - // We don't care about details - callback(new Error("Disabled")); - return; - } - - var time = Math.floor(Date.now() / 1000); - callback(null, {"time": time, "key": SteamTotp.getConfirmationKey(this._identitySecret, time, tag)}); - return; - } - - var existing = this._confirmationKeys[tag]; - var reusable = ['conf', 'details']; - - // See if we already have a key that we can reuse. - if(reusable.indexOf(tag) != -1 && existing && (Date.now() - (existing.time * 1000) < (1000 * 60 * 5))) { - callback(null, existing); - return; - } - - // We need a fresh one - var self = this; - this.emit('confKeyNeeded', tag, function(err, time, key) { - if(err) { - callback(err); - return; - } - - self._confirmationKeys[tag] = {"time": time, "key": key}; - callback(null, {"time": time, "key": key}); - }); -};