diff options
Diffstat (limited to 'OpenSim/Region/ClientStack/Linden/Caps/BunchOfCaps/BunchOfCaps.cs')
-rw-r--r-- | OpenSim/Region/ClientStack/Linden/Caps/BunchOfCaps/BunchOfCaps.cs | 938 |
1 files changed, 938 insertions, 0 deletions
diff --git a/OpenSim/Region/ClientStack/Linden/Caps/BunchOfCaps/BunchOfCaps.cs b/OpenSim/Region/ClientStack/Linden/Caps/BunchOfCaps/BunchOfCaps.cs new file mode 100644 index 0000000..95713e9 --- /dev/null +++ b/OpenSim/Region/ClientStack/Linden/Caps/BunchOfCaps/BunchOfCaps.cs | |||
@@ -0,0 +1,938 @@ | |||
1 | using System; | ||
2 | using System.Collections; | ||
3 | using System.Collections.Generic; | ||
4 | using System.IO; | ||
5 | using System.Reflection; | ||
6 | |||
7 | using OpenMetaverse; | ||
8 | using Nini.Config; | ||
9 | using log4net; | ||
10 | |||
11 | using OpenSim.Framework; | ||
12 | using OpenSim.Framework.Capabilities; | ||
13 | using OpenSim.Region.Framework; | ||
14 | using OpenSim.Region.Framework.Scenes; | ||
15 | using OpenSim.Framework.Servers; | ||
16 | using OpenSim.Framework.Servers.HttpServer; | ||
17 | using OpenSim.Services.Interfaces; | ||
18 | |||
19 | using Caps = OpenSim.Framework.Capabilities.Caps; | ||
20 | |||
21 | namespace OpenSim.Region.ClientStack.Linden | ||
22 | { | ||
23 | public delegate void UpLoadedAsset( | ||
24 | string assetName, string description, UUID assetID, UUID inventoryItem, UUID parentFolder, | ||
25 | byte[] data, string inventoryType, string assetType); | ||
26 | |||
27 | public delegate void UploadedBakedTexture(UUID assetID, byte[] data); | ||
28 | |||
29 | public delegate UUID UpdateItem(UUID itemID, byte[] data); | ||
30 | |||
31 | public delegate void UpdateTaskScript(UUID itemID, UUID primID, bool isScriptRunning, byte[] data, ref ArrayList errors); | ||
32 | |||
33 | public delegate void NewInventoryItem(UUID userID, InventoryItemBase item); | ||
34 | |||
35 | public delegate void NewAsset(AssetBase asset); | ||
36 | |||
37 | public delegate UUID ItemUpdatedCallback(UUID userID, UUID itemID, byte[] data); | ||
38 | |||
39 | public delegate ArrayList TaskScriptUpdatedCallback(UUID userID, UUID itemID, UUID primID, | ||
40 | bool isScriptRunning, byte[] data); | ||
41 | |||
42 | public delegate InventoryCollection FetchInventoryDescendentsCAPS(UUID agentID, UUID folderID, UUID ownerID, | ||
43 | bool fetchFolders, bool fetchItems, int sortOrder, out int version); | ||
44 | |||
45 | /// <summary> | ||
46 | /// XXX Probably not a particularly nice way of allow us to get the scene presence from the scene (chiefly so that | ||
47 | /// we can popup a message on the user's client if the inventory service has permanently failed). But I didn't want | ||
48 | /// to just pass the whole Scene into CAPS. | ||
49 | /// </summary> | ||
50 | public delegate IClientAPI GetClientDelegate(UUID agentID); | ||
51 | |||
52 | public class BunchOfCaps | ||
53 | { | ||
54 | private static readonly ILog m_log = | ||
55 | LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
56 | |||
57 | private Scene m_Scene; | ||
58 | private Caps m_HostCapsObj; | ||
59 | |||
60 | private static readonly string m_requestPath = "0000/"; | ||
61 | // private static readonly string m_mapLayerPath = "0001/"; | ||
62 | private static readonly string m_newInventory = "0002/"; | ||
63 | //private static readonly string m_requestTexture = "0003/"; | ||
64 | private static readonly string m_notecardUpdatePath = "0004/"; | ||
65 | private static readonly string m_notecardTaskUpdatePath = "0005/"; | ||
66 | // private static readonly string m_fetchInventoryPath = "0006/"; | ||
67 | // private static readonly string m_remoteParcelRequestPath = "0009/";// This is in the LandManagementModule. | ||
68 | private static readonly string m_uploadBakedTexturePath = "0010/";// This is in the LandManagementModule. | ||
69 | |||
70 | |||
71 | // These are callbacks which will be setup by the scene so that we can update scene data when we | ||
72 | // receive capability calls | ||
73 | public NewInventoryItem AddNewInventoryItem = null; | ||
74 | public NewAsset AddNewAsset = null; | ||
75 | public ItemUpdatedCallback ItemUpdatedCall = null; | ||
76 | public TaskScriptUpdatedCallback TaskScriptUpdatedCall = null; | ||
77 | public FetchInventoryDescendentsCAPS CAPSFetchInventoryDescendents = null; | ||
78 | public GetClientDelegate GetClient = null; | ||
79 | |||
80 | private bool m_persistBakedTextures = false; | ||
81 | private IAssetService m_assetService; | ||
82 | private bool m_dumpAssetsToFile; | ||
83 | private string m_regionName; | ||
84 | |||
85 | public BunchOfCaps(Scene scene, Caps caps) | ||
86 | { | ||
87 | m_Scene = scene; | ||
88 | m_HostCapsObj = caps; | ||
89 | IConfigSource config = m_Scene.Config; | ||
90 | if (config != null) | ||
91 | { | ||
92 | IConfig sconfig = config.Configs["Startup"]; | ||
93 | if (sconfig != null) | ||
94 | m_persistBakedTextures = sconfig.GetBoolean("PersistBakedTextures", m_persistBakedTextures); | ||
95 | } | ||
96 | |||
97 | m_assetService = m_Scene.AssetService; | ||
98 | m_regionName = m_Scene.RegionInfo.RegionName; | ||
99 | |||
100 | RegisterHandlers(); | ||
101 | |||
102 | AddNewInventoryItem = m_Scene.AddUploadedInventoryItem; | ||
103 | ItemUpdatedCall = m_Scene.CapsUpdateInventoryItemAsset; | ||
104 | TaskScriptUpdatedCall = m_Scene.CapsUpdateTaskInventoryScriptAsset; | ||
105 | CAPSFetchInventoryDescendents = m_Scene.HandleFetchInventoryDescendentsCAPS; | ||
106 | GetClient = m_Scene.SceneContents.GetControllingClient; | ||
107 | |||
108 | } | ||
109 | |||
110 | /// <summary> | ||
111 | /// Register a bunch of CAPS http service handlers | ||
112 | /// </summary> | ||
113 | public void RegisterHandlers() | ||
114 | { | ||
115 | string capsBase = "/CAPS/" + m_HostCapsObj.CapsObjectPath; | ||
116 | |||
117 | RegisterRegionServiceHandlers(capsBase); | ||
118 | RegisterInventoryServiceHandlers(capsBase); | ||
119 | } | ||
120 | |||
121 | public void RegisterRegionServiceHandlers(string capsBase) | ||
122 | { | ||
123 | try | ||
124 | { | ||
125 | // the root of all evil | ||
126 | m_HostCapsObj.RegisterHandler("SEED", new RestStreamHandler("POST", capsBase + m_requestPath, SeedCapRequest)); | ||
127 | m_log.DebugFormat( | ||
128 | "[CAPS]: Registered seed capability {0} for {1}", capsBase + m_requestPath, m_HostCapsObj.AgentID); | ||
129 | |||
130 | //m_capsHandlers["MapLayer"] = | ||
131 | // new LLSDStreamhandler<OSDMapRequest, OSDMapLayerResponse>("POST", | ||
132 | // capsBase + m_mapLayerPath, | ||
133 | // GetMapLayer); | ||
134 | IRequestHandler req = new RestStreamHandler("POST", capsBase + m_notecardTaskUpdatePath, ScriptTaskInventory); | ||
135 | m_HostCapsObj.RegisterHandler("UpdateScriptTaskInventory", req); | ||
136 | m_HostCapsObj.RegisterHandler("UpdateScriptTask", req); | ||
137 | m_HostCapsObj.RegisterHandler("UploadBakedTexture", new RestStreamHandler("POST", capsBase + m_uploadBakedTexturePath, UploadBakedTexture)); | ||
138 | |||
139 | } | ||
140 | catch (Exception e) | ||
141 | { | ||
142 | m_log.Error("[CAPS]: " + e.ToString()); | ||
143 | } | ||
144 | } | ||
145 | |||
146 | public void RegisterInventoryServiceHandlers(string capsBase) | ||
147 | { | ||
148 | try | ||
149 | { | ||
150 | // I don't think this one works... | ||
151 | m_HostCapsObj.RegisterHandler("NewFileAgentInventory", new LLSDStreamhandler<LLSDAssetUploadRequest, LLSDAssetUploadResponse>("POST", | ||
152 | capsBase + m_newInventory, | ||
153 | NewAgentInventoryRequest)); | ||
154 | IRequestHandler req = new RestStreamHandler("POST", capsBase + m_notecardUpdatePath, NoteCardAgentInventory); | ||
155 | m_HostCapsObj.RegisterHandler("UpdateNotecardAgentInventory", req); | ||
156 | m_HostCapsObj.RegisterHandler("UpdateScriptAgentInventory", req); | ||
157 | m_HostCapsObj.RegisterHandler("UpdateScriptAgent", req); | ||
158 | |||
159 | // As of RC 1.22.9 of the Linden client this is | ||
160 | // supported | ||
161 | |||
162 | //m_capsHandlers["WebFetchInventoryDescendents"] =new RestStreamHandler("POST", capsBase + m_fetchInventoryPath, FetchInventoryDescendentsRequest); | ||
163 | |||
164 | // justincc: I've disabled the CAPS service for now to fix problems with selecting textures, and | ||
165 | // subsequent inventory breakage, in the edit object pane (such as mantis 1085). This requires | ||
166 | // enhancements (probably filling out the folder part of the LLSD reply) to our CAPS service, | ||
167 | // but when I went on the Linden grid, the | ||
168 | // simulators I visited (version 1.21) were, surprisingly, no longer supplying this capability. Instead, | ||
169 | // the 1.19.1.4 client appeared to be happily flowing inventory data over UDP | ||
170 | // | ||
171 | // This is very probably just a temporary measure - once the CAPS service appears again on the Linden grid | ||
172 | // we will be | ||
173 | // able to get the data we need to implement the necessary part of the protocol to fix the issue above. | ||
174 | // m_capsHandlers["FetchInventoryDescendents"] = | ||
175 | // new RestStreamHandler("POST", capsBase + m_fetchInventoryPath, FetchInventoryRequest); | ||
176 | |||
177 | // m_capsHandlers["FetchInventoryDescendents"] = | ||
178 | // new LLSDStreamhandler<LLSDFetchInventoryDescendents, LLSDInventoryDescendents>("POST", | ||
179 | // capsBase + m_fetchInventory, | ||
180 | // FetchInventory)); | ||
181 | // m_capsHandlers["RequestTextureDownload"] = new RestStreamHandler("POST", | ||
182 | // capsBase + m_requestTexture, | ||
183 | // RequestTexture); | ||
184 | } | ||
185 | catch (Exception e) | ||
186 | { | ||
187 | m_log.Error("[CAPS]: " + e.ToString()); | ||
188 | } | ||
189 | } | ||
190 | |||
191 | /// <summary> | ||
192 | /// Construct a client response detailing all the capabilities this server can provide. | ||
193 | /// </summary> | ||
194 | /// <param name="request"></param> | ||
195 | /// <param name="path"></param> | ||
196 | /// <param name="param"></param> | ||
197 | /// <param name="httpRequest">HTTP request header object</param> | ||
198 | /// <param name="httpResponse">HTTP response header object</param> | ||
199 | /// <returns></returns> | ||
200 | public string SeedCapRequest(string request, string path, string param, | ||
201 | OSHttpRequest httpRequest, OSHttpResponse httpResponse) | ||
202 | { | ||
203 | m_log.Debug("[CAPS]: Seed Caps Request in region: " + m_regionName); | ||
204 | |||
205 | if (!m_Scene.CheckClient(m_HostCapsObj.AgentID, httpRequest.RemoteIPEndPoint)) | ||
206 | { | ||
207 | m_log.DebugFormat("[CAPS]: Unauthorized CAPS client"); | ||
208 | return string.Empty; | ||
209 | } | ||
210 | |||
211 | Hashtable caps = m_HostCapsObj.CapsHandlers.CapsDetails; | ||
212 | // Add the external too | ||
213 | foreach (KeyValuePair<string, string> kvp in m_HostCapsObj.ExternalCapsHandlers) | ||
214 | caps[kvp.Key] = kvp.Value; | ||
215 | |||
216 | string result = LLSDHelpers.SerialiseLLSDReply(caps); | ||
217 | |||
218 | //m_log.DebugFormat("[CAPS] CapsRequest {0}", result); | ||
219 | |||
220 | return result; | ||
221 | } | ||
222 | |||
223 | /// <summary> | ||
224 | /// Called by the script task update handler. Provides a URL to which the client can upload a new asset. | ||
225 | /// </summary> | ||
226 | /// <param name="request"></param> | ||
227 | /// <param name="path"></param> | ||
228 | /// <param name="param"></param> | ||
229 | /// <param name="httpRequest">HTTP request header object</param> | ||
230 | /// <param name="httpResponse">HTTP response header object</param> | ||
231 | /// <returns></returns> | ||
232 | public string ScriptTaskInventory(string request, string path, string param, | ||
233 | OSHttpRequest httpRequest, OSHttpResponse httpResponse) | ||
234 | { | ||
235 | try | ||
236 | { | ||
237 | m_log.Debug("[CAPS]: ScriptTaskInventory Request in region: " + m_regionName); | ||
238 | //m_log.DebugFormat("[CAPS]: request: {0}, path: {1}, param: {2}", request, path, param); | ||
239 | |||
240 | Hashtable hash = (Hashtable)LLSD.LLSDDeserialize(Utils.StringToBytes(request)); | ||
241 | LLSDTaskScriptUpdate llsdUpdateRequest = new LLSDTaskScriptUpdate(); | ||
242 | LLSDHelpers.DeserialiseOSDMap(hash, llsdUpdateRequest); | ||
243 | |||
244 | string capsBase = "/CAPS/" + m_HostCapsObj.CapsObjectPath; | ||
245 | string uploaderPath = Util.RandomClass.Next(5000, 8000).ToString("0000"); | ||
246 | |||
247 | TaskInventoryScriptUpdater uploader = | ||
248 | new TaskInventoryScriptUpdater( | ||
249 | llsdUpdateRequest.item_id, | ||
250 | llsdUpdateRequest.task_id, | ||
251 | llsdUpdateRequest.is_script_running, | ||
252 | capsBase + uploaderPath, | ||
253 | m_HostCapsObj.HttpListener, | ||
254 | m_dumpAssetsToFile); | ||
255 | uploader.OnUpLoad += TaskScriptUpdated; | ||
256 | |||
257 | m_HostCapsObj.HttpListener.AddStreamHandler(new BinaryStreamHandler("POST", capsBase + uploaderPath, uploader.uploaderCaps)); | ||
258 | |||
259 | string protocol = "http://"; | ||
260 | |||
261 | if (m_HostCapsObj.SSLCaps) | ||
262 | protocol = "https://"; | ||
263 | |||
264 | string uploaderURL = protocol + m_HostCapsObj.HostName + ":" + m_HostCapsObj.Port.ToString() + capsBase + | ||
265 | uploaderPath; | ||
266 | |||
267 | LLSDAssetUploadResponse uploadResponse = new LLSDAssetUploadResponse(); | ||
268 | uploadResponse.uploader = uploaderURL; | ||
269 | uploadResponse.state = "upload"; | ||
270 | |||
271 | // m_log.InfoFormat("[CAPS]: " + | ||
272 | // "ScriptTaskInventory response: {0}", | ||
273 | // LLSDHelpers.SerialiseLLSDReply(uploadResponse))); | ||
274 | |||
275 | return LLSDHelpers.SerialiseLLSDReply(uploadResponse); | ||
276 | } | ||
277 | catch (Exception e) | ||
278 | { | ||
279 | m_log.Error("[CAPS]: " + e.ToString()); | ||
280 | } | ||
281 | |||
282 | return null; | ||
283 | } | ||
284 | |||
285 | /// <summary> | ||
286 | /// Called when new asset data for an agent inventory item update has been uploaded. | ||
287 | /// </summary> | ||
288 | /// <param name="itemID">Item to update</param> | ||
289 | /// <param name="primID">Prim containing item to update</param> | ||
290 | /// <param name="isScriptRunning">Signals whether the script to update is currently running</param> | ||
291 | /// <param name="data">New asset data</param> | ||
292 | public void TaskScriptUpdated(UUID itemID, UUID primID, bool isScriptRunning, byte[] data, ref ArrayList errors) | ||
293 | { | ||
294 | if (TaskScriptUpdatedCall != null) | ||
295 | { | ||
296 | ArrayList e = TaskScriptUpdatedCall(m_HostCapsObj.AgentID, itemID, primID, isScriptRunning, data); | ||
297 | foreach (Object item in e) | ||
298 | errors.Add(item); | ||
299 | } | ||
300 | } | ||
301 | |||
302 | public string UploadBakedTexture(string request, string path, | ||
303 | string param, OSHttpRequest httpRequest, | ||
304 | OSHttpResponse httpResponse) | ||
305 | { | ||
306 | try | ||
307 | { | ||
308 | // m_log.Debug("[CAPS]: UploadBakedTexture Request in region: " + | ||
309 | // m_regionName); | ||
310 | |||
311 | string capsBase = "/CAPS/" + m_HostCapsObj.CapsObjectPath; | ||
312 | string uploaderPath = Util.RandomClass.Next(5000, 8000).ToString("0000"); | ||
313 | |||
314 | BakedTextureUploader uploader = | ||
315 | new BakedTextureUploader(capsBase + uploaderPath, m_HostCapsObj.HttpListener); | ||
316 | uploader.OnUpLoad += BakedTextureUploaded; | ||
317 | |||
318 | m_HostCapsObj.HttpListener.AddStreamHandler( | ||
319 | new BinaryStreamHandler("POST", capsBase + uploaderPath, | ||
320 | uploader.uploaderCaps)); | ||
321 | |||
322 | string protocol = "http://"; | ||
323 | |||
324 | if (m_HostCapsObj.SSLCaps) | ||
325 | protocol = "https://"; | ||
326 | |||
327 | string uploaderURL = protocol + m_HostCapsObj.HostName + ":" + | ||
328 | m_HostCapsObj.Port.ToString() + capsBase + uploaderPath; | ||
329 | |||
330 | LLSDAssetUploadResponse uploadResponse = | ||
331 | new LLSDAssetUploadResponse(); | ||
332 | uploadResponse.uploader = uploaderURL; | ||
333 | uploadResponse.state = "upload"; | ||
334 | |||
335 | return LLSDHelpers.SerialiseLLSDReply(uploadResponse); | ||
336 | } | ||
337 | catch (Exception e) | ||
338 | { | ||
339 | m_log.Error("[CAPS]: " + e.ToString()); | ||
340 | } | ||
341 | |||
342 | return null; | ||
343 | } | ||
344 | |||
345 | public void BakedTextureUploaded(UUID assetID, byte[] data) | ||
346 | { | ||
347 | // m_log.WarnFormat("[CAPS]: Received baked texture {0}", assetID.ToString()); | ||
348 | |||
349 | AssetBase asset; | ||
350 | asset = new AssetBase(assetID, "Baked Texture", (sbyte)AssetType.Texture, m_HostCapsObj.AgentID.ToString()); | ||
351 | asset.Data = data; | ||
352 | asset.Temporary = true; | ||
353 | asset.Local = !m_persistBakedTextures; // Local assets aren't persisted, non-local are | ||
354 | m_assetService.Store(asset); | ||
355 | } | ||
356 | |||
357 | /// <summary> | ||
358 | /// Called when new asset data for an agent inventory item update has been uploaded. | ||
359 | /// </summary> | ||
360 | /// <param name="itemID">Item to update</param> | ||
361 | /// <param name="data">New asset data</param> | ||
362 | /// <returns></returns> | ||
363 | public UUID ItemUpdated(UUID itemID, byte[] data) | ||
364 | { | ||
365 | if (ItemUpdatedCall != null) | ||
366 | { | ||
367 | return ItemUpdatedCall(m_HostCapsObj.AgentID, itemID, data); | ||
368 | } | ||
369 | |||
370 | return UUID.Zero; | ||
371 | } | ||
372 | |||
373 | /// <summary> | ||
374 | /// | ||
375 | /// </summary> | ||
376 | /// <param name="llsdRequest"></param> | ||
377 | /// <returns></returns> | ||
378 | public LLSDAssetUploadResponse NewAgentInventoryRequest(LLSDAssetUploadRequest llsdRequest) | ||
379 | { | ||
380 | //m_log.Debug("[CAPS]: NewAgentInventoryRequest Request is: " + llsdRequest.ToString()); | ||
381 | //m_log.Debug("asset upload request via CAPS" + llsdRequest.inventory_type + " , " + llsdRequest.asset_type); | ||
382 | |||
383 | if (llsdRequest.asset_type == "texture" || | ||
384 | llsdRequest.asset_type == "animation" || | ||
385 | llsdRequest.asset_type == "sound") | ||
386 | { | ||
387 | IClientAPI client = null; | ||
388 | IScene scene = null; | ||
389 | if (GetClient != null) | ||
390 | { | ||
391 | client = GetClient(m_HostCapsObj.AgentID); | ||
392 | scene = client.Scene; | ||
393 | |||
394 | IMoneyModule mm = scene.RequestModuleInterface<IMoneyModule>(); | ||
395 | |||
396 | if (mm != null) | ||
397 | { | ||
398 | if (!mm.UploadCovered(client, mm.UploadCharge)) | ||
399 | { | ||
400 | if (client != null) | ||
401 | client.SendAgentAlertMessage("Unable to upload asset. Insufficient funds.", false); | ||
402 | |||
403 | LLSDAssetUploadResponse errorResponse = new LLSDAssetUploadResponse(); | ||
404 | errorResponse.uploader = ""; | ||
405 | errorResponse.state = "error"; | ||
406 | return errorResponse; | ||
407 | } | ||
408 | } | ||
409 | } | ||
410 | } | ||
411 | |||
412 | string assetName = llsdRequest.name; | ||
413 | string assetDes = llsdRequest.description; | ||
414 | string capsBase = "/CAPS/" + m_HostCapsObj.CapsObjectPath; | ||
415 | UUID newAsset = UUID.Random(); | ||
416 | UUID newInvItem = UUID.Random(); | ||
417 | UUID parentFolder = llsdRequest.folder_id; | ||
418 | string uploaderPath = Util.RandomClass.Next(5000, 8000).ToString("0000"); | ||
419 | |||
420 | AssetUploader uploader = | ||
421 | new AssetUploader(assetName, assetDes, newAsset, newInvItem, parentFolder, llsdRequest.inventory_type, | ||
422 | llsdRequest.asset_type, capsBase + uploaderPath, m_HostCapsObj.HttpListener, m_dumpAssetsToFile); | ||
423 | m_HostCapsObj.HttpListener.AddStreamHandler( | ||
424 | new BinaryStreamHandler("POST", capsBase + uploaderPath, uploader.uploaderCaps)); | ||
425 | |||
426 | string protocol = "http://"; | ||
427 | |||
428 | if (m_HostCapsObj.SSLCaps) | ||
429 | protocol = "https://"; | ||
430 | |||
431 | string uploaderURL = protocol + m_HostCapsObj.HostName + ":" + m_HostCapsObj.Port.ToString() + capsBase + | ||
432 | uploaderPath; | ||
433 | |||
434 | LLSDAssetUploadResponse uploadResponse = new LLSDAssetUploadResponse(); | ||
435 | uploadResponse.uploader = uploaderURL; | ||
436 | uploadResponse.state = "upload"; | ||
437 | uploader.OnUpLoad += UploadCompleteHandler; | ||
438 | return uploadResponse; | ||
439 | } | ||
440 | |||
441 | /// <summary> | ||
442 | /// | ||
443 | /// </summary> | ||
444 | /// <param name="assetID"></param> | ||
445 | /// <param name="inventoryItem"></param> | ||
446 | /// <param name="data"></param> | ||
447 | public void UploadCompleteHandler(string assetName, string assetDescription, UUID assetID, | ||
448 | UUID inventoryItem, UUID parentFolder, byte[] data, string inventoryType, | ||
449 | string assetType) | ||
450 | { | ||
451 | sbyte assType = 0; | ||
452 | sbyte inType = 0; | ||
453 | |||
454 | if (inventoryType == "sound") | ||
455 | { | ||
456 | inType = 1; | ||
457 | assType = 1; | ||
458 | } | ||
459 | else if (inventoryType == "animation") | ||
460 | { | ||
461 | inType = 19; | ||
462 | assType = 20; | ||
463 | } | ||
464 | else if (inventoryType == "wearable") | ||
465 | { | ||
466 | inType = 18; | ||
467 | switch (assetType) | ||
468 | { | ||
469 | case "bodypart": | ||
470 | assType = 13; | ||
471 | break; | ||
472 | case "clothing": | ||
473 | assType = 5; | ||
474 | break; | ||
475 | } | ||
476 | } | ||
477 | |||
478 | AssetBase asset; | ||
479 | asset = new AssetBase(assetID, assetName, assType, m_HostCapsObj.AgentID.ToString()); | ||
480 | asset.Data = data; | ||
481 | if (AddNewAsset != null) | ||
482 | AddNewAsset(asset); | ||
483 | else if (m_assetService != null) | ||
484 | m_assetService.Store(asset); | ||
485 | |||
486 | InventoryItemBase item = new InventoryItemBase(); | ||
487 | item.Owner = m_HostCapsObj.AgentID; | ||
488 | item.CreatorId = m_HostCapsObj.AgentID.ToString(); | ||
489 | item.CreatorData = String.Empty; | ||
490 | item.ID = inventoryItem; | ||
491 | item.AssetID = asset.FullID; | ||
492 | item.Description = assetDescription; | ||
493 | item.Name = assetName; | ||
494 | item.AssetType = assType; | ||
495 | item.InvType = inType; | ||
496 | item.Folder = parentFolder; | ||
497 | item.CurrentPermissions = (uint)PermissionMask.All; | ||
498 | item.BasePermissions = (uint)PermissionMask.All; | ||
499 | item.EveryOnePermissions = 0; | ||
500 | item.NextPermissions = (uint)(PermissionMask.Move | PermissionMask.Modify | PermissionMask.Transfer); | ||
501 | item.CreationDate = Util.UnixTimeSinceEpoch(); | ||
502 | |||
503 | if (AddNewInventoryItem != null) | ||
504 | { | ||
505 | AddNewInventoryItem(m_HostCapsObj.AgentID, item); | ||
506 | } | ||
507 | } | ||
508 | |||
509 | |||
510 | |||
511 | /// <summary> | ||
512 | /// | ||
513 | /// </summary> | ||
514 | /// <param name="mapReq"></param> | ||
515 | /// <returns></returns> | ||
516 | public LLSDMapLayerResponse GetMapLayer(LLSDMapRequest mapReq) | ||
517 | { | ||
518 | m_log.Debug("[CAPS]: MapLayer Request in region: " + m_regionName); | ||
519 | LLSDMapLayerResponse mapResponse = new LLSDMapLayerResponse(); | ||
520 | mapResponse.LayerData.Array.Add(GetOSDMapLayerResponse()); | ||
521 | return mapResponse; | ||
522 | } | ||
523 | |||
524 | /// <summary> | ||
525 | /// | ||
526 | /// </summary> | ||
527 | /// <returns></returns> | ||
528 | protected static OSDMapLayer GetOSDMapLayerResponse() | ||
529 | { | ||
530 | OSDMapLayer mapLayer = new OSDMapLayer(); | ||
531 | mapLayer.Right = 5000; | ||
532 | mapLayer.Top = 5000; | ||
533 | mapLayer.ImageID = new UUID("00000000-0000-1111-9999-000000000006"); | ||
534 | |||
535 | return mapLayer; | ||
536 | } | ||
537 | |||
538 | /// <summary> | ||
539 | /// | ||
540 | /// </summary> | ||
541 | /// <param name="request"></param> | ||
542 | /// <param name="path"></param> | ||
543 | /// <param name="param"></param> | ||
544 | /// <returns></returns> | ||
545 | public string RequestTexture(string request, string path, string param) | ||
546 | { | ||
547 | m_log.Debug("texture request " + request); | ||
548 | // Needs implementing (added to remove compiler warning) | ||
549 | return String.Empty; | ||
550 | } | ||
551 | |||
552 | |||
553 | /// <summary> | ||
554 | /// Called by the notecard update handler. Provides a URL to which the client can upload a new asset. | ||
555 | /// </summary> | ||
556 | /// <param name="request"></param> | ||
557 | /// <param name="path"></param> | ||
558 | /// <param name="param"></param> | ||
559 | /// <returns></returns> | ||
560 | public string NoteCardAgentInventory(string request, string path, string param, | ||
561 | OSHttpRequest httpRequest, OSHttpResponse httpResponse) | ||
562 | { | ||
563 | //m_log.Debug("[CAPS]: NoteCardAgentInventory Request in region: " + m_regionName + "\n" + request); | ||
564 | //m_log.Debug("[CAPS]: NoteCardAgentInventory Request is: " + request); | ||
565 | |||
566 | //OpenMetaverse.StructuredData.OSDMap hash = (OpenMetaverse.StructuredData.OSDMap)OpenMetaverse.StructuredData.LLSDParser.DeserializeBinary(Utils.StringToBytes(request)); | ||
567 | Hashtable hash = (Hashtable)LLSD.LLSDDeserialize(Utils.StringToBytes(request)); | ||
568 | LLSDItemUpdate llsdRequest = new LLSDItemUpdate(); | ||
569 | LLSDHelpers.DeserialiseOSDMap(hash, llsdRequest); | ||
570 | |||
571 | string capsBase = "/CAPS/" + m_HostCapsObj.CapsObjectPath; | ||
572 | string uploaderPath = Util.RandomClass.Next(5000, 8000).ToString("0000"); | ||
573 | |||
574 | ItemUpdater uploader = | ||
575 | new ItemUpdater(llsdRequest.item_id, capsBase + uploaderPath, m_HostCapsObj.HttpListener, m_dumpAssetsToFile); | ||
576 | uploader.OnUpLoad += ItemUpdated; | ||
577 | |||
578 | m_HostCapsObj.HttpListener.AddStreamHandler( | ||
579 | new BinaryStreamHandler("POST", capsBase + uploaderPath, uploader.uploaderCaps)); | ||
580 | |||
581 | string protocol = "http://"; | ||
582 | |||
583 | if (m_HostCapsObj.SSLCaps) | ||
584 | protocol = "https://"; | ||
585 | |||
586 | string uploaderURL = protocol + m_HostCapsObj.HostName + ":" + m_HostCapsObj.Port.ToString() + capsBase + | ||
587 | uploaderPath; | ||
588 | |||
589 | LLSDAssetUploadResponse uploadResponse = new LLSDAssetUploadResponse(); | ||
590 | uploadResponse.uploader = uploaderURL; | ||
591 | uploadResponse.state = "upload"; | ||
592 | |||
593 | // m_log.InfoFormat("[CAPS]: " + | ||
594 | // "NoteCardAgentInventory response: {0}", | ||
595 | // LLSDHelpers.SerialiseLLSDReply(uploadResponse))); | ||
596 | |||
597 | return LLSDHelpers.SerialiseLLSDReply(uploadResponse); | ||
598 | } | ||
599 | } | ||
600 | |||
601 | public class AssetUploader | ||
602 | { | ||
603 | public event UpLoadedAsset OnUpLoad; | ||
604 | private UpLoadedAsset handlerUpLoad = null; | ||
605 | |||
606 | private string uploaderPath = String.Empty; | ||
607 | private UUID newAssetID; | ||
608 | private UUID inventoryItemID; | ||
609 | private UUID parentFolder; | ||
610 | private IHttpServer httpListener; | ||
611 | private bool m_dumpAssetsToFile; | ||
612 | private string m_assetName = String.Empty; | ||
613 | private string m_assetDes = String.Empty; | ||
614 | |||
615 | private string m_invType = String.Empty; | ||
616 | private string m_assetType = String.Empty; | ||
617 | |||
618 | public AssetUploader(string assetName, string description, UUID assetID, UUID inventoryItem, | ||
619 | UUID parentFolderID, string invType, string assetType, string path, | ||
620 | IHttpServer httpServer, bool dumpAssetsToFile) | ||
621 | { | ||
622 | m_assetName = assetName; | ||
623 | m_assetDes = description; | ||
624 | newAssetID = assetID; | ||
625 | inventoryItemID = inventoryItem; | ||
626 | uploaderPath = path; | ||
627 | httpListener = httpServer; | ||
628 | parentFolder = parentFolderID; | ||
629 | m_assetType = assetType; | ||
630 | m_invType = invType; | ||
631 | m_dumpAssetsToFile = dumpAssetsToFile; | ||
632 | } | ||
633 | |||
634 | /// <summary> | ||
635 | /// | ||
636 | /// </summary> | ||
637 | /// <param name="data"></param> | ||
638 | /// <param name="path"></param> | ||
639 | /// <param name="param"></param> | ||
640 | /// <returns></returns> | ||
641 | public string uploaderCaps(byte[] data, string path, string param) | ||
642 | { | ||
643 | UUID inv = inventoryItemID; | ||
644 | string res = String.Empty; | ||
645 | LLSDAssetUploadComplete uploadComplete = new LLSDAssetUploadComplete(); | ||
646 | uploadComplete.new_asset = newAssetID.ToString(); | ||
647 | uploadComplete.new_inventory_item = inv; | ||
648 | uploadComplete.state = "complete"; | ||
649 | |||
650 | res = LLSDHelpers.SerialiseLLSDReply(uploadComplete); | ||
651 | |||
652 | httpListener.RemoveStreamHandler("POST", uploaderPath); | ||
653 | |||
654 | // TODO: probably make this a better set of extensions here | ||
655 | string extension = ".jp2"; | ||
656 | if (m_invType != "image") | ||
657 | { | ||
658 | extension = ".dat"; | ||
659 | } | ||
660 | |||
661 | if (m_dumpAssetsToFile) | ||
662 | { | ||
663 | SaveAssetToFile(m_assetName + extension, data); | ||
664 | } | ||
665 | handlerUpLoad = OnUpLoad; | ||
666 | if (handlerUpLoad != null) | ||
667 | { | ||
668 | handlerUpLoad(m_assetName, m_assetDes, newAssetID, inv, parentFolder, data, m_invType, m_assetType); | ||
669 | } | ||
670 | |||
671 | return res; | ||
672 | } | ||
673 | ///Left this in and commented in case there are unforseen issues | ||
674 | //private void SaveAssetToFile(string filename, byte[] data) | ||
675 | //{ | ||
676 | // FileStream fs = File.Create(filename); | ||
677 | // BinaryWriter bw = new BinaryWriter(fs); | ||
678 | // bw.Write(data); | ||
679 | // bw.Close(); | ||
680 | // fs.Close(); | ||
681 | //} | ||
682 | private static void SaveAssetToFile(string filename, byte[] data) | ||
683 | { | ||
684 | string assetPath = "UserAssets"; | ||
685 | if (!Directory.Exists(assetPath)) | ||
686 | { | ||
687 | Directory.CreateDirectory(assetPath); | ||
688 | } | ||
689 | FileStream fs = File.Create(Path.Combine(assetPath, Util.safeFileName(filename))); | ||
690 | BinaryWriter bw = new BinaryWriter(fs); | ||
691 | bw.Write(data); | ||
692 | bw.Close(); | ||
693 | fs.Close(); | ||
694 | } | ||
695 | } | ||
696 | |||
697 | /// <summary> | ||
698 | /// This class is a callback invoked when a client sends asset data to | ||
699 | /// an agent inventory notecard update url | ||
700 | /// </summary> | ||
701 | public class ItemUpdater | ||
702 | { | ||
703 | public event UpdateItem OnUpLoad; | ||
704 | |||
705 | private UpdateItem handlerUpdateItem = null; | ||
706 | |||
707 | private string uploaderPath = String.Empty; | ||
708 | private UUID inventoryItemID; | ||
709 | private IHttpServer httpListener; | ||
710 | private bool m_dumpAssetToFile; | ||
711 | |||
712 | public ItemUpdater(UUID inventoryItem, string path, IHttpServer httpServer, bool dumpAssetToFile) | ||
713 | { | ||
714 | m_dumpAssetToFile = dumpAssetToFile; | ||
715 | |||
716 | inventoryItemID = inventoryItem; | ||
717 | uploaderPath = path; | ||
718 | httpListener = httpServer; | ||
719 | } | ||
720 | |||
721 | /// <summary> | ||
722 | /// | ||
723 | /// </summary> | ||
724 | /// <param name="data"></param> | ||
725 | /// <param name="path"></param> | ||
726 | /// <param name="param"></param> | ||
727 | /// <returns></returns> | ||
728 | public string uploaderCaps(byte[] data, string path, string param) | ||
729 | { | ||
730 | UUID inv = inventoryItemID; | ||
731 | string res = String.Empty; | ||
732 | LLSDAssetUploadComplete uploadComplete = new LLSDAssetUploadComplete(); | ||
733 | UUID assetID = UUID.Zero; | ||
734 | handlerUpdateItem = OnUpLoad; | ||
735 | if (handlerUpdateItem != null) | ||
736 | { | ||
737 | assetID = handlerUpdateItem(inv, data); | ||
738 | } | ||
739 | |||
740 | uploadComplete.new_asset = assetID.ToString(); | ||
741 | uploadComplete.new_inventory_item = inv; | ||
742 | uploadComplete.state = "complete"; | ||
743 | |||
744 | res = LLSDHelpers.SerialiseLLSDReply(uploadComplete); | ||
745 | |||
746 | httpListener.RemoveStreamHandler("POST", uploaderPath); | ||
747 | |||
748 | if (m_dumpAssetToFile) | ||
749 | { | ||
750 | SaveAssetToFile("updateditem" + Util.RandomClass.Next(1, 1000) + ".dat", data); | ||
751 | } | ||
752 | |||
753 | return res; | ||
754 | } | ||
755 | ///Left this in and commented in case there are unforseen issues | ||
756 | //private void SaveAssetToFile(string filename, byte[] data) | ||
757 | //{ | ||
758 | // FileStream fs = File.Create(filename); | ||
759 | // BinaryWriter bw = new BinaryWriter(fs); | ||
760 | // bw.Write(data); | ||
761 | // bw.Close(); | ||
762 | // fs.Close(); | ||
763 | //} | ||
764 | private static void SaveAssetToFile(string filename, byte[] data) | ||
765 | { | ||
766 | string assetPath = "UserAssets"; | ||
767 | if (!Directory.Exists(assetPath)) | ||
768 | { | ||
769 | Directory.CreateDirectory(assetPath); | ||
770 | } | ||
771 | FileStream fs = File.Create(Path.Combine(assetPath, filename)); | ||
772 | BinaryWriter bw = new BinaryWriter(fs); | ||
773 | bw.Write(data); | ||
774 | bw.Close(); | ||
775 | fs.Close(); | ||
776 | } | ||
777 | } | ||
778 | |||
779 | /// <summary> | ||
780 | /// This class is a callback invoked when a client sends asset data to | ||
781 | /// a task inventory script update url | ||
782 | /// </summary> | ||
783 | public class TaskInventoryScriptUpdater | ||
784 | { | ||
785 | private static readonly ILog m_log = | ||
786 | LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
787 | |||
788 | public event UpdateTaskScript OnUpLoad; | ||
789 | |||
790 | private UpdateTaskScript handlerUpdateTaskScript = null; | ||
791 | |||
792 | private string uploaderPath = String.Empty; | ||
793 | private UUID inventoryItemID; | ||
794 | private UUID primID; | ||
795 | private bool isScriptRunning; | ||
796 | private IHttpServer httpListener; | ||
797 | private bool m_dumpAssetToFile; | ||
798 | |||
799 | public TaskInventoryScriptUpdater(UUID inventoryItemID, UUID primID, int isScriptRunning, | ||
800 | string path, IHttpServer httpServer, bool dumpAssetToFile) | ||
801 | { | ||
802 | m_dumpAssetToFile = dumpAssetToFile; | ||
803 | |||
804 | this.inventoryItemID = inventoryItemID; | ||
805 | this.primID = primID; | ||
806 | |||
807 | // This comes in over the packet as an integer, but actually appears to be treated as a bool | ||
808 | this.isScriptRunning = (0 == isScriptRunning ? false : true); | ||
809 | |||
810 | uploaderPath = path; | ||
811 | httpListener = httpServer; | ||
812 | } | ||
813 | |||
814 | /// <summary> | ||
815 | /// | ||
816 | /// </summary> | ||
817 | /// <param name="data"></param> | ||
818 | /// <param name="path"></param> | ||
819 | /// <param name="param"></param> | ||
820 | /// <returns></returns> | ||
821 | public string uploaderCaps(byte[] data, string path, string param) | ||
822 | { | ||
823 | try | ||
824 | { | ||
825 | // m_log.InfoFormat("[CAPS]: " + | ||
826 | // "TaskInventoryScriptUpdater received data: {0}, path: {1}, param: {2}", | ||
827 | // data, path, param)); | ||
828 | |||
829 | string res = String.Empty; | ||
830 | LLSDTaskScriptUploadComplete uploadComplete = new LLSDTaskScriptUploadComplete(); | ||
831 | |||
832 | ArrayList errors = new ArrayList(); | ||
833 | handlerUpdateTaskScript = OnUpLoad; | ||
834 | if (handlerUpdateTaskScript != null) | ||
835 | { | ||
836 | handlerUpdateTaskScript(inventoryItemID, primID, isScriptRunning, data, ref errors); | ||
837 | } | ||
838 | |||
839 | uploadComplete.new_asset = inventoryItemID; | ||
840 | uploadComplete.compiled = errors.Count > 0 ? false : true; | ||
841 | uploadComplete.state = "complete"; | ||
842 | uploadComplete.errors = new OSDArray(); | ||
843 | uploadComplete.errors.Array = errors; | ||
844 | |||
845 | res = LLSDHelpers.SerialiseLLSDReply(uploadComplete); | ||
846 | |||
847 | httpListener.RemoveStreamHandler("POST", uploaderPath); | ||
848 | |||
849 | if (m_dumpAssetToFile) | ||
850 | { | ||
851 | SaveAssetToFile("updatedtaskscript" + Util.RandomClass.Next(1, 1000) + ".dat", data); | ||
852 | } | ||
853 | |||
854 | // m_log.InfoFormat("[CAPS]: TaskInventoryScriptUpdater.uploaderCaps res: {0}", res); | ||
855 | |||
856 | return res; | ||
857 | } | ||
858 | catch (Exception e) | ||
859 | { | ||
860 | m_log.Error("[CAPS]: " + e.ToString()); | ||
861 | } | ||
862 | |||
863 | // XXX Maybe this should be some meaningful error packet | ||
864 | return null; | ||
865 | } | ||
866 | ///Left this in and commented in case there are unforseen issues | ||
867 | //private void SaveAssetToFile(string filename, byte[] data) | ||
868 | //{ | ||
869 | // FileStream fs = File.Create(filename); | ||
870 | // BinaryWriter bw = new BinaryWriter(fs); | ||
871 | // bw.Write(data); | ||
872 | // bw.Close(); | ||
873 | // fs.Close(); | ||
874 | //} | ||
875 | private static void SaveAssetToFile(string filename, byte[] data) | ||
876 | { | ||
877 | string assetPath = "UserAssets"; | ||
878 | if (!Directory.Exists(assetPath)) | ||
879 | { | ||
880 | Directory.CreateDirectory(assetPath); | ||
881 | } | ||
882 | FileStream fs = File.Create(Path.Combine(assetPath, filename)); | ||
883 | BinaryWriter bw = new BinaryWriter(fs); | ||
884 | bw.Write(data); | ||
885 | bw.Close(); | ||
886 | fs.Close(); | ||
887 | } | ||
888 | } | ||
889 | |||
890 | public class BakedTextureUploader | ||
891 | { | ||
892 | public event UploadedBakedTexture OnUpLoad; | ||
893 | private UploadedBakedTexture handlerUpLoad = null; | ||
894 | |||
895 | private string uploaderPath = String.Empty; | ||
896 | private UUID newAssetID; | ||
897 | private IHttpServer httpListener; | ||
898 | |||
899 | public BakedTextureUploader(string path, IHttpServer httpServer) | ||
900 | { | ||
901 | newAssetID = UUID.Random(); | ||
902 | uploaderPath = path; | ||
903 | httpListener = httpServer; | ||
904 | // m_log.InfoFormat("[CAPS] baked texture upload starting for {0}",newAssetID); | ||
905 | } | ||
906 | |||
907 | /// <summary> | ||
908 | /// | ||
909 | /// </summary> | ||
910 | /// <param name="data"></param> | ||
911 | /// <param name="path"></param> | ||
912 | /// <param name="param"></param> | ||
913 | /// <returns></returns> | ||
914 | public string uploaderCaps(byte[] data, string path, string param) | ||
915 | { | ||
916 | handlerUpLoad = OnUpLoad; | ||
917 | if (handlerUpLoad != null) | ||
918 | { | ||
919 | Util.FireAndForget(delegate(object o) { handlerUpLoad(newAssetID, data); }); | ||
920 | } | ||
921 | |||
922 | string res = String.Empty; | ||
923 | LLSDAssetUploadComplete uploadComplete = new LLSDAssetUploadComplete(); | ||
924 | uploadComplete.new_asset = newAssetID.ToString(); | ||
925 | uploadComplete.new_inventory_item = UUID.Zero; | ||
926 | uploadComplete.state = "complete"; | ||
927 | |||
928 | res = LLSDHelpers.SerialiseLLSDReply(uploadComplete); | ||
929 | |||
930 | httpListener.RemoveStreamHandler("POST", uploaderPath); | ||
931 | |||
932 | // m_log.InfoFormat("[CAPS] baked texture upload completed for {0}",newAssetID); | ||
933 | |||
934 | return res; | ||
935 | } | ||
936 | } | ||
937 | |||
938 | } | ||