sharp-chat/SharpChat/Packet/ChatMessageAddPacket.cs

56 lines
1.7 KiB
C#
Raw Normal View History

2022-08-30 15:00:58 +00:00
using SharpChat.Events;
using System;
using System.Collections.Generic;
using System.Text;
namespace SharpChat.Packet {
public class ChatMessageAddPacket : ServerPacket {
public IChatMessage Message { get; private set; }
public ChatMessageAddPacket(IChatMessage message) : base(message?.SequenceId ?? 0) {
Message = message ?? throw new ArgumentNullException(nameof(message));
2023-02-07 15:01:56 +00:00
if(Message.SequenceId < 1)
2022-08-30 15:00:58 +00:00
Message.SequenceId = SequenceId;
}
public override IEnumerable<string> Pack() {
2023-02-07 15:01:56 +00:00
StringBuilder sb = new();
2022-08-30 15:00:58 +00:00
2023-02-07 14:34:31 +00:00
sb.Append('2');
2022-08-30 15:00:58 +00:00
sb.Append('\t');
sb.Append(Message.DateTime.ToUnixTimeSeconds());
sb.Append('\t');
sb.Append(Message.Sender?.UserId ?? -1);
sb.Append('\t');
2023-02-07 15:01:56 +00:00
if(Message.Flags.HasFlag(ChatMessageFlags.Action))
sb.Append("<i>");
2022-08-30 15:00:58 +00:00
sb.Append(
Message.Text
.Replace("<", "&lt;")
.Replace(">", "&gt;")
.Replace("\n", " <br/> ")
.Replace("\t", " ")
2022-08-30 15:00:58 +00:00
);
2023-02-07 15:01:56 +00:00
if(Message.Flags.HasFlag(ChatMessageFlags.Action))
sb.Append("</i>");
2022-08-30 15:00:58 +00:00
sb.Append('\t');
sb.Append(SequenceId);
sb.AppendFormat(
"\t1{0}0{1}{2}",
Message.Flags.HasFlag(ChatMessageFlags.Action) ? '1' : '0',
Message.Flags.HasFlag(ChatMessageFlags.Action) ? '0' : '1',
Message.Flags.HasFlag(ChatMessageFlags.Private) ? '1' : '0'
);
yield return sb.ToString();
}
}
}