1
0

XInventoryService.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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 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.Folder, "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. private 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. private 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 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("[INVENTORY]: Adding folder {0} to skeleton", x.folderName);
  163. folders.Add(ConvertToOpenSim(x));
  164. }
  165. return folders;
  166. }
  167. public 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. return ConvertToOpenSim(folders[0]);
  175. }
  176. public InventoryFolderBase GetFolderForType(UUID principalID, AssetType type)
  177. {
  178. XInventoryFolder[] folders = m_Database.GetFolders(
  179. new string[] { "agentID", "type"},
  180. new string[] { principalID.ToString(), ((int)type).ToString() });
  181. if (folders.Length == 0)
  182. return null;
  183. return ConvertToOpenSim(folders[0]);
  184. }
  185. public InventoryCollection GetFolderContent(UUID principalID, UUID folderID)
  186. {
  187. // This method doesn't receive a valud principal id from the
  188. // connector. So we disregard the principal and look
  189. // by ID.
  190. //
  191. m_log.DebugFormat("[INVENTORY]: Fetch contents for folder {0}", folderID.ToString());
  192. InventoryCollection inventory = new InventoryCollection();
  193. inventory.UserID = principalID;
  194. inventory.Folders = new List<InventoryFolderBase>();
  195. inventory.Items = new List<InventoryItemBase>();
  196. XInventoryFolder[] folders = m_Database.GetFolders(
  197. new string[] { "parentFolderID"},
  198. new string[] { folderID.ToString() });
  199. foreach (XInventoryFolder x in folders)
  200. {
  201. m_log.DebugFormat("[INVENTORY]: Adding folder {0} to response", x.folderName);
  202. inventory.Folders.Add(ConvertToOpenSim(x));
  203. }
  204. XInventoryItem[] items = m_Database.GetItems(
  205. new string[] { "parentFolderID"},
  206. new string[] { folderID.ToString() });
  207. foreach (XInventoryItem i in items)
  208. {
  209. m_log.DebugFormat("[INVENTORY]: Adding item {0} to response", i.inventoryName);
  210. inventory.Items.Add(ConvertToOpenSim(i));
  211. }
  212. return inventory;
  213. }
  214. public List<InventoryItemBase> GetFolderItems(UUID principalID, UUID folderID)
  215. {
  216. // Since we probably don't get a valid principal here, either ...
  217. //
  218. List<InventoryItemBase> invItems = new List<InventoryItemBase>();
  219. XInventoryItem[] items = m_Database.GetItems(
  220. new string[] { "parentFolderID"},
  221. new string[] { UUID.Zero.ToString() });
  222. foreach (XInventoryItem i in items)
  223. invItems.Add(ConvertToOpenSim(i));
  224. return invItems;
  225. }
  226. public bool AddFolder(InventoryFolderBase folder)
  227. {
  228. XInventoryFolder xFolder = ConvertFromOpenSim(folder);
  229. return m_Database.StoreFolder(xFolder);
  230. }
  231. public bool UpdateFolder(InventoryFolderBase folder)
  232. {
  233. return AddFolder(folder);
  234. }
  235. public bool MoveFolder(InventoryFolderBase folder)
  236. {
  237. XInventoryFolder[] x = m_Database.GetFolders(
  238. new string[] { "folderID" },
  239. new string[] { folder.ID.ToString() });
  240. if (x.Length == 0)
  241. return false;
  242. x[0].parentFolderID = folder.ParentID;
  243. return m_Database.StoreFolder(x[0]);
  244. }
  245. // We don't check the principal's ID here
  246. //
  247. public bool DeleteFolders(UUID principalID, List<UUID> folderIDs)
  248. {
  249. // Ignore principal ID, it's bogus at connector level
  250. //
  251. foreach (UUID id in folderIDs)
  252. {
  253. InventoryFolderBase f = new InventoryFolderBase();
  254. f.ID = id;
  255. PurgeFolder(f);
  256. m_Database.DeleteFolders("folderID", id.ToString());
  257. }
  258. return true;
  259. }
  260. public bool PurgeFolder(InventoryFolderBase folder)
  261. {
  262. XInventoryFolder[] subFolders = m_Database.GetFolders(
  263. new string[] { "parentFolderID" },
  264. new string[] { folder.ID.ToString() });
  265. foreach (XInventoryFolder x in subFolders)
  266. {
  267. PurgeFolder(ConvertToOpenSim(x));
  268. m_Database.DeleteFolders("folderID", x.folderID.ToString());
  269. }
  270. m_Database.DeleteItems("parentFolderID", folder.ID.ToString());
  271. return true;
  272. }
  273. public bool AddItem(InventoryItemBase item)
  274. {
  275. return m_Database.StoreItem(ConvertFromOpenSim(item));
  276. }
  277. public bool UpdateItem(InventoryItemBase item)
  278. {
  279. return m_Database.StoreItem(ConvertFromOpenSim(item));
  280. }
  281. public bool MoveItems(UUID principalID, List<InventoryItemBase> items)
  282. {
  283. // Principal is b0rked. *sigh*
  284. //
  285. foreach (InventoryItemBase i in items)
  286. {
  287. m_Database.MoveItem(i.ID.ToString(), i.Folder.ToString());
  288. }
  289. return true;
  290. }
  291. public bool DeleteItems(UUID principalID, List<UUID> itemIDs)
  292. {
  293. // Just use the ID... *facepalms*
  294. //
  295. foreach (UUID id in itemIDs)
  296. m_Database.DeleteItems("inventoryID", id.ToString());
  297. return true;
  298. }
  299. public InventoryItemBase GetItem(InventoryItemBase item)
  300. {
  301. XInventoryItem[] items = m_Database.GetItems(
  302. new string[] { "inventoryID" },
  303. new string[] { item.ID.ToString() });
  304. if (items.Length == 0)
  305. return null;
  306. return ConvertToOpenSim(items[0]);
  307. }
  308. public InventoryFolderBase GetFolder(InventoryFolderBase folder)
  309. {
  310. XInventoryFolder[] folders = m_Database.GetFolders(
  311. new string[] { "folderID"},
  312. new string[] { folder.ID.ToString() });
  313. if (folders.Length == 0)
  314. return null;
  315. return ConvertToOpenSim(folders[0]);
  316. }
  317. public List<InventoryItemBase> GetActiveGestures(UUID principalID)
  318. {
  319. XInventoryItem[] items = m_Database.GetActiveGestures(principalID);
  320. if (items.Length == 0)
  321. return null;
  322. List<InventoryItemBase> ret = new List<InventoryItemBase>();
  323. foreach (XInventoryItem x in items)
  324. ret.Add(ConvertToOpenSim(x));
  325. return ret;
  326. }
  327. public int GetAssetPermissions(UUID principalID, UUID assetID)
  328. {
  329. return m_Database.GetAssetPermissions(principalID, assetID);
  330. }
  331. // CM never needed those. Left unimplemented.
  332. // Obsolete in core
  333. //
  334. public InventoryCollection GetUserInventory(UUID userID)
  335. {
  336. return null;
  337. }
  338. public void GetUserInventory(UUID userID, InventoryReceiptCallback callback)
  339. {
  340. }
  341. // Unused.
  342. //
  343. public bool HasInventoryForUser(UUID userID)
  344. {
  345. return false;
  346. }
  347. // CM Helpers
  348. //
  349. private InventoryFolderBase ConvertToOpenSim(XInventoryFolder folder)
  350. {
  351. InventoryFolderBase newFolder = new InventoryFolderBase();
  352. newFolder.ParentID = folder.parentFolderID;
  353. newFolder.Type = (short)folder.type;
  354. newFolder.Version = (ushort)folder.version;
  355. newFolder.Name = folder.folderName;
  356. newFolder.Owner = folder.agentID;
  357. newFolder.ID = folder.folderID;
  358. return newFolder;
  359. }
  360. private XInventoryFolder ConvertFromOpenSim(InventoryFolderBase folder)
  361. {
  362. XInventoryFolder newFolder = new XInventoryFolder();
  363. newFolder.parentFolderID = folder.ParentID;
  364. newFolder.type = (int)folder.Type;
  365. newFolder.version = (int)folder.Version;
  366. newFolder.folderName = folder.Name;
  367. newFolder.agentID = folder.Owner;
  368. newFolder.folderID = folder.ID;
  369. return newFolder;
  370. }
  371. private InventoryItemBase ConvertToOpenSim(XInventoryItem item)
  372. {
  373. InventoryItemBase newItem = new InventoryItemBase();
  374. newItem.AssetID = item.assetID;
  375. newItem.AssetType = item.assetType;
  376. newItem.Name = item.inventoryName;
  377. newItem.Owner = item.avatarID;
  378. newItem.ID = item.inventoryID;
  379. newItem.InvType = item.invType;
  380. newItem.Folder = item.parentFolderID;
  381. newItem.CreatorId = item.creatorID.ToString();
  382. newItem.Description = item.inventoryDescription;
  383. newItem.NextPermissions = (uint)item.inventoryNextPermissions;
  384. newItem.CurrentPermissions = (uint)item.inventoryCurrentPermissions;
  385. newItem.BasePermissions = (uint)item.inventoryBasePermissions;
  386. newItem.EveryOnePermissions = (uint)item.inventoryEveryOnePermissions;
  387. newItem.GroupPermissions = (uint)item.inventoryGroupPermissions;
  388. newItem.GroupID = item.groupID;
  389. newItem.GroupOwned = item.groupOwned;
  390. newItem.SalePrice = item.salePrice;
  391. newItem.SaleType = (byte)item.saleType;
  392. newItem.Flags = (uint)item.flags;
  393. newItem.CreationDate = item.creationDate;
  394. return newItem;
  395. }
  396. private XInventoryItem ConvertFromOpenSim(InventoryItemBase item)
  397. {
  398. XInventoryItem newItem = new XInventoryItem();
  399. newItem.assetID = item.AssetID;
  400. newItem.assetType = item.AssetType;
  401. newItem.inventoryName = item.Name;
  402. newItem.avatarID = item.Owner;
  403. newItem.inventoryID = item.ID;
  404. newItem.invType = item.InvType;
  405. newItem.parentFolderID = item.Folder;
  406. newItem.creatorID = item.CreatorIdAsUuid;
  407. newItem.inventoryDescription = item.Description;
  408. newItem.inventoryNextPermissions = (int)item.NextPermissions;
  409. newItem.inventoryCurrentPermissions = (int)item.CurrentPermissions;
  410. newItem.inventoryBasePermissions = (int)item.BasePermissions;
  411. newItem.inventoryEveryOnePermissions = (int)item.EveryOnePermissions;
  412. newItem.inventoryGroupPermissions = (int)item.GroupPermissions;
  413. newItem.groupID = item.GroupID;
  414. newItem.groupOwned = item.GroupOwned;
  415. newItem.salePrice = item.SalePrice;
  416. newItem.saleType = (int)item.SaleType;
  417. newItem.flags = (int)item.Flags;
  418. newItem.creationDate = item.CreationDate;
  419. return newItem;
  420. }
  421. }
  422. }