OpenSim.Region.Framework.Scenes.Scene.GiveInventoryItem C# (CSharp) Method

GiveInventoryItem() public method

Give an inventory item from one user to another
public GiveInventoryItem ( UUID recipient, UUID senderId, UUID itemId, UUID recipientFolderId ) : InventoryItemBase
recipient UUID
senderId UUID ID of the sender of the item
itemId UUID
recipientFolderId UUID /// The id of the folder in which the copy item should go. If UUID.Zero then the item is placed in the most /// appropriate default folder. ///
return InventoryItemBase
        public virtual InventoryItemBase GiveInventoryItem(
            UUID recipient, UUID senderId, UUID itemId, UUID recipientFolderId)
        {
            //Console.WriteLine("Scene.Inventory.cs: GiveInventoryItem");

            InventoryItemBase item = new InventoryItemBase(itemId, senderId);
            item = InventoryService.GetItem(item);

            if ((item != null) && (item.Owner == senderId))
            {
                if (!Permissions.BypassPermissions())
                {
                    if ((item.CurrentPermissions & (uint)PermissionMask.Transfer) == 0)
                        return null;
                }

                // Insert a copy of the item into the recipient
                InventoryItemBase itemCopy = new InventoryItemBase();
                itemCopy.Owner = recipient;
                itemCopy.CreatorId = item.CreatorId;
                itemCopy.ID = UUID.Random();
                itemCopy.AssetID = item.AssetID;
                itemCopy.Description = item.Description;
                itemCopy.Name = item.Name;
                itemCopy.AssetType = item.AssetType;
                itemCopy.InvType = item.InvType;
                itemCopy.Folder = recipientFolderId;

                if (Permissions.PropagatePermissions() && recipient != senderId)
                {
                    // Trying to do this right this time. This is evil. If
                    // you believe in Good, go elsewhere. Vampires and other
                    // evil creatores only beyond this point. You have been
                    // warned.

                    // We're going to mask a lot of things by the next perms
                    // Tweak the next perms to be nicer to our data
                    //
                    // In this mask, all the bits we do NOT want to mess
                    // with are set. These are:
                    //
                    // Transfer
                    // Copy
                    // Modufy
                    uint permsMask = ~ ((uint)PermissionMask.Copy |
                                        (uint)PermissionMask.Transfer |
                                        (uint)PermissionMask.Modify);

                    // Now, reduce the next perms to the mask bits
                    // relevant to the operation
                    uint nextPerms = permsMask | (item.NextPermissions &
                                      ((uint)PermissionMask.Copy |
                                       (uint)PermissionMask.Transfer |
                                       (uint)PermissionMask.Modify));

                    // nextPerms now has all bits set, except for the actual
                    // next permission bits.

                    // This checks for no mod, no copy, no trans.
                    // This indicates an error or messed up item. Do it like
                    // SL and assume trans
                    if (nextPerms == permsMask)
                        nextPerms |= (uint)PermissionMask.Transfer;

                    // Inventory owner perms are the logical AND of the
                    // folded perms and the root prim perms, however, if
                    // the root prim is mod, the inventory perms will be
                    // mod. This happens on "take" and is of little concern
                    // here, save for preventing escalation

                    // This hack ensures that items previously permalocked
                    // get unlocked when they're passed or rezzed
                    uint basePerms = item.BasePermissions |
                                    (uint)PermissionMask.Move;
                    uint ownerPerms = item.CurrentPermissions;

                    // If this is an object, root prim perms may be more
                    // permissive than folded perms. Use folded perms as
                    // a mask
                    if (item.InvType == (int)InventoryType.Object)
                    {
                        // Create a safe mask for the current perms
                        uint foldedPerms = (item.CurrentPermissions & 7) << 13;
                        foldedPerms |= permsMask;

                        bool isRootMod = (item.CurrentPermissions &
                                          (uint)PermissionMask.Modify) != 0 ?
                                          true : false;

                        // Mask the owner perms to the folded perms
                        ownerPerms &= foldedPerms;
                        basePerms &= foldedPerms;

                        // If the root was mod, let the mask reflect that
                        // We also need to adjust the base here, because
                        // we should be able to edit in-inventory perms
                        // for the root prim, if it's mod.
                        if (isRootMod)
                        {
                            ownerPerms |= (uint)PermissionMask.Modify;
                            basePerms |= (uint)PermissionMask.Modify;
                        }
                    }

                    // These will be applied to the root prim at next rez.
                    // The slam bit (bit 3) and folded permission (bits 0-2)
                    // are preserved due to the above mangling
                    ownerPerms &= nextPerms;

                    // Mask the base permissions. This is a conservative
                    // approach altering only the three main perms
                    basePerms &= nextPerms;

                    // Assign to the actual item. Make sure the slam bit is
                    // set, if it wasn't set before.
                    itemCopy.BasePermissions = basePerms;
                    itemCopy.CurrentPermissions = ownerPerms | 16; // Slam

                    itemCopy.NextPermissions = item.NextPermissions;

                    // This preserves "everyone can move"
                    itemCopy.EveryOnePermissions = item.EveryOnePermissions &
                                                   nextPerms;

                    // Intentionally killing "share with group" here, as
                    // the recipient will not have the group this is
                    // set to
                    itemCopy.GroupPermissions = 0;
                }
                else
                {
                    itemCopy.CurrentPermissions = item.CurrentPermissions;
                    itemCopy.NextPermissions = item.NextPermissions;
                    itemCopy.EveryOnePermissions = item.EveryOnePermissions & item.NextPermissions;
                    itemCopy.GroupPermissions = item.GroupPermissions & item.NextPermissions;
                    itemCopy.BasePermissions = item.BasePermissions;
                }
                
                if (itemCopy.Folder == UUID.Zero)
                {
                    InventoryFolderBase folder = InventoryService.GetFolderForType(recipient, (AssetType)itemCopy.AssetType);

                    if (folder != null)
                    {
                        itemCopy.Folder = folder.ID;
                    }
                    else
                    {
                        InventoryFolderBase root = InventoryService.GetRootFolder(recipient);

                        if (root != null)
                            itemCopy.Folder = root.ID;
                        else
                            return null; // No destination
                    }
                }

                itemCopy.GroupID = UUID.Zero;
                itemCopy.GroupOwned = false;
                itemCopy.Flags = item.Flags;
                itemCopy.SalePrice = item.SalePrice;
                itemCopy.SaleType = item.SaleType;

                if (AddInventoryItem(itemCopy))
                {
                    IInventoryAccessModule invAccess = RequestModuleInterface<IInventoryAccessModule>();
                    if (invAccess != null)
                        invAccess.TransferInventoryAssets(itemCopy, senderId, recipient);
                }

                if (!Permissions.BypassPermissions())
                {
                    if ((item.CurrentPermissions & (uint)PermissionMask.Copy) == 0)
                    {
                        List<UUID> items = new List<UUID>();
                        items.Add(itemId);
                        InventoryService.DeleteItems(senderId, items);
                    }
                }

                return itemCopy;
            }
            else
            {
                m_log.WarnFormat("[AGENT INVENTORY]: Failed to find item {0} or item does not belong to giver ", itemId);
                return null;
            }

        }

Same methods

Scene::GiveInventoryItem ( UUID recipient, UUID senderId, UUID itemId ) : InventoryItemBase
Scene::GiveInventoryItem ( IClientAPI recipientClient, UUID senderId, UUID itemId ) : void

Usage Example

//////////////////////////////////////////////// 
// User-to-user inventory offers

        private void UserInventoryOffer(IClientAPI client, Scene scene, GridInstantMessage im)
        {
            InventoryFolderBase folder = null;
            InventoryItemBase item = null;

            UUID toAgentID = new UUID(im.toAgentID);

            IMuteListModule m_muteListModule = scene.RequestModuleInterface<IMuteListModule>();
            if (m_muteListModule != null)
            {
                if (m_muteListModule.IsMuted(client.AgentId, toAgentID))
                {
                    client.SendAgentAlertMessage("Inventory offer was automatically declined.", false);
                    return; // recipient has sender muted
                }
            }

            // Unpack the binary bucket
            AssetType assetType = (AssetType)im.binaryBucket[0];
            UUID destID = new UUID(im.binaryBucket, 1);
            UUID copyID;
            bool isFolder = (assetType == AssetType.Folder);

            ScenePresence recipient = scene.GetScenePresence(toAgentID);

            if (recipient != null && recipient.IsBot)
            {
                client.SendAgentAlertMessage("Can't give inventory to bots.", false);
                return;//can't give objects to bots
            }

            if (assetType == AssetType.Folder)
            {
                folder = scene.GiveInventoryFolder(toAgentID, client.AgentId, destID, UUID.Zero);
                if (folder == null)
                {
                    client.SendAgentAlertMessage("Can't find folder to give. Nothing given.", false);
                    return;
                }
                copyID = folder.ID;
            }
            else
            {
                item = scene.GiveInventoryItem(toAgentID, client.AgentId, destID);
                if (item == null)
                {
                    client.SendAgentAlertMessage("Can't find item to give. Nothing given.", false);
                    return;
                }
                copyID = item.ID;
            }
//            m_log.InfoFormat("[AGENT INVENTORY]: Offering {0} {1} to user {2} inventory as {3}", isFolder ? "folder" : "item", destID, toAgentID, copyID);

            // Update the asset type and destination ID into the outgoing IM.
            im.binaryBucket = new byte[17];
            im.binaryBucket[0] = (byte)assetType;
            Array.Copy(copyID.GetBytes(), 0, im.binaryBucket, 1, 16);
            // Also stuff the destination ID into the session ID field for retrieval in accept/decline
            im.imSessionID = copyID.Guid;

            CachedUserInfo recipientInfo = scene.CommsManager.UserService.GetUserDetails(toAgentID);
            if (recipientInfo != null && recipient != null)
            {
                if ((!isFolder) && (item != null))
                {
                    // item offer?
                    recipient.ControllingClient.SendInventoryItemCreateUpdate(item, 0);
                }

                if (isFolder && (folder != null))
                {
                    // folder offer?
                    folder = recipientInfo.GetFolder(folder.ID);
                    if (folder != null)
                    {
                        recipient.ControllingClient.SendBulkUpdateInventory(folder);
                        recipientInfo.SendInventoryDecendents(recipient.ControllingClient, folder.ID, false, true);
                    }
                }
            }

            // Send the IM to the recipient. The item is already in their inventory, so
            // it will not be lost if they are offline. Transaction ID is the item ID.
            // We get that same ID back on the reply so we know what to act on.
            RelayInventoryOfferIM(scene, recipient, im);
        }
Scene