SceneObjectGroup.Inventory.cs 23 KB

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