InventoryTransferModule.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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.Reflection;
  30. using log4net;
  31. using Mono.Addins;
  32. using Nini.Config;
  33. using OpenMetaverse;
  34. using OpenSim.Framework;
  35. using OpenSim.Region.Framework.Interfaces;
  36. using OpenSim.Region.Framework.Scenes;
  37. using OpenSim.Services.Interfaces;
  38. namespace OpenSim.Region.CoreModules.Avatar.Inventory.Transfer
  39. {
  40. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "InventoryTransferModule")]
  41. public class InventoryTransferModule : ISharedRegionModule
  42. {
  43. private static readonly ILog m_log
  44. = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. /// <summary>
  46. private List<Scene> m_Scenelist = new List<Scene>();
  47. private IMessageTransferModule m_TransferModule;
  48. private bool m_Enabled = true;
  49. #region Region Module interface
  50. public void Initialise(IConfigSource config)
  51. {
  52. if (config.Configs["Messaging"] != null)
  53. {
  54. // Allow disabling this module in config
  55. //
  56. if (config.Configs["Messaging"].GetString(
  57. "InventoryTransferModule", "InventoryTransferModule") !=
  58. "InventoryTransferModule")
  59. {
  60. m_Enabled = false;
  61. return;
  62. }
  63. }
  64. }
  65. public void AddRegion(Scene scene)
  66. {
  67. if (!m_Enabled)
  68. return;
  69. m_Scenelist.Add(scene);
  70. // scene.RegisterModuleInterface<IInventoryTransferModule>(this);
  71. scene.EventManager.OnNewClient += OnNewClient;
  72. scene.EventManager.OnIncomingInstantMessage += OnGridInstantMessage;
  73. }
  74. public void RegionLoaded(Scene scene)
  75. {
  76. if (m_TransferModule == null)
  77. {
  78. m_TransferModule = m_Scenelist[0].RequestModuleInterface<IMessageTransferModule>();
  79. if (m_TransferModule == null)
  80. {
  81. m_log.Error("[INVENTORY TRANSFER]: No Message transfer module found, transfers will be local only");
  82. m_Enabled = false;
  83. // m_Scenelist.Clear();
  84. // scene.EventManager.OnNewClient -= OnNewClient;
  85. scene.EventManager.OnIncomingInstantMessage -= OnGridInstantMessage;
  86. }
  87. }
  88. }
  89. public void RemoveRegion(Scene scene)
  90. {
  91. scene.EventManager.OnNewClient -= OnNewClient;
  92. scene.EventManager.OnIncomingInstantMessage -= OnGridInstantMessage;
  93. m_Scenelist.Remove(scene);
  94. }
  95. public void PostInitialise()
  96. {
  97. }
  98. public void Close()
  99. {
  100. }
  101. public string Name
  102. {
  103. get { return "InventoryModule"; }
  104. }
  105. public Type ReplaceableInterface
  106. {
  107. get { return null; }
  108. }
  109. #endregion
  110. private void OnNewClient(IClientAPI client)
  111. {
  112. // Inventory giving is conducted via instant message
  113. client.OnInstantMessage += OnInstantMessage;
  114. }
  115. private Scene FindClientScene(UUID agentId)
  116. {
  117. lock (m_Scenelist)
  118. {
  119. foreach (Scene scene in m_Scenelist)
  120. {
  121. ScenePresence presence = scene.GetScenePresence(agentId);
  122. if (presence != null)
  123. return scene;
  124. }
  125. }
  126. return null;
  127. }
  128. private void OnInstantMessage(IClientAPI client, GridInstantMessage im)
  129. {
  130. // m_log.DebugFormat(
  131. // "[INVENTORY TRANSFER]: {0} IM type received from {1}",
  132. // (InstantMessageDialog)im.dialog, client.Name);
  133. Scene scene = FindClientScene(client.AgentId);
  134. if (scene == null) // Something seriously wrong here.
  135. return;
  136. if (im.dialog == (byte) InstantMessageDialog.InventoryOffered)
  137. {
  138. //m_log.DebugFormat("Asset type {0}", ((AssetType)im.binaryBucket[0]));
  139. if (im.binaryBucket.Length < 17) // Invalid
  140. return;
  141. UUID receipientID = new UUID(im.toAgentID);
  142. ScenePresence user = scene.GetScenePresence(receipientID);
  143. UUID copyID;
  144. // First byte is the asset type
  145. AssetType assetType = (AssetType)im.binaryBucket[0];
  146. if (AssetType.Folder == assetType)
  147. {
  148. UUID folderID = new UUID(im.binaryBucket, 1);
  149. m_log.DebugFormat(
  150. "[INVENTORY TRANSFER]: Inserting original folder {0} into agent {1}'s inventory",
  151. folderID, new UUID(im.toAgentID));
  152. InventoryFolderBase folderCopy
  153. = scene.GiveInventoryFolder(receipientID, client.AgentId, folderID, UUID.Zero);
  154. if (folderCopy == null)
  155. {
  156. client.SendAgentAlertMessage("Can't find folder to give. Nothing given.", false);
  157. return;
  158. }
  159. // The outgoing binary bucket should contain only the byte which signals an asset folder is
  160. // being copied and the following bytes for the copied folder's UUID
  161. copyID = folderCopy.ID;
  162. byte[] copyIDBytes = copyID.GetBytes();
  163. im.binaryBucket = new byte[1 + copyIDBytes.Length];
  164. im.binaryBucket[0] = (byte)AssetType.Folder;
  165. Array.Copy(copyIDBytes, 0, im.binaryBucket, 1, copyIDBytes.Length);
  166. if (user != null)
  167. user.ControllingClient.SendBulkUpdateInventory(folderCopy);
  168. // HACK!!
  169. // Insert the ID of the copied folder into the IM so that we know which item to move to trash if it
  170. // is rejected.
  171. // XXX: This is probably a misuse of the session ID slot.
  172. im.imSessionID = copyID.Guid;
  173. }
  174. else
  175. {
  176. // First byte of the array is probably the item type
  177. // Next 16 bytes are the UUID
  178. UUID itemID = new UUID(im.binaryBucket, 1);
  179. m_log.DebugFormat("[INVENTORY TRANSFER]: (giving) Inserting item {0} "+
  180. "into agent {1}'s inventory",
  181. itemID, new UUID(im.toAgentID));
  182. InventoryItemBase itemCopy = scene.GiveInventoryItem(
  183. new UUID(im.toAgentID),
  184. client.AgentId, itemID);
  185. if (itemCopy == null)
  186. {
  187. client.SendAgentAlertMessage("Can't find item to give. Nothing given.", false);
  188. return;
  189. }
  190. copyID = itemCopy.ID;
  191. Array.Copy(copyID.GetBytes(), 0, im.binaryBucket, 1, 16);
  192. if (user != null)
  193. user.ControllingClient.SendBulkUpdateInventory(itemCopy);
  194. // HACK!!
  195. // Insert the ID of the copied item into the IM so that we know which item to move to trash if it
  196. // is rejected.
  197. // XXX: This is probably a misuse of the session ID slot.
  198. im.imSessionID = copyID.Guid;
  199. }
  200. // Send the IM to the recipient. The item is already
  201. // in their inventory, so it will not be lost if
  202. // they are offline.
  203. //
  204. if (user != null)
  205. {
  206. user.ControllingClient.SendInstantMessage(im);
  207. return;
  208. }
  209. else
  210. {
  211. if (m_TransferModule != null)
  212. m_TransferModule.SendInstantMessage(im, delegate(bool success)
  213. {
  214. if (!success)
  215. client.SendAlertMessage("User not online. Inventory has been saved");
  216. });
  217. }
  218. }
  219. else if (im.dialog == (byte) InstantMessageDialog.InventoryAccepted)
  220. {
  221. ScenePresence user = scene.GetScenePresence(new UUID(im.toAgentID));
  222. if (user != null) // Local
  223. {
  224. user.ControllingClient.SendInstantMessage(im);
  225. }
  226. else
  227. {
  228. if (m_TransferModule != null)
  229. m_TransferModule.SendInstantMessage(im, delegate(bool success) {
  230. // justincc - FIXME: Comment out for now. This code was added in commit db91044 Mon Aug 22 2011
  231. // and is apparently supposed to fix bulk inventory updates after accepting items. But
  232. // instead it appears to cause two copies of an accepted folder for the receiving user in
  233. // at least some cases. Folder/item update is already done when the offer is made (see code above)
  234. // // Send BulkUpdateInventory
  235. // IInventoryService invService = scene.InventoryService;
  236. // UUID inventoryEntityID = new UUID(im.imSessionID); // The inventory item /folder, back from it's trip
  237. //
  238. // InventoryFolderBase folder = new InventoryFolderBase(inventoryEntityID, client.AgentId);
  239. // folder = invService.GetFolder(folder);
  240. //
  241. // ScenePresence fromUser = scene.GetScenePresence(new UUID(im.fromAgentID));
  242. //
  243. // // If the user has left the scene by the time the message comes back then we can't send
  244. // // them the update.
  245. // if (fromUser != null)
  246. // fromUser.ControllingClient.SendBulkUpdateInventory(folder);
  247. });
  248. }
  249. }
  250. // XXX: This code was placed here to try and accomodate RLV which moves given folders named #RLV/~<name>
  251. // to the requested folder, which in this case is #RLV. However, it is the viewer that appears to be
  252. // response from renaming the #RLV/~example folder to ~example. For some reason this is not yet
  253. // happening, possibly because we are not sending the correct inventory update messages with the correct
  254. // transaction IDs
  255. else if (im.dialog == (byte) InstantMessageDialog.TaskInventoryAccepted)
  256. {
  257. UUID destinationFolderID = UUID.Zero;
  258. if (im.binaryBucket != null && im.binaryBucket.Length >= 16)
  259. {
  260. destinationFolderID = new UUID(im.binaryBucket, 0);
  261. }
  262. if (destinationFolderID != UUID.Zero)
  263. {
  264. InventoryFolderBase destinationFolder = new InventoryFolderBase(destinationFolderID, client.AgentId);
  265. if (destinationFolder == null)
  266. {
  267. m_log.WarnFormat(
  268. "[INVENTORY TRANSFER]: TaskInventoryAccepted message from {0} in {1} specified folder {2} which does not exist",
  269. client.Name, scene.Name, destinationFolderID);
  270. return;
  271. }
  272. IInventoryService invService = scene.InventoryService;
  273. UUID inventoryID = new UUID(im.imSessionID); // The inventory item/folder, back from it's trip
  274. InventoryItemBase item = new InventoryItemBase(inventoryID, client.AgentId);
  275. item = invService.GetItem(item);
  276. InventoryFolderBase folder = null;
  277. UUID? previousParentFolderID = null;
  278. if (item != null) // It's an item
  279. {
  280. previousParentFolderID = item.Folder;
  281. item.Folder = destinationFolderID;
  282. invService.DeleteItems(item.Owner, new List<UUID>() { item.ID });
  283. scene.AddInventoryItem(client, item);
  284. }
  285. else
  286. {
  287. folder = new InventoryFolderBase(inventoryID, client.AgentId);
  288. folder = invService.GetFolder(folder);
  289. if (folder != null) // It's a folder
  290. {
  291. previousParentFolderID = folder.ParentID;
  292. folder.ParentID = destinationFolderID;
  293. invService.MoveFolder(folder);
  294. }
  295. }
  296. // Tell client about updates to original parent and new parent (this should probably be factored with existing move item/folder code).
  297. if (previousParentFolderID != null)
  298. {
  299. InventoryFolderBase previousParentFolder
  300. = new InventoryFolderBase((UUID)previousParentFolderID, client.AgentId);
  301. previousParentFolder = invService.GetFolder(previousParentFolder);
  302. scene.SendInventoryUpdate(client, previousParentFolder, true, true);
  303. scene.SendInventoryUpdate(client, destinationFolder, true, true);
  304. }
  305. }
  306. }
  307. else if (
  308. im.dialog == (byte)InstantMessageDialog.InventoryDeclined
  309. || im.dialog == (byte)InstantMessageDialog.TaskInventoryDeclined)
  310. {
  311. // Here, the recipient is local and we can assume that the
  312. // inventory is loaded. Courtesy of the above bulk update,
  313. // It will have been pushed to the client, too
  314. //
  315. IInventoryService invService = scene.InventoryService;
  316. InventoryFolderBase trashFolder =
  317. invService.GetFolderForType(client.AgentId, AssetType.TrashFolder);
  318. UUID inventoryID = new UUID(im.imSessionID); // The inventory item/folder, back from it's trip
  319. InventoryItemBase item = new InventoryItemBase(inventoryID, client.AgentId);
  320. item = invService.GetItem(item);
  321. InventoryFolderBase folder = null;
  322. UUID? previousParentFolderID = null;
  323. if (item != null && trashFolder != null)
  324. {
  325. previousParentFolderID = item.Folder;
  326. item.Folder = trashFolder.ID;
  327. // Diva comment: can't we just update this item???
  328. List<UUID> uuids = new List<UUID>();
  329. uuids.Add(item.ID);
  330. invService.DeleteItems(item.Owner, uuids);
  331. scene.AddInventoryItem(client, item);
  332. }
  333. else
  334. {
  335. folder = new InventoryFolderBase(inventoryID, client.AgentId);
  336. folder = invService.GetFolder(folder);
  337. if (folder != null & trashFolder != null)
  338. {
  339. previousParentFolderID = folder.ParentID;
  340. folder.ParentID = trashFolder.ID;
  341. invService.MoveFolder(folder);
  342. }
  343. }
  344. if ((null == item && null == folder) | null == trashFolder)
  345. {
  346. string reason = String.Empty;
  347. if (trashFolder == null)
  348. reason += " Trash folder not found.";
  349. if (item == null)
  350. reason += " Item not found.";
  351. if (folder == null)
  352. reason += " Folder not found.";
  353. client.SendAgentAlertMessage("Unable to delete "+
  354. "received inventory" + reason, false);
  355. }
  356. // Tell client about updates to original parent and new parent (this should probably be factored with existing move item/folder code).
  357. else if (previousParentFolderID != null)
  358. {
  359. InventoryFolderBase previousParentFolder
  360. = new InventoryFolderBase((UUID)previousParentFolderID, client.AgentId);
  361. previousParentFolder = invService.GetFolder(previousParentFolder);
  362. scene.SendInventoryUpdate(client, previousParentFolder, true, true);
  363. scene.SendInventoryUpdate(client, trashFolder, true, true);
  364. }
  365. if (im.dialog == (byte)InstantMessageDialog.InventoryDeclined)
  366. {
  367. ScenePresence user = scene.GetScenePresence(new UUID(im.toAgentID));
  368. if (user != null) // Local
  369. {
  370. user.ControllingClient.SendInstantMessage(im);
  371. }
  372. else
  373. {
  374. if (m_TransferModule != null)
  375. m_TransferModule.SendInstantMessage(im, delegate(bool success) { });
  376. }
  377. }
  378. }
  379. }
  380. /// <summary>
  381. ///
  382. /// </summary>
  383. /// <param name="msg"></param>
  384. private void OnGridInstantMessage(GridInstantMessage msg)
  385. {
  386. // Check if this is ours to handle
  387. //
  388. Scene scene = FindClientScene(new UUID(msg.toAgentID));
  389. if (scene == null)
  390. return;
  391. // Find agent to deliver to
  392. //
  393. ScenePresence user = scene.GetScenePresence(new UUID(msg.toAgentID));
  394. // Just forward to local handling
  395. OnInstantMessage(user.ControllingClient, msg);
  396. }
  397. }
  398. }