AssetTransactionModule.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 Nini.Config;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. using OpenSim.Region.Framework.Interfaces;
  35. using OpenSim.Region.Framework.Scenes;
  36. using Mono.Addins;
  37. namespace OpenSim.Region.CoreModules.Agent.AssetTransaction
  38. {
  39. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "AssetTransactionModule")]
  40. public class AssetTransactionModule : INonSharedRegionModule,
  41. IAgentAssetTransactions
  42. {
  43. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. protected Scene m_Scene;
  45. private bool m_dumpAssetsToFile = false;
  46. private int m_levelUpload = 0;
  47. /// <summary>
  48. /// Each agent has its own singleton collection of transactions
  49. /// </summary>
  50. private Dictionary<UUID, AgentAssetTransactions> AgentTransactions =
  51. new Dictionary<UUID, AgentAssetTransactions>();
  52. #region Region Module interface
  53. public void Initialise(IConfigSource source)
  54. {
  55. IConfig sconfig = source.Configs["Startup"];
  56. if (sconfig != null)
  57. {
  58. m_levelUpload = sconfig.GetInt("LevelUpload", 0);
  59. }
  60. }
  61. public void AddRegion(Scene scene)
  62. {
  63. m_Scene = scene;
  64. scene.RegisterModuleInterface<IAgentAssetTransactions>(this);
  65. scene.EventManager.OnNewClient += NewClient;
  66. }
  67. public void RegionLoaded(Scene scene)
  68. {
  69. }
  70. public void RemoveRegion(Scene scene)
  71. {
  72. }
  73. public void Close()
  74. {
  75. }
  76. public string Name
  77. {
  78. get { return "AgentTransactionModule"; }
  79. }
  80. public Type ReplaceableInterface
  81. {
  82. get { return typeof(IAgentAssetTransactions); }
  83. }
  84. #endregion
  85. public void NewClient(IClientAPI client)
  86. {
  87. client.OnAssetUploadRequest += HandleUDPUploadRequest;
  88. client.OnXferReceive += HandleXfer;
  89. }
  90. #region AgentAssetTransactions
  91. /// <summary>
  92. /// Get the collection of asset transactions for the given user.
  93. /// If one does not already exist, it is created.
  94. /// </summary>
  95. /// <param name="userID"></param>
  96. /// <returns></returns>
  97. private AgentAssetTransactions GetUserTransactions(UUID userID)
  98. {
  99. lock (AgentTransactions)
  100. {
  101. if (!AgentTransactions.ContainsKey(userID))
  102. {
  103. AgentAssetTransactions transactions =
  104. new AgentAssetTransactions(userID, m_Scene,
  105. m_dumpAssetsToFile);
  106. AgentTransactions.Add(userID, transactions);
  107. }
  108. return AgentTransactions[userID];
  109. }
  110. }
  111. /// <summary>
  112. /// Remove the given agent asset transactions. This should be called
  113. /// when a client is departing from a scene (and hence won't be making
  114. /// any more transactions here).
  115. /// </summary>
  116. /// <param name="userID"></param>
  117. public void RemoveAgentAssetTransactions(UUID userID)
  118. {
  119. // m_log.DebugFormat("Removing agent asset transactions structure for agent {0}", userID);
  120. lock (AgentTransactions)
  121. {
  122. AgentTransactions.Remove(userID);
  123. }
  124. }
  125. /// <summary>
  126. /// Create an inventory item from data that has been received through
  127. /// a transaction.
  128. /// This is called when new clothing or body parts are created.
  129. /// It may also be called in other situations.
  130. /// </summary>
  131. /// <param name="remoteClient"></param>
  132. /// <param name="transactionID"></param>
  133. /// <param name="folderID"></param>
  134. /// <param name="callbackID"></param>
  135. /// <param name="description"></param>
  136. /// <param name="name"></param>
  137. /// <param name="invType"></param>
  138. /// <param name="type"></param>
  139. /// <param name="wearableType"></param>
  140. /// <param name="nextOwnerMask"></param>
  141. public bool HandleItemCreationFromTransaction(IClientAPI remoteClient,
  142. UUID transactionID, UUID folderID, uint callbackID,
  143. string description, string name, sbyte invType,
  144. sbyte type, byte wearableType, uint nextOwnerMask)
  145. {
  146. //m_log.DebugFormat(
  147. // "[TRANSACTIONS MANAGER] Called HandleItemCreationFromTransaction with item {0}", name);
  148. AgentAssetTransactions transactions = GetUserTransactions(remoteClient.AgentId);
  149. return transactions.RequestCreateInventoryItem(remoteClient, transactionID,
  150. folderID, callbackID, description, name, invType, type,
  151. wearableType, nextOwnerMask);
  152. }
  153. /// <summary>
  154. /// Update an inventory item with data that has been received through a
  155. /// transaction.
  156. /// </summary>
  157. /// <remarks>
  158. /// This is called when clothing or body parts are updated (for
  159. /// instance, with new textures or colours). It may also be called in
  160. /// other situations.
  161. /// </remarks>
  162. /// <param name="remoteClient"></param>
  163. /// <param name="transactionID"></param>
  164. /// <param name="item"></param>
  165. public void HandleItemUpdateFromTransaction(IClientAPI remoteClient,
  166. UUID transactionID, InventoryItemBase item)
  167. {
  168. // m_log.DebugFormat(
  169. // "[ASSET TRANSACTION MODULE]: Called HandleItemUpdateFromTransaction with item {0}",
  170. // item.Name);
  171. AgentAssetTransactions transactions = GetUserTransactions(remoteClient.AgentId);
  172. transactions.RequestUpdateInventoryItem(remoteClient, transactionID, item);
  173. }
  174. /// <summary>
  175. /// Update a task inventory item with data that has been received
  176. /// through a transaction.
  177. ///
  178. /// This is currently called when, for instance, a notecard in a prim
  179. /// is saved. The data is sent up through a single AssetUploadRequest.
  180. /// A subsequent UpdateTaskInventory then references the transaction
  181. /// and comes through this method.
  182. /// </summary>
  183. /// <param name="remoteClient"></param>
  184. /// <param name="part"></param>
  185. /// <param name="transactionID"></param>
  186. /// <param name="item"></param>
  187. public void HandleTaskItemUpdateFromTransaction(
  188. IClientAPI remoteClient, SceneObjectPart part, UUID transactionID, TaskInventoryItem item)
  189. {
  190. // m_log.DebugFormat(
  191. // "[ASSET TRANSACTION MODULE]: Called HandleTaskItemUpdateFromTransaction with item {0} in {1} for {2} in {3}",
  192. // item.Name, part.Name, remoteClient.Name, m_Scene.RegionInfo.RegionName);
  193. AgentAssetTransactions transactions =
  194. GetUserTransactions(remoteClient.AgentId);
  195. transactions.RequestUpdateTaskInventoryItem(remoteClient, part,
  196. transactionID, item);
  197. }
  198. /// <summary>
  199. /// Request that a client (agent) begin an asset transfer.
  200. /// </summary>
  201. /// <param name="remoteClient"></param>
  202. /// <param name="assetID"></param>
  203. /// <param name="transactionID"></param>
  204. /// <param name="type"></param>
  205. /// <param name="data"></param></param>
  206. /// <param name="tempFile"></param>
  207. public void HandleUDPUploadRequest(IClientAPI remoteClient,
  208. UUID assetID, UUID transactionID, sbyte type, byte[] data,
  209. bool storeLocal, bool tempFile)
  210. {
  211. // m_log.DebugFormat(
  212. // "[ASSET TRANSACTION MODULE]: HandleUDPUploadRequest - assetID: {0}, transaction {1}, type {2}, storeLocal {3}, tempFile {4}, data.Length {5}",
  213. // assetID, transactionID, type, storeLocal, tempFile, data.Length);
  214. if (((AssetType)type == AssetType.Texture ||
  215. (AssetType)type == AssetType.Sound ||
  216. (AssetType)type == AssetType.TextureTGA ||
  217. (AssetType)type == AssetType.Animation) &&
  218. tempFile == false)
  219. {
  220. ScenePresence avatar = null;
  221. Scene scene = (Scene)remoteClient.Scene;
  222. scene.TryGetScenePresence(remoteClient.AgentId, out avatar);
  223. // check user level
  224. if (avatar != null)
  225. {
  226. if (avatar.GodController.UserLevel < m_levelUpload)
  227. {
  228. remoteClient.SendAgentAlertMessage("Unable to upload asset. Insufficient permissions.", false);
  229. return;
  230. }
  231. }
  232. // check funds
  233. IMoneyModule mm = scene.RequestModuleInterface<IMoneyModule>();
  234. if (mm != null)
  235. {
  236. if (!mm.UploadCovered(remoteClient.AgentId, mm.UploadCharge))
  237. {
  238. remoteClient.SendAgentAlertMessage("Unable to upload asset. Insufficient funds.", false);
  239. return;
  240. }
  241. }
  242. }
  243. AgentAssetTransactions transactions = GetUserTransactions(remoteClient.AgentId);
  244. AssetXferUploader uploader = transactions.RequestXferUploader(transactionID);
  245. uploader.StartUpload(remoteClient, assetID, transactionID, type, data, storeLocal, tempFile);
  246. }
  247. /// <summary>
  248. /// Handle asset transfer data packets received in response to the
  249. /// asset upload request in HandleUDPUploadRequest()
  250. /// </summary>
  251. /// <param name="remoteClient"></param>
  252. /// <param name="xferID"></param>
  253. /// <param name="packetID"></param>
  254. /// <param name="data"></param>
  255. public void HandleXfer(IClientAPI remoteClient, ulong xferID,
  256. uint packetID, byte[] data)
  257. {
  258. // m_log.Debug("xferID: " + xferID + " packetID: " + packetID + " data length " + data.Length);
  259. AgentAssetTransactions transactions = GetUserTransactions(remoteClient.AgentId);
  260. transactions.HandleXfer(xferID, packetID, data);
  261. }
  262. #endregion
  263. }
  264. }