2023-02-16 21:16:06 +00:00
|
|
|
|
using SharpChat.Commands;
|
|
|
|
|
using SharpChat.Config;
|
2023-07-23 21:31:13 +00:00
|
|
|
|
using SharpChat.Events;
|
2023-02-23 21:46:49 +00:00
|
|
|
|
using SharpChat.EventStorage;
|
2023-02-16 21:16:06 +00:00
|
|
|
|
using SharpChat.Packet;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2023-02-23 21:46:49 +00:00
|
|
|
|
namespace SharpChat.PacketHandlers
|
|
|
|
|
{
|
2023-02-16 21:16:06 +00:00
|
|
|
|
public class SendMessageHandler : IChatPacketHandler {
|
|
|
|
|
private readonly CachedValue<int> MaxMessageLength;
|
|
|
|
|
|
|
|
|
|
private List<IChatCommand> Commands { get; } = new();
|
|
|
|
|
|
|
|
|
|
public SendMessageHandler(CachedValue<int> maxMsgLength) {
|
2024-05-19 01:53:14 +00:00
|
|
|
|
MaxMessageLength = maxMsgLength;
|
2023-02-16 21:16:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddCommand(IChatCommand command) {
|
2024-05-19 01:53:14 +00:00
|
|
|
|
Commands.Add(command);
|
2023-02-16 21:16:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddCommands(IEnumerable<IChatCommand> commands) {
|
2024-05-19 01:53:14 +00:00
|
|
|
|
Commands.AddRange(commands);
|
2023-02-16 21:16:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsMatch(ChatPacketHandlerContext ctx) {
|
|
|
|
|
return ctx.CheckPacketId("2");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Handle(ChatPacketHandlerContext ctx) {
|
|
|
|
|
string[] args = ctx.SplitText(3);
|
|
|
|
|
|
2024-05-10 19:18:55 +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
|
2024-05-10 19:18:55 +00:00
|
|
|
|
string? messageText = args.ElementAtOrDefault(2);
|
2023-02-16 21:16:06 +00:00
|
|
|
|
|
2024-05-17 23:50:22 +00:00
|
|
|
|
if(user == null || !user.Permissions.HasFlag(ChatUserPermissions.SendMessage) || string.IsNullOrWhiteSpace(messageText))
|
2023-02-16 21:16:06 +00:00
|
|
|
|
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;
|
|
|
|
|
|
2023-02-19 22:47:53 +00:00
|
|
|
|
ctx.Chat.ContextAccess.Wait();
|
|
|
|
|
try {
|
2024-05-10 19:18:55 +00:00
|
|
|
|
if(!ctx.Chat.UserLastChannel.TryGetValue(user.UserId, out ChatChannel? channel)
|
|
|
|
|
|| !ctx.Chat.IsInChannel(user, channel))
|
2023-02-19 22:47:53 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2023-02-22 00:28:53 +00:00
|
|
|
|
if(user.Status != ChatUserStatus.Online)
|
|
|
|
|
ctx.Chat.UpdateUser(user, status: ChatUserStatus.Online);
|
2023-02-16 21:16:06 +00:00
|
|
|
|
|
2023-02-19 22:47:53 +00:00
|
|
|
|
int maxMsgLength = MaxMessageLength;
|
|
|
|
|
if(messageText.Length > maxMsgLength)
|
|
|
|
|
messageText = messageText[..maxMsgLength];
|
2023-02-16 21:16:06 +00:00
|
|
|
|
|
2023-02-19 22:47:53 +00:00
|
|
|
|
messageText = messageText.Trim();
|
2023-02-16 21:16:06 +00:00
|
|
|
|
|
|
|
|
|
#if DEBUG
|
2023-02-22 00:28:53 +00:00
|
|
|
|
Logger.Write($"<{ctx.Connection.Id} {user.UserName}> {messageText}");
|
2023-02-16 21:16:06 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2023-02-19 22:47:53 +00:00
|
|
|
|
if(messageText.StartsWith("/")) {
|
|
|
|
|
ChatCommandContext context = new(messageText, ctx.Chat, user, ctx.Connection, channel);
|
2023-02-16 21:16:06 +00:00
|
|
|
|
|
2024-05-10 19:18:55 +00:00
|
|
|
|
IChatCommand? command = null;
|
2023-02-16 21:16:06 +00:00
|
|
|
|
|
2023-02-19 22:47:53 +00:00
|
|
|
|
foreach(IChatCommand cmd in Commands)
|
|
|
|
|
if(cmd.IsMatch(context)) {
|
|
|
|
|
command = cmd;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-02-16 21:16:06 +00:00
|
|
|
|
|
2023-02-19 22:47:53 +00:00
|
|
|
|
if(command != null) {
|
2023-07-23 21:31:13 +00:00
|
|
|
|
command.Dispatch(context);
|
|
|
|
|
return;
|
2023-02-16 21:16:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-23 21:31:13 +00:00
|
|
|
|
ctx.Chat.DispatchEvent(new MessageCreateEvent(
|
|
|
|
|
SharpId.Next(),
|
|
|
|
|
channel,
|
|
|
|
|
user,
|
2023-02-23 21:46:49 +00:00
|
|
|
|
DateTimeOffset.Now,
|
2023-07-23 21:31:13 +00:00
|
|
|
|
messageText,
|
|
|
|
|
false, false, false
|
2023-02-23 21:46:49 +00:00
|
|
|
|
));
|
2023-02-19 22:47:53 +00:00
|
|
|
|
} finally {
|
|
|
|
|
ctx.Chat.ContextAccess.Release();
|
|
|
|
|
}
|
2023-02-16 21:16:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|