sharp-chat/SharpChat.Protocol.SockChat/Users/IUserExtensions.cs

94 lines
3.4 KiB
C#

using SharpChat.Events;
using SharpChat.Protocol.SockChat.Packets;
using SharpChat.Users;
using System;
using System.Text;
namespace SharpChat.Protocol.SockChat.Users {
public static class IUserExtensions {
public static string GetDisplayName(this IUser user) {
if(user is ChatBot)
return user.UserName;
StringBuilder sb = new();
if(user is ILocalUser localUser) {
if(localUser.Status == UserStatus.Away)
sb.Append(localUser.StatusMessage.ToAFKString());
if(string.IsNullOrWhiteSpace(localUser.NickName))
sb.Append(user.UserName);
else {
sb.Append('~');
sb.Append(localUser.NickName);
}
} else
sb.Append(user.UserName);
return sb.ToString();
}
public static string GetDisplayName(this UserUpdateEvent uue) {
StringBuilder sb = new();
if((uue.NewStatus ?? uue.OldStatus) == UserStatus.Away)
sb.Append((uue.NewStatusMessage ?? uue.OldStatusMessage).ToAFKString());
if(string.IsNullOrWhiteSpace(uue.NewNickName ?? uue.OldNickName))
sb.Append(uue.NewUserName ?? uue.OldUserName);
else {
sb.Append('~');
sb.Append(uue.NewNickName ?? uue.OldNickName);
}
return sb.ToString();
}
public static string ToAFKString(this string str)
=> string.Format(@"<{0}>_", str.Substring(0, Math.Min(str.Length, 5)).ToUpperInvariant());
public static string Pack(this IUser user) {
StringBuilder sb = new();
user.Pack(sb);
return sb.ToString();
}
public static void Pack(this IUser user, StringBuilder sb) {
sb.Append(user.UserId);
sb.Append(IServerPacket.SEPARATOR);
sb.Append(user.GetDisplayName());
sb.Append(IServerPacket.SEPARATOR);
sb.Append(user.Colour);
sb.Append(IServerPacket.SEPARATOR);
if(!user.IsBot()) { // permission part is empty for bot apparently
sb.Append(user.Rank);
sb.Append(' ');
sb.Append(user.Can(UserPermissions.KickUser) ? '1' : '0');
sb.Append(@" 0 ");
sb.Append(user.Can(UserPermissions.SetOwnNickname) ? '1' : '0');
sb.Append(' ');
sb.Append(user.Can(UserPermissions.CreateChannel | UserPermissions.SetChannelPermanent) ? 2 : (
user.Can(UserPermissions.CreateChannel) ? 1 : 0
));
}
}
public static void Pack(this UserPermissions perms, StringBuilder sb) {
sb.Append(' ');
sb.Append((perms & UserPermissions.KickUser) > 0 ? '1' : '0');
sb.Append(' ');
sb.Append(0); // Legacy view logs
sb.Append(' ');
sb.Append((perms & UserPermissions.SetOwnNickname) > 0 ? '1' : '0');
sb.Append(' ');
sb.Append((perms & UserPermissions.CreateChannel) > 0 ? ((perms & UserPermissions.SetChannelPermanent) > 0 ? 2 : 1) : 0);
}
public static string Pack(this UserPermissions perms) {
StringBuilder sb = new();
perms.Pack(sb);
return sb.ToString();
}
}
}