Use HasFlag instead of custom Can method.

This commit is contained in:
flash 2024-05-17 23:50:22 +00:00
parent 322500739e
commit 68a523f76a
17 changed files with 27 additions and 29 deletions

View file

@ -295,7 +295,7 @@ namespace SharpChat {
return;
}
if(!user.Can(ChatUserPermissions.JoinAnyChannel) && chan.IsOwner(user)) {
if(!user.Permissions.HasFlag(ChatUserPermissions.JoinAnyChannel) && chan.IsOwner(user)) {
if(chan.Rank > user.Rank) {
SendTo(user, new ChannelRankTooLowErrorPacket(chan.Name));
ForceChannel(user);

View file

@ -54,11 +54,6 @@ namespace SharpChat {
IsSuper = isSuper;
}
public bool Can(ChatUserPermissions perm, bool strict = false) {
ChatUserPermissions perms = Permissions & perm;
return strict ? perms == perm : perms > 0;
}
public bool NameEquals(string? name) {
return string.Equals(name, UserName, StringComparison.InvariantCultureIgnoreCase)
|| string.Equals(name, NickName, StringComparison.InvariantCultureIgnoreCase)

View file

@ -18,7 +18,8 @@ namespace SharpChat.Commands {
}
public void Dispatch(ChatCommandContext ctx) {
if(!ctx.User.Can(ChatUserPermissions.BanUser | ChatUserPermissions.KickUser)) {
if(!ctx.User.Permissions.HasFlag(ChatUserPermissions.KickUser)
&& !ctx.User.Permissions.HasFlag(ChatUserPermissions.BanUser)) {
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
return;
}

View file

@ -8,7 +8,7 @@ namespace SharpChat.Commands {
}
public void Dispatch(ChatCommandContext ctx) {
if(!ctx.User.Can(ChatUserPermissions.CreateChannel)) {
if(!ctx.User.Permissions.HasFlag(ChatUserPermissions.CreateChannel)) {
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
return;
}
@ -45,7 +45,7 @@ namespace SharpChat.Commands {
ChatChannel createChan = new(
ctx.User, createChanName,
isTemporary: !ctx.User.Can(ChatUserPermissions.SetChannelPermanent),
isTemporary: !ctx.User.Permissions.HasFlag(ChatUserPermissions.SetChannelPermanent),
rank: createChanHierarchy
);

View file

@ -24,7 +24,7 @@ namespace SharpChat.Commands {
return;
}
if(!ctx.User.Can(ChatUserPermissions.DeleteChannel) && delChan.IsOwner(ctx.User)) {
if(!ctx.User.Permissions.HasFlag(ChatUserPermissions.DeleteChannel) || !delChan.IsOwner(ctx.User)) {
ctx.Chat.SendTo(ctx.User, new ChannelDeleteNotAllowedErrorPacket(delChan.Name));
return;
}

View file

@ -8,7 +8,7 @@ namespace SharpChat.Commands {
}
public void Dispatch(ChatCommandContext ctx) {
if(!ctx.User.Can(ChatUserPermissions.SetChannelPassword) || ctx.Channel.IsOwner(ctx.User)) {
if(!ctx.User.Permissions.HasFlag(ChatUserPermissions.SetChannelPassword) || !ctx.Channel.IsOwner(ctx.User)) {
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
return;
}

View file

@ -10,7 +10,7 @@ namespace SharpChat.Commands {
}
public void Dispatch(ChatCommandContext ctx) {
if(!ctx.User.Can(ChatUserPermissions.SetChannelHierarchy) || ctx.Channel.IsOwner(ctx.User)) {
if(!ctx.User.Permissions.HasFlag(ChatUserPermissions.SetChannelHierarchy) || !ctx.Channel.IsOwner(ctx.User)) {
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
return;
}

View file

@ -20,7 +20,7 @@ namespace SharpChat.Commands {
public void Dispatch(ChatCommandContext ctx) {
bool isBanning = ctx.NameEquals("ban");
if(!ctx.User.Can(isBanning ? ChatUserPermissions.BanUser : ChatUserPermissions.KickUser)) {
if(!ctx.User.Permissions.HasFlag(isBanning ? ChatUserPermissions.BanUser : ChatUserPermissions.KickUser)) {
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
return;
}

View file

@ -10,7 +10,7 @@ namespace SharpChat.Commands {
}
public void Dispatch(ChatCommandContext ctx) {
if(!ctx.User.Can(ChatUserPermissions.Broadcast)) {
if(!ctx.User.Permissions.HasFlag(ChatUserPermissions.Broadcast)) {
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
return;
}

View file

@ -13,9 +13,9 @@ namespace SharpChat.Commands
}
public void Dispatch(ChatCommandContext ctx) {
bool deleteAnyMessage = ctx.User.Can(ChatUserPermissions.DeleteAnyMessage);
bool deleteAnyMessage = ctx.User.Permissions.HasFlag(ChatUserPermissions.DeleteAnyMessage);
if(!deleteAnyMessage && !ctx.User.Can(ChatUserPermissions.DeleteOwnMessage)) {
if(!deleteAnyMessage && !ctx.User.Permissions.HasFlag(ChatUserPermissions.DeleteOwnMessage)) {
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
return;
}

View file

@ -19,7 +19,8 @@ namespace SharpChat.Commands {
}
public void Dispatch(ChatCommandContext ctx) {
if(!ctx.User.Can(ChatUserPermissions.BanUser | ChatUserPermissions.KickUser)) {
if(!ctx.User.Permissions.HasFlag(ChatUserPermissions.KickUser)
&& !ctx.User.Permissions.HasFlag(ChatUserPermissions.BanUser)) {
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
return;
}

View file

@ -18,7 +18,8 @@ namespace SharpChat.Commands {
}
public void Dispatch(ChatCommandContext ctx) {
if(!ctx.User.Can(ChatUserPermissions.BanUser | ChatUserPermissions.KickUser)) {
if(!ctx.User.Permissions.HasFlag(ChatUserPermissions.KickUser)
&& !ctx.User.Permissions.HasFlag(ChatUserPermissions.BanUser)) {
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
return;
}

View file

@ -8,9 +8,9 @@ namespace SharpChat.Commands {
}
public void Dispatch(ChatCommandContext ctx) {
bool setOthersNick = ctx.User.Can(ChatUserPermissions.SetOthersNickname);
bool setOthersNick = ctx.User.Permissions.HasFlag(ChatUserPermissions.SetOthersNickname);
if(!setOthersNick && !ctx.User.Can(ChatUserPermissions.SetOwnNickname)) {
if(!setOthersNick && !ctx.User.Permissions.HasFlag(ChatUserPermissions.SetOwnNickname)) {
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
return;
}

View file

@ -25,7 +25,7 @@ namespace SharpChat.Commands {
return;
}
if(channel.Rank > ctx.User.Rank || (channel.HasPassword && !ctx.User.Can(ChatUserPermissions.JoinAnyChannel))) {
if(channel.Rank > ctx.User.Rank || (channel.HasPassword && !ctx.User.Permissions.HasFlag(ChatUserPermissions.JoinAnyChannel))) {
ctx.Chat.SendTo(ctx.User, new WhoChannelNotFoundErrorPacket(channelName));
return;
}

View file

@ -10,7 +10,7 @@ namespace SharpChat.Commands {
}
public void Dispatch(ChatCommandContext ctx) {
if(!ctx.User.Can(ChatUserPermissions.SeeIPAddress)) {
if(!ctx.User.Permissions.HasFlag(ChatUserPermissions.SeeIPAddress)) {
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
return;
}

View file

@ -35,12 +35,12 @@ namespace SharpChat.Packet {
Event.Sender?.LegacyNameWithStatus,
Event.Sender?.Colour,
Event.Sender?.Rank,
Event.Sender?.Can(ChatUserPermissions.KickUser) == true ? 1 : 0,
Event.Sender?.Can(ChatUserPermissions.ViewLogs) == true ? 1 : 0,
Event.Sender?.Can(ChatUserPermissions.SetOwnNickname) == true ? 1 : 0,
Event.Sender?.Can(ChatUserPermissions.CreateChannel | ChatUserPermissions.SetChannelPermanent, true) == true ? 2 : (
Event.Sender?.Can(ChatUserPermissions.CreateChannel) == true ? 1 : 0
)
Event.Sender?.Permissions.HasFlag(ChatUserPermissions.KickUser) == true ? 1 : 0,
Event.Sender?.Permissions.HasFlag(ChatUserPermissions.ViewLogs) == true ? 1 : 0,
Event.Sender?.Permissions.HasFlag(ChatUserPermissions.SetOwnNickname) == true ? 1 : 0,
Event.Sender?.Permissions.HasFlag(ChatUserPermissions.CreateChannel) == true ? (
Event.Sender?.Permissions.HasFlag(ChatUserPermissions.SetChannelPermanent) == true ? 2 : 1
) : 0
);
}

View file

@ -38,7 +38,7 @@ namespace SharpChat.PacketHandlers
// No longer concats everything after index 1 with \t, no previous implementation did that either
string? messageText = args.ElementAtOrDefault(2);
if(user == null || !user.Can(ChatUserPermissions.SendMessage) || string.IsNullOrWhiteSpace(messageText))
if(user == null || !user.Permissions.HasFlag(ChatUserPermissions.SendMessage) || string.IsNullOrWhiteSpace(messageText))
return;
// Extra validation step, not necessary at all but enforces proper formatting in SCv1.