sharp-chat/SharpChat/Packet/ContextMessagePacket.cs

106 lines
4.2 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 {
2022-08-30 15:00:58 +00:00
public class ContextMessagePacket : ServerPacket {
public StoredEventInfo Event { get; private set; }
2022-08-30 15:00:58 +00:00
public bool Notify { get; private set; }
public ContextMessagePacket(StoredEventInfo evt, bool notify = false) {
2022-08-30 15:00:58 +00:00
Event = evt ?? throw new ArgumentNullException(nameof(evt));
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":
case "SharpChat.Events.ChatMessage":
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}",
Event.Sender.UserId,
Event.Sender.LegacyNameWithStatus,
Event.Sender.Colour,
Event.Sender.Rank,
Event.Sender.Can(ChatUserPermissions.KickUser) ? 1 : 0,
Event.Sender.Can(ChatUserPermissions.ViewLogs) ? 1 : 0,
Event.Sender.Can(ChatUserPermissions.SetOwnNickname) ? 1 : 0,
Event.Sender.Can(ChatUserPermissions.CreateChannel | ChatUserPermissions.SetChannelPermanent, true) ? 2 : (
Event.Sender.Can(ChatUserPermissions.CreateChannel) ? 1 : 0
)
);
}
2024-05-10 17:28:52 +00:00
sb.Append('\t');
if(isBroadcast)
sb.Append("0\fsay\f");
2024-05-10 17:28:52 +00:00
string body = Event.Data.RootElement.GetProperty("text").GetString().Replace("<", "&lt;").Replace(">", "&gt;").Replace("\n", " <br/> ").Replace("\t", " ");
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 "user:connect":
case "SharpChat.Events.UserConnectEvent":
2024-05-10 17:28:52 +00:00
sb.AppendFormat("{0}\t0\fjoin\f{1}", V1_CHATBOT, Event.Sender.LegacyName);
2022-08-30 15:00:58 +00:00
break;
case "chan:join":
case "SharpChat.Events.UserChannelJoinEvent":
2024-05-10 17:28:52 +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":
case "SharpChat.Events.UserChannelLeaveEvent":
2024-05-10 17:28:52 +00:00
sb.AppendFormat("{0}\t0\flchan\f{1}", V1_CHATBOT, Event.Sender.LegacyName);
2022-08-30 15:00:58 +00:00
break;
case "user:disconnect":
case "SharpChat.Events.UserDisconnectEvent":
2024-05-10 17:28:52 +00:00
sb.AppendFormat(
"{0}\t0\f{1}\f{2}",
V1_CHATBOT,
(UserDisconnectReason)Event.Data.RootElement.GetProperty("reason").GetByte() switch {
UserDisconnectReason.Flood => "flood",
UserDisconnectReason.Kicked => "kick",
UserDisconnectReason.TimeOut => "timeout",
UserDisconnectReason.Leave => "leave",
_ => "leave",
},
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
}
}
}