BunchOfCaps.cs 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251
  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.Timers;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using System.Collections.Specialized;
  32. using System.IO;
  33. using System.Reflection;
  34. using System.Text;
  35. using System.Web;
  36. using OpenMetaverse;
  37. using OpenMetaverse.StructuredData;
  38. using Nini.Config;
  39. using log4net;
  40. using OpenSim.Framework;
  41. using OpenSim.Framework.Capabilities;
  42. using OpenSim.Region.Framework;
  43. using OpenSim.Region.Framework.Interfaces;
  44. using OpenSim.Region.Framework.Scenes;
  45. using OpenSim.Region.Framework.Scenes.Serialization;
  46. using OpenSim.Framework.Servers;
  47. using OpenSim.Framework.Servers.HttpServer;
  48. using OpenSim.Services.Interfaces;
  49. using Caps = OpenSim.Framework.Capabilities.Caps;
  50. using OSDArray = OpenMetaverse.StructuredData.OSDArray;
  51. using OSDMap = OpenMetaverse.StructuredData.OSDMap;
  52. using PermissionMask = OpenSim.Framework.PermissionMask;
  53. namespace OpenSim.Region.ClientStack.Linden
  54. {
  55. public delegate void UpLoadedAsset(
  56. string assetName, string description, UUID assetID, UUID inventoryItem, UUID parentFolder,
  57. byte[] data, string inventoryType, string assetType,
  58. int cost, UUID texturesFolder, int nreqtextures, int nreqmeshs, int nreqinstances,
  59. bool IsAtestUpload, ref string error, ref int nextOwnerMask, ref int groupMask, ref int everyoneMask);
  60. public delegate UUID UpdateItem(UUID itemID, byte[] data);
  61. public delegate void UpdateTaskScript(UUID itemID, UUID primID, bool isScriptRunning, byte[] data, ref ArrayList errors);
  62. public delegate void NewInventoryItem(UUID userID, InventoryItemBase item, uint cost);
  63. public delegate void NewAsset(AssetBase asset);
  64. public delegate UUID ItemUpdatedCallback(UUID userID, UUID itemID, byte[] data);
  65. public delegate ArrayList TaskScriptUpdatedCallback(UUID userID, UUID itemID, UUID primID,
  66. bool isScriptRunning, byte[] data);
  67. public delegate InventoryCollection FetchInventoryDescendentsCAPS(UUID agentID, UUID folderID, UUID ownerID,
  68. bool fetchFolders, bool fetchItems, int sortOrder, out int version);
  69. /// <summary>
  70. /// XXX Probably not a particularly nice way of allow us to get the scene presence from the scene (chiefly so that
  71. /// we can popup a message on the user's client if the inventory service has permanently failed). But I didn't want
  72. /// to just pass the whole Scene into CAPS.
  73. /// </summary>
  74. public delegate IClientAPI GetClientDelegate(UUID agentID);
  75. public class BunchOfCaps
  76. {
  77. private static readonly ILog m_log =
  78. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  79. private Scene m_Scene;
  80. private UUID m_AgentID;
  81. private Caps m_HostCapsObj;
  82. private ModelCost m_ModelCost;
  83. // private static readonly string m_remoteParcelRequestPath = "0009/";// This is in the LandManagementModule.
  84. // These are callbacks which will be setup by the scene so that we can update scene data when we
  85. // receive capability calls
  86. public NewInventoryItem AddNewInventoryItem = null;
  87. public NewAsset AddNewAsset = null;
  88. public ItemUpdatedCallback ItemUpdatedCall = null;
  89. public TaskScriptUpdatedCallback TaskScriptUpdatedCall = null;
  90. public FetchInventoryDescendentsCAPS CAPSFetchInventoryDescendents = null;
  91. public GetClientDelegate GetClient = null;
  92. private bool m_persistBakedTextures = false;
  93. private IAssetService m_assetService;
  94. private bool m_dumpAssetsToFile = false;
  95. private string m_regionName;
  96. private int m_levelUpload = 0;
  97. private bool m_enableFreeTestUpload = false; // allows "TEST-" prefix hack
  98. private bool m_ForceFreeTestUpload = false; // forces all uploads to be test
  99. private bool m_enableModelUploadTextureToInventory = false; // place uploaded textures also in inventory
  100. // may not be visible till relog
  101. private bool m_RestrictFreeTestUploadPerms = false; // reduces also the permitions. Needs a creator defined!!
  102. private UUID m_testAssetsCreatorID = UUID.Zero;
  103. private float m_PrimScaleMin = 0.001f;
  104. private bool m_AllowCapHomeLocation = true;
  105. private bool m_AllowCapGroupMemberData = true;
  106. private IUserManagement m_UserManager;
  107. private enum FileAgentInventoryState : int
  108. {
  109. idle = 0,
  110. processRequest = 1,
  111. waitUpload = 2,
  112. processUpload = 3
  113. }
  114. private FileAgentInventoryState m_FileAgentInventoryState = FileAgentInventoryState.idle;
  115. public BunchOfCaps(Scene scene, UUID agentID, Caps caps)
  116. {
  117. m_Scene = scene;
  118. m_AgentID = agentID;
  119. m_HostCapsObj = caps;
  120. // create a model upload cost provider
  121. m_ModelCost = new ModelCost(scene);
  122. m_PrimScaleMin = m_ModelCost.PrimScaleMin;
  123. IConfigSource config = m_Scene.Config;
  124. if (config != null)
  125. {
  126. IConfig sconfig = config.Configs["Startup"];
  127. if (sconfig != null)
  128. {
  129. m_levelUpload = sconfig.GetInt("LevelUpload", 0);
  130. }
  131. IConfig appearanceConfig = config.Configs["Appearance"];
  132. if (appearanceConfig != null)
  133. {
  134. m_persistBakedTextures = appearanceConfig.GetBoolean("PersistBakedTextures", m_persistBakedTextures);
  135. }
  136. // economy for model upload
  137. IConfig EconomyConfig = config.Configs["Economy"];
  138. if (EconomyConfig != null)
  139. {
  140. m_ModelCost.Econfig(EconomyConfig);
  141. m_enableModelUploadTextureToInventory = EconomyConfig.GetBoolean("MeshModelAllowTextureToInventory", m_enableModelUploadTextureToInventory);
  142. m_RestrictFreeTestUploadPerms = EconomyConfig.GetBoolean("m_RestrictFreeTestUploadPerms", m_RestrictFreeTestUploadPerms);
  143. m_enableFreeTestUpload = EconomyConfig.GetBoolean("AllowFreeTestUpload", m_enableFreeTestUpload);
  144. m_ForceFreeTestUpload = EconomyConfig.GetBoolean("ForceFreeTestUpload", m_ForceFreeTestUpload);
  145. string testcreator = EconomyConfig.GetString("TestAssetsCreatorID", "");
  146. if (testcreator != "")
  147. {
  148. UUID id;
  149. UUID.TryParse(testcreator, out id);
  150. if (id != null)
  151. m_testAssetsCreatorID = id;
  152. }
  153. }
  154. IConfig CapsConfig = config.Configs["ClientStack.LindenCaps"];
  155. if (CapsConfig != null)
  156. {
  157. string homeLocationUrl = CapsConfig.GetString("Cap_HomeLocation", "localhost");
  158. if(homeLocationUrl == String.Empty)
  159. m_AllowCapHomeLocation = false;
  160. string GroupMemberDataUrl = CapsConfig.GetString("Cap_GroupMemberData", "localhost");
  161. if(GroupMemberDataUrl == String.Empty)
  162. m_AllowCapGroupMemberData = false;
  163. }
  164. }
  165. m_assetService = m_Scene.AssetService;
  166. m_regionName = m_Scene.RegionInfo.RegionName;
  167. m_UserManager = m_Scene.RequestModuleInterface<IUserManagement>();
  168. if (m_UserManager == null)
  169. m_log.Error("[CAPS]: GetDisplayNames disabled because user management component not found");
  170. RegisterHandlers();
  171. AddNewInventoryItem = m_Scene.AddUploadedInventoryItem;
  172. ItemUpdatedCall = m_Scene.CapsUpdateInventoryItemAsset;
  173. TaskScriptUpdatedCall = m_Scene.CapsUpdateTaskInventoryScriptAsset;
  174. GetClient = m_Scene.SceneGraph.GetControllingClient;
  175. m_FileAgentInventoryState = FileAgentInventoryState.idle;
  176. }
  177. public string GetNewCapPath()
  178. {
  179. return "/CAPS/" + UUID.Random();
  180. }
  181. /// <summary>
  182. /// Register a bunch of CAPS http service handlers
  183. /// </summary>
  184. public void RegisterHandlers()
  185. {
  186. // this path is also defined elsewhere so keeping it
  187. string seedcapsBase = "/CAPS/" + m_HostCapsObj.CapsObjectPath +"0000/";
  188. // the root of all evil path needs to be capsBase + m_requestPath
  189. m_HostCapsObj.RegisterHandler(
  190. "SEED", new RestStreamHandler("POST", seedcapsBase, SeedCapRequest, "SEED", null));
  191. // m_log.DebugFormat(
  192. // "[CAPS]: Registered seed capability {0} for {1}", seedcapsBase, m_HostCapsObj.AgentID);
  193. RegisterRegionServiceHandlers();
  194. RegisterInventoryServiceHandlers();
  195. RegisterOtherHandlers();
  196. }
  197. public void RegisterRegionServiceHandlers()
  198. {
  199. try
  200. {
  201. //m_capsHandlers["MapLayer"] =
  202. // new LLSDStreamhandler<OSDMapRequest, OSDMapLayerResponse>("POST",
  203. // GetNewCapPath(),
  204. // GetMapLayer);
  205. IRequestHandler getObjectPhysicsDataHandler = new RestStreamHandler(
  206. "POST", GetNewCapPath(), GetObjectPhysicsData, "GetObjectPhysicsData", null);
  207. m_HostCapsObj.RegisterHandler("GetObjectPhysicsData", getObjectPhysicsDataHandler);
  208. IRequestHandler getObjectCostHandler = new RestStreamHandler(
  209. "POST", GetNewCapPath(), GetObjectCost, "GetObjectCost", null );
  210. m_HostCapsObj.RegisterHandler("GetObjectCost", getObjectCostHandler);
  211. IRequestHandler ResourceCostSelectedHandler = new RestStreamHandler(
  212. "POST", GetNewCapPath(), ResourceCostSelected, "ResourceCostSelected", null);
  213. m_HostCapsObj.RegisterHandler("ResourceCostSelected", ResourceCostSelectedHandler);
  214. IRequestHandler req = new RestStreamHandler(
  215. "POST", GetNewCapPath(), ScriptTaskInventory, "UpdateScript", null);
  216. m_HostCapsObj.RegisterHandler("UpdateScriptTaskInventory", req);
  217. m_HostCapsObj.RegisterHandler("UpdateScriptTask", req);
  218. if(m_AllowCapHomeLocation)
  219. {
  220. IRequestHandler HomeLocationHandler = new RestStreamHandler(
  221. "POST", GetNewCapPath(), HomeLocation, "HomeLocation", null);
  222. m_HostCapsObj.RegisterHandler("HomeLocation", HomeLocationHandler);
  223. }
  224. if(m_AllowCapGroupMemberData)
  225. {
  226. IRequestHandler GroupMemberDataHandler = new RestStreamHandler(
  227. "POST", GetNewCapPath(), GroupMemberData, "GroupMemberData", null);
  228. m_HostCapsObj.RegisterHandler("GroupMemberData", GroupMemberDataHandler);
  229. }
  230. // IRequestHandler animSetRequestHandler
  231. // = new RestStreamHandler(
  232. // "POST", capsBase + m_animSetTaskUpdatePath, AnimSetTaskInventory, "UpdateScript", null);
  233. // m_HostCapsObj.RegisterHandler("UpdateAnimSetTaskInventory", animSetRequestHandler);
  234. }
  235. catch (Exception e)
  236. {
  237. m_log.Error("[CAPS]: " + e.ToString());
  238. }
  239. }
  240. public void RegisterInventoryServiceHandlers()
  241. {
  242. try
  243. {
  244. m_HostCapsObj.RegisterHandler("NewFileAgentInventory",
  245. new LLSDStreamhandler<LLSDAssetUploadRequest, LLSDAssetUploadResponse>(
  246. "POST", GetNewCapPath(), NewAgentInventoryRequest, "NewFileAgentInventory", null));
  247. IRequestHandler req = new RestStreamHandler(
  248. "POST", GetNewCapPath(), NoteCardAgentInventory, "Update*", null);
  249. m_HostCapsObj.RegisterHandler("UpdateNotecardAgentInventory", req);
  250. m_HostCapsObj.RegisterHandler("UpdateAnimSetAgentInventory", req);
  251. m_HostCapsObj.RegisterHandler("UpdateScriptAgentInventory", req);
  252. m_HostCapsObj.RegisterHandler("UpdateScriptAgent", req);
  253. IRequestHandler UpdateAgentInformationHandler = new RestStreamHandler(
  254. "POST", GetNewCapPath(), UpdateAgentInformation, "UpdateAgentInformation", null);
  255. m_HostCapsObj.RegisterHandler("UpdateAgentInformation", UpdateAgentInformationHandler);
  256. IRequestHandler CopyInventoryFromNotecardHandler = new RestStreamHandler(
  257. "POST", GetNewCapPath(), CopyInventoryFromNotecard, "CopyInventoryFromNotecard", null);
  258. m_HostCapsObj.RegisterHandler("CopyInventoryFromNotecard", CopyInventoryFromNotecardHandler);
  259. }
  260. catch (Exception e)
  261. {
  262. m_log.Error("[CAPS]: " + e.ToString());
  263. }
  264. }
  265. public void RegisterOtherHandlers()
  266. {
  267. try
  268. {
  269. if (m_UserManager != null)
  270. {
  271. IRequestHandler GetDisplayNamesHandler = new RestStreamHandler(
  272. "GET", GetNewCapPath(), GetDisplayNames, "GetDisplayNames", null);
  273. m_HostCapsObj.RegisterHandler("GetDisplayNames", GetDisplayNamesHandler);
  274. }
  275. }
  276. catch (Exception e)
  277. {
  278. m_log.Error("[CAPS]: " + e.ToString());
  279. }
  280. }
  281. /// <summary>
  282. /// Construct a client response detailing all the capabilities this server can provide.
  283. /// </summary>
  284. /// <param name="request"></param>
  285. /// <param name="path"></param>
  286. /// <param name="param"></param>
  287. /// <param name="httpRequest">HTTP request header object</param>
  288. /// <param name="httpResponse">HTTP response header object</param>
  289. /// <returns></returns>
  290. public string SeedCapRequest(string request, string path, string param,
  291. IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  292. {
  293. m_log.DebugFormat(
  294. "[CAPS]: Received SEED caps request in {0} for agent {1}", m_regionName, m_HostCapsObj.AgentID);
  295. if (!m_HostCapsObj.WaitForActivation())
  296. return string.Empty;
  297. if (!m_Scene.CheckClient(m_HostCapsObj.AgentID, httpRequest.RemoteIPEndPoint))
  298. {
  299. m_log.WarnFormat(
  300. "[CAPS]: Unauthorized CAPS client {0} from {1}",
  301. m_HostCapsObj.AgentID, httpRequest.RemoteIPEndPoint);
  302. return string.Empty;
  303. }
  304. OSDArray capsRequested = (OSDArray)OSDParser.DeserializeLLSDXml(request);
  305. List<string> validCaps = new List<string>();
  306. foreach (OSD c in capsRequested)
  307. validCaps.Add(c.AsString());
  308. string result = LLSDHelpers.SerialiseLLSDReply(m_HostCapsObj.GetCapsDetails(true, validCaps));
  309. //m_log.DebugFormat("[CAPS] CapsRequest {0}", result);
  310. return result;
  311. }
  312. /// <summary>
  313. /// Called by the script task update handler. Provides a URL to which the client can upload a new asset.
  314. /// </summary>
  315. /// <param name="request"></param>
  316. /// <param name="path"></param>
  317. /// <param name="param"></param>
  318. /// <param name="httpRequest">HTTP request header object</param>
  319. /// <param name="httpResponse">HTTP response header object</param>
  320. /// <returns></returns>
  321. public string ScriptTaskInventory(string request, string path, string param,
  322. IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  323. {
  324. try
  325. {
  326. // m_log.Debug("[CAPS]: ScriptTaskInventory Request in region: " + m_regionName);
  327. //m_log.DebugFormat("[CAPS]: request: {0}, path: {1}, param: {2}", request, path, param);
  328. Hashtable hash = (Hashtable)LLSD.LLSDDeserialize(Utils.StringToBytes(request));
  329. LLSDTaskScriptUpdate llsdUpdateRequest = new LLSDTaskScriptUpdate();
  330. LLSDHelpers.DeserialiseOSDMap(hash, llsdUpdateRequest);
  331. string uploaderPath = GetNewCapPath();
  332. TaskInventoryScriptUpdater uploader =
  333. new TaskInventoryScriptUpdater(
  334. llsdUpdateRequest.item_id,
  335. llsdUpdateRequest.task_id,
  336. llsdUpdateRequest.is_script_running,
  337. uploaderPath,
  338. m_HostCapsObj.HttpListener,
  339. m_dumpAssetsToFile);
  340. uploader.OnUpLoad += TaskScriptUpdated;
  341. m_HostCapsObj.HttpListener.AddStreamHandler(
  342. new BinaryStreamHandler(
  343. "POST", uploaderPath, uploader.uploaderCaps, "TaskInventoryScriptUpdater", null));
  344. string protocol = "http://";
  345. if (m_HostCapsObj.SSLCaps)
  346. protocol = "https://";
  347. string uploaderURL = protocol + m_HostCapsObj.HostName + ":" + m_HostCapsObj.Port.ToString() + uploaderPath;
  348. LLSDAssetUploadResponse uploadResponse = new LLSDAssetUploadResponse();
  349. uploadResponse.uploader = uploaderURL;
  350. uploadResponse.state = "upload";
  351. // m_log.InfoFormat("[CAPS]: " +
  352. // "ScriptTaskInventory response: {0}",
  353. // LLSDHelpers.SerialiseLLSDReply(uploadResponse)));
  354. return LLSDHelpers.SerialiseLLSDReply(uploadResponse);
  355. }
  356. catch (Exception e)
  357. {
  358. m_log.Error("[CAPS]: " + e.ToString());
  359. }
  360. return null;
  361. }
  362. /// <summary>
  363. /// Called when new asset data for an agent inventory item update has been uploaded.
  364. /// </summary>
  365. /// <param name="itemID">Item to update</param>
  366. /// <param name="primID">Prim containing item to update</param>
  367. /// <param name="isScriptRunning">Signals whether the script to update is currently running</param>
  368. /// <param name="data">New asset data</param>
  369. public void TaskScriptUpdated(UUID itemID, UUID primID, bool isScriptRunning, byte[] data, ref ArrayList errors)
  370. {
  371. if (TaskScriptUpdatedCall != null)
  372. {
  373. ArrayList e = TaskScriptUpdatedCall(m_HostCapsObj.AgentID, itemID, primID, isScriptRunning, data);
  374. foreach (Object item in e)
  375. errors.Add(item);
  376. }
  377. }
  378. /// <summary>
  379. /// Called when new asset data for an agent inventory item update has been uploaded.
  380. /// </summary>
  381. /// <param name="itemID">Item to update</param>
  382. /// <param name="data">New asset data</param>
  383. /// <returns></returns>
  384. public UUID ItemUpdated(UUID itemID, byte[] data)
  385. {
  386. if (ItemUpdatedCall != null)
  387. {
  388. return ItemUpdatedCall(m_HostCapsObj.AgentID, itemID, data);
  389. }
  390. return UUID.Zero;
  391. }
  392. /// <summary>
  393. ///
  394. /// </summary>
  395. /// <param name="llsdRequest"></param>
  396. /// <returns></returns>
  397. public LLSDAssetUploadResponse NewAgentInventoryRequest(LLSDAssetUploadRequest llsdRequest)
  398. {
  399. //m_log.Debug("[CAPS]: NewAgentInventoryRequest Request is: " + llsdRequest.ToString());
  400. //m_log.Debug("asset upload request via CAPS" + llsdRequest.inventory_type + " , " + llsdRequest.asset_type);
  401. // start by getting the client
  402. IClientAPI client = null;
  403. m_Scene.TryGetClient(m_HostCapsObj.AgentID, out client);
  404. // check current state so we only have one service at a time
  405. lock (m_ModelCost)
  406. {
  407. switch (m_FileAgentInventoryState)
  408. {
  409. case FileAgentInventoryState.processRequest:
  410. case FileAgentInventoryState.processUpload:
  411. LLSDAssetUploadError resperror = new LLSDAssetUploadError();
  412. resperror.message = "Uploader busy processing previus request";
  413. resperror.identifier = UUID.Zero;
  414. LLSDAssetUploadResponse errorResponse = new LLSDAssetUploadResponse();
  415. errorResponse.uploader = "";
  416. errorResponse.state = "error";
  417. errorResponse.error = resperror;
  418. return errorResponse;
  419. break;
  420. case FileAgentInventoryState.waitUpload:
  421. // todo stop current uploader server
  422. break;
  423. case FileAgentInventoryState.idle:
  424. default:
  425. break;
  426. }
  427. m_FileAgentInventoryState = FileAgentInventoryState.processRequest;
  428. }
  429. int cost = 0;
  430. int nreqtextures = 0;
  431. int nreqmeshs= 0;
  432. int nreqinstances = 0;
  433. bool IsAtestUpload = false;
  434. string assetName = llsdRequest.name;
  435. LLSDAssetUploadResponseData meshcostdata = new LLSDAssetUploadResponseData();
  436. if (llsdRequest.asset_type == "texture" ||
  437. llsdRequest.asset_type == "animation" ||
  438. llsdRequest.asset_type == "animatn" || // this is the asset name actually used by viewers
  439. llsdRequest.asset_type == "mesh" ||
  440. llsdRequest.asset_type == "sound")
  441. {
  442. ScenePresence avatar = null;
  443. m_Scene.TryGetScenePresence(m_HostCapsObj.AgentID, out avatar);
  444. // check user level
  445. if (avatar != null)
  446. {
  447. if (avatar.GodController.UserLevel < m_levelUpload)
  448. {
  449. LLSDAssetUploadError resperror = new LLSDAssetUploadError();
  450. resperror.message = "Insufficient permissions to upload";
  451. resperror.identifier = UUID.Zero;
  452. LLSDAssetUploadResponse errorResponse = new LLSDAssetUploadResponse();
  453. errorResponse.uploader = "";
  454. errorResponse.state = "error";
  455. errorResponse.error = resperror;
  456. lock (m_ModelCost)
  457. m_FileAgentInventoryState = FileAgentInventoryState.idle;
  458. return errorResponse;
  459. }
  460. }
  461. // check test upload and funds
  462. if (client != null)
  463. {
  464. IMoneyModule mm = m_Scene.RequestModuleInterface<IMoneyModule>();
  465. int baseCost = 0;
  466. if (mm != null)
  467. baseCost = mm.UploadCharge;
  468. string warning = String.Empty;
  469. if (llsdRequest.asset_type == "mesh")
  470. {
  471. string error;
  472. int modelcost;
  473. if (!m_ModelCost.MeshModelCost(llsdRequest.asset_resources, baseCost, out modelcost,
  474. meshcostdata, out error, ref warning))
  475. {
  476. LLSDAssetUploadError resperror = new LLSDAssetUploadError();
  477. resperror.message = error;
  478. resperror.identifier = UUID.Zero;
  479. LLSDAssetUploadResponse errorResponse = new LLSDAssetUploadResponse();
  480. errorResponse.uploader = "";
  481. errorResponse.state = "error";
  482. errorResponse.error = resperror;
  483. lock (m_ModelCost)
  484. m_FileAgentInventoryState = FileAgentInventoryState.idle;
  485. return errorResponse;
  486. }
  487. cost = modelcost;
  488. }
  489. else
  490. {
  491. cost = baseCost;
  492. }
  493. if (cost > 0 && mm != null)
  494. {
  495. // check for test upload
  496. if (m_ForceFreeTestUpload) // all are test
  497. {
  498. if (!(assetName.Length > 5 && assetName.StartsWith("TEST-"))) // has normal name lets change it
  499. assetName = "TEST-" + assetName;
  500. IsAtestUpload = true;
  501. }
  502. else if (m_enableFreeTestUpload) // only if prefixed with "TEST-"
  503. {
  504. IsAtestUpload = (assetName.Length > 5 && assetName.StartsWith("TEST-"));
  505. }
  506. if(IsAtestUpload) // let user know, still showing cost estimation
  507. warning += "Upload will have no cost, for testing purposes only. Other uses are prohibited. Items will not work after 48 hours or on other regions";
  508. // check funds
  509. else
  510. {
  511. if (!mm.UploadCovered(client.AgentId, (int)cost))
  512. {
  513. LLSDAssetUploadError resperror = new LLSDAssetUploadError();
  514. resperror.message = "Insuficient funds";
  515. resperror.identifier = UUID.Zero;
  516. LLSDAssetUploadResponse errorResponse = new LLSDAssetUploadResponse();
  517. errorResponse.uploader = "";
  518. errorResponse.state = "error";
  519. errorResponse.error = resperror;
  520. lock (m_ModelCost)
  521. m_FileAgentInventoryState = FileAgentInventoryState.idle;
  522. return errorResponse;
  523. }
  524. }
  525. }
  526. if (client != null && warning != String.Empty)
  527. client.SendAgentAlertMessage(warning, true);
  528. }
  529. }
  530. string assetDes = llsdRequest.description;
  531. UUID newAsset = UUID.Random();
  532. UUID newInvItem = UUID.Random();
  533. UUID parentFolder = llsdRequest.folder_id;
  534. string uploaderPath = GetNewCapPath();
  535. UUID texturesFolder = UUID.Zero;
  536. if(!IsAtestUpload && m_enableModelUploadTextureToInventory)
  537. texturesFolder = llsdRequest.texture_folder_id;
  538. AssetUploader uploader =
  539. new AssetUploader(assetName, assetDes, newAsset, newInvItem, parentFolder, llsdRequest.inventory_type,
  540. llsdRequest.asset_type, uploaderPath, m_HostCapsObj.HttpListener, m_dumpAssetsToFile, cost,
  541. texturesFolder, nreqtextures, nreqmeshs, nreqinstances, IsAtestUpload,
  542. llsdRequest.next_owner_mask, llsdRequest.group_mask, llsdRequest.everyone_mask);
  543. m_HostCapsObj.HttpListener.AddStreamHandler(
  544. new BinaryStreamHandler(
  545. "POST",
  546. uploaderPath,
  547. uploader.uploaderCaps,
  548. "NewAgentInventoryRequest",
  549. m_HostCapsObj.AgentID.ToString()));
  550. string protocol = "http://";
  551. if (m_HostCapsObj.SSLCaps)
  552. protocol = "https://";
  553. string uploaderURL = protocol + m_HostCapsObj.HostName + ":" + m_HostCapsObj.Port.ToString() + uploaderPath;
  554. LLSDAssetUploadResponse uploadResponse = new LLSDAssetUploadResponse();
  555. uploadResponse.uploader = uploaderURL;
  556. uploadResponse.state = "upload";
  557. uploadResponse.upload_price = (int)cost;
  558. if (llsdRequest.asset_type == "mesh")
  559. {
  560. uploadResponse.data = meshcostdata;
  561. }
  562. uploader.OnUpLoad += UploadCompleteHandler;
  563. lock (m_ModelCost)
  564. m_FileAgentInventoryState = FileAgentInventoryState.waitUpload;
  565. return uploadResponse;
  566. }
  567. /// <summary>
  568. /// Convert raw uploaded data into the appropriate asset and item.
  569. /// </summary>
  570. /// <param name="assetID"></param>
  571. /// <param name="inventoryItem"></param>
  572. /// <param name="data"></param>
  573. public void UploadCompleteHandler(string assetName, string assetDescription, UUID assetID,
  574. UUID inventoryItem, UUID parentFolder, byte[] data, string inventoryType,
  575. string assetType, int cost,
  576. UUID texturesFolder, int nreqtextures, int nreqmeshs, int nreqinstances,
  577. bool IsAtestUpload, ref string error,
  578. ref int nextOwnerMask, ref int groupMask, ref int everyoneMask)
  579. {
  580. lock (m_ModelCost)
  581. m_FileAgentInventoryState = FileAgentInventoryState.processUpload;
  582. m_log.DebugFormat(
  583. "[BUNCH OF CAPS]: Uploaded asset {0} for inventory item {1}, inv type {2}, asset type {3}",
  584. assetID, inventoryItem, inventoryType, assetType);
  585. sbyte assType = 0;
  586. sbyte inType = 0;
  587. IClientAPI client = null;
  588. UUID owner_id = m_HostCapsObj.AgentID;
  589. UUID creatorID;
  590. bool istest = IsAtestUpload && m_enableFreeTestUpload && (cost > 0);
  591. bool restrictPerms = m_RestrictFreeTestUploadPerms && istest;
  592. if (istest && m_testAssetsCreatorID != UUID.Zero)
  593. creatorID = m_testAssetsCreatorID;
  594. else
  595. creatorID = owner_id;
  596. string creatorIDstr = creatorID.ToString();
  597. IMoneyModule mm = m_Scene.RequestModuleInterface<IMoneyModule>();
  598. if (mm != null)
  599. {
  600. // make sure client still has enougth credit
  601. if (!mm.UploadCovered(m_HostCapsObj.AgentID, (int)cost))
  602. {
  603. error = "Insufficient funds.";
  604. return;
  605. }
  606. }
  607. // strings to types
  608. if (inventoryType == "sound")
  609. {
  610. inType = (sbyte)InventoryType.Sound;
  611. assType = (sbyte)AssetType.Sound;
  612. }
  613. else if (inventoryType == "snapshot")
  614. {
  615. inType = (sbyte)InventoryType.Snapshot;
  616. }
  617. else if (inventoryType == "animation")
  618. {
  619. inType = (sbyte)InventoryType.Animation;
  620. assType = (sbyte)AssetType.Animation;
  621. }
  622. else if (inventoryType == "animset")
  623. {
  624. inType = (sbyte)CustomInventoryType.AnimationSet;
  625. assType = (sbyte)CustomAssetType.AnimationSet;
  626. m_log.Debug("got animset upload request");
  627. }
  628. else if (inventoryType == "wearable")
  629. {
  630. inType = (sbyte)InventoryType.Wearable;
  631. switch (assetType)
  632. {
  633. case "bodypart":
  634. assType = (sbyte)AssetType.Bodypart;
  635. break;
  636. case "clothing":
  637. assType = (sbyte)AssetType.Clothing;
  638. break;
  639. }
  640. }
  641. else if (inventoryType == "object")
  642. {
  643. if (assetType == "mesh") // this code for now is for mesh models uploads only
  644. {
  645. inType = (sbyte)InventoryType.Object;
  646. assType = (sbyte)AssetType.Object;
  647. List<Vector3> positions = new List<Vector3>();
  648. List<Quaternion> rotations = new List<Quaternion>();
  649. OSDMap request = (OSDMap)OSDParser.DeserializeLLSDXml(data);
  650. // compare and get updated information
  651. /* does nothing still we do need something to avoid special viewer to upload something diferent from the cost estimation
  652. bool mismatchError = true;
  653. while (mismatchError)
  654. {
  655. mismatchError = false;
  656. }
  657. if (mismatchError)
  658. {
  659. error = "Upload and fee estimation information don't match";
  660. lock (m_ModelCost)
  661. m_FileAgentInventoryState = FileAgentInventoryState.idle;
  662. return;
  663. }
  664. */
  665. OSDArray instance_list = (OSDArray)request["instance_list"];
  666. OSDArray mesh_list = (OSDArray)request["mesh_list"];
  667. OSDArray texture_list = (OSDArray)request["texture_list"];
  668. SceneObjectGroup grp = null;
  669. // create and store texture assets
  670. bool doTextInv = (!istest && m_enableModelUploadTextureToInventory &&
  671. texturesFolder != UUID.Zero);
  672. List<UUID> textures = new List<UUID>();
  673. // if (doTextInv)
  674. m_Scene.TryGetClient(m_HostCapsObj.AgentID, out client);
  675. if(client == null) // don't put textures in inventory if there is no client
  676. doTextInv = false;
  677. for (int i = 0; i < texture_list.Count; i++)
  678. {
  679. AssetBase textureAsset = new AssetBase(UUID.Random(), assetName, (sbyte)AssetType.Texture, creatorIDstr);
  680. textureAsset.Data = texture_list[i].AsBinary();
  681. if (istest)
  682. textureAsset.Local = true;
  683. m_assetService.Store(textureAsset);
  684. textures.Add(textureAsset.FullID);
  685. if (doTextInv)
  686. {
  687. string name = assetName;
  688. if (name.Length > 25)
  689. name = name.Substring(0, 24);
  690. name += "_Texture#" + i.ToString();
  691. InventoryItemBase texitem = new InventoryItemBase();
  692. texitem.Owner = m_HostCapsObj.AgentID;
  693. texitem.CreatorId = creatorIDstr;
  694. texitem.CreatorData = String.Empty;
  695. texitem.ID = UUID.Random();
  696. texitem.AssetID = textureAsset.FullID;
  697. texitem.Description = "mesh model texture";
  698. texitem.Name = name;
  699. texitem.AssetType = (int)AssetType.Texture;
  700. texitem.InvType = (int)InventoryType.Texture;
  701. texitem.Folder = texturesFolder;
  702. texitem.CurrentPermissions
  703. = (uint)(PermissionMask.Move | PermissionMask.Copy | PermissionMask.Modify | PermissionMask.Transfer | PermissionMask.Export);
  704. texitem.BasePermissions = (uint)PermissionMask.All | (uint)PermissionMask.Export;
  705. texitem.EveryOnePermissions = 0;
  706. texitem.NextPermissions = (uint)PermissionMask.All;
  707. texitem.CreationDate = Util.UnixTimeSinceEpoch();
  708. m_Scene.AddInventoryItem(client, texitem);
  709. texitem = null;
  710. }
  711. }
  712. // create and store meshs assets
  713. List<UUID> meshAssets = new List<UUID>();
  714. List<bool> meshAvatarSkeletons = new List<bool>();
  715. List<bool> meshAvatarColliders = new List<bool>();
  716. bool curAvSkeleton;
  717. bool curAvCollider;
  718. for (int i = 0; i < mesh_list.Count; i++)
  719. {
  720. curAvSkeleton = false;
  721. curAvCollider = false;
  722. // we do need to parse the mesh now
  723. OSD osd = OSDParser.DeserializeLLSDBinary(mesh_list[i]);
  724. if (osd is OSDMap)
  725. {
  726. OSDMap mosd = (OSDMap)osd;
  727. if (mosd.ContainsKey("skeleton"))
  728. {
  729. OSDMap skeleton = (OSDMap)mosd["skeleton"];
  730. int sksize = skeleton["size"].AsInteger();
  731. if (sksize > 0)
  732. curAvSkeleton = true;
  733. }
  734. }
  735. AssetBase meshAsset = new AssetBase(UUID.Random(), assetName, (sbyte)AssetType.Mesh, creatorIDstr);
  736. meshAsset.Data = mesh_list[i].AsBinary();
  737. if (istest)
  738. meshAsset.Local = true;
  739. m_assetService.Store(meshAsset);
  740. meshAssets.Add(meshAsset.FullID);
  741. meshAvatarSkeletons.Add(curAvSkeleton);
  742. meshAvatarColliders.Add(curAvCollider);
  743. // test code
  744. if (curAvSkeleton && client != null)
  745. {
  746. string name = assetName;
  747. if (name.Length > 25)
  748. name = name.Substring(0, 24);
  749. name += "_Mesh#" + i.ToString();
  750. InventoryItemBase meshitem = new InventoryItemBase();
  751. meshitem.Owner = m_HostCapsObj.AgentID;
  752. meshitem.CreatorId = creatorIDstr;
  753. meshitem.CreatorData = String.Empty;
  754. meshitem.ID = UUID.Random();
  755. meshitem.AssetID = meshAsset.FullID;
  756. meshitem.Description = "mesh ";
  757. meshitem.Name = name;
  758. meshitem.AssetType = (int)AssetType.Mesh;
  759. meshitem.InvType = (int)InventoryType.Mesh;
  760. // meshitem.Folder = UUID.Zero; // send to default
  761. meshitem.Folder = parentFolder; // dont let it go to folder Meshes that viewers dont show
  762. // If we set PermissionMask.All then when we rez the item the next permissions will replace the current
  763. // (owner) permissions. This becomes a problem if next permissions are changed.
  764. meshitem.CurrentPermissions
  765. = (uint)(PermissionMask.Move | PermissionMask.Copy | PermissionMask.Modify | PermissionMask.Transfer);
  766. meshitem.BasePermissions = (uint)PermissionMask.All;
  767. meshitem.EveryOnePermissions = 0;
  768. meshitem.NextPermissions = (uint)PermissionMask.All;
  769. meshitem.CreationDate = Util.UnixTimeSinceEpoch();
  770. m_Scene.AddInventoryItem(client, meshitem);
  771. meshitem = null;
  772. }
  773. }
  774. int skipedMeshs = 0;
  775. // build prims from instances
  776. for (int i = 0; i < instance_list.Count; i++)
  777. {
  778. OSDMap inner_instance_list = (OSDMap)instance_list[i];
  779. // skip prims that are 2 small
  780. Vector3 scale = inner_instance_list["scale"].AsVector3();
  781. if (scale.X < m_PrimScaleMin || scale.Y < m_PrimScaleMin || scale.Z < m_PrimScaleMin)
  782. {
  783. skipedMeshs++;
  784. continue;
  785. }
  786. OSDArray face_list = (OSDArray)inner_instance_list["face_list"];
  787. PrimitiveBaseShape pbs = null;
  788. if (inner_instance_list.ContainsKey("mesh")) // seems to happen always but ...
  789. {
  790. int meshindx = inner_instance_list["mesh"].AsInteger();
  791. if (meshAssets.Count > meshindx)
  792. pbs = PrimitiveBaseShape.CreateMesh(face_list.Count, meshAssets[meshindx]);
  793. }
  794. if(pbs == null) // fallback
  795. pbs = PrimitiveBaseShape.CreateBox();
  796. Primitive.TextureEntry textureEntry
  797. = new Primitive.TextureEntry(Primitive.TextureEntry.WHITE_TEXTURE);
  798. for (uint face = 0; face < face_list.Count; face++)
  799. {
  800. OSDMap faceMap = (OSDMap)face_list[(int)face];
  801. Primitive.TextureEntryFace f = textureEntry.CreateFace(face); //clone the default
  802. if (faceMap.ContainsKey("fullbright"))
  803. f.Fullbright = faceMap["fullbright"].AsBoolean();
  804. if (faceMap.ContainsKey("diffuse_color"))
  805. f.RGBA = faceMap["diffuse_color"].AsColor4();
  806. int textureNum = faceMap["image"].AsInteger();
  807. float imagerot = faceMap["imagerot"].AsInteger();
  808. float offsets = (float)faceMap["offsets"].AsReal();
  809. float offsett = (float)faceMap["offsett"].AsReal();
  810. float scales = (float)faceMap["scales"].AsReal();
  811. float scalet = (float)faceMap["scalet"].AsReal();
  812. if (imagerot != 0)
  813. f.Rotation = imagerot;
  814. if (offsets != 0)
  815. f.OffsetU = offsets;
  816. if (offsett != 0)
  817. f.OffsetV = offsett;
  818. if (scales != 0)
  819. f.RepeatU = scales;
  820. if (scalet != 0)
  821. f.RepeatV = scalet;
  822. if (textures.Count > textureNum)
  823. f.TextureID = textures[textureNum];
  824. textureEntry.FaceTextures[face] = f;
  825. }
  826. pbs.TextureEntry = textureEntry.GetBytes();
  827. Vector3 position = inner_instance_list["position"].AsVector3();
  828. Quaternion rotation = inner_instance_list["rotation"].AsQuaternion();
  829. // for now viwers do send fixed defaults
  830. // but this may change
  831. // int physicsShapeType = inner_instance_list["physics_shape_type"].AsInteger();
  832. byte physicsShapeType = (byte)PhysShapeType.convex; // default is simple convex
  833. // int material = inner_instance_list["material"].AsInteger();
  834. byte material = (byte)Material.Wood;
  835. SceneObjectPart prim
  836. = new SceneObjectPart(owner_id, pbs, position, Quaternion.Identity, Vector3.Zero);
  837. prim.Scale = scale;
  838. rotations.Add(rotation);
  839. positions.Add(position);
  840. prim.UUID = UUID.Random();
  841. prim.CreatorID = creatorID;
  842. prim.OwnerID = owner_id;
  843. prim.GroupID = UUID.Zero;
  844. prim.LastOwnerID = creatorID;
  845. prim.RezzerID = creatorID;
  846. prim.CreationDate = Util.UnixTimeSinceEpoch();
  847. if (grp == null)
  848. prim.Name = assetName;
  849. else
  850. prim.Name = assetName + "#" + i.ToString();
  851. prim.EveryoneMask = 0;
  852. prim.GroupMask = 0;
  853. if (restrictPerms)
  854. {
  855. prim.BaseMask = (uint)(PermissionMask.Move | PermissionMask.Modify);
  856. prim.OwnerMask = (uint)(PermissionMask.Move | PermissionMask.Modify);
  857. prim.NextOwnerMask = 0;
  858. }
  859. else
  860. {
  861. prim.BaseMask = (uint)PermissionMask.All | (uint)PermissionMask.Export;
  862. prim.OwnerMask = (uint)PermissionMask.All | (uint)PermissionMask.Export;
  863. prim.GroupMask = prim.BaseMask & (uint)groupMask;
  864. prim.EveryoneMask = prim.BaseMask & (uint)everyoneMask;
  865. prim.NextOwnerMask = prim.BaseMask & (uint)nextOwnerMask;
  866. // If the viewer gives us bogus permissions, revert to the SL
  867. // default of transfer only.
  868. if ((prim.NextOwnerMask & (uint)PermissionMask.All) == 0)
  869. prim.NextOwnerMask = (uint)PermissionMask.Transfer;
  870. }
  871. if(istest)
  872. prim.Description = "For testing only. Other uses are prohibited";
  873. else
  874. prim.Description = "";
  875. prim.Material = material;
  876. prim.PhysicsShapeType = physicsShapeType;
  877. // prim.BaseMask = (uint)base_mask;
  878. // prim.EveryoneMask = (uint)everyone_mask;
  879. // prim.GroupMask = (uint)group_mask;
  880. // prim.NextOwnerMask = (uint)next_owner_mask;
  881. // prim.OwnerMask = (uint)owner_mask;
  882. if (grp == null)
  883. {
  884. grp = new SceneObjectGroup(prim);
  885. grp.LastOwnerID = creatorID;
  886. grp.RezzerID = creatorID;
  887. }
  888. else
  889. grp.AddPart(prim);
  890. }
  891. Vector3 rootPos = positions[0];
  892. if (grp.Parts.Length > 1)
  893. {
  894. // Fix first link number
  895. grp.RootPart.LinkNum++;
  896. Quaternion rootRotConj = Quaternion.Conjugate(rotations[0]);
  897. Quaternion tmprot;
  898. Vector3 offset;
  899. // fix children rotations and positions
  900. for (int i = 1; i < rotations.Count; i++)
  901. {
  902. tmprot = rotations[i];
  903. tmprot = rootRotConj * tmprot;
  904. grp.Parts[i].RotationOffset = tmprot;
  905. offset = positions[i] - rootPos;
  906. offset *= rootRotConj;
  907. grp.Parts[i].OffsetPosition = offset;
  908. }
  909. grp.AbsolutePosition = rootPos;
  910. grp.UpdateGroupRotationR(rotations[0]);
  911. }
  912. else
  913. {
  914. grp.AbsolutePosition = rootPos;
  915. grp.UpdateGroupRotationR(rotations[0]);
  916. }
  917. data = ASCIIEncoding.ASCII.GetBytes(SceneObjectSerializer.ToOriginalXmlFormat(grp));
  918. }
  919. else // not a mesh model
  920. {
  921. m_log.ErrorFormat("[CAPS Asset Upload] got unsuported assetType for object upload");
  922. return;
  923. }
  924. }
  925. AssetBase asset;
  926. asset = new AssetBase(assetID, assetName, assType, creatorIDstr);
  927. asset.Data = data;
  928. if (istest)
  929. asset.Local = true;
  930. if (AddNewAsset != null)
  931. AddNewAsset(asset);
  932. else if (m_assetService != null)
  933. m_assetService.Store(asset);
  934. InventoryItemBase item = new InventoryItemBase();
  935. item.Owner = m_HostCapsObj.AgentID;
  936. item.CreatorId = creatorIDstr;
  937. item.CreatorData = String.Empty;
  938. item.ID = inventoryItem;
  939. item.AssetID = asset.FullID;
  940. if (istest)
  941. {
  942. item.Description = "For testing only. Other uses are prohibited";
  943. item.Flags = (uint) (InventoryItemFlags.SharedSingleReference);
  944. }
  945. else
  946. item.Description = assetDescription;
  947. item.Name = assetName;
  948. item.AssetType = assType;
  949. item.InvType = inType;
  950. item.Folder = parentFolder;
  951. // If we set PermissionMask.All then when we rez the item the next permissions will replace the current
  952. // (owner) permissions. This becomes a problem if next permissions are changed.
  953. if (inType == (sbyte)CustomInventoryType.AnimationSet)
  954. {
  955. AnimationSet.setCreateItemPermitions(item);
  956. }
  957. else if (restrictPerms)
  958. {
  959. item.BasePermissions = (uint)(PermissionMask.Move | PermissionMask.Modify);
  960. item.CurrentPermissions = (uint)(PermissionMask.Move | PermissionMask.Modify);
  961. item.GroupPermissions = 0;
  962. item.EveryOnePermissions = 0;
  963. item.NextPermissions = 0;
  964. }
  965. else
  966. {
  967. item.BasePermissions = (uint)PermissionMask.All | (uint)PermissionMask.Export;
  968. item.CurrentPermissions = (uint)PermissionMask.All | (uint)PermissionMask.Export;
  969. item.GroupPermissions = item.BasePermissions & (uint)groupMask;
  970. item.EveryOnePermissions = item.BasePermissions & (uint)everyoneMask;
  971. item.NextPermissions = item.BasePermissions & (uint)nextOwnerMask;
  972. if ((item.NextPermissions & (uint)PermissionMask.All) == 0)
  973. item.NextPermissions = (uint)PermissionMask.Transfer;
  974. }
  975. item.CreationDate = Util.UnixTimeSinceEpoch();
  976. everyoneMask = (int)item.EveryOnePermissions;
  977. groupMask = (int)item.GroupPermissions;
  978. nextOwnerMask = (int)item.NextPermissions;
  979. m_Scene.TryGetClient(m_HostCapsObj.AgentID, out client);
  980. if (AddNewInventoryItem != null)
  981. {
  982. if (istest)
  983. {
  984. m_Scene.AddInventoryItem(client, item);
  985. /*
  986. AddNewInventoryItem(m_HostCapsObj.AgentID, item, 0);
  987. if (client != null)
  988. client.SendAgentAlertMessage("Upload will have no cost, for personal test purposes only. Other uses are forbiden. Items may not work on a another region" , true);
  989. */
  990. }
  991. else
  992. {
  993. AddNewInventoryItem(m_HostCapsObj.AgentID, item, (uint)cost);
  994. // if (client != null)
  995. // {
  996. // // let users see anything.. i don't so far
  997. // string str;
  998. // if (cost > 0)
  999. // // dont remember where is money unit name to put here
  1000. // str = "Upload complete. charged " + cost.ToString() + "$";
  1001. // else
  1002. // str = "Upload complete";
  1003. // client.SendAgentAlertMessage(str, true);
  1004. // }
  1005. }
  1006. }
  1007. lock (m_ModelCost)
  1008. m_FileAgentInventoryState = FileAgentInventoryState.idle;
  1009. }
  1010. /// <summary>
  1011. ///
  1012. /// </summary>
  1013. /// <param name="mapReq"></param>
  1014. /// <returns></returns>
  1015. public LLSDMapLayerResponse GetMapLayer(LLSDMapRequest mapReq)
  1016. {
  1017. m_log.Debug("[CAPS]: MapLayer Request in region: " + m_regionName);
  1018. LLSDMapLayerResponse mapResponse = new LLSDMapLayerResponse();
  1019. mapResponse.LayerData.Array.Add(GetOSDMapLayerResponse());
  1020. return mapResponse;
  1021. }
  1022. /// <summary>
  1023. ///
  1024. /// </summary>
  1025. /// <returns></returns>
  1026. protected static OSDMapLayer GetOSDMapLayerResponse()
  1027. {
  1028. OSDMapLayer mapLayer = new OSDMapLayer();
  1029. mapLayer.Right = 5000;
  1030. mapLayer.Top = 5000;
  1031. mapLayer.ImageID = new UUID("00000000-0000-1111-9999-000000000006");
  1032. return mapLayer;
  1033. }
  1034. /// <summary>
  1035. ///
  1036. /// </summary>
  1037. /// <param name="request"></param>
  1038. /// <param name="path"></param>
  1039. /// <param name="param"></param>
  1040. /// <returns></returns>
  1041. public string RequestTexture(string request, string path, string param)
  1042. {
  1043. m_log.Debug("texture request " + request);
  1044. // Needs implementing (added to remove compiler warning)
  1045. return String.Empty;
  1046. }
  1047. /// <summary>
  1048. /// Called by the notecard update handler. Provides a URL to which the client can upload a new asset.
  1049. /// </summary>
  1050. /// <param name="request"></param>
  1051. /// <param name="path"></param>
  1052. /// <param name="param"></param>
  1053. /// <returns></returns>
  1054. public string NoteCardAgentInventory(string request, string path, string param,
  1055. IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  1056. {
  1057. //m_log.Debug("[CAPS]: NoteCardAgentInventory Request in region: " + m_regionName + "\n" + request);
  1058. //m_log.Debug("[CAPS]: NoteCardAgentInventory Request is: " + request);
  1059. //OpenMetaverse.StructuredData.OSDMap hash = (OpenMetaverse.StructuredData.OSDMap)OpenMetaverse.StructuredData.LLSDParser.DeserializeBinary(Utils.StringToBytes(request));
  1060. Hashtable hash = (Hashtable)LLSD.LLSDDeserialize(Utils.StringToBytes(request));
  1061. LLSDItemUpdate llsdRequest = new LLSDItemUpdate();
  1062. LLSDHelpers.DeserialiseOSDMap(hash, llsdRequest);
  1063. string uploaderPath = GetNewCapPath();
  1064. ItemUpdater uploader =
  1065. new ItemUpdater(llsdRequest.item_id, uploaderPath, m_HostCapsObj.HttpListener, m_dumpAssetsToFile);
  1066. uploader.OnUpLoad += ItemUpdated;
  1067. m_HostCapsObj.HttpListener.AddStreamHandler(
  1068. new BinaryStreamHandler(
  1069. "POST", uploaderPath, uploader.uploaderCaps, "NoteCardAgentInventory", null));
  1070. string protocol = "http://";
  1071. if (m_HostCapsObj.SSLCaps)
  1072. protocol = "https://";
  1073. string uploaderURL = protocol + m_HostCapsObj.HostName + ":" + m_HostCapsObj.Port.ToString() + uploaderPath;
  1074. LLSDAssetUploadResponse uploadResponse = new LLSDAssetUploadResponse();
  1075. uploadResponse.uploader = uploaderURL;
  1076. uploadResponse.state = "upload";
  1077. // m_log.InfoFormat("[CAPS]: " +
  1078. // "NoteCardAgentInventory response: {0}",
  1079. // LLSDHelpers.SerialiseLLSDReply(uploadResponse)));
  1080. return LLSDHelpers.SerialiseLLSDReply(uploadResponse);
  1081. }
  1082. /// <summary>
  1083. /// Called by the CopyInventoryFromNotecard caps handler.
  1084. /// </summary>
  1085. /// <param name="request"></param>
  1086. /// <param name="path"></param>
  1087. /// <param name="param"></param>
  1088. public string CopyInventoryFromNotecard(string request, string path, string param,
  1089. IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  1090. {
  1091. Hashtable response = new Hashtable();
  1092. response["int_response_code"] = 404;
  1093. response["content_type"] = "text/plain";
  1094. response["keepalive"] = false;
  1095. response["str_response_string"] = "";
  1096. try
  1097. {
  1098. OSDMap content = (OSDMap)OSDParser.DeserializeLLSDXml(request);
  1099. UUID objectID = content["object-id"].AsUUID();
  1100. UUID notecardID = content["notecard-id"].AsUUID();
  1101. UUID folderID = content["folder-id"].AsUUID();
  1102. UUID itemID = content["item-id"].AsUUID();
  1103. // m_log.InfoFormat("[CAPS]: CopyInventoryFromNotecard, FolderID:{0}, ItemID:{1}, NotecardID:{2}, ObjectID:{3}", folderID, itemID, notecardID, objectID);
  1104. if (objectID != UUID.Zero)
  1105. {
  1106. SceneObjectPart part = m_Scene.GetSceneObjectPart(objectID);
  1107. if (part != null)
  1108. {
  1109. // TaskInventoryItem taskItem = part.Inventory.GetInventoryItem(notecardID);
  1110. if (!m_Scene.Permissions.CanCopyObjectInventory(notecardID, objectID, m_HostCapsObj.AgentID))
  1111. {
  1112. return LLSDHelpers.SerialiseLLSDReply(response);
  1113. }
  1114. }
  1115. }
  1116. InventoryItemBase item = null;
  1117. InventoryItemBase copyItem = null;
  1118. IClientAPI client = null;
  1119. m_Scene.TryGetClient(m_HostCapsObj.AgentID, out client);
  1120. item = m_Scene.InventoryService.GetItem(m_HostCapsObj.AgentID, itemID);
  1121. if (item != null)
  1122. {
  1123. string message;
  1124. copyItem = m_Scene.GiveInventoryItem(m_HostCapsObj.AgentID, item.Owner, itemID, folderID, out message);
  1125. if (copyItem != null && client != null)
  1126. {
  1127. m_log.InfoFormat("[CAPS]: CopyInventoryFromNotecard, ItemID:{0}, FolderID:{1}", copyItem.ID, copyItem.Folder);
  1128. client.SendBulkUpdateInventory(copyItem);
  1129. }
  1130. }
  1131. else
  1132. {
  1133. m_log.ErrorFormat("[CAPS]: CopyInventoryFromNotecard - Failed to retrieve item {0} from notecard {1}", itemID, notecardID);
  1134. if (client != null)
  1135. client.SendAlertMessage("Failed to retrieve item");
  1136. }
  1137. }
  1138. catch (Exception e)
  1139. {
  1140. m_log.ErrorFormat("[CAPS]: CopyInventoryFromNotecard : {0}", e.ToString());
  1141. }
  1142. response["int_response_code"] = 200;
  1143. return LLSDHelpers.SerialiseLLSDReply(response);
  1144. }
  1145. public string GetObjectPhysicsData(string request, string path,
  1146. string param, IOSHttpRequest httpRequest,
  1147. IOSHttpResponse httpResponse)
  1148. {
  1149. OSDMap req = (OSDMap)OSDParser.DeserializeLLSDXml(request);
  1150. OSDMap resp = new OSDMap();
  1151. OSDArray object_ids = (OSDArray)req["object_ids"];
  1152. for (int i = 0 ; i < object_ids.Count ; i++)
  1153. {
  1154. UUID uuid = object_ids[i].AsUUID();
  1155. SceneObjectPart obj = m_Scene.GetSceneObjectPart(uuid);
  1156. if (obj != null)
  1157. {
  1158. OSDMap object_data = new OSDMap();
  1159. object_data["PhysicsShapeType"] = obj.PhysicsShapeType;
  1160. object_data["Density"] = obj.Density;
  1161. object_data["Friction"] = obj.Friction;
  1162. object_data["Restitution"] = obj.Restitution;
  1163. object_data["GravityMultiplier"] = obj.GravityModifier;
  1164. resp[uuid.ToString()] = object_data;
  1165. }
  1166. }
  1167. string response = OSDParser.SerializeLLSDXmlString(resp);
  1168. return response;
  1169. }
  1170. public string GetObjectCost(string request, string path,
  1171. string param, IOSHttpRequest httpRequest,
  1172. IOSHttpResponse httpResponse)
  1173. {
  1174. OSDMap req = (OSDMap)OSDParser.DeserializeLLSDXml(request);
  1175. OSDMap resp = new OSDMap();
  1176. OSDArray object_ids = (OSDArray)req["object_ids"];
  1177. for (int i = 0; i < object_ids.Count; i++)
  1178. {
  1179. UUID uuid = object_ids[i].AsUUID();
  1180. SceneObjectPart part = m_Scene.GetSceneObjectPart(uuid);
  1181. SceneObjectGroup grp = null;
  1182. if (part != null)
  1183. grp = part.ParentGroup;
  1184. if (grp != null)
  1185. {
  1186. float linksetCost;
  1187. float linksetPhysCost;
  1188. float partCost;
  1189. float partPhysCost;
  1190. grp.GetResourcesCosts(part,out linksetCost,out linksetPhysCost,out partCost,out partPhysCost);
  1191. OSDMap object_data = new OSDMap();
  1192. object_data["linked_set_resource_cost"] = linksetCost;
  1193. object_data["resource_cost"] = partCost;
  1194. object_data["physics_cost"] = partPhysCost;
  1195. object_data["linked_set_physics_cost"] = linksetPhysCost;
  1196. object_data["resource_limiting_type"] = "legacy";
  1197. resp[uuid.ToString()] = object_data;
  1198. }
  1199. }
  1200. if(resp.Count == 0)
  1201. {
  1202. OSDMap object_data = new OSDMap();
  1203. object_data["linked_set_resource_cost"] = 0;
  1204. object_data["resource_cost"] = 0;
  1205. object_data["physics_cost"] = 0;
  1206. object_data["linked_set_physics_cost"] = 0;
  1207. resp[UUID.Zero.ToString()] = object_data;
  1208. }
  1209. string response = OSDParser.SerializeLLSDXmlString(resp);
  1210. return response;
  1211. }
  1212. public string ResourceCostSelected(string request, string path,
  1213. string param, IOSHttpRequest httpRequest,
  1214. IOSHttpResponse httpResponse)
  1215. {
  1216. OSDMap req = (OSDMap)OSDParser.DeserializeLLSDXml(request);
  1217. OSDMap resp = new OSDMap();
  1218. float phys=0;
  1219. float stream=0;
  1220. float simul=0;
  1221. if (req.ContainsKey("selected_roots"))
  1222. {
  1223. OSDArray object_ids = (OSDArray)req["selected_roots"];
  1224. // should go by SOG suming costs for all parts
  1225. // ll v3 works ok with several objects select we get the list and adds ok
  1226. // FS calls per object so results are wrong guess fs bug
  1227. for (int i = 0; i < object_ids.Count; i++)
  1228. {
  1229. UUID uuid = object_ids[i].AsUUID();
  1230. float Physc;
  1231. float simulc;
  1232. float streamc;
  1233. SceneObjectGroup grp = m_Scene.GetGroupByPrim(uuid);
  1234. if (grp != null)
  1235. {
  1236. grp.GetSelectedCosts(out Physc, out streamc, out simulc);
  1237. phys += Physc;
  1238. stream += streamc;
  1239. simul += simulc;
  1240. }
  1241. }
  1242. }
  1243. else if (req.ContainsKey("selected_prims"))
  1244. {
  1245. OSDArray object_ids = (OSDArray)req["selected_prims"];
  1246. // don't see in use in any of the 2 viewers
  1247. // guess it should be for edit linked but... nothing
  1248. // should go to SOP per part
  1249. for (int i = 0; i < object_ids.Count; i++)
  1250. {
  1251. UUID uuid = object_ids[i].AsUUID();
  1252. SceneObjectPart part = m_Scene.GetSceneObjectPart(uuid);
  1253. if (part != null)
  1254. {
  1255. phys += part.PhysicsCost;
  1256. stream += part.StreamingCost;
  1257. simul += part.SimulationCost;
  1258. }
  1259. }
  1260. }
  1261. OSDMap object_data = new OSDMap();
  1262. object_data["physics"] = phys;
  1263. object_data["streaming"] = stream;
  1264. object_data["simulation"] = simul;
  1265. resp["selected"] = object_data;
  1266. // resp["transaction_id"] = "undef";
  1267. string response = OSDParser.SerializeLLSDXmlString(resp);
  1268. return response;
  1269. }
  1270. public string UpdateAgentInformation(string request, string path,
  1271. string param, IOSHttpRequest httpRequest,
  1272. IOSHttpResponse httpResponse)
  1273. {
  1274. // OSDMap req = (OSDMap)OSDParser.DeserializeLLSDXml(request);
  1275. OSDMap resp = new OSDMap();
  1276. OSDMap accessPrefs = new OSDMap();
  1277. accessPrefs["max"] = "A";
  1278. resp["access_prefs"] = accessPrefs;
  1279. string response = OSDParser.SerializeLLSDXmlString(resp);
  1280. return response;
  1281. }
  1282. public bool OSDMapTOVector3(OSDMap map, out Vector3 v)
  1283. {
  1284. v = Vector3.Zero;
  1285. if(!map.ContainsKey("X"))
  1286. return false;
  1287. if(!map.ContainsKey("Y"))
  1288. return false;
  1289. if(!map.ContainsKey("Z"))
  1290. return false;
  1291. v.X = (float)map["X"].AsReal();
  1292. v.Y = (float)map["Y"].AsReal();
  1293. v.Z = (float)map["Z"].AsReal();
  1294. return true;
  1295. }
  1296. public string HomeLocation(string request, string path, string param, IOSHttpRequest httpRequest,
  1297. IOSHttpResponse httpResponse)
  1298. {
  1299. OSDMap resp = new OSDMap();
  1300. resp["success"] = "false";
  1301. bool fail = true;
  1302. string message = "Set Home request failed";
  1303. int locationID = 1;
  1304. Vector3 pos = Vector3.Zero;
  1305. Vector3 lookAt = Vector3.Zero;
  1306. IClientAPI client = null;
  1307. ScenePresence sp;
  1308. while(true)
  1309. {
  1310. if(m_Scene.GridUserService == null)
  1311. break;
  1312. if(m_Scene.UserManagementModule == null)
  1313. break;
  1314. m_Scene.TryGetScenePresence(m_AgentID, out sp);
  1315. if(sp == null || sp.IsChildAgent || sp.IsDeleted)
  1316. break;
  1317. if(sp.IsInTransit && !sp.IsInLocalTransit)
  1318. break;
  1319. client = sp.ControllingClient;
  1320. if(!m_Scene.UserManagementModule.IsLocalGridUser(m_AgentID))
  1321. break;
  1322. OSDMap req = (OSDMap)OSDParser.DeserializeLLSDXml(request);
  1323. if(!req.ContainsKey("HomeLocation"))
  1324. break;
  1325. OSDMap HLocation = (OSDMap)req["HomeLocation"];
  1326. if(!HLocation.ContainsKey("LocationPos"))
  1327. break;
  1328. if(!HLocation.ContainsKey("LocationLookAt"))
  1329. break;
  1330. locationID = HLocation["LocationId"].AsInteger();
  1331. if(!OSDMapTOVector3((OSDMap)HLocation["LocationPos"], out pos))
  1332. break;
  1333. if(!OSDMapTOVector3((OSDMap)HLocation["LocationLookAt"], out lookAt))
  1334. break;
  1335. ILandObject land = m_Scene.LandChannel.GetLandObject(pos);
  1336. if(land == null)
  1337. break;
  1338. ulong gpowers = client.GetGroupPowers(land.LandData.GroupID);
  1339. SceneObjectGroup telehub = null;
  1340. if (m_Scene.RegionInfo.RegionSettings.TelehubObject != UUID.Zero)
  1341. // Does the telehub exist in the scene?
  1342. telehub = m_Scene.GetSceneObjectGroup(m_Scene.RegionInfo.RegionSettings.TelehubObject);
  1343. if (!m_Scene.Permissions.IsAdministrator(m_AgentID) && // (a) gods and land managers can set home
  1344. !m_Scene.Permissions.IsGod(m_AgentID) &&
  1345. m_AgentID != land.LandData.OwnerID && // (b) land owners can set home
  1346. // (c) members of the land-associated group in roles that can set home
  1347. ((gpowers & (ulong)GroupPowers.AllowSetHome) != (ulong)GroupPowers.AllowSetHome) &&
  1348. // (d) parcels with telehubs can be the home of anyone
  1349. (telehub == null || !land.ContainsPoint((int)telehub.AbsolutePosition.X, (int)telehub.AbsolutePosition.Y)))
  1350. {
  1351. message = "You are not allowed to set your home location in this parcel.";
  1352. break;
  1353. }
  1354. string userId;
  1355. UUID test;
  1356. if (!m_Scene.UserManagementModule.GetUserUUI(m_AgentID, out userId))
  1357. {
  1358. message = "Set Home request failed. (User Lookup)";
  1359. break;
  1360. }
  1361. if (!UUID.TryParse(userId, out test))
  1362. {
  1363. message = "Set Home request failed. (HG visitor)";
  1364. break;
  1365. }
  1366. if (m_Scene.GridUserService.SetHome(userId, land.RegionUUID, pos, lookAt))
  1367. fail = false;
  1368. break;
  1369. }
  1370. string response;
  1371. if(fail)
  1372. {
  1373. if(client != null)
  1374. client.SendAlertMessage(message);
  1375. response = OSDParser.SerializeLLSDXmlString(resp);
  1376. return response;
  1377. }
  1378. // so its http but still needs a udp reply to inform user? crap :p
  1379. if(client != null)
  1380. client.SendAlertMessage("Home position set.","HomePositionSet");
  1381. resp["success"] = "true";
  1382. OSDMap homeloc = new OSDMap();
  1383. OSDMap homelocpos = new OSDMap();
  1384. // for some odd reason viewers send pos as reals but read as integer
  1385. homelocpos["X"] = new OSDReal(pos.X);
  1386. homelocpos["Y"] = new OSDReal(pos.Y);
  1387. homelocpos["Z"] = new OSDReal(pos.Z);
  1388. homeloc["LocationPos"] = homelocpos;
  1389. resp["HomeLocation"] = homeloc;
  1390. response = OSDParser.SerializeLLSDXmlString(resp);
  1391. return response;
  1392. }
  1393. private static int CompareRolesByMembersDesc(GroupRolesData x, GroupRolesData y)
  1394. {
  1395. return -(x.Members.CompareTo(y.Members));
  1396. }
  1397. public string GroupMemberData(string request, string path, string param, IOSHttpRequest httpRequest,
  1398. IOSHttpResponse httpResponse)
  1399. {
  1400. OSDMap resp = new OSDMap();
  1401. string response;
  1402. bool fail = true;
  1403. IClientAPI client = null;
  1404. ScenePresence sp;
  1405. IGroupsModule m_GroupsModule;
  1406. UUID groupID = UUID.Zero;
  1407. while(true)
  1408. {
  1409. m_GroupsModule = m_Scene.RequestModuleInterface<IGroupsModule>();
  1410. if(m_GroupsModule == null)
  1411. break;
  1412. m_Scene.TryGetScenePresence(m_AgentID, out sp);
  1413. if(sp == null || sp.IsChildAgent || sp.IsDeleted)
  1414. break;
  1415. if(sp.IsInTransit && !sp.IsInLocalTransit)
  1416. break;
  1417. client = sp.ControllingClient;
  1418. OSDMap req = (OSDMap)OSDParser.DeserializeLLSDXml(request);
  1419. if(!req.ContainsKey("group_id"))
  1420. break;
  1421. groupID = req["group_id"].AsUUID();
  1422. if(groupID == UUID.Zero)
  1423. break;
  1424. List<GroupRolesData> roles = m_GroupsModule.GroupRoleDataRequest(client, groupID);
  1425. if(roles == null || roles.Count == 0)
  1426. break;
  1427. List<GroupMembersData> members = m_GroupsModule.GroupMembersRequest(client, groupID);
  1428. if(members == null || members.Count == 0)
  1429. break;
  1430. int memberCount = members.Count;
  1431. Dictionary<string,int> titles = new Dictionary<string,int>();
  1432. int i = 0;
  1433. ulong defaultPowers = 0;
  1434. // build titles array and index
  1435. roles.Sort(CompareRolesByMembersDesc);
  1436. OSDArray osdtitles = new OSDArray();
  1437. foreach(GroupRolesData grd in roles)
  1438. {
  1439. if(grd.Title == null)
  1440. continue;
  1441. string title = grd.Title;
  1442. if(i==0)
  1443. defaultPowers = grd.Powers;
  1444. if(!titles.ContainsKey(title))
  1445. {
  1446. titles[title] = i++;
  1447. osdtitles.Add(new OSDString(title));
  1448. }
  1449. }
  1450. if(titles.Count == 0)
  1451. break;
  1452. OSDMap osdmembers = new OSDMap();
  1453. foreach(GroupMembersData gmd in members)
  1454. {
  1455. OSDMap m = new OSDMap();
  1456. if(gmd.OnlineStatus != null && gmd.OnlineStatus != "")
  1457. m["last_login"] = new OSDString(gmd.OnlineStatus);
  1458. if(gmd.AgentPowers != defaultPowers)
  1459. m["powers"] = new OSDString((gmd.AgentPowers).ToString("X"));
  1460. if(gmd.Title != null && titles.ContainsKey(gmd.Title) && titles[gmd.Title] != 0)
  1461. m["title"] = new OSDInteger(titles[gmd.Title]);
  1462. if(gmd.IsOwner)
  1463. m["owner"] = new OSDString("true");
  1464. if(gmd.Contribution != 0)
  1465. m["donated_square_meters"] = new OSDInteger(gmd.Contribution);
  1466. osdmembers[(gmd.AgentID).ToString()] = m;
  1467. }
  1468. OSDMap osddefaults = new OSDMap();
  1469. osddefaults["default_powers"] = new OSDString(defaultPowers.ToString("X"));
  1470. resp["group_id"] = new OSDUUID(groupID);
  1471. resp["agent_id"] = new OSDUUID(m_AgentID);
  1472. resp["member_count"] = new OSDInteger(memberCount);
  1473. resp["defaults"] = osddefaults;
  1474. resp["titles"] = osdtitles;
  1475. resp["members"] = osdmembers;
  1476. fail = false;
  1477. break;
  1478. }
  1479. if(fail)
  1480. {
  1481. resp["group_id"] = new OSDUUID(groupID);
  1482. resp["agent_id"] = new OSDUUID(m_AgentID);
  1483. resp["member_count"] = new OSDInteger(0);
  1484. resp["defaults"] = new OSDMap();
  1485. resp["titles"] = new OSDArray();
  1486. resp["members"] = new OSDMap();
  1487. }
  1488. response = OSDParser.SerializeLLSDXmlString(resp);
  1489. return response;
  1490. }
  1491. public string GetDisplayNames(string request, string path,
  1492. string param, IOSHttpRequest httpRequest,
  1493. IOSHttpResponse httpResponse)
  1494. {
  1495. httpResponse.StatusCode = (int)System.Net.HttpStatusCode.Gone;
  1496. httpResponse.ContentType = "text/plain";
  1497. ScenePresence sp = m_Scene.GetScenePresence(m_AgentID);
  1498. if(sp == null || sp.IsDeleted)
  1499. return "";
  1500. if(sp.IsInTransit && !sp.IsInLocalTransit)
  1501. {
  1502. httpResponse.StatusCode = (int)System.Net.HttpStatusCode.ServiceUnavailable;
  1503. httpResponse.AddHeader("Retry-After","30");
  1504. return "";
  1505. }
  1506. NameValueCollection query = HttpUtility.ParseQueryString(httpRequest.Url.Query);
  1507. string[] ids = query.GetValues("ids");
  1508. Dictionary<UUID,string> names = m_UserManager.GetUsersNames(ids);
  1509. OSDMap osdReply = new OSDMap();
  1510. OSDArray agents = new OSDArray();
  1511. osdReply["agents"] = agents;
  1512. foreach (KeyValuePair<UUID,string> kvp in names)
  1513. {
  1514. if (string.IsNullOrEmpty(kvp.Value))
  1515. continue;
  1516. if(kvp.Key == UUID.Zero)
  1517. continue;
  1518. string[] parts = kvp.Value.Split(new char[] {' '});
  1519. OSDMap osdname = new OSDMap();
  1520. // dont tell about unknown users, we can't send them back on Bad either
  1521. if(parts[0] == "Unknown")
  1522. continue;
  1523. /*
  1524. if(parts[0] == "Unknown")
  1525. {
  1526. osdname["display_name_next_update"] = OSD.FromDate(DateTime.UtcNow.AddHours(1));
  1527. osdname["display_name_expires"] = OSD.FromDate(DateTime.UtcNow.AddHours(2));
  1528. }
  1529. else
  1530. */
  1531. {
  1532. osdname["display_name_next_update"] = OSD.FromDate(DateTime.UtcNow.AddDays(8));
  1533. osdname["display_name_expires"] = OSD.FromDate(DateTime.UtcNow.AddMonths(1));
  1534. }
  1535. osdname["display_name"] = OSD.FromString(kvp.Value);
  1536. osdname["legacy_first_name"] = parts[0];
  1537. osdname["legacy_last_name"] = parts[1];
  1538. osdname["username"] = OSD.FromString(kvp.Value);
  1539. osdname["id"] = OSD.FromUUID(kvp.Key);
  1540. osdname["is_display_name_default"] = OSD.FromBoolean(true);
  1541. agents.Add(osdname);
  1542. }
  1543. // Full content request
  1544. httpResponse.StatusCode = (int)System.Net.HttpStatusCode.OK;
  1545. //httpResponse.ContentLength = ??;
  1546. httpResponse.ContentType = "application/llsd+xml";
  1547. string reply = OSDParser.SerializeLLSDXmlString(osdReply);
  1548. return reply;
  1549. }
  1550. }
  1551. public class AssetUploader
  1552. {
  1553. private static readonly ILog m_log =
  1554. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  1555. public event UpLoadedAsset OnUpLoad;
  1556. private UpLoadedAsset handlerUpLoad = null;
  1557. private string uploaderPath = String.Empty;
  1558. private UUID newAssetID;
  1559. private UUID inventoryItemID;
  1560. private UUID parentFolder;
  1561. private IHttpServer httpListener;
  1562. private bool m_dumpAssetsToFile;
  1563. private string m_assetName = String.Empty;
  1564. private string m_assetDes = String.Empty;
  1565. private string m_invType = String.Empty;
  1566. private string m_assetType = String.Empty;
  1567. private int m_cost;
  1568. private string m_error = String.Empty;
  1569. private Timer m_timeoutTimer = new Timer();
  1570. private UUID m_texturesFolder;
  1571. private int m_nreqtextures;
  1572. private int m_nreqmeshs;
  1573. private int m_nreqinstances;
  1574. private bool m_IsAtestUpload;
  1575. private int m_nextOwnerMask;
  1576. private int m_groupMask;
  1577. private int m_everyoneMask;
  1578. public AssetUploader(string assetName, string description, UUID assetID, UUID inventoryItem,
  1579. UUID parentFolderID, string invType, string assetType, string path,
  1580. IHttpServer httpServer, bool dumpAssetsToFile,
  1581. int totalCost, UUID texturesFolder, int nreqtextures, int nreqmeshs, int nreqinstances,
  1582. bool IsAtestUpload, int nextOwnerMask, int groupMask, int everyoneMask)
  1583. {
  1584. m_assetName = assetName;
  1585. m_assetDes = description;
  1586. newAssetID = assetID;
  1587. inventoryItemID = inventoryItem;
  1588. uploaderPath = path;
  1589. httpListener = httpServer;
  1590. parentFolder = parentFolderID;
  1591. m_assetType = assetType;
  1592. m_invType = invType;
  1593. m_dumpAssetsToFile = dumpAssetsToFile;
  1594. m_cost = totalCost;
  1595. m_texturesFolder = texturesFolder;
  1596. m_nreqtextures = nreqtextures;
  1597. m_nreqmeshs = nreqmeshs;
  1598. m_nreqinstances = nreqinstances;
  1599. m_IsAtestUpload = IsAtestUpload;
  1600. m_timeoutTimer.Elapsed += TimedOut;
  1601. m_timeoutTimer.Interval = 120000;
  1602. m_timeoutTimer.AutoReset = false;
  1603. m_timeoutTimer.Start();
  1604. m_nextOwnerMask = nextOwnerMask;
  1605. m_groupMask = groupMask;
  1606. m_everyoneMask = everyoneMask;
  1607. }
  1608. /// <summary>
  1609. /// Handle raw asset upload data via the capability.
  1610. /// </summary>
  1611. /// <param name="data"></param>
  1612. /// <param name="path"></param>
  1613. /// <param name="param"></param>
  1614. /// <returns></returns>
  1615. public string uploaderCaps(byte[] data, string path, string param)
  1616. {
  1617. UUID inv = inventoryItemID;
  1618. string res = String.Empty;
  1619. LLSDAssetUploadComplete uploadComplete = new LLSDAssetUploadComplete();
  1620. /*
  1621. uploadComplete.new_asset = newAssetID.ToString();
  1622. uploadComplete.new_inventory_item = inv;
  1623. uploadComplete.state = "complete";
  1624. res = LLSDHelpers.SerialiseLLSDReply(uploadComplete);
  1625. */
  1626. m_timeoutTimer.Stop();
  1627. httpListener.RemoveStreamHandler("POST", uploaderPath);
  1628. // TODO: probably make this a better set of extensions here
  1629. string extension = ".jp2";
  1630. if (m_invType != "image")
  1631. {
  1632. extension = ".dat";
  1633. }
  1634. if (m_dumpAssetsToFile)
  1635. {
  1636. SaveAssetToFile(m_assetName + extension, data);
  1637. }
  1638. handlerUpLoad = OnUpLoad;
  1639. if (handlerUpLoad != null)
  1640. {
  1641. handlerUpLoad(m_assetName, m_assetDes, newAssetID, inv, parentFolder, data, m_invType, m_assetType,
  1642. m_cost, m_texturesFolder, m_nreqtextures, m_nreqmeshs, m_nreqinstances, m_IsAtestUpload,
  1643. ref m_error, ref m_nextOwnerMask, ref m_groupMask, ref m_everyoneMask);
  1644. }
  1645. uploadComplete.new_next_owner_mask = m_nextOwnerMask;
  1646. uploadComplete.new_group_mask = m_groupMask;
  1647. uploadComplete.new_everyone_mask = m_everyoneMask;
  1648. if (m_IsAtestUpload)
  1649. {
  1650. LLSDAssetUploadError resperror = new LLSDAssetUploadError();
  1651. resperror.message = "Upload SUCESSEFULL for testing purposes only. Other uses are prohibited. Item will not work after 48 hours or on other regions";
  1652. resperror.identifier = inv;
  1653. uploadComplete.error = resperror;
  1654. uploadComplete.state = "Upload4Testing";
  1655. }
  1656. else
  1657. {
  1658. if (m_error == String.Empty)
  1659. {
  1660. uploadComplete.new_asset = newAssetID.ToString();
  1661. uploadComplete.new_inventory_item = inv;
  1662. // if (m_texturesFolder != UUID.Zero)
  1663. // uploadComplete.new_texture_folder_id = m_texturesFolder;
  1664. uploadComplete.state = "complete";
  1665. }
  1666. else
  1667. {
  1668. LLSDAssetUploadError resperror = new LLSDAssetUploadError();
  1669. resperror.message = m_error;
  1670. resperror.identifier = inv;
  1671. uploadComplete.error = resperror;
  1672. uploadComplete.state = "failed";
  1673. }
  1674. }
  1675. res = LLSDHelpers.SerialiseLLSDReply(uploadComplete);
  1676. return res;
  1677. }
  1678. private void TimedOut(object sender, ElapsedEventArgs args)
  1679. {
  1680. m_log.InfoFormat("[CAPS]: Removing URL and handler for timed out mesh upload");
  1681. httpListener.RemoveStreamHandler("POST", uploaderPath);
  1682. }
  1683. ///Left this in and commented in case there are unforseen issues
  1684. //private void SaveAssetToFile(string filename, byte[] data)
  1685. //{
  1686. // FileStream fs = File.Create(filename);
  1687. // BinaryWriter bw = new BinaryWriter(fs);
  1688. // bw.Write(data);
  1689. // bw.Close();
  1690. // fs.Close();
  1691. //}
  1692. private static void SaveAssetToFile(string filename, byte[] data)
  1693. {
  1694. string assetPath = "UserAssets";
  1695. if (!Directory.Exists(assetPath))
  1696. {
  1697. Directory.CreateDirectory(assetPath);
  1698. }
  1699. FileStream fs = File.Create(Path.Combine(assetPath, Util.safeFileName(filename)));
  1700. BinaryWriter bw = new BinaryWriter(fs);
  1701. bw.Write(data);
  1702. bw.Close();
  1703. fs.Close();
  1704. }
  1705. }
  1706. /// <summary>
  1707. /// This class is a callback invoked when a client sends asset data to
  1708. /// an agent inventory notecard update url
  1709. /// </summary>
  1710. public class ItemUpdater
  1711. {
  1712. public event UpdateItem OnUpLoad;
  1713. private UpdateItem handlerUpdateItem = null;
  1714. private string uploaderPath = String.Empty;
  1715. private UUID inventoryItemID;
  1716. private IHttpServer httpListener;
  1717. private bool m_dumpAssetToFile;
  1718. public ItemUpdater(UUID inventoryItem, string path, IHttpServer httpServer, bool dumpAssetToFile)
  1719. {
  1720. m_dumpAssetToFile = dumpAssetToFile;
  1721. inventoryItemID = inventoryItem;
  1722. uploaderPath = path;
  1723. httpListener = httpServer;
  1724. }
  1725. /// <summary>
  1726. /// Handle raw uploaded asset data.
  1727. /// </summary>
  1728. /// <param name="data"></param>
  1729. /// <param name="path"></param>
  1730. /// <param name="param"></param>
  1731. /// <returns></returns>
  1732. public string uploaderCaps(byte[] data, string path, string param)
  1733. {
  1734. UUID inv = inventoryItemID;
  1735. string res = String.Empty;
  1736. LLSDAssetUploadComplete uploadComplete = new LLSDAssetUploadComplete();
  1737. UUID assetID = UUID.Zero;
  1738. handlerUpdateItem = OnUpLoad;
  1739. if (handlerUpdateItem != null)
  1740. {
  1741. assetID = handlerUpdateItem(inv, data);
  1742. }
  1743. uploadComplete.new_asset = assetID.ToString();
  1744. uploadComplete.new_inventory_item = inv;
  1745. uploadComplete.state = "complete";
  1746. res = LLSDHelpers.SerialiseLLSDReply(uploadComplete);
  1747. httpListener.RemoveStreamHandler("POST", uploaderPath);
  1748. if (m_dumpAssetToFile)
  1749. {
  1750. SaveAssetToFile("updateditem" + Util.RandomClass.Next(1, 1000) + ".dat", data);
  1751. }
  1752. return res;
  1753. }
  1754. ///Left this in and commented in case there are unforseen issues
  1755. //private void SaveAssetToFile(string filename, byte[] data)
  1756. //{
  1757. // FileStream fs = File.Create(filename);
  1758. // BinaryWriter bw = new BinaryWriter(fs);
  1759. // bw.Write(data);
  1760. // bw.Close();
  1761. // fs.Close();
  1762. //}
  1763. private static void SaveAssetToFile(string filename, byte[] data)
  1764. {
  1765. string assetPath = "UserAssets";
  1766. if (!Directory.Exists(assetPath))
  1767. {
  1768. Directory.CreateDirectory(assetPath);
  1769. }
  1770. FileStream fs = File.Create(Path.Combine(assetPath, filename));
  1771. BinaryWriter bw = new BinaryWriter(fs);
  1772. bw.Write(data);
  1773. bw.Close();
  1774. fs.Close();
  1775. }
  1776. }
  1777. /// <summary>
  1778. /// This class is a callback invoked when a client sends asset data to
  1779. /// a task inventory script update url
  1780. /// </summary>
  1781. public class TaskInventoryScriptUpdater
  1782. {
  1783. private static readonly ILog m_log =
  1784. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  1785. public event UpdateTaskScript OnUpLoad;
  1786. private UpdateTaskScript handlerUpdateTaskScript = null;
  1787. private string uploaderPath = String.Empty;
  1788. private UUID inventoryItemID;
  1789. private UUID primID;
  1790. private bool isScriptRunning;
  1791. private IHttpServer httpListener;
  1792. private bool m_dumpAssetToFile;
  1793. public TaskInventoryScriptUpdater(UUID inventoryItemID, UUID primID, int isScriptRunning,
  1794. string path, IHttpServer httpServer, bool dumpAssetToFile)
  1795. {
  1796. m_dumpAssetToFile = dumpAssetToFile;
  1797. this.inventoryItemID = inventoryItemID;
  1798. this.primID = primID;
  1799. // This comes in over the packet as an integer, but actually appears to be treated as a bool
  1800. this.isScriptRunning = (0 == isScriptRunning ? false : true);
  1801. uploaderPath = path;
  1802. httpListener = httpServer;
  1803. }
  1804. /// <summary>
  1805. ///
  1806. /// </summary>
  1807. /// <param name="data"></param>
  1808. /// <param name="path"></param>
  1809. /// <param name="param"></param>
  1810. /// <returns></returns>
  1811. public string uploaderCaps(byte[] data, string path, string param)
  1812. {
  1813. try
  1814. {
  1815. // m_log.InfoFormat("[CAPS]: " +
  1816. // "TaskInventoryScriptUpdater received data: {0}, path: {1}, param: {2}",
  1817. // data, path, param));
  1818. string res = String.Empty;
  1819. LLSDTaskScriptUploadComplete uploadComplete = new LLSDTaskScriptUploadComplete();
  1820. ArrayList errors = new ArrayList();
  1821. handlerUpdateTaskScript = OnUpLoad;
  1822. if (handlerUpdateTaskScript != null)
  1823. {
  1824. handlerUpdateTaskScript(inventoryItemID, primID, isScriptRunning, data, ref errors);
  1825. }
  1826. uploadComplete.new_asset = inventoryItemID;
  1827. uploadComplete.compiled = errors.Count > 0 ? false : true;
  1828. uploadComplete.state = "complete";
  1829. uploadComplete.errors = new OpenSim.Framework.Capabilities.OSDArray();
  1830. uploadComplete.errors.Array = errors;
  1831. res = LLSDHelpers.SerialiseLLSDReply(uploadComplete);
  1832. httpListener.RemoveStreamHandler("POST", uploaderPath);
  1833. if (m_dumpAssetToFile)
  1834. {
  1835. SaveAssetToFile("updatedtaskscript" + Util.RandomClass.Next(1, 1000) + ".dat", data);
  1836. }
  1837. // m_log.InfoFormat("[CAPS]: TaskInventoryScriptUpdater.uploaderCaps res: {0}", res);
  1838. return res;
  1839. }
  1840. catch (Exception e)
  1841. {
  1842. m_log.Error("[CAPS]: " + e.ToString());
  1843. }
  1844. // XXX Maybe this should be some meaningful error packet
  1845. return null;
  1846. }
  1847. ///Left this in and commented in case there are unforseen issues
  1848. //private void SaveAssetToFile(string filename, byte[] data)
  1849. //{
  1850. // FileStream fs = File.Create(filename);
  1851. // BinaryWriter bw = new BinaryWriter(fs);
  1852. // bw.Write(data);
  1853. // bw.Close();
  1854. // fs.Close();
  1855. //}
  1856. private static void SaveAssetToFile(string filename, byte[] data)
  1857. {
  1858. string assetPath = "UserAssets";
  1859. if (!Directory.Exists(assetPath))
  1860. {
  1861. Directory.CreateDirectory(assetPath);
  1862. }
  1863. FileStream fs = File.Create(Path.Combine(assetPath, filename));
  1864. BinaryWriter bw = new BinaryWriter(fs);
  1865. bw.Write(data);
  1866. bw.Close();
  1867. fs.Close();
  1868. }
  1869. }
  1870. }