diff --git a/SharpChat/ChatContext.cs b/SharpChat/ChatContext.cs index 6b53505..e4b3471 100644 --- a/SharpChat/ChatContext.cs +++ b/SharpChat/ChatContext.cs @@ -203,13 +203,38 @@ namespace SharpChat { public void HandleChannelEventLog(string channelName, Action handler) { foreach(StoredEventInfo msg in Events.GetChannelEventLog(channelName)) handler(msg.Type switch { - "user:connect" => new UserConnectLogPacket(msg.Created, msg.Sender == null ? string.Empty : SockChatUtility.GetUserName(msg.Sender)), + "msg:add" => new MessageAddLogPacket( + msg.Id, + msg.Created, + msg.Sender?.UserId ?? -1, + msg.Sender == null ? "ChatBot" : SockChatUtility.GetUserName(msg.Sender), + msg.Sender?.Colour ?? Colour.None, + msg.Sender?.Rank ?? 0, + msg.Sender?.Permissions ?? 0, + msg.Data.RootElement.GetProperty("text").GetString() ?? string.Empty, + msg.Flags.HasFlag(StoredEventFlags.Action), + msg.Flags.HasFlag(StoredEventFlags.Private), + msg.Flags.HasFlag(StoredEventFlags.Broadcast), + false + ), + "user:connect" => new UserConnectLogPacket( + msg.Created, + msg.Sender == null ? string.Empty : SockChatUtility.GetUserName(msg.Sender) + ), "user:disconnect" => new UserDisconnectLogPacket( msg.Created, msg.Sender == null ? string.Empty : SockChatUtility.GetUserNameWithStatus(msg.Sender), (UserDisconnectReason)msg.Data.RootElement.GetProperty("reason").GetByte() ), - _ => new MessagePopulatePacket(msg), + "chan:join" => new UserChannelJoinLogPacket( + msg.Created, + msg.Sender == null ? string.Empty : SockChatUtility.GetUserName(msg.Sender) + ), + "chan:leave" => new UserChannelLeaveLogPacket( + msg.Created, + msg.Sender == null ? string.Empty : SockChatUtility.GetUserName(msg.Sender) + ), + _ => throw new Exception($"Unsupported backlog type: {msg.Type}"), }); } diff --git a/SharpChat/Packet/MessageAddLogPacket.cs b/SharpChat/Packet/MessageAddLogPacket.cs new file mode 100644 index 0000000..ca1b075 --- /dev/null +++ b/SharpChat/Packet/MessageAddLogPacket.cs @@ -0,0 +1,81 @@ +using System; + +namespace SharpChat.Packet { + public class MessageAddLogPacket : ServerPacket { + private readonly long Created; + private readonly long UserId; + private readonly string UserName; + private readonly Colour UserColour; + private readonly int UserRank; + private readonly UserPermissions UserPerms; + private readonly string Body; + private readonly bool IsAction; + private readonly bool IsPrivate; + private readonly bool IsBroadcast; // this should be MessageBroadcastLogPacket + private readonly bool Notify; + + public MessageAddLogPacket( + long msgId, + DateTimeOffset created, + long userId, + string userName, + Colour userColour, + int userRank, + UserPermissions userPerms, + string body, + bool isAction, + bool isPrivate, + bool isBroadcast, + bool notify + ) : base(msgId) { + Created = created.ToUnixTimeSeconds(); + UserId = userId < 0 ? -1 : userId; + UserName = userName; + UserColour = userColour; + UserRank = userRank; + UserPerms = userPerms; + Body = body; + IsAction = isAction; + IsPrivate = isPrivate; + IsBroadcast = isBroadcast; + Notify = notify; + } + + public override string Pack() { + string body = SockChatUtility.SanitiseMessageBody(Body); + if(IsAction) + body = string.Format("{0}", body); + + if(IsBroadcast) + body = "0\fsay\f" + body; + + string userPerms = UserId < 0 ? string.Empty : string.Format( + "{0} {1} {2} {3} {4}", + UserRank, + UserPerms.HasFlag(UserPermissions.KickUser) == true ? 1 : 0, + UserPerms.HasFlag(UserPermissions.ViewLogs) == true ? 1 : 0, + UserPerms.HasFlag(UserPermissions.SetOwnNickname) == true ? 1 : 0, + UserPerms.HasFlag(UserPermissions.CreateChannel) == true ? ( + UserPerms.HasFlag(UserPermissions.SetChannelPermanent) == true ? 2 : 1 + ) : 0 + ); + + return string.Format( + "7\t1\t{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}{9}{10}{11}{12}", + Created, + UserId, + UserName, + UserColour, + userPerms, + body, + SequenceId, + Notify ? 1 : 0, + 1, + IsAction ? 1 : 0, + 0, + IsAction ? 0 : 1, + IsPrivate ? 1 : 0 + ); + } + } +} diff --git a/SharpChat/Packet/MessagePopulatePacket.cs b/SharpChat/Packet/MessagePopulatePacket.cs deleted file mode 100644 index 948dc66..0000000 --- a/SharpChat/Packet/MessagePopulatePacket.cs +++ /dev/null @@ -1,89 +0,0 @@ -using SharpChat.EventStorage; -using System.Text; - -namespace SharpChat.Packet { - // this entire class is disgusting - public class MessagePopulatePacket : ServerPacket { - private readonly StoredEventInfo Event; - private readonly bool Notify; - - public MessagePopulatePacket(StoredEventInfo evt, bool notify = false) { - Event = evt; - Notify = notify; - } - - private const string V1_CHATBOT = "-1\tChatBot\tinherit\t"; - - public override 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.AppendFormat("7\t1\t{0}\t", Event.Created.ToUnixTimeSeconds()); - - switch(Event.Type) { - case "msg:add": - if(isBroadcast) { - sb.Append(V1_CHATBOT); - } else { - sb.AppendFormat( - "{0}\t{1}\t{2}\t{3} {4} {5} {6} {7}", - Event.Sender?.UserId, - Event.Sender == null ? string.Empty : SockChatUtility.GetUserNameWithStatus(Event.Sender), - Event.Sender?.Colour, - Event.Sender?.Rank, - Event.Sender?.Permissions.HasFlag(UserPermissions.KickUser) == true ? 1 : 0, - Event.Sender?.Permissions.HasFlag(UserPermissions.ViewLogs) == true ? 1 : 0, - Event.Sender?.Permissions.HasFlag(UserPermissions.SetOwnNickname) == true ? 1 : 0, - Event.Sender?.Permissions.HasFlag(UserPermissions.CreateChannel) == true ? ( - Event.Sender?.Permissions.HasFlag(UserPermissions.SetChannelPermanent) == true ? 2 : 1 - ) : 0 - ); - } - - sb.Append('\t'); - - if(isBroadcast) - sb.Append("0\fsay\f"); - - string body = SockChatUtility.SanitiseMessageBody(Event.Data.RootElement.GetProperty("text").GetString()); - if(isAction) - body = string.Format("{0}", body); - - sb.Append(body); - break; - - case "chan:join": - sb.AppendFormat( - "{0}\t0\fjchan\f{1}", - V1_CHATBOT, - Event.Sender == null ? string.Empty : SockChatUtility.GetUserName(Event.Sender) - ); - break; - - case "chan:leave": - sb.AppendFormat( - "{0}\t0\flchan\f{1}", - V1_CHATBOT, - Event.Sender == null ? string.Empty : SockChatUtility.GetUserName(Event.Sender) - ); - break; - } - - sb.AppendFormat( - "\t{0}\t{1}\t{2}{3}{4}{5}{6}", - Event.Id < 1 ? SequenceId : Event.Id, - Notify ? 1 : 0, - 1, - isAction ? 1 : 0, - 0, - isAction ? 0 : 1, - isPrivate ? 1 : 0 - ); - - return sb.ToString(); - } - } -} diff --git a/SharpChat/Packet/UserChannelJoinLogPacket.cs b/SharpChat/Packet/UserChannelJoinLogPacket.cs new file mode 100644 index 0000000..dc3ad26 --- /dev/null +++ b/SharpChat/Packet/UserChannelJoinLogPacket.cs @@ -0,0 +1,25 @@ +using System; + +namespace SharpChat.Packet { + public class UserChannelJoinLogPacket : ServerPacket { + private readonly long Timestamp; + private readonly string UserName; + + public UserChannelJoinLogPacket( + DateTimeOffset timestamp, + string userName + ) { + Timestamp = timestamp.ToUnixTimeSeconds(); + UserName = userName; + } + + public override string Pack() { + return string.Format( + "7\t1\t{0}\t-1\tChatBot\tinherit\t\t0\fjchan\f{1}\t{2}\t0\t10010", + Timestamp, + UserName, + SequenceId + ); + } + } +} diff --git a/SharpChat/Packet/UserChannelLeaveLogPacket.cs b/SharpChat/Packet/UserChannelLeaveLogPacket.cs new file mode 100644 index 0000000..89dadb1 --- /dev/null +++ b/SharpChat/Packet/UserChannelLeaveLogPacket.cs @@ -0,0 +1,25 @@ +using System; + +namespace SharpChat.Packet { + public class UserChannelLeaveLogPacket : ServerPacket { + private readonly long Timestamp; + private readonly string UserName; + + public UserChannelLeaveLogPacket( + DateTimeOffset timestamp, + string userName + ) { + Timestamp = timestamp.ToUnixTimeSeconds(); + UserName = userName; + } + + public override string Pack() { + return string.Format( + "7\t1\t{0}\t-1\tChatBot\tinherit\t\t0\flchan\f{1}\t{2}\t0\t10010", + Timestamp, + UserName, + SequenceId + ); + } + } +}