public Bot(Configuration.BotInfo config, string apiKey, UserHandlerCreator handlerCreator, Interface gui, bool debug = false) { while (Interface.loginClicked == false) { // Wait for user to login } logOnDetails = new SteamUser.LogOnDetails { Username = Interface.username, Password = Interface.password }; Admins = new ulong[1]; Admins[0] = 123456789; this.apiKey = apiKey; LogLevel = Log.LogLevel.Info; //log = new Log(config.LogFile, this.DisplayName, LogLevel); CreateHandler = handlerCreator; BotControlClass = config.BotControlClass; // Hacking around https ServicePointManager.ServerCertificateValidationCallback += SteamWeb.ValidateRemoteCertificate; ////log.Debug("Initializing Steam Bot..."); SteamClient = new SteamClient(); SteamTrade = SteamClient.GetHandler <SteamTrading>(); SteamUser = SteamClient.GetHandler <SteamUser>(); SteamFriends = SteamClient.GetHandler <SteamFriends>(); gui.UpdateLog("\r\nConnecting, please wait..."); ////log.Info("Connecting..."); SteamClient.Connect(); Thread CallbackThread = new Thread(() => // Callback Handling { while (true) { CallbackMsg msg = SteamClient.WaitForCallback(true); HandleSteamMessage(msg, gui); } }); CallbackThread.Start(); ////log.Success("Done Loading Bot!"); CallbackThread.Join(); }
public Bot(Configuration.BotInfo config, string apiKey, UserHandlerCreator handlerCreator, Interface gui, bool debug = false) { while (Interface.loginClicked == false) { // Wait for user to login } logOnDetails = new SteamUser.LogOnDetails { Username = Interface.username, Password = Interface.password }; Admins = new ulong[1]; Admins[0] = 123456789; this.apiKey = apiKey; LogLevel = Log.LogLevel.Info; //log = new Log(config.LogFile, this.DisplayName, LogLevel); CreateHandler = handlerCreator; BotControlClass = config.BotControlClass; // Hacking around https ServicePointManager.ServerCertificateValidationCallback += SteamWeb.ValidateRemoteCertificate; ////log.Debug("Initializing Steam Bot..."); SteamClient = new SteamClient(); SteamTrade = SteamClient.GetHandler<SteamTrading>(); SteamUser = SteamClient.GetHandler<SteamUser>(); SteamFriends = SteamClient.GetHandler<SteamFriends>(); gui.UpdateLog("\r\nConnecting, please wait..."); ////log.Info("Connecting..."); SteamClient.Connect(); Thread CallbackThread = new Thread(() => // Callback Handling { while (true) { CallbackMsg msg = SteamClient.WaitForCallback(true); HandleSteamMessage(msg, gui); } }); CallbackThread.Start(); ////log.Success("Done Loading Bot!"); CallbackThread.Join(); }
void HandleSteamMessage(CallbackMsg msg, Interface gui) { //log.Debug(msg.ToString()); #region Login msg.Handle<SteamClient.ConnectedCallback>(callback => { //log.Debug("Connection Callback: " + callback.Result); if (callback.Result == EResult.OK) { UserLogOn(); } else { gui.UpdateLog("Failed to connect to Steam Community, trying again..."); SteamClient.Connect(); } }); msg.Handle<SteamUser.LoggedOnCallback>(callback => { //log.Debug("Logged On Callback: " + callback.Result); if (callback.Result != EResult.OK) { gui.UpdateLog("\r\nLogin Error: " + callback.Result); //log.Error("Login Error: " + callback.Result); gui.Invoke((Action)(() => gui.text_username.Enabled = true)); gui.Invoke((Action)(() => gui.text_password.Enabled = true)); gui.Invoke((Action)(() => gui.button_login.Text = "Login")); } if (callback.Result == EResult.AccountLogonDenied) { ////log.Interface("This account is protected by Steam Guard. Enter the authentication code sent to the proper email: "); gui.Invoke((Action)(() => gui.SteamGuard())); while (Application.OpenForms.Count > 1) { } logOnDetails.AuthCode = SteamGuard.authCode; } if (callback.Result == EResult.InvalidLoginAuthCode) { gui.Invoke((Action)(() => gui.SteamGuard(true))); while (Application.OpenForms.Count > 1) { } logOnDetails.AuthCode = SteamGuard.authCode; } }); msg.Handle<SteamUser.LoginKeyCallback>(callback => { while (true) { bool authd = SteamWeb.Authenticate(callback, SteamClient, out sessionId, out token); if (authd) { gui.UpdateLog("\r\nUser authenticated!"); break; } else { gui.UpdateLog("\r\nAuthentication failed, retrying in 2s..."); Thread.Sleep(2000); } } SteamFriends.SetPersonaState(EPersonaState.Snooze); gui.UpdateLog("\r\nLogged in completely!\r\n"); gui.Invoke((Action)(() => gui.button_login.Text = "Logged in")); IsLoggedIn = true; while (true) { Console.ReadLine(); } }); // handle a special JobCallback differently than the others if (msg.IsType<SteamClient.JobCallback<SteamUser.UpdateMachineAuthCallback>>()) { msg.Handle<SteamClient.JobCallback<SteamUser.UpdateMachineAuthCallback>>( jobCallback => OnUpdateMachineAuthCallback(jobCallback.Callback, jobCallback.JobID) ); } #endregion #region Friends /*msg.Handle<SteamFriends.FriendsListCallback>(callback => { foreach (SteamFriends.FriendsListCallback.Friend friend in callback.FriendList) { if (!friends.Contains(friend.SteamID)) { friends.Add(friend.SteamID); if (friend.Relationship == EFriendRelationship.PendingInvitee && GetUserHandler(friend.SteamID).OnFriendAdd()) { SteamFriends.AddFriend(friend.SteamID); } } } });*/ msg.Handle<SteamFriends.FriendMsgCallback>(callback => { EChatEntryType type = callback.EntryType; if (callback.EntryType == EChatEntryType.ChatMsg || callback.EntryType == EChatEntryType.Emote) { //log.Info(String.Format("Chat Message from {0}: {1}", //SteamFriends.GetFriendPersonaName(callback.Sender), //callback.Message //)); GetUserHandler(callback.Sender).OnMessage(callback.Message, type); } }); #endregion #region Trading msg.Handle<SteamTrading.SessionStartCallback>(callback => { bool started = HandleTradeSessionStart(callback.OtherClient); if (!started) log.Error("Could not start the trade session."); else log.Debug("SteamTrading.SessionStartCallback handled successfully. Trade Opened."); }); msg.Handle<SteamTrading.TradeProposedCallback>(callback => { try { tradeManager.InitializeTrade(SteamUser.SteamID, callback.OtherClient); } catch { SteamFriends.SendChatMessage(callback.OtherClient, EChatEntryType.ChatMsg, "Trade declined. Could not correctly fetch your backpack."); SteamTrade.RespondToTrade(callback.TradeID, false); return; } if (tradeManager.OtherInventory.IsPrivate) { SteamFriends.SendChatMessage(callback.OtherClient, EChatEntryType.ChatMsg, "Trade declined. Your backpack cannot be private."); SteamTrade.RespondToTrade(callback.TradeID, false); return; } if (CurrentTrade == null && GetUserHandler(callback.OtherClient).OnTradeRequest()) SteamTrade.RespondToTrade(callback.TradeID, true); else SteamTrade.RespondToTrade(callback.TradeID, false); }); msg.Handle<SteamTrading.TradeResultCallback>(callback => { //log.Debug("Trade Status: " + callback.Response); if (callback.Response == EEconTradeResponse.Accepted) { //log.Info("Trade Accepted!"); } if (callback.Response == EEconTradeResponse.Cancel || callback.Response == EEconTradeResponse.ConnectionFailed || callback.Response == EEconTradeResponse.Declined || callback.Response == EEconTradeResponse.Error || callback.Response == EEconTradeResponse.InitiatorAlreadyTrading || callback.Response == EEconTradeResponse.TargetAlreadyTrading || callback.Response == EEconTradeResponse.Timeout || callback.Response == EEconTradeResponse.TooSoon || callback.Response == EEconTradeResponse.VacBannedInitiator || callback.Response == EEconTradeResponse.VacBannedTarget || callback.Response == EEconTradeResponse.NotLoggedIn) // uh... { CloseTrade(); } }); #endregion #region Disconnect msg.Handle<SteamUser.LoggedOffCallback>(callback => { IsLoggedIn = false; gui.UpdateLog("\r\nLogged Off: " + callback.Result); //log.Warn("Logged Off: " + callback.Result); }); msg.Handle<SteamClient.DisconnectedCallback>(callback => { IsLoggedIn = false; CloseTrade(); gui.UpdateLog("\r\nDisconnected from Steam Network! Trying to reconnect..."); //log.Warn("Disconnected from Steam Network!"); SteamClient.Connect(); }); #endregion }
void HandleSteamMessage(CallbackMsg msg, Interface gui) { //log.Debug(msg.ToString()); #region Login msg.Handle <SteamClient.ConnectedCallback>(callback => { //log.Debug("Connection Callback: " + callback.Result); if (callback.Result == EResult.OK) { UserLogOn(); } else { gui.UpdateLog("Failed to connect to Steam Community, trying again..."); SteamClient.Connect(); } }); msg.Handle <SteamUser.LoggedOnCallback>(callback => { //log.Debug("Logged On Callback: " + callback.Result); if (callback.Result != EResult.OK) { gui.UpdateLog("\r\nLogin Error: " + callback.Result); //log.Error("Login Error: " + callback.Result); gui.Invoke((Action)(() => gui.text_username.Enabled = true)); gui.Invoke((Action)(() => gui.text_password.Enabled = true)); gui.Invoke((Action)(() => gui.button_login.Text = "Login")); } if (callback.Result == EResult.AccountLogonDenied) { ////log.Interface("This account is protected by Steam Guard. Enter the authentication code sent to the proper email: "); gui.Invoke((Action)(() => gui.SteamGuard())); while (Application.OpenForms.Count > 1) { } logOnDetails.AuthCode = SteamGuard.authCode; } if (callback.Result == EResult.InvalidLoginAuthCode) { gui.Invoke((Action)(() => gui.SteamGuard(true))); while (Application.OpenForms.Count > 1) { } logOnDetails.AuthCode = SteamGuard.authCode; } }); msg.Handle <SteamUser.LoginKeyCallback>(callback => { while (true) { bool authd = SteamWeb.Authenticate(callback, SteamClient, out sessionId, out token); if (authd) { gui.UpdateLog("\r\nUser authenticated!"); break; } else { gui.UpdateLog("\r\nAuthentication failed, retrying in 2s..."); Thread.Sleep(2000); } } SteamFriends.SetPersonaState(EPersonaState.Snooze); gui.UpdateLog("\r\nLogged in completely!\r\n"); gui.Invoke((Action)(() => gui.button_login.Text = "Logged in")); IsLoggedIn = true; while (true) { Console.ReadLine(); } }); // handle a special JobCallback differently than the others if (msg.IsType <SteamClient.JobCallback <SteamUser.UpdateMachineAuthCallback> >()) { msg.Handle <SteamClient.JobCallback <SteamUser.UpdateMachineAuthCallback> >( jobCallback => OnUpdateMachineAuthCallback(jobCallback.Callback, jobCallback.JobID) ); } #endregion #region Friends /*msg.Handle<SteamFriends.FriendsListCallback>(callback => * { * foreach (SteamFriends.FriendsListCallback.Friend friend in callback.FriendList) * { * if (!friends.Contains(friend.SteamID)) * { * friends.Add(friend.SteamID); * if (friend.Relationship == EFriendRelationship.PendingInvitee && * GetUserHandler(friend.SteamID).OnFriendAdd()) * { * SteamFriends.AddFriend(friend.SteamID); * } * } * } * });*/ msg.Handle <SteamFriends.FriendMsgCallback>(callback => { EChatEntryType type = callback.EntryType; if (callback.EntryType == EChatEntryType.ChatMsg || callback.EntryType == EChatEntryType.Emote) { //log.Info(String.Format("Chat Message from {0}: {1}", //SteamFriends.GetFriendPersonaName(callback.Sender), //callback.Message //)); GetUserHandler(callback.Sender).OnMessage(callback.Message, type); } }); #endregion #region Trading msg.Handle <SteamTrading.SessionStartCallback>(callback => { bool started = HandleTradeSessionStart(callback.OtherClient); if (!started) { log.Error("Could not start the trade session."); } else { log.Debug("SteamTrading.SessionStartCallback handled successfully. Trade Opened."); } }); msg.Handle <SteamTrading.TradeProposedCallback>(callback => { try { tradeManager.InitializeTrade(SteamUser.SteamID, callback.OtherClient); } catch { SteamFriends.SendChatMessage(callback.OtherClient, EChatEntryType.ChatMsg, "Trade declined. Could not correctly fetch your backpack."); SteamTrade.RespondToTrade(callback.TradeID, false); return; } if (tradeManager.OtherInventory.IsPrivate) { SteamFriends.SendChatMessage(callback.OtherClient, EChatEntryType.ChatMsg, "Trade declined. Your backpack cannot be private."); SteamTrade.RespondToTrade(callback.TradeID, false); return; } if (CurrentTrade == null && GetUserHandler(callback.OtherClient).OnTradeRequest()) { SteamTrade.RespondToTrade(callback.TradeID, true); } else { SteamTrade.RespondToTrade(callback.TradeID, false); } }); msg.Handle <SteamTrading.TradeResultCallback>(callback => { //log.Debug("Trade Status: " + callback.Response); if (callback.Response == EEconTradeResponse.Accepted) { //log.Info("Trade Accepted!"); } if (callback.Response == EEconTradeResponse.Cancel || callback.Response == EEconTradeResponse.ConnectionFailed || callback.Response == EEconTradeResponse.Declined || callback.Response == EEconTradeResponse.Error || callback.Response == EEconTradeResponse.InitiatorAlreadyTrading || callback.Response == EEconTradeResponse.TargetAlreadyTrading || callback.Response == EEconTradeResponse.Timeout || callback.Response == EEconTradeResponse.TooSoon || callback.Response == EEconTradeResponse.VacBannedInitiator || callback.Response == EEconTradeResponse.VacBannedTarget || callback.Response == EEconTradeResponse.NotLoggedIn) // uh... { CloseTrade(); } }); #endregion #region Disconnect msg.Handle <SteamUser.LoggedOffCallback>(callback => { IsLoggedIn = false; gui.UpdateLog("\r\nLogged Off: " + callback.Result); //log.Warn("Logged Off: " + callback.Result); }); msg.Handle <SteamClient.DisconnectedCallback>(callback => { IsLoggedIn = false; CloseTrade(); gui.UpdateLog("\r\nDisconnected from Steam Network! Trying to reconnect..."); //log.Warn("Disconnected from Steam Network!"); SteamClient.Connect(); }); #endregion }