sharp-chat/SharpChat/Packet/MessagePopulatePacket.cs

83 lines
3 KiB
C#
Raw Normal View History

using SharpChat.EventStorage;
2022-08-30 15:00:58 +00:00
using System;
using System.Text;
2024-05-10 15:24:43 +00:00
namespace SharpChat.Packet {
// this entire class is disgusting
2024-05-14 22:17:25 +00:00
public class MessagePopulatePacket : ServerPacket {
private readonly StoredEventInfo Event;
private readonly bool Notify;
2022-08-30 15:00:58 +00:00
2024-05-14 22:17:25 +00:00
public MessagePopulatePacket(StoredEventInfo evt, bool notify = false) {
Event = evt;
2022-08-30 15:00:58 +00:00
Notify = notify;
}
2024-05-10 17:28:52 +00:00
private const string V1_CHATBOT = "-1\tChatBot\tinherit\t";
2022-08-30 15:00:58 +00:00
2024-05-10 15:24:43 +00:00
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);
2023-02-07 15:01:56 +00:00
StringBuilder sb = new();
2022-08-30 15:00:58 +00:00
2024-05-10 17:28:52 +00:00
sb.AppendFormat("7\t1\t{0}\t", Event.Created.ToUnixTimeSeconds());
2022-08-30 15:00:58 +00:00
switch(Event.Type) {
case "msg:add":
if(isBroadcast) {
sb.Append(V1_CHATBOT);
} else {
2024-05-10 17:28:52 +00:00
sb.AppendFormat(
"{0}\t{1}\t{2}\t{3} {4} {5} {6} {7}",
2024-05-10 19:18:55 +00:00
Event.Sender?.UserId,
Event.Sender?.LegacyNameWithStatus,
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
2024-05-10 17:28:52 +00:00
);
}
2024-05-10 17:28:52 +00:00
sb.Append('\t');
if(isBroadcast)
sb.Append("0\fsay\f");
string body = Utility.Sanitise(Event.Data.RootElement.GetProperty("text").GetString());
if(isAction)
2024-05-10 17:28:52 +00:00
body = string.Format("<i>{0}</i>", body);
sb.Append(body);
2022-08-30 15:00:58 +00:00
break;
case "chan:join":
2024-05-10 19:18:55 +00:00
sb.AppendFormat("{0}\t0\fjchan\f{1}", V1_CHATBOT, Event.Sender?.LegacyName);
2022-08-30 15:00:58 +00:00
break;
case "chan:leave":
2024-05-10 19:18:55 +00:00
sb.AppendFormat("{0}\t0\flchan\f{1}", V1_CHATBOT, Event.Sender?.LegacyName);
2022-08-30 15:00:58 +00:00
break;
}
sb.AppendFormat(
2024-05-10 17:28:52 +00:00
"\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
2022-08-30 15:00:58 +00:00
);
2024-05-10 15:24:43 +00:00
return sb.ToString();
2022-08-30 15:00:58 +00:00
}
}
}