sharp-chat/SharpChat/Packet/ContextMessagePacket.cs

104 lines
3.7 KiB
C#

using SharpChat.EventStorage;
using System;
using System.Collections.Generic;
using System.Text;
namespace SharpChat.Packet
{
public class ContextMessagePacket : ServerPacket {
public StoredEventInfo Event { get; private set; }
public bool Notify { get; private set; }
public ContextMessagePacket(StoredEventInfo evt, bool notify = false) {
Event = evt ?? throw new ArgumentNullException(nameof(evt));
Notify = notify;
}
private const string V1_CHATBOT = "-1\tChatBot\tinherit\t\t";
public override IEnumerable<string> Pack() {
StringBuilder sb = new();
sb.Append('7');
sb.Append('\t');
sb.Append('1');
sb.Append('\t');
sb.Append(Event.Created.ToUnixTimeSeconds());
sb.Append('\t');
switch(Event.Type) {
case "msg:add":
case "SharpChat.Events.ChatMessage":
sb.Append(Event.Sender.Pack());
sb.Append('\t');
sb.Append(
Event.Data.RootElement.GetProperty("text").GetString()
.Replace("<", "&lt;")
.Replace(">", "&gt;")
.Replace("\n", " <br/> ")
.Replace("\t", " ")
);
break;
case "user:connect":
case "SharpChat.Events.UserConnectEvent":
sb.Append(V1_CHATBOT);
sb.Append("0\fjoin\f");
sb.Append(Event.Sender.UserName);
break;
case "chan:join":
case "SharpChat.Events.UserChannelJoinEvent":
sb.Append(V1_CHATBOT);
sb.Append("0\fjchan\f");
sb.Append(Event.Sender.UserName);
break;
case "chan:leave":
case "SharpChat.Events.UserChannelLeaveEvent":
sb.Append(V1_CHATBOT);
sb.Append("0\flchan\f");
sb.Append(Event.Sender.UserName);
break;
case "user:disconnect":
case "SharpChat.Events.UserDisconnectEvent":
sb.Append(V1_CHATBOT);
sb.Append("0\f");
switch((UserDisconnectReason)Event.Data.RootElement.GetProperty("reason").GetByte()) {
case UserDisconnectReason.Flood:
sb.Append("flood");
break;
case UserDisconnectReason.Kicked:
sb.Append("kick");
break;
case UserDisconnectReason.TimeOut:
sb.Append("timeout");
break;
case UserDisconnectReason.Leave:
default:
sb.Append("leave");
break;
}
sb.Append('\f');
sb.Append(Event.Sender.UserName);
break;
}
sb.Append('\t');
sb.Append(Event.Id < 1 ? SequenceId : Event.Id);
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'
);
yield return sb.ToString();
}
}
}