Fixed various inconsistencies with Sock Chat.

This commit is contained in:
flash 2024-05-09 21:31:19 +00:00
parent 0d0f2e68b9
commit 54af837c82
6 changed files with 33 additions and 28 deletions

View file

@ -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

View file

@ -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() {

View file

@ -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; }

View file

@ -17,6 +17,10 @@ namespace SharpChat.Packet
private const string V1_CHATBOT = "-1\tChatBot\tinherit\t\t";
public override IEnumerable<string> 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("<i>");
sb.Append(
Event.Data.RootElement.GetProperty("text").GetString()
.Replace("<", "&lt;")
@ -44,27 +51,30 @@ namespace SharpChat.Packet
.Replace("\n", " <br/> ")
.Replace("\t", " ")
);
if(isAction)
sb.Append("</i>");
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();

View file

@ -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) {

View file

@ -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");