sharp-chat/SharpChat/Packet/ContextMessagePacket.cs

119 lines
4.1 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;
}
private const string V1_CHATBOT = "-1\tChatBot\tinherit\t\t";
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
2023-02-07 14:34:31 +00:00
sb.Append('7');
2022-08-30 15:00:58 +00:00
sb.Append('\t');
2023-02-07 14:34:31 +00:00
sb.Append('1');
2022-08-30 15:00:58 +00:00
sb.Append('\t');
sb.Append(Event.Created.ToUnixTimeSeconds());
2022-08-30 15:00:58 +00:00
sb.Append('\t');
switch(Event.Type) {
case "msg:add":
case "SharpChat.Events.ChatMessage":
if(isBroadcast) {
sb.Append(V1_CHATBOT);
sb.Append("0\fsay\f");
} else {
sb.Append(Event.Sender.Pack());
sb.Append('\t');
}
if(isAction)
sb.Append("<i>");
2022-08-30 15:00:58 +00:00
sb.Append(
Event.Data.RootElement.GetProperty("text").GetString()
.Replace("<", "&lt;")
.Replace(">", "&gt;")
.Replace("\n", " <br/> ")
.Replace("\t", " ")
2022-08-30 15:00:58 +00:00
);
if(isAction)
sb.Append("</i>");
2022-08-30 15:00:58 +00:00
break;
case "user:connect":
case "SharpChat.Events.UserConnectEvent":
2022-08-30 15:00:58 +00:00
sb.Append(V1_CHATBOT);
sb.Append("0\fjoin\f");
sb.Append(Event.Sender.LegacyName);
2022-08-30 15:00:58 +00:00
break;
case "chan:join":
case "SharpChat.Events.UserChannelJoinEvent":
2022-08-30 15:00:58 +00:00
sb.Append(V1_CHATBOT);
sb.Append("0\fjchan\f");
sb.Append(Event.Sender.LegacyName);
2022-08-30 15:00:58 +00:00
break;
case "chan:leave":
case "SharpChat.Events.UserChannelLeaveEvent":
2022-08-30 15:00:58 +00:00
sb.Append(V1_CHATBOT);
sb.Append("0\flchan\f");
sb.Append(Event.Sender.LegacyName);
2022-08-30 15:00:58 +00:00
break;
case "user:disconnect":
case "SharpChat.Events.UserDisconnectEvent":
2022-08-30 15:00:58 +00:00
sb.Append(V1_CHATBOT);
sb.Append("0\f");
switch((UserDisconnectReason)Event.Data.RootElement.GetProperty("reason").GetByte()) {
2022-08-30 15:00:58 +00:00
case UserDisconnectReason.Flood:
sb.Append("flood");
2022-08-30 15:00:58 +00:00
break;
case UserDisconnectReason.Kicked:
sb.Append("kick");
2022-08-30 15:00:58 +00:00
break;
case UserDisconnectReason.TimeOut:
sb.Append("timeout");
2022-08-30 15:00:58 +00:00
break;
case UserDisconnectReason.Leave:
default:
sb.Append("leave");
2022-08-30 15:00:58 +00:00
break;
}
sb.Append('\f');
sb.Append(Event.Sender.LegacyName);
2022-08-30 15:00:58 +00:00
break;
}
sb.Append('\t');
sb.Append(Event.Id < 1 ? SequenceId : Event.Id);
2022-08-30 15:00:58 +00:00
sb.Append('\t');
sb.Append(Notify ? '1' : '0');
sb.AppendFormat(
"\t1{0}0{1}{2}",
isAction ? '1' : '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
}
}
}