2023-02-23 21:46:49 +00:00
|
|
|
|
using SharpChat.EventStorage;
|
2024-05-20 23:40:34 +00:00
|
|
|
|
using SharpChat.SockChat.PacketsS2C;
|
2023-02-16 20:34:59 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2024-05-20 23:40:34 +00:00
|
|
|
|
namespace SharpChat.SockChat.Commands {
|
2024-05-20 23:00:47 +00:00
|
|
|
|
public class MessageDeleteCommand : ISockChatClientCommand {
|
|
|
|
|
public bool IsMatch(SockChatClientCommandContext ctx) {
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return ctx.NameEquals("delmsg") || (
|
|
|
|
|
ctx.NameEquals("delete")
|
|
|
|
|
&& ctx.Args.FirstOrDefault()?.All(char.IsDigit) == true
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 23:00:47 +00:00
|
|
|
|
public void Dispatch(SockChatClientCommandContext ctx) {
|
2024-05-19 02:17:51 +00:00
|
|
|
|
bool deleteAnyMessage = ctx.User.Permissions.HasFlag(UserPermissions.DeleteAnyMessage);
|
2023-02-16 20:34:59 +00:00
|
|
|
|
|
2024-05-19 02:17:51 +00:00
|
|
|
|
if(!deleteAnyMessage && !ctx.User.Permissions.HasFlag(UserPermissions.DeleteOwnMessage)) {
|
2024-05-20 23:00:47 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorS2CPacket(ctx.Name));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-10 19:18:55 +00:00
|
|
|
|
string? firstArg = ctx.Args.FirstOrDefault();
|
2023-02-16 20:34:59 +00:00
|
|
|
|
|
|
|
|
|
if(string.IsNullOrWhiteSpace(firstArg) || !firstArg.All(char.IsDigit) || !long.TryParse(firstArg, out long delSeqId)) {
|
2024-05-20 23:00:47 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new CommandFormatErrorS2CPacket());
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-10 19:18:55 +00:00
|
|
|
|
StoredEventInfo? delMsg = ctx.Chat.Events.GetEvent(delSeqId);
|
2023-02-16 20:34:59 +00:00
|
|
|
|
|
2024-05-10 19:18:55 +00:00
|
|
|
|
if(delMsg == null || delMsg.Sender?.Rank > ctx.User.Rank || (!deleteAnyMessage && delMsg.Sender?.UserId != ctx.User.UserId)) {
|
2024-05-20 23:00:47 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new MessageDeleteNotAllowedErrorS2CPacket());
|
2023-02-19 22:27:08 +00:00
|
|
|
|
return;
|
2023-02-16 20:34:59 +00:00
|
|
|
|
}
|
2023-02-19 22:27:08 +00:00
|
|
|
|
|
|
|
|
|
ctx.Chat.Events.RemoveEvent(delMsg);
|
2024-05-20 23:00:47 +00:00
|
|
|
|
ctx.Chat.Send(new MessageDeleteS2CPacket(delMsg.Id));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|