<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace core.LinkHub
{
    class HubProcessor
    {
        public static void Eval(Leaf leaf, LinkMsg msg, TCPPacketReader packet, ulong time, LinkMode mode)
        {
            switch (msg)
            {
                case LinkMsg.MSG_LINK_LEAF_AVATAR:
                    LeafAvatar(leaf, packet);
                    break;

                case LinkMsg.MSG_LINK_LEAF_CUSTOM_NAME:
                    LeafCustomName(leaf, packet);
                    break;

                case LinkMsg.MSG_LINK_LEAF_EMOTE_TEXT:
                    LeafEmoteText(leaf, packet);
                    break;

                case LinkMsg.MSG_LINK_LEAF_LOGIN:
                    LeafLogin(leaf, packet, time, mode);
                    break;

                case LinkMsg.MSG_LINK_LEAF_PERSONAL_MESSAGE:
                    LeafPersonalMessage(leaf, packet);
                    break;

                case LinkMsg.MSG_LINK_LEAF_PRIVATE_IGNORED:
                    LeafPrivateIgnored(leaf, packet);
                    break;

                case LinkMsg.MSG_LINK_LEAF_PRIVATE_TEXT:
                    LeafPrivateText(leaf, packet);
                    break;

                case LinkMsg.MSG_LINK_LEAF_PUBLIC_TEXT:
                    LeafPublicText(leaf, packet);
                    break;

                case LinkMsg.MSG_LINK_LEAF_USERLIST_END:
                    LeafUserlistEnd(leaf);
                    break;

                case LinkMsg.MSG_LINK_LEAF_USERLIST_ITEM:
                    LeafUserlistItem(leaf, packet);
                    break;

                case LinkMsg.MSG_LINK_LEAF_JOIN:
                    LeafJoin(leaf, packet);
                    break;

                case LinkMsg.MSG_LINK_LEAF_PART:
                    LeafPart(leaf, packet);
                    break;

                case LinkMsg.MSG_LINK_LEAF_PING:
                    LeafPing(leaf, time);
                    break;

                case LinkMsg.MSG_LINK_LEAF_RELAY:
                    LeafRelay(leaf, packet);
                    break;

                case LinkMsg.MSG_LINK_LEAF_BROADCAST:
                    LeafBroadcast(leaf, packet);
                    break;

                case LinkMsg.MSG_LINK_LEAF_USER_UPDATED:
                    LeafUserUpdated(leaf, packet);
                    break;

                case LinkMsg.MSG_LINK_LEAF_NICK_CHANGED:
                    LeafNickChanged(leaf, packet);
                    break;

                case LinkMsg.MSG_LINK_LEAF_VROOM_CHANGED:
                    LeafVroomChanged(leaf, packet);
                    break;
            }
        }

        private static void LeafNickChanged(Leaf leaf, TCPPacketReader packet)
        {
            String name = packet.ReadString(leaf);
            LinkUser user = leaf.Users.Find(x => x.Name == name);

            if (user != null)
            {
                user.Name = packet.ReadString(leaf);

                LeafPool.Leaves.ForEachWhere(x => x.SendPacket(HubOutbound.HubNickChanged(x, leaf.Ident, name, user.Name)),
                    x => x.Ident != leaf.Ident && x.LoginPhase == LinkLogin.Ready);
            }
        }

        private static void LeafVroomChanged(Leaf leaf, TCPPacketReader packet)
        {
            String name = packet.ReadString(leaf);
            LinkUser user = leaf.Users.Find(x => x.Name == name);

            if (user != null)
            {
                user.Vroom = packet;

                LeafPool.Leaves.ForEachWhere(x => x.SendPacket(HubOutbound.HubVroomChanged(x, leaf.Ident, user)),
                    x => x.Ident != leaf.Ident && x.LoginPhase == LinkLogin.Ready);
            }
        }

        private static void LeafUserUpdated(Leaf leaf, TCPPacketReader packet)
        {
            String name = packet.ReadString(leaf);
            LinkUser user = leaf.Users.Find(x => x.Name == name);

            if (user != null)
            {
                user.Level = (iconnect.ILevel)((byte)packet);
                user.Muzzled = ((byte)packet) == 1;
                user.Registered = ((byte)packet) == 1;
                user.Idle = ((byte)packet) == 1;

                LeafPool.Leaves.ForEachWhere(x => x.SendPacket(HubOutbound.HubUserUpdated(x, leaf.Ident, user)),
                    x => x.Ident != leaf.Ident && x.LoginPhase == LinkLogin.Ready);
            }
        }

        private static void LeafRelay(Leaf leaf, TCPPacketReader packet)
        {
            uint target_leaf = packet;
            String target_name = packet.ReadString(leaf);
            byte[] data = packet;
            Leaf l = LeafPool.Leaves.Find(x => x.Ident == target_leaf && x.LoginPhase == LinkLogin.Ready);

            if (l != null)
                l.SendPacket(HubOutbound.HubRelay(l, leaf.Ident, target_name, data));
        }

        private static void LeafBroadcast(Leaf leaf, TCPPacketReader packet)
        {
            byte[] data = packet;

            LeafPool.Leaves.ForEachWhere(x => x.SendPacket(HubOutbound.HubBroadcast(x, leaf.Ident, data)),
                x => x.Ident != leaf.Ident && x.LoginPhase == LinkLogin.Ready);
        }

        private static void LeafEmoteText(Leaf leaf, TCPPacketReader packet)
        {
            String name = packet.ReadString(leaf);
            String text = packet.ReadString(leaf);

            LeafPool.Leaves.ForEachWhere(x => x.SendPacket(HubOutbound.HubEmoteText(x, leaf.Ident, name, text)),
                x => x.Ident != leaf.Ident && x.LoginPhase == LinkLogin.Ready);
        }

        private static void LeafPublicText(Leaf leaf, TCPPacketReader packet)
        {
            String name = packet.ReadString(leaf);
            String text = packet.ReadString(leaf);

            LeafPool.Leaves.ForEachWhere(x => x.SendPacket(HubOutbound.HubPublicText(x, leaf.Ident, name, text)),
                x => x.Ident != leaf.Ident && x.LoginPhase == LinkLogin.Ready);
        }

        private static void LeafPrivateText(Leaf leaf, TCPPacketReader packet)
        {
            uint target_leaf = packet;
            String target_name = packet.ReadString(leaf);
            String send_name = packet.ReadString(leaf);
            String text = packet.ReadString(leaf);
            Leaf l = LeafPool.Leaves.Find(x => x.Ident == target_leaf && x.LoginPhase == LinkLogin.Ready);

            if (l != null)
                l.SendPacket(HubOutbound.HubPrivateText(l, leaf.Ident, send_name, target_name, text));
        }

        private static void LeafPrivateIgnored(Leaf leaf, TCPPacketReader packet)
        {
            uint sender_leaf = packet;
            String target = packet.ReadString(leaf);
            String sender = packet.ReadString(leaf);
            Leaf l = LeafPool.Leaves.Find(x => x.Ident == sender_leaf && x.LoginPhase == LinkLogin.Ready);

            if (l != null)
                l.SendPacket(HubOutbound.HubPrivateIgnored(l, leaf.Ident, sender, target));
        }

        private static void LeafJoin(Leaf leaf, TCPPacketReader packet)
        {<commit_msg>Protocol enforcement.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace core.LinkHub
{
    class HubProcessor
    {
        public static void Eval(Leaf leaf, LinkMsg msg, TCPPacketReader packet, ulong time, LinkMode mode)
        {
            switch (msg)
            {
                case LinkMsg.MSG_LINK_LEAF_AVATAR:
                    LeafAvatar(leaf, packet);
                    break;

                case LinkMsg.MSG_LINK_LEAF_CUSTOM_NAME:
                    LeafCustomName(leaf, packet);
                    break;

                case LinkMsg.MSG_LINK_LEAF_EMOTE_TEXT:
                    LeafEmoteText(leaf, packet);
                    break;

                case LinkMsg.MSG_LINK_LEAF_LOGIN:
                    LeafLogin(leaf, packet, time, mode);
                    break;

                case LinkMsg.MSG_LINK_LEAF_PERSONAL_MESSAGE:
                    LeafPersonalMessage(leaf, packet);
                    break;

                case LinkMsg.MSG_LINK_LEAF_PRIVATE_IGNORED:
                    LeafPrivateIgnored(leaf, packet);
                    break;

                case LinkMsg.MSG_LINK_LEAF_PRIVATE_TEXT:
                    LeafPrivateText(leaf, packet);
                    break;

                case LinkMsg.MSG_LINK_LEAF_PUBLIC_TEXT:
                    LeafPublicText(leaf, packet);
                    break;

                case LinkMsg.MSG_LINK_LEAF_USERLIST_END:
                    LeafUserlistEnd(leaf);
                    break;

                case LinkMsg.MSG_LINK_LEAF_USERLIST_ITEM:
                    LeafUserlistItem(leaf, packet);
                    break;

                case LinkMsg.MSG_LINK_LEAF_JOIN:
                    LeafJoin(leaf, packet);
                    break;

                case LinkMsg.MSG_LINK_LEAF_PART:
                    LeafPart(leaf, packet);
                    break;

                case LinkMsg.MSG_LINK_LEAF_PING:
                    LeafPing(leaf, time);
                    break;

                case LinkMsg.MSG_LINK_LEAF_RELAY:
                    LeafRelay(leaf, packet);
                    break;

                case LinkMsg.MSG_LINK_LEAF_BROADCAST:
                    LeafBroadcast(leaf, packet);
                    break;

                case LinkMsg.MSG_LINK_LEAF_USER_UPDATED:
                    LeafUserUpdated(leaf, packet);
                    break;

                case LinkMsg.MSG_LINK_LEAF_NICK_CHANGED:
                    LeafNickChanged(leaf, packet);
                    break;

                case LinkMsg.MSG_LINK_LEAF_VROOM_CHANGED:
                    LeafVroomChanged(leaf, packet);
                    break;
            }
        }

        private static void LeafNickChanged(Leaf leaf, TCPPacketReader packet)
        {
            if (leaf.LoginPhase != LinkLogin.Ready)
            {
                leaf.SendPacket(HubOutbound.LinkError(LinkError.BadProtocol));
                leaf.Disconnect();
                return;
            }

            String name = packet.ReadString(leaf);
            LinkUser user = leaf.Users.Find(x => x.Name == name);

            if (user != null)
            {
                user.Name = packet.ReadString(leaf);

                LeafPool.Leaves.ForEachWhere(x => x.SendPacket(HubOutbound.HubNickChanged(x, leaf.Ident, name, user.Name)),
                    x => x.Ident != leaf.Ident && x.LoginPhase == LinkLogin.Ready);
            }
        }

        private static void LeafVroomChanged(Leaf leaf, TCPPacketReader packet)
        {
            if (leaf.LoginPhase != LinkLogin.Ready)
            {
                leaf.SendPacket(HubOutbound.LinkError(LinkError.BadProtocol));
                leaf.Disconnect();
                return;
            }

            String name = packet.ReadString(leaf);
            LinkUser user = leaf.Users.Find(x => x.Name == name);

            if (user != null)
            {
                user.Vroom = packet;

                LeafPool.Leaves.ForEachWhere(x => x.SendPacket(HubOutbound.HubVroomChanged(x, leaf.Ident, user)),
                    x => x.Ident != leaf.Ident && x.LoginPhase == LinkLogin.Ready);
            }
        }

        private static void LeafUserUpdated(Leaf leaf, TCPPacketReader packet)
        {
            if (leaf.LoginPhase != LinkLogin.Ready)
            {
                leaf.SendPacket(HubOutbound.LinkError(LinkError.BadProtocol));
                leaf.Disconnect();
                return;
            }

            String name = packet.ReadString(leaf);
            LinkUser user = leaf.Users.Find(x => x.Name == name);

            if (user != null)
            {
                user.Level = (iconnect.ILevel)((byte)packet);
                user.Muzzled = ((byte)packet) == 1;
                user.Registered = ((byte)packet) == 1;
                user.Idle = ((byte)packet) == 1;

                LeafPool.Leaves.ForEachWhere(x => x.SendPacket(HubOutbound.HubUserUpdated(x, leaf.Ident, user)),
                    x => x.Ident != leaf.Ident && x.LoginPhase == LinkLogin.Ready);
            }
        }

        private static void LeafRelay(Leaf leaf, TCPPacketReader packet)
        {
            if (leaf.LoginPhase != LinkLogin.Ready)
            {
                leaf.SendPacket(HubOutbound.LinkError(LinkError.BadProtocol));
                leaf.Disconnect();
                return;
            }

            uint target_leaf = packet;
            String target_name = packet.ReadString(leaf);
            byte[] data = packet;
            Leaf l = LeafPool.Leaves.Find(x => x.Ident == target_leaf && x.LoginPhase == LinkLogin.Ready);

            if (l != null)
                l.SendPacket(HubOutbound.HubRelay(l, leaf.Ident, target_name, data));
        }

        private static void LeafBroadcast(Leaf leaf, TCPPacketReader packet)
        {
            if (leaf.LoginPhase != LinkLogin.Ready)
            {
                leaf.SendPacket(HubOutbound.LinkError(LinkError.BadProtocol));
                leaf.Disconnect();
                return;
            }

            byte[] data = packet;

            LeafPool.Leaves.ForEachWhere(x => x.SendPacket(HubOutbound.HubBroadcast(x, leaf.Ident, data)),
                x => x.Ident != leaf.Ident && x.LoginPhase == LinkLogin.Ready);
        }

        private static void LeafEmoteText(Leaf leaf, TCPPacketReader packet)
        {
            if (leaf.LoginPhase != LinkLogin.Ready)
            {
                leaf.SendPacket(HubOutbound.LinkError(LinkError.BadProtocol));
                leaf.Disconnect();
                return;
            }

            String name = packet.ReadString(leaf);
            String text = packet.ReadString(leaf);

            LeafPool.Leaves.ForEachWhere(x => x.SendPacket(HubOutbound.HubEmoteText(x, leaf.Ident, name, text)),
                x => x.Ident != leaf.Ident && x.LoginPhase == LinkLogin.Ready);
        }

        private static void LeafPublicText(Leaf leaf, TCPPacketReader packet)
        {
            if (leaf.LoginPhase != LinkLogin.Ready)
            {
                leaf.SendPacket(HubOutbound.LinkError(LinkError.BadProtocol));
                leaf.Disconnect();
                return;
            }

            String name = packet.ReadString(leaf);
            String text = packet.ReadString(leaf);

            LeafPool.Leaves.ForEachWhere(x => x.SendPacket(HubOutbound.HubPublicText(x, leaf.Ident, name, text)),
                x => x.Ident != leaf.Ident && x.LoginPhase == LinkLogin.Ready);
        }

        private static void LeafPrivateText(Leaf leaf, TCPPacketReader packet)
        {
            if (leaf.LoginPhase != LinkLogin.Ready)
            {
                leaf.SendPacket(HubOutbound.LinkError(LinkError.BadProtocol));
                leaf.Disconnect();
                return;
            }

            uint target_leaf = packet;
            String target_name = packet.ReadString(leaf);
            String send_name = packet.ReadString(leaf);
            String text = packet.ReadString(leaf);
            Leaf l = LeafPool.Leaves.Find(x => x.Ident == target_leaf && x.LoginPhase == LinkLogin.Ready);

            if (l != null)
                l.SendPacket(HubOutbound.HubPrivateText(l, leaf.Ident, send_name, target_name, text));
        }

        private static void LeafPrivateIgnored(Leaf leaf, TCPPacketReader packet)
        {
            if (leaf.LoginPhase != LinkLogin.Ready)
            {
                leaf.SendPacket(HubOutbound.LinkError(LinkError.BadProtocol));
                leaf.Disconnect();
                return;
            }

            uint sender_leaf = packet;
            String target = packet.ReadString(leaf);
            String sender = packet.ReadString(leaf);
            Leaf l = LeafPool.Leaves.Find(x => x.Ident == sender_leaf && x.LoginPhase == LinkLogin.Ready);

            if (l != null)
                l.SendPacket(HubOutbound.HubPrivateIgnored(l, leaf.Ident, sender, target));
        }

        private static void LeafJoin(Leaf leaf, TCPPacketReader packet)
        {
{'fraction_non_alphanumeric': 0.09237220201023617, 'fraction_numerical': 0.000369982117530986, 'mean_word_length': 1.5360437842064112, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4093880', 'n_tokens_mistral': 4545, 'n_tokens_neox': 4268, 'n_words': 817}