aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ClientStack/Linden/Caps/BunchOfCaps/BunchOfCaps.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/ClientStack/Linden/Caps/BunchOfCaps/BunchOfCaps.cs')
-rw-r--r--OpenSim/Region/ClientStack/Linden/Caps/BunchOfCaps/BunchOfCaps.cs1210
1 files changed, 1210 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..7a7964e
--- /dev/null
+++ b/OpenSim/Region/ClientStack/Linden/Caps/BunchOfCaps/BunchOfCaps.cs
@@ -0,0 +1,1210 @@
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.IO;
5using System.Reflection;
6
7using OpenMetaverse;
8using Nini.Config;
9using log4net;
10
11using OpenSim.Framework;
12using OpenSim.Framework.Capabilities;
13using OpenSim.Region.Framework;
14using OpenSim.Region.Framework.Scenes;
15using OpenSim.Framework.Servers;
16using OpenSim.Framework.Servers.HttpServer;
17using OpenSim.Services.Interfaces;
18
19using Caps = OpenSim.Framework.Capabilities.Caps;
20
21namespace 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_assetCache;
82 private bool m_dumpAssetsToFile;
83 private string m_regionName;
84 private object m_fetchLock = new Object();
85
86 public BunchOfCaps(Scene scene, Caps caps)
87 {
88 m_Scene = scene;
89 m_HostCapsObj = caps;
90 IConfigSource config = m_Scene.Config;
91 if (config != null)
92 {
93 IConfig sconfig = config.Configs["Startup"];
94 if (sconfig != null)
95 m_persistBakedTextures = sconfig.GetBoolean("PersistBakedTextures", m_persistBakedTextures);
96 }
97
98 RegisterHandlers();
99
100 AddNewInventoryItem = m_Scene.AddUploadedInventoryItem;
101 ItemUpdatedCall = m_Scene.CapsUpdateInventoryItemAsset;
102 TaskScriptUpdatedCall = m_Scene.CapsUpdateTaskInventoryScriptAsset;
103 CAPSFetchInventoryDescendents = m_Scene.HandleFetchInventoryDescendentsCAPS;
104 GetClient = m_Scene.SceneContents.GetControllingClient;
105
106 }
107
108 /// <summary>
109 /// Register a bunch of CAPS http service handlers
110 /// </summary>
111 public void RegisterHandlers()
112 {
113 m_HostCapsObj.DeregisterHandlers();
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 // WARNING: Add the external too
212 string result = LLSDHelpers.SerialiseLLSDReply(m_HostCapsObj.CapsHandlers.CapsDetails);
213
214 //m_log.DebugFormat("[CAPS] CapsRequest {0}", result);
215
216 return result;
217 }
218
219 /// <summary>
220 /// Called by the script task update handler. Provides a URL to which the client can upload a new asset.
221 /// </summary>
222 /// <param name="request"></param>
223 /// <param name="path"></param>
224 /// <param name="param"></param>
225 /// <param name="httpRequest">HTTP request header object</param>
226 /// <param name="httpResponse">HTTP response header object</param>
227 /// <returns></returns>
228 public string ScriptTaskInventory(string request, string path, string param,
229 OSHttpRequest httpRequest, OSHttpResponse httpResponse)
230 {
231 try
232 {
233 m_log.Debug("[CAPS]: ScriptTaskInventory Request in region: " + m_regionName);
234 //m_log.DebugFormat("[CAPS]: request: {0}, path: {1}, param: {2}", request, path, param);
235
236 Hashtable hash = (Hashtable)LLSD.LLSDDeserialize(Utils.StringToBytes(request));
237 LLSDTaskScriptUpdate llsdUpdateRequest = new LLSDTaskScriptUpdate();
238 LLSDHelpers.DeserialiseOSDMap(hash, llsdUpdateRequest);
239
240 string capsBase = "/CAPS/" + m_HostCapsObj.CapsObjectPath;
241 string uploaderPath = Util.RandomClass.Next(5000, 8000).ToString("0000");
242
243 TaskInventoryScriptUpdater uploader =
244 new TaskInventoryScriptUpdater(
245 llsdUpdateRequest.item_id,
246 llsdUpdateRequest.task_id,
247 llsdUpdateRequest.is_script_running,
248 capsBase + uploaderPath,
249 m_HostCapsObj.HttpListener,
250 m_dumpAssetsToFile);
251 uploader.OnUpLoad += TaskScriptUpdated;
252
253 m_HostCapsObj.HttpListener.AddStreamHandler(new BinaryStreamHandler("POST", capsBase + uploaderPath, uploader.uploaderCaps));
254
255 string protocol = "http://";
256
257 if (m_HostCapsObj.SSLCaps)
258 protocol = "https://";
259
260 string uploaderURL = protocol + m_HostCapsObj.HostName + ":" + m_HostCapsObj.Port.ToString() + capsBase +
261 uploaderPath;
262
263 LLSDAssetUploadResponse uploadResponse = new LLSDAssetUploadResponse();
264 uploadResponse.uploader = uploaderURL;
265 uploadResponse.state = "upload";
266
267 // m_log.InfoFormat("[CAPS]: " +
268 // "ScriptTaskInventory response: {0}",
269 // LLSDHelpers.SerialiseLLSDReply(uploadResponse)));
270
271 return LLSDHelpers.SerialiseLLSDReply(uploadResponse);
272 }
273 catch (Exception e)
274 {
275 m_log.Error("[CAPS]: " + e.ToString());
276 }
277
278 return null;
279 }
280
281 /// <summary>
282 /// Called when new asset data for an agent inventory item update has been uploaded.
283 /// </summary>
284 /// <param name="itemID">Item to update</param>
285 /// <param name="primID">Prim containing item to update</param>
286 /// <param name="isScriptRunning">Signals whether the script to update is currently running</param>
287 /// <param name="data">New asset data</param>
288 public void TaskScriptUpdated(UUID itemID, UUID primID, bool isScriptRunning, byte[] data, ref ArrayList errors)
289 {
290 if (TaskScriptUpdatedCall != null)
291 {
292 ArrayList e = TaskScriptUpdatedCall(m_HostCapsObj.AgentID, itemID, primID, isScriptRunning, data);
293 foreach (Object item in e)
294 errors.Add(item);
295 }
296 }
297
298 public string UploadBakedTexture(string request, string path,
299 string param, OSHttpRequest httpRequest,
300 OSHttpResponse httpResponse)
301 {
302 try
303 {
304 // m_log.Debug("[CAPS]: UploadBakedTexture Request in region: " +
305 // m_regionName);
306
307 string capsBase = "/CAPS/" + m_HostCapsObj.CapsObjectPath;
308 string uploaderPath = Util.RandomClass.Next(5000, 8000).ToString("0000");
309
310 BakedTextureUploader uploader =
311 new BakedTextureUploader(capsBase + uploaderPath, m_HostCapsObj.HttpListener);
312 uploader.OnUpLoad += BakedTextureUploaded;
313
314 m_HostCapsObj.HttpListener.AddStreamHandler(
315 new BinaryStreamHandler("POST", capsBase + uploaderPath,
316 uploader.uploaderCaps));
317
318 string protocol = "http://";
319
320 if (m_HostCapsObj.SSLCaps)
321 protocol = "https://";
322
323 string uploaderURL = protocol + m_HostCapsObj.HostName + ":" +
324 m_HostCapsObj.Port.ToString() + capsBase + uploaderPath;
325
326 LLSDAssetUploadResponse uploadResponse =
327 new LLSDAssetUploadResponse();
328 uploadResponse.uploader = uploaderURL;
329 uploadResponse.state = "upload";
330
331 return LLSDHelpers.SerialiseLLSDReply(uploadResponse);
332 }
333 catch (Exception e)
334 {
335 m_log.Error("[CAPS]: " + e.ToString());
336 }
337
338 return null;
339 }
340
341 public void BakedTextureUploaded(UUID assetID, byte[] data)
342 {
343 // m_log.WarnFormat("[CAPS]: Received baked texture {0}", assetID.ToString());
344
345 AssetBase asset;
346 asset = new AssetBase(assetID, "Baked Texture", (sbyte)AssetType.Texture, m_HostCapsObj.AgentID.ToString());
347 asset.Data = data;
348 asset.Temporary = true;
349 asset.Local = !m_persistBakedTextures; // Local assets aren't persisted, non-local are
350 m_assetCache.Store(asset);
351 }
352
353 /// <summary>
354 /// Called when new asset data for an agent inventory item update has been uploaded.
355 /// </summary>
356 /// <param name="itemID">Item to update</param>
357 /// <param name="data">New asset data</param>
358 /// <returns></returns>
359 public UUID ItemUpdated(UUID itemID, byte[] data)
360 {
361 if (ItemUpdatedCall != null)
362 {
363 return ItemUpdatedCall(m_HostCapsObj.AgentID, itemID, data);
364 }
365
366 return UUID.Zero;
367 }
368
369 /// <summary>
370 ///
371 /// </summary>
372 /// <param name="llsdRequest"></param>
373 /// <returns></returns>
374 public LLSDAssetUploadResponse NewAgentInventoryRequest(LLSDAssetUploadRequest llsdRequest)
375 {
376 //m_log.Debug("[CAPS]: NewAgentInventoryRequest Request is: " + llsdRequest.ToString());
377 //m_log.Debug("asset upload request via CAPS" + llsdRequest.inventory_type + " , " + llsdRequest.asset_type);
378
379 if (llsdRequest.asset_type == "texture" ||
380 llsdRequest.asset_type == "animation" ||
381 llsdRequest.asset_type == "sound")
382 {
383 IClientAPI client = null;
384 IScene scene = null;
385 if (GetClient != null)
386 {
387 client = GetClient(m_HostCapsObj.AgentID);
388 scene = client.Scene;
389
390 IMoneyModule mm = scene.RequestModuleInterface<IMoneyModule>();
391
392 if (mm != null)
393 {
394 if (!mm.UploadCovered(client, mm.UploadCharge))
395 {
396 if (client != null)
397 client.SendAgentAlertMessage("Unable to upload asset. Insufficient funds.", false);
398
399 LLSDAssetUploadResponse errorResponse = new LLSDAssetUploadResponse();
400 errorResponse.uploader = "";
401 errorResponse.state = "error";
402 return errorResponse;
403 }
404 }
405 }
406 }
407
408 string assetName = llsdRequest.name;
409 string assetDes = llsdRequest.description;
410 string capsBase = "/CAPS/" + m_HostCapsObj.CapsObjectPath;
411 UUID newAsset = UUID.Random();
412 UUID newInvItem = UUID.Random();
413 UUID parentFolder = llsdRequest.folder_id;
414 string uploaderPath = Util.RandomClass.Next(5000, 8000).ToString("0000");
415
416 AssetUploader uploader =
417 new AssetUploader(assetName, assetDes, newAsset, newInvItem, parentFolder, llsdRequest.inventory_type,
418 llsdRequest.asset_type, capsBase + uploaderPath, m_HostCapsObj.HttpListener, m_dumpAssetsToFile);
419 m_HostCapsObj.HttpListener.AddStreamHandler(
420 new BinaryStreamHandler("POST", capsBase + uploaderPath, uploader.uploaderCaps));
421
422 string protocol = "http://";
423
424 if (m_HostCapsObj.SSLCaps)
425 protocol = "https://";
426
427 string uploaderURL = protocol + m_HostCapsObj.HostName + ":" + m_HostCapsObj.Port.ToString() + capsBase +
428 uploaderPath;
429
430 LLSDAssetUploadResponse uploadResponse = new LLSDAssetUploadResponse();
431 uploadResponse.uploader = uploaderURL;
432 uploadResponse.state = "upload";
433 uploader.OnUpLoad += UploadCompleteHandler;
434 return uploadResponse;
435 }
436
437 /// <summary>
438 ///
439 /// </summary>
440 /// <param name="assetID"></param>
441 /// <param name="inventoryItem"></param>
442 /// <param name="data"></param>
443 public void UploadCompleteHandler(string assetName, string assetDescription, UUID assetID,
444 UUID inventoryItem, UUID parentFolder, byte[] data, string inventoryType,
445 string assetType)
446 {
447 sbyte assType = 0;
448 sbyte inType = 0;
449
450 if (inventoryType == "sound")
451 {
452 inType = 1;
453 assType = 1;
454 }
455 else if (inventoryType == "animation")
456 {
457 inType = 19;
458 assType = 20;
459 }
460 else if (inventoryType == "wearable")
461 {
462 inType = 18;
463 switch (assetType)
464 {
465 case "bodypart":
466 assType = 13;
467 break;
468 case "clothing":
469 assType = 5;
470 break;
471 }
472 }
473
474 AssetBase asset;
475 asset = new AssetBase(assetID, assetName, assType, m_HostCapsObj.AgentID.ToString());
476 asset.Data = data;
477 if (AddNewAsset != null)
478 AddNewAsset(asset);
479 else if (m_assetCache != null)
480 m_assetCache.Store(asset);
481
482 InventoryItemBase item = new InventoryItemBase();
483 item.Owner = m_HostCapsObj.AgentID;
484 item.CreatorId = m_HostCapsObj.AgentID.ToString();
485 item.CreatorData = String.Empty;
486 item.ID = inventoryItem;
487 item.AssetID = asset.FullID;
488 item.Description = assetDescription;
489 item.Name = assetName;
490 item.AssetType = assType;
491 item.InvType = inType;
492 item.Folder = parentFolder;
493 item.CurrentPermissions = (uint)PermissionMask.All;
494 item.BasePermissions = (uint)PermissionMask.All;
495 item.EveryOnePermissions = 0;
496 item.NextPermissions = (uint)(PermissionMask.Move | PermissionMask.Modify | PermissionMask.Transfer);
497 item.CreationDate = Util.UnixTimeSinceEpoch();
498
499 if (AddNewInventoryItem != null)
500 {
501 AddNewInventoryItem(m_HostCapsObj.AgentID, item);
502 }
503 }
504
505 /// <summary>
506 /// Processes a fetch inventory request and sends the reply
507
508 /// </summary>
509 /// <param name="request"></param>
510 /// <param name="path"></param>
511 /// <param name="param"></param>
512 /// <returns></returns>
513 // Request is like:
514 //<llsd>
515 // <map><key>folders</key>
516 // <array>
517 // <map>
518 // <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>
519 // </map>
520 // </array>
521 // </map>
522 //</llsd>
523 //
524 // multiple fetch-folder maps are allowed within the larger folders map.
525 public string FetchInventoryRequest(string request, string path, string param)
526 {
527 // string unmodifiedRequest = request.ToString();
528
529 //m_log.DebugFormat("[AGENT INVENTORY]: Received CAPS fetch inventory request {0}", unmodifiedRequest);
530 m_log.Debug("[CAPS]: Inventory Request in region: " + m_regionName);
531
532 Hashtable hash = new Hashtable();
533 try
534 {
535 hash = (Hashtable)LLSD.LLSDDeserialize(Utils.StringToBytes(request));
536 }
537 catch (LLSD.LLSDParseException pe)
538 {
539 m_log.Error("[AGENT INVENTORY]: Fetch error: " + pe.Message);
540 m_log.Error("Request: " + request.ToString());
541 }
542
543 ArrayList foldersrequested = (ArrayList)hash["folders"];
544
545 string response = "";
546
547 for (int i = 0; i < foldersrequested.Count; i++)
548 {
549 string inventoryitemstr = "";
550 Hashtable inventoryhash = (Hashtable)foldersrequested[i];
551
552 LLSDFetchInventoryDescendents llsdRequest = new LLSDFetchInventoryDescendents();
553 LLSDHelpers.DeserialiseOSDMap(inventoryhash, llsdRequest);
554 LLSDInventoryDescendents reply = FetchInventoryReply(llsdRequest);
555
556 inventoryitemstr = LLSDHelpers.SerialiseLLSDReply(reply);
557 inventoryitemstr = inventoryitemstr.Replace("<llsd><map><key>folders</key><array>", "");
558 inventoryitemstr = inventoryitemstr.Replace("</array></map></llsd>", "");
559
560 response += inventoryitemstr;
561 }
562
563 if (response.Length == 0)
564 {
565 // Ter-guess: If requests fail a lot, the client seems to stop requesting descendants.
566 // Therefore, I'm concluding that the client only has so many threads available to do requests
567 // and when a thread stalls.. is stays stalled.
568 // Therefore we need to return something valid
569 response = "<llsd><map><key>folders</key><array /></map></llsd>";
570 }
571 else
572 {
573 response = "<llsd><map><key>folders</key><array>" + response + "</array></map></llsd>";
574 }
575
576 //m_log.DebugFormat("[AGENT INVENTORY]: Replying to CAPS fetch inventory request with following xml");
577 //m_log.Debug(Util.GetFormattedXml(response));
578
579 return response;
580 }
581
582 public string FetchInventoryDescendentsRequest(string request, string path, string param, OSHttpRequest httpRequest, OSHttpResponse httpResponse)
583 {
584 // nasty temporary hack here, the linden client falsely
585 // identifies the uuid 00000000-0000-0000-0000-000000000000
586 // as a string which breaks us
587 //
588 // correctly mark it as a uuid
589 //
590 request = request.Replace("<string>00000000-0000-0000-0000-000000000000</string>", "<uuid>00000000-0000-0000-0000-000000000000</uuid>");
591
592 // another hack <integer>1</integer> results in a
593 // System.ArgumentException: Object type System.Int32 cannot
594 // be converted to target type: System.Boolean
595 //
596 request = request.Replace("<key>fetch_folders</key><integer>0</integer>", "<key>fetch_folders</key><boolean>0</boolean>");
597 request = request.Replace("<key>fetch_folders</key><integer>1</integer>", "<key>fetch_folders</key><boolean>1</boolean>");
598
599 Hashtable hash = new Hashtable();
600 try
601 {
602 hash = (Hashtable)LLSD.LLSDDeserialize(Utils.StringToBytes(request));
603 }
604 catch (LLSD.LLSDParseException pe)
605 {
606 m_log.Error("[AGENT INVENTORY]: Fetch error: " + pe.Message);
607 m_log.Error("Request: " + request.ToString());
608 }
609
610 ArrayList foldersrequested = (ArrayList)hash["folders"];
611
612 string response = "";
613 lock (m_fetchLock)
614 {
615 for (int i = 0; i < foldersrequested.Count; i++)
616 {
617 string inventoryitemstr = "";
618 Hashtable inventoryhash = (Hashtable)foldersrequested[i];
619
620 LLSDFetchInventoryDescendents llsdRequest = new LLSDFetchInventoryDescendents();
621
622 try
623 {
624 LLSDHelpers.DeserialiseOSDMap(inventoryhash, llsdRequest);
625 }
626 catch (Exception e)
627 {
628 m_log.Debug("[CAPS]: caught exception doing OSD deserialize" + e);
629 }
630 LLSDInventoryDescendents reply = FetchInventoryReply(llsdRequest);
631
632 inventoryitemstr = LLSDHelpers.SerialiseLLSDReply(reply);
633 inventoryitemstr = inventoryitemstr.Replace("<llsd><map><key>folders</key><array>", "");
634 inventoryitemstr = inventoryitemstr.Replace("</array></map></llsd>", "");
635
636 response += inventoryitemstr;
637 }
638
639
640 if (response.Length == 0)
641 {
642 // Ter-guess: If requests fail a lot, the client seems to stop requesting descendants.
643 // Therefore, I'm concluding that the client only has so many threads available to do requests
644 // and when a thread stalls.. is stays stalled.
645 // Therefore we need to return something valid
646 response = "<llsd><map><key>folders</key><array /></map></llsd>";
647 }
648 else
649 {
650 response = "<llsd><map><key>folders</key><array>" + response + "</array></map></llsd>";
651 }
652
653 //m_log.DebugFormat("[CAPS]: Replying to CAPS fetch inventory request with following xml");
654 //m_log.Debug("[CAPS] "+response);
655
656 }
657 return response;
658 }
659
660
661
662 /// <summary>
663 /// Construct an LLSD reply packet to a CAPS inventory request
664 /// </summary>
665 /// <param name="invFetch"></param>
666 /// <returns></returns>
667 private LLSDInventoryDescendents FetchInventoryReply(LLSDFetchInventoryDescendents invFetch)
668 {
669 LLSDInventoryDescendents reply = new LLSDInventoryDescendents();
670 LLSDInventoryFolderContents contents = new LLSDInventoryFolderContents();
671 contents.agent_id = m_HostCapsObj.AgentID;
672 contents.owner_id = invFetch.owner_id;
673 contents.folder_id = invFetch.folder_id;
674
675 reply.folders.Array.Add(contents);
676 InventoryCollection inv = new InventoryCollection();
677 inv.Folders = new List<InventoryFolderBase>();
678 inv.Items = new List<InventoryItemBase>();
679 int version = 0;
680 if (CAPSFetchInventoryDescendents != null)
681 {
682 inv = CAPSFetchInventoryDescendents(m_HostCapsObj.AgentID, invFetch.folder_id, invFetch.owner_id, invFetch.fetch_folders, invFetch.fetch_items, invFetch.sort_order, out version);
683 }
684
685 if (inv.Folders != null)
686 {
687 foreach (InventoryFolderBase invFolder in inv.Folders)
688 {
689 contents.categories.Array.Add(ConvertInventoryFolder(invFolder));
690 }
691 }
692
693 if (inv.Items != null)
694 {
695 foreach (InventoryItemBase invItem in inv.Items)
696 {
697 contents.items.Array.Add(ConvertInventoryItem(invItem));
698 }
699 }
700
701 contents.descendents = contents.items.Array.Count + contents.categories.Array.Count;
702 contents.version = version;
703
704 return reply;
705 }
706
707 /// <summary>
708 /// Convert an internal inventory folder object into an LLSD object.
709 /// </summary>
710 /// <param name="invFolder"></param>
711 /// <returns></returns>
712 private LLSDInventoryFolder ConvertInventoryFolder(InventoryFolderBase invFolder)
713 {
714 LLSDInventoryFolder llsdFolder = new LLSDInventoryFolder();
715 llsdFolder.folder_id = invFolder.ID;
716 llsdFolder.parent_id = invFolder.ParentID;
717 llsdFolder.name = invFolder.Name;
718 if (invFolder.Type < 0 || invFolder.Type >= TaskInventoryItem.Types.Length)
719 llsdFolder.type = "-1";
720 else
721 llsdFolder.type = TaskInventoryItem.Types[invFolder.Type];
722 llsdFolder.preferred_type = "-1";
723
724 return llsdFolder;
725 }
726
727 /// <summary>
728 /// Convert an internal inventory item object into an LLSD object.
729 /// </summary>
730 /// <param name="invItem"></param>
731 /// <returns></returns>
732 private LLSDInventoryItem ConvertInventoryItem(InventoryItemBase invItem)
733 {
734 LLSDInventoryItem llsdItem = new LLSDInventoryItem();
735 llsdItem.asset_id = invItem.AssetID;
736 llsdItem.created_at = invItem.CreationDate;
737 llsdItem.desc = invItem.Description;
738 llsdItem.flags = (int)invItem.Flags;
739 llsdItem.item_id = invItem.ID;
740 llsdItem.name = invItem.Name;
741 llsdItem.parent_id = invItem.Folder;
742 try
743 {
744 // TODO reevaluate after upgrade to libomv >= r2566. Probably should use UtilsConversions.
745 llsdItem.type = TaskInventoryItem.Types[invItem.AssetType];
746 llsdItem.inv_type = TaskInventoryItem.InvTypes[invItem.InvType];
747 }
748 catch (Exception e)
749 {
750 m_log.Error("[CAPS]: Problem setting asset/inventory type while converting inventory item " + invItem.Name + " to LLSD:", e);
751 }
752 llsdItem.permissions = new LLSDPermissions();
753 llsdItem.permissions.creator_id = invItem.CreatorIdAsUuid;
754 llsdItem.permissions.base_mask = (int)invItem.CurrentPermissions;
755 llsdItem.permissions.everyone_mask = (int)invItem.EveryOnePermissions;
756 llsdItem.permissions.group_id = invItem.GroupID;
757 llsdItem.permissions.group_mask = (int)invItem.GroupPermissions;
758 llsdItem.permissions.is_owner_group = invItem.GroupOwned;
759 llsdItem.permissions.next_owner_mask = (int)invItem.NextPermissions;
760 llsdItem.permissions.owner_id = m_HostCapsObj.AgentID;
761 llsdItem.permissions.owner_mask = (int)invItem.CurrentPermissions;
762 llsdItem.sale_info = new LLSDSaleInfo();
763 llsdItem.sale_info.sale_price = invItem.SalePrice;
764 switch (invItem.SaleType)
765 {
766 default:
767 llsdItem.sale_info.sale_type = "not";
768 break;
769 case 1:
770 llsdItem.sale_info.sale_type = "original";
771 break;
772 case 2:
773 llsdItem.sale_info.sale_type = "copy";
774 break;
775 case 3:
776 llsdItem.sale_info.sale_type = "contents";
777 break;
778 }
779
780 return llsdItem;
781 }
782
783 /// <summary>
784 ///
785 /// </summary>
786 /// <param name="mapReq"></param>
787 /// <returns></returns>
788 public LLSDMapLayerResponse GetMapLayer(LLSDMapRequest mapReq)
789 {
790 m_log.Debug("[CAPS]: MapLayer Request in region: " + m_regionName);
791 LLSDMapLayerResponse mapResponse = new LLSDMapLayerResponse();
792 mapResponse.LayerData.Array.Add(GetOSDMapLayerResponse());
793 return mapResponse;
794 }
795
796 /// <summary>
797 ///
798 /// </summary>
799 /// <returns></returns>
800 protected static OSDMapLayer GetOSDMapLayerResponse()
801 {
802 OSDMapLayer mapLayer = new OSDMapLayer();
803 mapLayer.Right = 5000;
804 mapLayer.Top = 5000;
805 mapLayer.ImageID = new UUID("00000000-0000-1111-9999-000000000006");
806
807 return mapLayer;
808 }
809
810 /// <summary>
811 ///
812 /// </summary>
813 /// <param name="request"></param>
814 /// <param name="path"></param>
815 /// <param name="param"></param>
816 /// <returns></returns>
817 public string RequestTexture(string request, string path, string param)
818 {
819 m_log.Debug("texture request " + request);
820 // Needs implementing (added to remove compiler warning)
821 return String.Empty;
822 }
823
824
825 /// <summary>
826 /// Called by the notecard update handler. Provides a URL to which the client can upload a new asset.
827 /// </summary>
828 /// <param name="request"></param>
829 /// <param name="path"></param>
830 /// <param name="param"></param>
831 /// <returns></returns>
832 public string NoteCardAgentInventory(string request, string path, string param,
833 OSHttpRequest httpRequest, OSHttpResponse httpResponse)
834 {
835 //m_log.Debug("[CAPS]: NoteCardAgentInventory Request in region: " + m_regionName + "\n" + request);
836 //m_log.Debug("[CAPS]: NoteCardAgentInventory Request is: " + request);
837
838 //OpenMetaverse.StructuredData.OSDMap hash = (OpenMetaverse.StructuredData.OSDMap)OpenMetaverse.StructuredData.LLSDParser.DeserializeBinary(Utils.StringToBytes(request));
839 Hashtable hash = (Hashtable)LLSD.LLSDDeserialize(Utils.StringToBytes(request));
840 LLSDItemUpdate llsdRequest = new LLSDItemUpdate();
841 LLSDHelpers.DeserialiseOSDMap(hash, llsdRequest);
842
843 string capsBase = "/CAPS/" + m_HostCapsObj.CapsObjectPath;
844 string uploaderPath = Util.RandomClass.Next(5000, 8000).ToString("0000");
845
846 ItemUpdater uploader =
847 new ItemUpdater(llsdRequest.item_id, capsBase + uploaderPath, m_HostCapsObj.HttpListener, m_dumpAssetsToFile);
848 uploader.OnUpLoad += ItemUpdated;
849
850 m_HostCapsObj.HttpListener.AddStreamHandler(
851 new BinaryStreamHandler("POST", capsBase + uploaderPath, uploader.uploaderCaps));
852
853 string protocol = "http://";
854
855 if (m_HostCapsObj.SSLCaps)
856 protocol = "https://";
857
858 string uploaderURL = protocol + m_HostCapsObj.HostName + ":" + m_HostCapsObj.Port.ToString() + capsBase +
859 uploaderPath;
860
861 LLSDAssetUploadResponse uploadResponse = new LLSDAssetUploadResponse();
862 uploadResponse.uploader = uploaderURL;
863 uploadResponse.state = "upload";
864
865 // m_log.InfoFormat("[CAPS]: " +
866 // "NoteCardAgentInventory response: {0}",
867 // LLSDHelpers.SerialiseLLSDReply(uploadResponse)));
868
869 return LLSDHelpers.SerialiseLLSDReply(uploadResponse);
870 }
871 }
872
873 public class AssetUploader
874 {
875 public event UpLoadedAsset OnUpLoad;
876 private UpLoadedAsset handlerUpLoad = null;
877
878 private string uploaderPath = String.Empty;
879 private UUID newAssetID;
880 private UUID inventoryItemID;
881 private UUID parentFolder;
882 private IHttpServer httpListener;
883 private bool m_dumpAssetsToFile;
884 private string m_assetName = String.Empty;
885 private string m_assetDes = String.Empty;
886
887 private string m_invType = String.Empty;
888 private string m_assetType = String.Empty;
889
890 public AssetUploader(string assetName, string description, UUID assetID, UUID inventoryItem,
891 UUID parentFolderID, string invType, string assetType, string path,
892 IHttpServer httpServer, bool dumpAssetsToFile)
893 {
894 m_assetName = assetName;
895 m_assetDes = description;
896 newAssetID = assetID;
897 inventoryItemID = inventoryItem;
898 uploaderPath = path;
899 httpListener = httpServer;
900 parentFolder = parentFolderID;
901 m_assetType = assetType;
902 m_invType = invType;
903 m_dumpAssetsToFile = dumpAssetsToFile;
904 }
905
906 /// <summary>
907 ///
908 /// </summary>
909 /// <param name="data"></param>
910 /// <param name="path"></param>
911 /// <param name="param"></param>
912 /// <returns></returns>
913 public string uploaderCaps(byte[] data, string path, string param)
914 {
915 UUID inv = inventoryItemID;
916 string res = String.Empty;
917 LLSDAssetUploadComplete uploadComplete = new LLSDAssetUploadComplete();
918 uploadComplete.new_asset = newAssetID.ToString();
919 uploadComplete.new_inventory_item = inv;
920 uploadComplete.state = "complete";
921
922 res = LLSDHelpers.SerialiseLLSDReply(uploadComplete);
923
924 httpListener.RemoveStreamHandler("POST", uploaderPath);
925
926 // TODO: probably make this a better set of extensions here
927 string extension = ".jp2";
928 if (m_invType != "image")
929 {
930 extension = ".dat";
931 }
932
933 if (m_dumpAssetsToFile)
934 {
935 SaveAssetToFile(m_assetName + extension, data);
936 }
937 handlerUpLoad = OnUpLoad;
938 if (handlerUpLoad != null)
939 {
940 handlerUpLoad(m_assetName, m_assetDes, newAssetID, inv, parentFolder, data, m_invType, m_assetType);
941 }
942
943 return res;
944 }
945 ///Left this in and commented in case there are unforseen issues
946 //private void SaveAssetToFile(string filename, byte[] data)
947 //{
948 // FileStream fs = File.Create(filename);
949 // BinaryWriter bw = new BinaryWriter(fs);
950 // bw.Write(data);
951 // bw.Close();
952 // fs.Close();
953 //}
954 private static void SaveAssetToFile(string filename, byte[] data)
955 {
956 string assetPath = "UserAssets";
957 if (!Directory.Exists(assetPath))
958 {
959 Directory.CreateDirectory(assetPath);
960 }
961 FileStream fs = File.Create(Path.Combine(assetPath, Util.safeFileName(filename)));
962 BinaryWriter bw = new BinaryWriter(fs);
963 bw.Write(data);
964 bw.Close();
965 fs.Close();
966 }
967 }
968
969 /// <summary>
970 /// This class is a callback invoked when a client sends asset data to
971 /// an agent inventory notecard update url
972 /// </summary>
973 public class ItemUpdater
974 {
975 public event UpdateItem OnUpLoad;
976
977 private UpdateItem handlerUpdateItem = null;
978
979 private string uploaderPath = String.Empty;
980 private UUID inventoryItemID;
981 private IHttpServer httpListener;
982 private bool m_dumpAssetToFile;
983
984 public ItemUpdater(UUID inventoryItem, string path, IHttpServer httpServer, bool dumpAssetToFile)
985 {
986 m_dumpAssetToFile = dumpAssetToFile;
987
988 inventoryItemID = inventoryItem;
989 uploaderPath = path;
990 httpListener = httpServer;
991 }
992
993 /// <summary>
994 ///
995 /// </summary>
996 /// <param name="data"></param>
997 /// <param name="path"></param>
998 /// <param name="param"></param>
999 /// <returns></returns>
1000 public string uploaderCaps(byte[] data, string path, string param)
1001 {
1002 UUID inv = inventoryItemID;
1003 string res = String.Empty;
1004 LLSDAssetUploadComplete uploadComplete = new LLSDAssetUploadComplete();
1005 UUID assetID = UUID.Zero;
1006 handlerUpdateItem = OnUpLoad;
1007 if (handlerUpdateItem != null)
1008 {
1009 assetID = handlerUpdateItem(inv, data);
1010 }
1011
1012 uploadComplete.new_asset = assetID.ToString();
1013 uploadComplete.new_inventory_item = inv;
1014 uploadComplete.state = "complete";
1015
1016 res = LLSDHelpers.SerialiseLLSDReply(uploadComplete);
1017
1018 httpListener.RemoveStreamHandler("POST", uploaderPath);
1019
1020 if (m_dumpAssetToFile)
1021 {
1022 SaveAssetToFile("updateditem" + Util.RandomClass.Next(1, 1000) + ".dat", data);
1023 }
1024
1025 return res;
1026 }
1027 ///Left this in and commented in case there are unforseen issues
1028 //private void SaveAssetToFile(string filename, byte[] data)
1029 //{
1030 // FileStream fs = File.Create(filename);
1031 // BinaryWriter bw = new BinaryWriter(fs);
1032 // bw.Write(data);
1033 // bw.Close();
1034 // fs.Close();
1035 //}
1036 private static void SaveAssetToFile(string filename, byte[] data)
1037 {
1038 string assetPath = "UserAssets";
1039 if (!Directory.Exists(assetPath))
1040 {
1041 Directory.CreateDirectory(assetPath);
1042 }
1043 FileStream fs = File.Create(Path.Combine(assetPath, filename));
1044 BinaryWriter bw = new BinaryWriter(fs);
1045 bw.Write(data);
1046 bw.Close();
1047 fs.Close();
1048 }
1049 }
1050
1051 /// <summary>
1052 /// This class is a callback invoked when a client sends asset data to
1053 /// a task inventory script update url
1054 /// </summary>
1055 public class TaskInventoryScriptUpdater
1056 {
1057 private static readonly ILog m_log =
1058 LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
1059
1060 public event UpdateTaskScript OnUpLoad;
1061
1062 private UpdateTaskScript handlerUpdateTaskScript = null;
1063
1064 private string uploaderPath = String.Empty;
1065 private UUID inventoryItemID;
1066 private UUID primID;
1067 private bool isScriptRunning;
1068 private IHttpServer httpListener;
1069 private bool m_dumpAssetToFile;
1070
1071 public TaskInventoryScriptUpdater(UUID inventoryItemID, UUID primID, int isScriptRunning,
1072 string path, IHttpServer httpServer, bool dumpAssetToFile)
1073 {
1074 m_dumpAssetToFile = dumpAssetToFile;
1075
1076 this.inventoryItemID = inventoryItemID;
1077 this.primID = primID;
1078
1079 // This comes in over the packet as an integer, but actually appears to be treated as a bool
1080 this.isScriptRunning = (0 == isScriptRunning ? false : true);
1081
1082 uploaderPath = path;
1083 httpListener = httpServer;
1084 }
1085
1086 /// <summary>
1087 ///
1088 /// </summary>
1089 /// <param name="data"></param>
1090 /// <param name="path"></param>
1091 /// <param name="param"></param>
1092 /// <returns></returns>
1093 public string uploaderCaps(byte[] data, string path, string param)
1094 {
1095 try
1096 {
1097 // m_log.InfoFormat("[CAPS]: " +
1098 // "TaskInventoryScriptUpdater received data: {0}, path: {1}, param: {2}",
1099 // data, path, param));
1100
1101 string res = String.Empty;
1102 LLSDTaskScriptUploadComplete uploadComplete = new LLSDTaskScriptUploadComplete();
1103
1104 ArrayList errors = new ArrayList();
1105 handlerUpdateTaskScript = OnUpLoad;
1106 if (handlerUpdateTaskScript != null)
1107 {
1108 handlerUpdateTaskScript(inventoryItemID, primID, isScriptRunning, data, ref errors);
1109 }
1110
1111 uploadComplete.new_asset = inventoryItemID;
1112 uploadComplete.compiled = errors.Count > 0 ? false : true;
1113 uploadComplete.state = "complete";
1114 uploadComplete.errors = new OSDArray();
1115 uploadComplete.errors.Array = errors;
1116
1117 res = LLSDHelpers.SerialiseLLSDReply(uploadComplete);
1118
1119 httpListener.RemoveStreamHandler("POST", uploaderPath);
1120
1121 if (m_dumpAssetToFile)
1122 {
1123 SaveAssetToFile("updatedtaskscript" + Util.RandomClass.Next(1, 1000) + ".dat", data);
1124 }
1125
1126 // m_log.InfoFormat("[CAPS]: TaskInventoryScriptUpdater.uploaderCaps res: {0}", res);
1127
1128 return res;
1129 }
1130 catch (Exception e)
1131 {
1132 m_log.Error("[CAPS]: " + e.ToString());
1133 }
1134
1135 // XXX Maybe this should be some meaningful error packet
1136 return null;
1137 }
1138 ///Left this in and commented in case there are unforseen issues
1139 //private void SaveAssetToFile(string filename, byte[] data)
1140 //{
1141 // FileStream fs = File.Create(filename);
1142 // BinaryWriter bw = new BinaryWriter(fs);
1143 // bw.Write(data);
1144 // bw.Close();
1145 // fs.Close();
1146 //}
1147 private static void SaveAssetToFile(string filename, byte[] data)
1148 {
1149 string assetPath = "UserAssets";
1150 if (!Directory.Exists(assetPath))
1151 {
1152 Directory.CreateDirectory(assetPath);
1153 }
1154 FileStream fs = File.Create(Path.Combine(assetPath, filename));
1155 BinaryWriter bw = new BinaryWriter(fs);
1156 bw.Write(data);
1157 bw.Close();
1158 fs.Close();
1159 }
1160 }
1161
1162 public class BakedTextureUploader
1163 {
1164 public event UploadedBakedTexture OnUpLoad;
1165 private UploadedBakedTexture handlerUpLoad = null;
1166
1167 private string uploaderPath = String.Empty;
1168 private UUID newAssetID;
1169 private IHttpServer httpListener;
1170
1171 public BakedTextureUploader(string path, IHttpServer httpServer)
1172 {
1173 newAssetID = UUID.Random();
1174 uploaderPath = path;
1175 httpListener = httpServer;
1176 // m_log.InfoFormat("[CAPS] baked texture upload starting for {0}",newAssetID);
1177 }
1178
1179 /// <summary>
1180 ///
1181 /// </summary>
1182 /// <param name="data"></param>
1183 /// <param name="path"></param>
1184 /// <param name="param"></param>
1185 /// <returns></returns>
1186 public string uploaderCaps(byte[] data, string path, string param)
1187 {
1188 handlerUpLoad = OnUpLoad;
1189 if (handlerUpLoad != null)
1190 {
1191 Util.FireAndForget(delegate(object o) { handlerUpLoad(newAssetID, data); });
1192 }
1193
1194 string res = String.Empty;
1195 LLSDAssetUploadComplete uploadComplete = new LLSDAssetUploadComplete();
1196 uploadComplete.new_asset = newAssetID.ToString();
1197 uploadComplete.new_inventory_item = UUID.Zero;
1198 uploadComplete.state = "complete";
1199
1200 res = LLSDHelpers.SerialiseLLSDReply(uploadComplete);
1201
1202 httpListener.RemoveStreamHandler("POST", uploaderPath);
1203
1204 // m_log.InfoFormat("[CAPS] baked texture upload completed for {0}",newAssetID);
1205
1206 return res;
1207 }
1208 }
1209
1210}