2024-05-20 23:40:34 +00:00
|
|
|
|
using SharpChat.SockChat.PacketsS2C;
|
2024-05-29 20:51:37 +00:00
|
|
|
|
using System;
|
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 ChannelJoinCommand : ISockChatClientCommand {
|
|
|
|
|
public bool IsMatch(SockChatClientCommandContext ctx) {
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return ctx.NameEquals("join");
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 23:00:47 +00:00
|
|
|
|
public void Dispatch(SockChatClientCommandContext ctx) {
|
2024-05-29 20:51:37 +00:00
|
|
|
|
string channelName = ctx.Args.FirstOrDefault() ?? string.Empty;
|
|
|
|
|
string password = string.Join(' ', ctx.Args.Skip(1));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
|
2024-05-29 20:51:37 +00:00
|
|
|
|
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));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-29 20:51:37 +00:00
|
|
|
|
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);
|
2023-02-16 20:34:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|