Added character limit for chat (#1586)

* Added character limit for chat

* Changed buffer reading from Int16 to Int32

Co-authored-by: Clément <clement.orlandini@gmail.com>
This commit is contained in:
Clement-O
2020-08-17 14:45:02 +02:00
committed by GitHub
parent b051261485
commit 0e6f55a23d
4 changed files with 151 additions and 3 deletions

View File

@@ -5,7 +5,9 @@ using Content.Server.Interfaces;
using Content.Server.Interfaces.Chat;
using Content.Shared.Chat;
using Content.Shared.GameObjects.EntitySystems;
using NFluidsynth;
using Robust.Server.Console;
using Robust.Server.Interfaces.GameObjects;
using Robust.Server.Interfaces.Player;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Network;
@@ -19,8 +21,18 @@ namespace Content.Server.Chat
/// </summary>
internal sealed class ChatManager : IChatManager
{
/// <summary>
/// The maximum length a player-sent message can be sent
/// </summary>
public int MaxMessageLength = 1000;
private const int VoiceRange = 7; // how far voice goes in world units
/// <summary>
/// The message displayed to the player when it exceeds the chat character limit
/// </summary>
private const string MaxLengthExceededMessage = "Your message exceeded {0} character limit";
#pragma warning disable 649
[Dependency] private readonly IEntitySystemManager _entitySystemManager;
[Dependency] private readonly IServerNetManager _netManager;
@@ -33,6 +45,12 @@ namespace Content.Server.Chat
public void Initialize()
{
_netManager.RegisterNetMessage<MsgChatMessage>(MsgChatMessage.NAME);
_netManager.RegisterNetMessage<ChatMaxMsgLengthMessage>(ChatMaxMsgLengthMessage.NAME, _onMaxLengthRequest);
// Tell all the connected players the chat's character limit
var msg = _netManager.CreateNetMessage<ChatMaxMsgLengthMessage>();
msg.MaxMessageLength = MaxMessageLength;
_netManager.ServerSendToAll(msg);
}
public void DispatchServerAnnouncement(string message)
@@ -69,6 +87,17 @@ namespace Content.Server.Chat
return;
}
// Get entity's PlayerSession
IPlayerSession playerSession = source.GetComponent<IActorComponent>().playerSession;
// Check if message exceeds the character limit if the sender is a player
if (playerSession != null)
if (message.Length > MaxMessageLength)
{
DispatchServerMessage(playerSession, Loc.GetString(MaxLengthExceededMessage, MaxMessageLength));
return;
}
var pos = source.Transform.GridPosition;
var clients = _playerManager.GetPlayersInRange(pos, VoiceRange).Select(p => p.ConnectedClient);
@@ -90,6 +119,17 @@ namespace Content.Server.Chat
return;
}
// Check if entity is a player
IPlayerSession playerSession = source.GetComponent<IActorComponent>().playerSession;
// Check if message exceeds the character limit
if (playerSession != null)
if (action.Length > MaxMessageLength)
{
DispatchServerMessage(playerSession, Loc.GetString(MaxLengthExceededMessage, MaxMessageLength));
return;
}
var pos = source.Transform.GridPosition;
var clients = _playerManager.GetPlayersInRange(pos, VoiceRange).Select(p => p.ConnectedClient);
@@ -103,6 +143,13 @@ namespace Content.Server.Chat
public void SendOOC(IPlayerSession player, string message)
{
// Check if message exceeds the character limi
if (message.Length > MaxMessageLength)
{
DispatchServerMessage(player, Loc.GetString(MaxLengthExceededMessage, MaxMessageLength));
return;
}
var msg = _netManager.CreateNetMessage<MsgChatMessage>();
msg.Channel = ChatChannel.OOC;
msg.Message = message;
@@ -114,6 +161,13 @@ namespace Content.Server.Chat
public void SendDeadChat(IPlayerSession player, string message)
{
// Check if message exceeds the character limit
if (message.Length > MaxMessageLength)
{
DispatchServerMessage(player, Loc.GetString(MaxLengthExceededMessage, MaxMessageLength));
return;
}
var clients = _playerManager.GetPlayersBy(x => x.AttachedEntity != null && x.AttachedEntity.HasComponent<GhostComponent>()).Select(p => p.ConnectedClient);;
var msg = _netManager.CreateNetMessage<MsgChatMessage>();
@@ -126,7 +180,14 @@ namespace Content.Server.Chat
public void SendAdminChat(IPlayerSession player, string message)
{
if(!_conGroupController.CanCommand(player, "asay"))
// Check if message exceeds the character limit
if (message.Length > MaxMessageLength)
{
DispatchServerMessage(player, Loc.GetString(MaxLengthExceededMessage, MaxMessageLength));
return;
}
if (!_conGroupController.CanCommand(player, "asay"))
{
SendOOC(player, message);
return;
@@ -149,5 +210,12 @@ namespace Content.Server.Chat
msg.MessageWrap = $"OOC: (D){sender}: {{0}}";
_netManager.ServerSendToAll(msg);
}
private void _onMaxLengthRequest(ChatMaxMsgLengthMessage msg)
{
var response = _netManager.CreateNetMessage<ChatMaxMsgLengthMessage>();
response.MaxMessageLength = MaxMessageLength;
_netManager.ServerSendMessage(response, msg.MsgChannel);
}
}
}