SimianInventoryServiceConnector.cs 36 KB

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