AssetTransactionModule.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 =
  149. GetUserTransactions(remoteClient.AgentId);
  150. return transactions.RequestCreateInventoryItem(remoteClient, transactionID,
  151. folderID, callbackID, description, name, invType, type,
  152. wearableType, nextOwnerMask);
  153. }
  154. /// <summary>
  155. /// Update an inventory item with data that has been received through a
  156. /// transaction.
  157. /// </summary>
  158. /// <remarks>
  159. /// This is called when clothing or body parts are updated (for
  160. /// instance, with new textures or colours). It may also be called in
  161. /// other situations.
  162. /// </remarks>
  163. /// <param name="remoteClient"></param>
  164. /// <param name="transactionID"></param>
  165. /// <param name="item"></param>
  166. public void HandleItemUpdateFromTransaction(IClientAPI remoteClient,
  167. UUID transactionID, InventoryItemBase item)
  168. {
  169. // m_log.DebugFormat(
  170. // "[ASSET TRANSACTION MODULE]: Called HandleItemUpdateFromTransaction with item {0}",
  171. // item.Name);
  172. AgentAssetTransactions transactions = GetUserTransactions(remoteClient.AgentId);
  173. transactions.RequestUpdateInventoryItem(remoteClient, transactionID, item);
  174. }
  175. /// <summary>
  176. /// Update a task inventory item with data that has been received
  177. /// through a transaction.
  178. ///
  179. /// This is currently called when, for instance, a notecard in a prim
  180. /// is saved. The data is sent up through a single AssetUploadRequest.
  181. /// A subsequent UpdateTaskInventory then references the transaction
  182. /// and comes through this method.
  183. /// </summary>
  184. /// <param name="remoteClient"></param>
  185. /// <param name="part"></param>
  186. /// <param name="transactionID"></param>
  187. /// <param name="item"></param>
  188. public void HandleTaskItemUpdateFromTransaction(
  189. IClientAPI remoteClient, SceneObjectPart part, UUID transactionID, TaskInventoryItem item)
  190. {
  191. // m_log.DebugFormat(
  192. // "[ASSET TRANSACTION MODULE]: Called HandleTaskItemUpdateFromTransaction with item {0} in {1} for {2} in {3}",
  193. // item.Name, part.Name, remoteClient.Name, m_Scene.RegionInfo.RegionName);
  194. AgentAssetTransactions transactions =
  195. GetUserTransactions(remoteClient.AgentId);
  196. transactions.RequestUpdateTaskInventoryItem(remoteClient, part,
  197. transactionID, item);
  198. }
  199. /// <summary>
  200. /// Request that a client (agent) begin an asset transfer.
  201. /// </summary>
  202. /// <param name="remoteClient"></param>
  203. /// <param name="assetID"></param>
  204. /// <param name="transactionID"></param>
  205. /// <param name="type"></param>
  206. /// <param name="data"></param></param>
  207. /// <param name="tempFile"></param>
  208. public void HandleUDPUploadRequest(IClientAPI remoteClient,
  209. UUID assetID, UUID transactionID, sbyte type, byte[] data,
  210. bool storeLocal, bool tempFile)
  211. {
  212. // m_log.DebugFormat(
  213. // "[ASSET TRANSACTION MODULE]: HandleUDPUploadRequest - assetID: {0}, transaction {1}, type {2}, storeLocal {3}, tempFile {4}, data.Length {5}",
  214. // assetID, transactionID, type, storeLocal, tempFile, data.Length);
  215. if (((AssetType)type == AssetType.Texture ||
  216. (AssetType)type == AssetType.Sound ||
  217. (AssetType)type == AssetType.TextureTGA ||
  218. (AssetType)type == AssetType.Animation) &&
  219. tempFile == false)
  220. {
  221. ScenePresence avatar = null;
  222. Scene scene = (Scene)remoteClient.Scene;
  223. scene.TryGetScenePresence(remoteClient.AgentId, out avatar);
  224. // check user level
  225. if (avatar != null)
  226. {
  227. if (avatar.GodController.UserLevel < m_levelUpload)
  228. {
  229. remoteClient.SendAgentAlertMessage("Unable to upload asset. Insufficient permissions.", false);
  230. return;
  231. }
  232. }
  233. // check funds
  234. IMoneyModule mm = scene.RequestModuleInterface<IMoneyModule>();
  235. if (mm != null)
  236. {
  237. if (!mm.UploadCovered(remoteClient.AgentId, mm.UploadCharge))
  238. {
  239. remoteClient.SendAgentAlertMessage("Unable to upload asset. Insufficient funds.", false);
  240. return;
  241. }
  242. }
  243. }
  244. AgentAssetTransactions transactions = GetUserTransactions(remoteClient.AgentId);
  245. AssetXferUploader uploader = transactions.RequestXferUploader(transactionID);
  246. uploader.StartUpload(remoteClient, assetID, transactionID, type, data, storeLocal, tempFile);
  247. }
  248. /// <summary>
  249. /// Handle asset transfer data packets received in response to the
  250. /// asset upload request in HandleUDPUploadRequest()
  251. /// </summary>
  252. /// <param name="remoteClient"></param>
  253. /// <param name="xferID"></param>
  254. /// <param name="packetID"></param>
  255. /// <param name="data"></param>
  256. public void HandleXfer(IClientAPI remoteClient, ulong xferID,
  257. uint packetID, byte[] data)
  258. {
  259. // m_log.Debug("xferID: " + xferID + " packetID: " + packetID + " data length " + data.Length);
  260. AgentAssetTransactions transactions = GetUserTransactions(remoteClient.AgentId);
  261. transactions.HandleXfer(xferID, packetID, data);
  262. }
  263. #endregion
  264. }
  265. }