1
0

XInventoryService.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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. protected bool m_AllowDelete = true;
  46. public XInventoryService(IConfigSource config) : base(config)
  47. {
  48. string dllName = String.Empty;
  49. string connString = String.Empty;
  50. //string realm = "Inventory"; // OSG version doesn't use this
  51. //
  52. // Try reading the [InventoryService] section first, if it exists
  53. //
  54. IConfig authConfig = config.Configs["InventoryService"];
  55. if (authConfig != null)
  56. {
  57. dllName = authConfig.GetString("StorageProvider", dllName);
  58. connString = authConfig.GetString("ConnectionString", connString);
  59. m_AllowDelete = authConfig.GetBoolean("AllowDelete", true);
  60. // realm = authConfig.GetString("Realm", realm);
  61. }
  62. //
  63. // Try reading the [DatabaseService] section, if it exists
  64. //
  65. IConfig dbConfig = config.Configs["DatabaseService"];
  66. if (dbConfig != null)
  67. {
  68. if (dllName == String.Empty)
  69. dllName = dbConfig.GetString("StorageProvider", String.Empty);
  70. if (connString == String.Empty)
  71. connString = dbConfig.GetString("ConnectionString", String.Empty);
  72. }
  73. //
  74. // We tried, but this doesn't exist. We can't proceed.
  75. //
  76. if (dllName == String.Empty)
  77. throw new Exception("No StorageProvider configured");
  78. m_Database = LoadPlugin<IXInventoryData>(dllName,
  79. new Object[] {connString, String.Empty});
  80. if (m_Database == null)
  81. throw new Exception("Could not find a storage interface in the given module");
  82. }
  83. public bool CreateUserInventory(UUID principalID)
  84. {
  85. // This is braindeaad. We can't ever communicate that we fixed
  86. // an existing inventory. Well, just return root folder status,
  87. // but check sanity anyway.
  88. //
  89. bool result = false;
  90. InventoryFolderBase rootFolder = GetRootFolder(principalID);
  91. if (rootFolder == null)
  92. {
  93. rootFolder = ConvertToOpenSim(CreateFolder(principalID, UUID.Zero, (int)AssetType.Folder, "My Inventory"));
  94. result = true;
  95. }
  96. XInventoryFolder[] sysFolders = GetSystemFolders(principalID);
  97. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.Animation) return true; return false; }))
  98. CreateFolder(principalID, rootFolder.ID, (int)AssetType.Animation, "Animations");
  99. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.Bodypart) return true; return false; }))
  100. CreateFolder(principalID, rootFolder.ID, (int)AssetType.Bodypart, "Body Parts");
  101. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.CallingCard) return true; return false; }))
  102. CreateFolder(principalID, rootFolder.ID, (int)AssetType.CallingCard, "Calling Cards");
  103. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.Clothing) return true; return false; }))
  104. CreateFolder(principalID, rootFolder.ID, (int)AssetType.Clothing, "Clothing");
  105. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.Gesture) return true; return false; }))
  106. CreateFolder(principalID, rootFolder.ID, (int)AssetType.Gesture, "Gestures");
  107. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.Landmark) return true; return false; }))
  108. CreateFolder(principalID, rootFolder.ID, (int)AssetType.Landmark, "Landmarks");
  109. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.LostAndFoundFolder) return true; return false; }))
  110. CreateFolder(principalID, rootFolder.ID, (int)AssetType.LostAndFoundFolder, "Lost And Found");
  111. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.Notecard) return true; return false; }))
  112. CreateFolder(principalID, rootFolder.ID, (int)AssetType.Notecard, "Notecards");
  113. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.Object) return true; return false; }))
  114. CreateFolder(principalID, rootFolder.ID, (int)AssetType.Object, "Objects");
  115. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.SnapshotFolder) return true; return false; }))
  116. CreateFolder(principalID, rootFolder.ID, (int)AssetType.SnapshotFolder, "Photo Album");
  117. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.LSLText) return true; return false; }))
  118. CreateFolder(principalID, rootFolder.ID, (int)AssetType.LSLText, "Scripts");
  119. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.Sound) return true; return false; }))
  120. CreateFolder(principalID, rootFolder.ID, (int)AssetType.Sound, "Sounds");
  121. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.Texture) return true; return false; }))
  122. CreateFolder(principalID, rootFolder.ID, (int)AssetType.Texture, "Textures");
  123. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)AssetType.TrashFolder) return true; return false; }))
  124. CreateFolder(principalID, rootFolder.ID, (int)AssetType.TrashFolder, "Trash");
  125. return result;
  126. }
  127. private XInventoryFolder CreateFolder(UUID principalID, UUID parentID, int type, string name)
  128. {
  129. XInventoryFolder newFolder = new XInventoryFolder();
  130. newFolder.folderName = name;
  131. newFolder.type = type;
  132. newFolder.version = 1;
  133. newFolder.folderID = UUID.Random();
  134. newFolder.agentID = principalID;
  135. newFolder.parentFolderID = parentID;
  136. m_Database.StoreFolder(newFolder);
  137. return newFolder;
  138. }
  139. private XInventoryFolder[] GetSystemFolders(UUID principalID)
  140. {
  141. XInventoryFolder[] allFolders = m_Database.GetFolders(
  142. new string[] { "agentID" },
  143. new string[] { principalID.ToString() });
  144. XInventoryFolder[] sysFolders = Array.FindAll(
  145. allFolders,
  146. delegate (XInventoryFolder f)
  147. {
  148. if (f.type > 0)
  149. return true;
  150. return false;
  151. });
  152. return sysFolders;
  153. }
  154. public List<InventoryFolderBase> GetInventorySkeleton(UUID principalID)
  155. {
  156. XInventoryFolder[] allFolders = m_Database.GetFolders(
  157. new string[] { "agentID" },
  158. new string[] { principalID.ToString() });
  159. if (allFolders.Length == 0)
  160. return null;
  161. List<InventoryFolderBase> folders = new List<InventoryFolderBase>();
  162. foreach (XInventoryFolder x in allFolders)
  163. {
  164. m_log.DebugFormat("[INVENTORY]: Adding folder {0} to skeleton", x.folderName);
  165. folders.Add(ConvertToOpenSim(x));
  166. }
  167. return folders;
  168. }
  169. public InventoryFolderBase GetRootFolder(UUID principalID)
  170. {
  171. XInventoryFolder[] folders = m_Database.GetFolders(
  172. new string[] { "agentID", "parentFolderID"},
  173. new string[] { principalID.ToString(), UUID.Zero.ToString() });
  174. if (folders.Length == 0)
  175. return null;
  176. return ConvertToOpenSim(folders[0]);
  177. }
  178. public InventoryFolderBase GetFolderForType(UUID principalID, AssetType type)
  179. {
  180. XInventoryFolder[] folders = m_Database.GetFolders(
  181. new string[] { "agentID", "type"},
  182. new string[] { principalID.ToString(), ((int)type).ToString() });
  183. if (folders.Length == 0)
  184. return null;
  185. return ConvertToOpenSim(folders[0]);
  186. }
  187. public InventoryCollection GetFolderContent(UUID principalID, UUID folderID)
  188. {
  189. // This method doesn't receive a valud principal id from the
  190. // connector. So we disregard the principal and look
  191. // by ID.
  192. //
  193. m_log.DebugFormat("[INVENTORY]: Fetch contents for folder {0}", folderID.ToString());
  194. InventoryCollection inventory = new InventoryCollection();
  195. inventory.UserID = principalID;
  196. inventory.Folders = new List<InventoryFolderBase>();
  197. inventory.Items = new List<InventoryItemBase>();
  198. XInventoryFolder[] folders = m_Database.GetFolders(
  199. new string[] { "parentFolderID"},
  200. new string[] { folderID.ToString() });
  201. foreach (XInventoryFolder x in folders)
  202. {
  203. m_log.DebugFormat("[INVENTORY]: Adding folder {0} to response", x.folderName);
  204. inventory.Folders.Add(ConvertToOpenSim(x));
  205. }
  206. XInventoryItem[] items = m_Database.GetItems(
  207. new string[] { "parentFolderID"},
  208. new string[] { folderID.ToString() });
  209. foreach (XInventoryItem i in items)
  210. {
  211. m_log.DebugFormat("[INVENTORY]: Adding item {0} to response", i.inventoryName);
  212. inventory.Items.Add(ConvertToOpenSim(i));
  213. }
  214. return inventory;
  215. }
  216. public List<InventoryItemBase> GetFolderItems(UUID principalID, UUID folderID)
  217. {
  218. // Since we probably don't get a valid principal here, either ...
  219. //
  220. List<InventoryItemBase> invItems = new List<InventoryItemBase>();
  221. XInventoryItem[] items = m_Database.GetItems(
  222. new string[] { "parentFolderID"},
  223. new string[] { UUID.Zero.ToString() });
  224. foreach (XInventoryItem i in items)
  225. invItems.Add(ConvertToOpenSim(i));
  226. return invItems;
  227. }
  228. public bool AddFolder(InventoryFolderBase folder)
  229. {
  230. InventoryFolderBase check = GetFolder(folder);
  231. if (check != null)
  232. return false;
  233. XInventoryFolder xFolder = ConvertFromOpenSim(folder);
  234. return m_Database.StoreFolder(xFolder);
  235. }
  236. public bool UpdateFolder(InventoryFolderBase folder)
  237. {
  238. XInventoryFolder xFolder = ConvertFromOpenSim(folder);
  239. InventoryFolderBase check = GetFolder(folder);
  240. if (check == null)
  241. return AddFolder(folder);
  242. if (check.Type != -1 || xFolder.type != -1)
  243. {
  244. if (xFolder.version > check.Version)
  245. return false;
  246. check.Version = (ushort)xFolder.version;
  247. xFolder = ConvertFromOpenSim(check);
  248. return m_Database.StoreFolder(xFolder);
  249. }
  250. if (xFolder.version < check.Version)
  251. xFolder.version = check.Version;
  252. xFolder.folderID = check.ID;
  253. return m_Database.StoreFolder(xFolder);
  254. }
  255. public bool MoveFolder(InventoryFolderBase folder)
  256. {
  257. XInventoryFolder[] x = m_Database.GetFolders(
  258. new string[] { "folderID" },
  259. new string[] { folder.ID.ToString() });
  260. if (x.Length == 0)
  261. return false;
  262. x[0].parentFolderID = folder.ParentID;
  263. return m_Database.StoreFolder(x[0]);
  264. }
  265. // We don't check the principal's ID here
  266. //
  267. public bool DeleteFolders(UUID principalID, List<UUID> folderIDs)
  268. {
  269. if (!m_AllowDelete)
  270. return false;
  271. // Ignore principal ID, it's bogus at connector level
  272. //
  273. foreach (UUID id in folderIDs)
  274. {
  275. if (!ParentIsTrash(id))
  276. continue;
  277. InventoryFolderBase f = new InventoryFolderBase();
  278. f.ID = id;
  279. PurgeFolder(f);
  280. m_Database.DeleteFolders("folderID", id.ToString());
  281. }
  282. return true;
  283. }
  284. public bool PurgeFolder(InventoryFolderBase folder)
  285. {
  286. if (!m_AllowDelete)
  287. return false;
  288. if (!ParentIsTrash(folder.ID))
  289. return false;
  290. XInventoryFolder[] subFolders = m_Database.GetFolders(
  291. new string[] { "parentFolderID" },
  292. new string[] { folder.ID.ToString() });
  293. foreach (XInventoryFolder x in subFolders)
  294. {
  295. PurgeFolder(ConvertToOpenSim(x));
  296. m_Database.DeleteFolders("folderID", x.folderID.ToString());
  297. }
  298. m_Database.DeleteItems("parentFolderID", folder.ID.ToString());
  299. return true;
  300. }
  301. public bool AddItem(InventoryItemBase item)
  302. {
  303. return m_Database.StoreItem(ConvertFromOpenSim(item));
  304. }
  305. public bool UpdateItem(InventoryItemBase item)
  306. {
  307. return m_Database.StoreItem(ConvertFromOpenSim(item));
  308. }
  309. public bool MoveItems(UUID principalID, List<InventoryItemBase> items)
  310. {
  311. // Principal is b0rked. *sigh*
  312. //
  313. foreach (InventoryItemBase i in items)
  314. {
  315. m_Database.MoveItem(i.ID.ToString(), i.Folder.ToString());
  316. }
  317. return true;
  318. }
  319. public bool DeleteItems(UUID principalID, List<UUID> itemIDs)
  320. {
  321. if (!m_AllowDelete)
  322. return false;
  323. // Just use the ID... *facepalms*
  324. //
  325. foreach (UUID id in itemIDs)
  326. m_Database.DeleteItems("inventoryID", id.ToString());
  327. return true;
  328. }
  329. public InventoryItemBase GetItem(InventoryItemBase item)
  330. {
  331. XInventoryItem[] items = m_Database.GetItems(
  332. new string[] { "inventoryID" },
  333. new string[] { item.ID.ToString() });
  334. if (items.Length == 0)
  335. return null;
  336. return ConvertToOpenSim(items[0]);
  337. }
  338. public InventoryFolderBase GetFolder(InventoryFolderBase folder)
  339. {
  340. XInventoryFolder[] folders = m_Database.GetFolders(
  341. new string[] { "folderID"},
  342. new string[] { folder.ID.ToString() });
  343. if (folders.Length == 0)
  344. return null;
  345. return ConvertToOpenSim(folders[0]);
  346. }
  347. public List<InventoryItemBase> GetActiveGestures(UUID principalID)
  348. {
  349. XInventoryItem[] items = m_Database.GetActiveGestures(principalID);
  350. if (items.Length == 0)
  351. return null;
  352. List<InventoryItemBase> ret = new List<InventoryItemBase>();
  353. foreach (XInventoryItem x in items)
  354. ret.Add(ConvertToOpenSim(x));
  355. return ret;
  356. }
  357. public int GetAssetPermissions(UUID principalID, UUID assetID)
  358. {
  359. return m_Database.GetAssetPermissions(principalID, assetID);
  360. }
  361. // CM never needed those. Left unimplemented.
  362. // Obsolete in core
  363. //
  364. public InventoryCollection GetUserInventory(UUID userID)
  365. {
  366. return null;
  367. }
  368. public void GetUserInventory(UUID userID, InventoryReceiptCallback callback)
  369. {
  370. }
  371. // Unused.
  372. //
  373. public bool HasInventoryForUser(UUID userID)
  374. {
  375. return false;
  376. }
  377. // CM Helpers
  378. //
  379. private InventoryFolderBase ConvertToOpenSim(XInventoryFolder folder)
  380. {
  381. InventoryFolderBase newFolder = new InventoryFolderBase();
  382. newFolder.ParentID = folder.parentFolderID;
  383. newFolder.Type = (short)folder.type;
  384. newFolder.Version = (ushort)folder.version;
  385. newFolder.Name = folder.folderName;
  386. newFolder.Owner = folder.agentID;
  387. newFolder.ID = folder.folderID;
  388. return newFolder;
  389. }
  390. private XInventoryFolder ConvertFromOpenSim(InventoryFolderBase folder)
  391. {
  392. XInventoryFolder newFolder = new XInventoryFolder();
  393. newFolder.parentFolderID = folder.ParentID;
  394. newFolder.type = (int)folder.Type;
  395. newFolder.version = (int)folder.Version;
  396. newFolder.folderName = folder.Name;
  397. newFolder.agentID = folder.Owner;
  398. newFolder.folderID = folder.ID;
  399. return newFolder;
  400. }
  401. private InventoryItemBase ConvertToOpenSim(XInventoryItem item)
  402. {
  403. InventoryItemBase newItem = new InventoryItemBase();
  404. newItem.AssetID = item.assetID;
  405. newItem.AssetType = item.assetType;
  406. newItem.Name = item.inventoryName;
  407. newItem.Owner = item.avatarID;
  408. newItem.ID = item.inventoryID;
  409. newItem.InvType = item.invType;
  410. newItem.Folder = item.parentFolderID;
  411. newItem.CreatorId = item.creatorID.ToString();
  412. newItem.Description = item.inventoryDescription;
  413. newItem.NextPermissions = (uint)item.inventoryNextPermissions;
  414. newItem.CurrentPermissions = (uint)item.inventoryCurrentPermissions;
  415. newItem.BasePermissions = (uint)item.inventoryBasePermissions;
  416. newItem.EveryOnePermissions = (uint)item.inventoryEveryOnePermissions;
  417. newItem.GroupPermissions = (uint)item.inventoryGroupPermissions;
  418. newItem.GroupID = item.groupID;
  419. newItem.GroupOwned = item.groupOwned;
  420. newItem.SalePrice = item.salePrice;
  421. newItem.SaleType = (byte)item.saleType;
  422. newItem.Flags = (uint)item.flags;
  423. newItem.CreationDate = item.creationDate;
  424. return newItem;
  425. }
  426. private XInventoryItem ConvertFromOpenSim(InventoryItemBase item)
  427. {
  428. XInventoryItem newItem = new XInventoryItem();
  429. newItem.assetID = item.AssetID;
  430. newItem.assetType = item.AssetType;
  431. newItem.inventoryName = item.Name;
  432. newItem.avatarID = item.Owner;
  433. newItem.inventoryID = item.ID;
  434. newItem.invType = item.InvType;
  435. newItem.parentFolderID = item.Folder;
  436. newItem.creatorID = item.CreatorIdAsUuid;
  437. newItem.inventoryDescription = item.Description;
  438. newItem.inventoryNextPermissions = (int)item.NextPermissions;
  439. newItem.inventoryCurrentPermissions = (int)item.CurrentPermissions;
  440. newItem.inventoryBasePermissions = (int)item.BasePermissions;
  441. newItem.inventoryEveryOnePermissions = (int)item.EveryOnePermissions;
  442. newItem.inventoryGroupPermissions = (int)item.GroupPermissions;
  443. newItem.groupID = item.GroupID;
  444. newItem.groupOwned = item.GroupOwned;
  445. newItem.salePrice = item.SalePrice;
  446. newItem.saleType = (int)item.SaleType;
  447. newItem.flags = (int)item.Flags;
  448. newItem.creationDate = item.CreationDate;
  449. return newItem;
  450. }
  451. private bool ParentIsTrash(UUID folderID)
  452. {
  453. XInventoryFolder[] folder = m_Database.GetFolders(new string[] {"folderID"}, new string[] {folderID.ToString()});
  454. if (folder.Length < 1)
  455. return false;
  456. UUID parentFolder = folder[0].parentFolderID;
  457. while (parentFolder != UUID.Zero)
  458. {
  459. XInventoryFolder[] parent = m_Database.GetFolders(new string[] {"folderID"}, new string[] {parentFolder.ToString()});
  460. if (parent.Length < 1)
  461. return false;
  462. if (parent[0].type == (int)AssetType.TrashFolder)
  463. return true;
  464. if (parent[0].type == (int)AssetType.RootFolder)
  465. return false;
  466. parentFolder = parent[0].parentFolderID;
  467. }
  468. return false;
  469. }
  470. }
  471. }