SimianInventoryServiceConnector.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  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. { "ContentType", invContentType },
  544. { "ExtraData", OSDParser.SerializeJsonString(extraData) }
  545. };
  546. OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
  547. bool success = response["Success"].AsBoolean();
  548. if (!success)
  549. {
  550. m_log.Warn("[SIMIAN INVENTORY CONNECTOR]: Error creating item " + item.Name + " for " + item.Owner + ": " +
  551. response["Message"].AsString());
  552. }
  553. return success;
  554. }
  555. /// <summary>
  556. /// Update an item in the user's inventory
  557. /// </summary>
  558. /// <param name="item"></param>
  559. /// <returns>true if the item was successfully updated</returns>
  560. public bool UpdateItem(InventoryItemBase item)
  561. {
  562. if (item.AssetID != UUID.Zero)
  563. {
  564. return AddItem(item);
  565. }
  566. else
  567. {
  568. // This is actually a folder update
  569. InventoryFolderBase folder = new InventoryFolderBase(item.ID, item.Name, item.Owner, (short)item.AssetType, item.Folder, 0);
  570. return UpdateFolder(folder);
  571. }
  572. }
  573. public bool MoveItems(UUID ownerID, List<InventoryItemBase> items)
  574. {
  575. bool success = true;
  576. while (items.Count > 0)
  577. {
  578. List<InventoryItemBase> currentItems = new List<InventoryItemBase>();
  579. UUID destFolderID = items[0].Folder;
  580. // Find all of the items being moved to the current destination folder
  581. for (int i = 0; i < items.Count; i++)
  582. {
  583. InventoryItemBase item = items[i];
  584. if (item.Folder == destFolderID)
  585. currentItems.Add(item);
  586. }
  587. // Do the inventory move for the current items
  588. success &= MoveItems(ownerID, items, destFolderID);
  589. // Remove the processed items from the list
  590. for (int i = 0; i < currentItems.Count; i++)
  591. items.Remove(currentItems[i]);
  592. }
  593. return success;
  594. }
  595. /// <summary>
  596. /// Does the given user have an inventory structure?
  597. /// </summary>
  598. /// <param name="userID"></param>
  599. /// <returns></returns>
  600. public bool HasInventoryForUser(UUID userID)
  601. {
  602. return GetRootFolder(userID) != null;
  603. }
  604. /// <summary>
  605. /// Get the active gestures of the agent.
  606. /// </summary>
  607. /// <param name="userID"></param>
  608. /// <returns></returns>
  609. public List<InventoryItemBase> GetActiveGestures(UUID userID)
  610. {
  611. OSDArray items = FetchGestures(userID);
  612. string[] itemIDs = new string[items.Count];
  613. for (int i = 0; i < items.Count; i++)
  614. itemIDs[i] = items[i].AsUUID().ToString();
  615. // NameValueCollection requestArgs = new NameValueCollection
  616. // {
  617. // { "RequestMethod", "GetInventoryNodes" },
  618. // { "OwnerID", userID.ToString() },
  619. // { "Items", String.Join(",", itemIDs) }
  620. // };
  621. // FIXME: Implement this in SimianGrid
  622. return new List<InventoryItemBase>(0);
  623. }
  624. /// <summary>
  625. /// Get the union of permissions of all inventory items
  626. /// that hold the given assetID.
  627. /// </summary>
  628. /// <param name="userID"></param>
  629. /// <param name="assetID"></param>
  630. /// <returns>The permissions or 0 if no such asset is found in
  631. /// the user's inventory</returns>
  632. public int GetAssetPermissions(UUID userID, UUID assetID)
  633. {
  634. // NameValueCollection requestArgs = new NameValueCollection
  635. // {
  636. // { "RequestMethod", "GetInventoryNodes" },
  637. // { "OwnerID", userID.ToString() },
  638. // { "AssetID", assetID.ToString() }
  639. // };
  640. // FIXME: Implement this in SimianGrid
  641. return (int)PermissionMask.All;
  642. }
  643. private List<InventoryFolderBase> GetFoldersFromResponse(OSDArray items, UUID baseFolder, bool includeBaseFolder)
  644. {
  645. List<InventoryFolderBase> invFolders = new List<InventoryFolderBase>(items.Count);
  646. for (int i = 0; i < items.Count; i++)
  647. {
  648. OSDMap item = items[i] as OSDMap;
  649. if (item != null && item["Type"].AsString() == "Folder")
  650. {
  651. UUID folderID = item["ID"].AsUUID();
  652. if (folderID == baseFolder && !includeBaseFolder)
  653. continue;
  654. invFolders.Add(new InventoryFolderBase(
  655. folderID,
  656. item["Name"].AsString(),
  657. item["OwnerID"].AsUUID(),
  658. (short)SLUtil.ContentTypeToSLAssetType(item["ContentType"].AsString()),
  659. item["ParentID"].AsUUID(),
  660. (ushort)item["Version"].AsInteger()
  661. ));
  662. }
  663. }
  664. m_log.Debug("[SIMIAN INVENTORY CONNECTOR]: Parsed " + invFolders.Count + " folders from SimianGrid response");
  665. return invFolders;
  666. }
  667. private List<InventoryItemBase> GetItemsFromResponse(OSDArray items)
  668. {
  669. List<InventoryItemBase> invItems = new List<InventoryItemBase>(items.Count);
  670. for (int i = 0; i < items.Count; i++)
  671. {
  672. OSDMap item = items[i] as OSDMap;
  673. if (item != null && item["Type"].AsString() == "Item")
  674. {
  675. InventoryItemBase invItem = new InventoryItemBase();
  676. invItem.AssetID = item["AssetID"].AsUUID();
  677. invItem.AssetType = SLUtil.ContentTypeToSLAssetType(item["ContentType"].AsString());
  678. invItem.CreationDate = item["CreationDate"].AsInteger();
  679. invItem.CreatorId = item["CreatorID"].AsString();
  680. invItem.CreatorIdAsUuid = item["CreatorID"].AsUUID();
  681. invItem.Description = item["Description"].AsString();
  682. invItem.Folder = item["ParentID"].AsUUID();
  683. invItem.ID = item["ID"].AsUUID();
  684. invItem.InvType = SLUtil.ContentTypeToSLInvType(item["ContentType"].AsString());
  685. invItem.Name = item["Name"].AsString();
  686. invItem.Owner = item["OwnerID"].AsUUID();
  687. OSDMap extraData = item["ExtraData"] as OSDMap;
  688. if (extraData != null && extraData.Count > 0)
  689. {
  690. invItem.Flags = extraData["Flags"].AsUInteger();
  691. invItem.GroupID = extraData["GroupID"].AsUUID();
  692. invItem.GroupOwned = extraData["GroupOwned"].AsBoolean();
  693. invItem.SalePrice = extraData["SalePrice"].AsInteger();
  694. invItem.SaleType = (byte)extraData["SaleType"].AsInteger();
  695. OSDMap perms = extraData["Permissions"] as OSDMap;
  696. if (perms != null)
  697. {
  698. invItem.BasePermissions = perms["BaseMask"].AsUInteger();
  699. invItem.CurrentPermissions = perms["OwnerMask"].AsUInteger();
  700. invItem.EveryOnePermissions = perms["EveryoneMask"].AsUInteger();
  701. invItem.GroupPermissions = perms["GroupMask"].AsUInteger();
  702. invItem.NextPermissions = perms["NextOwnerMask"].AsUInteger();
  703. }
  704. if (extraData.ContainsKey("LinkedItemType"))
  705. invItem.AssetType = SLUtil.ContentTypeToSLAssetType(extraData["LinkedItemType"].AsString());
  706. }
  707. if (invItem.BasePermissions == 0)
  708. {
  709. m_log.InfoFormat("[SIMIAN INVENTORY CONNECTOR]: Forcing item permissions to full for item {0} ({1})",
  710. invItem.Name, invItem.ID);
  711. invItem.BasePermissions = (uint)PermissionMask.All;
  712. invItem.CurrentPermissions = (uint)PermissionMask.All;
  713. invItem.EveryOnePermissions = (uint)PermissionMask.All;
  714. invItem.GroupPermissions = (uint)PermissionMask.All;
  715. invItem.NextPermissions = (uint)PermissionMask.All;
  716. }
  717. invItems.Add(invItem);
  718. }
  719. }
  720. m_log.Debug("[SIMIAN INVENTORY CONNECTOR]: Parsed " + invItems.Count + " items from SimianGrid response");
  721. return invItems;
  722. }
  723. private bool MoveItems(UUID ownerID, List<InventoryItemBase> items, UUID destFolderID)
  724. {
  725. string[] itemIDs = new string[items.Count];
  726. for (int i = 0; i < items.Count; i++)
  727. itemIDs[i] = items[i].ID.ToString();
  728. NameValueCollection requestArgs = new NameValueCollection
  729. {
  730. { "RequestMethod", "MoveInventoryNodes" },
  731. { "OwnerID", ownerID.ToString() },
  732. { "FolderID", destFolderID.ToString() },
  733. { "Items", String.Join(",", itemIDs) }
  734. };
  735. OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
  736. bool success = response["Success"].AsBoolean();
  737. if (!success)
  738. {
  739. m_log.Warn("[SIMIAN INVENTORY CONNECTOR]: Failed to move " + items.Count + " items to " +
  740. destFolderID + ": " + response["Message"].AsString());
  741. }
  742. return success;
  743. }
  744. private void UpdateGesture(UUID userID, UUID itemID, bool enabled)
  745. {
  746. OSDArray gestures = FetchGestures(userID);
  747. OSDArray newGestures = new OSDArray();
  748. for (int i = 0; i < gestures.Count; i++)
  749. {
  750. UUID gesture = gestures[i].AsUUID();
  751. if (gesture != itemID)
  752. newGestures.Add(OSD.FromUUID(gesture));
  753. }
  754. if (enabled)
  755. newGestures.Add(OSD.FromUUID(itemID));
  756. SaveGestures(userID, newGestures);
  757. }
  758. private OSDArray FetchGestures(UUID userID)
  759. {
  760. NameValueCollection requestArgs = new NameValueCollection
  761. {
  762. { "RequestMethod", "GetUser" },
  763. { "UserID", userID.ToString() }
  764. };
  765. OSDMap response = WebUtil.PostToService(m_userServerUrl, requestArgs);
  766. if (response["Success"].AsBoolean())
  767. {
  768. OSDMap user = response["User"] as OSDMap;
  769. if (user != null && response.ContainsKey("Gestures"))
  770. {
  771. OSD gestures = OSDParser.DeserializeJson(response["Gestures"].AsString());
  772. if (gestures != null && gestures is OSDArray)
  773. return (OSDArray)gestures;
  774. else
  775. m_log.Error("[SIMIAN INVENTORY CONNECTOR]: Unrecognized active gestures data for " + userID);
  776. }
  777. }
  778. else
  779. {
  780. m_log.Warn("[SIMIAN INVENTORY CONNECTOR]: Failed to fetch active gestures for " + userID + ": " +
  781. response["Message"].AsString());
  782. }
  783. return new OSDArray();
  784. }
  785. private void SaveGestures(UUID userID, OSDArray gestures)
  786. {
  787. NameValueCollection requestArgs = new NameValueCollection
  788. {
  789. { "RequestMethod", "AddUserData" },
  790. { "UserID", userID.ToString() },
  791. { "Gestures", OSDParser.SerializeJsonString(gestures) }
  792. };
  793. OSDMap response = WebUtil.PostToService(m_userServerUrl, requestArgs);
  794. if (!response["Success"].AsBoolean())
  795. {
  796. m_log.Warn("[SIMIAN INVENTORY CONNECTOR]: Failed to save active gestures for " + userID + ": " +
  797. response["Message"].AsString());
  798. }
  799. }
  800. }
  801. }