InventoryModule.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 OpenSim 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.Collections.Generic;
  28. using System.Reflection;
  29. using libsecondlife;
  30. using log4net;
  31. using Nini.Config;
  32. using OpenSim.Framework;
  33. using OpenSim.Region.Environment.Interfaces;
  34. using OpenSim.Region.Environment.Scenes;
  35. namespace OpenSim.Region.Environment.Modules.Avatar.Inventory
  36. {
  37. public class InventoryModule : IRegionModule
  38. {
  39. private static readonly ILog m_log
  40. = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  41. /// <summary>
  42. /// We need to keep track of the pending item offers between clients since the itemId offered only
  43. /// occurs in the initial offer message, not the accept message. So this dictionary links
  44. /// IM Session Ids to ItemIds
  45. /// </summary>
  46. private IDictionary<LLUUID, LLUUID> m_pendingOffers = new Dictionary<LLUUID, LLUUID>();
  47. private Scene m_scene;
  48. #region IRegionModule Members
  49. public void Initialise(Scene scene, IConfigSource config)
  50. {
  51. m_scene = scene;
  52. scene.EventManager.OnNewClient += OnNewClient;
  53. }
  54. public void PostInitialise()
  55. {
  56. }
  57. public void Close()
  58. {
  59. }
  60. public string Name
  61. {
  62. get { return "InventoryModule"; }
  63. }
  64. public bool IsSharedModule
  65. {
  66. get { return false; }
  67. }
  68. #endregion
  69. private void OnNewClient(IClientAPI client)
  70. {
  71. // Inventory giving is conducted via instant message
  72. client.OnInstantMessage += OnInstantMessage;
  73. }
  74. private void OnInstantMessage(IClientAPI client, LLUUID fromAgentID,
  75. LLUUID fromAgentSession, LLUUID toAgentID,
  76. LLUUID imSessionID, uint timestamp, string fromAgentName,
  77. string message, byte dialog, bool fromGroup, byte offline,
  78. uint ParentEstateID, LLVector3 Position, LLUUID RegionID,
  79. byte[] binaryBucket)
  80. {
  81. if (dialog == (byte) InstantMessageDialog.InventoryOffered)
  82. {
  83. m_log.DebugFormat(
  84. "[AGENT INVENTORY]: Routing inventory offering message from {0}, {1} to {2}",
  85. client.AgentId, client.Name, toAgentID);
  86. if (m_scene.Entities.ContainsKey(toAgentID) && m_scene.Entities[toAgentID] is ScenePresence)
  87. {
  88. ScenePresence user = (ScenePresence) m_scene.Entities[toAgentID];
  89. if (!user.IsChildAgent)
  90. {
  91. //byte[] rawId = new byte[16];
  92. // First byte of the array is probably the item type
  93. // Next 16 bytes are the UUID
  94. //Array.Copy(binaryBucket, 1, rawId, 0, 16);
  95. //LLUUID itemId = new LLUUID(new Guid(rawId));
  96. LLUUID itemId = new LLUUID(binaryBucket, 1);
  97. m_log.DebugFormat(
  98. "[AGENT INVENTORY]: ItemId for giving is {0}", itemId);
  99. m_pendingOffers[imSessionID] = itemId;
  100. user.ControllingClient.SendInstantMessage(
  101. fromAgentID, fromAgentSession, message, toAgentID, imSessionID, fromAgentName,
  102. dialog, timestamp, binaryBucket);
  103. return;
  104. }
  105. else
  106. {
  107. m_log.WarnFormat(
  108. "[AGENT INVENTORY]: Agent {0} targeted for inventory give by {1}, {2} of {3} was a child agent!",
  109. toAgentID, client.AgentId, client.Name, message);
  110. }
  111. }
  112. else
  113. {
  114. m_log.WarnFormat(
  115. "[AGENT INVENTORY]: Could not find agent {0} for user {1}, {2} to give {3}",
  116. toAgentID, client.AgentId, client.Name, message);
  117. }
  118. }
  119. else if (dialog == (byte) InstantMessageDialog.InventoryAccepted)
  120. {
  121. m_log.DebugFormat(
  122. "[AGENT INVENTORY]: Routing inventory accepted message from {0}, {1} to {2}",
  123. client.AgentId, client.Name, toAgentID);
  124. if (m_scene.Entities.ContainsKey(toAgentID) && m_scene.Entities[toAgentID] is ScenePresence)
  125. {
  126. ScenePresence user = (ScenePresence) m_scene.Entities[toAgentID];
  127. if (!user.IsChildAgent)
  128. {
  129. user.ControllingClient.SendInstantMessage(
  130. fromAgentID, fromAgentSession, message, toAgentID, imSessionID, fromAgentName,
  131. dialog, timestamp, binaryBucket);
  132. if (m_pendingOffers.ContainsKey(imSessionID))
  133. {
  134. m_log.DebugFormat(
  135. "[AGENT INVENTORY]: Accepted item id {0}", m_pendingOffers[imSessionID]);
  136. // Since the message originates from the accepting client, the toAgentID is
  137. // the agent giving the item.
  138. m_scene.GiveInventoryItem(client, toAgentID, m_pendingOffers[imSessionID]);
  139. m_pendingOffers.Remove(imSessionID);
  140. }
  141. else
  142. {
  143. m_log.ErrorFormat(
  144. "[AGENT INVENTORY]: Could not find an item associated with session id {0} to accept",
  145. imSessionID);
  146. }
  147. return;
  148. }
  149. else
  150. {
  151. m_log.WarnFormat(
  152. "[AGENT INVENTORY]: Agent {0} targeted for inventory give by {1}, {2} of {3} was a child agent!",
  153. toAgentID, client.AgentId, client.Name, message);
  154. }
  155. }
  156. else
  157. {
  158. m_log.WarnFormat(
  159. "[AGENT INVENTORY]: Could not find agent {0} for user {1}, {2} to give {3}",
  160. toAgentID, client.AgentId, client.Name, message);
  161. }
  162. }
  163. else if (dialog == (byte) InstantMessageDialog.InventoryDeclined)
  164. {
  165. if (m_scene.Entities.ContainsKey(toAgentID) && m_scene.Entities[toAgentID] is ScenePresence)
  166. {
  167. ScenePresence user = (ScenePresence) m_scene.Entities[toAgentID];
  168. if (!user.IsChildAgent)
  169. {
  170. user.ControllingClient.SendInstantMessage(
  171. fromAgentID, fromAgentSession, message, toAgentID, imSessionID, fromAgentName,
  172. dialog, timestamp, binaryBucket);
  173. if (m_pendingOffers.ContainsKey(imSessionID))
  174. {
  175. m_log.DebugFormat(
  176. "[AGENT INVENTORY]: Declined item id {0}", m_pendingOffers[imSessionID]);
  177. m_pendingOffers.Remove(imSessionID);
  178. }
  179. else
  180. {
  181. m_log.ErrorFormat(
  182. "[AGENT INVENTORY]: Could not find an item associated with session id {0} to decline",
  183. imSessionID);
  184. }
  185. }
  186. }
  187. }
  188. }
  189. }
  190. }