Reintroduces separate contexts for users, channels, connections (now split into sessions and connections) and user-channel associations. It builds which is as much assurance as I can give about the stability of this commit, but its also the bare minimum of what i like to commit sooooo A lot of things still need to be broadcast through events throughout the application in order to keep states consistent but we'll cross that bridge when we get to it. I really need to stop using that phrase thingy, I'm overusing it.
59 lines
2.2 KiB
C#
59 lines
2.2 KiB
C#
using SharpChat.SockChat.S2CPackets;
|
|
using SharpChat.Users;
|
|
using System.Globalization;
|
|
using System.Text;
|
|
|
|
namespace SharpChat.ClientCommands;
|
|
|
|
public class NickClientCommand : ClientCommand {
|
|
private const int MAX_GRAPHEMES = 16;
|
|
private const int MAX_BYTES = MAX_GRAPHEMES * 10;
|
|
|
|
public bool IsMatch(ClientCommandContext ctx) {
|
|
return ctx.NameEquals("nick");
|
|
}
|
|
|
|
public async Task Dispatch(ClientCommandContext ctx) {
|
|
long msgId = ctx.Chat.RandomSnowflake.Next();
|
|
bool setOthersNick = ctx.User.Permissions.HasFlag(UserPermissions.SetOthersNickname);
|
|
|
|
if(!setOthersNick && !ctx.User.Permissions.HasFlag(UserPermissions.SetOwnNickname)) {
|
|
await ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
|
|
return;
|
|
}
|
|
|
|
User? targetUser = null;
|
|
int offset = 0;
|
|
|
|
if(setOthersNick && long.TryParse(ctx.Args.FirstOrDefault(), out long targetUserId) && targetUserId > 0) {
|
|
targetUser = ctx.Chat.Users.GetUser(targetUserId.ToString());
|
|
++offset;
|
|
}
|
|
|
|
targetUser ??= ctx.User;
|
|
|
|
if(ctx.Args.Length < offset) {
|
|
await ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.COMMAND_FORMAT_ERROR));
|
|
return;
|
|
}
|
|
|
|
string nickStr = string.Join('_', ctx.Args.Skip(offset))
|
|
.Replace("\n", string.Empty).Replace("\r", string.Empty)
|
|
.Replace("\f", string.Empty).Replace("\t", string.Empty)
|
|
.Replace(' ', '_').Trim();
|
|
|
|
nickStr = nickStr == targetUser.UserName
|
|
? string.Empty
|
|
: (string.IsNullOrEmpty(nickStr)
|
|
? string.Empty
|
|
: nickStr.TruncateIfTooLong(MAX_GRAPHEMES, MAX_BYTES).Trim());
|
|
|
|
if(!string.IsNullOrWhiteSpace(nickStr) && ctx.Chat.Users.UserWithLegacyNameExists(nickStr)) {
|
|
await ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.NAME_IN_USE, true, nickStr));
|
|
return;
|
|
}
|
|
|
|
string? previousName = targetUser.UserId == ctx.User.UserId ? (targetUser.NickName ?? targetUser.UserName) : null;
|
|
await ctx.Chat.UpdateUser(targetUser, nickName: nickStr, silent: previousName == null);
|
|
}
|
|
}
|