private static void Prefix()
 {
     if (MPBanPlayers.JoiningPlayerConnectionId >= 0)
     {
         if (MPBanPlayers.JoiningPlayerIsAnnoyed)
         {
             // rename annoy-banned players
             if (MPBanPlayers.JoiningPlayerIsAnnoyed)
             {
                 foreach (KeyValuePair <int, PlayerLobbyData> p in NetworkMatch.m_players)
                 {
                     if (p.Key == MPBanPlayers.JoiningPlayerConnectionId)
                     {
                         if (p.Value != null)
                         {
                             MPChatTools.SendTo(String.Format("ANNOY-BANNED player {0} joined, renamed to {1}", p.Value.m_name, MPBanPlayers.bannedName), -1, MPBanPlayers.JoiningPlayerConnectionId, true);
                             p.Value.m_name = MPBanPlayers.bannedName;
                         }
                     }
                 }
             }
         }
         if (MPBanPlayers.JoiningPlayerIsBanned)
         {
             if (NetworkMatch.m_players.ContainsKey(MPBanPlayers.JoiningPlayerConnectionId))
             {
                 // Save the PlayerLobbyData that we remove here, we add it back after SendPlayersInLobbyToAllClients
                 MPBanPlayers.JoiningPlayerLobbyData = NetworkMatch.m_players[MPBanPlayers.JoiningPlayerConnectionId];
                 NetworkMatch.m_players.Remove(MPBanPlayers.JoiningPlayerConnectionId);
             }
         }
     }
 }
        // Execute SAY command
        public bool DoSay()
        {
            string msg = (String.IsNullOrEmpty(arg))?"":arg;

            MPChatTools.SendTo(inLobby, msg, -1, -1, false, true, sender_name, sender_team, isTeamMessage, sender_conn);
            return(false);
        }
 // Kicks a player by a connection id. Also works in the Lobby
 public static void KickPlayer(int connection_id, string name = "", bool banned = false)
 {
     if (connection_id < 0 || connection_id >= NetworkServer.connections.Count)
     {
         return;
     }
     if (NetworkServer.connections[connection_id] != null)
     {
         MPChatTools.SendTo(String.Format("KICKING {0}player {1}", ((banned)?"BANNED ":""), name), -1, connection_id, true);
         // Goodbye
         // disconnect it in short moment, to give client time to execute the commands
         GameManager.m_gm.StartCoroutine(DelayedDisconnect(connection_id, banned));
     }
 }
 // Annoys a player
 public static void AnnoyPlayer(Player p)
 {
     if (p != null)
     {
         if (p.m_spectator == false)
         {
             p.m_spectator = true;
             Debug.LogFormat("BAN ANNOY player {0}", p.m_mp_name);
             if (p.connectionToClient != null)
             {
                 MPChatTools.SendTo(String.Format("ANNOY-BANNING player {0}", p.m_mp_name), -1, p.connectionToClient.connectionId, true);
             }
         }
     }
 }
 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 to all clients except the given connection id, -1 for all
 public bool ReturnTo(string msg, int connection_id = -1, int except_connection_id = -1, bool authOnly = false)
 {
     return(MPChatTools.SendTo(inLobby, msg, connection_id, except_connection_id, authOnly, false, "Server", MpTeam.TEAM0, false, sender_conn));
 }
 // Send a chat message back to the sender of the command
 // HA: An Elvis reference!
 public bool ReturnToSender(string msg)
 {
     return(MPChatTools.SendTo(inLobby, msg, sender_conn, -1, false, false, "Server", MpTeam.TEAM0, false, sender_conn));
 }
        // 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);
        }