Scene.PacketHandlers.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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).GetProperties(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.GetProperties(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. // The prim is in the process of being deleted.
  178. if (null == part.ParentGroup.RootPart)
  179. return;
  180. // A deselect packet contains all the local prims being deselected. However, since selection is still
  181. // group based we only want the root prim to trigger a full update - otherwise on objects with many prims
  182. // we end up sending many duplicate ObjectUpdates
  183. if (part.ParentGroup.RootPart.LocalId != part.LocalId)
  184. return;
  185. bool isAttachment = false;
  186. // This is wrong, wrong, wrong. Selection should not be
  187. // handled by group, but by prim. Legacy cruft.
  188. // TODO: Make selection flagging per prim!
  189. //
  190. part.ParentGroup.IsSelected = false;
  191. if (part.ParentGroup.IsAttachment)
  192. isAttachment = true;
  193. else
  194. part.ParentGroup.ScheduleGroupForFullUpdate();
  195. // If it's not an attachment, and we are allowed to move it,
  196. // then we might have done so. If we moved across a parcel
  197. // boundary, we will need to recount prims on the parcels.
  198. // For attachments, that makes no sense.
  199. //
  200. if (!isAttachment)
  201. {
  202. if (Permissions.CanEditObject(
  203. part.UUID, remoteClient.AgentId)
  204. || Permissions.CanMoveObject(
  205. part.UUID, remoteClient.AgentId))
  206. EventManager.TriggerParcelPrimCountTainted();
  207. }
  208. }
  209. public virtual void ProcessMoneyTransferRequest(UUID source, UUID destination, int amount,
  210. int transactiontype, string description)
  211. {
  212. EventManager.MoneyTransferArgs args = new EventManager.MoneyTransferArgs(source, destination, amount,
  213. transactiontype, description);
  214. EventManager.TriggerMoneyTransfer(this, args);
  215. }
  216. public virtual void ProcessParcelBuy(UUID agentId, UUID groupId, bool final, bool groupOwned,
  217. bool removeContribution, int parcelLocalID, int parcelArea, int parcelPrice, bool authenticated)
  218. {
  219. EventManager.LandBuyArgs args = new EventManager.LandBuyArgs(agentId, groupId, final, groupOwned,
  220. removeContribution, parcelLocalID, parcelArea,
  221. parcelPrice, authenticated);
  222. // First, allow all validators a stab at it
  223. m_eventManager.TriggerValidateLandBuy(this, args);
  224. // Then, check validation and transfer
  225. m_eventManager.TriggerLandBuy(this, args);
  226. }
  227. public virtual void ProcessObjectGrab(uint localID, Vector3 offsetPos, IClientAPI remoteClient, List<SurfaceTouchEventArgs> surfaceArgs)
  228. {
  229. EntityBase[] EntityList = GetEntities();
  230. SurfaceTouchEventArgs surfaceArg = null;
  231. if (surfaceArgs != null && surfaceArgs.Count > 0)
  232. surfaceArg = surfaceArgs[0];
  233. foreach (EntityBase ent in EntityList)
  234. {
  235. if (ent is SceneObjectGroup)
  236. {
  237. SceneObjectGroup obj = ent as SceneObjectGroup;
  238. if (obj != null)
  239. {
  240. // Is this prim part of the group
  241. if (obj.HasChildPrim(localID))
  242. {
  243. // Currently only grab/touch for the single prim
  244. // the client handles rez correctly
  245. obj.ObjectGrabHandler(localID, offsetPos, remoteClient);
  246. SceneObjectPart part = obj.GetChildPart(localID);
  247. // If the touched prim handles touches, deliver it
  248. // If not, deliver to root prim
  249. if ((part.ScriptEvents & scriptEvents.touch_start) != 0)
  250. EventManager.TriggerObjectGrab(part.LocalId, 0, part.OffsetPosition, remoteClient, surfaceArg);
  251. // Deliver to the root prim if the touched prim doesn't handle touches
  252. // or if we're meant to pass on touches anyway. Don't send to root prim
  253. // if prim touched is the root prim as we just did it
  254. if (((part.ScriptEvents & scriptEvents.touch_start) == 0) ||
  255. (part.PassTouches && (part.LocalId != obj.RootPart.LocalId)))
  256. {
  257. EventManager.TriggerObjectGrab(obj.RootPart.LocalId, part.LocalId, part.OffsetPosition, remoteClient, surfaceArg);
  258. }
  259. return;
  260. }
  261. }
  262. }
  263. }
  264. }
  265. public virtual void ProcessObjectGrabUpdate(UUID objectID, Vector3 offset, Vector3 pos, IClientAPI remoteClient, List<SurfaceTouchEventArgs> surfaceArgs)
  266. {
  267. EntityBase[] EntityList = GetEntities();
  268. SurfaceTouchEventArgs surfaceArg = null;
  269. if (surfaceArgs != null && surfaceArgs.Count > 0)
  270. surfaceArg = surfaceArgs[0];
  271. foreach (EntityBase ent in EntityList)
  272. {
  273. if (ent is SceneObjectGroup)
  274. {
  275. SceneObjectGroup obj = ent as SceneObjectGroup;
  276. if (obj != null)
  277. {
  278. // Is this prim part of the group
  279. if (obj.HasChildPrim(objectID))
  280. {
  281. SceneObjectPart part = obj.GetChildPart(objectID);
  282. // If the touched prim handles touches, deliver it
  283. // If not, deliver to root prim
  284. if ((part.ScriptEvents & scriptEvents.touch) != 0)
  285. EventManager.TriggerObjectGrabbing(part.LocalId, 0, part.OffsetPosition, remoteClient, surfaceArg);
  286. // Deliver to the root prim if the touched prim doesn't handle touches
  287. // or if we're meant to pass on touches anyway. Don't send to root prim
  288. // if prim touched is the root prim as we just did it
  289. if (((part.ScriptEvents & scriptEvents.touch) == 0) ||
  290. (part.PassTouches && (part.LocalId != obj.RootPart.LocalId)))
  291. {
  292. EventManager.TriggerObjectGrabbing(obj.RootPart.LocalId, part.LocalId, part.OffsetPosition, remoteClient, surfaceArg);
  293. }
  294. return;
  295. }
  296. }
  297. }
  298. }
  299. }
  300. public virtual void ProcessObjectDeGrab(uint localID, IClientAPI remoteClient, List<SurfaceTouchEventArgs> surfaceArgs)
  301. {
  302. EntityBase[] EntityList = GetEntities();
  303. SurfaceTouchEventArgs surfaceArg = null;
  304. if (surfaceArgs != null && surfaceArgs.Count > 0)
  305. surfaceArg = surfaceArgs[0];
  306. foreach (EntityBase ent in EntityList)
  307. {
  308. if (ent is SceneObjectGroup)
  309. {
  310. SceneObjectGroup obj = ent as SceneObjectGroup;
  311. // Is this prim part of the group
  312. if (obj.HasChildPrim(localID))
  313. {
  314. SceneObjectPart part=obj.GetChildPart(localID);
  315. if (part != null)
  316. {
  317. // If the touched prim handles touches, deliver it
  318. // If not, deliver to root prim
  319. if ((part.ScriptEvents & scriptEvents.touch_end) != 0)
  320. EventManager.TriggerObjectDeGrab(part.LocalId, 0, remoteClient, surfaceArg);
  321. else
  322. EventManager.TriggerObjectDeGrab(obj.RootPart.LocalId, part.LocalId, remoteClient, surfaceArg);
  323. return;
  324. }
  325. return;
  326. }
  327. }
  328. }
  329. }
  330. public void ProcessAvatarPickerRequest(IClientAPI client, UUID avatarID, UUID RequestID, string query)
  331. {
  332. //EventManager.TriggerAvatarPickerRequest();
  333. List<UserAccount> accounts = UserAccountService.GetUserAccounts(RegionInfo.ScopeID, query);
  334. if (accounts == null)
  335. return;
  336. AvatarPickerReplyPacket replyPacket = (AvatarPickerReplyPacket) PacketPool.Instance.GetPacket(PacketType.AvatarPickerReply);
  337. // TODO: don't create new blocks if recycling an old packet
  338. AvatarPickerReplyPacket.DataBlock[] searchData =
  339. new AvatarPickerReplyPacket.DataBlock[accounts.Count];
  340. AvatarPickerReplyPacket.AgentDataBlock agentData = new AvatarPickerReplyPacket.AgentDataBlock();
  341. agentData.AgentID = avatarID;
  342. agentData.QueryID = RequestID;
  343. replyPacket.AgentData = agentData;
  344. //byte[] bytes = new byte[AvatarResponses.Count*32];
  345. int i = 0;
  346. foreach (UserAccount item in accounts)
  347. {
  348. UUID translatedIDtem = item.PrincipalID;
  349. searchData[i] = new AvatarPickerReplyPacket.DataBlock();
  350. searchData[i].AvatarID = translatedIDtem;
  351. searchData[i].FirstName = Utils.StringToBytes((string) item.FirstName);
  352. searchData[i].LastName = Utils.StringToBytes((string) item.LastName);
  353. i++;
  354. }
  355. if (accounts.Count == 0)
  356. {
  357. searchData = new AvatarPickerReplyPacket.DataBlock[0];
  358. }
  359. replyPacket.Data = searchData;
  360. AvatarPickerReplyAgentDataArgs agent_data = new AvatarPickerReplyAgentDataArgs();
  361. agent_data.AgentID = replyPacket.AgentData.AgentID;
  362. agent_data.QueryID = replyPacket.AgentData.QueryID;
  363. List<AvatarPickerReplyDataArgs> data_args = new List<AvatarPickerReplyDataArgs>();
  364. for (i = 0; i < replyPacket.Data.Length; i++)
  365. {
  366. AvatarPickerReplyDataArgs data_arg = new AvatarPickerReplyDataArgs();
  367. data_arg.AvatarID = replyPacket.Data[i].AvatarID;
  368. data_arg.FirstName = replyPacket.Data[i].FirstName;
  369. data_arg.LastName = replyPacket.Data[i].LastName;
  370. data_args.Add(data_arg);
  371. }
  372. client.SendAvatarPickerReply(agent_data, data_args);
  373. }
  374. public void ProcessScriptReset(IClientAPI remoteClient, UUID objectID,
  375. UUID itemID)
  376. {
  377. SceneObjectPart part=GetSceneObjectPart(objectID);
  378. if (part == null)
  379. return;
  380. if (Permissions.CanResetScript(objectID, itemID, remoteClient.AgentId))
  381. {
  382. EventManager.TriggerScriptReset(part.LocalId, itemID);
  383. }
  384. }
  385. void ProcessViewerEffect(IClientAPI remoteClient, List<ViewerEffectEventHandlerArg> args)
  386. {
  387. // TODO: don't create new blocks if recycling an old packet
  388. ViewerEffectPacket.EffectBlock[] effectBlockArray = new ViewerEffectPacket.EffectBlock[args.Count];
  389. for (int i = 0; i < args.Count; i++)
  390. {
  391. ViewerEffectPacket.EffectBlock effect = new ViewerEffectPacket.EffectBlock();
  392. effect.AgentID = args[i].AgentID;
  393. effect.Color = args[i].Color;
  394. effect.Duration = args[i].Duration;
  395. effect.ID = args[i].ID;
  396. effect.Type = args[i].Type;
  397. effect.TypeData = args[i].TypeData;
  398. effectBlockArray[i] = effect;
  399. }
  400. ForEachClient(
  401. delegate(IClientAPI client)
  402. {
  403. if (client.AgentId != remoteClient.AgentId)
  404. client.SendViewerEffect(effectBlockArray);
  405. }
  406. );
  407. }
  408. /// <summary>
  409. /// Tell the client about the various child items and folders contained in the requested folder.
  410. /// </summary>
  411. /// <param name="remoteClient"></param>
  412. /// <param name="folderID"></param>
  413. /// <param name="ownerID"></param>
  414. /// <param name="fetchFolders"></param>
  415. /// <param name="fetchItems"></param>
  416. /// <param name="sortOrder"></param>
  417. public void HandleFetchInventoryDescendents(IClientAPI remoteClient, UUID folderID, UUID ownerID,
  418. bool fetchFolders, bool fetchItems, int sortOrder)
  419. {
  420. // m_log.DebugFormat(
  421. // "[USER INVENTORY]: HandleFetchInventoryDescendents() for {0}, folder={1}, fetchFolders={2}, fetchItems={3}, sortOrder={4}",
  422. // remoteClient.Name, folderID, fetchFolders, fetchItems, sortOrder);
  423. if (folderID == UUID.Zero)
  424. return;
  425. // FIXME MAYBE: We're not handling sortOrder!
  426. // TODO: This code for looking in the folder for the library should be folded somewhere else
  427. // so that this class doesn't have to know the details (and so that multiple libraries, etc.
  428. // can be handled transparently).
  429. InventoryFolderImpl fold = null;
  430. if (LibraryService != null && LibraryService.LibraryRootFolder != null)
  431. if ((fold = LibraryService.LibraryRootFolder.FindFolder(folderID)) != null)
  432. {
  433. remoteClient.SendInventoryFolderDetails(
  434. fold.Owner, folderID, fold.RequestListOfItems(),
  435. fold.RequestListOfFolders(), fold.Version, fetchFolders, fetchItems);
  436. return;
  437. }
  438. // We're going to send the reply async, because there may be
  439. // an enormous quantity of packets -- basically the entire inventory!
  440. // We don't want to block the client thread while all that is happening.
  441. SendInventoryDelegate d = SendInventoryAsync;
  442. d.BeginInvoke(remoteClient, folderID, ownerID, fetchFolders, fetchItems, sortOrder, SendInventoryComplete, d);
  443. }
  444. delegate void SendInventoryDelegate(IClientAPI remoteClient, UUID folderID, UUID ownerID, bool fetchFolders, bool fetchItems, int sortOrder);
  445. void SendInventoryAsync(IClientAPI remoteClient, UUID folderID, UUID ownerID, bool fetchFolders, bool fetchItems, int sortOrder)
  446. {
  447. SendInventoryUpdate(remoteClient, new InventoryFolderBase(folderID), fetchFolders, fetchItems);
  448. }
  449. void SendInventoryComplete(IAsyncResult iar)
  450. {
  451. SendInventoryDelegate d = (SendInventoryDelegate)iar.AsyncState;
  452. d.EndInvoke(iar);
  453. }
  454. /// <summary>
  455. /// Handle the caps inventory descendents fetch.
  456. ///
  457. /// Since the folder structure is sent to the client on login, I believe we only need to handle items.
  458. /// Diva comment 8/13/2009: what if someone gave us a folder in the meantime??
  459. /// </summary>
  460. /// <param name="agentID"></param>
  461. /// <param name="folderID"></param>
  462. /// <param name="ownerID"></param>
  463. /// <param name="fetchFolders"></param>
  464. /// <param name="fetchItems"></param>
  465. /// <param name="sortOrder"></param>
  466. /// <returns>null if the inventory look up failed</returns>
  467. public InventoryCollection HandleFetchInventoryDescendentsCAPS(UUID agentID, UUID folderID, UUID ownerID,
  468. bool fetchFolders, bool fetchItems, int sortOrder, out int version)
  469. {
  470. m_log.DebugFormat(
  471. "[INVENTORY CACHE]: Fetching folders ({0}), items ({1}) from {2} for agent {3}",
  472. fetchFolders, fetchItems, folderID, agentID);
  473. // FIXME MAYBE: We're not handling sortOrder!
  474. // TODO: This code for looking in the folder for the library should be folded back into the
  475. // CachedUserInfo so that this class doesn't have to know the details (and so that multiple libraries, etc.
  476. // can be handled transparently).
  477. InventoryFolderImpl fold;
  478. if (LibraryService != null && LibraryService.LibraryRootFolder != null)
  479. if ((fold = LibraryService.LibraryRootFolder.FindFolder(folderID)) != null)
  480. {
  481. version = 0;
  482. InventoryCollection ret = new InventoryCollection();
  483. ret.Folders = new List<InventoryFolderBase>();
  484. ret.Items = fold.RequestListOfItems();
  485. return ret;
  486. }
  487. InventoryCollection contents = new InventoryCollection();
  488. if (folderID != UUID.Zero)
  489. {
  490. contents = InventoryService.GetFolderContent(agentID, folderID);
  491. InventoryFolderBase containingFolder = new InventoryFolderBase();
  492. containingFolder.ID = folderID;
  493. containingFolder.Owner = agentID;
  494. containingFolder = InventoryService.GetFolder(containingFolder);
  495. version = containingFolder.Version;
  496. }
  497. else
  498. {
  499. // Lost itemsm don't really need a version
  500. version = 1;
  501. }
  502. return contents;
  503. }
  504. /// <summary>
  505. /// Handle an inventory folder creation request from the client.
  506. /// </summary>
  507. /// <param name="remoteClient"></param>
  508. /// <param name="folderID"></param>
  509. /// <param name="folderType"></param>
  510. /// <param name="folderName"></param>
  511. /// <param name="parentID"></param>
  512. public void HandleCreateInventoryFolder(IClientAPI remoteClient, UUID folderID, ushort folderType,
  513. string folderName, UUID parentID)
  514. {
  515. InventoryFolderBase folder = new InventoryFolderBase(folderID, folderName, remoteClient.AgentId, (short)folderType, parentID, 1);
  516. if (!InventoryService.AddFolder(folder))
  517. {
  518. m_log.WarnFormat(
  519. "[AGENT INVENTORY]: Failed to move create folder for user {0} {1}",
  520. remoteClient.Name, remoteClient.AgentId);
  521. }
  522. }
  523. /// <summary>
  524. /// Handle a client request to update the inventory folder
  525. /// </summary>
  526. ///
  527. /// FIXME: We call add new inventory folder because in the data layer, we happen to use an SQL REPLACE
  528. /// so this will work to rename an existing folder. Needless to say, to rely on this is very confusing,
  529. /// and needs to be changed.
  530. ///
  531. /// <param name="remoteClient"></param>
  532. /// <param name="folderID"></param>
  533. /// <param name="type"></param>
  534. /// <param name="name"></param>
  535. /// <param name="parentID"></param>
  536. public void HandleUpdateInventoryFolder(IClientAPI remoteClient, UUID folderID, ushort type, string name,
  537. UUID parentID)
  538. {
  539. // m_log.DebugFormat(
  540. // "[AGENT INVENTORY]: Updating inventory folder {0} {1} for {2} {3}", folderID, name, remoteClient.Name, remoteClient.AgentId);
  541. InventoryFolderBase folder = new InventoryFolderBase(folderID, remoteClient.AgentId);
  542. folder = InventoryService.GetFolder(folder);
  543. if (folder != null)
  544. {
  545. folder.Name = name;
  546. folder.Type = (short)type;
  547. folder.ParentID = parentID;
  548. if (!InventoryService.UpdateFolder(folder))
  549. {
  550. m_log.ErrorFormat(
  551. "[AGENT INVENTORY]: Failed to update folder for user {0} {1}",
  552. remoteClient.Name, remoteClient.AgentId);
  553. }
  554. }
  555. }
  556. public void HandleMoveInventoryFolder(IClientAPI remoteClient, UUID folderID, UUID parentID)
  557. {
  558. InventoryFolderBase folder = new InventoryFolderBase(folderID, remoteClient.AgentId);
  559. folder = InventoryService.GetFolder(folder);
  560. if (folder != null)
  561. {
  562. folder.ParentID = parentID;
  563. if (!InventoryService.MoveFolder(folder))
  564. m_log.WarnFormat("[AGENT INVENTORY]: could not move folder {0}", folderID);
  565. else
  566. m_log.DebugFormat("[AGENT INVENTORY]: folder {0} moved to parent {1}", folderID, parentID);
  567. }
  568. else
  569. {
  570. m_log.WarnFormat("[AGENT INVENTORY]: request to move folder {0} but folder not found", folderID);
  571. }
  572. }
  573. /// <summary>
  574. /// This should delete all the items and folders in the given directory.
  575. /// </summary>
  576. /// <param name="remoteClient"></param>
  577. /// <param name="folderID"></param>
  578. delegate void PurgeFolderDelegate(UUID userID, UUID folder);
  579. public void HandlePurgeInventoryDescendents(IClientAPI remoteClient, UUID folderID)
  580. {
  581. PurgeFolderDelegate d = PurgeFolderAsync;
  582. try
  583. {
  584. d.BeginInvoke(remoteClient.AgentId, folderID, PurgeFolderCompleted, d);
  585. }
  586. catch (Exception e)
  587. {
  588. m_log.WarnFormat("[AGENT INVENTORY]: Exception on purge folder for user {0}: {1}", remoteClient.AgentId, e.Message);
  589. }
  590. }
  591. private void PurgeFolderAsync(UUID userID, UUID folderID)
  592. {
  593. InventoryFolderBase folder = new InventoryFolderBase(folderID, userID);
  594. if (InventoryService.PurgeFolder(folder))
  595. m_log.DebugFormat("[AGENT INVENTORY]: folder {0} purged successfully", folderID);
  596. else
  597. m_log.WarnFormat("[AGENT INVENTORY]: could not purge folder {0}", folderID);
  598. }
  599. private void PurgeFolderCompleted(IAsyncResult iar)
  600. {
  601. PurgeFolderDelegate d = (PurgeFolderDelegate)iar.AsyncState;
  602. d.EndInvoke(iar);
  603. }
  604. }
  605. }