From 71cb14a1c47bf78ddef44c3089e9a1034ecfdbc9 Mon Sep 17 00:00:00 2001 From: Kyle Smith Date: Thu, 6 Aug 2015 13:40:25 -0500 Subject: [PATCH] Fix some stuff --- lib/chatBot.js | 82 ++++++++++++++++++++++++++++---------------------- 1 file changed, 46 insertions(+), 36 deletions(-) diff --git a/lib/chatBot.js b/lib/chatBot.js index e19e029b..757e1294 100644 --- a/lib/chatBot.js +++ b/lib/chatBot.js @@ -31,16 +31,18 @@ if (fs.existsSync(serversFile)) { // Bot should be usually created without options, it is a parameter mainly for testing var ChatBot = function(username, password, options) { + var that = this; + this.version = ver; this.winston = new winston.Logger; this.options = options || {}; - + this.steamClient = options.client || new steam.SteamClient(); this.steamUser = options.user || new steam.SteamUser(this.steamClient); this.steamFriends = options.friends || new steam.SteamFriends(this.steamClient); this.steamTrading = options.trading || new steam.SteamTrading(this.steamClient); - this.steamWebLogon = new SteamWebLogon(this.steamClient, this.steamUser); + this.steamWebLogon = new SteamWebLogon(that.steamClient, that.steamUser); this.steamTrade = new SteamTrade(); this.name = options.name || username; this.username = username; @@ -68,7 +70,6 @@ var ChatBot = function(username, password, options) { json: false }); - var that = this; if(!options.logFile || options.logFile !== false || options.logFile === null || options.logFile === undefined) { if(options.logFile === true) { that.logFile = "bot." + that.name + ".log"; } else { that.logFile = options.logFile; } @@ -146,12 +147,13 @@ var ChatBot = function(username, password, options) { this.steamClient.on("error", function() { that._onError(); }); this.steamClient.on("logOnResponse", function(res) { that._onLogOnResponse(res); that.onLogon(that); that.triggerLoggedOn(); }); this.steamClient.on("loggedOff", function(eresult) { that._onDisconnected(eresult); that.triggerLoggedOff(); }); - this.steamClient.on('connected', function() { that._onConnected(); }); - + this.steamClient.on('connected', function() { that._onConnected(); }); + this.steamClient.on('message', function(header, body, cb) { that._onMessage(header, body, cb) }); + //Steam-user events - this.steamUser.on("updateMachineAuth", function(sentry, cb) { that._onUpdateMachineAuth(sentry, cb); }) - this.steamUser.on('tradeOffers', function(number) { that._onTradeOffers(number); }); - + this.steamUser.on("updateMachineAuth", function(sentry, cb) { that._onUpdateMachineAuth(sentry, cb); }) + this.steamUser.on('tradeOffers', function(number) { that._onTradeOffers(number); }); + //Steam-friends events this.steamFriends.on("chatInvite", function(roomId, roomName, inviterId) { that._onChatInvite(roomId, roomName, inviterId); }); this.steamFriends.on("friend", function(userId, relationship) { that._onRelationship(userId, relationship); }); @@ -159,15 +161,15 @@ var ChatBot = function(username, password, options) { this.steamFriends.on("chatMsg", function(roomId, message, type, chatterId) { that._onChatMsg(roomId, message, type, chatterId); }); this.steamFriends.on("chatStateChange", function(stateChange, chatterActedOn, steamChatId, actedOnBy) { that._onChatStateChange(stateChange, chatterActedOn, steamChatId, actedOnBy); }); //this.steamFriends.on('clanState', function(res) { that._onClanState(res); }); - + //Steam-trading events this.steamTrading.on('tradeProposed', function(tradeID, steamID) { that._onTradeProposed(tradeID, steamID, that.acceptTrade); }); this.steamTrading.on('tradeResult', function(tradeID, result, steamID) { that._onTradeResult(tradeID, result, steamID) }); this.steamTrading.on('sessionStart', function(steamID) { that._onSessionStart(steamID) }); - + //This was removed in node-steam 1.0.0, using module node-steam-weblogon by Alex7Kom instead //this.steamClient.on('webSessionID', function(sessionID) { that._onWebSessionID(sessionID); }); - + //This was replaced in node-steam 1.0.0 with 'clanState', which is handled above in steam-friends //this.steamClient.on('announcement', function(groupID, headline) { that._onAnnouncement(groupID, headline); }); /* @@ -176,7 +178,7 @@ var ChatBot = function(username, password, options) { this.steamClient.on("group", function(group, clanRelationship) {that._onGroup(group, clanRelationship); }); this.steamClient.on("richPresence", function(steamId,status,obscureData) {that._onRichPresence(steamId,status,obscureData); }); */ - + // Store latest servers this.steamClient.on("servers", function(servers) { try{ @@ -201,7 +203,7 @@ ChatBot.prototype.onLogon = function(bot) { ChatBot.prototype.logOn = function() { - // Continuously try to reconnect if started but not connected + // Continuously try to reconnect if started but not connected // If someone logs in as the bot it will be disconnected, so this allows the bot to recover automatically when it can if (this.options.autoReconnect && !this.babysitInterval) { var babysitTimer = this.options.babysitTimer || 5*60*1000; @@ -394,17 +396,17 @@ ChatBot.prototype.sendMessage = function(steamId, message) { //left this here because some configs might still be using it? ChatBot.prototype.joinGame = function(appId) { this.games=[appId]; //update this.games - this.steamUser.gamesPlayed([this.games]); + this.steamUser.gamesPlayed({ 'games_played': [{ 'game_id': parseInt(appId) }] }); } -//this function will play all the games it's told to. This doesn't always show -//the first game as the one being played, so there's another function that +//this function will play all the games it's told to. This doesn't always show +//the first game as the one being played, so there's another function that //plays the first game, then waits a fraction of a second to play the others ChatBot.prototype.setGames = function(appIdArray) { this.games=appIdArray; //update this.games if(this.games) this.winston.info("Chatbot "+this.name+": Playing gameIDs " + this.games.toString()); else this.winston.info("Chatbot "+this.name+": Playing nothing"); - this.steamUser.gamesPlayed(this.games); //play them! + this.steamUser.gamesPlayed({ 'games_played': [{ 'game_id': parseInt(that.games.toString()) }] }); //play them! } ChatBot.prototype.setPrimaryGame = function(appId,delay) { @@ -415,11 +417,11 @@ ChatBot.prototype.setPrimaryGame = function(appId,delay) { this.games.unshift(appId); //update this.games } this.winston.info("Chatbot "+this.name+": Playing gameID " + appId); - this.steamUser.gamesPlayed([appId]); //first, play only this game, so it shows up + this.steamUser.gamesPlayed({ 'games_played': [{ 'game_id': parseInt(appId) }] }); //first, play only this game, so it shows up var that = this; setTimeout(function(){ that.winston.info("Chatbot "+that.name+": Playing gameIDs " + that.games.toString()); - that.steamUser.gamesPlayed(that.games); //play them! + that.steamUser.gamesPlayed({ 'games_played': [{ 'game_id': parseInt(that.games.toString()) }] }); //play them! },(delay||1000)); //play all the games in 1 second. } @@ -614,7 +616,12 @@ ChatBot.prototype._onConnected = function() { this.logOn(); } -ChatBot.prototype._onError = function() { +ChatBot.prototype._onMessage = function(header, body, callback) { + var that = this; + this.winston.silly('ChatBot ' + this.name + ' new ProtoBuf message: ' + header.msg + (header.proto ? ', ' + JSON.stringify(header.proto) : '')); +} + +ChatBot.prototype._onError = function() { this.winston.error("Chatbot "+this.name+" disconnected"); this.connected = false; this.connect(); @@ -622,12 +629,12 @@ ChatBot.prototype._onError = function() { ChatBot.prototype._onLogOnResponse = function(res) { var that = this; - if(res.EResult === steam.EResult.OK) { + if(res.eresult === steam.EResult.OK) { this.winston.info("ChatBot " + this.name + " logged on"); this.connected = true; this._updatePersonaState(); this._autojoinChatrooms(); - this.steamUser.gamesPlayed(this.games); + this.steamUser.gamesPlayed({ 'games_played': [{ 'game_id': parseInt(that.games.toString()) }] }); this.steamWebLogon.webLogOn(function(sessionid, cookies) { that.steamTrade.sessionID = sessionid; that.winston.debug('New cookie'); @@ -635,12 +642,15 @@ ChatBot.prototype._onLogOnResponse = function(res) { that.cookie = cookies; that.winston.silly('New sessionid: ' + sessionid); that.winston.silly('New cookie: ' + cookies); - that.steamTrade.setCookie(cookies); - that.winston.info('Logged into Steam web'); + cookies.forEach(function(cookie) { + that.steamTrade.setCookie(cookie.trim()); + }); + that.winston.info('ChatBot ' + that.name + ':lLogged into Steam web'); }); } else { - this.logger.warn('EResult for logon response: ' + res.EResult); + this.winston.warn("ChatBot " + this.name + ': EResult for logon response: ' + res.eresult); + process.exit(res.eresult); } } @@ -649,7 +659,7 @@ ChatBot.prototype._onDisconnected = function() { this.logOn(); } -ChatBot.prototype._onChatInvite = function(roomId, roomName, inviterId) { +ChatBot.prototype._onChatInvite = function(roomId, roomName, inviterId) { this.winston.info("ChatBot " + this.name + " was invited to chat in " + roomName + " (" + roomId + ")" + " by " + this._userString(inviterId)); _.each(this.triggers, function(trigger) { @@ -657,7 +667,7 @@ ChatBot.prototype._onChatInvite = function(roomId, roomName, inviterId) { }); }; -ChatBot.prototype._onRelationship = function(userId, relationship) { +ChatBot.prototype._onRelationship = function(userId, relationship) { this.winston.info("ChatBot " + this.name + " relationship event for " + this._userString(userId) + " type " + relationship); if (relationship === 2) { @@ -667,7 +677,7 @@ ChatBot.prototype._onRelationship = function(userId, relationship) { } }; -ChatBot.prototype._onFriendMsg = function(userId, message, type) { +ChatBot.prototype._onFriendMsg = function(userId, message, type) { this.winston.info("ChatBot " + this.name + " friendMsg " + type + " <" + this._userString(userId) + ">: " + message); if (type === steam.EChatEntryType.ChatMsg) { @@ -679,7 +689,7 @@ ChatBot.prototype._onFriendMsg = function(userId, message, type) { } }; -ChatBot.prototype._onChatMsg = function(roomId, message, type, chatterId) { +ChatBot.prototype._onChatMsg = function(roomId, message, type, chatterId) { this.winston.info("ChatBot " + this.name + " chatMsg " + type + " in " + roomId + " <" + this._userString(chatterId) + ">: " + message); if (type === steam.EChatEntryType.ChatMsg) { @@ -692,12 +702,12 @@ ChatBot.prototype._onChatMsg = function(roomId, message, type, chatterId) { } }; -ChatBot.prototype._onChatStateChange = function(stateChange, chatterActedOn, steamChatId, chatterActedBy) { +ChatBot.prototype._onChatStateChange = function(stateChange, chatterActedOn, steamChatId, chatterActedBy) { this.winston.info("ChatBot " + this.name + " chatStateChange " + stateChange + " in " + steamChatId + " " + chatterActedOn + " acted on by " + chatterActedBy); var muted = this.muted; var haveSentMessage = false; var sentMessageThisTrigger = false; - + if ((stateChange & steam.EChatMemberStateChange.Kicked) > 0) { this.winston.info("Chatbot "+this.name+":"+this._userString(chatterActedOn) + " was kicked from " + steamChatId + " by " + this._userString(chatterActedBy)); @@ -753,10 +763,10 @@ ChatBot.prototype._onChatStateChange = function(stateChange, chatterActedOn, ste ChatBot.prototype._onUpdateMachineAuth = function(sentry, cb) { if(this.sentryFile) { - this.winston.info("Chatbot "+this.name+":Obtained sentry. Writing to " + this.sentryFile); + this.winston.info("Chatbot "+this.name+":Obtained sentry " + sentry.filename + ". Writing to " + this.sentryFile); fs.writeFileSync(this.sentryFile, sentry.bytes); cb({ - sha_file: crypto.createHash('sha1').update(sentry.bytes).disgest() + sha_file: crypto.createHash('sha1').update(sentry.bytes).digest() }); } else { this.winston.info("Chatbot "+this.name+":Obtained sentry. Writing to " + this.name + ".hash", sentry); @@ -838,21 +848,21 @@ ChatBot.prototype._onClanState = function(res) { trigger.onLoggedOff(); }); } - + ChatBot.prototype._onUser = function(obscureData) { this.winston.info("ChatBot " + this.name + " - User event detected"); _.each(this.triggers, function(trigger) { trigger.onUserEvent(obscureData); }); } - + ChatBot.prototype._onFriend = function(steamId,friendRelationship) { this.winston.info("ChatBot " + this.name + " - Friend relationship event detected"); _.each(this.triggers, function(trigger) { trigger.onFriendEvent(steamId,friendRelationship); }); } - + ChatBot.prototype._onGroup = function(steamId,clanRelationship) { this.winston.info("ChatBot " + this.name + " - Group relationship event detected"); _.each(this.triggers, function(trigger) {