/*
* Copyright (c) Contributors, http://opensimulator.org/
* See CONTRIBUTORS.TXT for a full list of copyright holders.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the OpenSimulator Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using OpenMetaverse;
using log4net;
using Nini.Config;
using System.Reflection;
using OpenSim.Services.Base;
using OpenSim.Services.Interfaces;
using OpenSim.Services.InventoryService;
using OpenSim.Data;
using OpenSim.Framework;
using OpenSim.Server.Base;
namespace OpenSim.Services.HypergridService
{
///
/// Hypergrid inventory service. It serves the IInventoryService interface,
/// but implements it in ways that are appropriate for inter-grid
/// inventory exchanges. Specifically, it does not performs deletions
/// and it responds to GetRootFolder requests with the ID of the
/// Suitcase folder, not the actual "My Inventory" folder.
///
public class HGSuitcaseInventoryService : XInventoryService, IInventoryService
{
private static readonly ILog m_log =
LogManager.GetLogger(
MethodBase.GetCurrentMethod().DeclaringType);
private string m_HomeURL;
private IUserAccountService m_UserAccountService;
private UserAccountCache m_Cache;
private ExpiringCache> m_SuitcaseTrees = new ExpiringCache>();
public HGSuitcaseInventoryService(IConfigSource config, string configName)
: base(config, configName)
{
m_log.DebugFormat("[HG SUITCASE INVENTORY SERVICE]: Starting with config name {0}", configName);
if (configName != string.Empty)
m_ConfigName = configName;
if (m_Database == null)
m_log.WarnFormat("[XXX]: m_Database is null!");
//
// Try reading the [InventoryService] section, if it exists
//
IConfig invConfig = config.Configs[m_ConfigName];
if (invConfig != null)
{
// realm = authConfig.GetString("Realm", realm);
string userAccountsDll = invConfig.GetString("UserAccountsService", string.Empty);
if (userAccountsDll == string.Empty)
throw new Exception("Please specify UserAccountsService in HGInventoryService configuration");
Object[] args = new Object[] { config };
m_UserAccountService = ServerUtils.LoadPlugin(userAccountsDll, args);
if (m_UserAccountService == null)
throw new Exception(String.Format("Unable to create UserAccountService from {0}", userAccountsDll));
// legacy configuration [obsolete]
m_HomeURL = invConfig.GetString("ProfileServerURI", string.Empty);
// Preferred
m_HomeURL = invConfig.GetString("HomeURI", m_HomeURL);
m_Cache = UserAccountCache.CreateUserAccountCache(m_UserAccountService);
}
m_log.Debug("[HG SUITCASE INVENTORY SERVICE]: Starting...");
}
public override bool CreateUserInventory(UUID principalID)
{
// NOGO
return false;
}
public override List GetInventorySkeleton(UUID principalID)
{
XInventoryFolder suitcase = GetSuitcaseXFolder(principalID);
XInventoryFolder root = GetRootXFolder(principalID);
List tree = GetFolderTree(suitcase.folderID);
if (tree == null || (tree != null && tree.Count == 0))
return null;
List folders = new List();
foreach (XInventoryFolder x in tree)
{
folders.Add(ConvertToOpenSim(x));
}
SetAsRootFolder(suitcase, root);
folders.Add(ConvertToOpenSim(suitcase));
return folders;
}
public override InventoryCollection GetUserInventory(UUID userID)
{
m_log.DebugFormat("[HG SUITCASE INVENTORY SERVICE]: Get Suitcase inventory for user {0}", userID);
InventoryCollection userInventory = new InventoryCollection();
userInventory.UserID = userID;
userInventory.Folders = new List();
userInventory.Items = new List();
XInventoryFolder suitcase = GetSuitcaseXFolder(userID);
XInventoryFolder root = GetRootXFolder(userID);
List tree = GetFolderTree(suitcase.folderID);
if (tree == null || (tree != null && tree.Count == 0))
{
SetAsRootFolder(suitcase, root);
userInventory.Folders.Add(ConvertToOpenSim(suitcase));
return userInventory;
}
List items;
foreach (XInventoryFolder f in tree)
{
// Add the items of this subfolder
items = GetFolderItems(userID, f.folderID);
if (items != null && items.Count > 0)
{
userInventory.Items.AddRange(items);
}
// Add the folder itself
userInventory.Folders.Add(ConvertToOpenSim(f));
}
items = GetFolderItems(userID, suitcase.folderID);
if (items != null && items.Count > 0)
{
userInventory.Items.AddRange(items);
}
SetAsRootFolder(suitcase, root);
userInventory.Folders.Add(ConvertToOpenSim(suitcase));
m_log.DebugFormat("[HG SUITCASE INVENTORY SERVICE]: GetUserInventory for user {0} returning {1} folders and {2} items",
userID, userInventory.Folders.Count, userInventory.Items.Count);
return userInventory;
}
public override InventoryFolderBase GetRootFolder(UUID principalID)
{
m_log.DebugFormat("[HG SUITCASE INVENTORY SERVICE]: GetRootFolder for {0}", principalID);
if (m_Database == null)
m_log.ErrorFormat("[XXX]: m_Database is NULL!");
// Let's find out the local root folder
XInventoryFolder root = GetRootXFolder(principalID); ;
if (root == null)
{
m_log.WarnFormat("[HG SUITCASE INVENTORY SERVICE]: Unable to retrieve local root folder for user {0}", principalID);
}
// Warp! Root folder for travelers is the suitcase folder
XInventoryFolder suitcase = GetSuitcaseXFolder(principalID);
if (suitcase == null)
{
m_log.DebugFormat("[HG SUITCASE INVENTORY SERVICE]: User {0} does not have a Suitcase folder. Creating it...", principalID);
// make one, and let's add it to the user's inventory as a direct child of the root folder
// In the DB we tag it as type 100, but we use -1 (Unknown) outside
suitcase = CreateFolder(principalID, root.folderID, 100, "My Suitcase");
if (suitcase == null)
m_log.ErrorFormat("[HG SUITCASE INVENTORY SERVICE]: Unable to create suitcase folder");
m_Database.StoreFolder(suitcase);
// Create System folders
CreateSystemFolders(principalID, suitcase.folderID);
}
SetAsRootFolder(suitcase, root);
return ConvertToOpenSim(suitcase);
}
protected void CreateSystemFolders(UUID principalID, UUID rootID)
{
m_log.Debug("[HG SUITCASE INVENTORY SERVICE]: Creating System folders under Suitcase...");
XInventoryFolder[] sysFolders = GetSystemFolders(principalID, rootID);
if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.Animation) return true; return false; }))
CreateFolder(principalID, rootID, (int)AssetType.Animation, "Animations");
if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.Bodypart) return true; return false; }))
CreateFolder(principalID, rootID, (int)AssetType.Bodypart, "Body Parts");
if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.CallingCard) return true; return false; }))
CreateFolder(principalID, rootID, (int)AssetType.CallingCard, "Calling Cards");
if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.Clothing) return true; return false; }))
CreateFolder(principalID, rootID, (int)AssetType.Clothing, "Clothing");
if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.Gesture) return true; return false; }))
CreateFolder(principalID, rootID, (int)AssetType.Gesture, "Gestures");
if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.Landmark) return true; return false; }))
CreateFolder(principalID, rootID, (int)AssetType.Landmark, "Landmarks");
if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.LostAndFoundFolder) return true; return false; }))
CreateFolder(principalID, rootID, (int)AssetType.LostAndFoundFolder, "Lost And Found");
if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.Notecard) return true; return false; }))
CreateFolder(principalID, rootID, (int)AssetType.Notecard, "Notecards");
if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.Object) return true; return false; }))
CreateFolder(principalID, rootID, (int)AssetType.Object, "Objects");
if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.SnapshotFolder) return true; return false; }))
CreateFolder(principalID, rootID, (int)AssetType.SnapshotFolder, "Photo Album");
if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.LSLText) return true; return false; }))
CreateFolder(principalID, rootID, (int)AssetType.LSLText, "Scripts");
if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.Sound) return true; return false; }))
CreateFolder(principalID, rootID, (int)AssetType.Sound, "Sounds");
if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.Texture) return true; return false; }))
CreateFolder(principalID, rootID, (int)AssetType.Texture, "Textures");
if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.TrashFolder) return true; return false; }))
CreateFolder(principalID, rootID, (int)AssetType.TrashFolder, "Trash");
if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.FavoriteFolder) return true; return false; }))
CreateFolder(principalID, rootID, (int)AssetType.FavoriteFolder, "Favorites");
if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)AssetType.CurrentOutfitFolder) return true; return false; }))
CreateFolder(principalID, rootID, (int)AssetType.CurrentOutfitFolder, "Current Outfit");
}
public override InventoryFolderBase GetFolderForType(UUID principalID, AssetType type)
{
//m_log.DebugFormat("[HG INVENTORY SERVICE]: GetFolderForType for {0} {0}", principalID, type);
XInventoryFolder suitcase = GetSuitcaseXFolder(principalID);
XInventoryFolder[] folders = m_Database.GetFolders(
new string[] { "agentID", "type", "parentFolderID" },
new string[] { principalID.ToString(), ((int)type).ToString(), suitcase.folderID.ToString() });
if (folders.Length == 0)
{
m_log.WarnFormat("[HG SUITCASE INVENTORY SERVICE]: Found no folder for type {0} for user {1}", type, principalID);
return null;
}
m_log.DebugFormat(
"[HG SUITCASE INVENTORY SERVICE]: Found folder {0} {1} for type {2} for user {3}",
folders[0].folderName, folders[0].folderID, type, principalID);
return ConvertToOpenSim(folders[0]);
}
public override InventoryCollection GetFolderContent(UUID principalID, UUID folderID)
{
InventoryCollection coll = null;
XInventoryFolder suitcase = GetSuitcaseXFolder(principalID);
if (!IsWithinSuitcaseTree(folderID, suitcase))
return new InventoryCollection();
coll = base.GetFolderContent(principalID, folderID);
if (coll == null)
{
m_log.WarnFormat("[HG SUITCASE INVENTORY SERVICE]: Something wrong with user {0}'s suitcase folder", principalID);
coll = new InventoryCollection();
}
return coll;
}
public override List GetFolderItems(UUID principalID, UUID folderID)
{
// Let's do a bit of sanity checking, more than the base service does
// make sure the given folder exists under the suitcase tree of this user
XInventoryFolder suitcase = GetSuitcaseXFolder(principalID);
if (!IsWithinSuitcaseTree(folderID, suitcase))
return new List();
return base.GetFolderItems(principalID, folderID);
}
public override bool AddFolder(InventoryFolderBase folder)
{
m_log.WarnFormat("[HG SUITCASE INVENTORY SERVICE]: AddFolder {0} {1}", folder.Name, folder.ParentID);
// Let's do a bit of sanity checking, more than the base service does
// make sure the given folder's parent folder exists under the suitcase tree of this user
XInventoryFolder suitcase = GetSuitcaseXFolder(folder.Owner);
if (!IsWithinSuitcaseTree(folder.ParentID, suitcase))
return false;
// OK, it's legit
return base.AddFolder(folder);
}
public override bool UpdateFolder(InventoryFolderBase folder)
{
XInventoryFolder suitcase = GetSuitcaseXFolder(folder.Owner);
m_log.DebugFormat("[HG SUITCASE INVENTORY SERVICE]: Update folder {0}, version {1}", folder.ID, folder.Version);
if (!IsWithinSuitcaseTree(folder.ID, suitcase))
{
m_log.DebugFormat("[HG SUITCASE INVENTORY SERVICE]: folder {0} not within Suitcase tree", folder.Name);
return false;
}
// For all others
return base.UpdateFolder(folder);
}
public override bool MoveFolder(InventoryFolderBase folder)
{
XInventoryFolder suitcase = GetSuitcaseXFolder(folder.Owner);
if (!IsWithinSuitcaseTree(folder.ID, suitcase) || !IsWithinSuitcaseTree(folder.ParentID, suitcase))
return false;
return base.MoveFolder(folder);
}
public override bool DeleteFolders(UUID principalID, List folderIDs)
{
// NOGO
return false;
}
public override bool PurgeFolder(InventoryFolderBase folder)
{
// NOGO
return false;
}
public override bool AddItem(InventoryItemBase item)
{
// Let's do a bit of sanity checking, more than the base service does
// make sure the given folder's parent folder exists under the suitcase tree of this user
XInventoryFolder suitcase = GetSuitcaseXFolder(item.Owner);
if (!IsWithinSuitcaseTree(item.Folder, suitcase))
return false;
// OK, it's legit
return base.AddItem(item);
}
public override bool UpdateItem(InventoryItemBase item)
{
XInventoryFolder suitcase = GetSuitcaseXFolder(item.Owner);
if (!IsWithinSuitcaseTree(item.Folder, suitcase))
return false;
return base.UpdateItem(item);
}
public override bool MoveItems(UUID principalID, List items)
{
// Principal is b0rked. *sigh*
XInventoryFolder suitcase = GetSuitcaseXFolder(items[0].Owner);
if (!IsWithinSuitcaseTree(items[0].Folder, suitcase))
return false;
return base.MoveItems(principalID, items);
}
public override bool DeleteItems(UUID principalID, List itemIDs)
{
return false;
}
public new InventoryItemBase GetItem(InventoryItemBase item)
{
InventoryItemBase it = base.GetItem(item);
if (it == null)
{
m_log.DebugFormat("[HG SUITCASE INVENTORY SERVICE]: Unable to retrieve item {0} ({1}) in folder {2}",
item.Name, item.ID, item.Folder);
return null;
}
XInventoryFolder suitcase = GetSuitcaseXFolder(it.Owner);
if (suitcase == null)
{
m_log.DebugFormat("[HG SUITCASE INVENTORY SERVICE]: Root or Suitcase are null for user {0}",
it.Owner);
return null;
}
if (!IsWithinSuitcaseTree(it.Folder, suitcase))
{
m_log.DebugFormat("[HG SUITCASE INVENTORY SERVICE]: Item {0} (folder {1}) is not within Suitcase",
it.Name, it.Folder);
return null;
}
// UserAccount user = m_Cache.GetUser(it.CreatorId);
// // Adjust the creator data
// if (user != null && it != null && (it.CreatorData == null || it.CreatorData == string.Empty))
// it.CreatorData = m_HomeURL + ";" + user.FirstName + " " + user.LastName;
//}
return it;
}
public new InventoryFolderBase GetFolder(InventoryFolderBase folder)
{
InventoryFolderBase f = base.GetFolder(folder);
if (f != null)
{
XInventoryFolder suitcase = GetSuitcaseXFolder(f.Owner);
if (!IsWithinSuitcaseTree(f.ID, suitcase))
return null;
}
return f;
}
//public List GetActiveGestures(UUID principalID)
//{
//}
//public int GetAssetPermissions(UUID principalID, UUID assetID)
//{
//}
#region Auxiliary functions
private XInventoryFolder GetXFolder(UUID userID, UUID folderID)
{
XInventoryFolder[] folders = m_Database.GetFolders(
new string[] { "agentID", "folderID" },
new string[] { userID.ToString(), folderID.ToString() });
if (folders.Length == 0)
return null;
return folders[0];
}
private XInventoryFolder GetRootXFolder(UUID principalID)
{
XInventoryFolder[] folders = m_Database.GetFolders(
new string[] { "agentID", "folderName", "type" },
new string[] { principalID.ToString(), "My Inventory", ((int)AssetType.RootFolder).ToString() });
if (folders != null && folders.Length > 0)
return folders[0];
return null;
}
private XInventoryFolder GetSuitcaseXFolder(UUID principalID)
{
// Warp! Root folder for travelers
XInventoryFolder[] folders = m_Database.GetFolders(
new string[] { "agentID", "type" },
new string[] { principalID.ToString(), "100" }); // This is a special folder type...
if (folders != null && folders.Length > 0)
return folders[0];
return null;
}
private void SetAsRootFolder(XInventoryFolder suitcase, XInventoryFolder root)
{
suitcase.type = (short)AssetType.Folder;
}
private List GetFolderTree(UUID folder)
{
List t = null;
if (m_SuitcaseTrees.TryGetValue(folder, out t))
return t;
t = GetFolderTreeRecursive(folder);
m_SuitcaseTrees.AddOrUpdate(folder, t, 120);
return t;
}
private List GetFolderTreeRecursive(UUID root)
{
List tree = new List();
XInventoryFolder[] folders = m_Database.GetFolders(
new string[] { "parentFolderID" },
new string[] { root.ToString() });
if (folders == null || (folders != null && folders.Length == 0))
return tree; // empty tree
else
{
foreach (XInventoryFolder f in folders)
{
tree.Add(f);
tree.AddRange(GetFolderTreeRecursive(f.folderID));
}
return tree;
}
}
///
/// Return true if the folderID is a subfolder of the Suitcase or the suitcase folder itself
///
///
///
///
///
private bool IsWithinSuitcaseTree(UUID folderID, XInventoryFolder suitcase)
{
List tree = new List();
tree.Add(suitcase); // Warp! the tree is the real root folder plus the children of the suitcase folder
tree.AddRange(GetFolderTree(suitcase.folderID));
XInventoryFolder f = tree.Find(delegate(XInventoryFolder fl)
{
if (fl.folderID == folderID) return true;
else return false;
});
if (f == null) return false;
else return true;
}
#endregion
}
}