PageRenderTime 82ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/OpenSim/Region/Framework/Scenes/Scene.PacketHandlers.cs

https://gitlab.com/N3X15/VoxelSim
C# | 725 lines | 471 code | 87 blank | 167 comment | 110 complexity | 1011bea1f161997e436c947cd46238c3 MD5 | raw file
Possible License(s): BSD-3-Clause
  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.Threading;
  30. using OpenMetaverse;
  31. using OpenMetaverse.Packets;
  32. using OpenSim.Framework;
  33. using OpenSim.Framework.Communications;
  34. using OpenSim.Services.Interfaces;
  35. namespace OpenSim.Region.Framework.Scenes
  36. {
  37. public partial class Scene
  38. {
  39. protected void SimChat(byte[] message, ChatTypeEnum type, int channel, Vector3 fromPos, string fromName,
  40. UUID fromID, bool fromAgent, bool broadcast)
  41. {
  42. OSChatMessage args = new OSChatMessage();
  43. args.Message = Utils.BytesToString(message);
  44. args.Channel = channel;
  45. args.Type = type;
  46. args.Position = fromPos;
  47. args.SenderUUID = fromID;
  48. args.Scene = this;
  49. if (fromAgent)
  50. {
  51. ScenePresence user = GetScenePresence(fromID);
  52. if (user != null)
  53. args.Sender = user.ControllingClient;
  54. }
  55. else
  56. {
  57. SceneObjectPart obj = GetSceneObjectPart(fromID);
  58. args.SenderObject = obj;
  59. }
  60. args.From = fromName;
  61. //args.
  62. if (broadcast)
  63. EventManager.TriggerOnChatBroadcast(this, args);
  64. else
  65. EventManager.TriggerOnChatFromWorld(this, args);
  66. }
  67. /// <summary>
  68. ///
  69. /// </summary>
  70. /// <param name="message"></param>
  71. /// <param name="type"></param>
  72. /// <param name="fromPos"></param>
  73. /// <param name="fromName"></param>
  74. /// <param name="fromAgentID"></param>
  75. public void SimChat(byte[] message, ChatTypeEnum type, int channel, Vector3 fromPos, string fromName,
  76. UUID fromID, bool fromAgent)
  77. {
  78. SimChat(message, type, channel, fromPos, fromName, fromID, fromAgent, false);
  79. }
  80. public void SimChat(string message, ChatTypeEnum type, Vector3 fromPos, string fromName, UUID fromID, bool fromAgent)
  81. {
  82. SimChat(Utils.StringToBytes(message), type, 0, fromPos, fromName, fromID, fromAgent);
  83. }
  84. public void SimChat(string message, string fromName)
  85. {
  86. SimChat(message, ChatTypeEnum.Broadcast, Vector3.Zero, fromName, UUID.Zero, false);
  87. }
  88. /// <summary>
  89. ///
  90. /// </summary>
  91. /// <param name="message"></param>
  92. /// <param name="type"></param>
  93. /// <param name="fromPos"></param>
  94. /// <param name="fromName"></param>
  95. /// <param name="fromAgentID"></param>
  96. public void SimChatBroadcast(byte[] message, ChatTypeEnum type, int channel, Vector3 fromPos, string fromName,
  97. UUID fromID, bool fromAgent)
  98. {
  99. SimChat(message, type, channel, fromPos, fromName, fromID, fromAgent, true);
  100. }
  101. /// <summary>
  102. /// Invoked when the client requests a prim.
  103. /// </summary>
  104. /// <param name="primLocalID"></param>
  105. /// <param name="remoteClient"></param>
  106. public void RequestPrim(uint primLocalID, IClientAPI remoteClient)
  107. {
  108. EntityBase[] entityList = GetEntities();
  109. foreach (EntityBase ent in entityList)
  110. {
  111. if (ent is SceneObjectGroup)
  112. {
  113. if (((SceneObjectGroup)ent).LocalId == primLocalID)
  114. {
  115. ((SceneObjectGroup)ent).SendFullUpdateToClient(remoteClient);
  116. return;
  117. }
  118. }
  119. }
  120. }
  121. /// <summary>
  122. /// Invoked when the client selects a prim.
  123. /// </summary>
  124. /// <param name="primLocalID"></param>
  125. /// <param name="remoteClient"></param>
  126. public void SelectPrim(uint primLocalID, IClientAPI remoteClient)
  127. {
  128. EntityBase[] entityList = GetEntities();
  129. foreach (EntityBase ent in entityList)
  130. {
  131. if (ent is SceneObjectGroup)
  132. {
  133. if (((SceneObjectGroup) ent).LocalId == primLocalID)
  134. {
  135. ((SceneObjectGroup) ent).GetProperties(remoteClient);
  136. ((SceneObjectGroup) ent).IsSelected = true;
  137. // A prim is only tainted if it's allowed to be edited by the person clicking it.
  138. if (Permissions.CanEditObject(((SceneObjectGroup)ent).UUID, remoteClient.AgentId)
  139. || Permissions.CanMoveObject(((SceneObjectGroup)ent).UUID, remoteClient.AgentId))
  140. {
  141. EventManager.TriggerParcelPrimCountTainted();
  142. }
  143. break;
  144. }
  145. else
  146. {
  147. // We also need to check the children of this prim as they
  148. // can be selected as well and send property information
  149. bool foundPrim = false;
  150. SceneObjectGroup sog = ent as SceneObjectGroup;
  151. SceneObjectPart[] partList = sog.Parts;
  152. foreach (SceneObjectPart part in partList)
  153. {
  154. if (part.LocalId == primLocalID)
  155. {
  156. part.GetProperties(remoteClient);
  157. foundPrim = true;
  158. break;
  159. }
  160. }
  161. if (foundPrim)
  162. break;
  163. }
  164. }
  165. }
  166. }
  167. /// <summary>
  168. /// Handle the deselection of a prim from the client.
  169. /// </summary>
  170. /// <param name="primLocalID"></param>
  171. /// <param name="remoteClient"></param>
  172. public void DeselectPrim(uint primLocalID, IClientAPI remoteClient)
  173. {
  174. SceneObjectPart part = GetSceneObjectPart(primLocalID);
  175. if (part == null)
  176. return;
  177. // The prim is in the process of being deleted.
  178. if (null == part.ParentGroup.RootPart)
  179. return;
  180. // A deselect packet contains all the local prims being deselected. However, since selection is still
  181. // group based we only want the root prim to trigger a full update - otherwise on objects with many prims
  182. // we end up sending many duplicate ObjectUpdates
  183. if (part.ParentGroup.RootPart.LocalId != part.LocalId)
  184. return;
  185. bool isAttachment = false;
  186. // This is wrong, wrong, wrong. Selection should not be
  187. // handled by group, but by prim. Legacy cruft.
  188. // TODO: Make selection flagging per prim!
  189. //
  190. part.ParentGroup.IsSelected = false;
  191. if (part.ParentGroup.IsAttachment)
  192. isAttachment = true;
  193. else
  194. part.ParentGroup.ScheduleGroupForFullUpdate();
  195. // If it's not an attachment, and we are allowed to move it,
  196. // then we might have done so. If we moved across a parcel
  197. // boundary, we will need to recount prims on the parcels.
  198. // For attachments, that makes no sense.
  199. //
  200. if (!isAttachment)
  201. {
  202. if (Permissions.CanEditObject(
  203. part.UUID, remoteClient.AgentId)
  204. || Permissions.CanMoveObject(
  205. part.UUID, remoteClient.AgentId))
  206. EventManager.TriggerParcelPrimCountTainted();
  207. }
  208. }
  209. public virtual void ProcessMoneyTransferRequest(UUID source, UUID destination, int amount,
  210. int transactiontype, string description)
  211. {
  212. EventManager.MoneyTransferArgs args = new EventManager.MoneyTransferArgs(source, destination, amount,
  213. transactiontype, description);
  214. EventManager.TriggerMoneyTransfer(this, args);
  215. }
  216. public virtual void ProcessParcelBuy(UUID agentId, UUID groupId, bool final, bool groupOwned,
  217. bool removeContribution, int parcelLocalID, int parcelArea, int parcelPrice, bool authenticated)
  218. {
  219. EventManager.LandBuyArgs args = new EventManager.LandBuyArgs(agentId, groupId, final, groupOwned,
  220. removeContribution, parcelLocalID, parcelArea,
  221. parcelPrice, authenticated);
  222. // First, allow all validators a stab at it
  223. m_eventManager.TriggerValidateLandBuy(this, args);
  224. // Then, check validation and transfer
  225. m_eventManager.TriggerLandBuy(this, args);
  226. }
  227. public virtual void ProcessObjectGrab(uint localID, Vector3 offsetPos, IClientAPI remoteClient, List<SurfaceTouchEventArgs> surfaceArgs)
  228. {
  229. EntityBase[] EntityList = GetEntities();
  230. SurfaceTouchEventArgs surfaceArg = null;
  231. if (surfaceArgs != null && surfaceArgs.Count > 0)
  232. surfaceArg = surfaceArgs[0];
  233. foreach (EntityBase ent in EntityList)
  234. {
  235. if (ent is SceneObjectGroup)
  236. {
  237. SceneObjectGroup obj = ent as SceneObjectGroup;
  238. if (obj != null)
  239. {
  240. // Is this prim part of the group
  241. if (obj.HasChildPrim(localID))
  242. {
  243. // Currently only grab/touch for the single prim
  244. // the client handles rez correctly
  245. obj.ObjectGrabHandler(localID, offsetPos, remoteClient);
  246. SceneObjectPart part = obj.GetChildPart(localID);
  247. // If the touched prim handles touches, deliver it
  248. // If not, deliver to root prim
  249. if ((part.ScriptEvents & scriptEvents.touch_start) != 0)
  250. EventManager.TriggerObjectGrab(part.LocalId, 0, part.OffsetPosition, remoteClient, surfaceArg);
  251. // Deliver to the root prim if the touched prim doesn't handle touches
  252. // or if we're meant to pass on touches anyway. Don't send to root prim
  253. // if prim touched is the root prim as we just did it
  254. if (((part.ScriptEvents & scriptEvents.touch_start) == 0) ||
  255. (part.PassTouches && (part.LocalId != obj.RootPart.LocalId)))
  256. {
  257. EventManager.TriggerObjectGrab(obj.RootPart.LocalId, part.LocalId, part.OffsetPosition, remoteClient, surfaceArg);
  258. }
  259. return;
  260. }
  261. }
  262. }
  263. }
  264. }
  265. public virtual void ProcessObjectGrabUpdate(UUID objectID, Vector3 offset, Vector3 pos, IClientAPI remoteClient, List<SurfaceTouchEventArgs> surfaceArgs)
  266. {
  267. EntityBase[] EntityList = GetEntities();
  268. SurfaceTouchEventArgs surfaceArg = null;
  269. if (surfaceArgs != null && surfaceArgs.Count > 0)
  270. surfaceArg = surfaceArgs[0];
  271. foreach (EntityBase ent in EntityList)
  272. {
  273. if (ent is SceneObjectGroup)
  274. {
  275. SceneObjectGroup obj = ent as SceneObjectGroup;
  276. if (obj != null)
  277. {
  278. // Is this prim part of the group
  279. if (obj.HasChildPrim(objectID))
  280. {
  281. SceneObjectPart part = obj.GetChildPart(objectID);
  282. // If the touched prim handles touches, deliver it
  283. // If not, deliver to root prim
  284. if ((part.ScriptEvents & scriptEvents.touch) != 0)
  285. EventManager.TriggerObjectGrabbing(part.LocalId, 0, part.OffsetPosition, remoteClient, surfaceArg);
  286. // Deliver to the root prim if the touched prim doesn't handle touches
  287. // or if we're meant to pass on touches anyway. Don't send to root prim
  288. // if prim touched is the root prim as we just did it
  289. if (((part.ScriptEvents & scriptEvents.touch) == 0) ||
  290. (part.PassTouches && (part.LocalId != obj.RootPart.LocalId)))
  291. {
  292. EventManager.TriggerObjectGrabbing(obj.RootPart.LocalId, part.LocalId, part.OffsetPosition, remoteClient, surfaceArg);
  293. }
  294. return;
  295. }
  296. }
  297. }
  298. }
  299. }
  300. public virtual void ProcessObjectDeGrab(uint localID, IClientAPI remoteClient, List<SurfaceTouchEventArgs> surfaceArgs)
  301. {
  302. EntityBase[] EntityList = GetEntities();
  303. SurfaceTouchEventArgs surfaceArg = null;
  304. if (surfaceArgs != null && surfaceArgs.Count > 0)
  305. surfaceArg = surfaceArgs[0];
  306. foreach (EntityBase ent in EntityList)
  307. {
  308. if (ent is SceneObjectGroup)
  309. {
  310. SceneObjectGroup obj = ent as SceneObjectGroup;
  311. // Is this prim part of the group
  312. if (obj.HasChildPrim(localID))
  313. {
  314. SceneObjectPart part=obj.GetChildPart(localID);
  315. if (part != null)
  316. {
  317. // If the touched prim handles touches, deliver it
  318. // If not, deliver to root prim
  319. if ((part.ScriptEvents & scriptEvents.touch_end) != 0)
  320. EventManager.TriggerObjectDeGrab(part.LocalId, 0, remoteClient, surfaceArg);
  321. else
  322. EventManager.TriggerObjectDeGrab(obj.RootPart.LocalId, part.LocalId, remoteClient, surfaceArg);
  323. return;
  324. }
  325. return;
  326. }
  327. }
  328. }
  329. }
  330. public void ProcessAvatarPickerRequest(IClientAPI client, UUID avatarID, UUID RequestID, string query)
  331. {
  332. //EventManager.TriggerAvatarPickerRequest();
  333. List<UserAccount> accounts = UserAccountService.GetUserAccounts(RegionInfo.ScopeID, query);
  334. if (accounts == null)
  335. return;
  336. AvatarPickerReplyPacket replyPacket = (AvatarPickerReplyPacket) PacketPool.Instance.GetPacket(PacketType.AvatarPickerReply);
  337. // TODO: don't create new blocks if recycling an old packet
  338. AvatarPickerReplyPacket.DataBlock[] searchData =
  339. new AvatarPickerReplyPacket.DataBlock[accounts.Count];
  340. AvatarPickerReplyPacket.AgentDataBlock agentData = new AvatarPickerReplyPacket.AgentDataBlock();
  341. agentData.AgentID = avatarID;
  342. agentData.QueryID = RequestID;
  343. replyPacket.AgentData = agentData;
  344. //byte[] bytes = new byte[AvatarResponses.Count*32];
  345. int i = 0;
  346. foreach (UserAccount item in accounts)
  347. {
  348. UUID translatedIDtem = item.PrincipalID;
  349. searchData[i] = new AvatarPickerReplyPacket.DataBlock();
  350. searchData[i].AvatarID = translatedIDtem;
  351. searchData[i].FirstName = Utils.StringToBytes((string) item.FirstName);
  352. searchData[i].LastName = Utils.StringToBytes((string) item.LastName);
  353. i++;
  354. }
  355. if (accounts.Count == 0)
  356. {
  357. searchData = new AvatarPickerReplyPacket.DataBlock[0];
  358. }
  359. replyPacket.Data = searchData;
  360. AvatarPickerReplyAgentDataArgs agent_data = new AvatarPickerReplyAgentDataArgs();
  361. agent_data.AgentID = replyPacket.AgentData.AgentID;
  362. agent_data.QueryID = replyPacket.AgentData.QueryID;
  363. List<AvatarPickerReplyDataArgs> data_args = new List<AvatarPickerReplyDataArgs>();
  364. for (i = 0; i < replyPacket.Data.Length; i++)
  365. {
  366. AvatarPickerReplyDataArgs data_arg = new AvatarPickerReplyDataArgs();
  367. data_arg.AvatarID = replyPacket.Data[i].AvatarID;
  368. data_arg.FirstName = replyPacket.Data[i].FirstName;
  369. data_arg.LastName = replyPacket.Data[i].LastName;
  370. data_args.Add(data_arg);
  371. }
  372. client.SendAvatarPickerReply(agent_data, data_args);
  373. }
  374. public void ProcessScriptReset(IClientAPI remoteClient, UUID objectID,
  375. UUID itemID)
  376. {
  377. SceneObjectPart part=GetSceneObjectPart(objectID);
  378. if (part == null)
  379. return;
  380. if (Permissions.CanResetScript(objectID, itemID, remoteClient.AgentId))
  381. {
  382. EventManager.TriggerScriptReset(part.LocalId, itemID);
  383. }
  384. }
  385. void ProcessViewerEffect(IClientAPI remoteClient, List<ViewerEffectEventHandlerArg> args)
  386. {
  387. // TODO: don't create new blocks if recycling an old packet
  388. ViewerEffectPacket.EffectBlock[] effectBlockArray = new ViewerEffectPacket.EffectBlock[args.Count];
  389. for (int i = 0; i < args.Count; i++)
  390. {
  391. ViewerEffectPacket.EffectBlock effect = new ViewerEffectPacket.EffectBlock();
  392. effect.AgentID = args[i].AgentID;
  393. effect.Color = args[i].Color;
  394. effect.Duration = args[i].Duration;
  395. effect.ID = args[i].ID;
  396. effect.Type = args[i].Type;
  397. effect.TypeData = args[i].TypeData;
  398. effectBlockArray[i] = effect;
  399. }
  400. ForEachClient(
  401. delegate(IClientAPI client)
  402. {
  403. if (client.AgentId != remoteClient.AgentId)
  404. client.SendViewerEffect(effectBlockArray);
  405. }
  406. );
  407. }
  408. public void HandleUUIDNameRequest(UUID uuid, IClientAPI remote_client)
  409. {
  410. if (LibraryService != null && (LibraryService.LibraryRootFolder.Owner == uuid))
  411. {
  412. remote_client.SendNameReply(uuid, "Mr", "OpenSim");
  413. }
  414. else
  415. {
  416. string[] names = GetUserNames(uuid);
  417. if (names.Length == 2)
  418. {
  419. remote_client.SendNameReply(uuid, names[0], names[1]);
  420. }
  421. }
  422. }
  423. /// <summary>
  424. /// Handle a fetch inventory request from the client
  425. /// </summary>
  426. /// <param name="remoteClient"></param>
  427. /// <param name="itemID"></param>
  428. /// <param name="ownerID"></param>
  429. public void HandleFetchInventory(IClientAPI remoteClient, UUID itemID, UUID ownerID)
  430. {
  431. if (LibraryService != null && LibraryService.LibraryRootFolder != null && ownerID == LibraryService.LibraryRootFolder.Owner)
  432. {
  433. //m_log.Debug("request info for library item");
  434. return;
  435. }
  436. InventoryItemBase item = new InventoryItemBase(itemID, remoteClient.AgentId);
  437. item = InventoryService.GetItem(item);
  438. if (item != null)
  439. {
  440. remoteClient.SendInventoryItemDetails(ownerID, item);
  441. }
  442. // else shouldn't we send an alert message?
  443. }
  444. /// <summary>
  445. /// Tell the client about the various child items and folders contained in the requested folder.
  446. /// </summary>
  447. /// <param name="remoteClient"></param>
  448. /// <param name="folderID"></param>
  449. /// <param name="ownerID"></param>
  450. /// <param name="fetchFolders"></param>
  451. /// <param name="fetchItems"></param>
  452. /// <param name="sortOrder"></param>
  453. public void HandleFetchInventoryDescendents(IClientAPI remoteClient, UUID folderID, UUID ownerID,
  454. bool fetchFolders, bool fetchItems, int sortOrder)
  455. {
  456. if (folderID == UUID.Zero)
  457. return;
  458. // FIXME MAYBE: We're not handling sortOrder!
  459. // TODO: This code for looking in the folder for the library should be folded somewhere else
  460. // so that this class doesn't have to know the details (and so that multiple libraries, etc.
  461. // can be handled transparently).
  462. InventoryFolderImpl fold = null;
  463. if (LibraryService != null && LibraryService.LibraryRootFolder != null)
  464. if ((fold = LibraryService.LibraryRootFolder.FindFolder(folderID)) != null)
  465. {
  466. remoteClient.SendInventoryFolderDetails(
  467. fold.Owner, folderID, fold.RequestListOfItems(),
  468. fold.RequestListOfFolders(), fold.Version, fetchFolders, fetchItems);
  469. return;
  470. }
  471. // We're going to send the reply async, because there may be
  472. // an enormous quantity of packets -- basically the entire inventory!
  473. // We don't want to block the client thread while all that is happening.
  474. SendInventoryDelegate d = SendInventoryAsync;
  475. d.BeginInvoke(remoteClient, folderID, ownerID, fetchFolders, fetchItems, sortOrder, SendInventoryComplete, d);
  476. }
  477. delegate void SendInventoryDelegate(IClientAPI remoteClient, UUID folderID, UUID ownerID, bool fetchFolders, bool fetchItems, int sortOrder);
  478. void SendInventoryAsync(IClientAPI remoteClient, UUID folderID, UUID ownerID, bool fetchFolders, bool fetchItems, int sortOrder)
  479. {
  480. SendInventoryUpdate(remoteClient, new InventoryFolderBase(folderID), fetchFolders, fetchItems);
  481. }
  482. void SendInventoryComplete(IAsyncResult iar)
  483. {
  484. SendInventoryDelegate d = (SendInventoryDelegate)iar.AsyncState;
  485. d.EndInvoke(iar);
  486. }
  487. /// <summary>
  488. /// Handle the caps inventory descendents fetch.
  489. ///
  490. /// Since the folder structure is sent to the client on login, I believe we only need to handle items.
  491. /// Diva comment 8/13/2009: what if someone gave us a folder in the meantime??
  492. /// </summary>
  493. /// <param name="agentID"></param>
  494. /// <param name="folderID"></param>
  495. /// <param name="ownerID"></param>
  496. /// <param name="fetchFolders"></param>
  497. /// <param name="fetchItems"></param>
  498. /// <param name="sortOrder"></param>
  499. /// <returns>null if the inventory look up failed</returns>
  500. public InventoryCollection HandleFetchInventoryDescendentsCAPS(UUID agentID, UUID folderID, UUID ownerID,
  501. bool fetchFolders, bool fetchItems, int sortOrder, out int version)
  502. {
  503. m_log.DebugFormat(
  504. "[INVENTORY CACHE]: Fetching folders ({0}), items ({1}) from {2} for agent {3}",
  505. fetchFolders, fetchItems, folderID, agentID);
  506. // FIXME MAYBE: We're not handling sortOrder!
  507. // TODO: This code for looking in the folder for the library should be folded back into the
  508. // CachedUserInfo so that this class doesn't have to know the details (and so that multiple libraries, etc.
  509. // can be handled transparently).
  510. InventoryFolderImpl fold;
  511. if (LibraryService != null && LibraryService.LibraryRootFolder != null)
  512. if ((fold = LibraryService.LibraryRootFolder.FindFolder(folderID)) != null)
  513. {
  514. version = 0;
  515. InventoryCollection ret = new InventoryCollection();
  516. ret.Folders = new List<InventoryFolderBase>();
  517. ret.Items = fold.RequestListOfItems();
  518. return ret;
  519. }
  520. InventoryCollection contents = new InventoryCollection();
  521. if (folderID != UUID.Zero)
  522. {
  523. contents = InventoryService.GetFolderContent(agentID, folderID);
  524. InventoryFolderBase containingFolder = new InventoryFolderBase();
  525. containingFolder.ID = folderID;
  526. containingFolder.Owner = agentID;
  527. containingFolder = InventoryService.GetFolder(containingFolder);
  528. version = containingFolder.Version;
  529. }
  530. else
  531. {
  532. // Lost itemsm don't really need a version
  533. version = 1;
  534. }
  535. return contents;
  536. }
  537. /// <summary>
  538. /// Handle an inventory folder creation request from the client.
  539. /// </summary>
  540. /// <param name="remoteClient"></param>
  541. /// <param name="folderID"></param>
  542. /// <param name="folderType"></param>
  543. /// <param name="folderName"></param>
  544. /// <param name="parentID"></param>
  545. public void HandleCreateInventoryFolder(IClientAPI remoteClient, UUID folderID, ushort folderType,
  546. string folderName, UUID parentID)
  547. {
  548. InventoryFolderBase folder = new InventoryFolderBase(folderID, folderName, remoteClient.AgentId, (short)folderType, parentID, 1);
  549. if (!InventoryService.AddFolder(folder))
  550. {
  551. m_log.WarnFormat(
  552. "[AGENT INVENTORY]: Failed to move create folder for user {0} {1}",
  553. remoteClient.Name, remoteClient.AgentId);
  554. }
  555. }
  556. /// <summary>
  557. /// Handle a client request to update the inventory folder
  558. /// </summary>
  559. ///
  560. /// FIXME: We call add new inventory folder because in the data layer, we happen to use an SQL REPLACE
  561. /// so this will work to rename an existing folder. Needless to say, to rely on this is very confusing,
  562. /// and needs to be changed.
  563. ///
  564. /// <param name="remoteClient"></param>
  565. /// <param name="folderID"></param>
  566. /// <param name="type"></param>
  567. /// <param name="name"></param>
  568. /// <param name="parentID"></param>
  569. public void HandleUpdateInventoryFolder(IClientAPI remoteClient, UUID folderID, ushort type, string name,
  570. UUID parentID)
  571. {
  572. // m_log.DebugFormat(
  573. // "[AGENT INVENTORY]: Updating inventory folder {0} {1} for {2} {3}", folderID, name, remoteClient.Name, remoteClient.AgentId);
  574. InventoryFolderBase folder = new InventoryFolderBase(folderID, remoteClient.AgentId);
  575. folder = InventoryService.GetFolder(folder);
  576. if (folder != null)
  577. {
  578. folder.Name = name;
  579. folder.Type = (short)type;
  580. folder.ParentID = parentID;
  581. if (!InventoryService.UpdateFolder(folder))
  582. {
  583. m_log.ErrorFormat(
  584. "[AGENT INVENTORY]: Failed to update folder for user {0} {1}",
  585. remoteClient.Name, remoteClient.AgentId);
  586. }
  587. }
  588. }
  589. public void HandleMoveInventoryFolder(IClientAPI remoteClient, UUID folderID, UUID parentID)
  590. {
  591. InventoryFolderBase folder = new InventoryFolderBase(folderID, remoteClient.AgentId);
  592. folder = InventoryService.GetFolder(folder);
  593. if (folder != null)
  594. {
  595. folder.ParentID = parentID;
  596. if (!InventoryService.MoveFolder(folder))
  597. m_log.WarnFormat("[AGENT INVENTORY]: could not move folder {0}", folderID);
  598. else
  599. m_log.DebugFormat("[AGENT INVENTORY]: folder {0} moved to parent {1}", folderID, parentID);
  600. }
  601. else
  602. {
  603. m_log.WarnFormat("[AGENT INVENTORY]: request to move folder {0} but folder not found", folderID);
  604. }
  605. }
  606. /// <summary>
  607. /// This should delete all the items and folders in the given directory.
  608. /// </summary>
  609. /// <param name="remoteClient"></param>
  610. /// <param name="folderID"></param>
  611. delegate void PurgeFolderDelegate(UUID userID, UUID folder);
  612. public void HandlePurgeInventoryDescendents(IClientAPI remoteClient, UUID folderID)
  613. {
  614. PurgeFolderDelegate d = PurgeFolderAsync;
  615. try
  616. {
  617. d.BeginInvoke(remoteClient.AgentId, folderID, PurgeFolderCompleted, d);
  618. }
  619. catch (Exception e)
  620. {
  621. m_log.WarnFormat("[AGENT INVENTORY]: Exception on purge folder for user {0}: {1}", remoteClient.AgentId, e.Message);
  622. }
  623. }
  624. private void PurgeFolderAsync(UUID userID, UUID folderID)
  625. {
  626. InventoryFolderBase folder = new InventoryFolderBase(folderID, userID);
  627. if (InventoryService.PurgeFolder(folder))
  628. m_log.DebugFormat("[AGENT INVENTORY]: folder {0} purged successfully", folderID);
  629. else
  630. m_log.WarnFormat("[AGENT INVENTORY]: could not purge folder {0}", folderID);
  631. }
  632. private void PurgeFolderCompleted(IAsyncResult iar)
  633. {
  634. PurgeFolderDelegate d = (PurgeFolderDelegate)iar.AsyncState;
  635. d.EndInvoke(iar);
  636. }
  637. }
  638. }