SceneObjectGroup.Inventory.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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.IO;
  29. using System.Reflection;
  30. using OpenMetaverse;
  31. using log4net;
  32. using OpenSim.Framework;
  33. using OpenSim.Region.Framework.Interfaces;
  34. using System.Collections.Generic;
  35. using System.Xml;
  36. using PermissionMask = OpenSim.Framework.PermissionMask;
  37. namespace OpenSim.Region.Framework.Scenes
  38. {
  39. public partial class SceneObjectGroup : EntityBase
  40. {
  41. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. /// <summary>
  43. /// Force all task inventories of prims in the scene object to persist
  44. /// </summary>
  45. public void ForceInventoryPersistence()
  46. {
  47. SceneObjectPart[] parts = m_parts.GetArray();
  48. for (int i = 0; i < parts.Length; i++)
  49. parts[i].Inventory.ForceInventoryPersistence();
  50. }
  51. /// <summary>
  52. /// Start the scripts contained in all the prims in this group.
  53. /// </summary>
  54. /// <param name="startParam"></param>
  55. /// <param name="postOnRez"></param>
  56. /// <param name="engine"></param>
  57. /// <param name="stateSource"></param>
  58. /// <returns>
  59. /// Number of scripts that were valid for starting. This does not guarantee that all these scripts
  60. /// were actually started, but just that the start could be attempt (e.g. the asset data for the script could be found)
  61. /// </returns>
  62. public int CreateScriptInstances(int startParam, bool postOnRez, string engine, int stateSource)
  63. {
  64. int scriptsStarted = 0;
  65. if (m_scene == null)
  66. {
  67. m_log.DebugFormat("[PRIM INVENTORY]: m_scene is null. Unable to create script instances");
  68. return 0;
  69. }
  70. // Don't start scripts if they're turned off in the region!
  71. if (!m_scene.RegionInfo.RegionSettings.DisableScripts)
  72. {
  73. SceneObjectPart[] parts = m_parts.GetArray();
  74. for (int i = 0; i < parts.Length; i++)
  75. scriptsStarted
  76. += parts[i].Inventory.CreateScriptInstances(startParam, postOnRez, engine, stateSource);
  77. }
  78. return scriptsStarted;
  79. }
  80. /// <summary>
  81. /// Stop and remove the scripts contained in all the prims in this group
  82. /// </summary>
  83. public void RemoveScriptInstances(bool sceneObjectBeingDeleted)
  84. {
  85. SceneObjectPart[] parts = m_parts.GetArray();
  86. for (int i = 0; i < parts.Length; i++)
  87. parts[i].Inventory.RemoveScriptInstances(sceneObjectBeingDeleted);
  88. }
  89. /// <summary>
  90. /// Stop the scripts contained in all the prims in this group
  91. /// </summary>
  92. public void StopScriptInstances()
  93. {
  94. Array.ForEach<SceneObjectPart>(m_parts.GetArray(), p => p.Inventory.StopScriptInstances());
  95. }
  96. /// <summary>
  97. /// Add an inventory item from a user's inventory to a prim in this scene object.
  98. /// </summary>
  99. /// <param name="agentID">The agent adding the item.</param>
  100. /// <param name="localID">The local ID of the part receiving the add.</param>
  101. /// <param name="item">The user inventory item being added.</param>
  102. /// <param name="copyItemID">The item UUID that should be used by the new item.</param>
  103. /// <returns></returns>
  104. public bool AddInventoryItem(UUID agentID, uint localID, InventoryItemBase item, UUID copyItemID, bool withModRights = true)
  105. {
  106. // m_log.DebugFormat(
  107. // "[PRIM INVENTORY]: Adding inventory item {0} from {1} to part with local ID {2}",
  108. // item.Name, remoteClient.Name, localID);
  109. UUID newItemId = (copyItemID != UUID.Zero) ? copyItemID : item.ID;
  110. SceneObjectPart part = GetPart(localID);
  111. if (part == null)
  112. {
  113. m_log.ErrorFormat(
  114. "[PRIM INVENTORY]: " +
  115. "Couldn't find prim local ID {0} in group {1}, {2} to add inventory item ID {3}",
  116. localID, Name, UUID, newItemId);
  117. return false;
  118. }
  119. TaskInventoryItem taskItem = new TaskInventoryItem();
  120. taskItem.ItemID = newItemId;
  121. taskItem.AssetID = item.AssetID;
  122. taskItem.Name = item.Name;
  123. taskItem.Description = item.Description;
  124. taskItem.OwnerID = part.OwnerID; // Transfer ownership
  125. taskItem.CreatorID = item.CreatorIdAsUuid;
  126. taskItem.Type = item.AssetType;
  127. taskItem.InvType = item.InvType;
  128. if (agentID != part.OwnerID && m_scene.Permissions.PropagatePermissions())
  129. {
  130. taskItem.BasePermissions = item.BasePermissions &
  131. item.NextPermissions;
  132. taskItem.CurrentPermissions = item.CurrentPermissions &
  133. item.NextPermissions;
  134. taskItem.EveryonePermissions = item.EveryOnePermissions &
  135. item.NextPermissions;
  136. taskItem.GroupPermissions = item.GroupPermissions &
  137. item.NextPermissions;
  138. taskItem.NextPermissions = item.NextPermissions;
  139. // We're adding this to a prim we don't own. Force
  140. // owner change
  141. taskItem.Flags |= (uint)InventoryItemFlags.ObjectSlamPerm;
  142. }
  143. else
  144. {
  145. taskItem.BasePermissions = item.BasePermissions;
  146. taskItem.CurrentPermissions = item.CurrentPermissions;
  147. taskItem.EveryonePermissions = item.EveryOnePermissions;
  148. taskItem.GroupPermissions = item.GroupPermissions;
  149. taskItem.NextPermissions = item.NextPermissions;
  150. }
  151. taskItem.Flags = item.Flags;
  152. // m_log.DebugFormat(
  153. // "[PRIM INVENTORY]: Flags are 0x{0:X} for item {1} added to part {2} by {3}",
  154. // taskItem.Flags, taskItem.Name, localID, remoteClient.Name);
  155. // TODO: These are pending addition of those fields to TaskInventoryItem
  156. // taskItem.SalePrice = item.SalePrice;
  157. // taskItem.SaleType = item.SaleType;
  158. taskItem.CreationDate = (uint)item.CreationDate;
  159. bool addFromAllowedDrop;
  160. if(withModRights)
  161. addFromAllowedDrop = false;
  162. else
  163. addFromAllowedDrop = (part.ParentGroup.RootPart.GetEffectiveObjectFlags() & (uint)PrimFlags.AllowInventoryDrop) != 0;
  164. part.Inventory.AddInventoryItem(taskItem, addFromAllowedDrop);
  165. part.ParentGroup.InvalidateEffectivePerms();
  166. return true;
  167. }
  168. /// <summary>
  169. /// Returns an existing inventory item. Returns the original, so any changes will be live.
  170. /// </summary>
  171. /// <param name="primID"></param>
  172. /// <param name="itemID"></param>
  173. /// <returns>null if the item does not exist</returns>
  174. public TaskInventoryItem GetInventoryItem(uint primID, UUID itemID)
  175. {
  176. SceneObjectPart part = GetPart(primID);
  177. if (part != null)
  178. {
  179. return part.Inventory.GetInventoryItem(itemID);
  180. }
  181. else
  182. {
  183. m_log.ErrorFormat(
  184. "[PRIM INVENTORY]: " +
  185. "Couldn't find prim local ID {0} in prim {1}, {2} to get inventory item ID {3}",
  186. primID, part.Name, part.UUID, itemID);
  187. }
  188. return null;
  189. }
  190. /// <summary>
  191. /// Update an existing inventory item.
  192. /// </summary>
  193. /// <param name="item">The updated item. An item with the same id must already exist
  194. /// in this prim's inventory</param>
  195. /// <returns>false if the item did not exist, true if the update occurred succesfully</returns>
  196. public bool UpdateInventoryItem(TaskInventoryItem item)
  197. {
  198. SceneObjectPart part = GetPart(item.ParentPartID);
  199. if (part != null)
  200. {
  201. part.Inventory.UpdateInventoryItem(item);
  202. return true;
  203. }
  204. else
  205. {
  206. m_log.ErrorFormat(
  207. "[PRIM INVENTORY]: " +
  208. "Couldn't find prim ID {0} to update item {1}, {2}",
  209. item.ParentPartID, item.Name, item.ItemID);
  210. }
  211. return false;
  212. }
  213. public int RemoveInventoryItem(uint localID, UUID itemID)
  214. {
  215. SceneObjectPart part = GetPart(localID);
  216. if (part != null)
  217. {
  218. int type = part.Inventory.RemoveInventoryItem(itemID);
  219. return type;
  220. }
  221. return -1;
  222. }
  223. // new test code, to place in better place later
  224. private object m_PermissionsLock = new object();
  225. private bool m_EffectivePermsInvalid = true;
  226. private bool m_DeepEffectivePermsInvalid = true;
  227. // should called when parts chanced by their contents did not, so we know their cacche is valid
  228. // in case of doubt call InvalidateDeepEffectivePerms(), it only costs a bit more cpu time
  229. public void InvalidateEffectivePerms()
  230. {
  231. lock(m_PermissionsLock)
  232. m_EffectivePermsInvalid = true;
  233. }
  234. // should called when parts chanced and their contents where accounted for
  235. public void InvalidateDeepEffectivePerms()
  236. {
  237. lock(m_PermissionsLock)
  238. {
  239. m_DeepEffectivePermsInvalid = true;
  240. m_EffectivePermsInvalid = true;
  241. }
  242. }
  243. private uint m_EffectiveEveryOnePerms;
  244. public uint EffectiveEveryOnePerms
  245. {
  246. get
  247. {
  248. lock(m_PermissionsLock)
  249. {
  250. if(m_EffectivePermsInvalid)
  251. AggregatePerms();
  252. return m_EffectiveEveryOnePerms;
  253. }
  254. }
  255. }
  256. private uint m_EffectiveGroupPerms;
  257. public uint EffectiveGroupPerms
  258. {
  259. get
  260. {
  261. lock(m_PermissionsLock)
  262. {
  263. if(m_EffectivePermsInvalid)
  264. AggregatePerms();
  265. return m_EffectiveGroupPerms;
  266. }
  267. }
  268. }
  269. private uint m_EffectiveGroupOrEveryOnePerms;
  270. public uint EffectiveGroupOrEveryOnePerms
  271. {
  272. get
  273. {
  274. lock(m_PermissionsLock)
  275. {
  276. if(m_EffectivePermsInvalid)
  277. AggregatePerms();
  278. return m_EffectiveGroupOrEveryOnePerms;
  279. }
  280. }
  281. }
  282. private uint m_EffectiveOwnerPerms;
  283. public uint EffectiveOwnerPerms
  284. {
  285. get
  286. {
  287. lock(m_PermissionsLock)
  288. {
  289. if(m_EffectivePermsInvalid)
  290. AggregatePerms();
  291. return m_EffectiveOwnerPerms;
  292. }
  293. }
  294. }
  295. public void AggregatePerms()
  296. {
  297. lock(m_PermissionsLock)
  298. {
  299. // aux
  300. const uint allmask = (uint)PermissionMask.AllEffective;
  301. const uint movemodmask = (uint)(PermissionMask.Move | PermissionMask.Modify);
  302. const uint copytransfermast = (uint)(PermissionMask.Copy | PermissionMask.Transfer);
  303. uint basePerms = (RootPart.BaseMask & allmask) | (uint)PermissionMask.Move;
  304. bool noBaseTransfer = (basePerms & (uint)PermissionMask.Transfer) == 0;
  305. uint rootOwnerPerms = RootPart.OwnerMask;
  306. uint owner = rootOwnerPerms;
  307. uint rootGroupPerms = RootPart.GroupMask;
  308. uint group = rootGroupPerms;
  309. uint rootEveryonePerms = RootPart.EveryoneMask;
  310. uint everyone = rootEveryonePerms;
  311. bool needUpdate = false;
  312. // date is time of writing april 30th 2017
  313. bool newobj = (RootPart.CreationDate == 0 || RootPart.CreationDate > 1493574994);
  314. SceneObjectPart[] parts = m_parts.GetArray();
  315. for (int i = 0; i < parts.Length; i++)
  316. {
  317. SceneObjectPart part = parts[i];
  318. if(m_DeepEffectivePermsInvalid)
  319. part.AggregatedInnerPermsForGroup();
  320. owner &= part.AggregatedInnerOwnerPerms;
  321. group &= part.AggregatedInnerGroupPerms;
  322. if(newobj)
  323. group &= part.AggregatedInnerGroupPerms;
  324. if(newobj)
  325. everyone &= part.AggregatedInnerEveryonePerms;
  326. }
  327. // recover modify and move
  328. rootOwnerPerms &= movemodmask;
  329. owner |= rootOwnerPerms;
  330. if((owner & copytransfermast) == 0)
  331. owner |= (uint)PermissionMask.Transfer;
  332. owner &= basePerms;
  333. if(owner != m_EffectiveOwnerPerms)
  334. {
  335. needUpdate = true;
  336. m_EffectiveOwnerPerms = owner;
  337. }
  338. uint ownertransfermask = owner & (uint)PermissionMask.Transfer;
  339. // recover modify and move
  340. rootGroupPerms &= movemodmask;
  341. group |= rootGroupPerms;
  342. if(noBaseTransfer)
  343. group &=~(uint)PermissionMask.Copy;
  344. else
  345. group |= ownertransfermask;
  346. uint groupOrEveryone = group;
  347. uint tmpPerms = group & owner;
  348. if(tmpPerms != m_EffectiveGroupPerms)
  349. {
  350. needUpdate = true;
  351. m_EffectiveGroupPerms = tmpPerms;
  352. }
  353. // recover move
  354. rootEveryonePerms &= (uint)PermissionMask.Move;
  355. everyone |= rootEveryonePerms;
  356. everyone &= ~(uint)PermissionMask.Modify;
  357. if(noBaseTransfer)
  358. everyone &=~(uint)PermissionMask.Copy;
  359. else
  360. everyone |= ownertransfermask;
  361. groupOrEveryone |= everyone;
  362. tmpPerms = everyone & owner;
  363. if(tmpPerms != m_EffectiveEveryOnePerms)
  364. {
  365. needUpdate = true;
  366. m_EffectiveEveryOnePerms = tmpPerms;
  367. }
  368. tmpPerms = groupOrEveryone & owner;
  369. if(tmpPerms != m_EffectiveGroupOrEveryOnePerms)
  370. {
  371. needUpdate = true;
  372. m_EffectiveGroupOrEveryOnePerms = tmpPerms;
  373. }
  374. m_DeepEffectivePermsInvalid = false;
  375. m_EffectivePermsInvalid = false;
  376. if(needUpdate)
  377. RootPart.ScheduleFullUpdate();
  378. }
  379. }
  380. public uint CurrentAndFoldedNextPermissions()
  381. {
  382. uint perms=(uint)(PermissionMask.Modify |
  383. PermissionMask.Copy |
  384. PermissionMask.Move |
  385. PermissionMask.Transfer |
  386. PermissionMask.FoldedMask);
  387. uint ownerMask = RootPart.OwnerMask;
  388. SceneObjectPart[] parts = m_parts.GetArray();
  389. for (int i = 0; i < parts.Length; i++)
  390. {
  391. SceneObjectPart part = parts[i];
  392. ownerMask &= part.BaseMask;
  393. perms &= part.Inventory.MaskEffectivePermissions();
  394. }
  395. if ((ownerMask & (uint)PermissionMask.Modify) == 0)
  396. perms &= ~(uint)PermissionMask.Modify;
  397. if ((ownerMask & (uint)PermissionMask.Copy) == 0)
  398. perms &= ~(uint)PermissionMask.Copy;
  399. if ((ownerMask & (uint)PermissionMask.Transfer) == 0)
  400. perms &= ~(uint)PermissionMask.Transfer;
  401. if ((ownerMask & (uint)PermissionMask.Export) == 0)
  402. perms &= ~(uint)PermissionMask.Export;
  403. return perms;
  404. }
  405. public void ApplyNextOwnerPermissions()
  406. {
  407. // m_log.DebugFormat("[PRIM INVENTORY]: Applying next owner permissions to {0} {1}", Name, UUID);
  408. SceneObjectPart[] parts = m_parts.GetArray();
  409. for (int i = 0; i < parts.Length; i++)
  410. parts[i].ApplyNextOwnerPermissions();
  411. }
  412. public string GetStateSnapshot()
  413. {
  414. Dictionary<UUID, string> states = new Dictionary<UUID, string>();
  415. SceneObjectPart[] parts = m_parts.GetArray();
  416. for (int i = 0; i < parts.Length; i++)
  417. {
  418. SceneObjectPart part = parts[i];
  419. foreach (KeyValuePair<UUID, string> s in part.Inventory.GetScriptStates())
  420. states[s.Key] = s.Value;
  421. }
  422. if (states.Count < 1)
  423. return String.Empty;
  424. XmlDocument xmldoc = new XmlDocument();
  425. XmlNode xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration,
  426. String.Empty, String.Empty);
  427. xmldoc.AppendChild(xmlnode);
  428. XmlElement rootElement = xmldoc.CreateElement("", "ScriptData",
  429. String.Empty);
  430. xmldoc.AppendChild(rootElement);
  431. XmlElement wrapper = xmldoc.CreateElement("", "ScriptStates",
  432. String.Empty);
  433. rootElement.AppendChild(wrapper);
  434. foreach (KeyValuePair<UUID, string> state in states)
  435. {
  436. XmlDocument sdoc = new XmlDocument();
  437. sdoc.LoadXml(state.Value);
  438. XmlNodeList rootL = sdoc.GetElementsByTagName("State");
  439. XmlNode rootNode = rootL[0];
  440. XmlNode newNode = xmldoc.ImportNode(rootNode, true);
  441. wrapper.AppendChild(newNode);
  442. }
  443. return xmldoc.InnerXml;
  444. }
  445. public void SetState(string objXMLData, IScene ins)
  446. {
  447. if (!(ins is Scene))
  448. return;
  449. Scene s = (Scene)ins;
  450. if (objXMLData == String.Empty)
  451. return;
  452. IScriptModule scriptModule = null;
  453. foreach (IScriptModule sm in s.RequestModuleInterfaces<IScriptModule>())
  454. {
  455. if (sm.ScriptEngineName == s.DefaultScriptEngine)
  456. scriptModule = sm;
  457. else if (scriptModule == null)
  458. scriptModule = sm;
  459. }
  460. if (scriptModule == null)
  461. return;
  462. XmlDocument doc = new XmlDocument();
  463. try
  464. {
  465. doc.LoadXml(objXMLData);
  466. }
  467. catch (Exception) // (System.Xml.XmlException)
  468. {
  469. // We will get here if the XML is invalid or in unit
  470. // tests. Really should determine which it is and either
  471. // fail silently or log it
  472. // Fail silently, for now.
  473. // TODO: Fix this
  474. //
  475. return;
  476. }
  477. XmlNodeList rootL = doc.GetElementsByTagName("ScriptData");
  478. if (rootL.Count != 1)
  479. return;
  480. XmlElement rootE = (XmlElement)rootL[0];
  481. XmlNodeList dataL = rootE.GetElementsByTagName("ScriptStates");
  482. if (dataL.Count != 1)
  483. return;
  484. XmlElement dataE = (XmlElement)dataL[0];
  485. foreach (XmlNode n in dataE.ChildNodes)
  486. {
  487. XmlElement stateE = (XmlElement)n;
  488. UUID itemID = new UUID(stateE.GetAttribute("UUID"));
  489. scriptModule.SetXMLState(itemID, n.OuterXml);
  490. }
  491. }
  492. public void ResumeScripts()
  493. {
  494. if (m_scene.RegionInfo.RegionSettings.DisableScripts)
  495. return;
  496. SceneObjectPart[] parts = m_parts.GetArray();
  497. for (int i = 0; i < parts.Length; i++)
  498. parts[i].Inventory.ResumeScripts();
  499. }
  500. /// <summary>
  501. /// Returns true if any part in the scene object contains scripts, false otherwise.
  502. /// </summary>
  503. /// <returns></returns>
  504. public bool ContainsScripts()
  505. {
  506. foreach (SceneObjectPart part in Parts)
  507. if (part.Inventory.ContainsScripts())
  508. return true;
  509. return false;
  510. }
  511. }
  512. }