1
0

Scene.PacketHandlers.cs 31 KB

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