sharp-chat/SharpChat.SockChat/Commands/ChannelJoinCommand.cs
2024-05-29 20:51:41 +00:00

49 lines
2.3 KiB
C#

using SharpChat.SockChat.PacketsS2C;
using System;
using System.Linq;
namespace SharpChat.SockChat.Commands {
public class ChannelJoinCommand : ISockChatClientCommand {
public bool IsMatch(SockChatClientCommandContext ctx) {
return ctx.NameEquals("join");
}
public void Dispatch(SockChatClientCommandContext ctx) {
string channelName = ctx.Args.FirstOrDefault() ?? string.Empty;
string password = string.Join(' ', ctx.Args.Skip(1));
ChannelInfo? channelInfo = ctx.Chat.Channels.Get(channelName, SockChatUtility.SanitiseChannelName);
if(channelInfo == null) {
ctx.Chat.SendTo(ctx.User, new ChannelNotFoundErrorS2CPacket(channelName));
ctx.Chat.SendTo(ctx.User, new UserChannelForceJoinS2CPacket(ctx.Channel.Name));
return;
}
if(channelInfo.Name.Equals(ctx.Channel.Name, StringComparison.InvariantCultureIgnoreCase)) {
// this is where the elusive commented out "samechan" error would go!
// https://patchii.net/sockchat/sockchat/src/commit/6c2111fb4b0241f9ef31060b0f86e7176664f572/server/lib/context.php#L61
ctx.Chat.SendTo(ctx.User, new UserChannelForceJoinS2CPacket(ctx.Channel.Name));
return;
}
if(!ctx.User.Permissions.HasFlag(UserPermissions.JoinAnyChannel) && channelInfo.OwnerId != ctx.User.UserId) {
if(channelInfo.Rank > ctx.User.Rank) {
ctx.Chat.SendTo(ctx.User, new ChannelRankTooLowErrorS2CPacket(channelInfo.Name));
ctx.Chat.SendTo(ctx.User, new UserChannelForceJoinS2CPacket(ctx.Channel.Name));
return;
}
if(!string.IsNullOrEmpty(channelInfo.Password) && channelInfo.Password.Equals(password)) {
ctx.Chat.SendTo(ctx.User, new ChannelPasswordWrongErrorS2CPacket(channelInfo.Name));
ctx.Chat.SendTo(ctx.User, new UserChannelForceJoinS2CPacket(ctx.Channel.Name));
return;
}
}
DateTimeOffset now = DateTimeOffset.UtcNow;
ctx.Chat.Events.Dispatch("chan:leave", now, ctx.Channel, ctx.User);
ctx.Chat.Events.Dispatch("chan:join", now, channelInfo, ctx.User);
}
}
}