2023-02-16 21:16:06 +00:00
|
|
|
|
using SharpChat.Commands;
|
|
|
|
|
using SharpChat.Config;
|
|
|
|
|
using SharpChat.Events;
|
|
|
|
|
using SharpChat.Packet;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace SharpChat.PacketHandlers {
|
|
|
|
|
public class SendMessageHandler : IChatPacketHandler {
|
|
|
|
|
private readonly CachedValue<int> MaxMessageLength;
|
|
|
|
|
|
|
|
|
|
private List<IChatCommand> Commands { get; } = new();
|
|
|
|
|
|
|
|
|
|
public SendMessageHandler(CachedValue<int> maxMsgLength) {
|
|
|
|
|
MaxMessageLength = maxMsgLength ?? throw new ArgumentNullException(nameof(maxMsgLength));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddCommand(IChatCommand command) {
|
|
|
|
|
Commands.Add(command ?? throw new ArgumentNullException(nameof(command)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddCommands(IEnumerable<IChatCommand> commands) {
|
|
|
|
|
Commands.AddRange(commands ?? throw new ArgumentNullException(nameof(commands)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsMatch(ChatPacketHandlerContext ctx) {
|
|
|
|
|
return ctx.CheckPacketId("2");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Handle(ChatPacketHandlerContext ctx) {
|
|
|
|
|
string[] args = ctx.SplitText(3);
|
|
|
|
|
|
2023-02-16 21:25:41 +00:00
|
|
|
|
ChatUser user = ctx.Connection.User;
|
2023-02-16 21:16:06 +00:00
|
|
|
|
|
|
|
|
|
// No longer concats everything after index 1 with \t, no previous implementation did that either
|
|
|
|
|
string messageText = args.ElementAtOrDefault(2);
|
|
|
|
|
|
|
|
|
|
if(user == null || !user.Can(ChatUserPermissions.SendMessage) || string.IsNullOrWhiteSpace(messageText))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Extra validation step, not necessary at all but enforces proper formatting in SCv1.
|
|
|
|
|
if(!long.TryParse(args[1], out long mUserId) || user.UserId != mUserId)
|
|
|
|
|
return;
|
|
|
|
|
ChatChannel channel = user.CurrentChannel;
|
|
|
|
|
|
|
|
|
|
if(channel == null
|
2023-02-17 19:02:35 +00:00
|
|
|
|
|| !ctx.Chat.IsInChannel(user, channel)
|
2023-02-17 21:47:44 +00:00
|
|
|
|
/*|| (user.IsSilenced && !user.Can(ChatUserPermissions.SilenceUser))*/)
|
2023-02-16 21:16:06 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if(user.Status != ChatUserStatus.Online) {
|
|
|
|
|
user.Status = ChatUserStatus.Online;
|
2023-02-16 22:33:48 +00:00
|
|
|
|
ctx.Chat.SendTo(channel, new UserUpdatePacket(user));
|
2023-02-16 21:16:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int maxMsgLength = MaxMessageLength;
|
|
|
|
|
if(messageText.Length > maxMsgLength)
|
|
|
|
|
messageText = messageText[..maxMsgLength];
|
|
|
|
|
|
|
|
|
|
messageText = messageText.Trim();
|
|
|
|
|
|
|
|
|
|
#if DEBUG
|
2023-02-16 21:25:41 +00:00
|
|
|
|
Logger.Write($"<{ctx.Connection.Id} {user.Username}> {messageText}");
|
2023-02-16 21:16:06 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
IChatMessage message = null;
|
|
|
|
|
|
|
|
|
|
if(messageText.StartsWith("/")) {
|
2023-02-16 21:25:41 +00:00
|
|
|
|
ChatCommandContext context = new(messageText, ctx.Chat, user, ctx.Connection, channel);
|
2023-02-16 21:16:06 +00:00
|
|
|
|
|
|
|
|
|
IChatCommand command = null;
|
|
|
|
|
|
|
|
|
|
foreach(IChatCommand cmd in Commands)
|
|
|
|
|
if(cmd.IsMatch(context)) {
|
|
|
|
|
command = cmd;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(command != null) {
|
|
|
|
|
if(command is ActionCommand actionCommand)
|
|
|
|
|
message = actionCommand.ActionDispatch(context);
|
|
|
|
|
else {
|
|
|
|
|
command.Dispatch(context);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
message ??= new ChatMessage {
|
2023-02-16 22:47:30 +00:00
|
|
|
|
ChannelName = channel.Name,
|
2023-02-16 21:16:06 +00:00
|
|
|
|
DateTime = DateTimeOffset.UtcNow,
|
|
|
|
|
Sender = user,
|
|
|
|
|
Text = messageText,
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-19 22:27:08 +00:00
|
|
|
|
ctx.Chat.Events.AddEvent(message);
|
|
|
|
|
ctx.Chat.SendTo(channel, new ChatMessageAddPacket(message));
|
2023-02-16 21:16:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|