Scene.PacketHandlers.cs 30 KB

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