diff options
Diffstat (limited to 'OpenSim/Services/Connectors/SimianGrid/SimianInventoryServiceConnector.cs')
-rw-r--r-- | OpenSim/Services/Connectors/SimianGrid/SimianInventoryServiceConnector.cs | 895 |
1 files changed, 895 insertions, 0 deletions
diff --git a/OpenSim/Services/Connectors/SimianGrid/SimianInventoryServiceConnector.cs b/OpenSim/Services/Connectors/SimianGrid/SimianInventoryServiceConnector.cs new file mode 100644 index 0000000..891782f --- /dev/null +++ b/OpenSim/Services/Connectors/SimianGrid/SimianInventoryServiceConnector.cs | |||
@@ -0,0 +1,895 @@ | |||
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 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.Collections.Specialized; | ||
31 | using System.Reflection; | ||
32 | using log4net; | ||
33 | using Mono.Addins; | ||
34 | using Nini.Config; | ||
35 | using OpenMetaverse; | ||
36 | using OpenMetaverse.StructuredData; | ||
37 | using OpenSim.Framework; | ||
38 | using OpenSim.Region.Framework.Interfaces; | ||
39 | using OpenSim.Region.Framework.Scenes; | ||
40 | using OpenSim.Server.Base; | ||
41 | using OpenSim.Services.Interfaces; | ||
42 | |||
43 | namespace OpenSim.Services.Connectors.SimianGrid | ||
44 | { | ||
45 | /// <summary> | ||
46 | /// Permissions bitflags | ||
47 | /// </summary> | ||
48 | [Flags] | ||
49 | public enum PermissionMask : uint | ||
50 | { | ||
51 | None = 0, | ||
52 | Transfer = 1 << 13, | ||
53 | Modify = 1 << 14, | ||
54 | Copy = 1 << 15, | ||
55 | Move = 1 << 19, | ||
56 | Damage = 1 << 20, | ||
57 | All = 0x7FFFFFFF | ||
58 | } | ||
59 | |||
60 | /// <summary> | ||
61 | /// Connects avatar inventories to the SimianGrid backend | ||
62 | /// </summary> | ||
63 | [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule")] | ||
64 | public class SimianInventoryServiceConnector : IInventoryService, ISharedRegionModule | ||
65 | { | ||
66 | private static readonly ILog m_log = | ||
67 | LogManager.GetLogger( | ||
68 | MethodBase.GetCurrentMethod().DeclaringType); | ||
69 | |||
70 | private string m_serverUrl = String.Empty; | ||
71 | private string m_userServerUrl = String.Empty; | ||
72 | private object m_gestureSyncRoot = new object(); | ||
73 | |||
74 | #region ISharedRegionModule | ||
75 | |||
76 | public Type ReplaceableInterface { get { return null; } } | ||
77 | public void RegionLoaded(Scene scene) { } | ||
78 | public void PostInitialise() { } | ||
79 | public void Close() { } | ||
80 | |||
81 | public SimianInventoryServiceConnector() { } | ||
82 | public string Name { get { return "SimianInventoryServiceConnector"; } } | ||
83 | public void AddRegion(Scene scene) { if (!String.IsNullOrEmpty(m_serverUrl)) { scene.RegisterModuleInterface<IInventoryService>(this); } } | ||
84 | public void RemoveRegion(Scene scene) { if (!String.IsNullOrEmpty(m_serverUrl)) { scene.UnregisterModuleInterface<IInventoryService>(this); } } | ||
85 | |||
86 | #endregion ISharedRegionModule | ||
87 | |||
88 | public SimianInventoryServiceConnector(IConfigSource source) | ||
89 | { | ||
90 | Initialise(source); | ||
91 | } | ||
92 | |||
93 | public void Initialise(IConfigSource source) | ||
94 | { | ||
95 | if (Simian.IsSimianEnabled(source, "InventoryServices")) | ||
96 | { | ||
97 | IConfig gridConfig = source.Configs["InventoryService"]; | ||
98 | if (gridConfig == null) | ||
99 | { | ||
100 | m_log.Error("[INVENTORY CONNECTOR]: InventoryService missing from OpenSim.ini"); | ||
101 | throw new Exception("Inventory connector init error"); | ||
102 | } | ||
103 | |||
104 | string serviceUrl = gridConfig.GetString("InventoryServerURI"); | ||
105 | if (String.IsNullOrEmpty(serviceUrl)) | ||
106 | { | ||
107 | m_log.Error("[INVENTORY CONNECTOR]: No Server URI named in section InventoryService"); | ||
108 | throw new Exception("Inventory connector init error"); | ||
109 | } | ||
110 | |||
111 | m_serverUrl = serviceUrl; | ||
112 | |||
113 | gridConfig = source.Configs["UserAccountService"]; | ||
114 | if (gridConfig != null) | ||
115 | { | ||
116 | serviceUrl = gridConfig.GetString("UserAccountServerURI"); | ||
117 | if (!String.IsNullOrEmpty(serviceUrl)) | ||
118 | m_userServerUrl = serviceUrl; | ||
119 | else | ||
120 | m_log.Info("[INVENTORY CONNECTOR]: No Server URI named in section UserAccountService"); | ||
121 | } | ||
122 | else | ||
123 | { | ||
124 | m_log.Warn("[INVENTORY CONNECTOR]: UserAccountService missing from OpenSim.ini"); | ||
125 | } | ||
126 | } | ||
127 | } | ||
128 | |||
129 | /// <summary> | ||
130 | /// Create the entire inventory for a given user | ||
131 | /// </summary> | ||
132 | /// <param name="user"></param> | ||
133 | /// <returns></returns> | ||
134 | public bool CreateUserInventory(UUID userID) | ||
135 | { | ||
136 | NameValueCollection requestArgs = new NameValueCollection | ||
137 | { | ||
138 | { "RequestMethod", "AddInventory" }, | ||
139 | { "OwnerID", userID.ToString() } | ||
140 | }; | ||
141 | |||
142 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
143 | bool success = response["Success"].AsBoolean(); | ||
144 | |||
145 | if (!success) | ||
146 | m_log.Warn("[INVENTORY CONNECTOR]: Inventory creation for " + userID + " failed: " + response["Message"].AsString()); | ||
147 | |||
148 | return success; | ||
149 | } | ||
150 | |||
151 | /// <summary> | ||
152 | /// Gets the skeleton of the inventory -- folders only | ||
153 | /// </summary> | ||
154 | /// <param name="userID"></param> | ||
155 | /// <returns></returns> | ||
156 | public List<InventoryFolderBase> GetInventorySkeleton(UUID userID) | ||
157 | { | ||
158 | NameValueCollection requestArgs = new NameValueCollection | ||
159 | { | ||
160 | { "RequestMethod", "GetInventoryNode" }, | ||
161 | { "ItemID", userID.ToString() }, | ||
162 | { "OwnerID", userID.ToString() }, | ||
163 | { "IncludeFolders", "1" }, | ||
164 | { "IncludeItems", "0" }, | ||
165 | { "ChildrenOnly", "0" } | ||
166 | }; | ||
167 | |||
168 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
169 | if (response["Success"].AsBoolean() && response["Items"] is OSDArray) | ||
170 | { | ||
171 | OSDArray items = (OSDArray)response["Items"]; | ||
172 | return GetFoldersFromResponse(items, userID, true); | ||
173 | } | ||
174 | else | ||
175 | { | ||
176 | m_log.Warn("[INVENTORY CONNECTOR]: Failed to retrieve inventory skeleton for " + userID + ": " + | ||
177 | response["Message"].AsString()); | ||
178 | return new List<InventoryFolderBase>(0); | ||
179 | } | ||
180 | } | ||
181 | |||
182 | /// <summary> | ||
183 | /// Synchronous inventory fetch. | ||
184 | /// </summary> | ||
185 | /// <param name="userID"></param> | ||
186 | /// <returns></returns> | ||
187 | [Obsolete] | ||
188 | public InventoryCollection GetUserInventory(UUID userID) | ||
189 | { | ||
190 | m_log.Error("[INVENTORY CONNECTOR]: Obsolete GetUserInventory called for " + userID); | ||
191 | |||
192 | InventoryCollection inventory = new InventoryCollection(); | ||
193 | inventory.UserID = userID; | ||
194 | inventory.Folders = new List<InventoryFolderBase>(); | ||
195 | inventory.Items = new List<InventoryItemBase>(); | ||
196 | |||
197 | return inventory; | ||
198 | } | ||
199 | |||
200 | /// <summary> | ||
201 | /// Request the inventory for a user. This is an asynchronous operation that will call the callback when the | ||
202 | /// inventory has been received | ||
203 | /// </summary> | ||
204 | /// <param name="userID"></param> | ||
205 | /// <param name="callback"></param> | ||
206 | [Obsolete] | ||
207 | public void GetUserInventory(UUID userID, InventoryReceiptCallback callback) | ||
208 | { | ||
209 | m_log.Error("[INVENTORY CONNECTOR]: Obsolete GetUserInventory called for " + userID); | ||
210 | callback(new List<InventoryFolderImpl>(0), new List<InventoryItemBase>(0)); | ||
211 | } | ||
212 | |||
213 | /// <summary> | ||
214 | /// Retrieve the root inventory folder for the given user. | ||
215 | /// </summary> | ||
216 | /// <param name="userID"></param> | ||
217 | /// <returns>null if no root folder was found</returns> | ||
218 | public InventoryFolderBase GetRootFolder(UUID userID) | ||
219 | { | ||
220 | NameValueCollection requestArgs = new NameValueCollection | ||
221 | { | ||
222 | { "RequestMethod", "GetInventoryNode" }, | ||
223 | { "ItemID", userID.ToString() }, | ||
224 | { "OwnerID", userID.ToString() }, | ||
225 | { "IncludeFolders", "1" }, | ||
226 | { "IncludeItems", "0" }, | ||
227 | { "ChildrenOnly", "1" } | ||
228 | }; | ||
229 | |||
230 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
231 | if (response["Success"].AsBoolean() && response["Items"] is OSDArray) | ||
232 | { | ||
233 | OSDArray items = (OSDArray)response["Items"]; | ||
234 | List<InventoryFolderBase> folders = GetFoldersFromResponse(items, userID, true); | ||
235 | |||
236 | if (folders.Count > 0) | ||
237 | return folders[0]; | ||
238 | } | ||
239 | |||
240 | return null; | ||
241 | } | ||
242 | |||
243 | /// <summary> | ||
244 | /// Gets the user folder for the given folder-type | ||
245 | /// </summary> | ||
246 | /// <param name="userID"></param> | ||
247 | /// <param name="type"></param> | ||
248 | /// <returns></returns> | ||
249 | public InventoryFolderBase GetFolderForType(UUID userID, AssetType type) | ||
250 | { | ||
251 | string contentType = SLUtil.SLAssetTypeToContentType((int)type); | ||
252 | |||
253 | NameValueCollection requestArgs = new NameValueCollection | ||
254 | { | ||
255 | { "RequestMethod", "GetFolderForType" }, | ||
256 | { "ContentType", contentType }, | ||
257 | { "OwnerID", userID.ToString() } | ||
258 | }; | ||
259 | |||
260 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
261 | if (response["Success"].AsBoolean() && response["Folder"] is OSDMap) | ||
262 | { | ||
263 | OSDMap folder = (OSDMap)response["Folder"]; | ||
264 | |||
265 | return new InventoryFolderBase( | ||
266 | folder["ID"].AsUUID(), | ||
267 | folder["Name"].AsString(), | ||
268 | folder["OwnerID"].AsUUID(), | ||
269 | (short)SLUtil.ContentTypeToSLAssetType(folder["ContentType"].AsString()), | ||
270 | folder["ParentID"].AsUUID(), | ||
271 | (ushort)folder["Version"].AsInteger() | ||
272 | ); | ||
273 | } | ||
274 | else | ||
275 | { | ||
276 | m_log.Warn("[INVENTORY CONNECTOR]: Default folder not found for content type " + contentType); | ||
277 | return GetRootFolder(userID); | ||
278 | } | ||
279 | } | ||
280 | |||
281 | /// <summary> | ||
282 | /// Get an item, given by its UUID | ||
283 | /// </summary> | ||
284 | /// <param name="item"></param> | ||
285 | /// <returns></returns> | ||
286 | public InventoryItemBase GetItem(InventoryItemBase item) | ||
287 | { | ||
288 | NameValueCollection requestArgs = new NameValueCollection | ||
289 | { | ||
290 | { "RequestMethod", "GetInventoryNode" }, | ||
291 | { "ItemID", item.ID.ToString() }, | ||
292 | { "OwnerID", item.Owner.ToString() }, | ||
293 | { "IncludeFolders", "1" }, | ||
294 | { "IncludeItems", "1" }, | ||
295 | { "ChildrenOnly", "1" } | ||
296 | }; | ||
297 | |||
298 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
299 | if (response["Success"].AsBoolean() && response["Items"] is OSDArray) | ||
300 | { | ||
301 | List<InventoryItemBase> items = GetItemsFromResponse((OSDArray)response["Items"]); | ||
302 | if (items.Count > 0) | ||
303 | { | ||
304 | // The requested item should be the first in this list, but loop through | ||
305 | // and sanity check just in case | ||
306 | for (int i = 0; i < items.Count; i++) | ||
307 | { | ||
308 | if (items[i].ID == item.ID) | ||
309 | return items[i]; | ||
310 | } | ||
311 | } | ||
312 | } | ||
313 | |||
314 | m_log.Warn("[INVENTORY CONNECTOR]: Item " + item.ID + " owned by " + item.Owner + " not found"); | ||
315 | return null; | ||
316 | } | ||
317 | |||
318 | /// <summary> | ||
319 | /// Get a folder, given by its UUID | ||
320 | /// </summary> | ||
321 | /// <param name="folder"></param> | ||
322 | /// <returns></returns> | ||
323 | public InventoryFolderBase GetFolder(InventoryFolderBase folder) | ||
324 | { | ||
325 | NameValueCollection requestArgs = new NameValueCollection | ||
326 | { | ||
327 | { "RequestMethod", "GetInventoryNode" }, | ||
328 | { "ItemID", folder.ID.ToString() }, | ||
329 | { "OwnerID", folder.Owner.ToString() }, | ||
330 | { "IncludeFolders", "1" }, | ||
331 | { "IncludeItems", "0" }, | ||
332 | { "ChildrenOnly", "1" } | ||
333 | }; | ||
334 | |||
335 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
336 | if (response["Success"].AsBoolean() && response["Items"] is OSDArray) | ||
337 | { | ||
338 | OSDArray items = (OSDArray)response["Items"]; | ||
339 | List<InventoryFolderBase> folders = GetFoldersFromResponse(items, folder.ID, true); | ||
340 | |||
341 | if (folders.Count > 0) | ||
342 | return folders[0]; | ||
343 | } | ||
344 | |||
345 | return null; | ||
346 | } | ||
347 | |||
348 | /// <summary> | ||
349 | /// Gets everything (folders and items) inside a folder | ||
350 | /// </summary> | ||
351 | /// <param name="userID"></param> | ||
352 | /// <param name="folderID"></param> | ||
353 | /// <returns></returns> | ||
354 | public InventoryCollection GetFolderContent(UUID userID, UUID folderID) | ||
355 | { | ||
356 | InventoryCollection inventory = new InventoryCollection(); | ||
357 | inventory.UserID = userID; | ||
358 | |||
359 | NameValueCollection requestArgs = new NameValueCollection | ||
360 | { | ||
361 | { "RequestMethod", "GetInventoryNode" }, | ||
362 | { "ItemID", folderID.ToString() }, | ||
363 | { "OwnerID", userID.ToString() }, | ||
364 | { "IncludeFolders", "1" }, | ||
365 | { "IncludeItems", "1" }, | ||
366 | { "ChildrenOnly", "1" } | ||
367 | }; | ||
368 | |||
369 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
370 | if (response["Success"].AsBoolean() && response["Items"] is OSDArray) | ||
371 | { | ||
372 | OSDArray items = (OSDArray)response["Items"]; | ||
373 | |||
374 | inventory.Folders = GetFoldersFromResponse(items, folderID, false); | ||
375 | inventory.Items = GetItemsFromResponse(items); | ||
376 | } | ||
377 | else | ||
378 | { | ||
379 | m_log.Warn("[INVENTORY CONNECTOR]: Error fetching folder " + folderID + " content for " + userID + ": " + | ||
380 | response["Message"].AsString()); | ||
381 | inventory.Folders = new List<InventoryFolderBase>(0); | ||
382 | inventory.Items = new List<InventoryItemBase>(0); | ||
383 | } | ||
384 | |||
385 | return inventory; | ||
386 | } | ||
387 | |||
388 | /// <summary> | ||
389 | /// Gets the items inside a folder | ||
390 | /// </summary> | ||
391 | /// <param name="userID"></param> | ||
392 | /// <param name="folderID"></param> | ||
393 | /// <returns></returns> | ||
394 | public List<InventoryItemBase> GetFolderItems(UUID userID, UUID folderID) | ||
395 | { | ||
396 | InventoryCollection inventory = new InventoryCollection(); | ||
397 | inventory.UserID = userID; | ||
398 | |||
399 | NameValueCollection requestArgs = new NameValueCollection | ||
400 | { | ||
401 | { "RequestMethod", "GetInventoryNode" }, | ||
402 | { "ItemID", folderID.ToString() }, | ||
403 | { "OwnerID", userID.ToString() }, | ||
404 | { "IncludeFolders", "0" }, | ||
405 | { "IncludeItems", "1" }, | ||
406 | { "ChildrenOnly", "1" } | ||
407 | }; | ||
408 | |||
409 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
410 | if (response["Success"].AsBoolean() && response["Items"] is OSDArray) | ||
411 | { | ||
412 | OSDArray items = (OSDArray)response["Items"]; | ||
413 | return GetItemsFromResponse(items); | ||
414 | } | ||
415 | else | ||
416 | { | ||
417 | m_log.Warn("[INVENTORY CONNECTOR]: Error fetching folder " + folderID + " for " + userID + ": " + | ||
418 | response["Message"].AsString()); | ||
419 | return new List<InventoryItemBase>(0); | ||
420 | } | ||
421 | } | ||
422 | |||
423 | /// <summary> | ||
424 | /// Add a new folder to the user's inventory | ||
425 | /// </summary> | ||
426 | /// <param name="folder"></param> | ||
427 | /// <returns>true if the folder was successfully added</returns> | ||
428 | public bool AddFolder(InventoryFolderBase folder) | ||
429 | { | ||
430 | NameValueCollection requestArgs = new NameValueCollection | ||
431 | { | ||
432 | { "RequestMethod", "AddInventoryFolder" }, | ||
433 | { "FolderID", folder.ID.ToString() }, | ||
434 | { "ParentID", folder.ParentID.ToString() }, | ||
435 | { "OwnerID", folder.Owner.ToString() }, | ||
436 | { "Name", folder.Name }, | ||
437 | { "ContentType", SLUtil.SLAssetTypeToContentType(folder.Type) } | ||
438 | }; | ||
439 | |||
440 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
441 | bool success = response["Success"].AsBoolean(); | ||
442 | |||
443 | if (!success) | ||
444 | { | ||
445 | m_log.Warn("[INVENTORY CONNECTOR]: Error creating folder " + folder.Name + " for " + folder.Owner + ": " + | ||
446 | response["Message"].AsString()); | ||
447 | } | ||
448 | |||
449 | return success; | ||
450 | } | ||
451 | |||
452 | /// <summary> | ||
453 | /// Update a folder in the user's inventory | ||
454 | /// </summary> | ||
455 | /// <param name="folder"></param> | ||
456 | /// <returns>true if the folder was successfully updated</returns> | ||
457 | public bool UpdateFolder(InventoryFolderBase folder) | ||
458 | { | ||
459 | return AddFolder(folder); | ||
460 | } | ||
461 | |||
462 | /// <summary> | ||
463 | /// Move an inventory folder to a new location | ||
464 | /// </summary> | ||
465 | /// <param name="folder">A folder containing the details of the new location</param> | ||
466 | /// <returns>true if the folder was successfully moved</returns> | ||
467 | public bool MoveFolder(InventoryFolderBase folder) | ||
468 | { | ||
469 | return AddFolder(folder); | ||
470 | } | ||
471 | |||
472 | /// <summary> | ||
473 | /// Delete an item from the user's inventory | ||
474 | /// </summary> | ||
475 | /// <param name="item"></param> | ||
476 | /// <returns>true if the item was successfully deleted</returns> | ||
477 | //bool DeleteItem(InventoryItemBase item); | ||
478 | public bool DeleteFolders(UUID userID, List<UUID> folderIDs) | ||
479 | { | ||
480 | return DeleteItems(userID, folderIDs); | ||
481 | } | ||
482 | |||
483 | /// <summary> | ||
484 | /// Delete an item from the user's inventory | ||
485 | /// </summary> | ||
486 | /// <param name="item"></param> | ||
487 | /// <returns>true if the item was successfully deleted</returns> | ||
488 | public bool DeleteItems(UUID userID, List<UUID> itemIDs) | ||
489 | { | ||
490 | // TODO: RemoveInventoryNode should be replaced with RemoveInventoryNodes | ||
491 | bool allSuccess = true; | ||
492 | |||
493 | for (int i = 0; i < itemIDs.Count; i++) | ||
494 | { | ||
495 | UUID itemID = itemIDs[i]; | ||
496 | |||
497 | NameValueCollection requestArgs = new NameValueCollection | ||
498 | { | ||
499 | { "RequestMethod", "RemoveInventoryNode" }, | ||
500 | { "OwnerID", userID.ToString() }, | ||
501 | { "ItemID", itemID.ToString() } | ||
502 | }; | ||
503 | |||
504 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
505 | bool success = response["Success"].AsBoolean(); | ||
506 | |||
507 | if (!success) | ||
508 | { | ||
509 | m_log.Warn("[INVENTORY CONNECTOR]: Error removing item " + itemID + " for " + userID + ": " + | ||
510 | response["Message"].AsString()); | ||
511 | allSuccess = false; | ||
512 | } | ||
513 | } | ||
514 | |||
515 | return allSuccess; | ||
516 | } | ||
517 | |||
518 | /// <summary> | ||
519 | /// Purge an inventory folder of all its items and subfolders. | ||
520 | /// </summary> | ||
521 | /// <param name="folder"></param> | ||
522 | /// <returns>true if the folder was successfully purged</returns> | ||
523 | public bool PurgeFolder(InventoryFolderBase folder) | ||
524 | { | ||
525 | NameValueCollection requestArgs = new NameValueCollection | ||
526 | { | ||
527 | { "RequestMethod", "PurgeInventoryFolder" }, | ||
528 | { "OwnerID", folder.Owner.ToString() }, | ||
529 | { "FolderID", folder.ID.ToString() } | ||
530 | }; | ||
531 | |||
532 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
533 | bool success = response["Success"].AsBoolean(); | ||
534 | |||
535 | if (!success) | ||
536 | { | ||
537 | m_log.Warn("[INVENTORY CONNECTOR]: Error purging folder " + folder.ID + " for " + folder.Owner + ": " + | ||
538 | response["Message"].AsString()); | ||
539 | } | ||
540 | |||
541 | return success; | ||
542 | } | ||
543 | |||
544 | /// <summary> | ||
545 | /// Add a new item to the user's inventory | ||
546 | /// </summary> | ||
547 | /// <param name="item"></param> | ||
548 | /// <returns>true if the item was successfully added</returns> | ||
549 | public bool AddItem(InventoryItemBase item) | ||
550 | { | ||
551 | // A folder of UUID.Zero means we need to find the most appropriate home for this item | ||
552 | if (item.Folder == UUID.Zero) | ||
553 | { | ||
554 | InventoryFolderBase folder = GetFolderForType(item.Owner, (AssetType)item.AssetType); | ||
555 | if (folder != null && folder.ID != UUID.Zero) | ||
556 | item.Folder = folder.ID; | ||
557 | else | ||
558 | item.Folder = item.Owner; // Root folder | ||
559 | } | ||
560 | |||
561 | if ((AssetType)item.AssetType == AssetType.Gesture) | ||
562 | UpdateGesture(item.Owner, item.ID, item.Flags == 1); | ||
563 | |||
564 | if (item.BasePermissions == 0) | ||
565 | m_log.WarnFormat("[INVENTORY CONNECTOR]: Adding inventory item {0} ({1}) with no base permissions", item.Name, item.ID); | ||
566 | |||
567 | OSDMap permissions = new OSDMap | ||
568 | { | ||
569 | { "BaseMask", OSD.FromInteger(item.BasePermissions) }, | ||
570 | { "EveryoneMask", OSD.FromInteger(item.EveryOnePermissions) }, | ||
571 | { "GroupMask", OSD.FromInteger(item.GroupPermissions) }, | ||
572 | { "NextOwnerMask", OSD.FromInteger(item.NextPermissions) }, | ||
573 | { "OwnerMask", OSD.FromInteger(item.CurrentPermissions) } | ||
574 | }; | ||
575 | |||
576 | OSDMap extraData = new OSDMap() | ||
577 | { | ||
578 | { "Flags", OSD.FromInteger(item.Flags) }, | ||
579 | { "GroupID", OSD.FromUUID(item.GroupID) }, | ||
580 | { "GroupOwned", OSD.FromBoolean(item.GroupOwned) }, | ||
581 | { "SalePrice", OSD.FromInteger(item.SalePrice) }, | ||
582 | { "SaleType", OSD.FromInteger(item.SaleType) }, | ||
583 | { "Permissions", permissions } | ||
584 | }; | ||
585 | |||
586 | NameValueCollection requestArgs = new NameValueCollection | ||
587 | { | ||
588 | { "RequestMethod", "AddInventoryItem" }, | ||
589 | { "ItemID", item.ID.ToString() }, | ||
590 | { "AssetID", item.AssetID.ToString() }, | ||
591 | { "ParentID", item.Folder.ToString() }, | ||
592 | { "OwnerID", item.Owner.ToString() }, | ||
593 | { "Name", item.Name }, | ||
594 | { "Description", item.Description }, | ||
595 | { "CreatorID", item.CreatorId }, | ||
596 | { "ExtraData", OSDParser.SerializeJsonString(extraData) } | ||
597 | }; | ||
598 | |||
599 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
600 | bool success = response["Success"].AsBoolean(); | ||
601 | |||
602 | if (!success) | ||
603 | { | ||
604 | m_log.Warn("[INVENTORY CONNECTOR]: Error creating item " + item.Name + " for " + item.Owner + ": " + | ||
605 | response["Message"].AsString()); | ||
606 | } | ||
607 | |||
608 | return success; | ||
609 | } | ||
610 | |||
611 | /// <summary> | ||
612 | /// Update an item in the user's inventory | ||
613 | /// </summary> | ||
614 | /// <param name="item"></param> | ||
615 | /// <returns>true if the item was successfully updated</returns> | ||
616 | public bool UpdateItem(InventoryItemBase item) | ||
617 | { | ||
618 | if (item.AssetID != UUID.Zero) | ||
619 | { | ||
620 | return AddItem(item); | ||
621 | } | ||
622 | else | ||
623 | { | ||
624 | // This is actually a folder update | ||
625 | InventoryFolderBase folder = new InventoryFolderBase(item.ID, item.Name, item.Owner, (short)item.AssetType, item.Folder, 0); | ||
626 | return UpdateFolder(folder); | ||
627 | } | ||
628 | } | ||
629 | |||
630 | public bool MoveItems(UUID ownerID, List<InventoryItemBase> items) | ||
631 | { | ||
632 | bool success = true; | ||
633 | |||
634 | while (items.Count > 0) | ||
635 | { | ||
636 | List<InventoryItemBase> currentItems = new List<InventoryItemBase>(); | ||
637 | UUID destFolderID = items[0].Folder; | ||
638 | |||
639 | // Find all of the items being moved to the current destination folder | ||
640 | for (int i = 0; i < items.Count; i++) | ||
641 | { | ||
642 | InventoryItemBase item = items[i]; | ||
643 | if (item.Folder == destFolderID) | ||
644 | currentItems.Add(item); | ||
645 | } | ||
646 | |||
647 | // Do the inventory move for the current items | ||
648 | success &= MoveItems(ownerID, items, destFolderID); | ||
649 | |||
650 | // Remove the processed items from the list | ||
651 | for (int i = 0; i < currentItems.Count; i++) | ||
652 | items.Remove(currentItems[i]); | ||
653 | } | ||
654 | |||
655 | return success; | ||
656 | } | ||
657 | |||
658 | /// <summary> | ||
659 | /// Does the given user have an inventory structure? | ||
660 | /// </summary> | ||
661 | /// <param name="userID"></param> | ||
662 | /// <returns></returns> | ||
663 | public bool HasInventoryForUser(UUID userID) | ||
664 | { | ||
665 | return GetRootFolder(userID) != null; | ||
666 | } | ||
667 | |||
668 | /// <summary> | ||
669 | /// Get the active gestures of the agent. | ||
670 | /// </summary> | ||
671 | /// <param name="userID"></param> | ||
672 | /// <returns></returns> | ||
673 | public List<InventoryItemBase> GetActiveGestures(UUID userID) | ||
674 | { | ||
675 | OSDArray items = FetchGestures(userID); | ||
676 | |||
677 | string[] itemIDs = new string[items.Count]; | ||
678 | for (int i = 0; i < items.Count; i++) | ||
679 | itemIDs[i] = items[i].AsUUID().ToString(); | ||
680 | |||
681 | NameValueCollection requestArgs = new NameValueCollection | ||
682 | { | ||
683 | { "RequestMethod", "GetInventoryNodes" }, | ||
684 | { "OwnerID", userID.ToString() }, | ||
685 | { "Items", String.Join(",", itemIDs) } | ||
686 | }; | ||
687 | |||
688 | // FIXME: Implement this in SimianGrid | ||
689 | return new List<InventoryItemBase>(0); | ||
690 | } | ||
691 | |||
692 | /// <summary> | ||
693 | /// Get the union of permissions of all inventory items | ||
694 | /// that hold the given assetID. | ||
695 | /// </summary> | ||
696 | /// <param name="userID"></param> | ||
697 | /// <param name="assetID"></param> | ||
698 | /// <returns>The permissions or 0 if no such asset is found in | ||
699 | /// the user's inventory</returns> | ||
700 | public int GetAssetPermissions(UUID userID, UUID assetID) | ||
701 | { | ||
702 | NameValueCollection requestArgs = new NameValueCollection | ||
703 | { | ||
704 | { "RequestMethod", "GetInventoryNodes" }, | ||
705 | { "OwnerID", userID.ToString() }, | ||
706 | { "AssetID", assetID.ToString() } | ||
707 | }; | ||
708 | |||
709 | // FIXME: Implement this in SimianGrid | ||
710 | return (int)PermissionMask.All; | ||
711 | } | ||
712 | |||
713 | private List<InventoryFolderBase> GetFoldersFromResponse(OSDArray items, UUID baseFolder, bool includeBaseFolder) | ||
714 | { | ||
715 | List<InventoryFolderBase> invFolders = new List<InventoryFolderBase>(items.Count); | ||
716 | |||
717 | for (int i = 0; i < items.Count; i++) | ||
718 | { | ||
719 | OSDMap item = items[i] as OSDMap; | ||
720 | |||
721 | if (item != null && item["Type"].AsString() == "Folder") | ||
722 | { | ||
723 | UUID folderID = item["ID"].AsUUID(); | ||
724 | |||
725 | if (folderID == baseFolder && !includeBaseFolder) | ||
726 | continue; | ||
727 | |||
728 | invFolders.Add(new InventoryFolderBase( | ||
729 | folderID, | ||
730 | item["Name"].AsString(), | ||
731 | item["OwnerID"].AsUUID(), | ||
732 | (short)SLUtil.ContentTypeToSLAssetType(item["ContentType"].AsString()), | ||
733 | item["ParentID"].AsUUID(), | ||
734 | (ushort)item["Version"].AsInteger() | ||
735 | )); | ||
736 | } | ||
737 | } | ||
738 | |||
739 | return invFolders; | ||
740 | } | ||
741 | |||
742 | private List<InventoryItemBase> GetItemsFromResponse(OSDArray items) | ||
743 | { | ||
744 | List<InventoryItemBase> invItems = new List<InventoryItemBase>(items.Count); | ||
745 | |||
746 | for (int i = 0; i < items.Count; i++) | ||
747 | { | ||
748 | OSDMap item = items[i] as OSDMap; | ||
749 | |||
750 | if (item != null && item["Type"].AsString() == "Item") | ||
751 | { | ||
752 | InventoryItemBase invItem = new InventoryItemBase(); | ||
753 | |||
754 | invItem.AssetID = item["AssetID"].AsUUID(); | ||
755 | invItem.AssetType = SLUtil.ContentTypeToSLAssetType(item["ContentType"].AsString()); | ||
756 | invItem.CreationDate = item["CreationDate"].AsInteger(); | ||
757 | invItem.CreatorId = item["CreatorID"].AsString(); | ||
758 | invItem.CreatorIdAsUuid = item["CreatorID"].AsUUID(); | ||
759 | invItem.Description = item["Description"].AsString(); | ||
760 | invItem.Folder = item["ParentID"].AsUUID(); | ||
761 | invItem.ID = item["ID"].AsUUID(); | ||
762 | invItem.InvType = SLUtil.ContentTypeToSLInvType(item["ContentType"].AsString()); | ||
763 | invItem.Name = item["Name"].AsString(); | ||
764 | invItem.Owner = item["OwnerID"].AsUUID(); | ||
765 | |||
766 | OSDMap extraData = item["ExtraData"] as OSDMap; | ||
767 | if (extraData != null && extraData.Count > 0) | ||
768 | { | ||
769 | invItem.Flags = extraData["Flags"].AsUInteger(); | ||
770 | invItem.GroupID = extraData["GroupID"].AsUUID(); | ||
771 | invItem.GroupOwned = extraData["GroupOwned"].AsBoolean(); | ||
772 | invItem.SalePrice = extraData["SalePrice"].AsInteger(); | ||
773 | invItem.SaleType = (byte)extraData["SaleType"].AsInteger(); | ||
774 | |||
775 | OSDMap perms = extraData["Permissions"] as OSDMap; | ||
776 | if (perms != null) | ||
777 | { | ||
778 | invItem.BasePermissions = perms["BaseMask"].AsUInteger(); | ||
779 | invItem.CurrentPermissions = perms["OwnerMask"].AsUInteger(); | ||
780 | invItem.EveryOnePermissions = perms["EveryoneMask"].AsUInteger(); | ||
781 | invItem.GroupPermissions = perms["GroupMask"].AsUInteger(); | ||
782 | invItem.NextPermissions = perms["NextOwnerMask"].AsUInteger(); | ||
783 | } | ||
784 | } | ||
785 | |||
786 | if (invItem.BasePermissions == 0) | ||
787 | { | ||
788 | m_log.InfoFormat("[INVENTORY CONNECTOR]: Forcing item permissions to full for item {0} ({1})", | ||
789 | invItem.Name, invItem.ID); | ||
790 | invItem.BasePermissions = (uint)PermissionMask.All; | ||
791 | invItem.CurrentPermissions = (uint)PermissionMask.All; | ||
792 | invItem.EveryOnePermissions = (uint)PermissionMask.All; | ||
793 | invItem.GroupPermissions = (uint)PermissionMask.All; | ||
794 | invItem.NextPermissions = (uint)PermissionMask.All; | ||
795 | } | ||
796 | |||
797 | invItems.Add(invItem); | ||
798 | } | ||
799 | } | ||
800 | |||
801 | return invItems; | ||
802 | } | ||
803 | |||
804 | private bool MoveItems(UUID ownerID, List<InventoryItemBase> items, UUID destFolderID) | ||
805 | { | ||
806 | string[] itemIDs = new string[items.Count]; | ||
807 | for (int i = 0; i < items.Count; i++) | ||
808 | itemIDs[i] = items[i].ID.ToString(); | ||
809 | |||
810 | NameValueCollection requestArgs = new NameValueCollection | ||
811 | { | ||
812 | { "RequestMethod", "MoveInventoryNodes" }, | ||
813 | { "OwnerID", ownerID.ToString() }, | ||
814 | { "FolderID", destFolderID.ToString() }, | ||
815 | { "Items", String.Join(",", itemIDs) } | ||
816 | }; | ||
817 | |||
818 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
819 | bool success = response["Success"].AsBoolean(); | ||
820 | |||
821 | if (!success) | ||
822 | { | ||
823 | m_log.Warn("[INVENTORY CONNECTOR]: Failed to move " + items.Count + " items to " + | ||
824 | destFolderID + ": " + response["Message"].AsString()); | ||
825 | } | ||
826 | |||
827 | return success; | ||
828 | } | ||
829 | |||
830 | private void UpdateGesture(UUID userID, UUID itemID, bool enabled) | ||
831 | { | ||
832 | OSDArray gestures = FetchGestures(userID); | ||
833 | OSDArray newGestures = new OSDArray(); | ||
834 | |||
835 | for (int i = 0; i < gestures.Count; i++) | ||
836 | { | ||
837 | UUID gesture = gestures[i].AsUUID(); | ||
838 | if (gesture != itemID) | ||
839 | newGestures.Add(OSD.FromUUID(gesture)); | ||
840 | } | ||
841 | |||
842 | if (enabled) | ||
843 | newGestures.Add(OSD.FromUUID(itemID)); | ||
844 | |||
845 | SaveGestures(userID, newGestures); | ||
846 | } | ||
847 | |||
848 | private OSDArray FetchGestures(UUID userID) | ||
849 | { | ||
850 | NameValueCollection requestArgs = new NameValueCollection | ||
851 | { | ||
852 | { "RequestMethod", "GetUser" }, | ||
853 | { "UserID", userID.ToString() } | ||
854 | }; | ||
855 | |||
856 | OSDMap response = WebUtil.PostToService(m_userServerUrl, requestArgs); | ||
857 | if (response["Success"].AsBoolean()) | ||
858 | { | ||
859 | OSDMap user = response["User"] as OSDMap; | ||
860 | if (user != null && response.ContainsKey("Gestures")) | ||
861 | { | ||
862 | OSD gestures = OSDParser.DeserializeJson(response["Gestures"].AsString()); | ||
863 | if (gestures != null && gestures is OSDArray) | ||
864 | return (OSDArray)gestures; | ||
865 | else | ||
866 | m_log.Error("[INVENTORY CONNECTOR]: Unrecognized active gestures data for " + userID); | ||
867 | } | ||
868 | } | ||
869 | else | ||
870 | { | ||
871 | m_log.Warn("[INVENTORY CONNECTOR]: Failed to fetch active gestures for " + userID + ": " + | ||
872 | response["Message"].AsString()); | ||
873 | } | ||
874 | |||
875 | return new OSDArray(); | ||
876 | } | ||
877 | |||
878 | private void SaveGestures(UUID userID, OSDArray gestures) | ||
879 | { | ||
880 | NameValueCollection requestArgs = new NameValueCollection | ||
881 | { | ||
882 | { "RequestMethod", "AddUserData" }, | ||
883 | { "UserID", userID.ToString() }, | ||
884 | { "Gestures", OSDParser.SerializeJsonString(gestures) } | ||
885 | }; | ||
886 | |||
887 | OSDMap response = WebUtil.PostToService(m_userServerUrl, requestArgs); | ||
888 | if (!response["Success"].AsBoolean()) | ||
889 | { | ||
890 | m_log.Warn("[INVENTORY CONNECTOR]: Failed to save active gestures for " + userID + ": " + | ||
891 | response["Message"].AsString()); | ||
892 | } | ||
893 | } | ||
894 | } | ||
895 | } | ||