sharp-chat/SharpChat/Packet/MessagePopulatePacket.cs

89 lines
3.4 KiB
C#

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("<i>{0}</i>", 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();
}
}
}