From 54af837c82022fc4351931e54f172322af54e71b Mon Sep 17 00:00:00 2001 From: flashwave Date: Thu, 9 May 2024 21:31:19 +0000 Subject: [PATCH] Fixed various inconsistencies with Sock Chat. --- SharpChat/ChatContext.cs | 6 ++--- SharpChat/ChatUser.cs | 16 ++++++------ SharpChat/Packet/ChatMessageAddPacket.cs | 3 +-- SharpChat/Packet/ContextMessagePacket.cs | 32 ++++++++++++++++-------- SharpChat/Packet/UserDisconnectPacket.cs | 2 +- SharpChat/Packet/UserUpdatePacket.cs | 2 +- 6 files changed, 33 insertions(+), 28 deletions(-) diff --git a/SharpChat/ChatContext.cs b/SharpChat/ChatContext.cs index f0cf96a..80ab8be 100644 --- a/SharpChat/ChatContext.cs +++ b/SharpChat/ChatContext.cs @@ -3,13 +3,11 @@ using SharpChat.EventStorage; using SharpChat.Packet; using System; using System.Collections.Generic; -using System.Diagnostics.Tracing; using System.Linq; using System.Net; using System.Threading; -namespace SharpChat -{ +namespace SharpChat { public class ChatContext { public record ChannelUserAssoc(long UserId, string ChannelName); @@ -195,7 +193,7 @@ namespace SharpChat } public void BanUser(ChatUser user, TimeSpan duration, UserDisconnectReason reason = UserDisconnectReason.Kicked) { - if (duration > TimeSpan.Zero) { + if(duration > TimeSpan.Zero) { DateTimeOffset expires = duration >= TimeSpan.MaxValue ? DateTimeOffset.MaxValue : DateTimeOffset.Now + duration; SendTo(user, new ForceDisconnectPacket(ForceDisconnectReason.Banned, expires)); } else diff --git a/SharpChat/ChatUser.cs b/SharpChat/ChatUser.cs index 1cae0f1..c301f21 100644 --- a/SharpChat/ChatUser.cs +++ b/SharpChat/ChatUser.cs @@ -17,19 +17,16 @@ namespace SharpChat { public ChatUserStatus Status { get; set; } public string StatusText { get; set; } - public string LegacyName { + public string LegacyName => string.IsNullOrWhiteSpace(NickName) ? UserName : $"~{NickName}"; + + public string LegacyNameWithStatus { get { StringBuilder sb = new(); if(Status == ChatUserStatus.Away) sb.AppendFormat("<{0}>_", StatusText[..Math.Min(StatusText.Length, 5)].ToUpperInvariant()); - if(string.IsNullOrWhiteSpace(NickName)) - sb.Append(UserName); - else { - sb.Append('~'); - sb.Append(NickName); - } + sb.Append(LegacyName); return sb.ToString(); } @@ -66,7 +63,7 @@ namespace SharpChat { sb.Append(UserId); sb.Append('\t'); - sb.Append(LegacyName); + sb.Append(LegacyNameWithStatus); sb.Append('\t'); sb.Append(Colour); sb.Append('\t'); @@ -88,7 +85,8 @@ namespace SharpChat { public bool NameEquals(string name) { return string.Equals(name, UserName, StringComparison.InvariantCultureIgnoreCase) || string.Equals(name, NickName, StringComparison.InvariantCultureIgnoreCase) - || string.Equals(name, LegacyName, StringComparison.InvariantCultureIgnoreCase); + || string.Equals(name, LegacyName, StringComparison.InvariantCultureIgnoreCase) + || string.Equals(name, LegacyNameWithStatus, StringComparison.InvariantCultureIgnoreCase); } public override int GetHashCode() { diff --git a/SharpChat/Packet/ChatMessageAddPacket.cs b/SharpChat/Packet/ChatMessageAddPacket.cs index 21bdd2d..39c7076 100644 --- a/SharpChat/Packet/ChatMessageAddPacket.cs +++ b/SharpChat/Packet/ChatMessageAddPacket.cs @@ -3,8 +3,7 @@ using System; using System.Collections.Generic; using System.Text; -namespace SharpChat.Packet -{ +namespace SharpChat.Packet { public class ChatMessageAddPacket : ServerPacket { public DateTimeOffset Created { get; } public long UserId { get; } diff --git a/SharpChat/Packet/ContextMessagePacket.cs b/SharpChat/Packet/ContextMessagePacket.cs index 78bede9..cf67340 100644 --- a/SharpChat/Packet/ContextMessagePacket.cs +++ b/SharpChat/Packet/ContextMessagePacket.cs @@ -17,6 +17,10 @@ namespace SharpChat.Packet private const string V1_CHATBOT = "-1\tChatBot\tinherit\t\t"; public override IEnumerable Pack() { + bool isAction = Event.Flags.HasFlag(StoredEventFlags.Action); + bool isBroadcast = Event.Flags.HasFlag(StoredEventFlags.Broadcast); + bool isPrivate = Event.Flags.HasFlag(StoredEventFlags.Private); + StringBuilder sb = new(); sb.Append('7'); @@ -29,14 +33,17 @@ namespace SharpChat.Packet switch(Event.Type) { case "msg:add": case "SharpChat.Events.ChatMessage": - if((Event.Flags & StoredEventFlags.Broadcast) > 0) + if(isBroadcast) { sb.Append(V1_CHATBOT); - else { + sb.Append("0\fsay\f"); + } else { sb.Append(Event.Sender.Pack()); sb.Append('\t'); } - if((Event.Flags & StoredEventFlags.Broadcast) > 0) - sb.Append("0\fsay\f"); + + if(isAction) + sb.Append(""); + sb.Append( Event.Data.RootElement.GetProperty("text").GetString() .Replace("<", "<") @@ -44,27 +51,30 @@ namespace SharpChat.Packet .Replace("\n", "
") .Replace("\t", " ") ); + + if(isAction) + sb.Append("
"); break; case "user:connect": case "SharpChat.Events.UserConnectEvent": sb.Append(V1_CHATBOT); sb.Append("0\fjoin\f"); - sb.Append(Event.Sender.UserName); + sb.Append(Event.Sender.LegacyName); break; case "chan:join": case "SharpChat.Events.UserChannelJoinEvent": sb.Append(V1_CHATBOT); sb.Append("0\fjchan\f"); - sb.Append(Event.Sender.UserName); + sb.Append(Event.Sender.LegacyName); break; case "chan:leave": case "SharpChat.Events.UserChannelLeaveEvent": sb.Append(V1_CHATBOT); sb.Append("0\flchan\f"); - sb.Append(Event.Sender.UserName); + sb.Append(Event.Sender.LegacyName); break; case "user:disconnect": @@ -89,7 +99,7 @@ namespace SharpChat.Packet } sb.Append('\f'); - sb.Append(Event.Sender.UserName); + sb.Append(Event.Sender.LegacyName); break; } @@ -99,9 +109,9 @@ namespace SharpChat.Packet sb.Append(Notify ? '1' : '0'); sb.AppendFormat( "\t1{0}0{1}{2}", - Event.Flags.HasFlag(StoredEventFlags.Action) ? '1' : '0', - Event.Flags.HasFlag(StoredEventFlags.Action) ? '0' : '1', - Event.Flags.HasFlag(StoredEventFlags.Private) ? '1' : '0' + isAction ? '1' : '0', + isAction ? '0' : '1', + isPrivate ? '1' : '0' ); yield return sb.ToString(); diff --git a/SharpChat/Packet/UserDisconnectPacket.cs b/SharpChat/Packet/UserDisconnectPacket.cs index 38c3acb..9d9840c 100644 --- a/SharpChat/Packet/UserDisconnectPacket.cs +++ b/SharpChat/Packet/UserDisconnectPacket.cs @@ -28,7 +28,7 @@ namespace SharpChat.Packet { sb.Append('\t'); sb.Append(User.UserId); sb.Append('\t'); - sb.Append(User.LegacyName); + sb.Append(User.LegacyNameWithStatus); sb.Append('\t'); switch(Reason) { diff --git a/SharpChat/Packet/UserUpdatePacket.cs b/SharpChat/Packet/UserUpdatePacket.cs index 292a260..d06bd74 100644 --- a/SharpChat/Packet/UserUpdatePacket.cs +++ b/SharpChat/Packet/UserUpdatePacket.cs @@ -24,7 +24,7 @@ namespace SharpChat.Packet { sb.Append("\t-1\t0\fnick\f"); sb.Append(PreviousName); sb.Append('\f'); - sb.Append(User.LegacyName); + sb.Append(User.LegacyNameWithStatus); sb.Append('\t'); sb.Append(SequenceId); sb.Append("\t10010");