Caps.cs 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  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.Generic;
  30. using System.IO;
  31. using System.Reflection;
  32. using log4net;
  33. using OpenMetaverse;
  34. using OpenSim.Framework.Servers;
  35. using OpenSim.Framework.Servers.HttpServer;
  36. using OpenSim.Services.Interfaces;
  37. // using OpenSim.Region.Framework.Interfaces;
  38. namespace OpenSim.Framework.Capabilities
  39. {
  40. public delegate void UpLoadedAsset(
  41. string assetName, string description, UUID assetID, UUID inventoryItem, UUID parentFolder,
  42. byte[] data, string inventoryType, string assetType);
  43. public delegate UUID UpdateItem(UUID itemID, byte[] data);
  44. public delegate void UpdateTaskScript(UUID itemID, UUID primID, bool isScriptRunning, byte[] data);
  45. public delegate void NewInventoryItem(UUID userID, InventoryItemBase item);
  46. public delegate void NewAsset(AssetBase asset);
  47. public delegate UUID ItemUpdatedCallback(UUID userID, UUID itemID, byte[] data);
  48. public delegate void TaskScriptUpdatedCallback(UUID userID, UUID itemID, UUID primID,
  49. bool isScriptRunning, byte[] data);
  50. public delegate InventoryCollection FetchInventoryDescendentsCAPS(UUID agentID, UUID folderID, UUID ownerID,
  51. bool fetchFolders, bool fetchItems, int sortOrder, out int version);
  52. /// <summary>
  53. /// XXX Probably not a particularly nice way of allow us to get the scene presence from the scene (chiefly so that
  54. /// we can popup a message on the user's client if the inventory service has permanently failed). But I didn't want
  55. /// to just pass the whole Scene into CAPS.
  56. /// </summary>
  57. public delegate IClientAPI GetClientDelegate(UUID agentID);
  58. public class Caps
  59. {
  60. private static readonly ILog m_log =
  61. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  62. private string m_httpListenerHostName;
  63. private uint m_httpListenPort;
  64. /// <summary>
  65. /// This is the uuid portion of every CAPS path. It is used to make capability urls private to the requester.
  66. /// </summary>
  67. private string m_capsObjectPath;
  68. public string CapsObjectPath { get { return m_capsObjectPath; } }
  69. private CapsHandlers m_capsHandlers;
  70. private static readonly string m_requestPath = "0000/";
  71. // private static readonly string m_mapLayerPath = "0001/";
  72. private static readonly string m_newInventory = "0002/";
  73. //private static readonly string m_requestTexture = "0003/";
  74. private static readonly string m_notecardUpdatePath = "0004/";
  75. private static readonly string m_notecardTaskUpdatePath = "0005/";
  76. // private static readonly string m_fetchInventoryPath = "0006/";
  77. // The following entries are in a module, however, they are also here so that we don't re-assign
  78. // the path to another cap by mistake.
  79. // private static readonly string m_parcelVoiceInfoRequestPath = "0007/"; // This is in a module.
  80. // private static readonly string m_provisionVoiceAccountRequestPath = "0008/";// This is in a module.
  81. // private static readonly string m_remoteParcelRequestPath = "0009/";// This is in the LandManagementModule.
  82. //private string eventQueue = "0100/";
  83. private IHttpServer m_httpListener;
  84. private UUID m_agentID;
  85. private IAssetService m_assetCache;
  86. private int m_eventQueueCount = 1;
  87. private Queue<string> m_capsEventQueue = new Queue<string>();
  88. private bool m_dumpAssetsToFile;
  89. private string m_regionName;
  90. private object m_fetchLock = new Object();
  91. public bool SSLCaps
  92. {
  93. get { return m_httpListener.UseSSL; }
  94. }
  95. public string SSLCommonName
  96. {
  97. get { return m_httpListener.SSLCommonName; }
  98. }
  99. public CapsHandlers CapsHandlers
  100. {
  101. get { return m_capsHandlers; }
  102. }
  103. // These are callbacks which will be setup by the scene so that we can update scene data when we
  104. // receive capability calls
  105. public NewInventoryItem AddNewInventoryItem = null;
  106. public NewAsset AddNewAsset = null;
  107. public ItemUpdatedCallback ItemUpdatedCall = null;
  108. public TaskScriptUpdatedCallback TaskScriptUpdatedCall = null;
  109. public FetchInventoryDescendentsCAPS CAPSFetchInventoryDescendents = null;
  110. public GetClientDelegate GetClient = null;
  111. public Caps(IAssetService assetCache, IHttpServer httpServer, string httpListen, uint httpPort, string capsPath,
  112. UUID agent, bool dumpAssetsToFile, string regionName)
  113. {
  114. m_assetCache = assetCache;
  115. m_capsObjectPath = capsPath;
  116. m_httpListener = httpServer;
  117. m_httpListenerHostName = httpListen;
  118. m_httpListenPort = httpPort;
  119. if (httpServer.UseSSL)
  120. {
  121. m_httpListenPort = httpServer.SSLPort;
  122. httpListen = httpServer.SSLCommonName;
  123. httpPort = httpServer.SSLPort;
  124. }
  125. m_agentID = agent;
  126. m_dumpAssetsToFile = dumpAssetsToFile;
  127. m_capsHandlers = new CapsHandlers(httpServer, httpListen, httpPort, httpServer.UseSSL);
  128. m_regionName = regionName;
  129. }
  130. /// <summary>
  131. /// Register all CAPS http service handlers
  132. /// </summary>
  133. public void RegisterHandlers()
  134. {
  135. DeregisterHandlers();
  136. string capsBase = "/CAPS/" + m_capsObjectPath;
  137. RegisterRegionServiceHandlers(capsBase);
  138. RegisterInventoryServiceHandlers(capsBase);
  139. }
  140. public void RegisterRegionServiceHandlers(string capsBase)
  141. {
  142. try
  143. {
  144. // the root of all evil
  145. m_capsHandlers["SEED"] = new RestStreamHandler("POST", capsBase + m_requestPath, CapsRequest);
  146. m_log.DebugFormat(
  147. "[CAPS]: Registered seed capability {0} for {1}", capsBase + m_requestPath, m_agentID);
  148. //m_capsHandlers["MapLayer"] =
  149. // new LLSDStreamhandler<OSDMapRequest, OSDMapLayerResponse>("POST",
  150. // capsBase + m_mapLayerPath,
  151. // GetMapLayer);
  152. m_capsHandlers["UpdateScriptTaskInventory"] =
  153. new RestStreamHandler("POST", capsBase + m_notecardTaskUpdatePath, ScriptTaskInventory);
  154. m_capsHandlers["UpdateScriptTask"] = m_capsHandlers["UpdateScriptTaskInventory"];
  155. }
  156. catch (Exception e)
  157. {
  158. m_log.Error("[CAPS]: " + e.ToString());
  159. }
  160. }
  161. public void RegisterInventoryServiceHandlers(string capsBase)
  162. {
  163. try
  164. {
  165. // I don't think this one works...
  166. m_capsHandlers["NewFileAgentInventory"] =
  167. new LLSDStreamhandler<LLSDAssetUploadRequest, LLSDAssetUploadResponse>("POST",
  168. capsBase + m_newInventory,
  169. NewAgentInventoryRequest);
  170. m_capsHandlers["UpdateNotecardAgentInventory"] =
  171. new RestStreamHandler("POST", capsBase + m_notecardUpdatePath, NoteCardAgentInventory);
  172. m_capsHandlers["UpdateScriptAgentInventory"] = m_capsHandlers["UpdateNotecardAgentInventory"];
  173. m_capsHandlers["UpdateScriptAgent"] = m_capsHandlers["UpdateScriptAgentInventory"];
  174. // As of RC 1.22.9 of the Linden client this is
  175. // supported
  176. //m_capsHandlers["WebFetchInventoryDescendents"] =new RestStreamHandler("POST", capsBase + m_fetchInventoryPath, FetchInventoryDescendentsRequest);
  177. // justincc: I've disabled the CAPS service for now to fix problems with selecting textures, and
  178. // subsequent inventory breakage, in the edit object pane (such as mantis 1085). This requires
  179. // enhancements (probably filling out the folder part of the LLSD reply) to our CAPS service,
  180. // but when I went on the Linden grid, the
  181. // simulators I visited (version 1.21) were, surprisingly, no longer supplying this capability. Instead,
  182. // the 1.19.1.4 client appeared to be happily flowing inventory data over UDP
  183. //
  184. // This is very probably just a temporary measure - once the CAPS service appears again on the Linden grid
  185. // we will be
  186. // able to get the data we need to implement the necessary part of the protocol to fix the issue above.
  187. // m_capsHandlers["FetchInventoryDescendents"] =
  188. // new RestStreamHandler("POST", capsBase + m_fetchInventoryPath, FetchInventoryRequest);
  189. // m_capsHandlers["FetchInventoryDescendents"] =
  190. // new LLSDStreamhandler<LLSDFetchInventoryDescendents, LLSDInventoryDescendents>("POST",
  191. // capsBase + m_fetchInventory,
  192. // FetchInventory));
  193. // m_capsHandlers["RequestTextureDownload"] = new RestStreamHandler("POST",
  194. // capsBase + m_requestTexture,
  195. // RequestTexture);
  196. }
  197. catch (Exception e)
  198. {
  199. m_log.Error("[CAPS]: " + e.ToString());
  200. }
  201. }
  202. /// <summary>
  203. /// Register a handler. This allows modules to register handlers.
  204. /// </summary>
  205. /// <param name="capName"></param>
  206. /// <param name="handler"></param>
  207. public void RegisterHandler(string capName, IRequestHandler handler)
  208. {
  209. m_capsHandlers[capName] = handler;
  210. //m_log.DebugFormat("[CAPS]: Registering handler for \"{0}\": path {1}", capName, handler.Path);
  211. }
  212. /// <summary>
  213. /// Remove all CAPS service handlers.
  214. ///
  215. /// </summary>
  216. /// <param name="httpListener"></param>
  217. /// <param name="path"></param>
  218. /// <param name="restMethod"></param>
  219. public void DeregisterHandlers()
  220. {
  221. if (m_capsHandlers != null)
  222. {
  223. foreach (string capsName in m_capsHandlers.Caps)
  224. {
  225. m_capsHandlers.Remove(capsName);
  226. }
  227. }
  228. }
  229. /// <summary>
  230. /// Construct a client response detailing all the capabilities this server can provide.
  231. /// </summary>
  232. /// <param name="request"></param>
  233. /// <param name="path"></param>
  234. /// <param name="param"></param>
  235. /// <param name="httpRequest">HTTP request header object</param>
  236. /// <param name="httpResponse">HTTP response header object</param>
  237. /// <returns></returns>
  238. public string CapsRequest(string request, string path, string param,
  239. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  240. {
  241. //m_log.Debug("[CAPS]: Seed Caps Request in region: " + m_regionName);
  242. string result = LLSDHelpers.SerialiseLLSDReply(m_capsHandlers.CapsDetails);
  243. //m_log.DebugFormat("[CAPS] CapsRequest {0}", result);
  244. return result;
  245. }
  246. // FIXME: these all should probably go into the respective region
  247. // modules
  248. /// <summary>
  249. /// Processes a fetch inventory request and sends the reply
  250. /// </summary>
  251. /// <param name="request"></param>
  252. /// <param name="path"></param>
  253. /// <param name="param"></param>
  254. /// <returns></returns>
  255. // Request is like:
  256. //<llsd>
  257. // <map><key>folders</key>
  258. // <array>
  259. // <map>
  260. // <key>fetch-folders</key><boolean>1</boolean><key>fetch-items</key><boolean>1</boolean><key>folder-id</key><uuid>8e1e3a30-b9bf-11dc-95ff-0800200c9a66</uuid><key>owner-id</key><uuid>11111111-1111-0000-0000-000100bba000</uuid><key>sort-order</key><integer>1</integer>
  261. // </map>
  262. // </array>
  263. // </map>
  264. //</llsd>
  265. //
  266. // multiple fetch-folder maps are allowed within the larger folders map.
  267. public string FetchInventoryRequest(string request, string path, string param)
  268. {
  269. // string unmodifiedRequest = request.ToString();
  270. //m_log.DebugFormat("[AGENT INVENTORY]: Received CAPS fetch inventory request {0}", unmodifiedRequest);
  271. m_log.Debug("[CAPS]: Inventory Request in region: " + m_regionName);
  272. Hashtable hash = new Hashtable();
  273. try
  274. {
  275. hash = (Hashtable)LLSD.LLSDDeserialize(Utils.StringToBytes(request));
  276. }
  277. catch (LLSD.LLSDParseException pe)
  278. {
  279. m_log.Error("[AGENT INVENTORY]: Fetch error: " + pe.Message);
  280. m_log.Error("Request: " + request.ToString());
  281. }
  282. ArrayList foldersrequested = (ArrayList)hash["folders"];
  283. string response = "";
  284. for (int i = 0; i < foldersrequested.Count; i++)
  285. {
  286. string inventoryitemstr = "";
  287. Hashtable inventoryhash = (Hashtable)foldersrequested[i];
  288. LLSDFetchInventoryDescendents llsdRequest = new LLSDFetchInventoryDescendents();
  289. LLSDHelpers.DeserialiseOSDMap(inventoryhash, llsdRequest);
  290. LLSDInventoryDescendents reply = FetchInventoryReply(llsdRequest);
  291. inventoryitemstr = LLSDHelpers.SerialiseLLSDReply(reply);
  292. inventoryitemstr = inventoryitemstr.Replace("<llsd><map><key>folders</key><array>", "");
  293. inventoryitemstr = inventoryitemstr.Replace("</array></map></llsd>", "");
  294. response += inventoryitemstr;
  295. }
  296. if (response.Length == 0)
  297. {
  298. // Ter-guess: If requests fail a lot, the client seems to stop requesting descendants.
  299. // Therefore, I'm concluding that the client only has so many threads available to do requests
  300. // and when a thread stalls.. is stays stalled.
  301. // Therefore we need to return something valid
  302. response = "<llsd><map><key>folders</key><array /></map></llsd>";
  303. }
  304. else
  305. {
  306. response = "<llsd><map><key>folders</key><array>" + response + "</array></map></llsd>";
  307. }
  308. //m_log.DebugFormat("[AGENT INVENTORY]: Replying to CAPS fetch inventory request with following xml");
  309. //m_log.Debug(Util.GetFormattedXml(response));
  310. return response;
  311. }
  312. public string FetchInventoryDescendentsRequest(string request, string path, string param,OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  313. {
  314. // nasty temporary hack here, the linden client falsely
  315. // identifies the uuid 00000000-0000-0000-0000-000000000000
  316. // as a string which breaks us
  317. //
  318. // correctly mark it as a uuid
  319. //
  320. request = request.Replace("<string>00000000-0000-0000-0000-000000000000</string>", "<uuid>00000000-0000-0000-0000-000000000000</uuid>");
  321. // another hack <integer>1</integer> results in a
  322. // System.ArgumentException: Object type System.Int32 cannot
  323. // be converted to target type: System.Boolean
  324. //
  325. request = request.Replace("<key>fetch_folders</key><integer>0</integer>", "<key>fetch_folders</key><boolean>0</boolean>");
  326. request = request.Replace("<key>fetch_folders</key><integer>1</integer>", "<key>fetch_folders</key><boolean>1</boolean>");
  327. Hashtable hash = new Hashtable();
  328. try
  329. {
  330. hash = (Hashtable)LLSD.LLSDDeserialize(Utils.StringToBytes(request));
  331. }
  332. catch (LLSD.LLSDParseException pe)
  333. {
  334. m_log.Error("[AGENT INVENTORY]: Fetch error: " + pe.Message);
  335. m_log.Error("Request: " + request.ToString());
  336. }
  337. ArrayList foldersrequested = (ArrayList)hash["folders"];
  338. string response = "";
  339. lock (m_fetchLock)
  340. {
  341. for (int i = 0; i < foldersrequested.Count; i++)
  342. {
  343. string inventoryitemstr = "";
  344. Hashtable inventoryhash = (Hashtable)foldersrequested[i];
  345. LLSDFetchInventoryDescendents llsdRequest = new LLSDFetchInventoryDescendents();
  346. try{
  347. LLSDHelpers.DeserialiseOSDMap(inventoryhash, llsdRequest);
  348. }
  349. catch(Exception e)
  350. {
  351. m_log.Debug("[CAPS]: caught exception doing OSD deserialize" + e);
  352. }
  353. LLSDInventoryDescendents reply = FetchInventoryReply(llsdRequest);
  354. inventoryitemstr = LLSDHelpers.SerialiseLLSDReply(reply);
  355. inventoryitemstr = inventoryitemstr.Replace("<llsd><map><key>folders</key><array>", "");
  356. inventoryitemstr = inventoryitemstr.Replace("</array></map></llsd>", "");
  357. response += inventoryitemstr;
  358. }
  359. if (response.Length == 0)
  360. {
  361. // Ter-guess: If requests fail a lot, the client seems to stop requesting descendants.
  362. // Therefore, I'm concluding that the client only has so many threads available to do requests
  363. // and when a thread stalls.. is stays stalled.
  364. // Therefore we need to return something valid
  365. response = "<llsd><map><key>folders</key><array /></map></llsd>";
  366. }
  367. else
  368. {
  369. response = "<llsd><map><key>folders</key><array>" + response + "</array></map></llsd>";
  370. }
  371. //m_log.DebugFormat("[CAPS]: Replying to CAPS fetch inventory request with following xml");
  372. //m_log.Debug("[CAPS] "+response);
  373. }
  374. return response;
  375. }
  376. /// <summary>
  377. /// Construct an LLSD reply packet to a CAPS inventory request
  378. /// </summary>
  379. /// <param name="invFetch"></param>
  380. /// <returns></returns>
  381. private LLSDInventoryDescendents FetchInventoryReply(LLSDFetchInventoryDescendents invFetch)
  382. {
  383. LLSDInventoryDescendents reply = new LLSDInventoryDescendents();
  384. LLSDInventoryFolderContents contents = new LLSDInventoryFolderContents();
  385. contents.agent_id = m_agentID;
  386. contents.owner_id = invFetch.owner_id;
  387. contents.folder_id = invFetch.folder_id;
  388. reply.folders.Array.Add(contents);
  389. InventoryCollection inv = new InventoryCollection();
  390. inv.Folders = new List<InventoryFolderBase>();
  391. inv.Items = new List<InventoryItemBase>();
  392. int version = 0;
  393. if (CAPSFetchInventoryDescendents != null)
  394. {
  395. inv = CAPSFetchInventoryDescendents(m_agentID, invFetch.folder_id, invFetch.owner_id, invFetch.fetch_folders, invFetch.fetch_items, invFetch.sort_order, out version);
  396. }
  397. if (inv.Folders != null)
  398. {
  399. foreach (InventoryFolderBase invFolder in inv.Folders)
  400. {
  401. contents.categories.Array.Add(ConvertInventoryFolder(invFolder));
  402. }
  403. }
  404. if (inv.Items != null)
  405. {
  406. foreach (InventoryItemBase invItem in inv.Items)
  407. {
  408. contents.items.Array.Add(ConvertInventoryItem(invItem));
  409. }
  410. }
  411. contents.descendents = contents.items.Array.Count + contents.categories.Array.Count;
  412. contents.version = version;
  413. return reply;
  414. }
  415. /// <summary>
  416. /// Convert an internal inventory folder object into an LLSD object.
  417. /// </summary>
  418. /// <param name="invFolder"></param>
  419. /// <returns></returns>
  420. private LLSDInventoryFolder ConvertInventoryFolder(InventoryFolderBase invFolder)
  421. {
  422. LLSDInventoryFolder llsdFolder = new LLSDInventoryFolder();
  423. llsdFolder.folder_id = invFolder.ID;
  424. llsdFolder.parent_id = invFolder.ParentID;
  425. llsdFolder.name = invFolder.Name;
  426. if (invFolder.Type < 0 || invFolder.Type >= TaskInventoryItem.Types.Length)
  427. llsdFolder.type = "-1";
  428. else
  429. llsdFolder.type = TaskInventoryItem.Types[invFolder.Type];
  430. llsdFolder.preferred_type = "-1";
  431. return llsdFolder;
  432. }
  433. /// <summary>
  434. /// Convert an internal inventory item object into an LLSD object.
  435. /// </summary>
  436. /// <param name="invItem"></param>
  437. /// <returns></returns>
  438. private LLSDInventoryItem ConvertInventoryItem(InventoryItemBase invItem)
  439. {
  440. LLSDInventoryItem llsdItem = new LLSDInventoryItem();
  441. llsdItem.asset_id = invItem.AssetID;
  442. llsdItem.created_at = invItem.CreationDate;
  443. llsdItem.desc = invItem.Description;
  444. llsdItem.flags = (int)invItem.Flags;
  445. llsdItem.item_id = invItem.ID;
  446. llsdItem.name = invItem.Name;
  447. llsdItem.parent_id = invItem.Folder;
  448. try
  449. {
  450. // TODO reevaluate after upgrade to libomv >= r2566. Probably should use UtilsConversions.
  451. llsdItem.type = TaskInventoryItem.Types[invItem.AssetType];
  452. llsdItem.inv_type = TaskInventoryItem.InvTypes[invItem.InvType];
  453. }
  454. catch (Exception e)
  455. {
  456. m_log.Error("[CAPS]: Problem setting asset/inventory type while converting inventory item " + invItem.Name + " to LLSD:", e);
  457. }
  458. llsdItem.permissions = new LLSDPermissions();
  459. llsdItem.permissions.creator_id = invItem.CreatorIdAsUuid;
  460. llsdItem.permissions.base_mask = (int)invItem.CurrentPermissions;
  461. llsdItem.permissions.everyone_mask = (int)invItem.EveryOnePermissions;
  462. llsdItem.permissions.group_id = invItem.GroupID;
  463. llsdItem.permissions.group_mask = (int)invItem.GroupPermissions;
  464. llsdItem.permissions.is_owner_group = invItem.GroupOwned;
  465. llsdItem.permissions.next_owner_mask = (int)invItem.NextPermissions;
  466. llsdItem.permissions.owner_id = m_agentID;
  467. llsdItem.permissions.owner_mask = (int)invItem.CurrentPermissions;
  468. llsdItem.sale_info = new LLSDSaleInfo();
  469. llsdItem.sale_info.sale_price = invItem.SalePrice;
  470. switch (invItem.SaleType)
  471. {
  472. default:
  473. llsdItem.sale_info.sale_type = "not";
  474. break;
  475. case 1:
  476. llsdItem.sale_info.sale_type = "original";
  477. break;
  478. case 2:
  479. llsdItem.sale_info.sale_type = "copy";
  480. break;
  481. case 3:
  482. llsdItem.sale_info.sale_type = "contents";
  483. break;
  484. }
  485. return llsdItem;
  486. }
  487. /// <summary>
  488. ///
  489. /// </summary>
  490. /// <param name="mapReq"></param>
  491. /// <returns></returns>
  492. public LLSDMapLayerResponse GetMapLayer(LLSDMapRequest mapReq)
  493. {
  494. m_log.Debug("[CAPS]: MapLayer Request in region: " + m_regionName);
  495. LLSDMapLayerResponse mapResponse = new LLSDMapLayerResponse();
  496. mapResponse.LayerData.Array.Add(GetOSDMapLayerResponse());
  497. return mapResponse;
  498. }
  499. /// <summary>
  500. ///
  501. /// </summary>
  502. /// <returns></returns>
  503. protected static OSDMapLayer GetOSDMapLayerResponse()
  504. {
  505. OSDMapLayer mapLayer = new OSDMapLayer();
  506. mapLayer.Right = 5000;
  507. mapLayer.Top = 5000;
  508. mapLayer.ImageID = new UUID("00000000-0000-1111-9999-000000000006");
  509. return mapLayer;
  510. }
  511. /// <summary>
  512. ///
  513. /// </summary>
  514. /// <param name="request"></param>
  515. /// <param name="path"></param>
  516. /// <param name="param"></param>
  517. /// <returns></returns>
  518. public string RequestTexture(string request, string path, string param)
  519. {
  520. m_log.Debug("texture request " + request);
  521. // Needs implementing (added to remove compiler warning)
  522. return String.Empty;
  523. }
  524. #region EventQueue (Currently not enabled)
  525. /// <summary>
  526. ///
  527. /// </summary>
  528. /// <param name="request"></param>
  529. /// <param name="path"></param>
  530. /// <param name="param"></param>
  531. /// <returns></returns>
  532. public string ProcessEventQueue(string request, string path, string param)
  533. {
  534. string res = String.Empty;
  535. if (m_capsEventQueue.Count > 0)
  536. {
  537. lock (m_capsEventQueue)
  538. {
  539. string item = m_capsEventQueue.Dequeue();
  540. res = item;
  541. }
  542. }
  543. else
  544. {
  545. res = CreateEmptyEventResponse();
  546. }
  547. return res;
  548. }
  549. /// <summary>
  550. ///
  551. /// </summary>
  552. /// <param name="caps"></param>
  553. /// <param name="ipAddressPort"></param>
  554. /// <returns></returns>
  555. public string CreateEstablishAgentComms(string caps, string ipAddressPort)
  556. {
  557. LLSDCapEvent eventItem = new LLSDCapEvent();
  558. eventItem.id = m_eventQueueCount;
  559. //should be creating a EstablishAgentComms item, but there isn't a class for it yet
  560. eventItem.events.Array.Add(new LLSDEmpty());
  561. string res = LLSDHelpers.SerialiseLLSDReply(eventItem);
  562. m_eventQueueCount++;
  563. m_capsEventQueue.Enqueue(res);
  564. return res;
  565. }
  566. /// <summary>
  567. ///
  568. /// </summary>
  569. /// <returns></returns>
  570. public string CreateEmptyEventResponse()
  571. {
  572. LLSDCapEvent eventItem = new LLSDCapEvent();
  573. eventItem.id = m_eventQueueCount;
  574. eventItem.events.Array.Add(new LLSDEmpty());
  575. string res = LLSDHelpers.SerialiseLLSDReply(eventItem);
  576. m_eventQueueCount++;
  577. return res;
  578. }
  579. #endregion
  580. /// <summary>
  581. /// Called by the script task update handler. Provides a URL to which the client can upload a new asset.
  582. /// </summary>
  583. /// <param name="request"></param>
  584. /// <param name="path"></param>
  585. /// <param name="param"></param>
  586. /// <param name="httpRequest">HTTP request header object</param>
  587. /// <param name="httpResponse">HTTP response header object</param>
  588. /// <returns></returns>
  589. public string ScriptTaskInventory(string request, string path, string param,
  590. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  591. {
  592. try
  593. {
  594. m_log.Debug("[CAPS]: ScriptTaskInventory Request in region: " + m_regionName);
  595. //m_log.DebugFormat("[CAPS]: request: {0}, path: {1}, param: {2}", request, path, param);
  596. Hashtable hash = (Hashtable) LLSD.LLSDDeserialize(Utils.StringToBytes(request));
  597. LLSDTaskScriptUpdate llsdUpdateRequest = new LLSDTaskScriptUpdate();
  598. LLSDHelpers.DeserialiseOSDMap(hash, llsdUpdateRequest);
  599. string capsBase = "/CAPS/" + m_capsObjectPath;
  600. string uploaderPath = Util.RandomClass.Next(5000, 8000).ToString("0000");
  601. TaskInventoryScriptUpdater uploader =
  602. new TaskInventoryScriptUpdater(
  603. llsdUpdateRequest.item_id,
  604. llsdUpdateRequest.task_id,
  605. llsdUpdateRequest.is_script_running,
  606. capsBase + uploaderPath,
  607. m_httpListener,
  608. m_dumpAssetsToFile);
  609. uploader.OnUpLoad += TaskScriptUpdated;
  610. m_httpListener.AddStreamHandler(
  611. new BinaryStreamHandler("POST", capsBase + uploaderPath, uploader.uploaderCaps));
  612. string protocol = "http://";
  613. if (m_httpListener.UseSSL)
  614. protocol = "https://";
  615. string uploaderURL = protocol + m_httpListenerHostName + ":" + m_httpListenPort.ToString() + capsBase +
  616. uploaderPath;
  617. LLSDAssetUploadResponse uploadResponse = new LLSDAssetUploadResponse();
  618. uploadResponse.uploader = uploaderURL;
  619. uploadResponse.state = "upload";
  620. // m_log.InfoFormat("[CAPS]: " +
  621. // "ScriptTaskInventory response: {0}",
  622. // LLSDHelpers.SerialiseLLSDReply(uploadResponse)));
  623. return LLSDHelpers.SerialiseLLSDReply(uploadResponse);
  624. }
  625. catch (Exception e)
  626. {
  627. m_log.Error("[CAPS]: " + e.ToString());
  628. }
  629. return null;
  630. }
  631. /// <summary>
  632. /// Called by the notecard update handler. Provides a URL to which the client can upload a new asset.
  633. /// </summary>
  634. /// <param name="request"></param>
  635. /// <param name="path"></param>
  636. /// <param name="param"></param>
  637. /// <returns></returns>
  638. public string NoteCardAgentInventory(string request, string path, string param,
  639. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  640. {
  641. //m_log.Debug("[CAPS]: NoteCardAgentInventory Request in region: " + m_regionName + "\n" + request);
  642. //m_log.Debug("[CAPS]: NoteCardAgentInventory Request is: " + request);
  643. //OpenMetaverse.StructuredData.OSDMap hash = (OpenMetaverse.StructuredData.OSDMap)OpenMetaverse.StructuredData.LLSDParser.DeserializeBinary(Utils.StringToBytes(request));
  644. Hashtable hash = (Hashtable) LLSD.LLSDDeserialize(Utils.StringToBytes(request));
  645. LLSDItemUpdate llsdRequest = new LLSDItemUpdate();
  646. LLSDHelpers.DeserialiseOSDMap(hash, llsdRequest);
  647. string capsBase = "/CAPS/" + m_capsObjectPath;
  648. string uploaderPath = Util.RandomClass.Next(5000, 8000).ToString("0000");
  649. ItemUpdater uploader =
  650. new ItemUpdater(llsdRequest.item_id, capsBase + uploaderPath, m_httpListener, m_dumpAssetsToFile);
  651. uploader.OnUpLoad += ItemUpdated;
  652. m_httpListener.AddStreamHandler(
  653. new BinaryStreamHandler("POST", capsBase + uploaderPath, uploader.uploaderCaps));
  654. string protocol = "http://";
  655. if (m_httpListener.UseSSL)
  656. protocol = "https://";
  657. string uploaderURL = protocol + m_httpListenerHostName + ":" + m_httpListenPort.ToString() + capsBase +
  658. uploaderPath;
  659. LLSDAssetUploadResponse uploadResponse = new LLSDAssetUploadResponse();
  660. uploadResponse.uploader = uploaderURL;
  661. uploadResponse.state = "upload";
  662. // m_log.InfoFormat("[CAPS]: " +
  663. // "NoteCardAgentInventory response: {0}",
  664. // LLSDHelpers.SerialiseLLSDReply(uploadResponse)));
  665. return LLSDHelpers.SerialiseLLSDReply(uploadResponse);
  666. }
  667. /// <summary>
  668. ///
  669. /// </summary>
  670. /// <param name="llsdRequest"></param>
  671. /// <returns></returns>
  672. public LLSDAssetUploadResponse NewAgentInventoryRequest(LLSDAssetUploadRequest llsdRequest)
  673. {
  674. //m_log.Debug("[CAPS]: NewAgentInventoryRequest Request is: " + llsdRequest.ToString());
  675. //m_log.Debug("asset upload request via CAPS" + llsdRequest.inventory_type + " , " + llsdRequest.asset_type);
  676. if (llsdRequest.asset_type == "texture" ||
  677. llsdRequest.asset_type == "animation" ||
  678. llsdRequest.asset_type == "sound")
  679. {
  680. IClientAPI client = null;
  681. IScene scene = null;
  682. if (GetClient != null)
  683. {
  684. client = GetClient(m_agentID);
  685. scene = client.Scene;
  686. IMoneyModule mm = scene.RequestModuleInterface<IMoneyModule>();
  687. if (mm != null)
  688. {
  689. if (!mm.UploadCovered(client))
  690. {
  691. if (client != null)
  692. client.SendAgentAlertMessage("Unable to upload asset. Insufficient funds.", false);
  693. LLSDAssetUploadResponse errorResponse = new LLSDAssetUploadResponse();
  694. errorResponse.uploader = "";
  695. errorResponse.state = "error";
  696. return errorResponse;
  697. }
  698. }
  699. }
  700. }
  701. string assetName = llsdRequest.name;
  702. string assetDes = llsdRequest.description;
  703. string capsBase = "/CAPS/" + m_capsObjectPath;
  704. UUID newAsset = UUID.Random();
  705. UUID newInvItem = UUID.Random();
  706. UUID parentFolder = llsdRequest.folder_id;
  707. string uploaderPath = Util.RandomClass.Next(5000, 8000).ToString("0000");
  708. AssetUploader uploader =
  709. new AssetUploader(assetName, assetDes, newAsset, newInvItem, parentFolder, llsdRequest.inventory_type,
  710. llsdRequest.asset_type, capsBase + uploaderPath, m_httpListener, m_dumpAssetsToFile);
  711. m_httpListener.AddStreamHandler(
  712. new BinaryStreamHandler("POST", capsBase + uploaderPath, uploader.uploaderCaps));
  713. string protocol = "http://";
  714. if (m_httpListener.UseSSL)
  715. protocol = "https://";
  716. string uploaderURL = protocol + m_httpListenerHostName + ":" + m_httpListenPort.ToString() + capsBase +
  717. uploaderPath;
  718. LLSDAssetUploadResponse uploadResponse = new LLSDAssetUploadResponse();
  719. uploadResponse.uploader = uploaderURL;
  720. uploadResponse.state = "upload";
  721. uploader.OnUpLoad += UploadCompleteHandler;
  722. return uploadResponse;
  723. }
  724. /// <summary>
  725. ///
  726. /// </summary>
  727. /// <param name="assetID"></param>
  728. /// <param name="inventoryItem"></param>
  729. /// <param name="data"></param>
  730. public void UploadCompleteHandler(string assetName, string assetDescription, UUID assetID,
  731. UUID inventoryItem, UUID parentFolder, byte[] data, string inventoryType,
  732. string assetType)
  733. {
  734. sbyte assType = 0;
  735. sbyte inType = 0;
  736. if (inventoryType == "sound")
  737. {
  738. inType = 1;
  739. assType = 1;
  740. }
  741. else if (inventoryType == "animation")
  742. {
  743. inType = 19;
  744. assType = 20;
  745. }
  746. else if (inventoryType == "wearable")
  747. {
  748. inType = 18;
  749. switch (assetType)
  750. {
  751. case "bodypart":
  752. assType = 13;
  753. break;
  754. case "clothing":
  755. assType = 5;
  756. break;
  757. }
  758. }
  759. AssetBase asset;
  760. asset = new AssetBase(assetID, assetName, assType);
  761. asset.Data = data;
  762. if (AddNewAsset != null)
  763. AddNewAsset(asset);
  764. else if (m_assetCache != null)
  765. m_assetCache.Store(asset);
  766. InventoryItemBase item = new InventoryItemBase();
  767. item.Owner = m_agentID;
  768. item.CreatorId = m_agentID.ToString();
  769. item.ID = inventoryItem;
  770. item.AssetID = asset.FullID;
  771. item.Description = assetDescription;
  772. item.Name = assetName;
  773. item.AssetType = assType;
  774. item.InvType = inType;
  775. item.Folder = parentFolder;
  776. item.CurrentPermissions = 2147483647;
  777. item.BasePermissions = 2147483647;
  778. item.EveryOnePermissions = 0;
  779. item.NextPermissions = 2147483647;
  780. item.CreationDate = Util.UnixTimeSinceEpoch();
  781. if (AddNewInventoryItem != null)
  782. {
  783. AddNewInventoryItem(m_agentID, item);
  784. }
  785. }
  786. /// <summary>
  787. /// Called when new asset data for an agent inventory item update has been uploaded.
  788. /// </summary>
  789. /// <param name="itemID">Item to update</param>
  790. /// <param name="data">New asset data</param>
  791. /// <returns></returns>
  792. public UUID ItemUpdated(UUID itemID, byte[] data)
  793. {
  794. if (ItemUpdatedCall != null)
  795. {
  796. return ItemUpdatedCall(m_agentID, itemID, data);
  797. }
  798. return UUID.Zero;
  799. }
  800. /// <summary>
  801. /// Called when new asset data for an agent inventory item update has been uploaded.
  802. /// </summary>
  803. /// <param name="itemID">Item to update</param>
  804. /// <param name="primID">Prim containing item to update</param>
  805. /// <param name="isScriptRunning">Signals whether the script to update is currently running</param>
  806. /// <param name="data">New asset data</param>
  807. public void TaskScriptUpdated(UUID itemID, UUID primID, bool isScriptRunning, byte[] data)
  808. {
  809. if (TaskScriptUpdatedCall != null)
  810. {
  811. TaskScriptUpdatedCall(m_agentID, itemID, primID, isScriptRunning, data);
  812. }
  813. }
  814. public class AssetUploader
  815. {
  816. public event UpLoadedAsset OnUpLoad;
  817. private UpLoadedAsset handlerUpLoad = null;
  818. private string uploaderPath = String.Empty;
  819. private UUID newAssetID;
  820. private UUID inventoryItemID;
  821. private UUID parentFolder;
  822. private IHttpServer httpListener;
  823. private bool m_dumpAssetsToFile;
  824. private string m_assetName = String.Empty;
  825. private string m_assetDes = String.Empty;
  826. private string m_invType = String.Empty;
  827. private string m_assetType = String.Empty;
  828. public AssetUploader(string assetName, string description, UUID assetID, UUID inventoryItem,
  829. UUID parentFolderID, string invType, string assetType, string path,
  830. IHttpServer httpServer, bool dumpAssetsToFile)
  831. {
  832. m_assetName = assetName;
  833. m_assetDes = description;
  834. newAssetID = assetID;
  835. inventoryItemID = inventoryItem;
  836. uploaderPath = path;
  837. httpListener = httpServer;
  838. parentFolder = parentFolderID;
  839. m_assetType = assetType;
  840. m_invType = invType;
  841. m_dumpAssetsToFile = dumpAssetsToFile;
  842. }
  843. /// <summary>
  844. ///
  845. /// </summary>
  846. /// <param name="data"></param>
  847. /// <param name="path"></param>
  848. /// <param name="param"></param>
  849. /// <returns></returns>
  850. public string uploaderCaps(byte[] data, string path, string param)
  851. {
  852. UUID inv = inventoryItemID;
  853. string res = String.Empty;
  854. LLSDAssetUploadComplete uploadComplete = new LLSDAssetUploadComplete();
  855. uploadComplete.new_asset = newAssetID.ToString();
  856. uploadComplete.new_inventory_item = inv;
  857. uploadComplete.state = "complete";
  858. res = LLSDHelpers.SerialiseLLSDReply(uploadComplete);
  859. httpListener.RemoveStreamHandler("POST", uploaderPath);
  860. // TODO: probably make this a better set of extensions here
  861. string extension = ".jp2";
  862. if (m_invType != "image")
  863. {
  864. extension = ".dat";
  865. }
  866. if (m_dumpAssetsToFile)
  867. {
  868. SaveAssetToFile(m_assetName + extension, data);
  869. }
  870. handlerUpLoad = OnUpLoad;
  871. if (handlerUpLoad != null)
  872. {
  873. handlerUpLoad(m_assetName, m_assetDes, newAssetID, inv, parentFolder, data, m_invType, m_assetType);
  874. }
  875. return res;
  876. }
  877. ///Left this in and commented in case there are unforseen issues
  878. //private void SaveAssetToFile(string filename, byte[] data)
  879. //{
  880. // FileStream fs = File.Create(filename);
  881. // BinaryWriter bw = new BinaryWriter(fs);
  882. // bw.Write(data);
  883. // bw.Close();
  884. // fs.Close();
  885. //}
  886. private static void SaveAssetToFile(string filename, byte[] data)
  887. {
  888. string assetPath = "UserAssets";
  889. if (!Directory.Exists(assetPath))
  890. {
  891. Directory.CreateDirectory(assetPath);
  892. }
  893. FileStream fs = File.Create(Path.Combine(assetPath, Util.safeFileName(filename)));
  894. BinaryWriter bw = new BinaryWriter(fs);
  895. bw.Write(data);
  896. bw.Close();
  897. fs.Close();
  898. }
  899. }
  900. /// <summary>
  901. /// This class is a callback invoked when a client sends asset data to
  902. /// an agent inventory notecard update url
  903. /// </summary>
  904. public class ItemUpdater
  905. {
  906. public event UpdateItem OnUpLoad;
  907. private UpdateItem handlerUpdateItem = null;
  908. private string uploaderPath = String.Empty;
  909. private UUID inventoryItemID;
  910. private IHttpServer httpListener;
  911. private bool m_dumpAssetToFile;
  912. public ItemUpdater(UUID inventoryItem, string path, IHttpServer httpServer, bool dumpAssetToFile)
  913. {
  914. m_dumpAssetToFile = dumpAssetToFile;
  915. inventoryItemID = inventoryItem;
  916. uploaderPath = path;
  917. httpListener = httpServer;
  918. }
  919. /// <summary>
  920. ///
  921. /// </summary>
  922. /// <param name="data"></param>
  923. /// <param name="path"></param>
  924. /// <param name="param"></param>
  925. /// <returns></returns>
  926. public string uploaderCaps(byte[] data, string path, string param)
  927. {
  928. UUID inv = inventoryItemID;
  929. string res = String.Empty;
  930. LLSDAssetUploadComplete uploadComplete = new LLSDAssetUploadComplete();
  931. UUID assetID = UUID.Zero;
  932. handlerUpdateItem = OnUpLoad;
  933. if (handlerUpdateItem != null)
  934. {
  935. assetID = handlerUpdateItem(inv, data);
  936. }
  937. uploadComplete.new_asset = assetID.ToString();
  938. uploadComplete.new_inventory_item = inv;
  939. uploadComplete.state = "complete";
  940. res = LLSDHelpers.SerialiseLLSDReply(uploadComplete);
  941. httpListener.RemoveStreamHandler("POST", uploaderPath);
  942. if (m_dumpAssetToFile)
  943. {
  944. SaveAssetToFile("updateditem" + Util.RandomClass.Next(1, 1000) + ".dat", data);
  945. }
  946. return res;
  947. }
  948. ///Left this in and commented in case there are unforseen issues
  949. //private void SaveAssetToFile(string filename, byte[] data)
  950. //{
  951. // FileStream fs = File.Create(filename);
  952. // BinaryWriter bw = new BinaryWriter(fs);
  953. // bw.Write(data);
  954. // bw.Close();
  955. // fs.Close();
  956. //}
  957. private static void SaveAssetToFile(string filename, byte[] data)
  958. {
  959. string assetPath = "UserAssets";
  960. if (!Directory.Exists(assetPath))
  961. {
  962. Directory.CreateDirectory(assetPath);
  963. }
  964. FileStream fs = File.Create(Path.Combine(assetPath, filename));
  965. BinaryWriter bw = new BinaryWriter(fs);
  966. bw.Write(data);
  967. bw.Close();
  968. fs.Close();
  969. }
  970. }
  971. /// <summary>
  972. /// This class is a callback invoked when a client sends asset data to
  973. /// a task inventory script update url
  974. /// </summary>
  975. public class TaskInventoryScriptUpdater
  976. {
  977. public event UpdateTaskScript OnUpLoad;
  978. private UpdateTaskScript handlerUpdateTaskScript = null;
  979. private string uploaderPath = String.Empty;
  980. private UUID inventoryItemID;
  981. private UUID primID;
  982. private bool isScriptRunning;
  983. private IHttpServer httpListener;
  984. private bool m_dumpAssetToFile;
  985. public TaskInventoryScriptUpdater(UUID inventoryItemID, UUID primID, int isScriptRunning,
  986. string path, IHttpServer httpServer, bool dumpAssetToFile)
  987. {
  988. m_dumpAssetToFile = dumpAssetToFile;
  989. this.inventoryItemID = inventoryItemID;
  990. this.primID = primID;
  991. // This comes in over the packet as an integer, but actually appears to be treated as a bool
  992. this.isScriptRunning = (0 == isScriptRunning ? false : true);
  993. uploaderPath = path;
  994. httpListener = httpServer;
  995. }
  996. /// <summary>
  997. ///
  998. /// </summary>
  999. /// <param name="data"></param>
  1000. /// <param name="path"></param>
  1001. /// <param name="param"></param>
  1002. /// <returns></returns>
  1003. public string uploaderCaps(byte[] data, string path, string param)
  1004. {
  1005. try
  1006. {
  1007. // m_log.InfoFormat("[CAPS]: " +
  1008. // "TaskInventoryScriptUpdater received data: {0}, path: {1}, param: {2}",
  1009. // data, path, param));
  1010. string res = String.Empty;
  1011. LLSDTaskInventoryUploadComplete uploadComplete = new LLSDTaskInventoryUploadComplete();
  1012. handlerUpdateTaskScript = OnUpLoad;
  1013. if (handlerUpdateTaskScript != null)
  1014. {
  1015. handlerUpdateTaskScript(inventoryItemID, primID, isScriptRunning, data);
  1016. }
  1017. uploadComplete.item_id = inventoryItemID;
  1018. uploadComplete.task_id = primID;
  1019. uploadComplete.state = "complete";
  1020. res = LLSDHelpers.SerialiseLLSDReply(uploadComplete);
  1021. httpListener.RemoveStreamHandler("POST", uploaderPath);
  1022. if (m_dumpAssetToFile)
  1023. {
  1024. SaveAssetToFile("updatedtaskscript" + Util.RandomClass.Next(1, 1000) + ".dat", data);
  1025. }
  1026. // m_log.InfoFormat("[CAPS]: TaskInventoryScriptUpdater.uploaderCaps res: {0}", res);
  1027. return res;
  1028. }
  1029. catch (Exception e)
  1030. {
  1031. m_log.Error("[CAPS]: " + e.ToString());
  1032. }
  1033. // XXX Maybe this should be some meaningful error packet
  1034. return null;
  1035. }
  1036. ///Left this in and commented in case there are unforseen issues
  1037. //private void SaveAssetToFile(string filename, byte[] data)
  1038. //{
  1039. // FileStream fs = File.Create(filename);
  1040. // BinaryWriter bw = new BinaryWriter(fs);
  1041. // bw.Write(data);
  1042. // bw.Close();
  1043. // fs.Close();
  1044. //}
  1045. private static void SaveAssetToFile(string filename, byte[] data)
  1046. {
  1047. string assetPath = "UserAssets";
  1048. if (!Directory.Exists(assetPath))
  1049. {
  1050. Directory.CreateDirectory(assetPath);
  1051. }
  1052. FileStream fs = File.Create(Path.Combine(assetPath, filename));
  1053. BinaryWriter bw = new BinaryWriter(fs);
  1054. bw.Write(data);
  1055. bw.Close();
  1056. fs.Close();
  1057. }
  1058. }
  1059. }
  1060. }