clockwerk-opensim-stable – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 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 log4net;
29 using System;
30 using System.Collections.Generic;
31 using System.IO;
32 using System.Reflection;
33 using Nini.Config;
34 using OpenSim.Framework;
35 using OpenSim.Framework.Console;
36 using OpenSim.Framework.Communications;
37 using OpenSim.Services.Interfaces;
38 using OpenSim.Server.Base;
39 using OpenMetaverse;
40  
41 namespace OpenSim.Services.Connectors
42 {
43 public class XInventoryServicesConnector : IInventoryService
44 {
45 private static readonly ILog m_log =
46 LogManager.GetLogger(
47 MethodBase.GetCurrentMethod().DeclaringType);
48  
49 private string m_ServerURI = String.Empty;
50  
51 private object m_Lock = new object();
52  
53 public XInventoryServicesConnector()
54 {
55 }
56  
57 public XInventoryServicesConnector(string serverURI)
58 {
59 m_ServerURI = serverURI.TrimEnd('/');
60 }
61  
62 public XInventoryServicesConnector(IConfigSource source)
63 {
64 Initialise(source);
65 }
66  
67 public virtual void Initialise(IConfigSource source)
68 {
69 IConfig assetConfig = source.Configs["InventoryService"];
70 if (assetConfig == null)
71 {
72 m_log.Error("[INVENTORY CONNECTOR]: InventoryService missing from OpenSim.ini");
73 throw new Exception("Inventory connector init error");
74 }
75  
76 string serviceURI = assetConfig.GetString("InventoryServerURI",
77 String.Empty);
78  
79 if (serviceURI == String.Empty)
80 {
81 m_log.Error("[INVENTORY CONNECTOR]: No Server URI named in section InventoryService");
82 throw new Exception("Inventory connector init error");
83 }
84 m_ServerURI = serviceURI;
85 }
86  
87 private bool CheckReturn(Dictionary<string, object> ret)
88 {
89 if (ret == null)
90 return false;
91  
92 if (ret.Count == 0)
93 return false;
94  
95 if (ret.ContainsKey("RESULT"))
96 {
97 if (ret["RESULT"] is string)
98 {
99 bool result;
100  
101 if (bool.TryParse((string)ret["RESULT"], out result))
102 return result;
103  
104 return false;
105 }
106 }
107  
108 return true;
109 }
110  
111 public bool CreateUserInventory(UUID principalID)
112 {
113 Dictionary<string,object> ret = MakeRequest("CREATEUSERINVENTORY",
114 new Dictionary<string,object> {
115 { "PRINCIPAL", principalID.ToString() }
116 });
117  
118 return CheckReturn(ret);
119 }
120  
121 public List<InventoryFolderBase> GetInventorySkeleton(UUID principalID)
122 {
123 Dictionary<string,object> ret = MakeRequest("GETINVENTORYSKELETON",
124 new Dictionary<string,object> {
125 { "PRINCIPAL", principalID.ToString() }
126 });
127  
128 if (!CheckReturn(ret))
129 return null;
130  
131 Dictionary<string, object> folders = (Dictionary<string, object>)ret["FOLDERS"];
132  
133 List<InventoryFolderBase> fldrs = new List<InventoryFolderBase>();
134  
135 try
136 {
137 foreach (Object o in folders.Values)
138 fldrs.Add(BuildFolder((Dictionary<string, object>)o));
139 }
140 catch (Exception e)
141 {
142 m_log.Error("[XINVENTORY SERVICES CONNECTOR]: Exception unwrapping folder list: ", e);
143 }
144  
145 return fldrs;
146 }
147  
148 public InventoryFolderBase GetRootFolder(UUID principalID)
149 {
150 Dictionary<string,object> ret = MakeRequest("GETROOTFOLDER",
151 new Dictionary<string,object> {
152 { "PRINCIPAL", principalID.ToString() }
153 });
154  
155 if (!CheckReturn(ret))
156 return null;
157  
158 return BuildFolder((Dictionary<string, object>)ret["folder"]);
159 }
160  
161 public InventoryFolderBase GetFolderForType(UUID principalID, AssetType type)
162 {
163 Dictionary<string,object> ret = MakeRequest("GETFOLDERFORTYPE",
164 new Dictionary<string,object> {
165 { "PRINCIPAL", principalID.ToString() },
166 { "TYPE", ((int)type).ToString() }
167 });
168  
169 if (!CheckReturn(ret))
170 return null;
171  
172 return BuildFolder((Dictionary<string, object>)ret["folder"]);
173 }
174  
175 public InventoryCollection GetFolderContent(UUID principalID, UUID folderID)
176 {
177 InventoryCollection inventory = new InventoryCollection();
178 inventory.Folders = new List<InventoryFolderBase>();
179 inventory.Items = new List<InventoryItemBase>();
180 inventory.UserID = principalID;
181  
182 try
183 {
184 Dictionary<string,object> ret = MakeRequest("GETFOLDERCONTENT",
185 new Dictionary<string,object> {
186 { "PRINCIPAL", principalID.ToString() },
187 { "FOLDER", folderID.ToString() }
188 });
189  
190 if (!CheckReturn(ret))
191 return null;
192  
193 Dictionary<string,object> folders =
194 (Dictionary<string,object>)ret["FOLDERS"];
195 Dictionary<string,object> items =
196 (Dictionary<string,object>)ret["ITEMS"];
197  
198 foreach (Object o in folders.Values) // getting the values directly, we don't care about the keys folder_i
199 inventory.Folders.Add(BuildFolder((Dictionary<string, object>)o));
200 foreach (Object o in items.Values) // getting the values directly, we don't care about the keys item_i
201 inventory.Items.Add(BuildItem((Dictionary<string, object>)o));
202 }
203 catch (Exception e)
204 {
205 m_log.WarnFormat("[XINVENTORY SERVICES CONNECTOR]: Exception in GetFolderContent: {0}", e.Message);
206 }
207  
208 return inventory;
209 }
210  
211 public List<InventoryItemBase> GetFolderItems(UUID principalID, UUID folderID)
212 {
213 Dictionary<string,object> ret = MakeRequest("GETFOLDERITEMS",
214 new Dictionary<string,object> {
215 { "PRINCIPAL", principalID.ToString() },
216 { "FOLDER", folderID.ToString() }
217 });
218  
219 if (!CheckReturn(ret))
220 return null;
221  
222 Dictionary<string, object> items = (Dictionary<string, object>)ret["ITEMS"];
223 List<InventoryItemBase> fitems = new List<InventoryItemBase>();
224 foreach (Object o in items.Values) // getting the values directly, we don't care about the keys item_i
225 fitems.Add(BuildItem((Dictionary<string, object>)o));
226  
227 return fitems;
228 }
229  
230 public bool AddFolder(InventoryFolderBase folder)
231 {
232 Dictionary<string,object> ret = MakeRequest("ADDFOLDER",
233 new Dictionary<string,object> {
234 { "ParentID", folder.ParentID.ToString() },
235 { "Type", folder.Type.ToString() },
236 { "Version", folder.Version.ToString() },
237 { "Name", folder.Name.ToString() },
238 { "Owner", folder.Owner.ToString() },
239 { "ID", folder.ID.ToString() }
240 });
241  
242 return CheckReturn(ret);
243 }
244  
245 public bool UpdateFolder(InventoryFolderBase folder)
246 {
247 Dictionary<string,object> ret = MakeRequest("UPDATEFOLDER",
248 new Dictionary<string,object> {
249 { "ParentID", folder.ParentID.ToString() },
250 { "Type", folder.Type.ToString() },
251 { "Version", folder.Version.ToString() },
252 { "Name", folder.Name.ToString() },
253 { "Owner", folder.Owner.ToString() },
254 { "ID", folder.ID.ToString() }
255 });
256  
257 return CheckReturn(ret);
258 }
259  
260 public bool MoveFolder(InventoryFolderBase folder)
261 {
262 Dictionary<string,object> ret = MakeRequest("MOVEFOLDER",
263 new Dictionary<string,object> {
264 { "ParentID", folder.ParentID.ToString() },
265 { "ID", folder.ID.ToString() },
266 { "PRINCIPAL", folder.Owner.ToString() }
267 });
268  
269 return CheckReturn(ret);
270 }
271  
272 public bool DeleteFolders(UUID principalID, List<UUID> folderIDs)
273 {
274 List<string> slist = new List<string>();
275  
276 foreach (UUID f in folderIDs)
277 slist.Add(f.ToString());
278  
279 Dictionary<string,object> ret = MakeRequest("DELETEFOLDERS",
280 new Dictionary<string,object> {
281 { "PRINCIPAL", principalID.ToString() },
282 { "FOLDERS", slist }
283 });
284  
285 return CheckReturn(ret);
286 }
287  
288 public bool PurgeFolder(InventoryFolderBase folder)
289 {
290 Dictionary<string,object> ret = MakeRequest("PURGEFOLDER",
291 new Dictionary<string,object> {
292 { "ID", folder.ID.ToString() }
293 });
294  
295 return CheckReturn(ret);
296 }
297  
298 public bool AddItem(InventoryItemBase item)
299 {
300 if (item.CreatorData == null)
301 item.CreatorData = String.Empty;
302 Dictionary<string,object> ret = MakeRequest("ADDITEM",
303 new Dictionary<string,object> {
304 { "AssetID", item.AssetID.ToString() },
305 { "AssetType", item.AssetType.ToString() },
306 { "Name", item.Name.ToString() },
307 { "Owner", item.Owner.ToString() },
308 { "ID", item.ID.ToString() },
309 { "InvType", item.InvType.ToString() },
310 { "Folder", item.Folder.ToString() },
311 { "CreatorId", item.CreatorId.ToString() },
312 { "CreatorData", item.CreatorData.ToString() },
313 { "Description", item.Description.ToString() },
314 { "NextPermissions", item.NextPermissions.ToString() },
315 { "CurrentPermissions", item.CurrentPermissions.ToString() },
316 { "BasePermissions", item.BasePermissions.ToString() },
317 { "EveryOnePermissions", item.EveryOnePermissions.ToString() },
318 { "GroupPermissions", item.GroupPermissions.ToString() },
319 { "GroupID", item.GroupID.ToString() },
320 { "GroupOwned", item.GroupOwned.ToString() },
321 { "SalePrice", item.SalePrice.ToString() },
322 { "SaleType", item.SaleType.ToString() },
323 { "Flags", item.Flags.ToString() },
324 { "CreationDate", item.CreationDate.ToString() }
325 });
326  
327 return CheckReturn(ret);
328 }
329  
330 public bool UpdateItem(InventoryItemBase item)
331 {
332 if (item.CreatorData == null)
333 item.CreatorData = String.Empty;
334 Dictionary<string,object> ret = MakeRequest("UPDATEITEM",
335 new Dictionary<string,object> {
336 { "AssetID", item.AssetID.ToString() },
337 { "AssetType", item.AssetType.ToString() },
338 { "Name", item.Name.ToString() },
339 { "Owner", item.Owner.ToString() },
340 { "ID", item.ID.ToString() },
341 { "InvType", item.InvType.ToString() },
342 { "Folder", item.Folder.ToString() },
343 { "CreatorId", item.CreatorId.ToString() },
344 { "CreatorData", item.CreatorData.ToString() },
345 { "Description", item.Description.ToString() },
346 { "NextPermissions", item.NextPermissions.ToString() },
347 { "CurrentPermissions", item.CurrentPermissions.ToString() },
348 { "BasePermissions", item.BasePermissions.ToString() },
349 { "EveryOnePermissions", item.EveryOnePermissions.ToString() },
350 { "GroupPermissions", item.GroupPermissions.ToString() },
351 { "GroupID", item.GroupID.ToString() },
352 { "GroupOwned", item.GroupOwned.ToString() },
353 { "SalePrice", item.SalePrice.ToString() },
354 { "SaleType", item.SaleType.ToString() },
355 { "Flags", item.Flags.ToString() },
356 { "CreationDate", item.CreationDate.ToString() }
357 });
358  
359 return CheckReturn(ret);
360 }
361  
362 public bool MoveItems(UUID principalID, List<InventoryItemBase> items)
363 {
364 List<string> idlist = new List<string>();
365 List<string> destlist = new List<string>();
366  
367 foreach (InventoryItemBase item in items)
368 {
369 idlist.Add(item.ID.ToString());
370 destlist.Add(item.Folder.ToString());
371 }
372  
373 Dictionary<string,object> ret = MakeRequest("MOVEITEMS",
374 new Dictionary<string,object> {
375 { "PRINCIPAL", principalID.ToString() },
376 { "IDLIST", idlist },
377 { "DESTLIST", destlist }
378 });
379  
380 return CheckReturn(ret);
381 }
382  
383 public bool DeleteItems(UUID principalID, List<UUID> itemIDs)
384 {
385 List<string> slist = new List<string>();
386  
387 foreach (UUID f in itemIDs)
388 slist.Add(f.ToString());
389  
390 Dictionary<string,object> ret = MakeRequest("DELETEITEMS",
391 new Dictionary<string,object> {
392 { "PRINCIPAL", principalID.ToString() },
393 { "ITEMS", slist }
394 });
395  
396 return CheckReturn(ret);
397 }
398  
399 public InventoryItemBase GetItem(InventoryItemBase item)
400 {
401 try
402 {
403 Dictionary<string, object> ret = MakeRequest("GETITEM",
404 new Dictionary<string, object> {
405 { "ID", item.ID.ToString() }
406 });
407  
408 if (!CheckReturn(ret))
409 return null;
410  
411 return BuildItem((Dictionary<string, object>)ret["item"]);
412 }
413 catch (Exception e)
414 {
415 m_log.Error("[XINVENTORY SERVICES CONNECTOR]: Exception in GetItem: ", e);
416 }
417  
418 return null;
419 }
420  
421 public InventoryFolderBase GetFolder(InventoryFolderBase folder)
422 {
423 try
424 {
425 Dictionary<string, object> ret = MakeRequest("GETFOLDER",
426 new Dictionary<string, object> {
427 { "ID", folder.ID.ToString() }
428 });
429  
430 if (!CheckReturn(ret))
431 return null;
432  
433 return BuildFolder((Dictionary<string, object>)ret["folder"]);
434 }
435 catch (Exception e)
436 {
437 m_log.Error("[XINVENTORY SERVICES CONNECTOR]: Exception in GetFolder: ", e);
438 }
439  
440 return null;
441 }
442  
443 public List<InventoryItemBase> GetActiveGestures(UUID principalID)
444 {
445 Dictionary<string,object> ret = MakeRequest("GETACTIVEGESTURES",
446 new Dictionary<string,object> {
447 { "PRINCIPAL", principalID.ToString() }
448 });
449  
450 if (!CheckReturn(ret))
451 return null;
452  
453 List<InventoryItemBase> items = new List<InventoryItemBase>();
454  
455 foreach (Object o in ((Dictionary<string,object>)ret["ITEMS"]).Values)
456 items.Add(BuildItem((Dictionary<string, object>)o));
457  
458 return items;
459 }
460  
461 public int GetAssetPermissions(UUID principalID, UUID assetID)
462 {
463 Dictionary<string,object> ret = MakeRequest("GETASSETPERMISSIONS",
464 new Dictionary<string,object> {
465 { "PRINCIPAL", principalID.ToString() },
466 { "ASSET", assetID.ToString() }
467 });
468  
469 // We cannot use CheckReturn() here because valid values for RESULT are "false" (in the case of request failure) or an int
470 if (ret == null)
471 return 0;
472  
473 if (ret.ContainsKey("RESULT"))
474 {
475 if (ret["RESULT"] is string)
476 {
477 int intResult;
478  
479 if (int.TryParse ((string)ret["RESULT"], out intResult))
480 return intResult;
481 }
482 }
483  
484 return 0;
485 }
486  
487 public InventoryCollection GetUserInventory(UUID principalID)
488 {
489 InventoryCollection inventory = new InventoryCollection();
490 inventory.Folders = new List<InventoryFolderBase>();
491 inventory.Items = new List<InventoryItemBase>();
492 inventory.UserID = principalID;
493  
494 try
495 {
496 Dictionary<string, object> ret = MakeRequest("GETUSERINVENTORY",
497 new Dictionary<string, object> {
498 { "PRINCIPAL", principalID.ToString() }
499 });
500  
501 if (!CheckReturn(ret))
502 return null;
503  
504 Dictionary<string, object> folders =
505 (Dictionary<string, object>)ret["FOLDERS"];
506 Dictionary<string, object> items =
507 (Dictionary<string, object>)ret["ITEMS"];
508  
509 foreach (Object o in folders.Values) // getting the values directly, we don't care about the keys folder_i
510 inventory.Folders.Add(BuildFolder((Dictionary<string, object>)o));
511 foreach (Object o in items.Values) // getting the values directly, we don't care about the keys item_i
512 inventory.Items.Add(BuildItem((Dictionary<string, object>)o));
513 }
514 catch (Exception e)
515 {
516 m_log.Error("[XINVENTORY SERVICES CONNECTOR]: Exception in GetUserInventory: ", e);
517 }
518  
519 return inventory;
520 }
521  
522 public void GetUserInventory(UUID principalID, InventoryReceiptCallback callback)
523 {
524 }
525  
526 public bool HasInventoryForUser(UUID principalID)
527 {
528 return false;
529 }
530  
531 // Helpers
532 //
533 private Dictionary<string,object> MakeRequest(string method,
534 Dictionary<string,object> sendData)
535 {
536 sendData["METHOD"] = method;
537  
538 string reply = string.Empty;
539 lock (m_Lock)
540 reply = SynchronousRestFormsRequester.MakeRequest("POST",
541 m_ServerURI + "/xinventory",
542 ServerUtils.BuildQueryString(sendData));
543  
544 Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(
545 reply);
546  
547 return replyData;
548 }
549  
550 private InventoryFolderBase BuildFolder(Dictionary<string,object> data)
551 {
552 InventoryFolderBase folder = new InventoryFolderBase();
553  
554 try
555 {
556 folder.ParentID = new UUID(data["ParentID"].ToString());
557 folder.Type = short.Parse(data["Type"].ToString());
558 folder.Version = ushort.Parse(data["Version"].ToString());
559 folder.Name = data["Name"].ToString();
560 folder.Owner = new UUID(data["Owner"].ToString());
561 folder.ID = new UUID(data["ID"].ToString());
562 }
563 catch (Exception e)
564 {
565 m_log.Error("[XINVENTORY SERVICES CONNECTOR]: Exception building folder: ", e);
566 }
567  
568 return folder;
569 }
570  
571 private InventoryItemBase BuildItem(Dictionary<string,object> data)
572 {
573 InventoryItemBase item = new InventoryItemBase();
574  
575 try
576 {
577 item.AssetID = new UUID(data["AssetID"].ToString());
578 item.AssetType = int.Parse(data["AssetType"].ToString());
579 item.Name = data["Name"].ToString();
580 item.Owner = new UUID(data["Owner"].ToString());
581 item.ID = new UUID(data["ID"].ToString());
582 item.InvType = int.Parse(data["InvType"].ToString());
583 item.Folder = new UUID(data["Folder"].ToString());
584 item.CreatorId = data["CreatorId"].ToString();
585 if (data.ContainsKey("CreatorData"))
586 item.CreatorData = data["CreatorData"].ToString();
587 else
588 item.CreatorData = String.Empty;
589 item.Description = data["Description"].ToString();
590 item.NextPermissions = uint.Parse(data["NextPermissions"].ToString());
591 item.CurrentPermissions = uint.Parse(data["CurrentPermissions"].ToString());
592 item.BasePermissions = uint.Parse(data["BasePermissions"].ToString());
593 item.EveryOnePermissions = uint.Parse(data["EveryOnePermissions"].ToString());
594 item.GroupPermissions = uint.Parse(data["GroupPermissions"].ToString());
595 item.GroupID = new UUID(data["GroupID"].ToString());
596 item.GroupOwned = bool.Parse(data["GroupOwned"].ToString());
597 item.SalePrice = int.Parse(data["SalePrice"].ToString());
598 item.SaleType = byte.Parse(data["SaleType"].ToString());
599 item.Flags = uint.Parse(data["Flags"].ToString());
600 item.CreationDate = int.Parse(data["CreationDate"].ToString());
601 }
602 catch (Exception e)
603 {
604 m_log.Error("[XINVENTORY CONNECTOR]: Exception building item: ", e);
605 }
606  
607 return item;
608 }
609 }
610 }