sharp-chat/SharpChat/Packet/ChatMessageAddPacket.cs

45 lines
1.4 KiB
C#

using System;
namespace SharpChat.Packet {
public class ChatMessageAddPacket : ServerPacket {
private readonly DateTimeOffset Created;
private readonly long UserId;
private readonly string Body;
private readonly bool IsAction;
private readonly bool IsPrivate;
public ChatMessageAddPacket(
long msgId,
DateTimeOffset created,
long userId,
string body,
bool isAction,
bool isPrivate
) : base(msgId) {
Created = created;
UserId = userId < 0 ? -1 : userId;
Body = body ?? throw new ArgumentNullException(nameof(body));
IsAction = isAction;
IsPrivate = isPrivate;
}
public override string Pack() {
string body = Body.Replace("<", "&lt;").Replace(">", "&gt;").Replace("\n", " <br/> ").Replace("\t", " ");
if(IsAction)
body = string.Format("<i>{0}</i>", body);
return string.Format(
"2\t{0}\t{1}\t{2}\t{3}\t{4}{5}{6}{7}{8}",
Created.ToUnixTimeSeconds(),
UserId,
body,
SequenceId,
1,
IsAction ? 1 : 0,
0,
IsAction ? 0 : 1,
IsPrivate ? 1 : 0
);
}
}
}