FetchInvDescHandler.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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;
  29. using System.Collections.Generic;
  30. using System.Linq;
  31. using System.Reflection;
  32. using System.Text;
  33. using log4net;
  34. using Nini.Config;
  35. using OpenMetaverse;
  36. using OpenMetaverse.StructuredData;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.Capabilities;
  39. using OpenSim.Region.Framework.Interfaces;
  40. using OpenSim.Framework.Servers.HttpServer;
  41. using OpenSim.Services.Interfaces;
  42. using Caps = OpenSim.Framework.Capabilities.Caps;
  43. namespace OpenSim.Capabilities.Handlers
  44. {
  45. public class FetchInvDescHandler
  46. {
  47. private static readonly ILog m_log =
  48. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. private IInventoryService m_InventoryService;
  50. private ILibraryService m_LibraryService;
  51. private IScene m_Scene;
  52. // private object m_fetchLock = new Object();
  53. public FetchInvDescHandler(IInventoryService invService, ILibraryService libService, IScene s)
  54. {
  55. m_InventoryService = invService;
  56. m_LibraryService = libService;
  57. m_Scene = s;
  58. }
  59. public string FetchInventoryDescendentsRequest(string request, string path, string param, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  60. {
  61. //m_log.DebugFormat("[XXX]: FetchInventoryDescendentsRequest in {0}, {1}", (m_Scene == null) ? "none" : m_Scene.Name, request);
  62. Hashtable hash = new Hashtable();
  63. try
  64. {
  65. hash = (Hashtable)LLSD.LLSDDeserialize(Utils.StringToBytes(request));
  66. }
  67. catch (LLSD.LLSDParseException e)
  68. {
  69. m_log.ErrorFormat("[WEB FETCH INV DESC HANDLER]: Fetch error: {0}{1}" + e.Message, e.StackTrace);
  70. m_log.Error("Request: " + request);
  71. }
  72. ArrayList foldersrequested = (ArrayList)hash["folders"];
  73. List<LLSDFetchInventoryDescendents> folders = new List<LLSDFetchInventoryDescendents>();
  74. for (int i = 0; i < foldersrequested.Count; i++)
  75. {
  76. Hashtable inventoryhash = (Hashtable)foldersrequested[i];
  77. LLSDFetchInventoryDescendents llsdRequest = new LLSDFetchInventoryDescendents();
  78. try
  79. {
  80. LLSDHelpers.DeserialiseOSDMap(inventoryhash, llsdRequest);
  81. }
  82. catch (Exception e)
  83. {
  84. m_log.Debug("[WEB FETCH INV DESC HANDLER]: caught exception doing OSD deserialize" + e);
  85. continue;
  86. }
  87. folders.Add(llsdRequest);
  88. }
  89. StringBuilder lastresponse = new StringBuilder(1024);
  90. lastresponse.Append("<llsd>");
  91. if (folders.Count > 0)
  92. {
  93. List<UUID> bad_folders = new List<UUID>();
  94. List<InventoryCollectionWithDescendents> invcollSet = Fetch(folders, bad_folders);
  95. //m_log.DebugFormat("[XXX]: Got {0} folders from a request of {1}", invcollSet.Count, folders.Count);
  96. if (invcollSet == null)
  97. {
  98. m_log.DebugFormat("[WEB FETCH INV DESC HANDLER]: Multiple folder fetch failed. Trying old protocol.");
  99. #pragma warning disable 0612
  100. return FetchInventoryDescendentsRequest(foldersrequested, httpRequest, httpResponse);
  101. #pragma warning restore 0612
  102. }
  103. if(invcollSet.Count > 0)
  104. {
  105. lastresponse.Append("<map><key>folders</key><array>");
  106. foreach (InventoryCollectionWithDescendents icoll in invcollSet)
  107. {
  108. LLSDInventoryFolderContents thiscontents = contentsToLLSD(icoll.Collection, icoll.Descendents);
  109. lastresponse.Append(LLSDHelpers.SerialiseLLSDReplyNoHeader(thiscontents));
  110. }
  111. lastresponse.Append("</array></map>");
  112. }
  113. else
  114. lastresponse.Append("<map><key>folders</key><array /></map>");
  115. //m_log.DebugFormat("[WEB FETCH INV DESC HANDLER]: Bad folders {0}", string.Join(", ", bad_folders));
  116. if(bad_folders.Count > 0)
  117. {
  118. lastresponse.Append("<map><key>bad_folders</key><array>");
  119. foreach (UUID bad in bad_folders)
  120. {
  121. lastresponse.Append("<map><key>folder_id</key><uuid>");
  122. lastresponse.Append(bad.ToString());
  123. lastresponse.Append("</uuid><key>error</key><string>Unknown</string></map>");
  124. }
  125. lastresponse.Append("</array></map>");
  126. }
  127. lastresponse.Append("</llsd>");
  128. }
  129. else
  130. {
  131. lastresponse.Append("<map><key>folders</key><array /></map></llsd>");
  132. }
  133. return lastresponse.ToString();;
  134. }
  135. /// <summary>
  136. /// Construct an LLSD reply packet to a CAPS inventory request
  137. /// </summary>
  138. /// <param name="invFetch"></param>
  139. /// <returns></returns>
  140. private LLSDInventoryDescendents FetchInventoryReply(LLSDFetchInventoryDescendents invFetch)
  141. {
  142. LLSDInventoryDescendents reply = new LLSDInventoryDescendents();
  143. LLSDInventoryFolderContents contents = new LLSDInventoryFolderContents();
  144. contents.agent_id = invFetch.owner_id;
  145. contents.owner_id = invFetch.owner_id;
  146. contents.folder_id = invFetch.folder_id;
  147. reply.folders.Array.Add(contents);
  148. InventoryCollection inv = new InventoryCollection();
  149. inv.Folders = new List<InventoryFolderBase>();
  150. inv.Items = new List<InventoryItemBase>();
  151. int version = 0;
  152. int descendents = 0;
  153. #pragma warning disable 0612
  154. inv = Fetch(
  155. invFetch.owner_id, invFetch.folder_id, invFetch.owner_id,
  156. invFetch.fetch_folders, invFetch.fetch_items, invFetch.sort_order, out version, out descendents);
  157. #pragma warning restore 0612
  158. if (inv != null && inv.Folders != null)
  159. {
  160. foreach (InventoryFolderBase invFolder in inv.Folders)
  161. {
  162. contents.categories.Array.Add(ConvertInventoryFolder(invFolder));
  163. }
  164. descendents += inv.Folders.Count;
  165. }
  166. if (inv != null && inv.Items != null)
  167. {
  168. foreach (InventoryItemBase invItem in inv.Items)
  169. {
  170. contents.items.Array.Add(ConvertInventoryItem(invItem));
  171. }
  172. }
  173. contents.descendents = descendents;
  174. contents.version = version;
  175. //m_log.DebugFormat(
  176. // "[WEB FETCH INV DESC HANDLER]: Replying to request for folder {0} (fetch items {1}, fetch folders {2}) with {3} items and {4} folders for agent {5}",
  177. // invFetch.folder_id,
  178. // invFetch.fetch_items,
  179. // invFetch.fetch_folders,
  180. // contents.items.Array.Count,
  181. // contents.categories.Array.Count,
  182. // invFetch.owner_id);
  183. return reply;
  184. }
  185. private LLSDInventoryFolderContents contentsToLLSD(InventoryCollection inv, int descendents)
  186. {
  187. LLSDInventoryFolderContents contents = new LLSDInventoryFolderContents();
  188. contents.agent_id = inv.OwnerID;
  189. contents.owner_id = inv.OwnerID;
  190. contents.folder_id = inv.FolderID;
  191. if (inv.Folders != null)
  192. {
  193. foreach (InventoryFolderBase invFolder in inv.Folders)
  194. {
  195. contents.categories.Array.Add(ConvertInventoryFolder(invFolder));
  196. }
  197. }
  198. if (inv.Items != null)
  199. {
  200. foreach (InventoryItemBase invItem in inv.Items)
  201. {
  202. contents.items.Array.Add(ConvertInventoryItem(invItem));
  203. }
  204. }
  205. contents.descendents = descendents;
  206. contents.version = inv.Version;
  207. return contents;
  208. }
  209. /// <summary>
  210. /// Old style. Soon to be deprecated.
  211. /// </summary>
  212. /// <param name="request"></param>
  213. /// <param name="httpRequest"></param>
  214. /// <param name="httpResponse"></param>
  215. /// <returns></returns>
  216. [Obsolete]
  217. private string FetchInventoryDescendentsRequest(ArrayList foldersrequested, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  218. {
  219. //m_log.DebugFormat("[WEB FETCH INV DESC HANDLER]: Received request for {0} folders", foldersrequested.Count);
  220. StringBuilder tmpresponse = new StringBuilder(1024);
  221. StringBuilder tmpbadfolders = new StringBuilder(1024);
  222. for (int i = 0; i < foldersrequested.Count; i++)
  223. {
  224. string inventoryitemstr = "";
  225. Hashtable inventoryhash = (Hashtable)foldersrequested[i];
  226. LLSDFetchInventoryDescendents llsdRequest = new LLSDFetchInventoryDescendents();
  227. try
  228. {
  229. LLSDHelpers.DeserialiseOSDMap(inventoryhash, llsdRequest);
  230. }
  231. catch (Exception e)
  232. {
  233. m_log.Debug("[WEB FETCH INV DESC HANDLER]: caught exception doing OSD deserialize" + e);
  234. }
  235. LLSDInventoryDescendents reply = FetchInventoryReply(llsdRequest);
  236. if (null == reply)
  237. {
  238. tmpbadfolders.Append("<map><key>folder_id</key><uuid>");
  239. tmpbadfolders.Append(llsdRequest.folder_id.ToString());
  240. tmpbadfolders.Append("</uuid><key>error</key><string>Unknown</string></map>");
  241. }
  242. else
  243. {
  244. inventoryitemstr = LLSDHelpers.SerialiseLLSDReply(reply);
  245. inventoryitemstr = inventoryitemstr.Replace("<llsd><map><key>folders</key><array>", "");
  246. inventoryitemstr = inventoryitemstr.Replace("</array></map></llsd>", "");
  247. }
  248. tmpresponse.Append(inventoryitemstr);
  249. }
  250. StringBuilder lastresponse = new StringBuilder(1024);
  251. lastresponse.Append("<llsd>");
  252. if(tmpresponse.Length > 0)
  253. {
  254. lastresponse.Append("<map><key>folders</key><array>");
  255. lastresponse.Append(tmpresponse.ToString());
  256. lastresponse.Append("</array></map>");
  257. }
  258. else
  259. lastresponse.Append("<map><key>folders</key><array /></map>");
  260. if(tmpbadfolders.Length > 0)
  261. {
  262. lastresponse.Append("<map><key>bad_folders</key><array>");
  263. lastresponse.Append(tmpbadfolders.ToString());
  264. lastresponse.Append("</array></map>");
  265. }
  266. lastresponse.Append("</llsd>");
  267. return lastresponse.ToString();
  268. }
  269. /// <summary>
  270. /// Handle the caps inventory descendents fetch.
  271. /// </summary>
  272. /// <param name="agentID"></param>
  273. /// <param name="folderID"></param>
  274. /// <param name="ownerID"></param>
  275. /// <param name="fetchFolders"></param>
  276. /// <param name="fetchItems"></param>
  277. /// <param name="sortOrder"></param>
  278. /// <param name="version"></param>
  279. /// <returns>An empty InventoryCollection if the inventory look up failed</returns>
  280. [Obsolete]
  281. private InventoryCollection Fetch(
  282. UUID agentID, UUID folderID, UUID ownerID,
  283. bool fetchFolders, bool fetchItems, int sortOrder, out int version, out int descendents)
  284. {
  285. //m_log.DebugFormat(
  286. // "[WEB FETCH INV DESC HANDLER]: Fetching folders ({0}), items ({1}) from {2} for agent {3}",
  287. // fetchFolders, fetchItems, folderID, agentID);
  288. // FIXME MAYBE: We're not handling sortOrder!
  289. version = 0;
  290. descendents = 0;
  291. InventoryFolderImpl fold;
  292. if (m_LibraryService != null && m_LibraryService.LibraryRootFolder != null && agentID == m_LibraryService.LibraryRootFolder.Owner)
  293. {
  294. if ((fold = m_LibraryService.LibraryRootFolder.FindFolder(folderID)) != null)
  295. {
  296. InventoryCollection ret = new InventoryCollection();
  297. ret.Folders = new List<InventoryFolderBase>();
  298. ret.Items = fold.RequestListOfItems();
  299. descendents = ret.Folders.Count + ret.Items.Count;
  300. return ret;
  301. }
  302. }
  303. InventoryCollection contents = new InventoryCollection();
  304. if (folderID != UUID.Zero)
  305. {
  306. InventoryCollection fetchedContents = m_InventoryService.GetFolderContent(agentID, folderID);
  307. if (fetchedContents == null)
  308. {
  309. m_log.WarnFormat("[WEB FETCH INV DESC HANDLER]: Could not get contents of folder {0} for user {1}", folderID, agentID);
  310. return contents;
  311. }
  312. contents = fetchedContents;
  313. InventoryFolderBase containingFolder = m_InventoryService.GetFolder(agentID, folderID);
  314. if (containingFolder != null)
  315. {
  316. //m_log.DebugFormat(
  317. // "[WEB FETCH INV DESC HANDLER]: Retrieved folder {0} {1} for agent id {2}",
  318. // containingFolder.Name, containingFolder.ID, agentID);
  319. version = containingFolder.Version;
  320. if (fetchItems && containingFolder.Type != (short)FolderType.Trash)
  321. {
  322. List<InventoryItemBase> itemsToReturn = contents.Items;
  323. List<InventoryItemBase> originalItems = new List<InventoryItemBase>(itemsToReturn);
  324. // descendents must only include the links, not the linked items we add
  325. descendents = originalItems.Count;
  326. // Add target items for links in this folder before the links themselves.
  327. foreach (InventoryItemBase item in originalItems)
  328. {
  329. if (item.AssetType == (int)AssetType.Link)
  330. {
  331. InventoryItemBase linkedItem = m_InventoryService.GetItem(agentID, item.AssetID);
  332. // Take care of genuinely broken links where the target doesn't exist
  333. // HACK: Also, don't follow up links that just point to other links. In theory this is legitimate,
  334. // but no viewer has been observed to set these up and this is the lazy way of avoiding cycles
  335. // rather than having to keep track of every folder requested in the recursion.
  336. if (linkedItem != null && linkedItem.AssetType != (int)AssetType.Link)
  337. itemsToReturn.Insert(0, linkedItem);
  338. }
  339. }
  340. }
  341. }
  342. }
  343. else
  344. {
  345. // Lost items don't really need a version
  346. version = 1;
  347. }
  348. return contents;
  349. }
  350. private void AddLibraryFolders(List<LLSDFetchInventoryDescendents> libFolders, List<InventoryCollectionWithDescendents> result)
  351. {
  352. InventoryFolderImpl fold;
  353. foreach (LLSDFetchInventoryDescendents f in libFolders)
  354. {
  355. if ((fold = m_LibraryService.LibraryRootFolder.FindFolder(f.folder_id)) != null)
  356. {
  357. InventoryCollectionWithDescendents ret = new InventoryCollectionWithDescendents();
  358. ret.Collection = new InventoryCollection();
  359. // ret.Collection.Folders = new List<InventoryFolderBase>();
  360. ret.Collection.Folders = fold.RequestListOfFolders();
  361. ret.Collection.Items = fold.RequestListOfItems();
  362. ret.Collection.OwnerID = m_LibraryService.LibraryRootFolder.Owner;
  363. ret.Collection.FolderID = f.folder_id;
  364. ret.Collection.Version = fold.Version;
  365. ret.Descendents = ret.Collection.Items.Count + ret.Collection.Folders.Count;
  366. result.Add(ret);
  367. //m_log.DebugFormat("[XXX]: Added libfolder {0} ({1}) {2}", ret.Collection.FolderID, ret.Collection.OwnerID);
  368. }
  369. }
  370. }
  371. private List<InventoryCollectionWithDescendents> Fetch(List<LLSDFetchInventoryDescendents> fetchFolders, List<UUID> bad_folders)
  372. {
  373. //m_log.DebugFormat(
  374. // "[WEB FETCH INV DESC HANDLER]: Fetching {0} folders for owner {1}", fetchFolders.Count, fetchFolders[0].owner_id);
  375. // FIXME MAYBE: We're not handling sortOrder!
  376. List<InventoryCollectionWithDescendents> result = new List<InventoryCollectionWithDescendents>();
  377. if(fetchFolders.Count <= 0)
  378. return result;
  379. List<LLSDFetchInventoryDescendents> libFolders = new List<LLSDFetchInventoryDescendents>();
  380. List<LLSDFetchInventoryDescendents> otherFolders = new List<LLSDFetchInventoryDescendents>();
  381. HashSet<UUID> libIDs = new HashSet<UUID>();
  382. HashSet<UUID> otherIDs = new HashSet<UUID>();
  383. bool dolib = (m_LibraryService != null && m_LibraryService.LibraryRootFolder != null);
  384. UUID libOwner = UUID.Zero;
  385. if(dolib)
  386. libOwner = m_LibraryService.LibraryRootFolder.Owner;
  387. // Filter folder Zero right here. Some viewers (Firestorm) send request for folder Zero, which doesn't make sense
  388. // and can kill the sim (all root folders have parent_id Zero)
  389. // send something.
  390. foreach(LLSDFetchInventoryDescendents f in fetchFolders)
  391. {
  392. if (f.folder_id == UUID.Zero)
  393. {
  394. InventoryCollectionWithDescendents zeroColl = new InventoryCollectionWithDescendents();
  395. zeroColl.Collection = new InventoryCollection();
  396. zeroColl.Collection.OwnerID = f.owner_id;
  397. zeroColl.Collection.Version = 0;
  398. zeroColl.Collection.FolderID = f.folder_id;
  399. zeroColl.Descendents = 0;
  400. result.Add(zeroColl);
  401. continue;
  402. }
  403. if(dolib && f.owner_id == libOwner)
  404. {
  405. if(libIDs.Contains(f.folder_id))
  406. continue;
  407. libIDs.Add(f.folder_id);
  408. libFolders.Add(f);
  409. continue;
  410. }
  411. if(otherIDs.Contains(f.folder_id))
  412. continue;
  413. otherIDs.Add(f.folder_id);
  414. otherFolders.Add(f);
  415. }
  416. if(otherFolders.Count > 0)
  417. {
  418. UUID[] fids = new UUID[otherFolders.Count];
  419. int i = 0;
  420. foreach (LLSDFetchInventoryDescendents f in otherFolders)
  421. fids[i++] = f.folder_id;
  422. //m_log.DebugFormat("[XXX]: {0}", string.Join(",", fids));
  423. InventoryCollection[] fetchedContents = m_InventoryService.GetMultipleFoldersContent(otherFolders[0].owner_id, fids);
  424. if (fetchedContents == null)
  425. return null;
  426. if (fetchedContents.Length == 0)
  427. {
  428. foreach (LLSDFetchInventoryDescendents freq in otherFolders)
  429. BadFolder(freq, null, bad_folders);
  430. }
  431. else
  432. {
  433. i = 0;
  434. // Do some post-processing. May need to fetch more from inv server for links
  435. foreach (InventoryCollection contents in fetchedContents)
  436. {
  437. // Find the original request
  438. LLSDFetchInventoryDescendents freq = otherFolders[i++];
  439. InventoryCollectionWithDescendents coll = new InventoryCollectionWithDescendents();
  440. coll.Collection = contents;
  441. if (BadFolder(freq, contents, bad_folders))
  442. continue;
  443. // Next: link management
  444. ProcessLinks(freq, coll);
  445. result.Add(coll);
  446. }
  447. }
  448. }
  449. if(dolib && libFolders.Count > 0)
  450. {
  451. AddLibraryFolders(libFolders, result);
  452. }
  453. return result;
  454. }
  455. private bool BadFolder(LLSDFetchInventoryDescendents freq, InventoryCollection contents, List<UUID> bad_folders)
  456. {
  457. bool bad = false;
  458. if (contents == null)
  459. {
  460. bad_folders.Add(freq.folder_id);
  461. bad = true;
  462. }
  463. // The inventory server isn't sending FolderID in the collection...
  464. // Must fetch it individually
  465. else if (contents.FolderID == UUID.Zero)
  466. {
  467. InventoryFolderBase containingFolder = m_InventoryService.GetFolder(freq.owner_id, freq.folder_id);
  468. if (containingFolder != null)
  469. {
  470. contents.FolderID = containingFolder.ID;
  471. contents.OwnerID = containingFolder.Owner;
  472. contents.Version = containingFolder.Version;
  473. }
  474. else
  475. {
  476. m_log.WarnFormat("[WEB FETCH INV DESC HANDLER]: Unable to fetch folder {0}", freq.folder_id);
  477. bad_folders.Add(freq.folder_id);
  478. bad = true;
  479. }
  480. }
  481. return bad;
  482. }
  483. private void ProcessLinks(LLSDFetchInventoryDescendents freq, InventoryCollectionWithDescendents coll)
  484. {
  485. InventoryCollection contents = coll.Collection;
  486. if (freq.fetch_items && contents.Items != null)
  487. {
  488. // viewers are lasy and want a copy of the linked item sent before the link to it
  489. // descendents must only include the links, not the linked items we add
  490. coll.Descendents = contents.Items.Count + contents.Folders.Count;
  491. // look for item links
  492. List<UUID> itemIDs = new List<UUID>();
  493. foreach (InventoryItemBase item in contents.Items)
  494. {
  495. //m_log.DebugFormat("[XXX]: {0} {1}", item.Name, item.AssetType);
  496. if (item.AssetType == (int)AssetType.Link)
  497. itemIDs.Add(item.AssetID);
  498. }
  499. // get the linked if any
  500. if (itemIDs.Count > 0)
  501. {
  502. InventoryItemBase[] linked = m_InventoryService.GetMultipleItems(freq.owner_id, itemIDs.ToArray());
  503. if (linked == null)
  504. {
  505. // OMG!!! One by one!!! This is fallback code, in case the backend isn't updated
  506. m_log.WarnFormat("[WEB FETCH INV DESC HANDLER]: GetMultipleItems failed. Falling back to fetching inventory items one by one.");
  507. linked = new InventoryItemBase[itemIDs.Count];
  508. int i = 0;
  509. foreach (UUID id in itemIDs)
  510. {
  511. linked[i++] = m_InventoryService.GetItem(freq.owner_id, id);
  512. }
  513. }
  514. if (linked != null)
  515. {
  516. List<InventoryItemBase> linkedItems = new List<InventoryItemBase>();
  517. // check for broken
  518. foreach (InventoryItemBase linkedItem in linked)
  519. {
  520. // Take care of genuinely broken links where the target doesn't exist
  521. // HACK: Also, don't follow up links that just point to other links. In theory this is legitimate,
  522. // but no viewer has been observed to set these up and this is the lazy way of avoiding cycles
  523. // rather than having to keep track of every folder requested in the recursion.
  524. if (linkedItem != null && linkedItem.AssetType != (int)AssetType.Link)
  525. {
  526. linkedItems.Add(linkedItem);
  527. //m_log.DebugFormat("[WEB FETCH INV DESC HANDLER]: Added {0} {1} {2}", linkedItem.Name, linkedItem.AssetType, linkedItem.Folder);
  528. }
  529. }
  530. // insert them
  531. if(linkedItems.Count > 0)
  532. contents.Items.InsertRange(0,linkedItems);
  533. }
  534. }
  535. }
  536. }
  537. /// <summary>
  538. /// Convert an internal inventory folder object into an LLSD object.
  539. /// </summary>
  540. /// <param name="invFolder"></param>
  541. /// <returns></returns>
  542. private LLSDInventoryFolder ConvertInventoryFolder(InventoryFolderBase invFolder)
  543. {
  544. LLSDInventoryFolder llsdFolder = new LLSDInventoryFolder();
  545. llsdFolder.folder_id = invFolder.ID;
  546. llsdFolder.parent_id = invFolder.ParentID;
  547. llsdFolder.name = invFolder.Name;
  548. llsdFolder.type = invFolder.Type;
  549. llsdFolder.preferred_type = -1;
  550. return llsdFolder;
  551. }
  552. /// <summary>
  553. /// Convert an internal inventory item object into an LLSD object.
  554. /// </summary>
  555. /// <param name="invItem"></param>
  556. /// <returns></returns>
  557. private LLSDInventoryItem ConvertInventoryItem(InventoryItemBase invItem)
  558. {
  559. LLSDInventoryItem llsdItem = new LLSDInventoryItem();
  560. llsdItem.asset_id = invItem.AssetID;
  561. llsdItem.created_at = invItem.CreationDate;
  562. llsdItem.desc = invItem.Description;
  563. llsdItem.flags = (int)invItem.Flags;
  564. llsdItem.item_id = invItem.ID;
  565. llsdItem.name = invItem.Name;
  566. llsdItem.parent_id = invItem.Folder;
  567. llsdItem.type = invItem.AssetType;
  568. llsdItem.inv_type = invItem.InvType;
  569. llsdItem.permissions = new LLSDPermissions();
  570. llsdItem.permissions.creator_id = invItem.CreatorIdAsUuid;
  571. llsdItem.permissions.base_mask = (int)invItem.CurrentPermissions;
  572. llsdItem.permissions.everyone_mask = (int)invItem.EveryOnePermissions;
  573. llsdItem.permissions.group_id = invItem.GroupID;
  574. llsdItem.permissions.group_mask = (int)invItem.GroupPermissions;
  575. llsdItem.permissions.is_owner_group = invItem.GroupOwned;
  576. llsdItem.permissions.next_owner_mask = (int)invItem.NextPermissions;
  577. llsdItem.permissions.owner_id = invItem.Owner;
  578. llsdItem.permissions.owner_mask = (int)invItem.CurrentPermissions;
  579. llsdItem.sale_info = new LLSDSaleInfo();
  580. llsdItem.sale_info.sale_price = invItem.SalePrice;
  581. llsdItem.sale_info.sale_type = invItem.SaleType;
  582. return llsdItem;
  583. }
  584. }
  585. class InventoryCollectionWithDescendents
  586. {
  587. public InventoryCollection Collection;
  588. public int Descendents;
  589. }
  590. }