SimianInventoryServiceConnector.cs 35 KB

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