1
0

Scene.PacketHandlers.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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. List<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. List<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. foreach (KeyValuePair<UUID, SceneObjectPart> child in ((SceneObjectGroup) ent).Children)
  151. {
  152. if (child.Value.LocalId == primLocalID)
  153. {
  154. child.Value.GetProperties(remoteClient);
  155. foundPrim = true;
  156. break;
  157. }
  158. }
  159. if (foundPrim) break;
  160. }
  161. }
  162. }
  163. }
  164. /// <summary>
  165. /// Handle the deselection of a prim from the client.
  166. /// </summary>
  167. /// <param name="primLocalID"></param>
  168. /// <param name="remoteClient"></param>
  169. public void DeselectPrim(uint primLocalID, IClientAPI remoteClient)
  170. {
  171. SceneObjectPart part = GetSceneObjectPart(primLocalID);
  172. if (part == null)
  173. return;
  174. // The prim is in the process of being deleted.
  175. if (null == part.ParentGroup.RootPart)
  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. List<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. List<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. List<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. public void HandleUUIDNameRequest(UUID uuid, IClientAPI remote_client)
  406. {
  407. if (LibraryService != null && (LibraryService.LibraryRootFolder.Owner == uuid))
  408. {
  409. remote_client.SendNameReply(uuid, "Mr", "OpenSim");
  410. }
  411. else
  412. {
  413. string[] names = GetUserNames(uuid);
  414. if (names.Length == 2)
  415. {
  416. remote_client.SendNameReply(uuid, names[0], names[1]);
  417. }
  418. }
  419. }
  420. /// <summary>
  421. /// Handle a fetch inventory request from the client
  422. /// </summary>
  423. /// <param name="remoteClient"></param>
  424. /// <param name="itemID"></param>
  425. /// <param name="ownerID"></param>
  426. public void HandleFetchInventory(IClientAPI remoteClient, UUID itemID, UUID ownerID)
  427. {
  428. if (LibraryService != null && LibraryService.LibraryRootFolder != null && ownerID == LibraryService.LibraryRootFolder.Owner)
  429. {
  430. //m_log.Debug("request info for library item");
  431. return;
  432. }
  433. InventoryItemBase item = new InventoryItemBase(itemID, remoteClient.AgentId);
  434. item = InventoryService.GetItem(item);
  435. if (item != null)
  436. {
  437. remoteClient.SendInventoryItemDetails(ownerID, item);
  438. }
  439. // else shouldn't we send an alert message?
  440. }
  441. /// <summary>
  442. /// Tell the client about the various child items and folders contained in the requested folder.
  443. /// </summary>
  444. /// <param name="remoteClient"></param>
  445. /// <param name="folderID"></param>
  446. /// <param name="ownerID"></param>
  447. /// <param name="fetchFolders"></param>
  448. /// <param name="fetchItems"></param>
  449. /// <param name="sortOrder"></param>
  450. public void HandleFetchInventoryDescendents(IClientAPI remoteClient, UUID folderID, UUID ownerID,
  451. bool fetchFolders, bool fetchItems, int sortOrder)
  452. {
  453. // FIXME MAYBE: We're not handling sortOrder!
  454. // TODO: This code for looking in the folder for the library should be folded somewhere else
  455. // so that this class doesn't have to know the details (and so that multiple libraries, etc.
  456. // can be handled transparently).
  457. InventoryFolderImpl fold = null;
  458. if (LibraryService != null && LibraryService.LibraryRootFolder != null)
  459. if ((fold = LibraryService.LibraryRootFolder.FindFolder(folderID)) != null)
  460. {
  461. remoteClient.SendInventoryFolderDetails(
  462. fold.Owner, folderID, fold.RequestListOfItems(),
  463. fold.RequestListOfFolders(), fold.Version, fetchFolders, fetchItems);
  464. return;
  465. }
  466. // We're going to send the reply async, because there may be
  467. // an enormous quantity of packets -- basically the entire inventory!
  468. // We don't want to block the client thread while all that is happening.
  469. SendInventoryDelegate d = SendInventoryAsync;
  470. d.BeginInvoke(remoteClient, folderID, ownerID, fetchFolders, fetchItems, sortOrder, SendInventoryComplete, d);
  471. }
  472. delegate void SendInventoryDelegate(IClientAPI remoteClient, UUID folderID, UUID ownerID, bool fetchFolders, bool fetchItems, int sortOrder);
  473. void SendInventoryAsync(IClientAPI remoteClient, UUID folderID, UUID ownerID, bool fetchFolders, bool fetchItems, int sortOrder)
  474. {
  475. SendInventoryUpdate(remoteClient, new InventoryFolderBase(folderID), fetchFolders, fetchItems);
  476. }
  477. void SendInventoryComplete(IAsyncResult iar)
  478. {
  479. SendInventoryDelegate d = (SendInventoryDelegate)iar.AsyncState;
  480. d.EndInvoke(iar);
  481. }
  482. /// <summary>
  483. /// Handle the caps inventory descendents fetch.
  484. ///
  485. /// Since the folder structure is sent to the client on login, I believe we only need to handle items.
  486. /// Diva comment 8/13/2009: what if someone gave us a folder in the meantime??
  487. /// </summary>
  488. /// <param name="agentID"></param>
  489. /// <param name="folderID"></param>
  490. /// <param name="ownerID"></param>
  491. /// <param name="fetchFolders"></param>
  492. /// <param name="fetchItems"></param>
  493. /// <param name="sortOrder"></param>
  494. /// <returns>null if the inventory look up failed</returns>
  495. public InventoryCollection HandleFetchInventoryDescendentsCAPS(UUID agentID, UUID folderID, UUID ownerID,
  496. bool fetchFolders, bool fetchItems, int sortOrder, out int version)
  497. {
  498. m_log.DebugFormat(
  499. "[INVENTORY CACHE]: Fetching folders ({0}), items ({1}) from {2} for agent {3}",
  500. fetchFolders, fetchItems, folderID, agentID);
  501. // FIXME MAYBE: We're not handling sortOrder!
  502. // TODO: This code for looking in the folder for the library should be folded back into the
  503. // CachedUserInfo so that this class doesn't have to know the details (and so that multiple libraries, etc.
  504. // can be handled transparently).
  505. InventoryFolderImpl fold;
  506. if (LibraryService != null && LibraryService.LibraryRootFolder != null)
  507. if ((fold = LibraryService.LibraryRootFolder.FindFolder(folderID)) != null)
  508. {
  509. version = 0;
  510. InventoryCollection ret = new InventoryCollection();
  511. ret.Folders = new List<InventoryFolderBase>();
  512. ret.Items = fold.RequestListOfItems();
  513. return ret;
  514. }
  515. InventoryCollection contents = new InventoryCollection();
  516. if (folderID != UUID.Zero)
  517. {
  518. contents = InventoryService.GetFolderContent(agentID, folderID);
  519. InventoryFolderBase containingFolder = new InventoryFolderBase();
  520. containingFolder.ID = folderID;
  521. containingFolder.Owner = agentID;
  522. containingFolder = InventoryService.GetFolder(containingFolder);
  523. version = containingFolder.Version;
  524. }
  525. else
  526. {
  527. // Lost itemsm don't really need a version
  528. version = 1;
  529. }
  530. return contents;
  531. }
  532. /// <summary>
  533. /// Handle an inventory folder creation request from the client.
  534. /// </summary>
  535. /// <param name="remoteClient"></param>
  536. /// <param name="folderID"></param>
  537. /// <param name="folderType"></param>
  538. /// <param name="folderName"></param>
  539. /// <param name="parentID"></param>
  540. public void HandleCreateInventoryFolder(IClientAPI remoteClient, UUID folderID, ushort folderType,
  541. string folderName, UUID parentID)
  542. {
  543. InventoryFolderBase folder = new InventoryFolderBase(folderID, folderName, remoteClient.AgentId, (short)folderType, parentID, 1);
  544. if (!InventoryService.AddFolder(folder))
  545. {
  546. m_log.WarnFormat(
  547. "[AGENT INVENTORY]: Failed to move create folder for user {0} {1}",
  548. remoteClient.Name, remoteClient.AgentId);
  549. }
  550. }
  551. /// <summary>
  552. /// Handle a client request to update the inventory folder
  553. /// </summary>
  554. ///
  555. /// FIXME: We call add new inventory folder because in the data layer, we happen to use an SQL REPLACE
  556. /// so this will work to rename an existing folder. Needless to say, to rely on this is very confusing,
  557. /// and needs to be changed.
  558. ///
  559. /// <param name="remoteClient"></param>
  560. /// <param name="folderID"></param>
  561. /// <param name="type"></param>
  562. /// <param name="name"></param>
  563. /// <param name="parentID"></param>
  564. public void HandleUpdateInventoryFolder(IClientAPI remoteClient, UUID folderID, ushort type, string name,
  565. UUID parentID)
  566. {
  567. // m_log.DebugFormat(
  568. // "[AGENT INVENTORY]: Updating inventory folder {0} {1} for {2} {3}", folderID, name, remoteClient.Name, remoteClient.AgentId);
  569. InventoryFolderBase folder = new InventoryFolderBase(folderID, remoteClient.AgentId);
  570. folder = InventoryService.GetFolder(folder);
  571. if (folder != null)
  572. {
  573. folder.Name = name;
  574. folder.Type = (short)type;
  575. folder.ParentID = parentID;
  576. if (!InventoryService.UpdateFolder(folder))
  577. {
  578. m_log.ErrorFormat(
  579. "[AGENT INVENTORY]: Failed to update folder for user {0} {1}",
  580. remoteClient.Name, remoteClient.AgentId);
  581. }
  582. }
  583. }
  584. public void HandleMoveInventoryFolder(IClientAPI remoteClient, UUID folderID, UUID parentID)
  585. {
  586. InventoryFolderBase folder = new InventoryFolderBase(folderID, remoteClient.AgentId);
  587. folder = InventoryService.GetFolder(folder);
  588. if (folder != null)
  589. {
  590. folder.ParentID = parentID;
  591. if (!InventoryService.MoveFolder(folder))
  592. m_log.WarnFormat("[AGENT INVENTORY]: could not move folder {0}", folderID);
  593. else
  594. m_log.DebugFormat("[AGENT INVENTORY]: folder {0} moved to parent {1}", folderID, parentID);
  595. }
  596. else
  597. {
  598. m_log.WarnFormat("[AGENT INVENTORY]: request to move folder {0} but folder not found", folderID);
  599. }
  600. }
  601. /// <summary>
  602. /// This should delete all the items and folders in the given directory.
  603. /// </summary>
  604. /// <param name="remoteClient"></param>
  605. /// <param name="folderID"></param>
  606. delegate void PurgeFolderDelegate(UUID userID, UUID folder);
  607. public void HandlePurgeInventoryDescendents(IClientAPI remoteClient, UUID folderID)
  608. {
  609. PurgeFolderDelegate d = PurgeFolderAsync;
  610. try
  611. {
  612. d.BeginInvoke(remoteClient.AgentId, folderID, PurgeFolderCompleted, d);
  613. }
  614. catch (Exception e)
  615. {
  616. m_log.WarnFormat("[AGENT INVENTORY]: Exception on purge folder for user {0}: {1}", remoteClient.AgentId, e.Message);
  617. }
  618. }
  619. private void PurgeFolderAsync(UUID userID, UUID folderID)
  620. {
  621. InventoryFolderBase folder = new InventoryFolderBase(folderID, userID);
  622. if (InventoryService.PurgeFolder(folder))
  623. m_log.DebugFormat("[AGENT INVENTORY]: folder {0} purged successfully", folderID);
  624. else
  625. m_log.WarnFormat("[AGENT INVENTORY]: could not purge folder {0}", folderID);
  626. }
  627. private void PurgeFolderCompleted(IAsyncResult iar)
  628. {
  629. PurgeFolderDelegate d = (PurgeFolderDelegate)iar.AsyncState;
  630. d.EndInvoke(iar);
  631. }
  632. }
  633. }