SimianInventoryServiceConnector.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Collections.Specialized;
  30. using System.Reflection;
  31. using log4net;
  32. using Mono.Addins;
  33. using Nini.Config;
  34. using OpenMetaverse;
  35. using OpenMetaverse.StructuredData;
  36. using OpenSim.Framework;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Region.Framework.Scenes;
  39. using OpenSim.Services.Interfaces;
  40. using PermissionMask = OpenSim.Framework.PermissionMask;
  41. namespace OpenSim.Services.Connectors.SimianGrid
  42. {
  43. /// <summary>
  44. /// Permissions bitflags
  45. /// </summary>
  46. /*
  47. [Flags]
  48. public enum PermissionMask : uint
  49. {
  50. None = 0,
  51. Transfer = 1 << 13,
  52. Modify = 1 << 14,
  53. Copy = 1 << 15,
  54. Move = 1 << 19,
  55. Damage = 1 << 20,
  56. All = 0x7FFFFFFF
  57. }
  58. */
  59. /// <summary>
  60. /// Connects avatar inventories to the SimianGrid backend
  61. /// </summary>
  62. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "SimianInventoryServiceConnector")]
  63. public class SimianInventoryServiceConnector : IInventoryService, ISharedRegionModule
  64. {
  65. private static readonly ILog m_log =
  66. LogManager.GetLogger(
  67. MethodBase.GetCurrentMethod().DeclaringType);
  68. private string m_serverUrl = String.Empty;
  69. private string m_userServerUrl = String.Empty;
  70. // private object m_gestureSyncRoot = new object();
  71. private bool m_Enabled = false;
  72. private const double CACHE_EXPIRATION_SECONDS = 20.0;
  73. private static ExpiringCache<UUID, InventoryItemBase> m_ItemCache;
  74. #region ISharedRegionModule
  75. public Type ReplaceableInterface { get { return null; } }
  76. public void RegionLoaded(Scene scene) { }
  77. public void PostInitialise() { }
  78. public void Close() { }
  79. public SimianInventoryServiceConnector() { }
  80. public string Name { get { return "SimianInventoryServiceConnector"; } }
  81. public void AddRegion(Scene scene) { if (m_Enabled) { scene.RegisterModuleInterface<IInventoryService>(this); } }
  82. public void RemoveRegion(Scene scene) { if (m_Enabled) { scene.UnregisterModuleInterface<IInventoryService>(this); } }
  83. #endregion ISharedRegionModule
  84. public SimianInventoryServiceConnector(IConfigSource source)
  85. {
  86. CommonInit(source);
  87. }
  88. public SimianInventoryServiceConnector(string url)
  89. {
  90. if (!url.EndsWith("/") && !url.EndsWith("="))
  91. url = url + '/';
  92. m_serverUrl = url;
  93. if (m_ItemCache == null)
  94. m_ItemCache = new ExpiringCache<UUID, InventoryItemBase>();
  95. }
  96. public void Initialise(IConfigSource source)
  97. {
  98. IConfig moduleConfig = source.Configs["Modules"];
  99. if (moduleConfig != null)
  100. {
  101. string name = moduleConfig.GetString("InventoryServices", "");
  102. if (name == Name)
  103. CommonInit(source);
  104. }
  105. }
  106. private void CommonInit(IConfigSource source)
  107. {
  108. IConfig gridConfig = source.Configs["InventoryService"];
  109. if (gridConfig != null)
  110. {
  111. string serviceUrl = gridConfig.GetString("InventoryServerURI");
  112. if (!String.IsNullOrEmpty(serviceUrl))
  113. {
  114. if (!serviceUrl.EndsWith("/") && !serviceUrl.EndsWith("="))
  115. serviceUrl = serviceUrl + '/';
  116. m_serverUrl = serviceUrl;
  117. gridConfig = source.Configs["UserAccountService"];
  118. if (gridConfig != null)
  119. {
  120. serviceUrl = gridConfig.GetString("UserAccountServerURI");
  121. if (!String.IsNullOrEmpty(serviceUrl))
  122. {
  123. m_userServerUrl = serviceUrl;
  124. m_Enabled = true;
  125. if (m_ItemCache == null)
  126. m_ItemCache = new ExpiringCache<UUID, InventoryItemBase>();
  127. }
  128. }
  129. }
  130. }
  131. if (String.IsNullOrEmpty(m_serverUrl))
  132. m_log.Info("[SIMIAN INVENTORY CONNECTOR]: No InventoryServerURI specified, disabling connector");
  133. else if (String.IsNullOrEmpty(m_userServerUrl))
  134. m_log.Info("[SIMIAN INVENTORY CONNECTOR]: No UserAccountServerURI specified, disabling connector");
  135. }
  136. /// <summary>
  137. /// Create the entire inventory for a given user
  138. /// </summary>
  139. /// <param name="user"></param>
  140. /// <returns></returns>
  141. public bool CreateUserInventory(UUID userID)
  142. {
  143. NameValueCollection requestArgs = new NameValueCollection
  144. {
  145. { "RequestMethod", "AddInventory" },
  146. { "OwnerID", userID.ToString() }
  147. };
  148. OSDMap response = SimianGrid.PostToService(m_serverUrl, requestArgs);
  149. bool success = response["Success"].AsBoolean();
  150. if (!success)
  151. m_log.Warn("[SIMIAN INVENTORY CONNECTOR]: Inventory creation for " + userID + " failed: " + response["Message"].AsString());
  152. return success;
  153. }
  154. /// <summary>
  155. /// Gets the skeleton of the inventory -- folders only
  156. /// </summary>
  157. /// <param name="userID"></param>
  158. /// <returns></returns>
  159. public List<InventoryFolderBase> GetInventorySkeleton(UUID userID)
  160. {
  161. NameValueCollection requestArgs = new NameValueCollection
  162. {
  163. { "RequestMethod", "GetInventoryNode" },
  164. { "ItemID", userID.ToString() },
  165. { "OwnerID", userID.ToString() },
  166. { "IncludeFolders", "1" },
  167. { "IncludeItems", "0" },
  168. { "ChildrenOnly", "0" }
  169. };
  170. OSDMap response = SimianGrid.PostToService(m_serverUrl, requestArgs);
  171. if (response["Success"].AsBoolean() && response["Items"] is OSDArray)
  172. {
  173. OSDArray items = (OSDArray)response["Items"];
  174. return GetFoldersFromResponse(items, userID, true);
  175. }
  176. else
  177. {
  178. m_log.Warn("[SIMIAN INVENTORY CONNECTOR]: Failed to retrieve inventory skeleton for " + userID + ": " +
  179. response["Message"].AsString());
  180. return new List<InventoryFolderBase>(0);
  181. }
  182. }
  183. /// <summary>
  184. /// Retrieve the root inventory folder for the given user.
  185. /// </summary>
  186. /// <param name="userID"></param>
  187. /// <returns>null if no root folder was found</returns>
  188. public InventoryFolderBase GetRootFolder(UUID userID)
  189. {
  190. NameValueCollection requestArgs = new NameValueCollection
  191. {
  192. { "RequestMethod", "GetInventoryNode" },
  193. { "ItemID", userID.ToString() },
  194. { "OwnerID", userID.ToString() },
  195. { "IncludeFolders", "1" },
  196. { "IncludeItems", "0" },
  197. { "ChildrenOnly", "1" }
  198. };
  199. OSDMap response = SimianGrid.PostToService(m_serverUrl, requestArgs);
  200. if (response["Success"].AsBoolean() && response["Items"] is OSDArray)
  201. {
  202. OSDArray items = (OSDArray)response["Items"];
  203. List<InventoryFolderBase> folders = GetFoldersFromResponse(items, userID, true);
  204. if (folders.Count > 0)
  205. return folders[0];
  206. }
  207. return null;
  208. }
  209. /// <summary>
  210. /// Gets the user folder for the given folder-type
  211. /// </summary>
  212. /// <param name="userID"></param>
  213. /// <param name="type"></param>
  214. /// <returns></returns>
  215. public InventoryFolderBase GetFolderForType(UUID userID, FolderType type)
  216. {
  217. string contentType = SLUtil.SLAssetTypeToContentType((int)type);
  218. NameValueCollection requestArgs = new NameValueCollection
  219. {
  220. { "RequestMethod", "GetFolderForType" },
  221. { "ContentType", contentType },
  222. { "OwnerID", userID.ToString() }
  223. };
  224. OSDMap response = SimianGrid.PostToService(m_serverUrl, requestArgs);
  225. if (response["Success"].AsBoolean() && response["Folder"] is OSDMap)
  226. {
  227. OSDMap folder = (OSDMap)response["Folder"];
  228. return new InventoryFolderBase(
  229. folder["ID"].AsUUID(),
  230. folder["Name"].AsString(),
  231. folder["OwnerID"].AsUUID(),
  232. (short)SLUtil.ContentTypeToSLAssetType(folder["ContentType"].AsString()),
  233. folder["ParentID"].AsUUID(),
  234. (ushort)folder["Version"].AsInteger()
  235. );
  236. }
  237. else
  238. {
  239. m_log.Warn("[SIMIAN INVENTORY CONNECTOR]: Default folder not found for content type " + contentType + ": " + response["Message"].AsString());
  240. return GetRootFolder(userID);
  241. }
  242. }
  243. /// <summary>
  244. /// Get an item, given by its UUID
  245. /// </summary>
  246. /// <param name="item"></param>
  247. /// <returns></returns>
  248. public InventoryItemBase GetItem(UUID principalID, UUID itemID)
  249. {
  250. InventoryItemBase retrieved = null;
  251. if (m_ItemCache.TryGetValue(itemID, out retrieved))
  252. return retrieved;
  253. NameValueCollection requestArgs = new NameValueCollection
  254. {
  255. { "RequestMethod", "GetInventoryNode" },
  256. { "ItemID", itemID.ToString() },
  257. { "OwnerID", principalID.ToString() },
  258. { "IncludeFolders", "1" },
  259. { "IncludeItems", "1" },
  260. { "ChildrenOnly", "1" }
  261. };
  262. OSDMap response = SimianGrid.PostToService(m_serverUrl, requestArgs);
  263. if (response["Success"].AsBoolean() && response["Items"] is OSDArray)
  264. {
  265. List<InventoryItemBase> items = GetItemsFromResponse((OSDArray)response["Items"]);
  266. if (items.Count > 0)
  267. {
  268. // The requested item should be the first in this list, but loop through
  269. // and sanity check just in case
  270. for (int i = 0; i < items.Count; i++)
  271. {
  272. if (items[i].ID == itemID)
  273. {
  274. retrieved = items[i];
  275. m_ItemCache.AddOrUpdate(itemID, retrieved, CACHE_EXPIRATION_SECONDS);
  276. return retrieved;
  277. }
  278. }
  279. }
  280. }
  281. m_log.Warn("[SIMIAN INVENTORY CONNECTOR]: Item " + itemID + " owned by " + principalID + " not found");
  282. return null;
  283. }
  284. public InventoryItemBase[] GetMultipleItems(UUID principalID, UUID[] itemIDs)
  285. {
  286. InventoryItemBase[] result = new InventoryItemBase[itemIDs.Length];
  287. int i = 0;
  288. foreach (UUID id in itemIDs)
  289. result[i++] = GetItem(principalID, id);
  290. return result;
  291. }
  292. /// <summary>
  293. /// Get a folder, given by its UUID
  294. /// </summary>
  295. /// <param name="folder"></param>
  296. /// <returns></returns>
  297. public InventoryFolderBase GetFolder(UUID principalID, UUID folderID)
  298. {
  299. NameValueCollection requestArgs = new NameValueCollection
  300. {
  301. { "RequestMethod", "GetInventoryNode" },
  302. { "ItemID", folderID.ToString() },
  303. { "OwnerID", principalID.ToString() },
  304. { "IncludeFolders", "1" },
  305. { "IncludeItems", "0" },
  306. { "ChildrenOnly", "1" }
  307. };
  308. OSDMap response = SimianGrid.PostToService(m_serverUrl, requestArgs);
  309. if (response["Success"].AsBoolean() && response["Items"] is OSDArray)
  310. {
  311. OSDArray items = (OSDArray)response["Items"];
  312. List<InventoryFolderBase> folders = GetFoldersFromResponse(items, folderID, true);
  313. if (folders.Count > 0)
  314. return folders[0];
  315. }
  316. return null;
  317. }
  318. /// <summary>
  319. /// Gets everything (folders and items) inside a folder
  320. /// </summary>
  321. /// <param name="userID"></param>
  322. /// <param name="folderID"></param>
  323. /// <returns></returns>
  324. public InventoryCollection GetFolderContent(UUID userID, UUID folderID)
  325. {
  326. InventoryCollection inventory = new InventoryCollection();
  327. inventory.OwnerID = userID;
  328. NameValueCollection requestArgs = new NameValueCollection
  329. {
  330. { "RequestMethod", "GetInventoryNode" },
  331. { "ItemID", folderID.ToString() },
  332. { "OwnerID", userID.ToString() },
  333. { "IncludeFolders", "1" },
  334. { "IncludeItems", "1" },
  335. { "ChildrenOnly", "1" }
  336. };
  337. OSDMap response = SimianGrid.PostToService(m_serverUrl, requestArgs);
  338. if (response["Success"].AsBoolean() && response["Items"] is OSDArray)
  339. {
  340. OSDArray items = (OSDArray)response["Items"];
  341. inventory.Folders = GetFoldersFromResponse(items, folderID, false);
  342. inventory.Items = GetItemsFromResponse(items);
  343. }
  344. else
  345. {
  346. m_log.Warn("[SIMIAN INVENTORY CONNECTOR]: Error fetching folder " + folderID + " content for " + userID + ": " +
  347. response["Message"].AsString());
  348. inventory.Folders = new List<InventoryFolderBase>(0);
  349. inventory.Items = new List<InventoryItemBase>(0);
  350. }
  351. return inventory;
  352. }
  353. public virtual InventoryCollection[] GetMultipleFoldersContent(UUID principalID, UUID[] folderIDs)
  354. {
  355. InventoryCollection[] invColl = new InventoryCollection[folderIDs.Length];
  356. int i = 0;
  357. foreach (UUID fid in folderIDs)
  358. {
  359. invColl[i++] = GetFolderContent(principalID, fid);
  360. }
  361. return invColl;
  362. }
  363. /// <summary>
  364. /// Gets the items inside a folder
  365. /// </summary>
  366. /// <param name="userID"></param>
  367. /// <param name="folderID"></param>
  368. /// <returns></returns>
  369. public List<InventoryItemBase> GetFolderItems(UUID userID, UUID folderID)
  370. {
  371. InventoryCollection inventory = new InventoryCollection();
  372. inventory.OwnerID = userID;
  373. NameValueCollection requestArgs = new NameValueCollection
  374. {
  375. { "RequestMethod", "GetInventoryNode" },
  376. { "ItemID", folderID.ToString() },
  377. { "OwnerID", userID.ToString() },
  378. { "IncludeFolders", "0" },
  379. { "IncludeItems", "1" },
  380. { "ChildrenOnly", "1" }
  381. };
  382. OSDMap response = SimianGrid.PostToService(m_serverUrl, requestArgs);
  383. if (response["Success"].AsBoolean() && response["Items"] is OSDArray)
  384. {
  385. OSDArray items = (OSDArray)response["Items"];
  386. return GetItemsFromResponse(items);
  387. }
  388. else
  389. {
  390. m_log.Warn("[SIMIAN INVENTORY CONNECTOR]: Error fetching folder " + folderID + " for " + userID + ": " +
  391. response["Message"].AsString());
  392. return new List<InventoryItemBase>(0);
  393. }
  394. }
  395. /// <summary>
  396. /// Add a new folder to the user's inventory
  397. /// </summary>
  398. /// <param name="folder"></param>
  399. /// <returns>true if the folder was successfully added</returns>
  400. public bool AddFolder(InventoryFolderBase folder)
  401. {
  402. NameValueCollection requestArgs = new NameValueCollection
  403. {
  404. { "RequestMethod", "AddInventoryFolder" },
  405. { "FolderID", folder.ID.ToString() },
  406. { "ParentID", folder.ParentID.ToString() },
  407. { "OwnerID", folder.Owner.ToString() },
  408. { "Name", folder.Name },
  409. { "ContentType", SLUtil.SLAssetTypeToContentType(folder.Type) }
  410. };
  411. OSDMap response = SimianGrid.PostToService(m_serverUrl, requestArgs);
  412. bool success = response["Success"].AsBoolean();
  413. if (!success)
  414. {
  415. m_log.Warn("[SIMIAN INVENTORY CONNECTOR]: Error creating folder " + folder.Name + " for " + folder.Owner + ": " +
  416. response["Message"].AsString());
  417. }
  418. return success;
  419. }
  420. /// <summary>
  421. /// Update a folder in the user's inventory
  422. /// </summary>
  423. /// <param name="folder"></param>
  424. /// <returns>true if the folder was successfully updated</returns>
  425. public bool UpdateFolder(InventoryFolderBase folder)
  426. {
  427. return AddFolder(folder);
  428. }
  429. /// <summary>
  430. /// Move an inventory folder to a new location
  431. /// </summary>
  432. /// <param name="folder">A folder containing the details of the new location</param>
  433. /// <returns>true if the folder was successfully moved</returns>
  434. public bool MoveFolder(InventoryFolderBase folder)
  435. {
  436. return AddFolder(folder);
  437. }
  438. /// <summary>
  439. /// Delete an item from the user's inventory
  440. /// </summary>
  441. /// <param name="item"></param>
  442. /// <returns>true if the item was successfully deleted</returns>
  443. //bool DeleteItem(InventoryItemBase item);
  444. public bool DeleteFolders(UUID userID, List<UUID> folderIDs)
  445. {
  446. return DeleteItems(userID, folderIDs);
  447. }
  448. /// <summary>
  449. /// Delete an item from the user's inventory
  450. /// </summary>
  451. /// <param name="item"></param>
  452. /// <returns>true if the item was successfully deleted</returns>
  453. public bool DeleteItems(UUID userID, List<UUID> itemIDs)
  454. {
  455. // TODO: RemoveInventoryNode should be replaced with RemoveInventoryNodes
  456. bool allSuccess = true;
  457. for (int i = 0; i < itemIDs.Count; i++)
  458. {
  459. UUID itemID = itemIDs[i];
  460. NameValueCollection requestArgs = new NameValueCollection
  461. {
  462. { "RequestMethod", "RemoveInventoryNode" },
  463. { "OwnerID", userID.ToString() },
  464. { "ItemID", itemID.ToString() }
  465. };
  466. OSDMap response = SimianGrid.PostToService(m_serverUrl, requestArgs);
  467. bool success = response["Success"].AsBoolean();
  468. if (!success)
  469. {
  470. m_log.Warn("[SIMIAN INVENTORY CONNECTOR]: Error removing item " + itemID + " for " + userID + ": " +
  471. response["Message"].AsString());
  472. allSuccess = false;
  473. }
  474. }
  475. return allSuccess;
  476. }
  477. /// <summary>
  478. /// Purge an inventory folder of all its items and subfolders.
  479. /// </summary>
  480. /// <param name="folder"></param>
  481. /// <returns>true if the folder was successfully purged</returns>
  482. public bool PurgeFolder(InventoryFolderBase folder)
  483. {
  484. NameValueCollection requestArgs = new NameValueCollection
  485. {
  486. { "RequestMethod", "PurgeInventoryFolder" },
  487. { "OwnerID", folder.Owner.ToString() },
  488. { "FolderID", folder.ID.ToString() }
  489. };
  490. OSDMap response = SimianGrid.PostToService(m_serverUrl, requestArgs);
  491. bool success = response["Success"].AsBoolean();
  492. if (!success)
  493. {
  494. m_log.Warn("[SIMIAN INVENTORY CONNECTOR]: Error purging folder " + folder.ID + " for " + folder.Owner + ": " +
  495. response["Message"].AsString());
  496. }
  497. return success;
  498. }
  499. /// <summary>
  500. /// Add a new item to the user's inventory
  501. /// </summary>
  502. /// <param name="item"></param>
  503. /// <returns>true if the item was successfully added</returns>
  504. public bool AddItem(InventoryItemBase item)
  505. {
  506. // A folder of UUID.Zero means we need to find the most appropriate home for this item
  507. if (item.Folder == UUID.Zero)
  508. {
  509. InventoryFolderBase folder = null;
  510. if (Enum.IsDefined(typeof(FolderType), (sbyte)item.AssetType))
  511. folder = GetFolderForType(item.Owner, (FolderType)item.AssetType);
  512. if (folder != null && folder.ID != UUID.Zero)
  513. item.Folder = folder.ID;
  514. else
  515. item.Folder = item.Owner; // Root folder
  516. }
  517. if ((AssetType)item.AssetType == AssetType.Gesture)
  518. UpdateGesture(item.Owner, item.ID, item.Flags == 1);
  519. if (item.BasePermissions == 0)
  520. m_log.WarnFormat("[SIMIAN INVENTORY CONNECTOR]: Adding inventory item {0} ({1}) with no base permissions", item.Name, item.ID);
  521. OSDMap permissions = new OSDMap
  522. {
  523. { "BaseMask", OSD.FromInteger(item.BasePermissions) },
  524. { "EveryoneMask", OSD.FromInteger(item.EveryOnePermissions) },
  525. { "GroupMask", OSD.FromInteger(item.GroupPermissions) },
  526. { "NextOwnerMask", OSD.FromInteger(item.NextPermissions) },
  527. { "OwnerMask", OSD.FromInteger(item.CurrentPermissions) }
  528. };
  529. OSDMap extraData = new OSDMap()
  530. {
  531. { "Flags", OSD.FromInteger(item.Flags) },
  532. { "GroupID", OSD.FromUUID(item.GroupID) },
  533. { "GroupOwned", OSD.FromBoolean(item.GroupOwned) },
  534. { "SalePrice", OSD.FromInteger(item.SalePrice) },
  535. { "SaleType", OSD.FromInteger(item.SaleType) },
  536. { "Permissions", permissions }
  537. };
  538. // Add different asset type only if it differs from inventory type
  539. // (needed for links)
  540. string invContentType = SLUtil.SLInvTypeToContentType(item.InvType);
  541. string assetContentType = SLUtil.SLAssetTypeToContentType(item.AssetType);
  542. if (invContentType != assetContentType)
  543. extraData["LinkedItemType"] = OSD.FromString(assetContentType);
  544. NameValueCollection requestArgs = new NameValueCollection
  545. {
  546. { "RequestMethod", "AddInventoryItem" },
  547. { "ItemID", item.ID.ToString() },
  548. { "AssetID", item.AssetID.ToString() },
  549. { "ParentID", item.Folder.ToString() },
  550. { "OwnerID", item.Owner.ToString() },
  551. { "Name", item.Name },
  552. { "Description", item.Description },
  553. { "CreatorID", item.CreatorId },
  554. { "CreatorData", item.CreatorData },
  555. { "ContentType", invContentType },
  556. { "ExtraData", OSDParser.SerializeJsonString(extraData) }
  557. };
  558. OSDMap response = SimianGrid.PostToService(m_serverUrl, requestArgs);
  559. bool success = response["Success"].AsBoolean();
  560. if (!success)
  561. {
  562. m_log.Warn("[SIMIAN INVENTORY CONNECTOR]: Error creating item " + item.Name + " for " + item.Owner + ": " +
  563. response["Message"].AsString());
  564. }
  565. return success;
  566. }
  567. /// <summary>
  568. /// Update an item in the user's inventory
  569. /// </summary>
  570. /// <param name="item"></param>
  571. /// <returns>true if the item was successfully updated</returns>
  572. public bool UpdateItem(InventoryItemBase item)
  573. {
  574. if (item.AssetID != UUID.Zero)
  575. {
  576. return AddItem(item);
  577. }
  578. else
  579. {
  580. // This is actually a folder update
  581. InventoryFolderBase folder = new InventoryFolderBase(item.ID, item.Name, item.Owner, (short)item.AssetType, item.Folder, 0);
  582. return UpdateFolder(folder);
  583. }
  584. }
  585. public bool MoveItems(UUID ownerID, List<InventoryItemBase> items)
  586. {
  587. bool success = true;
  588. while (items.Count > 0)
  589. {
  590. List<InventoryItemBase> currentItems = new List<InventoryItemBase>();
  591. UUID destFolderID = items[0].Folder;
  592. // Find all of the items being moved to the current destination folder
  593. for (int i = 0; i < items.Count; i++)
  594. {
  595. InventoryItemBase item = items[i];
  596. if (item.Folder == destFolderID)
  597. currentItems.Add(item);
  598. }
  599. // Do the inventory move for the current items
  600. success &= MoveItems(ownerID, items, destFolderID);
  601. // Remove the processed items from the list
  602. for (int i = 0; i < currentItems.Count; i++)
  603. items.Remove(currentItems[i]);
  604. }
  605. return success;
  606. }
  607. /// <summary>
  608. /// Does the given user have an inventory structure?
  609. /// </summary>
  610. /// <param name="userID"></param>
  611. /// <returns></returns>
  612. public bool HasInventoryForUser(UUID userID)
  613. {
  614. return GetRootFolder(userID) != null;
  615. }
  616. /// <summary>
  617. /// Get the active gestures of the agent.
  618. /// </summary>
  619. /// <param name="userID"></param>
  620. /// <returns></returns>
  621. public List<InventoryItemBase> GetActiveGestures(UUID userID)
  622. {
  623. OSDArray items = FetchGestures(userID);
  624. string[] itemIDs = new string[items.Count];
  625. for (int i = 0; i < items.Count; i++)
  626. itemIDs[i] = items[i].AsUUID().ToString();
  627. // NameValueCollection requestArgs = new NameValueCollection
  628. // {
  629. // { "RequestMethod", "GetInventoryNodes" },
  630. // { "OwnerID", userID.ToString() },
  631. // { "Items", String.Join(",", itemIDs) }
  632. // };
  633. // FIXME: Implement this in SimianGrid
  634. return new List<InventoryItemBase>(0);
  635. }
  636. /// <summary>
  637. /// Get the union of permissions of all inventory items
  638. /// that hold the given assetID.
  639. /// </summary>
  640. /// <param name="userID"></param>
  641. /// <param name="assetID"></param>
  642. /// <returns>The permissions or 0 if no such asset is found in
  643. /// the user's inventory</returns>
  644. public int GetAssetPermissions(UUID userID, UUID assetID)
  645. {
  646. // NameValueCollection requestArgs = new NameValueCollection
  647. // {
  648. // { "RequestMethod", "GetInventoryNodes" },
  649. // { "OwnerID", userID.ToString() },
  650. // { "AssetID", assetID.ToString() }
  651. // };
  652. // FIXME: Implement this in SimianGrid
  653. return (int)PermissionMask.All;
  654. }
  655. private List<InventoryFolderBase> GetFoldersFromResponse(OSDArray items, UUID baseFolder, bool includeBaseFolder)
  656. {
  657. List<InventoryFolderBase> invFolders = new List<InventoryFolderBase>(items.Count);
  658. for (int i = 0; i < items.Count; i++)
  659. {
  660. OSDMap item = items[i] as OSDMap;
  661. if (item != null && item["Type"].AsString() == "Folder")
  662. {
  663. UUID folderID = item["ID"].AsUUID();
  664. if (folderID == baseFolder && !includeBaseFolder)
  665. continue;
  666. invFolders.Add(new InventoryFolderBase(
  667. folderID,
  668. item["Name"].AsString(),
  669. item["OwnerID"].AsUUID(),
  670. (short)SLUtil.ContentTypeToSLAssetType(item["ContentType"].AsString()),
  671. item["ParentID"].AsUUID(),
  672. (ushort)item["Version"].AsInteger()
  673. ));
  674. }
  675. }
  676. // m_log.Debug("[SIMIAN INVENTORY CONNECTOR]: Parsed " + invFolders.Count + " folders from SimianGrid response");
  677. return invFolders;
  678. }
  679. private List<InventoryItemBase> GetItemsFromResponse(OSDArray items)
  680. {
  681. List<InventoryItemBase> invItems = new List<InventoryItemBase>(items.Count);
  682. for (int i = 0; i < items.Count; i++)
  683. {
  684. OSDMap item = items[i] as OSDMap;
  685. if (item != null && item["Type"].AsString() == "Item")
  686. {
  687. InventoryItemBase invItem = new InventoryItemBase();
  688. invItem.AssetID = item["AssetID"].AsUUID();
  689. invItem.AssetType = SLUtil.ContentTypeToSLAssetType(item["ContentType"].AsString());
  690. invItem.CreationDate = item["CreationDate"].AsInteger();
  691. invItem.CreatorId = item["CreatorID"].AsString();
  692. invItem.CreatorData = item["CreatorData"].AsString();
  693. invItem.Description = item["Description"].AsString();
  694. invItem.Folder = item["ParentID"].AsUUID();
  695. invItem.ID = item["ID"].AsUUID();
  696. invItem.InvType = SLUtil.ContentTypeToSLInvType(item["ContentType"].AsString());
  697. invItem.Name = item["Name"].AsString();
  698. invItem.Owner = item["OwnerID"].AsUUID();
  699. OSDMap extraData = item["ExtraData"] as OSDMap;
  700. if (extraData != null && extraData.Count > 0)
  701. {
  702. invItem.Flags = extraData["Flags"].AsUInteger();
  703. invItem.GroupID = extraData["GroupID"].AsUUID();
  704. invItem.GroupOwned = extraData["GroupOwned"].AsBoolean();
  705. invItem.SalePrice = extraData["SalePrice"].AsInteger();
  706. invItem.SaleType = (byte)extraData["SaleType"].AsInteger();
  707. OSDMap perms = extraData["Permissions"] as OSDMap;
  708. if (perms != null)
  709. {
  710. invItem.BasePermissions = perms["BaseMask"].AsUInteger();
  711. invItem.CurrentPermissions = perms["OwnerMask"].AsUInteger();
  712. invItem.EveryOnePermissions = perms["EveryoneMask"].AsUInteger();
  713. invItem.GroupPermissions = perms["GroupMask"].AsUInteger();
  714. invItem.NextPermissions = perms["NextOwnerMask"].AsUInteger();
  715. }
  716. if (extraData.ContainsKey("LinkedItemType"))
  717. invItem.AssetType = SLUtil.ContentTypeToSLAssetType(extraData["LinkedItemType"].AsString());
  718. }
  719. if (invItem.BasePermissions == 0)
  720. {
  721. m_log.InfoFormat("[SIMIAN INVENTORY CONNECTOR]: Forcing item permissions to full for item {0} ({1})",
  722. invItem.Name, invItem.ID);
  723. invItem.BasePermissions = (uint)PermissionMask.All;
  724. invItem.CurrentPermissions = (uint)PermissionMask.All;
  725. invItem.EveryOnePermissions = (uint)PermissionMask.All;
  726. invItem.GroupPermissions = (uint)PermissionMask.All;
  727. invItem.NextPermissions = (uint)PermissionMask.All;
  728. }
  729. invItems.Add(invItem);
  730. }
  731. }
  732. // m_log.Debug("[SIMIAN INVENTORY CONNECTOR]: Parsed " + invItems.Count + " items from SimianGrid response");
  733. return invItems;
  734. }
  735. private bool MoveItems(UUID ownerID, List<InventoryItemBase> items, UUID destFolderID)
  736. {
  737. string[] itemIDs = new string[items.Count];
  738. for (int i = 0; i < items.Count; i++)
  739. itemIDs[i] = items[i].ID.ToString();
  740. NameValueCollection requestArgs = new NameValueCollection
  741. {
  742. { "RequestMethod", "MoveInventoryNodes" },
  743. { "OwnerID", ownerID.ToString() },
  744. { "FolderID", destFolderID.ToString() },
  745. { "Items", String.Join(",", itemIDs) }
  746. };
  747. OSDMap response = SimianGrid.PostToService(m_serverUrl, requestArgs);
  748. bool success = response["Success"].AsBoolean();
  749. if (!success)
  750. {
  751. m_log.Warn("[SIMIAN INVENTORY CONNECTOR]: Failed to move " + items.Count + " items to " +
  752. destFolderID + ": " + response["Message"].AsString());
  753. }
  754. return success;
  755. }
  756. private void UpdateGesture(UUID userID, UUID itemID, bool enabled)
  757. {
  758. OSDArray gestures = FetchGestures(userID);
  759. OSDArray newGestures = new OSDArray();
  760. for (int i = 0; i < gestures.Count; i++)
  761. {
  762. UUID gesture = gestures[i].AsUUID();
  763. if (gesture != itemID)
  764. newGestures.Add(OSD.FromUUID(gesture));
  765. }
  766. if (enabled)
  767. newGestures.Add(OSD.FromUUID(itemID));
  768. SaveGestures(userID, newGestures);
  769. }
  770. private OSDArray FetchGestures(UUID userID)
  771. {
  772. NameValueCollection requestArgs = new NameValueCollection
  773. {
  774. { "RequestMethod", "GetUser" },
  775. { "UserID", userID.ToString() }
  776. };
  777. OSDMap response = SimianGrid.PostToService(m_userServerUrl, requestArgs);
  778. if (response["Success"].AsBoolean())
  779. {
  780. OSDMap user = response["User"] as OSDMap;
  781. if (user != null && response.ContainsKey("Gestures"))
  782. {
  783. OSD gestures = OSDParser.DeserializeJson(response["Gestures"].AsString());
  784. if (gestures != null && gestures is OSDArray)
  785. return (OSDArray)gestures;
  786. else
  787. m_log.Error("[SIMIAN INVENTORY CONNECTOR]: Unrecognized active gestures data for " + userID);
  788. }
  789. }
  790. else
  791. {
  792. m_log.Warn("[SIMIAN INVENTORY CONNECTOR]: Failed to fetch active gestures for " + userID + ": " +
  793. response["Message"].AsString());
  794. }
  795. return new OSDArray();
  796. }
  797. private void SaveGestures(UUID userID, OSDArray gestures)
  798. {
  799. NameValueCollection requestArgs = new NameValueCollection
  800. {
  801. { "RequestMethod", "AddUserData" },
  802. { "UserID", userID.ToString() },
  803. { "Gestures", OSDParser.SerializeJsonString(gestures) }
  804. };
  805. OSDMap response = SimianGrid.PostToService(m_userServerUrl, requestArgs);
  806. if (!response["Success"].AsBoolean())
  807. {
  808. m_log.Warn("[SIMIAN INVENTORY CONNECTOR]: Failed to save active gestures for " + userID + ": " +
  809. response["Message"].AsString());
  810. }
  811. }
  812. }
  813. }