Ejemplo n.º 1
0
        /// <summary>
        /// Determines whether this callback is a certain type.
        /// </summary>
        /// <typeparam name="T">The type to check against.</typeparam>
        /// <returns>
        ///     <c>true</c> if this callback is the type specified; otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///     <c>msg</c> is null.
        /// </exception>
        public static bool IsType <T>(this ICallbackMsg msg)
            where T : ICallbackMsg
        {
            if (msg == null)
            {
                throw new ArgumentNullException("msg");
            }

            return(msg is T);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Invokes the specified handler delegate if the callback matches the type parameter.
        /// </summary>
        /// <typeparam name="T">The type to check against.</typeparam>
        /// <param name="msg">The callback in question.</param>
        /// <param name="handler">The handler to invoke.</param>
        /// <exception cref="ArgumentNullException">
        ///     <c>msg</c> is null or <c>handler</c> is null.
        /// </exception>
        public static void Handle <T>(this ICallbackMsg msg, Action <T> handler)
            where T : class, ICallbackMsg
        {
            if (msg == null)
            {
                throw new ArgumentNullException("msg");
            }

            if (handler == null)
            {
                throw new ArgumentNullException("handler");
            }

            var callback = msg as T;

            if (callback != null)
            {
                handler(callback);
            }
        }
Ejemplo n.º 3
0
        void HandleSteamMessage(ICallbackMsg msg)
        {
            Log.Debug(msg.ToString());
            #region Login
            msg.Handle<SteamClient.ConnectedCallback> (callback =>
            {
                Log.Debug ("Connection Callback: {0}", callback.Result);

                if (callback.Result == EResult.OK)
                {
                    UserLogOn();
                }
                else
                {
                    Log.Error ("Failed to connect to Steam Community, trying again...");
                    SteamClient.Connect ();
                }

            });

            msg.Handle<SteamUser.LoggedOnCallback> (callback =>
            {
                Log.Debug("Logged On Callback: {0}", callback.Result);

                if (callback.Result == EResult.OK)
                {
                    myUserNonce = callback.WebAPIUserNonce;
                }
                else
                {
                    Log.Error("Login Error: {0}", callback.Result);
                }

                if (callback.Result == EResult.AccountLogonDenied)
                {
                    Log.Interface ("This account is SteamGuard enabled. Enter the code via the `auth' command.");

                    // try to get the steamguard auth code from the event callback
                    var eva = new SteamGuardRequiredEventArgs();
                    FireOnSteamGuardRequired(eva);
                    if (!String.IsNullOrEmpty(eva.SteamGuard))
                        logOnDetails.AuthCode = eva.SteamGuard;
                    else
                        logOnDetails.AuthCode = Console.ReadLine();
                }

                if (callback.Result == EResult.InvalidLoginAuthCode)
                {
                    Log.Interface("The given SteamGuard code was invalid. Try again using the `auth' command.");
                    logOnDetails.AuthCode = Console.ReadLine();
                }
            });

            msg.Handle<SteamUser.LoginKeyCallback> (callback =>
            {
                myUniqueId = callback.UniqueID.ToString();

                UserWebLogOn();

                if (Trade.CurrentSchema == null)
                {
                    Log.Info ("Downloading Schema...");
                    Trade.CurrentSchema = Schema.FetchSchema (ApiKey, schemaLang);
                    Log.Success ("Schema Downloaded!");
                }

                SteamFriends.SetPersonaName (DisplayNamePrefix+DisplayName);
                SteamFriends.SetPersonaState (EPersonaState.Online);

                Log.Success ("Steam Bot Logged In Completely!");

                GetUserHandler(SteamClient.SteamID).OnLoginCompleted();
            });

            msg.Handle<SteamUser.WebAPIUserNonceCallback>(webCallback =>
            {
                Log.Debug("Received new WebAPIUserNonce.");

                if (webCallback.Result == EResult.OK)
                {
                    myUserNonce = webCallback.Nonce;
                    UserWebLogOn();
                }
                else
                {
                    Log.Error("WebAPIUserNonce Error: " + webCallback.Result);
                }
            });

            msg.Handle<SteamUser.UpdateMachineAuthCallback>(
                authCallback => OnUpdateMachineAuthCallback(authCallback)
            );
            #endregion

            #region Friends
            msg.Handle<SteamFriends.FriendsListCallback>(callback =>
            {
                foreach (SteamFriends.FriendsListCallback.Friend friend in callback.FriendList)
                {
                    switch (friend.SteamID.AccountType)
                    {
                        case EAccountType.Clan:
                            if (friend.Relationship == EFriendRelationship.RequestRecipient)
                            {
                                if (GetUserHandler(friend.SteamID).OnGroupAdd())
                                {
                                    AcceptGroupInvite(friend.SteamID);
                                }
                                else
                                {
                                    DeclineGroupInvite(friend.SteamID);
                                }
                            }
                            break;
                        default:
                            CreateFriendsListIfNecessary();
                            if (friend.Relationship == EFriendRelationship.None)
                            {
                                friends.Remove(friend.SteamID);
                                GetUserHandler(friend.SteamID).OnFriendRemove();
                                RemoveUserHandler(friend.SteamID);
                            }
                            else if (friend.Relationship == EFriendRelationship.RequestRecipient)
                    {
                                if (GetUserHandler(friend.SteamID).OnFriendAdd())
                                {
                        if (!friends.Contains(friend.SteamID))
                        {
                            friends.Add(friend.SteamID);
                                    }
                                    else
                            {
                                        Log.Error("Friend was added who was already in friends list: " + friend.SteamID);
                                    }
                                SteamFriends.AddFriend(friend.SteamID);
                            }
                        else
                        {
                                    SteamFriends.RemoveFriend(friend.SteamID);
                                RemoveUserHandler(friend.SteamID);
                            }
                        }
                            break;
                    }
                }
            });

            msg.Handle<SteamFriends.FriendMsgCallback> (callback =>
            {
                EChatEntryType type = callback.EntryType;

                if (callback.EntryType == EChatEntryType.ChatMsg)
                {
                    Log.Info ("Chat Message from {0}: {1}",
                                         SteamFriends.GetFriendPersonaName (callback.Sender),
                                         callback.Message
                                         );
                    GetUserHandler(callback.Sender).OnMessageHandler(callback.Message, type);
                }
            });
            #endregion

            #region Group Chat
            msg.Handle<SteamFriends.ChatMsgCallback>(callback =>
            {
                GetUserHandler(callback.ChatterID).OnChatRoomMessage(callback.ChatRoomID, callback.ChatterID, callback.Message);
            });
            #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 =>
            {
                if (CheckCookies() == false)
                {
                    SteamTrade.RespondToTrade(callback.TradeID, false);
                    return;
                }

                try
                {
                    tradeManager.InitializeTrade(SteamUser.SteamID, callback.OtherClient);
                }
                catch (WebException we)
                {
                    SteamFriends.SendChatMessage(callback.OtherClient,
                             EChatEntryType.ChatMsg,
                             "Trade error: " + we.Message);

                    SteamTrade.RespondToTrade(callback.TradeID, false);
                    return;
                }
                catch (Exception)
                {
                    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 =>
            {
                if (callback.Response == EEconTradeResponse.Accepted)
                {
                    Log.Debug("Trade Status: {0}", callback.Response);
                    Log.Info ("Trade Accepted!");
                    GetUserHandler(callback.OtherClient).OnTradeRequestReply(true, callback.Response.ToString());
                }
                else
                {
                    Log.Warn("Trade failed: {0}", callback.Response);
                    CloseTrade ();
                    GetUserHandler(callback.OtherClient).OnTradeRequestReply(false, callback.Response.ToString());
                }

            });
            #endregion

            #region Disconnect
            msg.Handle<SteamUser.LoggedOffCallback> (callback =>
            {
                IsLoggedIn = false;
                Log.Warn("Logged off Steam.  Reason: {0}", callback.Result);
            });

            msg.Handle<SteamClient.DisconnectedCallback> (callback =>
            {
                if(IsLoggedIn)
                {
                    IsLoggedIn = false;
                    CloseTrade();
                    Log.Warn("Disconnected from Steam Network!");
                }

                SteamClient.Connect ();
            });
            #endregion

            #region Notifications
            msg.Handle<SteamNotifications.NotificationCallback>(callback =>
            {
                //currently only appears to be of trade offer
                if (callback.Notifications.Count != 0)
                {
                    foreach (var notification in callback.Notifications)
                    {
                        Log.Info(notification.UserNotificationType + " notification");
            }
                }

                // Get offers only if cookies are valid
                if (CheckCookies())
                    tradeOfferManager.GetOffers();
            });

            msg.Handle<SteamNotifications.CommentNotificationCallback>(callback =>
            {
                //various types of comment notifications on profile/activity feed etc
                //Log.Info("received CommentNotificationCallback");
                //Log.Info("New Commments " + callback.CommentNotifications.CountNewComments);
                //Log.Info("New Commments Owners " + callback.CommentNotifications.CountNewCommentsOwner);
                //Log.Info("New Commments Subscriptions" + callback.CommentNotifications.CountNewCommentsSubscriptions);
            });
            #endregion
        }
Ejemplo n.º 4
0
Archivo: Bot.cs Proyecto: TF2Maps/VBot
        void HandleSteamMessage(ICallbackMsg msg)
        {
            Log.Debug(msg.ToString());
            #region Login
            msg.Handle<SteamClient.ConnectedCallback> (callback =>
            {
                Log.Debug ("Connection Callback: {0}", callback.Result);

                if (callback.Result == EResult.OK)
                {
                    UserLogOn();
                }
                else
                {
                    Log.Error ("Failed to connect to Steam Community, trying again...");
                    SteamClient.Connect ();
                }

            });

            msg.Handle<SteamUser.LoggedOnCallback> (callback =>
            {
                Log.Debug("Logged On Callback: {0}", callback.Result);

                if (callback.Result == EResult.OK)
                {
                    myUserNonce = callback.WebAPIUserNonce;
                }
                else
                {
                    Log.Error("Login Error: {0}", callback.Result);
                }

                if (callback.Result == EResult.AccountLogonDeniedNeedTwoFactorCode)
                {
                    var mobileAuthCode = GetMobileAuthCode();
                    if (string.IsNullOrEmpty(mobileAuthCode))
                    {
                        Log.Error("Failed to generate 2FA code. Make sure you have linked the authenticator via SteamBot.");
                    }
                    else
                    {
                        logOnDetails.TwoFactorCode = mobileAuthCode;
                        Log.Success("Generated 2FA code.");
                    }
                }
                else if (callback.Result == EResult.TwoFactorCodeMismatch)
                {
                    SteamAuth.TimeAligner.AlignTime();
                    logOnDetails.TwoFactorCode = SteamGuardAccount.GenerateSteamGuardCode();
                    Log.Success("Regenerated 2FA code.");
                }
                else if (callback.Result == EResult.AccountLogonDenied)
                {
                    Log.Interface ("This account is SteamGuard enabled. Enter the code via the `auth' command.");

                    // try to get the steamguard auth code from the event callback
                    var eva = new SteamGuardRequiredEventArgs();
                    FireOnSteamGuardRequired(eva);
                    if (!String.IsNullOrEmpty(eva.SteamGuard))
                        logOnDetails.AuthCode = eva.SteamGuard;
                    else
                        logOnDetails.AuthCode = Console.ReadLine();
                }
                else if (callback.Result == EResult.InvalidLoginAuthCode)
                {
                    Log.Interface("The given SteamGuard code was invalid. Try again using the `auth' command.");
                    logOnDetails.AuthCode = Console.ReadLine();
                }
            });

            msg.Handle<SteamUser.LoginKeyCallback> (callback =>
            {
                myUniqueId = callback.UniqueID.ToString();

                UserWebLogOn();

                if (Trade.CurrentSchema == null)
                {
                    Log.Info ("Downloading Schema...");
                    Trade.CurrentSchema = Schema.FetchSchema (ApiKey, schemaLang);
                    Log.Success ("Schema Downloaded!");
                }

               // SteamFriends.SetPersonaName (DisplayNamePrefix+DisplayName);
                    SteamFriends.SetPersonaState (EPersonaState.Online);

                Log.Success ("Steam Bot Logged In Completely!");

                GetUserHandler(SteamClient.SteamID).OnLoginCompleted();
            });

            msg.Handle<SteamUser.WebAPIUserNonceCallback>(webCallback =>
            {
                Log.Debug("Received new WebAPIUserNonce.");

                if (webCallback.Result == EResult.OK)
                {
                    myUserNonce = webCallback.Nonce;
                    UserWebLogOn();
                }
                else
                {
                    Log.Error("WebAPIUserNonce Error: " + webCallback.Result);
                }
            });

            msg.Handle<SteamUser.UpdateMachineAuthCallback>(
                authCallback => OnUpdateMachineAuthCallback(authCallback)
            );
            #endregion

            #region Friends
            msg.Handle<SteamFriends.FriendsListCallback>(callback =>
            {
                foreach (SteamFriends.FriendsListCallback.Friend friend in callback.FriendList)
                {
                    switch (friend.SteamID.AccountType)
                    {
                        case EAccountType.Clan:
                            if (friend.Relationship == EFriendRelationship.RequestRecipient)
                            {
                                if (GetUserHandler(friend.SteamID).OnGroupAdd())
                                {
                                    AcceptGroupInvite(friend.SteamID);
                                }
                                else
                                {
                                    DeclineGroupInvite(friend.SteamID);
                                }
                            }
                            break;
                        default:
                            CreateFriendsListIfNecessary();
                            if (friend.Relationship == EFriendRelationship.None)
                            {
                                friends.Remove(friend.SteamID);
                                GetUserHandler(friend.SteamID).OnFriendRemove();
                                RemoveUserHandler(friend.SteamID);
                            }
                            else if (friend.Relationship == EFriendRelationship.RequestRecipient)
                    {
                                if (GetUserHandler(friend.SteamID).OnFriendAdd())
                                {
                        if (!friends.Contains(friend.SteamID))
                        {
                            friends.Add(friend.SteamID);
                                    }
                                    else
                            {
                                        Log.Error("Friend was added who was already in friends list: " + friend.SteamID);
                                    }
                                SteamFriends.AddFriend(friend.SteamID);
                            }
                        else
                        {
                                    SteamFriends.RemoveFriend(friend.SteamID);
                                RemoveUserHandler(friend.SteamID);
                            }
                        }
                            break;
                    }
                }
            });

            msg.Handle<SteamFriends.FriendMsgCallback> (callback =>
            {
                EChatEntryType type = callback.EntryType;

                if (callback.EntryType == EChatEntryType.ChatMsg)
                {
                    Log.Info ("Chat Message from {0}: {1}",
                                         SteamFriends.GetFriendPersonaName (callback.Sender),
                            callback.Message

                                         );
                    GetUserHandler(callback.Sender).OnMessageHandler(callback.Message, type);
                }
            });
            #endregion

            msg.Handle<SteamFriends.ChatMemberInfoCallback>(callback =>
                {
                    if (callback.StateChangeInfo.MemberInfo != null)
                    {
                    string User = callback.StateChangeInfo.MemberInfo.SteamID.ToString();

                    //Retrieves the database of users
                    string filedata = System.IO.File.ReadAllText(@"users.json");
                        //Converts the JSON file to a dictionary

                        //Log.Interface(existing);
                    EClanPermission Status = callback.StateChangeInfo.MemberInfo.Details; //Their officer status
                    ChatMember ChatMember = new ChatMember();
                    //Sets up dictionary for the JSON file

                    //Adds a new entry to the dictionary, or overwrites an existing
                        if (GroupChatHandler.UserDatabase.ContainsKey(User)) //if it already exists, it deletes it so it can update the data
                            {
                                  GroupChatHandler.UserDatabase.Remove(User);
                            }
                    GroupChatHandler.UserDatabase.Add(User,Status);
                    //Resaves it into a usable JSON format to later save

                    //Shows on the screen
                    //log.Interface(json);
                    //log.Interface("ADDED: " + User + callback.StateChangeInfo.MemberInfo.Details.ToString());
                    //Saves the file
                    System.IO.File.WriteAllText(@"users.json", JsonConvert.SerializeObject(GroupChatHandler.UserDatabase));
                    }
                });

            #region Group Chat
            msg.Handle<SteamFriends.ChatMsgCallback>(callback =>
            {
                GetUserHandler(callback.ChatterID).OnChatRoomMessage(callback.ChatRoomID, callback.ChatterID, callback.Message);
            });
            #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 =>
            {
                if (CheckCookies() == false)
                {
                    SteamTrade.RespondToTrade(callback.TradeID, false);
                    return;
                }

                try
                {
                    tradeManager.InitializeTrade(SteamUser.SteamID, callback.OtherClient);
                }
                catch (WebException we)
                {
                    SteamFriends.SendChatMessage(callback.OtherClient,
                             EChatEntryType.ChatMsg,
                             "Trade error: " + we.Message);

                    SteamTrade.RespondToTrade(callback.TradeID, false);
                    return;
                }
                catch (Exception)
                {
                    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 =>
            {
                if (callback.Response == EEconTradeResponse.Accepted)
                {
                    Log.Debug("Trade Status: {0}", callback.Response);
                    Log.Info ("Trade Accepted!");
                    GetUserHandler(callback.OtherClient).OnTradeRequestReply(true, callback.Response.ToString());
                }
                else
                {
                    Log.Warn("Trade failed: {0}", callback.Response);
                    CloseTrade ();
                    GetUserHandler(callback.OtherClient).OnTradeRequestReply(false, callback.Response.ToString());
                }

            });
            #endregion

            #region Disconnect
            msg.Handle<SteamUser.LoggedOffCallback> (callback =>
            {
                IsLoggedIn = false;
                Log.Warn("Logged off Steam.  Reason: {0}", callback.Result);
            });

            msg.Handle<SteamClient.DisconnectedCallback> (callback =>
            {
                log.Warn(IsLoggedIn.ToString());
                if(IsLoggedIn)
                {
                    IsLoggedIn = false;
                    CloseTrade();
                    Log.Warn("Disconnected from Steam Network!");

                }
                Log.Warn("Will attempt reconnect in 15 seconds");
                Thread.Sleep(15000); //added a delay to attempted reconnection every 15 seconds
                Log.Warn("Attempting Reconnect");
                SteamClient.Connect ();
                Log.Warn("Bot should be reconnecting");
            });
            #endregion

            #region Notifications
            msg.Handle<SteamBot.SteamNotifications.NotificationCallback>(callback =>
            {
                //currently only appears to be of trade offer
                if (callback.Notifications.Count != 0)
                {
                    foreach (var notification in callback.Notifications)
                    {
                        Log.Info(notification.UserNotificationType + " notification");
            }
                }

                // Get offers only if cookies are valid
                if (CheckCookies())
                    tradeOfferManager.GetOffers();
            });

            msg.Handle<SteamBot.SteamNotifications.CommentNotificationCallback>(callback =>
            {
                //various types of comment notifications on profile/activity feed etc
                //Log.Info("received CommentNotificationCallback");
                //Log.Info("New Commments " + callback.CommentNotifications.CountNewComments);
                //Log.Info("New Commments Owners " + callback.CommentNotifications.CountNewCommentsOwner);
                //Log.Info("New Commments Subscriptions" + callback.CommentNotifications.CountNewCommentsSubscriptions);
            });
            #endregion
        }
Ejemplo n.º 5
0
        void HandleSteamMessage(ICallbackMsg msg)
        {
            Log.Debug(msg.ToString());
            #region Login
            msg.Handle <SteamClient.ConnectedCallback> (callback =>
            {
                Log.Debug("Connection Callback: {0}", callback.Result);

                if (callback.Result == EResult.OK)
                {
                    UserLogOn();
                }
                else
                {
                    Log.Error("Failed to connect to Steam Community, trying again...");
                    SteamClient.Connect();
                }
            });

            msg.Handle <SteamUser.LoggedOnCallback> (callback =>
            {
                Log.Debug("Logged On Callback: {0}", callback.Result);

                if (callback.Result == EResult.OK)
                {
                    myUserNonce = callback.WebAPIUserNonce;
                }
                else
                {
                    Log.Error("Login Error: {0}", callback.Result);
                }

                if (callback.Result == EResult.AccountLogonDenied)
                {
                    Log.Interface("This account is SteamGuard enabled. Enter the code via the `auth' command.");

                    // try to get the steamguard auth code from the event callback
                    var eva = new SteamGuardRequiredEventArgs();
                    FireOnSteamGuardRequired(eva);
                    if (!String.IsNullOrEmpty(eva.SteamGuard))
                    {
                        logOnDetails.AuthCode = eva.SteamGuard;
                    }
                    else
                    {
                        logOnDetails.AuthCode = Console.ReadLine();
                    }
                }

                if (callback.Result == EResult.InvalidLoginAuthCode)
                {
                    Log.Interface("The given SteamGuard code was invalid. Try again using the `auth' command.");
                    logOnDetails.AuthCode = Console.ReadLine();
                }
            });

            msg.Handle <SteamUser.LoginKeyCallback> (callback =>
            {
                myUniqueId = callback.UniqueID.ToString();

                UserWebLogOn();

                if (Trade.CurrentSchema == null)
                {
                    Log.Info("Downloading Schema...");
                    Trade.CurrentSchema = Schema.FetchSchema(ApiKey, schemaLang);
                    Log.Success("Schema Downloaded!");
                }

                SteamFriends.SetPersonaName(DisplayNamePrefix + DisplayName);
                SteamFriends.SetPersonaState(EPersonaState.Online);

                Log.Success("Steam Bot Logged In Completely!");

                GetUserHandler(SteamClient.SteamID).OnLoginCompleted();
            });

            msg.Handle <SteamUser.WebAPIUserNonceCallback>(webCallback =>
            {
                Log.Debug("Received new WebAPIUserNonce.");

                if (webCallback.Result == EResult.OK)
                {
                    myUserNonce = webCallback.Nonce;
                    UserWebLogOn();
                }
                else
                {
                    Log.Error("WebAPIUserNonce Error: " + webCallback.Result);
                }
            });

            msg.Handle <SteamUser.UpdateMachineAuthCallback>(
                authCallback => OnUpdateMachineAuthCallback(authCallback)
                );
            #endregion

            #region Friends
            msg.Handle <SteamFriends.FriendsListCallback>(callback =>
            {
                foreach (SteamFriends.FriendsListCallback.Friend friend in callback.FriendList)
                {
                    switch (friend.SteamID.AccountType)
                    {
                    case EAccountType.Clan:
                        if (friend.Relationship == EFriendRelationship.RequestRecipient)
                        {
                            if (GetUserHandler(friend.SteamID).OnGroupAdd())
                            {
                                AcceptGroupInvite(friend.SteamID);
                            }
                            else
                            {
                                DeclineGroupInvite(friend.SteamID);
                            }
                        }
                        break;

                    default:
                        CreateFriendsListIfNecessary();
                        if (friend.Relationship == EFriendRelationship.None)
                        {
                            friends.Remove(friend.SteamID);
                            GetUserHandler(friend.SteamID).OnFriendRemove();
                            RemoveUserHandler(friend.SteamID);
                        }
                        else if (friend.Relationship == EFriendRelationship.RequestRecipient)
                        {
                            if (GetUserHandler(friend.SteamID).OnFriendAdd())
                            {
                                if (!friends.Contains(friend.SteamID))
                                {
                                    friends.Add(friend.SteamID);
                                }
                                else
                                {
                                    Log.Error("Friend was added who was already in friends list: " + friend.SteamID);
                                }
                                SteamFriends.AddFriend(friend.SteamID);
                            }
                            else
                            {
                                SteamFriends.RemoveFriend(friend.SteamID);
                                RemoveUserHandler(friend.SteamID);
                            }
                        }
                        break;
                    }
                }
            });


            msg.Handle <SteamFriends.FriendMsgCallback> (callback =>
            {
                EChatEntryType type = callback.EntryType;

                if (callback.EntryType == EChatEntryType.ChatMsg)
                {
                    Log.Info("Chat Message from {0}: {1}",
                             SteamFriends.GetFriendPersonaName(callback.Sender),
                             callback.Message
                             );
                    GetUserHandler(callback.Sender).OnMessageHandler(callback.Message, type);
                }
            });
            #endregion

            #region Group Chat
            msg.Handle <SteamFriends.ChatMsgCallback>(callback =>
            {
                GetUserHandler(callback.ChatterID).OnChatRoomMessage(callback.ChatRoomID, callback.ChatterID, callback.Message);
            });
            #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 =>
            {
                if (CheckCookies() == false)
                {
                    SteamTrade.RespondToTrade(callback.TradeID, false);
                    return;
                }

                try
                {
                    tradeManager.InitializeTrade(SteamUser.SteamID, callback.OtherClient);
                }
                catch (WebException we)
                {
                    SteamFriends.SendChatMessage(callback.OtherClient,
                                                 EChatEntryType.ChatMsg,
                                                 "Trade error: " + we.Message);

                    SteamTrade.RespondToTrade(callback.TradeID, false);
                    return;
                }
                catch (Exception)
                {
                    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 =>
            {
                if (callback.Response == EEconTradeResponse.Accepted)
                {
                    Log.Debug("Trade Status: {0}", callback.Response);
                    Log.Info("Trade Accepted!");
                    GetUserHandler(callback.OtherClient).OnTradeRequestReply(true, callback.Response.ToString());
                }
                else
                {
                    Log.Warn("Trade failed: {0}", callback.Response);
                    CloseTrade();
                    GetUserHandler(callback.OtherClient).OnTradeRequestReply(false, callback.Response.ToString());
                }
            });
            #endregion

            #region Disconnect
            msg.Handle <SteamUser.LoggedOffCallback> (callback =>
            {
                IsLoggedIn = false;
                Log.Warn("Logged off Steam.  Reason: {0}", callback.Result);
            });

            msg.Handle <SteamClient.DisconnectedCallback> (callback =>
            {
                if (IsLoggedIn)
                {
                    IsLoggedIn = false;
                    CloseTrade();
                    Log.Warn("Disconnected from Steam Network!");
                }

                SteamClient.Connect();
            });
            #endregion

            #region Notifications
            msg.Handle <SteamBot.SteamNotifications.NotificationCallback>(callback =>
            {
                //currently only appears to be of trade offer
                if (callback.Notifications.Count != 0)
                {
                    foreach (var notification in callback.Notifications)
                    {
                        Log.Info(notification.UserNotificationType + " notification");
                    }
                }

                // Get offers only if cookies are valid
                if (CheckCookies())
                {
                    tradeOfferManager.GetOffers();
                }
            });

            msg.Handle <SteamBot.SteamNotifications.CommentNotificationCallback>(callback =>
            {
                //various types of comment notifications on profile/activity feed etc
                //Log.Info("received CommentNotificationCallback");
                //Log.Info("New Commments " + callback.CommentNotifications.CountNewComments);
                //Log.Info("New Commments Owners " + callback.CommentNotifications.CountNewCommentsOwner);
                //Log.Info("New Commments Subscriptions" + callback.CommentNotifications.CountNewCommentsSubscriptions);
            });
            #endregion
        }
Ejemplo n.º 6
0
 void Handle(ICallbackMsg call)
 {
     registeredCallbacks
     .FindAll(callback => callback.CallbackType.IsAssignableFrom(call.GetType())) // find handlers interested in this callback
     .ForEach(callback => callback.Run(call));                                    // run them
 }
Ejemplo n.º 7
0
        void HandleSteamMessage(ICallbackMsg msg)
        {
            Log.Debug(msg.ToString());
            #region Login
            msg.Handle <SteamClient.ConnectedCallback>(callback =>
            {
                Log.Debug("Connection Callback: {0}", callback.Result);

                if (callback.Result == EResult.OK)
                {
                    UserLogOn();
                }
                else
                {
                    Log.Error("Failed to connect to Steam Community, trying again...");
                    SteamClient.Connect();
                }
            });

            msg.Handle <SteamUser.LoggedOnCallback>(callback =>
            {
                Log.Debug("Logged On Callback: {0}", callback.Result);

                if (callback.Result == EResult.OK)
                {
                    myUserNonce = callback.WebAPIUserNonce;
                }
                else
                {
                    Log.Error("Login Error: {0}", callback.Result);
                }

                if (callback.Result == EResult.AccountLogonDeniedNeedTwoFactorCode)
                {
                    var mobileAuthCode = GetMobileAuthCode();
                    if (string.IsNullOrEmpty(mobileAuthCode))
                    {
                        Log.Error("Failed to generate 2FA code. Make sure you have linked the authenticator via SteamBot.");
                    }
                    else
                    {
                        logOnDetails.TwoFactorCode = mobileAuthCode;
                        Log.Success("Generated 2FA code.");
                    }
                }
                else if (callback.Result == EResult.TwoFactorCodeMismatch)
                {
                    SteamAuth.TimeAligner.AlignTime();
                    logOnDetails.TwoFactorCode = SteamGuardAccount.GenerateSteamGuardCode();
                    Log.Success("Regenerated 2FA code.");
                }
                else if (callback.Result == EResult.AccountLogonDenied)
                {
                    Log.Interface("This account is SteamGuard enabled. Enter the code via the `auth' command.");

                    // try to get the steamguard auth code from the event callback
                    var eva = new SteamGuardRequiredEventArgs();
                    FireOnSteamGuardRequired(eva);
                    if (!String.IsNullOrEmpty(eva.SteamGuard))
                    {
                        logOnDetails.AuthCode = eva.SteamGuard;
                    }
                    else
                    {
                        logOnDetails.AuthCode = Console.ReadLine();
                    }
                }
                else if (callback.Result == EResult.InvalidLoginAuthCode)
                {
                    Log.Interface("The given SteamGuard code was invalid. Try again using the `auth' command.");
                    logOnDetails.AuthCode = Console.ReadLine();
                }
            });

            msg.Handle <SteamUser.LoginKeyCallback>(callback =>
            {
                myUniqueId = callback.UniqueID.ToString();

                UserWebLogOn();

                SteamFriends.SetPersonaName(DisplayNamePrefix + DisplayName);
                SteamFriends.SetPersonaState(EPersonaState.Online);

                Log.Success(string.Format("Steam Bot Logged In Completely ({0} Friends)!", SteamFriends.GetFriendCount()));

                GetUserHandler(SteamClient.SteamID).OnLoginCompleted();
            });

            msg.Handle <SteamUser.WebAPIUserNonceCallback>(webCallback =>
            {
                Log.Debug("Received new WebAPIUserNonce.");

                if (webCallback.Result == EResult.OK)
                {
                    myUserNonce = webCallback.Nonce;
                    UserWebLogOn();
                }
                else
                {
                    Log.Error("WebAPIUserNonce Error: " + webCallback.Result);
                }
            });

            msg.Handle <SteamUser.UpdateMachineAuthCallback>(
                authCallback => OnUpdateMachineAuthCallback(authCallback)
                );
            #endregion

            #region Friends
            msg.Handle <SteamFriends.FriendsListCallback>(callback =>
            {
                foreach (SteamFriends.FriendsListCallback.Friend friend in callback.FriendList)
                {
                    switch (friend.SteamID.AccountType)
                    {
                    case EAccountType.Clan:
                        if (friend.Relationship == EFriendRelationship.RequestRecipient)
                        {
                            //if (GetUserHandler(friend.SteamID).OnGroupAdd())
                            //{
                            AcceptGroupInvite(friend.SteamID);
                            //}
                            //else
                            //{
                            //    DeclineGroupInvite(friend.SteamID);
                            //}
                        }
                        break;

                    default:
                        CreateFriendsListIfNecessary();
                        if (friend.Relationship == EFriendRelationship.None)
                        {
                            friends.Remove(friend.SteamID);
                            GetUserHandler(friend.SteamID).OnFriendRemove();
                            RemoveUserHandler(friend.SteamID);
                        }
                        else if (friend.Relationship == EFriendRelationship.RequestRecipient)
                        {
                            if (GetUserHandler(friend.SteamID).OnFriendAdd())
                            {
                                if (!friends.Contains(friend.SteamID))
                                {
                                    friends.Add(friend.SteamID);
                                }
                                else
                                {
                                    Log.Error("Friend was added who was already in friends list: " + friend.SteamID);
                                }
                                AddFriend(friend.SteamID);
                                GetUserHandler(friend.SteamID).OnFriendAdded();
                            }
                            else
                            {
                                SteamFriends.RemoveFriend(friend.SteamID);
                                RemoveUserHandler(friend.SteamID);
                            }
                        }
                        break;
                    }
                }
            });


            msg.Handle <SteamFriends.FriendMsgCallback>(callback =>
            {
                EChatEntryType type = callback.EntryType;

                if (callback.EntryType == EChatEntryType.ChatMsg)
                {
                    Log.Info("Chat Message from {0}: {1}",
                             SteamFriends.GetFriendPersonaName(callback.Sender),
                             callback.Message
                             );
                    GetUserHandler(callback.Sender).OnMessageHandler(callback.Message, type);
                }
            });
            #endregion

            #region Group Chat
            msg.Handle <SteamFriends.ChatMsgCallback>(callback =>
            {
                GetUserHandler(callback.ChatterID).OnChatRoomMessage(callback.ChatRoomID, callback.ChatterID, callback.Message);
            });
            #endregion

            #region Disconnect
            msg.Handle <SteamUser.LoggedOffCallback>(callback =>
            {
                IsLoggedIn = false;
                Log.Warn("Logged off Steam.  Reason: {0}", callback.Result);
            });

            msg.Handle <SteamClient.DisconnectedCallback>(callback =>
            {
                if (IsLoggedIn)
                {
                    IsLoggedIn = false;
                    Log.Warn("Disconnected from Steam Network!");
                }

                SteamClient.Connect();
            });
            #endregion

            #region Notifications
            msg.Handle <SteamBot.SteamNotifications.NotificationCallback>(callback =>
            {
                if (callback.Notifications.Count != 0)
                {
                    foreach (var notification in callback.Notifications)
                    {
                        Log.Info(notification.UserNotificationType + " notification");
                    }
                }
            });

            msg.Handle <SteamBot.SteamNotifications.CommentNotificationCallback>(callback =>
            {
                //various types of comment notifications on profile/activity feed etc
                //Log.Info("received CommentNotificationCallback");
                //Log.Info("New Commments " + callback.CommentNotifications.CountNewComments);
                //Log.Info("New Commments Owners " + callback.CommentNotifications.CountNewCommentsOwner);
                //Log.Info("New Commments Subscriptions" + callback.CommentNotifications.CountNewCommentsSubscriptions);
            });
            #endregion
        }
Ejemplo n.º 8
0
        void HandleSteamMessage(ICallbackMsg msg)
        {
            Log.Debug(msg.ToString());
            #region Login
            msg.Handle <SteamClient.ConnectedCallback> (callback =>
            {
                Log.Debug("Connection Callback: {0}", callback.Result);

                if (callback.Result == EResult.OK)
                {
                    UserLogOn();
                }
                else
                {
                    Log.Error("Failed to connect to Steam Community, trying again...");
                    SteamClient.Connect();
                }
            });

            msg.Handle <SteamUser.LoggedOnCallback> (callback =>
            {
                Log.Debug("Logged On Callback: {0}", callback.Result);

                if (callback.Result == EResult.OK)
                {
                    myUserNonce = callback.WebAPIUserNonce;
                }
                else
                {
                    Log.Error("Login Error: {0}", callback.Result);
                }

                if (callback.Result == EResult.AccountLogonDenied)
                {
                    Log.Interface("This account is SteamGuard enabled. Enter the code via the `auth' command.");

                    // try to get the steamguard auth code from the event callback
                    var eva = new SteamGuardRequiredEventArgs();
                    FireOnSteamGuardRequired(eva);
                    if (!String.IsNullOrEmpty(eva.SteamGuard))
                    {
                        logOnDetails.AuthCode = eva.SteamGuard;
                    }
                    else
                    {
                        logOnDetails.AuthCode = Console.ReadLine();
                    }
                }

                if (callback.Result == EResult.InvalidLoginAuthCode)
                {
                    Log.Interface("The given SteamGuard code was invalid. Try again using the `auth' command.");
                    logOnDetails.AuthCode = Console.ReadLine();
                }
            });

            msg.Handle <SteamUser.LoginKeyCallback> (callback =>
            {
                myUniqueId = callback.UniqueID.ToString();

                UserWebLogOn();

                SteamFriends.SetPersonaName(DisplayNamePrefix + DisplayName);
                SteamFriends.SetPersonaState(EPersonaState.Online);

                Log.Success("Steam Bot Logged In Completely!");

                GetUserHandler(SteamClient.SteamID).OnLoginCompleted();
            });

            msg.Handle <SteamUser.WebAPIUserNonceCallback>(webCallback =>
            {
                Log.Debug("Received new WebAPIUserNonce.");

                if (webCallback.Result == EResult.OK)
                {
                    myUserNonce = webCallback.Nonce;
                    UserWebLogOn();
                }
                else
                {
                    Log.Error("WebAPIUserNonce Error: " + webCallback.Result);
                }
            });

            msg.Handle <SteamUser.UpdateMachineAuthCallback>(
                authCallback => OnUpdateMachineAuthCallback(authCallback)
                );
            #endregion

            #region Friends
            msg.Handle <SteamFriends.FriendsListCallback>(callback =>
            {
                foreach (SteamFriends.FriendsListCallback.Friend friend in callback.FriendList)
                {
                    switch (friend.SteamID.AccountType)
                    {
                    case EAccountType.Clan:
                        if (friend.Relationship == EFriendRelationship.RequestRecipient)
                        {
                            if (GetUserHandler(friend.SteamID).OnGroupAdd())
                            {
                                AcceptGroupInvite(friend.SteamID);
                            }
                            else
                            {
                                DeclineGroupInvite(friend.SteamID);
                            }
                        }
                        break;

                    default:
                        CreateFriendsListIfNecessary();
                        if (friend.Relationship == EFriendRelationship.None)
                        {
                            friends.Remove(friend.SteamID);
                            GetUserHandler(friend.SteamID).OnFriendRemove();
                            RemoveUserHandler(friend.SteamID);
                        }
                        else if (friend.Relationship == EFriendRelationship.RequestRecipient)
                        {
                            if (GetUserHandler(friend.SteamID).OnFriendAdd())
                            {
                                if (!friends.Contains(friend.SteamID))
                                {
                                    friends.Add(friend.SteamID);
                                }
                                else
                                {
                                    Log.Error("Friend was added who was already in friends list: " + friend.SteamID);
                                }
                                SteamFriends.AddFriend(friend.SteamID);
                            }
                            else
                            {
                                SteamFriends.RemoveFriend(friend.SteamID);
                                RemoveUserHandler(friend.SteamID);
                            }
                        }
                        break;
                    }
                }
            });


            msg.Handle <SteamFriends.FriendMsgCallback> (callback =>
            {
                EChatEntryType type = callback.EntryType;

                if (callback.EntryType == EChatEntryType.ChatMsg)
                {
                    Log.Info("Chat Message from {0}: {1}",
                             SteamFriends.GetFriendPersonaName(callback.Sender),
                             callback.Message
                             );
                    GetUserHandler(callback.Sender).OnMessageHandler(callback.Message, type);
                }
            });
            #endregion

            #region Group Chat
            msg.Handle <SteamFriends.ChatMsgCallback>(callback =>
            {
                GetUserHandler(callback.ChatterID).OnChatRoomMessage(callback.ChatRoomID, callback.ChatterID, callback.Message);
            });
            #endregion

            #region Disconnect
            msg.Handle <SteamUser.LoggedOffCallback> (callback =>
            {
                IsLoggedIn = false;
                Log.Warn("Logged off Steam.  Reason: {0}", callback.Result);
            });

            msg.Handle <SteamClient.DisconnectedCallback> (callback =>
            {
                if (IsLoggedIn)
                {
                    IsLoggedIn = false;
                    Log.Warn("Disconnected from Steam Network!");
                }

                SteamClient.Connect();
            });
            #endregion
        }