sharp-chat/SharpChat/PacketHandlers/SendMessageHandler.cs

96 lines
3.1 KiB
C#
Raw Normal View History

2024-05-19 21:02:17 +00:00
using SharpChat.Config;
using SharpChat.Events;
2023-02-16 21:16:06 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
2024-05-19 21:02:17 +00:00
namespace SharpChat.PacketHandlers {
public class SendMessageHandler : IPacketHandler {
2023-02-16 21:16:06 +00:00
private readonly CachedValue<int> MaxMessageLength;
private List<IUserCommand> Commands { get; } = new();
2023-02-16 21:16:06 +00:00
public SendMessageHandler(CachedValue<int> maxMsgLength) {
MaxMessageLength = maxMsgLength;
2023-02-16 21:16:06 +00:00
}
public void AddCommand(IUserCommand command) {
Commands.Add(command);
2023-02-16 21:16:06 +00:00
}
public void AddCommands(IEnumerable<IUserCommand> commands) {
Commands.AddRange(commands);
2023-02-16 21:16:06 +00:00
}
public bool IsMatch(PacketHandlerContext ctx) {
2023-02-16 21:16:06 +00:00
return ctx.CheckPacketId("2");
}
public void Handle(PacketHandlerContext ctx) {
2023-02-16 21:16:06 +00:00
string[] args = ctx.SplitText(3);
UserInfo? 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
if(user == null || !user.Permissions.HasFlag(UserPermissions.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;
ctx.Chat.ContextAccess.Wait();
try {
2024-05-19 21:02:17 +00:00
ChannelInfo? channelInfo = ctx.Chat.Channels.Get(
ctx.Chat.ChannelsUsers.GetUserLastChannel(user)
);
if(channelInfo == null)
return;
if(user.Status != UserStatus.Online)
ctx.Chat.UpdateUser(user, status: UserStatus.Online);
2023-02-16 21:16:06 +00:00
int maxMsgLength = MaxMessageLength;
if(messageText.Length > maxMsgLength)
messageText = messageText[..maxMsgLength];
2023-02-16 21:16:06 +00:00
messageText = messageText.Trim();
2023-02-16 21:16:06 +00:00
#if DEBUG
Logger.Write($"<{ctx.Connection.Id} {user.UserName}> {messageText}");
2023-02-16 21:16:06 +00:00
#endif
if(messageText.StartsWith("/")) {
2024-05-19 21:02:17 +00:00
UserCommandContext context = new(messageText, ctx.Chat, user, ctx.Connection, channelInfo);
2023-02-16 21:16:06 +00:00
IUserCommand? command = null;
2023-02-16 21:16:06 +00:00
foreach(IUserCommand cmd in Commands)
if(cmd.IsMatch(context)) {
command = cmd;
break;
}
2023-02-16 21:16:06 +00:00
if(command != null) {
command.Dispatch(context);
return;
2023-02-16 21:16:06 +00:00
}
}
ctx.Chat.DispatchEvent(new MessageCreateEvent(
SharpId.Next(),
2024-05-19 21:02:17 +00:00
channelInfo,
user,
DateTimeOffset.Now,
messageText,
false, false, false
));
} finally {
ctx.Chat.ContextAccess.Release();
}
2023-02-16 21:16:06 +00:00
}
}
}