namespace OubHackMS.Servers.Handlers
{
    using OubHackMS.Core;
    using OubHackMS.Crypto;
    using OubHackMS.Logging;
    using OubHackMS.Servers;
    using OubHackMS.Users;
    using System;
    using System.IO;
    using System.Net.Sockets;
    using System.Xml;

    internal class PacketRequestScripts : PacketHandler
    {
        private UserDB userdb;

        public PacketRequestScripts(OubHackMS parent) : base(parent)
        {
            base.sIndex = 8;
            this.userdb = (UserDB) base.parent.GetClass("UserDB");
        }

        public override void doWork(NetworkStream str)
        {
            BinaryReader reader = new BinaryReader(str);
            BinaryWriter writer = new BinaryWriter(str);
            User user = this.userdb.getBySessionID(reader.ReadInt64());
            if (user == null)
            {
                writer.Write(false);
            }
            else
            {
                writer.Write(true);
                base.parent.Log(Level.USER_LOG_FILE, new object[] { user.sUsername, "Requseted scripts." });
                if (File.Exists("filelist.xml"))
                {
                    XmlDocument document = new XmlDocument();
                    document.Load("filelist.xml");
                    foreach (XmlNode node in document.SelectNodes("/FileInfo/Scripts/*"))
                    {
                        try
                        {
                            string path = node.Attributes["path"].Value;
                            string str3 = "/trunk";
                            if (Directory.Exists(path.Replace("{PATH}", (user.bBranch == 1) ? "oubhack_v3l_beta" : "oubhack_v3l") + "/branches/" + user.sUsername))
                            {
                                str3 = "/branches/" + user.sUsername;
                            }
                            string newValue = (user.bBranch == 1) ? ("oubhack_v3l_beta" + str3) : ("oubhack_v3l" + str3);
                            path = path.Replace("{PATH}", newValue);
                            if (File.Exists(path))
                            {
                                byte[] bKey = user.bKey;
                                AES aes = new AES(user.bIV, bKey);
                                byte[] inArray = (node.Attributes["encrypted"].Value == "true") ? aes.encrypt(File.ReadAllBytes(path)) : File.ReadAllBytes(path);
                                Convert.ToBase64String(inArray).ToCharArray();
                                string str5 = path.Substring(("files/" + newValue).Length + 1, (path.Length - ("files/" + newValue).Length) - 1);
                                writer.Write(str5.Length);
                                writer.Write(str5.ToCharArray());
                                writer.Write(node.Attributes["description"].Value.Length);
                                writer.Write(node.Attributes["description"].Value.ToCharArray());
                                if (reader.ReadBoolean())
                                {
                                    int length = inArray.Length;
                                    int num2 = inArray.Length;
                                    int num3 = 0x400;
                                    writer.Write(num3);
                                    writer.Write(length);
                                    while (num2 > 0)
                                    {
                                        int count = (num2 >= num3) ? num3 : num2;
                                        writer.Write(inArray, length - num2, count);
                                        num2 -= count;
                                    }
                                }
                                base.parent.Log(Level.USER_LOG_FILE, new object[] { user.sUsername, "Sent script " + node.Attributes["path"].Value });
                            }
                        }
                        catch (Exception exception)
                        {
                            base.parent.Log(Level.USER_LOG_FILE_EXCEPTION, new object[] { user.sUsername, "Failed to script " + node.Attributes["path"].Value + "  -  " + exception.ToString() });
                        }
                    }
                    writer.Write(0);
                    writer.Write("".ToCharArray());
                }
            }
        }
    }
}

