Scene.PacketHandlers.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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.Concurrent;
  30. using System.Threading;
  31. using OpenMetaverse;
  32. using OpenMetaverse.Packets;
  33. using OpenSim.Framework;
  34. using OpenSim.Services.Interfaces;
  35. namespace OpenSim.Region.Framework.Scenes
  36. {
  37. public partial class Scene
  38. {
  39. /// <summary>
  40. /// Send chat to listeners.
  41. /// </summary>
  42. /// <param name='message'></param>
  43. /// <param name='type'>/param>
  44. /// <param name='channel'></param>
  45. /// <param name='fromPos'></param>
  46. /// <param name='fromName'></param>
  47. /// <param name='fromID'></param>
  48. /// <param name='targetID'></param>
  49. /// <param name='fromAgent'></param>
  50. /// <param name='broadcast'></param>
  51. public void SimChat(string message, ChatTypeEnum type, int channel, Vector3 fromPos, string fromName,
  52. UUID fromID, UUID targetID, bool fromAgent, bool broadcast)
  53. {
  54. OSChatMessage args = new OSChatMessage
  55. {
  56. Message = message,
  57. Channel = channel,
  58. Type = type,
  59. Position = fromPos,
  60. SenderUUID = fromID,
  61. Scene = this,
  62. Destination = targetID,
  63. From = fromName
  64. };
  65. if (fromAgent)
  66. {
  67. ScenePresence user = GetScenePresence(fromID);
  68. if (user != null)
  69. args.Sender = user.ControllingClient;
  70. }
  71. else
  72. {
  73. SceneObjectPart obj = GetSceneObjectPart(fromID);
  74. args.SenderObject = obj;
  75. }
  76. //m_log.DebugFormat(
  77. // "[SCENE]: Sending message {0} on channel {1}, type {2} from {3}, broadcast {4}",
  78. // args.Message.Replace("\n", "\\n"), args.Channel, args.Type, fromName, broadcast);
  79. if (broadcast)
  80. EventManager.TriggerOnChatBroadcast(this, args);
  81. else
  82. EventManager.TriggerOnChatFromWorld(this, args);
  83. }
  84. protected void SimChat(byte[] message, ChatTypeEnum type, int channel, Vector3 fromPos, string fromName,
  85. UUID fromID, bool fromAgent, bool broadcast)
  86. {
  87. SimChat(Utils.BytesToString(message), type, channel, fromPos, fromName, fromID, UUID.Zero, fromAgent, broadcast);
  88. }
  89. protected void SimChat(string message, ChatTypeEnum type, int channel, Vector3 fromPos, string fromName,
  90. UUID fromID, bool fromAgent, bool broadcast)
  91. {
  92. SimChat(message, type, channel, fromPos, fromName, fromID, UUID.Zero, fromAgent, broadcast);
  93. }
  94. /// <summary>
  95. ///
  96. /// </summary>
  97. /// <param name="message"></param>
  98. /// <param name="type"></param>
  99. /// <param name="fromPos"></param>
  100. /// <param name="fromName"></param>
  101. /// <param name="fromAgentID"></param>
  102. public void SimChat(byte[] message, ChatTypeEnum type, int channel, Vector3 fromPos, string fromName,
  103. UUID fromID, bool fromAgent)
  104. {
  105. SimChat(Utils.BytesToString(message), type, channel, fromPos, fromName, fromID, UUID.Zero, fromAgent, false);
  106. }
  107. public void SimChat(string message, ChatTypeEnum type, int channel, Vector3 fromPos, string fromName,
  108. UUID fromID, bool fromAgent)
  109. {
  110. SimChat(message, type, channel, fromPos, fromName, fromID, UUID.Zero, fromAgent, false);
  111. }
  112. public void SimChat(string message, ChatTypeEnum type, Vector3 fromPos, string fromName, UUID fromID, bool fromAgent)
  113. {
  114. SimChat(message, type, 0, fromPos, fromName, fromID, UUID.Zero, fromAgent, false);
  115. }
  116. public void SimChat(string message, string fromName)
  117. {
  118. SimChat(message, ChatTypeEnum.Broadcast, 0, Vector3.Zero, fromName, UUID.Zero, UUID.Zero, false, false);
  119. }
  120. /// <summary>
  121. ///
  122. /// </summary>
  123. /// <param name="message"></param>
  124. /// <param name="type"></param>
  125. /// <param name="fromPos"></param>
  126. /// <param name="fromName"></param>
  127. /// <param name="fromAgentID"></param>
  128. public void SimChatBroadcast(string message, ChatTypeEnum type, int channel, Vector3 fromPos, string fromName,
  129. UUID fromID, bool fromAgent)
  130. {
  131. SimChat(message, type, channel, fromPos, fromName, fromID, UUID.Zero, fromAgent, true);
  132. }
  133. /// <summary>
  134. ///
  135. /// </summary>
  136. /// <param name="message"></param>
  137. /// <param name="type"></param>
  138. /// <param name="channel"></param>
  139. /// <param name="fromPos"></param>
  140. /// <param name="fromName"></param>
  141. /// <param name="fromAgentID"></param>
  142. /// <param name="targetID"></param>
  143. public void SimChatToAgent(UUID targetID, byte[] message, int channel, Vector3 fromPos, string fromName, UUID fromID, bool fromAgent)
  144. {
  145. SimChat(Utils.BytesToString(message), ChatTypeEnum.Region, channel, fromPos, fromName, fromID, targetID, fromAgent, false);
  146. }
  147. public void SimChatToAgent(UUID targetID, string message, int channel, Vector3 fromPos, string fromName, UUID fromID, bool fromAgent)
  148. {
  149. SimChat(message, ChatTypeEnum.Region, channel, fromPos, fromName, fromID, targetID, fromAgent, false);
  150. }
  151. /// <summary>
  152. /// Invoked when the client requests a prim.
  153. /// </summary>
  154. /// <param name="primLocalID"></param>
  155. /// <param name="remoteClient"></param>
  156. public void RequestPrim(uint primLocalID, IClientAPI remoteClient)
  157. {
  158. SceneObjectPart part = GetSceneObjectPart(primLocalID);
  159. if (part != null)
  160. {
  161. SceneObjectGroup sog = part.ParentGroup;
  162. if(!sog.IsDeleted)
  163. {
  164. PrimUpdateFlags update = sog.RootPart.Shape.MeshFlagEntry ?
  165. PrimUpdateFlags.FullUpdatewithAnim : PrimUpdateFlags.FullUpdate;
  166. if(part.Shape.RenderMaterials is not null)
  167. update |= PrimUpdateFlags.MaterialOvr;
  168. part.SendUpdate(remoteClient, update);
  169. }
  170. }
  171. //SceneObjectGroup sog = GetGroupByPrim(primLocalID);
  172. //if (sog != null)
  173. //sog.SendFullAnimUpdateToClient(remoteClient);
  174. }
  175. /// <summary>
  176. /// Invoked when the client selects a prim.
  177. /// </summary>
  178. /// <param name="primLocalID"></param>
  179. /// <param name="remoteClient"></param>
  180. public void SelectPrim(List<uint> primIDs, IClientAPI remoteClient)
  181. {
  182. foreach(uint primLocalID in primIDs)
  183. {
  184. SceneObjectPart part = GetSceneObjectPart(primLocalID);
  185. if (part == null)
  186. continue;
  187. SceneObjectGroup sog = part.ParentGroup;
  188. if (sog == null)
  189. continue;
  190. // waste of time because properties do not send prim flags as they should
  191. // if a friend got or lost edit rights after login, a full update is needed
  192. if(sog.OwnerID != remoteClient.AgentId)
  193. part.SendFullUpdate(remoteClient);
  194. // A prim is only tainted if it's allowed to be edited by the person clicking it.
  195. if (Permissions.CanChangeSelectedState(part, (ScenePresence)remoteClient.SceneAgent))
  196. {
  197. bool oldsel = part.IsSelected;
  198. part.IsSelected = true;
  199. if(!oldsel)
  200. EventManager.TriggerParcelPrimCountTainted();
  201. }
  202. part.SendPropertiesToClient(remoteClient);
  203. }
  204. }
  205. /// <summary>
  206. /// Handle the update of an object's user group.
  207. /// </summary>
  208. /// <param name="remoteClient"></param>
  209. /// <param name="groupID"></param>
  210. /// <param name="objectLocalID"></param>
  211. /// <param name="Garbage"></param>
  212. private void HandleObjectGroupUpdate(
  213. IClientAPI remoteClient, UUID groupID, uint objectLocalID, UUID Garbage)
  214. {
  215. if (m_groupsModule == null)
  216. return;
  217. // XXX: Might be better to get rid of this special casing and have GetMembershipData return something
  218. // reasonable for a UUID.Zero group.
  219. if (!groupID.IsZero())
  220. {
  221. GroupMembershipData gmd = m_groupsModule.GetMembershipData(groupID, remoteClient.AgentId);
  222. if (gmd == null)
  223. {
  224. // m_log.WarnFormat(
  225. // "[GROUPS]: User {0} is not a member of group {1} so they can't update {2} to this group",
  226. // remoteClient.Name, GroupID, objectLocalID);
  227. return;
  228. }
  229. }
  230. SceneObjectGroup so = ((Scene)remoteClient.Scene).GetGroupByPrim(objectLocalID);
  231. if (so != null)
  232. {
  233. if (so.OwnerID == remoteClient.AgentId)
  234. {
  235. so.SetGroup(groupID, remoteClient);
  236. EventManager.TriggerParcelPrimCountTainted();
  237. }
  238. }
  239. }
  240. /// <summary>
  241. /// Handle the deselection of a prim from the client.
  242. /// </summary>
  243. /// <param name="primLocalID"></param>
  244. /// <param name="remoteClient"></param>
  245. public void DeselectPrim(uint primLocalID, IClientAPI remoteClient)
  246. {
  247. SceneObjectPart part = GetSceneObjectPart(primLocalID);
  248. if (part == null)
  249. return;
  250. bool oldgprSelect = part.ParentGroup.IsSelected;
  251. part.IsSelected = false;
  252. if (oldgprSelect != part.ParentGroup.IsSelected)
  253. {
  254. if (!part.ParentGroup.IsAttachment )
  255. EventManager.TriggerParcelPrimCountTainted();
  256. }
  257. // restore targetOmega
  258. if (part.AngularVelocity != Vector3.Zero)
  259. part.ScheduleTerseUpdate();
  260. }
  261. public virtual void ProcessMoneyTransferRequest(UUID source, UUID destination, int amount,
  262. int transactiontype, string description)
  263. {
  264. EventManager.MoneyTransferArgs args = new EventManager.MoneyTransferArgs(source, destination, amount,
  265. transactiontype, description);
  266. EventManager.TriggerMoneyTransfer(this, args);
  267. }
  268. public virtual void ProcessParcelBuy(UUID agentId, UUID groupId, bool final, bool groupOwned,
  269. bool removeContribution, int parcelLocalID, int parcelArea, int parcelPrice, bool authenticated)
  270. {
  271. EventManager.LandBuyArgs args = new EventManager.LandBuyArgs(agentId, groupId, final, groupOwned,
  272. removeContribution, parcelLocalID, parcelArea,
  273. parcelPrice, authenticated);
  274. // First, allow all validators a stab at it
  275. m_eventManager.TriggerValidateLandBuy(this, args);
  276. // Then, check validation and transfer
  277. m_eventManager.TriggerLandBuy(this, args);
  278. }
  279. public virtual void ProcessObjectGrab(uint localID, Vector3 offsetPos, IClientAPI remoteClient, List<SurfaceTouchEventArgs> surfaceArgs)
  280. {
  281. SceneObjectPart part = GetSceneObjectPart(localID);
  282. if (part == null)
  283. return;
  284. SceneObjectGroup obj = part.ParentGroup;
  285. SurfaceTouchEventArgs surfaceArg = null;
  286. if (surfaceArgs != null && surfaceArgs.Count > 0)
  287. surfaceArg = surfaceArgs[0];
  288. // Currently only grab/touch for the single prim
  289. // the client handles rez correctly
  290. obj.ObjectGrabHandler(localID, offsetPos, remoteClient);
  291. uint partLocalId = part.LocalId;
  292. // If the touched prim handles touches, deliver it
  293. if ((part.ScriptEvents & scriptEvents.touch_start) != 0)
  294. {
  295. EventManager.TriggerObjectGrab(partLocalId, 0, offsetPos, remoteClient, surfaceArg);
  296. if(!part.PassTouches)
  297. return;
  298. }
  299. // Deliver to the root prim if the touched prim doesn't handle touches
  300. // or if we're meant to pass on touches anyway.
  301. uint rootLocalId = obj.RootPart.LocalId;
  302. if (partLocalId != rootLocalId && (obj.RootPart.ScriptEvents & scriptEvents.touch_start) != 0)
  303. {
  304. EventManager.TriggerObjectGrab(rootLocalId, partLocalId, offsetPos, remoteClient, surfaceArg);
  305. }
  306. }
  307. public virtual void ProcessObjectGrabUpdate(
  308. UUID objectID, Vector3 offset, Vector3 pos, IClientAPI remoteClient, List<SurfaceTouchEventArgs> surfaceArgs)
  309. {
  310. SceneObjectPart part = GetSceneObjectPart(objectID);
  311. if (part == null)
  312. return;
  313. SceneObjectGroup group = part.ParentGroup;
  314. if(group == null || group.IsDeleted)
  315. return;
  316. if (Permissions.CanMoveObject(group, remoteClient))
  317. {
  318. group.GrabMovement(objectID, offset, pos, remoteClient);
  319. }
  320. // This is outside the above permissions condition
  321. // so that if the object is locked the client moving the object
  322. // get's it's position on the simulator even if it was the same as before
  323. // This keeps the moving user's client in sync with the rest of the world.
  324. group.SendGroupTerseUpdate();
  325. SurfaceTouchEventArgs surfaceArg = null;
  326. if (surfaceArgs != null && surfaceArgs.Count > 0)
  327. surfaceArg = surfaceArgs[0];
  328. Vector3 grabOffset = pos - part.AbsolutePosition;
  329. // If the touched prim handles touches, deliver it
  330. uint partLocalId = part.LocalId;
  331. if ((part.ScriptEvents & scriptEvents.touch) != 0)
  332. {
  333. EventManager.TriggerObjectGrabbing(partLocalId, 0, grabOffset, remoteClient, surfaceArg);
  334. if(!part.PassTouches)
  335. return;
  336. }
  337. uint rootLocalId = group.RootPart.LocalId;
  338. // Deliver to the root prim if the touched prim doesn't handle touches
  339. // or if we're meant to pass on touches anyway.
  340. if (partLocalId != rootLocalId && (group.RootPart.ScriptEvents & scriptEvents.touch) != 0)
  341. {
  342. EventManager.TriggerObjectGrabbing(rootLocalId, partLocalId, grabOffset, remoteClient, surfaceArg);
  343. }
  344. }
  345. public virtual void ProcessObjectDeGrab(uint localID, IClientAPI remoteClient, List<SurfaceTouchEventArgs> surfaceArgs)
  346. {
  347. SceneObjectPart part = GetSceneObjectPart(localID);
  348. if (part == null)
  349. return;
  350. SceneObjectGroup grp = part.ParentGroup;
  351. SurfaceTouchEventArgs surfaceArg = null;
  352. if (surfaceArgs != null && surfaceArgs.Count > 0)
  353. surfaceArg = surfaceArgs[0];
  354. uint partLocalId = part.LocalId;
  355. // If the touched prim handles touches, deliver it
  356. if ((part.ScriptEvents & scriptEvents.touch_end) != 0)
  357. {
  358. EventManager.TriggerObjectDeGrab(partLocalId, 0, remoteClient, surfaceArg);
  359. if(!part.PassTouches)
  360. return;
  361. }
  362. uint rootPartLocalId = grp.RootPart.LocalId;
  363. if (partLocalId != rootPartLocalId && (grp.RootPart.ScriptEvents & scriptEvents.touch_end) != 0)
  364. {
  365. EventManager.TriggerObjectDeGrab(rootPartLocalId, partLocalId, remoteClient, surfaceArg);
  366. }
  367. }
  368. /// <summary>
  369. /// Start spinning the given object
  370. /// </summary>
  371. /// <param name="objectID"></param>
  372. /// <param name="rotation"></param>
  373. /// <param name="remoteClient"></param>
  374. public virtual void ProcessSpinStart(UUID objectID, IClientAPI remoteClient)
  375. {
  376. SceneObjectGroup group = GetGroupByPrim(objectID);
  377. if (group != null)
  378. {
  379. if (Permissions.CanMoveObject(group, remoteClient))// && PermissionsMngr.)
  380. {
  381. group.SpinStart(remoteClient);
  382. }
  383. }
  384. }
  385. /// <summary>
  386. /// Spin the given object
  387. /// </summary>
  388. /// <param name="objectID"></param>
  389. /// <param name="rotation"></param>
  390. /// <param name="remoteClient"></param>
  391. public virtual void ProcessSpinObject(UUID objectID, Quaternion rotation, IClientAPI remoteClient)
  392. {
  393. SceneObjectGroup group = GetGroupByPrim(objectID);
  394. if (group != null)
  395. {
  396. if (Permissions.CanMoveObject(group, remoteClient))// && PermissionsMngr.)
  397. {
  398. group.SpinMovement(rotation, remoteClient);
  399. }
  400. // This is outside the above permissions condition
  401. // so that if the object is locked the client moving the object
  402. // get's it's position on the simulator even if it was the same as before
  403. // This keeps the moving user's client in sync with the rest of the world.
  404. group.SendGroupTerseUpdate();
  405. }
  406. }
  407. public virtual void ProcessSpinObjectStop(UUID objectID, IClientAPI remoteClient)
  408. {
  409. /* no op for now
  410. SceneObjectGroup group = GetGroupByPrim(objectID);
  411. if (group != null)
  412. {
  413. if (Permissions.CanMoveObject(group.UUID, remoteClient.AgentId))// && PermissionsMngr.)
  414. {
  415. // group.SpinMovement(rotation, remoteClient);
  416. }
  417. group.SendGroupTerseUpdate();
  418. }
  419. */
  420. }
  421. public void ProcessScriptReset(IClientAPI remoteClient, UUID objectID,
  422. UUID itemID)
  423. {
  424. SceneObjectPart part=GetSceneObjectPart(objectID);
  425. if (part == null)
  426. return;
  427. if (Permissions.CanResetScript(objectID, itemID, remoteClient.AgentId))
  428. {
  429. EventManager.TriggerScriptReset(part.LocalId, itemID);
  430. }
  431. }
  432. void ProcessViewerEffect(IClientAPI remoteClient, List<ViewerEffectEventHandlerArg> args)
  433. {
  434. // TODO: don't create new blocks if recycling an old packet
  435. bool discardableEffects = true;
  436. ViewerEffectPacket.EffectBlock[] effectBlockArray = new ViewerEffectPacket.EffectBlock[args.Count];
  437. for (int i = 0; i < args.Count; i++)
  438. {
  439. ViewerEffectPacket.EffectBlock effect = new ViewerEffectPacket.EffectBlock();
  440. effect.AgentID = args[i].AgentID;
  441. effect.Color = args[i].Color;
  442. effect.Duration = args[i].Duration;
  443. effect.ID = args[i].ID;
  444. effect.Type = args[i].Type;
  445. effect.TypeData = args[i].TypeData;
  446. effectBlockArray[i] = effect;
  447. if ((EffectType)effect.Type != EffectType.LookAt && (EffectType)effect.Type != EffectType.Beam)
  448. discardableEffects = false;
  449. //m_log.DebugFormat("[YYY]: VE {0} {1} {2}", effect.AgentID, effect.Duration, (EffectType)effect.Type);
  450. }
  451. ForEachScenePresence(sp =>
  452. {
  453. if (sp.ControllingClient.AgentId != remoteClient.AgentId)
  454. {
  455. if (!discardableEffects ||
  456. (discardableEffects && ShouldSendDiscardableEffect(remoteClient, sp)))
  457. {
  458. //m_log.DebugFormat("[YYY]: Sending to {0}", sp.UUID);
  459. sp.ControllingClient.SendViewerEffect(effectBlockArray);
  460. }
  461. //else
  462. // m_log.DebugFormat("[YYY]: Not sending to {0}", sp.UUID);
  463. }
  464. });
  465. }
  466. private bool ShouldSendDiscardableEffect(IClientAPI thisClient, ScenePresence other)
  467. {
  468. return Vector3.DistanceSquared(other.CameraPosition, thisClient.SceneAgent.AbsolutePosition) < 100;
  469. }
  470. private class DescendentsRequestData
  471. {
  472. public IClientAPI RemoteClient;
  473. public UUID FolderID;
  474. //public UUID OwnerID;
  475. public bool FetchFolders;
  476. public bool FetchItems;
  477. //public int SortOrder;
  478. }
  479. static private ConcurrentQueue<DescendentsRequestData> m_descendentsRequestQueue = new ConcurrentQueue<DescendentsRequestData>();
  480. static private Object m_descendentsRequestLock = new Object();
  481. static private bool m_descendentsRequestProcessing = false;
  482. /// <summary>
  483. /// Tell the client about the various child items and folders contained in the requested folder.
  484. /// </summary>
  485. /// <param name="remoteClient"></param>
  486. /// <param name="folderID"></param>
  487. /// <param name="ownerID"></param>
  488. /// <param name="fetchFolders"></param>
  489. /// <param name="fetchItems"></param>
  490. /// <param name="sortOrder"></param>
  491. public void HandleFetchInventoryDescendents(IClientAPI remoteClient, UUID folderID, UUID ownerID,
  492. bool fetchFolders, bool fetchItems, int sortOrder)
  493. {
  494. // m_log.DebugFormat(
  495. // "[USER INVENTORY]: HandleFetchInventoryDescendents() for {0}, folder={1}, fetchFolders={2}, fetchItems={3}, sortOrder={4}",
  496. // remoteClient.Name, folderID, fetchFolders, fetchItems, sortOrder);
  497. if (folderID.IsZero())
  498. return;
  499. // FIXME MAYBE: We're not handling sortOrder!
  500. // TODO: This code for looking in the folder for the library should be folded somewhere else
  501. // so that this class doesn't have to know the details (and so that multiple libraries, etc.
  502. // can be handled transparently).
  503. InventoryFolderImpl fold = null;
  504. if (LibraryService != null && LibraryService.LibraryRootFolder != null)
  505. {
  506. if ((fold = LibraryService.LibraryRootFolder.FindFolder(folderID)) != null)
  507. {
  508. List<InventoryItemBase> its = fold.RequestListOfItems();
  509. List<InventoryFolderBase> fds = fold.RequestListOfFolders();
  510. remoteClient.SendInventoryFolderDetails(
  511. fold.Owner, folderID, its, fds,
  512. fold.Version, its.Count + fds.Count, fetchFolders, fetchItems);
  513. return;
  514. }
  515. }
  516. DescendentsRequestData req = new DescendentsRequestData();
  517. req.RemoteClient = remoteClient;
  518. req.FolderID = folderID;
  519. //req.OwnerID = ownerID;
  520. req.FetchFolders = fetchFolders;
  521. req.FetchItems = fetchItems;
  522. //req.SortOrder = sortOrder;
  523. m_descendentsRequestQueue.Enqueue(req);
  524. if (Monitor.TryEnter(m_descendentsRequestLock))
  525. {
  526. if (!m_descendentsRequestProcessing)
  527. {
  528. m_descendentsRequestProcessing = true;
  529. Util.FireAndForget(x => SendInventoryAsync());
  530. }
  531. Monitor.Exit(m_descendentsRequestLock);
  532. }
  533. }
  534. void SendInventoryAsync()
  535. {
  536. lock(m_descendentsRequestLock)
  537. {
  538. try
  539. {
  540. while(m_descendentsRequestQueue.TryDequeue(out DescendentsRequestData req))
  541. {
  542. if(!req.RemoteClient.IsActive)
  543. continue;
  544. SendInventoryUpdate(req.RemoteClient, new InventoryFolderBase(req.FolderID), req.FetchFolders, req.FetchItems);
  545. Thread.Sleep(50);
  546. }
  547. }
  548. catch (Exception e)
  549. {
  550. m_log.ErrorFormat("[AGENT INVENTORY]: Error in SendInventoryAsync(). Exception {0}", e);
  551. }
  552. m_descendentsRequestProcessing = false;
  553. }
  554. }
  555. /// <summary>
  556. /// Handle an inventory folder creation request from the client.
  557. /// </summary>
  558. /// <param name="remoteClient"></param>
  559. /// <param name="folderID"></param>
  560. /// <param name="folderType"></param>
  561. /// <param name="folderName"></param>
  562. /// <param name="parentID"></param>
  563. public void HandleCreateInventoryFolder(IClientAPI remoteClient, UUID folderID, ushort folderType,
  564. string folderName, UUID parentID)
  565. {
  566. InventoryFolderBase folder = new InventoryFolderBase(folderID, folderName, remoteClient.AgentId, (short)folderType, parentID, 1);
  567. if (!InventoryService.AddFolder(folder))
  568. {
  569. m_log.WarnFormat(
  570. "[AGENT INVENTORY]: Failed to create folder for user {0} {1}",
  571. remoteClient.Name, remoteClient.AgentId);
  572. }
  573. }
  574. /// <summary>
  575. /// Handle a client request to update the inventory folder
  576. /// </summary>
  577. ///
  578. /// <param name="remoteClient"></param>
  579. /// <param name="folderID"></param>
  580. /// <param name="type"></param>
  581. /// <param name="name"></param>
  582. /// <param name="parentID"></param>
  583. public void HandleUpdateInventoryFolder(IClientAPI remoteClient, UUID folderID, ushort type, string name,
  584. UUID parentID)
  585. {
  586. // m_log.DebugFormat(
  587. // "[AGENT INVENTORY]: Updating inventory folder {0} {1} for {2} {3}", folderID, name, remoteClient.Name, remoteClient.AgentId);
  588. InventoryFolderBase folder = InventoryService.GetFolder(remoteClient.AgentId, folderID);
  589. if (folder != null)
  590. {
  591. folder.Name = name;
  592. folder.Type = (short)type;
  593. folder.ParentID = parentID;
  594. if (!InventoryService.UpdateFolder(folder))
  595. {
  596. m_log.ErrorFormat(
  597. "[AGENT INVENTORY]: Failed to update folder for user {0} {1}",
  598. remoteClient.Name, remoteClient.AgentId);
  599. }
  600. }
  601. }
  602. public void HandleMoveInventoryFolder(IClientAPI remoteClient, UUID folderID, UUID parentID)
  603. {
  604. InventoryFolderBase folder = InventoryService.GetFolder(remoteClient.AgentId, folderID);
  605. if (folder != null)
  606. {
  607. folder.ParentID = parentID;
  608. if (!InventoryService.MoveFolder(folder))
  609. m_log.WarnFormat("[AGENT INVENTORY]: could not move folder {0}", folderID);
  610. else
  611. m_log.DebugFormat("[AGENT INVENTORY]: folder {0} moved to parent {1}", folderID, parentID);
  612. }
  613. else
  614. {
  615. m_log.WarnFormat("[AGENT INVENTORY]: request to move folder {0} but folder not found", folderID);
  616. }
  617. }
  618. /// <summary>
  619. /// This should delete all the items and folders in the given directory.
  620. /// </summary>
  621. /// <param name="remoteClient"></param>
  622. /// <param name="folderID"></param>
  623. public void HandlePurgeInventoryDescendents(IClientAPI remoteClient, UUID folderID)
  624. {
  625. try
  626. {
  627. UUID agent = remoteClient.AgentId;
  628. UUID folder = folderID;
  629. Util.FireAndForget(delegate
  630. {
  631. PurgeFolderAsync(agent, folder);
  632. });
  633. }
  634. catch (Exception e)
  635. {
  636. m_log.WarnFormat("[AGENT INVENTORY]: Exception on purge folder for user {0}: {1}", remoteClient.AgentId, e.Message);
  637. }
  638. }
  639. private void PurgeFolderAsync(UUID userID, UUID folderID)
  640. {
  641. InventoryFolderBase folder = new InventoryFolderBase(folderID, userID);
  642. try
  643. {
  644. if (InventoryService.PurgeFolder(folder))
  645. m_log.DebugFormat("[AGENT INVENTORY]: folder {0} purged successfully", folderID);
  646. else
  647. m_log.WarnFormat("[AGENT INVENTORY]: could not purge folder {0}", folderID);
  648. }
  649. catch (Exception e)
  650. {
  651. m_log.WarnFormat("[AGENT INVENTORY]: Exception on async purge folder for user {0}: {1}", userID, e.Message);
  652. }
  653. }
  654. }
  655. }