// helper function to send a lobby chat message
        private static void SendToLobbyHelper(LobbyChatMessage lmsg, int connection_id, bool authOnly, bool unblockedOnly)
        {
            bool doSend = true;

            if (authOnly || unblockedOnly)
            {
                MPBanEntry entry = MPChatCommand.FindPlayerEntryForConnection(connection_id, true);
                if (authOnly)
                {
                    doSend = doSend && MPChatCommand.CheckPermission(entry);
                }
                if (doSend && unblockedOnly)
                {
                    if (entry != null)
                    {
                        doSend = !MPBanPlayers.IsBanned(entry, MPBanMode.BlockChat);
                    }
                    else
                    {
                        doSend = false;
                    }
                }
            }
            if (doSend)
            {
                NetworkServer.SendToClient(connection_id, 75, lmsg);
            }
        }
        // Execute UNBAN or UNANNOY or UNBLOCKCHAT
        public bool DoUnban(MPBanMode banMode)
        {
            if (banMode == MPBanMode.Annoy)
            {
                // UNANNOY also implies UNBLOCKCHAT
                DoUnban(MPBanMode.BlockChat);
            }
            if (String.IsNullOrEmpty(arg))
            {
                MPBanPlayers.UnbanAll(banMode);
                ReturnTo(String.Format("ban list {0} cleared by {1}", banMode, senderEntry.name));
            }
            else
            {
                // check against names in ban list (may not be current player names)
                string pattern = arg.ToUpper();
                var    banList = MPBanPlayers.GetList(banMode);
                int    cnt     = banList.RemoveAll(entry => (MatchPlayerName(entry.name, pattern) != 0));
                if (cnt > 0)
                {
                    MPBanPlayers.OnUpdate(banMode, false);
                }
                else
                {
                    // check the currently connected players
                    if (SelectPlayer(arg))
                    {
                        cnt = MPBanPlayers.Unban(selectedPlayerEntry, banMode);
                    }
                }

                if (cnt > 0)
                {
                    ReturnTo(String.Format("{0} players UNBANNED from {1} list by {2}", cnt, banMode, senderEntry.name));
                }
                else
                {
                    ReturnToSender(String.Format("Un{0}: no player {1} found", banMode, arg));
                }
            }
            return(false);
        }
        // Execute STATUS command
        public bool DoStatus()
        {
            string creator;

            if (MPBanPlayers.MatchCreator != null && !String.IsNullOrEmpty(MPBanPlayers.MatchCreator.name))
            {
                creator = MPBanPlayers.MatchCreator.name;
            }
            else
            {
                creator = "<UNKNOWN>";
            }

            GetTrustedPlayerIds();
            ReturnToSender(String.Format("STATUS: {0}'s game, your auth: {1}", creator, CheckPermission(senderEntry)));
            ReturnToSender(String.Format("STATUS: bans: {0}, annoys: {1}, blocks: {2}, auth: {3} trust: {4}",
                                         MPBanPlayers.GetList(MPBanMode.Ban).Count,
                                         MPBanPlayers.GetList(MPBanMode.Annoy).Count,
                                         MPBanPlayers.GetList(MPBanMode.BlockChat).Count,
                                         authenticatedPlayers.Count,
                                         trustedPlayers.Count));
            return(false);
        }
        private static IEnumerator MatchStart(int connectionId)
        {
            var        newPlayer      = Server.FindPlayerByConnectionId(connectionId);
            MPBanEntry newPlayerEntry = new MPBanEntry(newPlayer);

            // prevent banned players from JIP into our match
            // there is already a delayed Disconnect going on, just
            // prevent this player from entering the JIP code
            if (MPBanPlayers.IsBanned(newPlayerEntry))
            {
                yield break;
            }

            float pregameWait = 3f;

            if (!newPlayer.m_mp_name.StartsWith("OBSERVER"))
            {
                foreach (Player player in Overload.NetworkManager.m_Players)
                {
                    if (MPTweaks.ClientHasTweak(player.connectionToClient.connectionId, "jip"))
                    {
                        NetworkServer.SendToClient(player.connectionToClient.connectionId, MessageTypes.MsgJIPJustJoined, new JIPJustJoinedMessage {
                            playerId = newPlayer.netId, ready = false
                        });
                    }
                }
                MPJoinInProgress.SetReady(newPlayer, false, true); // special case: do not disable the player completely, as this would prevent this player to be sent to new clients joining before we finally switch to ready
            }

            pregameWait = SendPreGame(connectionId, pregameWait);
            yield return(new WaitForSeconds(pregameWait));

            Server.SendLoadoutDataToClients();

            if (newPlayer.m_mp_name.StartsWith("OBSERVER"))
            {
                Debug.LogFormat("Enabling spectator for {0}", newPlayer.m_mp_name);
                newPlayer.Networkm_spectator = true;
                Debug.LogFormat("Enabled spectator for {0}", newPlayer.m_mp_name);

                yield return(null); // make sure spectator change is received before sending MatchStart
            }
            else
            {
                foreach (Player player in Overload.NetworkManager.m_Players)
                {
                    if (MPTweaks.ClientHasTweak(player.connectionToClient.connectionId, "jip"))
                    {
                        NetworkServer.SendToClient(player.connectionToClient.connectionId, MessageTypes.MsgJIPJustJoined, new JIPJustJoinedMessage {
                            playerId = newPlayer.netId, ready = true
                        });
                    }
                }
                MPJoinInProgress.SetReady(newPlayer, true);
            }

            if (NetworkMatch.GetMatchState() != MatchState.PLAYING)
            {
                yield break;
            }

            IntegerMessage modeMsg = new IntegerMessage((int)NetworkMatch.GetMode());

            NetworkServer.SendToClient(connectionId, CustomMsgType.MatchStart, modeMsg);
            SendMatchState(connectionId);

            NetworkSpawnPlayer.Respawn(newPlayer.c_player_ship);
            MPTweaks.Send(connectionId);
            //if (!newPlayer.m_spectator && RearView.MPNetworkMatchEnabled)
            //    newPlayer.CallTargetAddHUDMessage(newPlayer.connectionToClient, "REARVIEW ENABLED", -1, true);
            CTF.SendJoinUpdate(newPlayer);
            Race.SendJoinUpdate(newPlayer);
            foreach (Player player in Overload.NetworkManager.m_Players)
            {
                if (player.connectionToClient.connectionId == connectionId)
                {
                    continue;
                }
                // Resend mode for existing player to move h2h -> anarchy
                NetworkServer.SendToClient(player.connectionToClient.connectionId, CustomMsgType.MatchStart, modeMsg);

                if (!newPlayer.m_spectator)
                {
                    player.CallTargetAddHUDMessage(player.connectionToClient, String.Format(Loc.LS("{0} JOINED MATCH"), newPlayer.m_mp_name), -1, true);
                }

                //Debug.Log("JIP: spawning on new client net " + player.netId + " lobby " + player.connectionToClient.connectionId);
                NetworkServer.SendToClient(connectionId, CustomMsgType.Respawn, new RespawnMessage
                {
                    m_net_id     = player.netId,
                    lobby_id     = player.connectionToClient.connectionId,
                    m_pos        = player.transform.position,
                    m_rotation   = player.transform.rotation,
                    use_loadout1 = player.m_use_loadout1
                });
            }
            ServerStatLog.Connected(newPlayer.m_mp_name);
            MPBanPlayers.ApplyAllBans(); // make sure the newly connected player gets proper treatment if he is BANNED
        }
 private static void Postfix()
 {
     MPBanPlayers.ApplyAllBans();
 }
 private static void Postfix(ref bool __result, int connection_id, int version, string player_name, string player_session_id, string player_id, string player_uid)
 {
     MPBanPlayers.JoiningPlayerIsBanned     = false;
     MPBanPlayers.JoiningPlayerIsAnnoyed    = false;
     MPBanPlayers.JoiningPlayerConnectionId = connection_id;
     MPBanPlayers.JoiningPlayerLobbyData    = null;
     if (__result)
     {
         // the player has been accepted by the game's matchmaking
         MPBanEntry candidate = new MPBanEntry(player_name, connection_id, player_id);
         bool       isCreator = false;
         if (!MPBanPlayers.MatchCreatorIsInGame && MPBanPlayers.PlayerWithPrivateMatchData != null && !String.IsNullOrEmpty(MPBanPlayers.PlayerWithPrivateMatchData.id) && !String.IsNullOrEmpty(candidate.id))
         {
             if (candidate.id == MPBanPlayers.PlayerWithPrivateMatchData.id)
             {
                 Debug.LogFormat("MPBanPlayers: Match creator entered the lobby: {0}", player_name);
                 isCreator = true;
                 MPBanPlayers.MatchCreatorIsInGame = true;
             }
         }
         // check if player is banned
         MPBanPlayers.JoiningPlayerIsBanned  = MPBanPlayers.IsBanned(candidate);
         MPBanPlayers.JoiningPlayerIsAnnoyed = MPBanPlayers.IsBanned(candidate, MPBanMode.Annoy);
         if (isCreator && MPBanPlayers.JoiningPlayerIsAnnoyed)
         {
             // annoyed players are treated as Banned for creating new matches
             MPBanPlayers.JoiningPlayerIsBanned = true;
         }
         if (MPBanPlayers.JoiningPlayerIsBanned)
         {
             // banned player entered the lobby
             // NOTE: we cannot just say __accept = false, because this causes all sorts of troubles later
             MPBanPlayers.KickPlayer(connection_id, player_name, true);
             if (isCreator)
             {
                 Debug.LogFormat("Creator for this match {0} is BANNED, ending match", candidate.name);
                 GameManager.m_gm.StartCoroutine(DelayedEndMatch());
             }
         }
         else
         {
             // unbanned player entered the lobby
             if (isCreator)
             {
                 bool haveModifiedState = MPBanPlayers.HasModifiedState() || MPChatCommand.HasModifiedState();
                 bool doReset           = true;
                 if (MPChatCommand.CheckPermission(candidate))
                 {
                     Debug.Log("MPBanPlayers: same game creator as last match, or with permissions");
                     doReset = false;
                 }
                 MPBanPlayers.MatchCreator = candidate;
                 if (doReset)
                 {
                     Debug.Log("MPBanPlayers: new game creator, resetting bans and permissions");
                     MPBanPlayers.Reset();
                     MPChatCommand.Reset();
                     if (haveModifiedState)
                     {
                         MPChatTools.SendTo(true, "cleared all bans and permissions", connection_id);
                     }
                 }
                 else
                 {
                     if (haveModifiedState)
                     {
                         MPChatTools.SendTo(true, "keeping bans and permissions from previous match", connection_id);
                     }
                 }
             }
         }
     }
 }
        // Execute KICK or BAN or KICKBAN or ANNOY or BLOCKCHAT command
        public bool DoKickBan(bool doKick, bool doBan, MPBanMode banMode)
        {
            string op;
            string banOp = banMode.ToString().ToUpper();

            if (doKick && doBan)
            {
                op = "KICK" + banOp;
            }
            else if (doKick)
            {
                op = "KICK";
            }
            else if (doBan)
            {
                op = banOp;
            }
            else
            {
                return(false);
            }

            Debug.LogFormat("{0} request for {1}", op, arg);
            if (!SelectPlayer(arg))
            {
                Debug.LogFormat("{0}: no player {1} found", op, arg);
                ReturnToSender(String.Format("{0}: player {1} not found", op, arg));
                return(false);
            }

            if (IsTrustedPlayer(selectedPlayerEntry))
            {
                ReturnToSender(String.Format("{0}: not on this server, dude!", op));
                return(false);
            }

            if (selectedPlayerConnectionId >= 0 && sender_conn == selectedPlayerConnectionId)
            {
                Debug.LogFormat("{0}: won't self-apply", op, arg);
                ReturnToSender(String.Format("{0}: won't self-apply", op, arg));
                return(false);
            }

            if (doBan)
            {
                MPBanPlayers.Ban(selectedPlayerEntry, banMode);
                if (banMode == MPBanMode.Annoy)
                {
                    // ANNOY also implies BLOCKCHAT
                    MPBanPlayers.Ban(selectedPlayerEntry, MPBanMode.BlockChat);
                }
                ReturnTo(String.Format("{0} player {1} by {2}", banOp, selectedPlayerEntry.name, senderEntry.name), -1, selectedPlayerConnectionId);
            }
            if (doKick)
            {
                ReturnTo(String.Format("KICK player {0} by {1}", selectedPlayerEntry.name, senderEntry.name), -1, selectedPlayerConnectionId);
                if (selectedPlayer != null)
                {
                    MPBanPlayers.KickPlayer(selectedPlayer);
                }
                else if (selectedPlayerLobbyData != null)
                {
                    MPBanPlayers.KickPlayer(selectedPlayerLobbyData);
                }
                else
                {
                    MPBanPlayers.KickPlayer(selectedPlayerConnectionId, selectedPlayerEntry.name);
                }
            }
            return(false);
        }
        // Send a chat message
        // Set connection_id to the ID of a specific client, or to -1 to send to all
        // clients except except_connection_id (if that is >= 0)
        // If authOnly is true, only send to clients which are authenticated for chat
        // commands.
        // If unblockedOnly is true, only send to clients which are not BlockChat-BANNED
        // You need to already know if we are currently in Lobby or not
        public static bool SendTo(bool inLobby, string msg, int connection_id = -1, int except_connection_id = -1, bool authOnly = false, bool unblockedOnly = false, string sender_name = "Server", MpTeam team = MpTeam.TEAM0, bool isTeamMessage = false, int sender_connection_id = -1)
        {
            bool toAll = (connection_id < 0);

            if (!toAll)
            {
                if (connection_id >= NetworkServer.connections.Count || NetworkServer.connections[connection_id] == null)
                {
                    return(false);
                }
            }

            if (inLobby)
            {
                var lmsg = new LobbyChatMessage(sender_connection_id, sender_name, team, msg, isTeamMessage);
                if (toAll)
                {
                    if (except_connection_id < 0 && !authOnly && !unblockedOnly)
                    {
                        NetworkServer.SendToAll(75, lmsg);
                    }
                    else
                    {
                        foreach (NetworkConnection c in NetworkServer.connections)
                        {
                            if (c != null && c.connectionId != except_connection_id)
                            {
                                SendToLobbyHelper(lmsg, c.connectionId, authOnly, unblockedOnly);
                            }
                        }
                    }
                }
                else
                {
                    SendToLobbyHelper(lmsg, connection_id, authOnly, unblockedOnly);
                }
            }
            else
            {
                if (isTeamMessage)
                {
                    msg = Loc.LS("[TEAM]") + " " + msg;
                }
                foreach (var p in Overload.NetworkManager.m_Players)
                {
                    if (p != null && p.connectionToClient != null)
                    {
                        if ((toAll && p.connectionToClient.connectionId != except_connection_id) || (p.connectionToClient.connectionId == connection_id))
                        {
                            bool doSend = true;
                            if (authOnly || unblockedOnly)
                            {
                                MPBanEntry entry = new MPBanEntry(p);
                                if (authOnly)
                                {
                                    doSend = doSend && MPChatCommand.CheckPermission(entry);
                                }
                                if (doSend && unblockedOnly)
                                {
                                    doSend = !MPBanPlayers.IsBanned(entry, MPBanMode.BlockChat);
                                }
                            }
                            if (doSend)
                            {
                                p.CallTargetSendFullChat(p.connectionToClient, connection_id, sender_name, team, msg);
                            }
                        }
                    }
                }
            }
            return(true);
        }
        // Execute a command: Returns true if the caller should forward the chat message
        // to the clients, and false if not (when it was a special command for the server)
        public bool Execute()
        {
            if (!IsEnabled())
            {
                // chat commands are not enabled
                return(true);
            }

            if (cmd == Command.None)
            {
                // there might be BlockChat-Banned players, and we can just ignore their ramblings
                if (MPBanPlayers.GetList(MPBanMode.BlockChat).Count > 0)
                {
                    MPBanEntry we = FindPlayerEntryForConnection(sender_conn, inLobby);
                    if (we != null && MPBanPlayers.IsBanned(we, MPBanMode.BlockChat))
                    {
                        // send the message back to the sender ONLY
                        MPChatTools.SendTo(inLobby, sender_message, sender_conn, -1, false, false, sender_name, sender_team, isTeamMessage, sender_conn);
                        return(false);
                    }
                }
                return(true);
            }
            senderEntry = FindPlayerEntryForConnection(sender_conn, inLobby);
            if (senderEntry == null || String.IsNullOrEmpty(senderEntry.name))
            {
                Debug.LogFormat("CHATCMD {0}: {1} {2}: failed to identify sender", cmd, cmdName, arg);
                return(false);
            }
            Debug.LogFormat("CHATCMD {0}: {1} {2} by {3}", cmd, cmdName, arg, senderEntry.name);
            if (needAuth)
            {
                if (!CheckPermission(senderEntry))
                {
                    ReturnToSender(String.Format("You do not have the permission for command {0}!", cmd));
                    Debug.LogFormat("CHATCMD {0}: client is not authenticated!", cmd);
                    return(false);
                }
            }
            bool result = false;

            switch (cmd)
            {
            case Command.GivePerm:
                result = DoPerm(true);
                break;

            case Command.RevokePerm:
                result = DoPerm(false);
                break;

            case Command.Auth:
                result = DoAuth();
                break;

            case Command.Kick:
                result = DoKickBan(true, false, MPBanMode.Ban);
                break;

            case Command.Ban:
                result = DoKickBan(false, true, MPBanMode.Ban);
                break;

            case Command.Annoy:
                result = DoKickBan(false, true, MPBanMode.Annoy);
                break;

            case Command.KickBan:
                result = DoKickBan(true, true, MPBanMode.Ban);
                break;

            case Command.Unban:
                result = DoUnban(MPBanMode.Ban);
                break;

            case Command.Unannoy:
                result = DoUnban(MPBanMode.Annoy);
                break;

            case Command.BlockChat:
                result = DoKickBan(false, true, MPBanMode.BlockChat);
                break;

            case Command.UnblockChat:
                result = DoUnban(MPBanMode.BlockChat);
                break;

            case Command.End:
                result = DoEnd();
                break;

            case Command.Start:
                result = DoStart();
                break;

            case Command.Status:
                result = DoStatus();
                break;

            case Command.Say:
                result = DoSay();
                break;

            case Command.Test:
                result = DoTest();
                break;

            default:
                Debug.LogFormat("CHATCMD {0}: {1} {2} was not handled by server", cmd, cmdName, arg);
                result = true;     // treat it as normal chat message
                break;
            }
            return(result);
        }