Scene.PacketHandlers.cs 30 KB

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