sharp-chat/SharpChat/Packet/ContextMessagePacket.cs

111 lines
4 KiB
C#
Raw Normal View History

using SharpChat.EventStorage;
2022-08-30 15:00:58 +00:00
using System;
using System.Collections.Generic;
using System.Text;
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";
public override IEnumerable<string> Pack() {
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((Event.Flags & StoredEventFlags.Broadcast) > 0)
sb.Append(V1_CHATBOT);
else {
sb.Append(Event.Sender.Pack());
sb.Append('\t');
}
if((Event.Flags & StoredEventFlags.Broadcast) > 0)
sb.Append("0\fsay\f");
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
);
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.UserName);
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.UserName);
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.UserName);
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.UserName);
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}",
Event.Flags.HasFlag(StoredEventFlags.Action) ? '1' : '0',
Event.Flags.HasFlag(StoredEventFlags.Action) ? '0' : '1',
Event.Flags.HasFlag(StoredEventFlags.Private) ? '1' : '0'
2022-08-30 15:00:58 +00:00
);
yield return sb.ToString();
}
}
}