XInventoryService.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using OpenMetaverse;
  30. using log4net;
  31. using Nini.Config;
  32. using System.Reflection;
  33. using OpenSim.Services.Base;
  34. using OpenSim.Services.Interfaces;
  35. using OpenSim.Data;
  36. using OpenSim.Framework;
  37. namespace OpenSim.Services.InventoryService
  38. {
  39. public class XInventoryService : ServiceBase, IInventoryService
  40. {
  41. private static readonly ILog m_log =
  42. LogManager.GetLogger(
  43. MethodBase.GetCurrentMethod().DeclaringType);
  44. protected IXInventoryData m_Database;
  45. public XInventoryService(IConfigSource config) : base(config)
  46. {
  47. string dllName = String.Empty;
  48. string connString = String.Empty;
  49. //string realm = "Inventory"; // OSG version doesn't use this
  50. //
  51. // Try reading the [InventoryService] section first, if it exists
  52. //
  53. IConfig authConfig = config.Configs["InventoryService"];
  54. if (authConfig != null)
  55. {
  56. dllName = authConfig.GetString("StorageProvider", dllName);
  57. connString = authConfig.GetString("ConnectionString", connString);
  58. // realm = authConfig.GetString("Realm", realm);
  59. }
  60. //
  61. // Try reading the [DatabaseService] section, if it exists
  62. //
  63. IConfig dbConfig = config.Configs["DatabaseService"];
  64. if (dbConfig != null)
  65. {
  66. if (dllName == String.Empty)
  67. dllName = dbConfig.GetString("StorageProvider", String.Empty);
  68. if (connString == String.Empty)
  69. connString = dbConfig.GetString("ConnectionString", String.Empty);
  70. }
  71. //
  72. // We tried, but this doesn't exist. We can't proceed.
  73. //
  74. if (dllName == String.Empty)
  75. throw new Exception("No StorageProvider configured");
  76. m_Database = LoadPlugin<IXInventoryData>(dllName,
  77. new Object[] {connString, String.Empty});
  78. if (m_Database == null)
  79. throw new Exception("Could not find a storage interface in the given module");
  80. }
  81. public virtual bool CreateUserInventory(UUID principalID)
  82. {
  83. // This is braindeaad. We can't ever communicate that we fixed
  84. // an existing inventory. Well, just return root folder status,
  85. // but check sanity anyway.
  86. //
  87. bool result = false;
  88. InventoryFolderBase rootFolder = GetRootFolder(principalID);
  89. if (rootFolder == null)
  90. {
  91. rootFolder = ConvertToOpenSim(CreateFolder(principalID, UUID.Zero, (int)AssetType.RootFolder, "My Inventory"));
  92. result = true;
  93. }
  94. XInventoryFolder[] sysFolders = GetSystemFolders(principalID);
  95. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.Animation) return true; return false; }))
  96. CreateFolder(principalID, rootFolder.ID, (int)AssetType.Animation, "Animations");
  97. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.Bodypart) return true; return false; }))
  98. CreateFolder(principalID, rootFolder.ID, (int)AssetType.Bodypart, "Body Parts");
  99. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.CallingCard) return true; return false; }))
  100. CreateFolder(principalID, rootFolder.ID, (int)AssetType.CallingCard, "Calling Cards");
  101. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.Clothing) return true; return false; }))
  102. CreateFolder(principalID, rootFolder.ID, (int)AssetType.Clothing, "Clothing");
  103. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.Gesture) return true; return false; }))
  104. CreateFolder(principalID, rootFolder.ID, (int)AssetType.Gesture, "Gestures");
  105. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.Landmark) return true; return false; }))
  106. CreateFolder(principalID, rootFolder.ID, (int)AssetType.Landmark, "Landmarks");
  107. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.LostAndFoundFolder) return true; return false; }))
  108. CreateFolder(principalID, rootFolder.ID, (int)AssetType.LostAndFoundFolder, "Lost And Found");
  109. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.Notecard) return true; return false; }))
  110. CreateFolder(principalID, rootFolder.ID, (int)AssetType.Notecard, "Notecards");
  111. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.Object) return true; return false; }))
  112. CreateFolder(principalID, rootFolder.ID, (int)AssetType.Object, "Objects");
  113. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.SnapshotFolder) return true; return false; }))
  114. CreateFolder(principalID, rootFolder.ID, (int)AssetType.SnapshotFolder, "Photo Album");
  115. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.LSLText) return true; return false; }))
  116. CreateFolder(principalID, rootFolder.ID, (int)AssetType.LSLText, "Scripts");
  117. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.Sound) return true; return false; }))
  118. CreateFolder(principalID, rootFolder.ID, (int)AssetType.Sound, "Sounds");
  119. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.Texture) return true; return false; }))
  120. CreateFolder(principalID, rootFolder.ID, (int)AssetType.Texture, "Textures");
  121. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.TrashFolder) return true; return false; }))
  122. CreateFolder(principalID, rootFolder.ID, (int)AssetType.TrashFolder, "Trash");
  123. return result;
  124. }
  125. protected XInventoryFolder CreateFolder(UUID principalID, UUID parentID, int type, string name)
  126. {
  127. XInventoryFolder newFolder = new XInventoryFolder();
  128. newFolder.folderName = name;
  129. newFolder.type = type;
  130. newFolder.version = 1;
  131. newFolder.folderID = UUID.Random();
  132. newFolder.agentID = principalID;
  133. newFolder.parentFolderID = parentID;
  134. m_Database.StoreFolder(newFolder);
  135. return newFolder;
  136. }
  137. protected virtual XInventoryFolder[] GetSystemFolders(UUID principalID)
  138. {
  139. XInventoryFolder[] allFolders = m_Database.GetFolders(
  140. new string[] { "agentID" },
  141. new string[] { principalID.ToString() });
  142. XInventoryFolder[] sysFolders = Array.FindAll(
  143. allFolders,
  144. delegate (XInventoryFolder f)
  145. {
  146. if (f.type > 0)
  147. return true;
  148. return false;
  149. });
  150. return sysFolders;
  151. }
  152. public virtual List<InventoryFolderBase> GetInventorySkeleton(UUID principalID)
  153. {
  154. XInventoryFolder[] allFolders = m_Database.GetFolders(
  155. new string[] { "agentID" },
  156. new string[] { principalID.ToString() });
  157. if (allFolders.Length == 0)
  158. return null;
  159. List<InventoryFolderBase> folders = new List<InventoryFolderBase>();
  160. foreach (XInventoryFolder x in allFolders)
  161. {
  162. //m_log.DebugFormat("[XINVENTORY]: Adding folder {0} to skeleton", x.folderName);
  163. folders.Add(ConvertToOpenSim(x));
  164. }
  165. return folders;
  166. }
  167. public virtual InventoryFolderBase GetRootFolder(UUID principalID)
  168. {
  169. XInventoryFolder[] folders = m_Database.GetFolders(
  170. new string[] { "agentID", "parentFolderID"},
  171. new string[] { principalID.ToString(), UUID.Zero.ToString() });
  172. if (folders.Length == 0)
  173. return null;
  174. XInventoryFolder root = null;
  175. foreach (XInventoryFolder folder in folders)
  176. if (folder.folderName == "My Inventory")
  177. root = folder;
  178. if (folders == null) // oops
  179. root = folders[0];
  180. return ConvertToOpenSim(root);
  181. }
  182. public virtual InventoryFolderBase GetFolderForType(UUID principalID, AssetType type)
  183. {
  184. XInventoryFolder[] folders = m_Database.GetFolders(
  185. new string[] { "agentID", "type"},
  186. new string[] { principalID.ToString(), ((int)type).ToString() });
  187. if (folders.Length == 0)
  188. return null;
  189. return ConvertToOpenSim(folders[0]);
  190. }
  191. public virtual InventoryCollection GetFolderContent(UUID principalID, UUID folderID)
  192. {
  193. // This method doesn't receive a valud principal id from the
  194. // connector. So we disregard the principal and look
  195. // by ID.
  196. //
  197. m_log.DebugFormat("[XINVENTORY]: Fetch contents for folder {0}", folderID.ToString());
  198. InventoryCollection inventory = new InventoryCollection();
  199. inventory.UserID = principalID;
  200. inventory.Folders = new List<InventoryFolderBase>();
  201. inventory.Items = new List<InventoryItemBase>();
  202. XInventoryFolder[] folders = m_Database.GetFolders(
  203. new string[] { "parentFolderID"},
  204. new string[] { folderID.ToString() });
  205. foreach (XInventoryFolder x in folders)
  206. {
  207. //m_log.DebugFormat("[XINVENTORY]: Adding folder {0} to response", x.folderName);
  208. inventory.Folders.Add(ConvertToOpenSim(x));
  209. }
  210. XInventoryItem[] items = m_Database.GetItems(
  211. new string[] { "parentFolderID"},
  212. new string[] { folderID.ToString() });
  213. foreach (XInventoryItem i in items)
  214. {
  215. //m_log.DebugFormat("[XINVENTORY]: Adding item {0} to response", i.inventoryName);
  216. inventory.Items.Add(ConvertToOpenSim(i));
  217. }
  218. return inventory;
  219. }
  220. public virtual List<InventoryItemBase> GetFolderItems(UUID principalID, UUID folderID)
  221. {
  222. // m_log.DebugFormat("[XINVENTORY]: Fetch items for folder {0}", folderID);
  223. // Since we probably don't get a valid principal here, either ...
  224. //
  225. List<InventoryItemBase> invItems = new List<InventoryItemBase>();
  226. XInventoryItem[] items = m_Database.GetItems(
  227. new string[] { "parentFolderID" },
  228. new string[] { folderID.ToString() });
  229. foreach (XInventoryItem i in items)
  230. invItems.Add(ConvertToOpenSim(i));
  231. return invItems;
  232. }
  233. public virtual bool AddFolder(InventoryFolderBase folder)
  234. {
  235. XInventoryFolder xFolder = ConvertFromOpenSim(folder);
  236. return m_Database.StoreFolder(xFolder);
  237. }
  238. public virtual bool UpdateFolder(InventoryFolderBase folder)
  239. {
  240. return AddFolder(folder);
  241. }
  242. public virtual bool MoveFolder(InventoryFolderBase folder)
  243. {
  244. XInventoryFolder[] x = m_Database.GetFolders(
  245. new string[] { "folderID" },
  246. new string[] { folder.ID.ToString() });
  247. if (x.Length == 0)
  248. return false;
  249. x[0].parentFolderID = folder.ParentID;
  250. return m_Database.StoreFolder(x[0]);
  251. }
  252. // We don't check the principal's ID here
  253. //
  254. public virtual bool DeleteFolders(UUID principalID, List<UUID> folderIDs)
  255. {
  256. // Ignore principal ID, it's bogus at connector level
  257. //
  258. foreach (UUID id in folderIDs)
  259. {
  260. InventoryFolderBase f = new InventoryFolderBase();
  261. f.ID = id;
  262. PurgeFolder(f);
  263. m_Database.DeleteFolders("folderID", id.ToString());
  264. }
  265. return true;
  266. }
  267. public virtual bool PurgeFolder(InventoryFolderBase folder)
  268. {
  269. XInventoryFolder[] subFolders = m_Database.GetFolders(
  270. new string[] { "parentFolderID" },
  271. new string[] { folder.ID.ToString() });
  272. foreach (XInventoryFolder x in subFolders)
  273. {
  274. PurgeFolder(ConvertToOpenSim(x));
  275. m_Database.DeleteFolders("folderID", x.folderID.ToString());
  276. }
  277. m_Database.DeleteItems("parentFolderID", folder.ID.ToString());
  278. return true;
  279. }
  280. public virtual bool AddItem(InventoryItemBase item)
  281. {
  282. return m_Database.StoreItem(ConvertFromOpenSim(item));
  283. }
  284. public virtual bool UpdateItem(InventoryItemBase item)
  285. {
  286. return m_Database.StoreItem(ConvertFromOpenSim(item));
  287. }
  288. public virtual bool MoveItems(UUID principalID, List<InventoryItemBase> items)
  289. {
  290. // Principal is b0rked. *sigh*
  291. //
  292. foreach (InventoryItemBase i in items)
  293. {
  294. m_Database.MoveItem(i.ID.ToString(), i.Folder.ToString());
  295. }
  296. return true;
  297. }
  298. public virtual bool DeleteItems(UUID principalID, List<UUID> itemIDs)
  299. {
  300. // Just use the ID... *facepalms*
  301. //
  302. foreach (UUID id in itemIDs)
  303. m_Database.DeleteItems("inventoryID", id.ToString());
  304. return true;
  305. }
  306. public virtual InventoryItemBase GetItem(InventoryItemBase item)
  307. {
  308. XInventoryItem[] items = m_Database.GetItems(
  309. new string[] { "inventoryID" },
  310. new string[] { item.ID.ToString() });
  311. if (items.Length == 0)
  312. return null;
  313. return ConvertToOpenSim(items[0]);
  314. }
  315. public virtual InventoryFolderBase GetFolder(InventoryFolderBase folder)
  316. {
  317. XInventoryFolder[] folders = m_Database.GetFolders(
  318. new string[] { "folderID"},
  319. new string[] { folder.ID.ToString() });
  320. if (folders.Length == 0)
  321. return null;
  322. return ConvertToOpenSim(folders[0]);
  323. }
  324. public virtual List<InventoryItemBase> GetActiveGestures(UUID principalID)
  325. {
  326. XInventoryItem[] items = m_Database.GetActiveGestures(principalID);
  327. if (items.Length == 0)
  328. return new List<InventoryItemBase>();
  329. List<InventoryItemBase> ret = new List<InventoryItemBase>();
  330. foreach (XInventoryItem x in items)
  331. ret.Add(ConvertToOpenSim(x));
  332. return ret;
  333. }
  334. public virtual int GetAssetPermissions(UUID principalID, UUID assetID)
  335. {
  336. return m_Database.GetAssetPermissions(principalID, assetID);
  337. }
  338. // CM never needed those. Left unimplemented.
  339. // Obsolete in core
  340. //
  341. public InventoryCollection GetUserInventory(UUID userID)
  342. {
  343. return null;
  344. }
  345. public void GetUserInventory(UUID userID, InventoryReceiptCallback callback)
  346. {
  347. }
  348. // Unused.
  349. //
  350. public bool HasInventoryForUser(UUID userID)
  351. {
  352. return false;
  353. }
  354. // CM Helpers
  355. //
  356. protected InventoryFolderBase ConvertToOpenSim(XInventoryFolder folder)
  357. {
  358. InventoryFolderBase newFolder = new InventoryFolderBase();
  359. newFolder.ParentID = folder.parentFolderID;
  360. newFolder.Type = (short)folder.type;
  361. newFolder.Version = (ushort)folder.version;
  362. newFolder.Name = folder.folderName;
  363. newFolder.Owner = folder.agentID;
  364. newFolder.ID = folder.folderID;
  365. return newFolder;
  366. }
  367. protected XInventoryFolder ConvertFromOpenSim(InventoryFolderBase folder)
  368. {
  369. XInventoryFolder newFolder = new XInventoryFolder();
  370. newFolder.parentFolderID = folder.ParentID;
  371. newFolder.type = (int)folder.Type;
  372. newFolder.version = (int)folder.Version;
  373. newFolder.folderName = folder.Name;
  374. newFolder.agentID = folder.Owner;
  375. newFolder.folderID = folder.ID;
  376. return newFolder;
  377. }
  378. protected InventoryItemBase ConvertToOpenSim(XInventoryItem item)
  379. {
  380. InventoryItemBase newItem = new InventoryItemBase();
  381. newItem.AssetID = item.assetID;
  382. newItem.AssetType = item.assetType;
  383. newItem.Name = item.inventoryName;
  384. newItem.Owner = item.avatarID;
  385. newItem.ID = item.inventoryID;
  386. newItem.InvType = item.invType;
  387. newItem.Folder = item.parentFolderID;
  388. newItem.CreatorId = item.creatorID;
  389. newItem.Description = item.inventoryDescription;
  390. newItem.NextPermissions = (uint)item.inventoryNextPermissions;
  391. newItem.CurrentPermissions = (uint)item.inventoryCurrentPermissions;
  392. newItem.BasePermissions = (uint)item.inventoryBasePermissions;
  393. newItem.EveryOnePermissions = (uint)item.inventoryEveryOnePermissions;
  394. newItem.GroupPermissions = (uint)item.inventoryGroupPermissions;
  395. newItem.GroupID = item.groupID;
  396. if (item.groupOwned == 0)
  397. newItem.GroupOwned = false;
  398. else
  399. newItem.GroupOwned = true;
  400. newItem.SalePrice = item.salePrice;
  401. newItem.SaleType = (byte)item.saleType;
  402. newItem.Flags = (uint)item.flags;
  403. newItem.CreationDate = item.creationDate;
  404. return newItem;
  405. }
  406. protected XInventoryItem ConvertFromOpenSim(InventoryItemBase item)
  407. {
  408. XInventoryItem newItem = new XInventoryItem();
  409. newItem.assetID = item.AssetID;
  410. newItem.assetType = item.AssetType;
  411. newItem.inventoryName = item.Name;
  412. newItem.avatarID = item.Owner;
  413. newItem.inventoryID = item.ID;
  414. newItem.invType = item.InvType;
  415. newItem.parentFolderID = item.Folder;
  416. newItem.creatorID = item.CreatorId;
  417. newItem.inventoryDescription = item.Description;
  418. newItem.inventoryNextPermissions = (int)item.NextPermissions;
  419. newItem.inventoryCurrentPermissions = (int)item.CurrentPermissions;
  420. newItem.inventoryBasePermissions = (int)item.BasePermissions;
  421. newItem.inventoryEveryOnePermissions = (int)item.EveryOnePermissions;
  422. newItem.inventoryGroupPermissions = (int)item.GroupPermissions;
  423. newItem.groupID = item.GroupID;
  424. if (item.GroupOwned)
  425. newItem.groupOwned = 1;
  426. else
  427. newItem.groupOwned = 0;
  428. newItem.salePrice = item.SalePrice;
  429. newItem.saleType = (int)item.SaleType;
  430. newItem.flags = (int)item.Flags;
  431. newItem.creationDate = item.CreationDate;
  432. return newItem;
  433. }
  434. }
  435. }