// 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);
            }
        }
 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);
                     }
                 }
             }
         }
     }
 }
        // 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);
        }