Scene.PacketHandlers.cs 30 KB

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