XInventoryService.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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 = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. protected IXInventoryData m_Database;
  43. protected bool m_AllowDelete = true;
  44. protected string m_ConfigName = "InventoryService";
  45. public XInventoryService(IConfigSource config)
  46. : this(config, "InventoryService")
  47. {
  48. }
  49. public XInventoryService(IConfigSource config, string configName) : base(config)
  50. {
  51. if (configName != string.Empty)
  52. m_ConfigName = configName;
  53. string dllName = String.Empty;
  54. string connString = String.Empty;
  55. //string realm = "Inventory"; // OSG version doesn't use this
  56. //
  57. // Try reading the [InventoryService] section first, if it exists
  58. //
  59. IConfig authConfig = config.Configs[m_ConfigName];
  60. if (authConfig != null)
  61. {
  62. dllName = authConfig.GetString("StorageProvider", dllName);
  63. connString = authConfig.GetString("ConnectionString", connString);
  64. m_AllowDelete = authConfig.GetBoolean("AllowDelete", true);
  65. // realm = authConfig.GetString("Realm", realm);
  66. }
  67. //
  68. // Try reading the [DatabaseService] section, if it exists
  69. //
  70. IConfig dbConfig = config.Configs["DatabaseService"];
  71. if (dbConfig != null)
  72. {
  73. if (dllName.Length == 0)
  74. dllName = dbConfig.GetString("StorageProvider", String.Empty);
  75. if (connString.Length == 0)
  76. connString = dbConfig.GetString("ConnectionString", String.Empty);
  77. }
  78. //
  79. // We tried, but this doesn't exist. We can't proceed.
  80. //
  81. if (dllName.Length == 0)
  82. throw new Exception("No StorageProvider configured");
  83. m_Database = LoadPlugin<IXInventoryData>(dllName,
  84. new Object[] {connString, String.Empty});
  85. if (m_Database == null)
  86. throw new Exception("Could not find a storage interface in the given module");
  87. }
  88. public virtual bool CreateUserInventory(UUID principalID)
  89. {
  90. // This is braindeaad. We can't ever communicate that we fixed
  91. // an existing inventory. Well, just return root folder status,
  92. // but check sanity anyway.
  93. //
  94. bool result = false;
  95. InventoryFolderBase rootFolder = GetRootFolder(principalID);
  96. if (rootFolder == null)
  97. {
  98. rootFolder = ConvertToOpenSim(CreateFolder(principalID, UUID.Zero, (int)FolderType.Root, InventoryFolderBase.ROOT_FOLDER_NAME));
  99. result = true;
  100. }
  101. XInventoryFolder[] sysFolders = GetSystemFolders(principalID, rootFolder.ID);
  102. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)FolderType.Animation) return true; return false; }))
  103. CreateFolder(principalID, rootFolder.ID, (int)FolderType.Animation, "Animations");
  104. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)FolderType.BodyPart) return true; return false; }))
  105. CreateFolder(principalID, rootFolder.ID, (int)FolderType.BodyPart, "Body Parts");
  106. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)FolderType.CallingCard) return true; return false; }))
  107. {
  108. XInventoryFolder folder = CreateFolder(principalID, rootFolder.ID, (int)FolderType.CallingCard, "Calling Cards");
  109. folder = CreateFolder(principalID, folder.folderID, (int)FolderType.CallingCard, "Friends");
  110. CreateFolder(principalID, folder.folderID, (int)FolderType.CallingCard, "All");
  111. }
  112. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)FolderType.Clothing) return true; return false; }))
  113. CreateFolder(principalID, rootFolder.ID, (int)FolderType.Clothing, "Clothing");
  114. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)FolderType.CurrentOutfit) return true; return false; }))
  115. CreateFolder(principalID, rootFolder.ID, (int)FolderType.CurrentOutfit, "Current Outfit");
  116. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)FolderType.Favorites) return true; return false; }))
  117. CreateFolder(principalID, rootFolder.ID, (int)FolderType.Favorites, "Favorites");
  118. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)FolderType.Gesture) return true; return false; }))
  119. CreateFolder(principalID, rootFolder.ID, (int)FolderType.Gesture, "Gestures");
  120. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)FolderType.Landmark) return true; return false; }))
  121. CreateFolder(principalID, rootFolder.ID, (int)FolderType.Landmark, "Landmarks");
  122. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)FolderType.LostAndFound) return true; return false; }))
  123. CreateFolder(principalID, rootFolder.ID, (int)FolderType.LostAndFound, "Lost And Found");
  124. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)FolderType.Notecard) return true; return false; }))
  125. CreateFolder(principalID, rootFolder.ID, (int)FolderType.Notecard, "Notecards");
  126. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)FolderType.Object) return true; return false; }))
  127. CreateFolder(principalID, rootFolder.ID, (int)FolderType.Object, "Objects");
  128. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)FolderType.Snapshot) return true; return false; }))
  129. CreateFolder(principalID, rootFolder.ID, (int)FolderType.Snapshot, "Photo Album");
  130. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)FolderType.LSLText) return true; return false; }))
  131. CreateFolder(principalID, rootFolder.ID, (int)FolderType.LSLText, "Scripts");
  132. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)FolderType.Sound) return true; return false; }))
  133. CreateFolder(principalID, rootFolder.ID, (int)FolderType.Sound, "Sounds");
  134. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)FolderType.Texture) return true; return false; }))
  135. CreateFolder(principalID, rootFolder.ID, (int)FolderType.Texture, "Textures");
  136. if (!Array.Exists(sysFolders, delegate(XInventoryFolder f) { if (f.type == (int)FolderType.Trash) return true; return false; }))
  137. CreateFolder(principalID, rootFolder.ID, (int)FolderType.Trash, "Trash");
  138. if (!Array.Exists(sysFolders, delegate (XInventoryFolder f) { if (f.type == (int)FolderType.Settings) return true; return false; }))
  139. CreateFolder(principalID, rootFolder.ID, (int)FolderType.Settings, "Settings");
  140. return result;
  141. }
  142. protected XInventoryFolder CreateFolder(UUID principalID, UUID parentID, int type, string name)
  143. {
  144. var newFolder = new XInventoryFolder
  145. {
  146. folderName = name,
  147. type = type,
  148. version = 1,
  149. folderID = UUID.Random(),
  150. agentID = principalID,
  151. parentFolderID = parentID
  152. };
  153. m_Database.StoreFolder(newFolder);
  154. return newFolder;
  155. }
  156. protected virtual XInventoryFolder[] GetSystemFolders(UUID principalID, UUID rootID)
  157. {
  158. // m_log.DebugFormat("[XINVENTORY SERVICE]: Getting system folders for {0}", principalID);
  159. XInventoryFolder[] allFolders = m_Database.GetFolders(
  160. new string[] { "agentID", "parentFolderID" },
  161. new string[] { principalID.ToString(), rootID.ToString() });
  162. XInventoryFolder[] sysFolders = Array.FindAll(
  163. allFolders,
  164. delegate (XInventoryFolder f)
  165. {
  166. if (f.type > 0)
  167. return true;
  168. return false;
  169. });
  170. // m_log.DebugFormat(
  171. // "[XINVENTORY SERVICE]: Found {0} system folders for {1}", sysFolders.Length, principalID);
  172. return sysFolders;
  173. }
  174. public virtual List<InventoryFolderBase> GetInventorySkeleton(UUID principalID)
  175. {
  176. XInventoryFolder[] allFolders = m_Database.GetFolders(
  177. new string[] { "agentID" },
  178. new string[] { principalID.ToString() });
  179. if (allFolders.Length == 0)
  180. return null;
  181. List<InventoryFolderBase> folders = new();
  182. foreach (XInventoryFolder x in allFolders)
  183. {
  184. //m_log.DebugFormat("[XINVENTORY SERVICE]: Adding folder {0} to skeleton", x.folderName);
  185. folders.Add(ConvertToOpenSim(x));
  186. }
  187. return folders;
  188. }
  189. public virtual InventoryFolderBase GetRootFolder(UUID principalID)
  190. {
  191. XInventoryFolder[] folders = m_Database.GetFolders(
  192. new string[] { "agentID", "parentFolderID"},
  193. new string[] { principalID.ToString(), UUID.Zero.ToString() });
  194. if (folders.Length == 0)
  195. return null;
  196. XInventoryFolder root = null;
  197. foreach (XInventoryFolder folder in folders)
  198. {
  199. if (folder.folderName == InventoryFolderBase.ROOT_FOLDER_NAME)
  200. {
  201. root = folder;
  202. break;
  203. }
  204. }
  205. root ??= folders[0]; //oops
  206. return ConvertToOpenSim(root);
  207. }
  208. public virtual InventoryFolderBase GetFolderForType(UUID principalID, FolderType type)
  209. {
  210. // m_log.DebugFormat("[XINVENTORY SERVICE]: Getting folder type {0} for user {1}", type, principalID);
  211. InventoryFolderBase rootFolder = GetRootFolder(principalID);
  212. if (rootFolder == null)
  213. {
  214. m_log.WarnFormat(
  215. "[XINVENTORY]: Found no root folder for {0} in GetFolderForType() when looking for {1}",
  216. principalID, type);
  217. return null;
  218. }
  219. return GetSystemFolderForType(rootFolder, type);
  220. }
  221. private InventoryFolderBase GetSystemFolderForType(InventoryFolderBase rootFolder, FolderType type)
  222. {
  223. //m_log.DebugFormat("[XINVENTORY SERVICE]: Getting folder type {0}", type);
  224. if (type == FolderType.Root)
  225. return rootFolder;
  226. XInventoryFolder[] folders = m_Database.GetFolders(
  227. new string[] { "agentID", "parentFolderID", "type"},
  228. new string[] { rootFolder.Owner.ToString(), rootFolder.ID.ToString(), ((int)type).ToString() });
  229. if (folders.Length == 0)
  230. {
  231. //m_log.WarnFormat("[XINVENTORY SERVICE]: Found no folder for type {0} ", type);
  232. return null;
  233. }
  234. //m_log.DebugFormat(
  235. // "[XINVENTORY SERVICE]: Found folder {0} {1} for type {2}",
  236. // folders[0].folderName, folders[0].folderID, type);
  237. return ConvertToOpenSim(folders[0]);
  238. }
  239. public virtual InventoryCollection GetFolderContent(UUID principalID, UUID folderID)
  240. {
  241. // This method doesn't receive a valud principal id from the
  242. // connector. So we disregard the principal and look
  243. // by ID.
  244. //
  245. //m_log.DebugFormat("[XINVENTORY SERVICE]: Fetch contents for folder {0}", folderID.ToString());
  246. InventoryCollection inventory = new()
  247. {
  248. OwnerID = principalID,
  249. Folders = new List<InventoryFolderBase>(),
  250. Items = new List<InventoryItemBase>()
  251. };
  252. XInventoryFolder[] folders = m_Database.GetFolders(
  253. new string[] { "parentFolderID"},
  254. new string[] { folderID.ToString() });
  255. foreach (XInventoryFolder x in folders)
  256. {
  257. //m_log.DebugFormat("[XINVENTORY]: Adding folder {0} to response", x.folderName);
  258. inventory.Folders.Add(ConvertToOpenSim(x));
  259. }
  260. XInventoryItem[] items = m_Database.GetItems(
  261. new string[] { "parentFolderID"},
  262. new string[] { folderID.ToString() });
  263. foreach (XInventoryItem i in items)
  264. {
  265. //m_log.DebugFormat("[XINVENTORY]: Adding item {0} to response", i.inventoryName);
  266. inventory.Items.Add(ConvertToOpenSim(i));
  267. }
  268. InventoryFolderBase f = GetFolder(principalID, folderID);
  269. if (f != null)
  270. {
  271. inventory.Version = f.Version;
  272. inventory.OwnerID = f.Owner;
  273. }
  274. inventory.FolderID = folderID;
  275. return inventory;
  276. }
  277. public virtual InventoryCollection[] GetMultipleFoldersContent(UUID principalID, UUID[] folderIDs)
  278. {
  279. InventoryCollection[] multiple = new InventoryCollection[folderIDs.Length];
  280. int i = 0;
  281. foreach (UUID fid in folderIDs)
  282. multiple[i++] = GetFolderContent(principalID, fid);
  283. return multiple;
  284. }
  285. public virtual List<InventoryItemBase> GetFolderItems(UUID principalID, UUID folderID)
  286. {
  287. // m_log.DebugFormat("[XINVENTORY]: Fetch items for folder {0}", folderID);
  288. // Since we probably don't get a valid principal here, either ...
  289. //
  290. List<InventoryItemBase> invItems = new();
  291. XInventoryItem[] items = m_Database.GetItems(
  292. new string[] { "parentFolderID" },
  293. new string[] { folderID.ToString() });
  294. foreach (XInventoryItem i in items)
  295. invItems.Add(ConvertToOpenSim(i));
  296. return invItems;
  297. }
  298. public virtual bool AddFolder(InventoryFolderBase folder)
  299. {
  300. // m_log.DebugFormat("[XINVENTORY]: Add folder {0} type {1} in parent {2}", folder.Name, folder.Type, folder.ParentID);
  301. InventoryFolderBase check = GetFolder(folder.Owner, folder.ID);
  302. if (check != null)
  303. return false;
  304. if (folder.Type != (short)FolderType.None)
  305. {
  306. InventoryFolderBase rootFolder = GetRootFolder(folder.Owner);
  307. if (rootFolder == null)
  308. {
  309. m_log.WarnFormat(
  310. "[XINVENTORY]: Found no root folder for {0} in AddFolder() when looking for {1}",
  311. folder.Owner, folder.Type);
  312. return false;
  313. }
  314. // Check we're not trying to add this as a system folder.
  315. if (folder.ParentID == rootFolder.ID)
  316. {
  317. InventoryFolderBase existingSystemFolder
  318. = GetSystemFolderForType(rootFolder, (FolderType)folder.Type);
  319. if (existingSystemFolder != null)
  320. {
  321. m_log.WarnFormat(
  322. "[XINVENTORY]: System folder of type {0} already exists when tried to add {1} to {2} for {3}",
  323. folder.Type, folder.Name, folder.ParentID, folder.Owner);
  324. return false;
  325. }
  326. }
  327. }
  328. XInventoryFolder xFolder = ConvertFromOpenSim(folder);
  329. return m_Database.StoreFolder(xFolder);
  330. }
  331. public virtual bool UpdateFolder(InventoryFolderBase folder)
  332. {
  333. // m_log.DebugFormat("[XINVENTORY]: Update folder {0} {1} ({2})", folder.Name, folder.Type, folder.ID);
  334. XInventoryFolder xFolder = ConvertFromOpenSim(folder);
  335. InventoryFolderBase check = GetFolder(folder.Owner, folder.ID);
  336. if (check == null)
  337. return AddFolder(folder);
  338. if ((check.Type != (short)FolderType.None || xFolder.type != (short)FolderType.None)
  339. && (check.Type != (short)FolderType.Outfit || xFolder.type != (short)FolderType.Outfit))
  340. {
  341. if (xFolder.version < check.Version)
  342. {
  343. // m_log.DebugFormat("[XINVENTORY]: {0} < {1} can't do", xFolder.version, check.Version);
  344. return false;
  345. }
  346. check.Version = (ushort)xFolder.version;
  347. xFolder = ConvertFromOpenSim(check);
  348. // m_log.DebugFormat(
  349. // "[XINVENTORY]: Storing version only update to system folder {0} {1} {2}",
  350. // xFolder.folderName, xFolder.version, xFolder.type);
  351. return m_Database.StoreFolder(xFolder);
  352. }
  353. if (xFolder.version < check.Version)
  354. xFolder.version = check.Version;
  355. xFolder.folderID = check.ID;
  356. return m_Database.StoreFolder(xFolder);
  357. }
  358. public virtual bool MoveFolder(InventoryFolderBase folder)
  359. {
  360. return m_Database.MoveFolder(folder.ID.ToString(), folder.ParentID.ToString());
  361. }
  362. // We don't check the principal's ID here
  363. //
  364. public virtual bool DeleteFolders(UUID principalID, List<UUID> folderIDs)
  365. {
  366. return DeleteFolders(principalID, folderIDs, true);
  367. }
  368. public virtual bool DeleteFolders(UUID principalID, List<UUID> folderIDs, bool onlyIfTrash)
  369. {
  370. if (!m_AllowDelete)
  371. return false;
  372. // Ignore principal ID, it's bogus at connector level
  373. //
  374. foreach (UUID id in folderIDs)
  375. {
  376. //if (onlyIfTrash && !ParentIsTrash(id))
  377. if (onlyIfTrash && !ParentIsTrashOrLost(id))
  378. continue;
  379. //m_log.InfoFormat("[XINVENTORY SERVICE]: Delete folder {0}", id);
  380. InventoryFolderBase f = new() { ID = id };
  381. PurgeFolder(f, onlyIfTrash);
  382. m_Database.DeleteFolders("folderID", id.ToString());
  383. }
  384. return true;
  385. }
  386. public virtual bool PurgeFolder(InventoryFolderBase folder)
  387. {
  388. return PurgeFolder(folder, true);
  389. }
  390. public virtual bool PurgeFolder(InventoryFolderBase folder, bool onlyIfTrash)
  391. {
  392. if (!m_AllowDelete)
  393. return false;
  394. //if (onlyIfTrash && !ParentIsTrash(folder.ID))
  395. if (onlyIfTrash && !ParentIsTrashOrLost(folder.ID))
  396. return false;
  397. XInventoryFolder[] subFolders = m_Database.GetFolders(
  398. new string[] { "parentFolderID" },
  399. new string[] { folder.ID.ToString() });
  400. foreach (XInventoryFolder x in subFolders)
  401. {
  402. PurgeFolder(ConvertToOpenSim(x), onlyIfTrash);
  403. m_Database.DeleteFolders("folderID", x.folderID.ToString());
  404. }
  405. m_Database.DeleteItems("parentFolderID", folder.ID.ToString());
  406. return true;
  407. }
  408. public virtual bool AddItem(InventoryItemBase item)
  409. {
  410. // m_log.DebugFormat(
  411. // "[XINVENTORY SERVICE]: Adding item {0} {1} to folder {2} for {3}", item.Name, item.ID, item.Folder, item.Owner);
  412. return m_Database.StoreItem(ConvertFromOpenSim(item));
  413. }
  414. public virtual bool UpdateItem(InventoryItemBase item)
  415. {
  416. if (!m_AllowDelete)
  417. if (item.AssetType == (sbyte)AssetType.Link || item.AssetType == (sbyte)AssetType.LinkFolder)
  418. return false;
  419. // m_log.InfoFormat(
  420. // "[XINVENTORY SERVICE]: Updating item {0} {1} in folder {2}", item.Name, item.ID, item.Folder);
  421. InventoryItemBase retrievedItem = GetItem(item.Owner, item.ID);
  422. if (retrievedItem == null)
  423. {
  424. m_log.WarnFormat(
  425. "[XINVENTORY SERVICE]: Tried to update item {0} {1}, owner {2} but no existing item found.",
  426. item.Name, item.ID, item.Owner);
  427. return false;
  428. }
  429. // Do not allow invariants to change. Changes to folder ID occur in MoveItems()
  430. if (retrievedItem.InvType != item.InvType
  431. || retrievedItem.AssetType != item.AssetType
  432. || retrievedItem.Folder != item.Folder
  433. || retrievedItem.CreatorIdentification != item.CreatorIdentification
  434. || retrievedItem.Owner != item.Owner)
  435. {
  436. m_log.WarnFormat(
  437. "[XINVENTORY SERVICE]: Caller to UpdateItem() for {0} {1} tried to alter property(s) that should be invariant, (InvType, AssetType, Folder, CreatorIdentification, Owner), existing ({2}, {3}, {4}, {5}, {6}), update ({7}, {8}, {9}, {10}, {11})",
  438. retrievedItem.Name,
  439. retrievedItem.ID,
  440. retrievedItem.InvType,
  441. retrievedItem.AssetType,
  442. retrievedItem.Folder,
  443. retrievedItem.CreatorIdentification,
  444. retrievedItem.Owner,
  445. item.InvType,
  446. item.AssetType,
  447. item.Folder,
  448. item.CreatorIdentification,
  449. item.Owner);
  450. item.InvType = retrievedItem.InvType;
  451. item.AssetType = retrievedItem.AssetType;
  452. item.Folder = retrievedItem.Folder;
  453. item.CreatorIdentification = retrievedItem.CreatorIdentification;
  454. item.Owner = retrievedItem.Owner;
  455. }
  456. return m_Database.StoreItem(ConvertFromOpenSim(item));
  457. }
  458. public virtual bool MoveItems(UUID principalID, List<InventoryItemBase> items)
  459. {
  460. // Principal is b0rked. *sigh*
  461. //
  462. foreach (InventoryItemBase i in items)
  463. {
  464. m_Database.MoveItem(i.ID.ToString(), i.Folder.ToString());
  465. }
  466. return true;
  467. }
  468. public virtual bool DeleteItems(UUID principalID, List<UUID> itemIDs)
  469. {
  470. if (!m_AllowDelete)
  471. {
  472. // We must still allow links and links to folders to be deleted, otherwise they will build up
  473. // in the player's inventory until they can no longer log in. Deletions of links due to code bugs or
  474. // similar is inconvenient but on a par with accidental movement of items. The original item is never
  475. // touched.
  476. foreach (UUID id in itemIDs)
  477. {
  478. if (!m_Database.DeleteItems(
  479. new string[] { "inventoryID", "assetType" },
  480. new string[] { id.ToString(), ((sbyte)AssetType.Link).ToString() }))
  481. {
  482. m_Database.DeleteItems(
  483. new string[] { "inventoryID", "assetType" },
  484. new string[] { id.ToString(), ((sbyte)AssetType.LinkFolder).ToString() });
  485. }
  486. }
  487. }
  488. else
  489. {
  490. // Just use the ID... *facepalms*
  491. //
  492. foreach (UUID id in itemIDs)
  493. m_Database.DeleteItems("inventoryID", id.ToString());
  494. }
  495. return true;
  496. }
  497. public virtual InventoryItemBase GetItem(UUID principalID, UUID itemID)
  498. {
  499. XInventoryItem[] items = m_Database.GetItems(
  500. new string[] { "inventoryID" },
  501. new string[] { itemID.ToString() });
  502. if (items.Length == 0)
  503. return null;
  504. return ConvertToOpenSim(items[0]);
  505. }
  506. public virtual InventoryItemBase[] GetMultipleItems(UUID userID, UUID[] ids)
  507. {
  508. InventoryItemBase[] items = new InventoryItemBase[ids.Length];
  509. int i = 0;
  510. foreach (UUID id in ids)
  511. items[i++] = GetItem(userID, id);
  512. return items;
  513. }
  514. public virtual InventoryFolderBase GetFolder(UUID principalID, UUID folderID)
  515. {
  516. XInventoryFolder[] folders = m_Database.GetFolders(
  517. new string[] { "folderID"},
  518. new string[] { folderID.ToString() });
  519. if (folders.Length == 0)
  520. return null;
  521. return ConvertToOpenSim(folders[0]);
  522. }
  523. public virtual List<InventoryItemBase> GetActiveGestures(UUID principalID)
  524. {
  525. XInventoryItem[] items = m_Database.GetActiveGestures(principalID);
  526. if (items.Length == 0)
  527. return new List<InventoryItemBase>();
  528. List<InventoryItemBase> ret = new();
  529. foreach (XInventoryItem x in items)
  530. ret.Add(ConvertToOpenSim(x));
  531. return ret;
  532. }
  533. public virtual int GetAssetPermissions(UUID principalID, UUID assetID)
  534. {
  535. return m_Database.GetAssetPermissions(principalID, assetID);
  536. }
  537. // Unused.
  538. //
  539. public bool HasInventoryForUser(UUID userID)
  540. {
  541. return false;
  542. }
  543. // CM Helpers
  544. //
  545. protected static InventoryFolderBase ConvertToOpenSim(XInventoryFolder folder)
  546. {
  547. return new InventoryFolderBase
  548. {
  549. ParentID = folder.parentFolderID,
  550. Type = (short)folder.type,
  551. Version = (ushort)folder.version,
  552. Name = folder.folderName,
  553. Owner = folder.agentID,
  554. ID = folder.folderID
  555. };
  556. }
  557. protected static XInventoryFolder ConvertFromOpenSim(InventoryFolderBase folder)
  558. {
  559. return new XInventoryFolder
  560. {
  561. parentFolderID = folder.ParentID,
  562. type = (int)folder.Type,
  563. version = (int)folder.Version,
  564. folderName = folder.Name,
  565. agentID = folder.Owner,
  566. folderID = folder.ID
  567. };
  568. }
  569. protected static InventoryItemBase ConvertToOpenSim(XInventoryItem item)
  570. {
  571. return new InventoryItemBase
  572. {
  573. AssetID = item.assetID,
  574. AssetType = item.assetType,
  575. Name = item.inventoryName,
  576. Owner = item.avatarID,
  577. ID = item.inventoryID,
  578. InvType = item.invType,
  579. Folder = item.parentFolderID,
  580. CreatorIdentification = item.creatorID,
  581. Description = item.inventoryDescription,
  582. NextPermissions = (uint)item.inventoryNextPermissions,
  583. CurrentPermissions = (uint)item.inventoryCurrentPermissions,
  584. BasePermissions = (uint)item.inventoryBasePermissions,
  585. EveryOnePermissions = (uint)item.inventoryEveryOnePermissions,
  586. GroupPermissions = (uint)item.inventoryGroupPermissions,
  587. GroupID = item.groupID,
  588. GroupOwned = item.groupOwned != 0,
  589. SalePrice = item.salePrice,
  590. SaleType = (byte)item.saleType,
  591. Flags = (uint)item.flags,
  592. CreationDate = item.creationDate
  593. };
  594. }
  595. protected static XInventoryItem ConvertFromOpenSim(InventoryItemBase item)
  596. {
  597. return new XInventoryItem
  598. {
  599. assetID = item.AssetID,
  600. assetType = item.AssetType,
  601. inventoryName = item.Name,
  602. avatarID = item.Owner,
  603. inventoryID = item.ID,
  604. invType = item.InvType,
  605. parentFolderID = item.Folder,
  606. creatorID = item.CreatorIdentification,
  607. inventoryDescription = item.Description,
  608. inventoryNextPermissions = (int)item.NextPermissions,
  609. inventoryCurrentPermissions = (int)item.CurrentPermissions,
  610. inventoryBasePermissions = (int)item.BasePermissions,
  611. inventoryEveryOnePermissions = (int)item.EveryOnePermissions,
  612. inventoryGroupPermissions = (int)item.GroupPermissions,
  613. groupID = item.GroupID,
  614. groupOwned = item.GroupOwned ? 1 : 0,
  615. salePrice = item.SalePrice,
  616. saleType = (int)item.SaleType,
  617. flags = (int)item.Flags,
  618. creationDate = item.CreationDate
  619. };
  620. }
  621. private bool ParentIsTrash(UUID folderID)
  622. {
  623. XInventoryFolder[] folder = m_Database.GetFolders(new string[] {"folderID"}, new string[] {folderID.ToString()});
  624. if (folder.Length < 1)
  625. return false;
  626. if (folder[0].type == (int)FolderType.Trash)
  627. return true;
  628. UUID parentFolder = folder[0].parentFolderID;
  629. while (!parentFolder.IsZero())
  630. {
  631. XInventoryFolder[] parent = m_Database.GetFolders(new string[] {"folderID"}, new string[] {parentFolder.ToString()});
  632. if (parent.Length < 1)
  633. return false;
  634. if (parent[0].type == (int)FolderType.Trash)
  635. return true;
  636. if (parent[0].type == (int)FolderType.Root)
  637. return false;
  638. parentFolder = parent[0].parentFolderID;
  639. }
  640. return false;
  641. }
  642. private bool ParentIsTrashOrLost(UUID folderID)
  643. {
  644. XInventoryFolder[] folder = m_Database.GetFolders(new string[] { "folderID" }, new string[] { folderID.ToString() });
  645. if (folder.Length < 1)
  646. return false;
  647. if (folder[0].type == (int)FolderType.Trash || folder[0].type == (int)FolderType.LostAndFound)
  648. return true;
  649. UUID parentFolder = folder[0].parentFolderID;
  650. while (parentFolder.IsNotZero())
  651. {
  652. XInventoryFolder[] parent = m_Database.GetFolders(new string[] { "folderID" }, new string[] { parentFolder.ToString() });
  653. if (parent.Length < 1)
  654. return false;
  655. if (parent[0].type == (int)FolderType.Trash || folder[0].type == (int)FolderType.LostAndFound)
  656. return true;
  657. if (parent[0].type == (int)FolderType.Root)
  658. return false;
  659. parentFolder = parent[0].parentFolderID;
  660. }
  661. return false;
  662. }
  663. }
  664. }