public static void AddQueue(ScriptList ListItems, UUID Requester, string GitOwner = "OpenCollarTeam", string GitBranch = "master")
        {
            Queue inst = Queue.Instance;
            List <Queue.QueueType> Queued = new List <Queue.QueueType>();

            foreach (KeyValuePair <string, ScriptList.ScriptListFlags> kvp in ListItems.Scripts)
            {
                Queue.QueueType QT = new Queue.QueueType();
                QT.Name     = kvp.Value.ScriptName;
                QT.GitOwner = GitOwner;

                HttpWebRequest  hwr    = null;
                HttpWebResponse hwresp = null;

                try
                {
                    hwr = (HttpWebRequest)HttpWebRequest.Create("https://raw.githubusercontent.com/" + GitOwner + "/OpenCollar/" + GitBranch + "/src/" + kvp.Value.Container + "/" + kvp.Value.ScriptName + kvp.Value.FileExt);

                    hwr.Method = "GET";
                    hwresp     = (HttpWebResponse)hwr.GetResponse();
                }
                catch (Exception e) { }

                if (hwresp.StatusCode == HttpStatusCode.NotFound)
                {
                    MH(Destinations.DEST_AGENT, Requester, "ALERT: BDF Entry: " + kvp.Value.ScriptName + kvp.Value.FileExt + "; Does not exist on the server!");
                    continue;
                }
                else if (hwresp.StatusCode == HttpStatusCode.OK)
                {
                    // Get the file text and add to queued items
                    StreamReader sr = new StreamReader(hwresp.GetResponseStream());
                    QT.Text = sr.ReadToEnd();
                    //Console.WriteLine("Generating ZHX for string with length : "+QT.Text.Length.ToString());
                    Console.WriteLine("MD5 [" + QT.Name + "] : " + Tools.MD5Hash(QT.Text));
                    QT.Hash = Tools.MD5Hash(QT.Text);
                    //Console.WriteLine("ZHX [" + QT.Name + "] : " + QT.Hash);
                    QT.GitBranch = GitBranch;
                    QT.Container = kvp.Value.Container;
                    QT.ItemType  = kvp.Value.Type;
                    QT.FileExt   = kvp.Value.FileExt;

                    Queued.Add(QT);
                }
            }

            inst.ActualQueue.Add(Requester, Queued);
        }
Esempio n. 2
0
        // This class executes 1 queue item when called upon

        public static void run()
        {
            Queue X = Queue.Instance;

            // Get first queued item

            Queue.Instance.invItem = null;

            if (X.ActualQueue.Count == 0)
            {
                return;
            }
            KeyValuePair <UUID, List <Queue.QueueType> > kvp = X.ActualQueue.First();

            InventoryManager inv            = BotSession.Instance.grid.Inventory;
            UUID             GitOwnerFolder = UUID.Zero;
            UUID             Branch         = UUID.Zero;
            UUID             Recipient      = kvp.Key;
            UUID             Container      = UUID.Zero;

            UUID ParentFolder = inv.FindFolderForType(AssetType.Folder);


            Queue.QueueType QT = kvp.Value.First();

            InventoryBase GitOwnerFolderBase = null;

            try
            {
                GitOwnerFolderBase = inv.LocalFind(ParentFolder, new[] { QT.GitOwner }, 0, false).First();

                GitOwnerFolder = GitOwnerFolderBase.UUID;
            } catch (Exception e)
            {
            }

            if (GitOwnerFolder == UUID.Zero || GitOwnerFolder == ParentFolder)
            {
                GitOwnerFolder = inv.CreateFolder(ParentFolder, QT.GitOwner);
            }

            InventoryBase GitBranchBase = null;

            try
            {
                GitBranchBase = inv.LocalFind(GitOwnerFolder, new[] { QT.GitBranch }, 0, false).First();

                Branch = GitBranchBase.UUID;
            }
            catch (Exception e) { }
            if (Branch == UUID.Zero || Branch == ParentFolder)
            {
                Branch = inv.CreateFolder(GitOwnerFolder, QT.GitBranch);
            }

            InventoryBase GitContainer = null;

            try
            {
                GitContainer = inv.LocalFind(Branch, new[] { QT.Container }, 0, false).First();

                Container = GitContainer.UUID;
            }catch (Exception e) { }
            if (Container == UUID.Zero || Container == ParentFolder)
            {
                Container = inv.CreateFolder(Branch, QT.Container);
            }

            Queue.FinalQueueData Finals = new Queue.FinalQueueData();
            Dictionary <UUID, Queue.FinalQueueData> FQI = Queue.Instance.FinalQueue;

            if (FQI.ContainsKey(Recipient))
            {
                Finals = FQI[Recipient];
                if (Finals.Folders.ContainsKey(QT.Container))
                {
                    // do nothing
                }
                else
                {
                    Finals.Folders.Add(QT.Container, Container);
                }
                FQI[Recipient]            = Finals;
                Queue.Instance.FinalQueue = FQI;
            }

            Console.WriteLine("Git Owner Folder: " + GitOwnerFolder.ToString());
            Console.WriteLine("Branch: " + Branch.ToString());
            Console.WriteLine("Container: " + Container.ToString());

            // Locate the Script or Notecard
            InventoryBase importedItem = null;
            UUID          ItemID       = UUID.Zero;

            try
            {
                if (QT.ItemType == "script")
                {
                    importedItem = inv.LocalFind(Container, new[] { QT.Name }, 1, true).First();
                }
                else if (QT.ItemType == "notecard")
                {
                    importedItem = inv.LocalFind(Container, new[] { QT.Name + QT.FileExt }, 1, false).First();
                }
                ItemID = importedItem.UUID;
            }
            catch (Exception e) { }

            if (ItemID == UUID.Zero || ItemID == null)
            {
                Queue.Instance.invItem = null;
            }
            else
            {
                Queue.Instance.ARE = new AutoResetEvent(false);
                inv.ItemReceived  += Inv_ItemReceived;

                while (!Queue.Instance.ARE.WaitOne(TimeSpan.FromMinutes(1)))
                {
                    Queue.Instance.ARE.Reset();
                    inv.RequestFetchInventory(ItemID, importedItem.OwnerID);
                }
            }

            // Check that invItem is instantiated

            AssetType     itemType = AssetType.Folder;
            InventoryType InvType  = InventoryType.Animation;

            if (QT.ItemType == "notecard")
            {
                InvType  = InventoryType.Notecard;
                itemType = AssetType.Notecard;
            }
            else if (QT.ItemType == "script")
            {
                InvType  = InventoryType.LSL;
                itemType = AssetType.LSLText;
            }

            if (Queue.Instance.invItem == null)
            {
                // We can safely create the inventory item now

                string FinalName = QT.Name;
                if (InvType == InventoryType.Notecard)
                {
                    FinalName += QT.FileExt;
                }
                UUID Transaction = UUID.Random();
                inv.RequestCreateItem(Container, FinalName, QT.Hash, itemType, Transaction, InvType, PermissionMask.All, delegate(bool success, InventoryItem ii)
                {
                    if (success)
                    {
                        Queue.Instance.invItem = ii;
                        Queue.Instance.ARE.Set();
                    }
                });

                Queue.Instance.ARE.WaitOne(TimeSpan.FromSeconds(10));
                InventoryItem actualItem = Queue.Instance.invItem;
                if (itemType == AssetType.LSLText)
                {
                    inv.RequestUpdateScriptAgentInventory(EncodeScript(QT.Text), actualItem.UUID, true, delegate(bool uploaded, string uploadstatus, bool compileSuccess, List <string> msgs, UUID itemID, UUID assetID)
                    {
                        Queue.Instance.compileMessages = msgs;
                        Queue.Instance.CompileSuccess  = compileSuccess;
                        Queue.Instance.UploadSuccess   = uploaded;
                        Queue.Instance.ARE.Set();
                    });

                    Queue.Instance.ARE.WaitOne(TimeSpan.FromSeconds(20));

                    if (Queue.Instance.UploadSuccess)
                    {
                        if (Queue.Instance.CompileSuccess)
                        {
                            // Remove this from future queues
                            kvp.Value.Remove(QT);
                            // quickly check whether KVP.Value is empty
                            if (kvp.Value.Count == 0)
                            {
                                // Send the container folder, and remove this from queue
                                //inv.GiveFolder(Branch, GitBranchBase.Name, Recipient, false);
                                //List<InventoryBase> folders = inv.FolderContents(Branch, BotSession.Instance.grid.Self.AgentID, true, false, InventorySortOrder.ByName, TimeSpan.FromSeconds(30).Milliseconds);

                                if (Recipient != UUID.Zero)
                                {
                                    foreach (KeyValuePair <string, UUID> kvpx in Finals.Folders)
                                    {
                                        inv.GiveFolder(kvpx.Value, kvpx.Key, Recipient, false);
                                        MH(Destinations.DEST_AGENT, Recipient, "Sending : " + kvpx.Key);
                                    }
                                    Queue.Instance.FinalQueue.Remove(Recipient);
                                }

                                X.ActualQueue.Remove(Recipient);
                            }
                            else
                            {
                                X.ActualQueue[Recipient] = kvp.Value;
                            }
                            if (Recipient != UUID.Zero)
                            {
                                MH(Destinations.DEST_AGENT, Recipient, "Item '" + QT.Name + "' finished processing");
                            }
                        }
                    }
                    else
                    {
                        if (Recipient != UUID.Zero)
                        {
                            MH(Destinations.DEST_AGENT, Recipient, "Item: " + QT.Name + QT.FileExt + " failed!");
                        }
                    }
                }
                else if (itemType == AssetType.Notecard)
                {
                    inv.RequestUploadNotecardAsset(Encoding.UTF8.GetBytes(QT.Text), actualItem.UUID, delegate(bool success, string status, UUID itemID, UUID assetID)
                    {
                        if (success)
                        {
                            Queue.Instance.UploadSuccess = success;
                            Queue.Instance.ARE.Set();
                        }
                    });

                    if (Queue.Instance.UploadSuccess)
                    {
                        kvp.Value.Remove(QT);
                        if (kvp.Value.Count == 0)
                        {
                            X.ActualQueue.Remove(Recipient);
                            //                            inv.GiveFolder(Branch, GitBranchBase.Name, Recipient, false);

                            if (Recipient != UUID.Zero)
                            {
                                foreach (KeyValuePair <string, UUID> kvpx in Finals.Folders)
                                {
                                    inv.GiveFolder(kvpx.Value, kvpx.Key, Recipient, false);
                                    MH(Destinations.DEST_AGENT, Recipient, "Sending : " + kvpx.Key);
                                }
                                Queue.Instance.FinalQueue.Remove(Recipient);
                            }
                        }
                        else
                        {
                            X.ActualQueue[Recipient] = kvp.Value;
                        }
                        if (Recipient != UUID.Zero)
                        {
                            MH(Destinations.DEST_AGENT, Recipient, "Item '" + QT.Name + "' finished processing");
                        }
                    }
                    else
                    {
                        if (Recipient != UUID.Zero)
                        {
                            MH(Destinations.DEST_AGENT, Recipient, "Item: " + QT.Name + QT.FileExt + " failed!");
                        }
                    }
                }
            }
            else
            {
                // Item exists, verify hash data!
                if (itemType == AssetType.LSLText)
                {
                    if (Queue.Instance.invItem.Description == QT.Hash)
                    {
                        // remove from queue
                        kvp.Value.Remove(QT);
                        if (kvp.Value.Count == 0)
                        {
                            if (Recipient != UUID.Zero)
                            {
                                foreach (KeyValuePair <string, UUID> kvpx in Finals.Folders)
                                {
                                    inv.GiveFolder(kvpx.Value, kvpx.Key, Recipient, false);
                                    MH(Destinations.DEST_AGENT, Recipient, "Sending : " + kvpx.Key);
                                }
                                Queue.Instance.FinalQueue.Remove(Recipient);
                            }
                            X.ActualQueue.Remove(Recipient);
                            //inv.GiveFolder(Branch, GitBranchBase.Name, Recipient, false);
                        }
                        else
                        {
                            X.ActualQueue[Recipient] = kvp.Value;
                        }
                        if (Recipient != UUID.Zero)
                        {
                            MH(Destinations.DEST_AGENT, Recipient, "Item '" + QT.Name + "' finished processing - no changes needed");
                        }
                    }
                    else
                    {
                        // update the script now
                        inv.RequestUpdateScriptAgentInventory(EncodeScript(QT.Text), Queue.Instance.invItem.UUID, true, delegate(bool uploaded, string uploadstatus, bool compileSuccess, List <string> msgs, UUID itemID, UUID assetID)
                        {
                            Queue.Instance.compileMessages = msgs;
                            Queue.Instance.CompileSuccess  = compileSuccess;
                            Queue.Instance.UploadSuccess   = uploaded;
                            Queue.Instance.ARE.Set();
                        });

                        Queue.Instance.ARE.WaitOne(TimeSpan.FromSeconds(15));

                        if (Queue.Instance.UploadSuccess)
                        {
                            if (Queue.Instance.CompileSuccess)
                            {
                                X.invItem.Description = QT.Hash;
                                kvp.Value.Remove(QT);
                                if (kvp.Value.Count == 0)
                                {
                                    if (Recipient != UUID.Zero)
                                    {
                                        foreach (KeyValuePair <string, UUID> kvpx in Finals.Folders)
                                        {
                                            inv.GiveFolder(kvpx.Value, kvpx.Key, Recipient, false);
                                            MH(Destinations.DEST_AGENT, Recipient, "Sending : " + kvpx.Key);
                                        }
                                        Queue.Instance.FinalQueue.Remove(Recipient);
                                    }
                                    X.ActualQueue.Remove(Recipient);
                                    //inv.GiveFolder(Branch, GitBranchBase.Name, Recipient, false);
                                }
                                else
                                {
                                    X.ActualQueue[Recipient] = kvp.Value;
                                }
                                if (Recipient != UUID.Zero)
                                {
                                    MH(Destinations.DEST_AGENT, Recipient, "Item '" + QT.Name + "' finished processing");
                                }
                            }
                        }
                        else
                        {
                            if (Recipient != UUID.Zero)
                            {
                                MH(Destinations.DEST_AGENT, Recipient, "Item: " + QT.Name + QT.FileExt + " failed!");
                            }
                        }
                    }
                }
                else if (itemType == AssetType.Notecard)
                {
                    if (Queue.Instance.invItem.Description == QT.Hash)
                    {
                        kvp.Value.Remove(QT);
                        if (kvp.Value.Count == 0)
                        {
                            if (Recipient != UUID.Zero)
                            {
                                foreach (KeyValuePair <string, UUID> kvpx in Finals.Folders)
                                {
                                    inv.GiveFolder(kvpx.Value, kvpx.Key, Recipient, false);
                                    MH(Destinations.DEST_AGENT, Recipient, "Sending : " + kvpx.Key);
                                }
                                Queue.Instance.FinalQueue.Remove(Recipient);
                            }
                            X.ActualQueue.Remove(Recipient);
                            //inv.GiveFolder(Branch, GitBranchBase.Name, Recipient, false);
                        }
                        else
                        {
                            X.ActualQueue[Recipient] = kvp.Value;
                        }
                        if (Recipient != UUID.Zero)
                        {
                            MH(Destinations.DEST_AGENT, Recipient, "Item '" + QT.Name + "' finished processing - no changes needed");
                        }
                    }
                    else
                    {
                        inv.RequestUploadNotecardAsset(EncodeScript(QT.Text), Queue.Instance.invItem.UUID, delegate(bool success, string status, UUID itemID, UUID assetID)
                        {
                            Queue.Instance.UploadSuccess = success;
                            Queue.Instance.ARE.Set();
                        });

                        Queue.Instance.ARE.WaitOne(TimeSpan.FromSeconds(15));

                        if (Queue.Instance.UploadSuccess)
                        {
                            // Notecard updated
                            kvp.Value.Remove(QT);
                            if (kvp.Value.Count == 0)
                            {
                                if (Recipient != UUID.Zero)
                                {
                                    foreach (KeyValuePair <string, UUID> kvpx in Finals.Folders)
                                    {
                                        inv.GiveFolder(kvpx.Value, kvpx.Key, Recipient, false);
                                        MH(Destinations.DEST_AGENT, Recipient, "Sending : " + kvpx.Key);
                                    }
                                    Queue.Instance.FinalQueue.Remove(Recipient);
                                }
                                X.ActualQueue.Remove(Recipient);
                                //inv.GiveFolder(Branch, GitBranchBase.Name, Recipient, false);
                            }
                            else
                            {
                                X.ActualQueue[Recipient] = kvp.Value;
                            }
                            if (Recipient != UUID.Zero)
                            {
                                MH(Destinations.DEST_AGENT, Recipient, "Item '" + QT.Name + "' finished processing");
                            }
                        }
                        else
                        {
                            if (Recipient != UUID.Zero)
                            {
                                MH(Destinations.DEST_AGENT, Recipient, "Item: " + QT.Name + QT.FileExt + " failed!");
                            }
                        }
                    }
                }
            }

            X.ARE             = new AutoResetEvent(false);
            X.compileMessages = new List <string>();
            X.CompileSuccess  = false;
            X.invItem         = null;
            X.UploadSuccess   = false;
        }