using SharpChat.EventStorage; using System; using System.Text; namespace SharpChat.Packet { // this entire class is disgusting public class MessagePopulatePacket : ServerPacket { private readonly StoredEventInfo Event; private readonly bool Notify; public MessagePopulatePacket(StoredEventInfo evt, bool notify = false) { Event = evt ?? throw new ArgumentNullException(nameof(evt)); Notify = notify; } private const string V1_CHATBOT = "-1\tChatBot\tinherit\t"; 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); StringBuilder sb = new(); sb.AppendFormat("7\t1\t{0}\t", Event.Created.ToUnixTimeSeconds()); switch(Event.Type) { case "msg:add": if(isBroadcast) { sb.Append(V1_CHATBOT); } else { 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) == true ? 1 : 0, Event.Sender?.Can(ChatUserPermissions.ViewLogs) == true ? 1 : 0, Event.Sender?.Can(ChatUserPermissions.SetOwnNickname) == true ? 1 : 0, Event.Sender?.Can(ChatUserPermissions.CreateChannel | ChatUserPermissions.SetChannelPermanent, true) == true ? 2 : ( Event.Sender?.Can(ChatUserPermissions.CreateChannel) == true ? 1 : 0 ) ); } sb.Append('\t'); if(isBroadcast) sb.Append("0\fsay\f"); string body = SharpUtil.Sanitise(Event.Data.RootElement.GetProperty("text").GetString()); if(isAction) body = string.Format("{0}", body); sb.Append(body); break; case "user:connect": sb.AppendFormat("{0}\t0\fjoin\f{1}", V1_CHATBOT, Event.Sender?.LegacyName); break; case "chan:join": sb.AppendFormat("{0}\t0\fjchan\f{1}", V1_CHATBOT, Event.Sender?.LegacyName); break; case "chan:leave": sb.AppendFormat("{0}\t0\flchan\f{1}", V1_CHATBOT, Event.Sender?.LegacyName); break; case "user:disconnect": 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 ); break; } sb.AppendFormat( "\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 ); return sb.ToString(); } } }