1
0

NewFileAgentInventoryVariablePriceModule.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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;
  29. using System.Collections.Specialized;
  30. using System.Reflection;
  31. using System.IO;
  32. using System.Web;
  33. using Mono.Addins;
  34. using log4net;
  35. using Nini.Config;
  36. using OpenMetaverse;
  37. using OpenMetaverse.StructuredData;
  38. using OpenSim.Framework;
  39. using OpenSim.Framework.Servers;
  40. using OpenSim.Framework.Servers.HttpServer;
  41. using OpenSim.Region.Framework.Interfaces;
  42. using OpenSim.Region.Framework.Scenes;
  43. using OpenSim.Services.Interfaces;
  44. using Caps = OpenSim.Framework.Capabilities.Caps;
  45. using OpenSim.Framework.Capabilities;
  46. namespace OpenSim.Region.ClientStack.Linden
  47. {
  48. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "NewFileAgentInventoryVariablePriceModule")]
  49. public class NewFileAgentInventoryVariablePriceModule : INonSharedRegionModule
  50. {
  51. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  52. private Scene m_scene;
  53. // private IAssetService m_assetService;
  54. private bool m_dumpAssetsToFile = false;
  55. private bool m_enabled = true;
  56. private int m_levelUpload = 0;
  57. #region Region Module interfaceBase Members
  58. public Type ReplaceableInterface
  59. {
  60. get { return null; }
  61. }
  62. public void Initialise(IConfigSource source)
  63. {
  64. IConfig meshConfig = source.Configs["Mesh"];
  65. if (meshConfig == null)
  66. return;
  67. m_enabled = meshConfig.GetBoolean("AllowMeshUpload", true);
  68. m_levelUpload = meshConfig.GetInt("LevelUpload", 0);
  69. }
  70. public void AddRegion(Scene pScene)
  71. {
  72. m_scene = pScene;
  73. }
  74. public void RemoveRegion(Scene scene)
  75. {
  76. m_scene.EventManager.OnRegisterCaps -= RegisterCaps;
  77. m_scene = null;
  78. }
  79. public void RegionLoaded(Scene scene)
  80. {
  81. // m_assetService = m_scene.RequestModuleInterface<IAssetService>();
  82. m_scene.EventManager.OnRegisterCaps += RegisterCaps;
  83. }
  84. #endregion
  85. #region Region Module interface
  86. public void Close() { }
  87. public string Name { get { return "NewFileAgentInventoryVariablePriceModule"; } }
  88. public void RegisterCaps(UUID agentID, Caps caps)
  89. {
  90. if(!m_enabled)
  91. return;
  92. UUID capID = UUID.Random();
  93. // m_log.Debug("[NEW FILE AGENT INVENTORY VARIABLE PRICE]: /CAPS/" + capID);
  94. caps.RegisterHandler(
  95. "NewFileAgentInventoryVariablePrice",
  96. new LLSDStreamhandler<LLSDAssetUploadRequest, LLSDNewFileAngentInventoryVariablePriceReplyResponse>(
  97. "POST",
  98. "/CAPS/" + capID.ToString(),
  99. req => NewAgentInventoryRequest(req, agentID),
  100. "NewFileAgentInventoryVariablePrice",
  101. agentID.ToString()));
  102. }
  103. #endregion
  104. public LLSDNewFileAngentInventoryVariablePriceReplyResponse NewAgentInventoryRequest(LLSDAssetUploadRequest llsdRequest, UUID agentID)
  105. {
  106. //TODO: The Mesh uploader uploads many types of content. If you're going to implement a Money based limit
  107. // you need to be aware of this
  108. //if (llsdRequest.asset_type == "texture" ||
  109. // llsdRequest.asset_type == "animation" ||
  110. // llsdRequest.asset_type == "sound")
  111. // {
  112. // check user level
  113. ScenePresence avatar = null;
  114. IClientAPI client = null;
  115. m_scene.TryGetScenePresence(agentID, out avatar);
  116. if (avatar != null)
  117. {
  118. client = avatar.ControllingClient;
  119. if (avatar.UserLevel < m_levelUpload)
  120. {
  121. if (client != null)
  122. client.SendAgentAlertMessage("Unable to upload asset. Insufficient permissions.", false);
  123. LLSDNewFileAngentInventoryVariablePriceReplyResponse errorResponse = new LLSDNewFileAngentInventoryVariablePriceReplyResponse();
  124. errorResponse.rsvp = "";
  125. errorResponse.state = "error";
  126. return errorResponse;
  127. }
  128. }
  129. // check funds
  130. IMoneyModule mm = m_scene.RequestModuleInterface<IMoneyModule>();
  131. if (mm != null)
  132. {
  133. if (!mm.UploadCovered(agentID, mm.UploadCharge))
  134. {
  135. if (client != null)
  136. client.SendAgentAlertMessage("Unable to upload asset. Insufficient funds.", false);
  137. LLSDNewFileAngentInventoryVariablePriceReplyResponse errorResponse = new LLSDNewFileAngentInventoryVariablePriceReplyResponse();
  138. errorResponse.rsvp = "";
  139. errorResponse.state = "error";
  140. return errorResponse;
  141. }
  142. }
  143. // }
  144. string assetName = llsdRequest.name;
  145. string assetDes = llsdRequest.description;
  146. string capsBase = "/CAPS/NewFileAgentInventoryVariablePrice/";
  147. UUID newAsset = UUID.Random();
  148. UUID newInvItem = UUID.Random();
  149. UUID parentFolder = llsdRequest.folder_id;
  150. string uploaderPath = Util.RandomClass.Next(5000, 8000).ToString("0000") + "/";
  151. AssetUploader uploader =
  152. new AssetUploader(assetName, assetDes, newAsset, newInvItem, parentFolder, llsdRequest.inventory_type,
  153. llsdRequest.asset_type, capsBase + uploaderPath, MainServer.Instance, m_dumpAssetsToFile);
  154. MainServer.Instance.AddStreamHandler(
  155. new BinaryStreamHandler(
  156. "POST",
  157. capsBase + uploaderPath,
  158. uploader.uploaderCaps,
  159. "NewFileAgentInventoryVariablePrice",
  160. agentID.ToString()));
  161. string protocol = "http://";
  162. if (MainServer.Instance.UseSSL)
  163. protocol = "https://";
  164. string uploaderURL = protocol + m_scene.RegionInfo.ExternalHostName + ":" + MainServer.Instance.Port.ToString() + capsBase +
  165. uploaderPath;
  166. LLSDNewFileAngentInventoryVariablePriceReplyResponse uploadResponse = new LLSDNewFileAngentInventoryVariablePriceReplyResponse();
  167. uploadResponse.rsvp = uploaderURL;
  168. uploadResponse.state = "upload";
  169. uploadResponse.resource_cost = 0;
  170. uploadResponse.upload_price = 0;
  171. uploader.OnUpLoad += //UploadCompleteHandler;
  172. delegate(
  173. string passetName, string passetDescription, UUID passetID,
  174. UUID pinventoryItem, UUID pparentFolder, byte[] pdata, string pinventoryType,
  175. string passetType)
  176. {
  177. UploadCompleteHandler(passetName, passetDescription, passetID,
  178. pinventoryItem, pparentFolder, pdata, pinventoryType,
  179. passetType,agentID);
  180. };
  181. return uploadResponse;
  182. }
  183. public void UploadCompleteHandler(string assetName, string assetDescription, UUID assetID,
  184. UUID inventoryItem, UUID parentFolder, byte[] data, string inventoryType,
  185. string assetType,UUID AgentID)
  186. {
  187. // m_log.DebugFormat(
  188. // "[NEW FILE AGENT INVENTORY VARIABLE PRICE MODULE]: Upload complete for {0}", inventoryItem);
  189. sbyte assType = 0;
  190. sbyte inType = 0;
  191. if (inventoryType == "sound")
  192. {
  193. inType = 1;
  194. assType = 1;
  195. }
  196. else if (inventoryType == "animation")
  197. {
  198. inType = 19;
  199. assType = 20;
  200. }
  201. else if (inventoryType == "wearable")
  202. {
  203. inType = 18;
  204. switch (assetType)
  205. {
  206. case "bodypart":
  207. assType = 13;
  208. break;
  209. case "clothing":
  210. assType = 5;
  211. break;
  212. }
  213. }
  214. else if (inventoryType == "mesh")
  215. {
  216. inType = (sbyte)InventoryType.Mesh;
  217. assType = (sbyte)AssetType.Mesh;
  218. }
  219. AssetBase asset;
  220. asset = new AssetBase(assetID, assetName, assType, AgentID.ToString());
  221. asset.Data = data;
  222. if (m_scene.AssetService != null)
  223. m_scene.AssetService.Store(asset);
  224. InventoryItemBase item = new InventoryItemBase();
  225. item.Owner = AgentID;
  226. item.CreatorId = AgentID.ToString();
  227. item.ID = inventoryItem;
  228. item.AssetID = asset.FullID;
  229. item.Description = assetDescription;
  230. item.Name = assetName;
  231. item.AssetType = assType;
  232. item.InvType = inType;
  233. item.Folder = parentFolder;
  234. item.CurrentPermissions
  235. = (uint)(PermissionMask.Move | PermissionMask.Copy | PermissionMask.Modify | PermissionMask.Transfer);
  236. item.BasePermissions = (uint)PermissionMask.All;
  237. item.EveryOnePermissions = 0;
  238. item.NextPermissions = (uint)PermissionMask.All;
  239. item.CreationDate = Util.UnixTimeSinceEpoch();
  240. m_scene.AddInventoryItem(item);
  241. }
  242. }
  243. }