Scene.Inventory.cs 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161
  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 System.Collections;
  30. using System.Reflection;
  31. using System.Text;
  32. using System.Timers;
  33. using OpenMetaverse;
  34. using OpenMetaverse.Packets;
  35. using log4net;
  36. using OpenSim.Framework;
  37. using OpenSim.Region.Framework;
  38. using OpenSim.Region.Framework.Interfaces;
  39. using OpenSim.Region.Framework.Scenes.Serialization;
  40. namespace OpenSim.Region.Framework.Scenes
  41. {
  42. public partial class Scene
  43. {
  44. private static readonly ILog m_log
  45. = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. /// <summary>
  47. /// Allows asynchronous derezzing of objects from the scene into a client's inventory.
  48. /// </summary>
  49. protected AsyncSceneObjectGroupDeleter m_asyncSceneObjectDeleter;
  50. /// <summary>
  51. /// Start all the scripts in the scene which should be started.
  52. /// </summary>
  53. public void CreateScriptInstances()
  54. {
  55. m_log.Info("[PRIM INVENTORY]: Starting scripts in scene");
  56. foreach (EntityBase group in Entities)
  57. {
  58. if (group is SceneObjectGroup)
  59. {
  60. ((SceneObjectGroup) group).CreateScriptInstances(0, false, DefaultScriptEngine, 0);
  61. ((SceneObjectGroup) group).ResumeScripts();
  62. }
  63. }
  64. }
  65. public void AddUploadedInventoryItem(UUID agentID, InventoryItemBase item)
  66. {
  67. IMoneyModule money=RequestModuleInterface<IMoneyModule>();
  68. if (money != null)
  69. {
  70. money.ApplyUploadCharge(agentID, money.UploadCharge, "Asset upload");
  71. }
  72. AddInventoryItem(agentID, item);
  73. }
  74. public bool AddInventoryItemReturned(UUID AgentId, InventoryItemBase item)
  75. {
  76. if (InventoryService.AddItem(item))
  77. return true;
  78. else
  79. {
  80. m_log.WarnFormat(
  81. "[AGENT INVENTORY]: Unable to add item {1} to agent {2} inventory", item.Name, AgentId);
  82. return false;
  83. }
  84. }
  85. public void AddInventoryItem(UUID AgentID, InventoryItemBase item)
  86. {
  87. if (InventoryService.AddItem(item))
  88. {
  89. int userlevel = 0;
  90. if (Permissions.IsGod(AgentID))
  91. {
  92. userlevel = 1;
  93. }
  94. EventManager.TriggerOnNewInventoryItemUploadComplete(AgentID, item.AssetID, item.Name, userlevel);
  95. }
  96. else
  97. {
  98. m_log.WarnFormat(
  99. "[AGENT INVENTORY]: Agent {1} could not add item {2} {3}",
  100. AgentID, item.Name, item.ID);
  101. return;
  102. }
  103. }
  104. /// <summary>
  105. /// Add an inventory item to an avatar's inventory.
  106. /// </summary>
  107. /// <param name="remoteClient">The remote client controlling the avatar</param>
  108. /// <param name="item">The item. This structure contains all the item metadata, including the folder
  109. /// in which the item is to be placed.</param>
  110. public void AddInventoryItem(IClientAPI remoteClient, InventoryItemBase item)
  111. {
  112. AddInventoryItem(remoteClient.AgentId, item);
  113. remoteClient.SendInventoryItemCreateUpdate(item, 0);
  114. }
  115. /// <summary>
  116. /// <see>CapsUpdatedInventoryItemAsset(IClientAPI, UUID, byte[])</see>
  117. /// </summary>
  118. public UUID CapsUpdateInventoryItemAsset(UUID avatarId, UUID itemID, byte[] data)
  119. {
  120. ScenePresence avatar;
  121. if (TryGetScenePresence(avatarId, out avatar))
  122. {
  123. IInventoryAccessModule invAccess = RequestModuleInterface<IInventoryAccessModule>();
  124. if (invAccess != null)
  125. return invAccess.CapsUpdateInventoryItemAsset(avatar.ControllingClient, itemID, data);
  126. }
  127. else
  128. {
  129. m_log.ErrorFormat(
  130. "[AGENT INVENTORY]: " +
  131. "Avatar {0} cannot be found to update its inventory item asset",
  132. avatarId);
  133. }
  134. return UUID.Zero;
  135. }
  136. /// <summary>
  137. /// Capability originating call to update the asset of a script in a prim's (task's) inventory
  138. /// </summary>
  139. /// <param name="remoteClient"></param>
  140. /// <param name="itemID"></param>
  141. /// <param name="primID">The prim which contains the item to update</param>
  142. /// <param name="isScriptRunning">Indicates whether the script to update is currently running</param>
  143. /// <param name="data"></param>
  144. public ArrayList CapsUpdateTaskInventoryScriptAsset(IClientAPI remoteClient, UUID itemId,
  145. UUID primId, bool isScriptRunning, byte[] data)
  146. {
  147. if (!Permissions.CanEditScript(itemId, primId, remoteClient.AgentId))
  148. {
  149. remoteClient.SendAgentAlertMessage("Insufficient permissions to edit script", false);
  150. return new ArrayList();
  151. }
  152. // Retrieve group
  153. SceneObjectPart part = GetSceneObjectPart(primId);
  154. SceneObjectGroup group = part.ParentGroup;
  155. if (null == group)
  156. {
  157. m_log.ErrorFormat(
  158. "[PRIM INVENTORY]: " +
  159. "Prim inventory update requested for item ID {0} in prim ID {1} but this prim does not exist",
  160. itemId, primId);
  161. return new ArrayList();
  162. }
  163. // Retrieve item
  164. TaskInventoryItem item = group.GetInventoryItem(part.LocalId, itemId);
  165. if (null == item)
  166. {
  167. m_log.ErrorFormat(
  168. "[PRIM INVENTORY]: Tried to retrieve item ID {0} from prim {1}, {2} for caps script update "
  169. + " but the item does not exist in this inventory",
  170. itemId, part.Name, part.UUID);
  171. return new ArrayList();
  172. }
  173. AssetBase asset = CreateAsset(item.Name, item.Description, (sbyte)AssetType.LSLText, data, remoteClient.AgentId);
  174. AssetService.Store(asset);
  175. if (isScriptRunning)
  176. {
  177. part.Inventory.RemoveScriptInstance(item.ItemID, false);
  178. }
  179. // Update item with new asset
  180. item.AssetID = asset.FullID;
  181. if (group.UpdateInventoryItem(item))
  182. remoteClient.SendAgentAlertMessage("Script saved", false);
  183. part.GetProperties(remoteClient);
  184. // Trigger rerunning of script (use TriggerRezScript event, see RezScript)
  185. ArrayList errors = new ArrayList();
  186. if (isScriptRunning)
  187. {
  188. // Needs to determine which engine was running it and use that
  189. //
  190. part.Inventory.CreateScriptInstance(item.ItemID, 0, false, DefaultScriptEngine, 0);
  191. errors = part.Inventory.GetScriptErrors(item.ItemID);
  192. }
  193. else
  194. {
  195. remoteClient.SendAgentAlertMessage("Script saved", false);
  196. }
  197. part.ParentGroup.ResumeScripts();
  198. return errors;
  199. }
  200. /// <summary>
  201. /// <see>CapsUpdateTaskInventoryScriptAsset(IClientAPI, UUID, UUID, bool, byte[])</see>
  202. /// </summary>
  203. public ArrayList CapsUpdateTaskInventoryScriptAsset(UUID avatarId, UUID itemId,
  204. UUID primId, bool isScriptRunning, byte[] data)
  205. {
  206. ScenePresence avatar;
  207. if (TryGetScenePresence(avatarId, out avatar))
  208. {
  209. return CapsUpdateTaskInventoryScriptAsset(
  210. avatar.ControllingClient, itemId, primId, isScriptRunning, data);
  211. }
  212. else
  213. {
  214. m_log.ErrorFormat(
  215. "[PRIM INVENTORY]: " +
  216. "Avatar {0} cannot be found to update its prim item asset",
  217. avatarId);
  218. return new ArrayList();
  219. }
  220. }
  221. /// <summary>
  222. /// Update an item which is either already in the client's inventory or is within
  223. /// a transaction
  224. /// </summary>
  225. /// <param name="remoteClient"></param>
  226. /// <param name="transactionID">The transaction ID. If this is UUID.Zero we will
  227. /// assume that we are not in a transaction</param>
  228. /// <param name="itemID">The ID of the updated item</param>
  229. /// <param name="name">The name of the updated item</param>
  230. /// <param name="description">The description of the updated item</param>
  231. /// <param name="nextOwnerMask">The permissions of the updated item</param>
  232. /* public void UpdateInventoryItemAsset(IClientAPI remoteClient, UUID transactionID,
  233. UUID itemID, string name, string description,
  234. uint nextOwnerMask)*/
  235. public void UpdateInventoryItemAsset(IClientAPI remoteClient, UUID transactionID,
  236. UUID itemID, InventoryItemBase itemUpd)
  237. {
  238. // This one will let people set next perms on items in agent
  239. // inventory. Rut-Roh. Whatever. Make this secure. Yeah.
  240. //
  241. // Passing something to another avatar or a an object will already
  242. InventoryItemBase item = new InventoryItemBase(itemID, remoteClient.AgentId);
  243. item = InventoryService.GetItem(item);
  244. if (item != null)
  245. {
  246. if (UUID.Zero == transactionID)
  247. {
  248. item.Name = itemUpd.Name;
  249. item.Description = itemUpd.Description;
  250. item.NextPermissions = itemUpd.NextPermissions & item.BasePermissions;
  251. item.EveryOnePermissions = itemUpd.EveryOnePermissions & item.BasePermissions;
  252. item.GroupPermissions = itemUpd.GroupPermissions & item.BasePermissions;
  253. item.GroupID = itemUpd.GroupID;
  254. item.GroupOwned = itemUpd.GroupOwned;
  255. item.CreationDate = itemUpd.CreationDate;
  256. // The client sends zero if its newly created?
  257. if (itemUpd.CreationDate == 0)
  258. item.CreationDate = Util.UnixTimeSinceEpoch();
  259. else
  260. item.CreationDate = itemUpd.CreationDate;
  261. // TODO: Check if folder changed and move item
  262. //item.NextPermissions = itemUpd.Folder;
  263. item.InvType = itemUpd.InvType;
  264. item.SalePrice = itemUpd.SalePrice;
  265. item.SaleType = itemUpd.SaleType;
  266. item.Flags = itemUpd.Flags;
  267. InventoryService.UpdateItem(item);
  268. }
  269. else
  270. {
  271. IAgentAssetTransactions agentTransactions = this.RequestModuleInterface<IAgentAssetTransactions>();
  272. if (agentTransactions != null)
  273. {
  274. agentTransactions.HandleItemUpdateFromTransaction(
  275. remoteClient, transactionID, item);
  276. }
  277. }
  278. }
  279. else
  280. {
  281. m_log.Error(
  282. "[AGENTINVENTORY]: Item ID " + itemID + " not found for an inventory item update.");
  283. }
  284. }
  285. /// <summary>
  286. /// Give an inventory item from one user to another
  287. /// </summary>
  288. /// <param name="recipientClient"></param>
  289. /// <param name="senderId">ID of the sender of the item</param>
  290. /// <param name="itemId"></param>
  291. public virtual void GiveInventoryItem(IClientAPI recipientClient, UUID senderId, UUID itemId)
  292. {
  293. InventoryItemBase itemCopy = GiveInventoryItem(recipientClient.AgentId, senderId, itemId);
  294. if (itemCopy != null)
  295. recipientClient.SendBulkUpdateInventory(itemCopy);
  296. }
  297. /// <summary>
  298. /// Give an inventory item from one user to another
  299. /// </summary>
  300. /// <param name="recipient"></param>
  301. /// <param name="senderId">ID of the sender of the item</param>
  302. /// <param name="itemId"></param>
  303. /// <returns>The inventory item copy given, null if the give was unsuccessful</returns>
  304. public virtual InventoryItemBase GiveInventoryItem(UUID recipient, UUID senderId, UUID itemId)
  305. {
  306. return GiveInventoryItem(recipient, senderId, itemId, UUID.Zero);
  307. }
  308. /// <summary>
  309. /// Give an inventory item from one user to another
  310. /// </summary>
  311. /// <param name="recipient"></param>
  312. /// <param name="senderId">ID of the sender of the item</param>
  313. /// <param name="itemId"></param>
  314. /// <param name="recipientFolderId">
  315. /// The id of the folder in which the copy item should go. If UUID.Zero then the item is placed in the most
  316. /// appropriate default folder.
  317. /// </param>
  318. /// <returns>
  319. /// The inventory item copy given, null if the give was unsuccessful
  320. /// </returns>
  321. public virtual InventoryItemBase GiveInventoryItem(
  322. UUID recipient, UUID senderId, UUID itemId, UUID recipientFolderId)
  323. {
  324. //Console.WriteLine("Scene.Inventory.cs: GiveInventoryItem");
  325. InventoryItemBase item = new InventoryItemBase(itemId, senderId);
  326. item = InventoryService.GetItem(item);
  327. if ((item != null) && (item.Owner == senderId))
  328. {
  329. if (!Permissions.BypassPermissions())
  330. {
  331. if ((item.CurrentPermissions & (uint)PermissionMask.Transfer) == 0)
  332. return null;
  333. }
  334. // Insert a copy of the item into the recipient
  335. InventoryItemBase itemCopy = new InventoryItemBase();
  336. itemCopy.Owner = recipient;
  337. itemCopy.CreatorId = item.CreatorId;
  338. itemCopy.ID = UUID.Random();
  339. itemCopy.AssetID = item.AssetID;
  340. itemCopy.Description = item.Description;
  341. itemCopy.Name = item.Name;
  342. itemCopy.AssetType = item.AssetType;
  343. itemCopy.InvType = item.InvType;
  344. itemCopy.Folder = recipientFolderId;
  345. if (Permissions.PropagatePermissions() && recipient != senderId)
  346. {
  347. // Trying to do this right this time. This is evil. If
  348. // you believe in Good, go elsewhere. Vampires and other
  349. // evil creatores only beyond this point. You have been
  350. // warned.
  351. // We're going to mask a lot of things by the next perms
  352. // Tweak the next perms to be nicer to our data
  353. //
  354. // In this mask, all the bits we do NOT want to mess
  355. // with are set. These are:
  356. //
  357. // Transfer
  358. // Copy
  359. // Modufy
  360. uint permsMask = ~ ((uint)PermissionMask.Copy |
  361. (uint)PermissionMask.Transfer |
  362. (uint)PermissionMask.Modify);
  363. // Now, reduce the next perms to the mask bits
  364. // relevant to the operation
  365. uint nextPerms = permsMask | (item.NextPermissions &
  366. ((uint)PermissionMask.Copy |
  367. (uint)PermissionMask.Transfer |
  368. (uint)PermissionMask.Modify));
  369. // nextPerms now has all bits set, except for the actual
  370. // next permission bits.
  371. // This checks for no mod, no copy, no trans.
  372. // This indicates an error or messed up item. Do it like
  373. // SL and assume trans
  374. if (nextPerms == permsMask)
  375. nextPerms |= (uint)PermissionMask.Transfer;
  376. // Inventory owner perms are the logical AND of the
  377. // folded perms and the root prim perms, however, if
  378. // the root prim is mod, the inventory perms will be
  379. // mod. This happens on "take" and is of little concern
  380. // here, save for preventing escalation
  381. // This hack ensures that items previously permalocked
  382. // get unlocked when they're passed or rezzed
  383. uint basePerms = item.BasePermissions |
  384. (uint)PermissionMask.Move;
  385. uint ownerPerms = item.CurrentPermissions;
  386. // If this is an object, root prim perms may be more
  387. // permissive than folded perms. Use folded perms as
  388. // a mask
  389. if (item.InvType == (int)InventoryType.Object)
  390. {
  391. // Create a safe mask for the current perms
  392. uint foldedPerms = (item.CurrentPermissions & 7) << 13;
  393. foldedPerms |= permsMask;
  394. bool isRootMod = (item.CurrentPermissions &
  395. (uint)PermissionMask.Modify) != 0 ?
  396. true : false;
  397. // Mask the owner perms to the folded perms
  398. ownerPerms &= foldedPerms;
  399. basePerms &= foldedPerms;
  400. // If the root was mod, let the mask reflect that
  401. // We also need to adjust the base here, because
  402. // we should be able to edit in-inventory perms
  403. // for the root prim, if it's mod.
  404. if (isRootMod)
  405. {
  406. ownerPerms |= (uint)PermissionMask.Modify;
  407. basePerms |= (uint)PermissionMask.Modify;
  408. }
  409. }
  410. // These will be applied to the root prim at next rez.
  411. // The slam bit (bit 3) and folded permission (bits 0-2)
  412. // are preserved due to the above mangling
  413. ownerPerms &= nextPerms;
  414. // Mask the base permissions. This is a conservative
  415. // approach altering only the three main perms
  416. basePerms &= nextPerms;
  417. // Assign to the actual item. Make sure the slam bit is
  418. // set, if it wasn't set before.
  419. itemCopy.BasePermissions = basePerms;
  420. itemCopy.CurrentPermissions = ownerPerms | 16; // Slam
  421. itemCopy.NextPermissions = item.NextPermissions;
  422. // This preserves "everyone can move"
  423. itemCopy.EveryOnePermissions = item.EveryOnePermissions &
  424. nextPerms;
  425. // Intentionally killing "share with group" here, as
  426. // the recipient will not have the group this is
  427. // set to
  428. itemCopy.GroupPermissions = 0;
  429. }
  430. else
  431. {
  432. itemCopy.CurrentPermissions = item.CurrentPermissions;
  433. itemCopy.NextPermissions = item.NextPermissions;
  434. itemCopy.EveryOnePermissions = item.EveryOnePermissions & item.NextPermissions;
  435. itemCopy.GroupPermissions = item.GroupPermissions & item.NextPermissions;
  436. itemCopy.BasePermissions = item.BasePermissions;
  437. }
  438. if (itemCopy.Folder == UUID.Zero)
  439. {
  440. InventoryFolderBase folder = InventoryService.GetFolderForType(recipient, (AssetType)itemCopy.AssetType);
  441. if (folder != null)
  442. {
  443. itemCopy.Folder = folder.ID;
  444. }
  445. else
  446. {
  447. InventoryFolderBase root = InventoryService.GetRootFolder(recipient);
  448. if (root != null)
  449. itemCopy.Folder = root.ID;
  450. else
  451. return null; // No destination
  452. }
  453. }
  454. itemCopy.GroupID = UUID.Zero;
  455. itemCopy.GroupOwned = false;
  456. itemCopy.Flags = item.Flags;
  457. itemCopy.SalePrice = item.SalePrice;
  458. itemCopy.SaleType = item.SaleType;
  459. if (InventoryService.AddItem(itemCopy))
  460. {
  461. IInventoryAccessModule invAccess = RequestModuleInterface<IInventoryAccessModule>();
  462. if (invAccess != null)
  463. invAccess.TransferInventoryAssets(itemCopy, senderId, recipient);
  464. }
  465. if (!Permissions.BypassPermissions())
  466. {
  467. if ((item.CurrentPermissions & (uint)PermissionMask.Copy) == 0)
  468. {
  469. List<UUID> items = new List<UUID>();
  470. items.Add(itemId);
  471. InventoryService.DeleteItems(senderId, items);
  472. }
  473. }
  474. return itemCopy;
  475. }
  476. else
  477. {
  478. m_log.WarnFormat("[AGENT INVENTORY]: Failed to find item {0} or item does not belong to giver ", itemId);
  479. return null;
  480. }
  481. }
  482. /// <summary>
  483. /// Give an entire inventory folder from one user to another. The entire contents (including all descendent
  484. /// folders) is given.
  485. /// </summary>
  486. /// <param name="recipientId"></param>
  487. /// <param name="senderId">ID of the sender of the item</param>
  488. /// <param name="folderId"></param>
  489. /// <param name="recipientParentFolderId">
  490. /// The id of the receipient folder in which the send folder should be placed. If UUID.Zero then the
  491. /// recipient folder is the root folder
  492. /// </param>
  493. /// <returns>
  494. /// The inventory folder copy given, null if the copy was unsuccessful
  495. /// </returns>
  496. public virtual InventoryFolderBase GiveInventoryFolder(
  497. UUID recipientId, UUID senderId, UUID folderId, UUID recipientParentFolderId)
  498. {
  499. //// Retrieve the folder from the sender
  500. InventoryFolderBase folder = InventoryService.GetFolder(new InventoryFolderBase(folderId));
  501. if (null == folder)
  502. {
  503. m_log.ErrorFormat(
  504. "[AGENT INVENTORY]: Could not find inventory folder {0} to give", folderId);
  505. return null;
  506. }
  507. if (recipientParentFolderId == UUID.Zero)
  508. {
  509. InventoryFolderBase recipientRootFolder = InventoryService.GetRootFolder(recipientId);
  510. if (recipientRootFolder != null)
  511. recipientParentFolderId = recipientRootFolder.ID;
  512. else
  513. {
  514. m_log.WarnFormat("[AGENT INVENTORY]: Unable to find root folder for receiving agent");
  515. return null;
  516. }
  517. }
  518. UUID newFolderId = UUID.Random();
  519. InventoryFolderBase newFolder
  520. = new InventoryFolderBase(
  521. newFolderId, folder.Name, recipientId, folder.Type, recipientParentFolderId, folder.Version);
  522. InventoryService.AddFolder(newFolder);
  523. // Give all the subfolders
  524. InventoryCollection contents = InventoryService.GetFolderContent(senderId, folderId);
  525. foreach (InventoryFolderBase childFolder in contents.Folders)
  526. {
  527. GiveInventoryFolder(recipientId, senderId, childFolder.ID, newFolder.ID);
  528. }
  529. // Give all the items
  530. foreach (InventoryItemBase item in contents.Items)
  531. {
  532. GiveInventoryItem(recipientId, senderId, item.ID, newFolder.ID);
  533. }
  534. return newFolder;
  535. }
  536. public void CopyInventoryItem(IClientAPI remoteClient, uint callbackID, UUID oldAgentID, UUID oldItemID,
  537. UUID newFolderID, string newName)
  538. {
  539. m_log.DebugFormat(
  540. "[AGENT INVENTORY]: CopyInventoryItem received by {0} with oldAgentID {1}, oldItemID {2}, new FolderID {3}, newName {4}",
  541. remoteClient.AgentId, oldAgentID, oldItemID, newFolderID, newName);
  542. InventoryItemBase item = null;
  543. if (LibraryService != null && LibraryService.LibraryRootFolder != null)
  544. item = LibraryService.LibraryRootFolder.FindItem(oldItemID);
  545. if (item == null)
  546. {
  547. item = new InventoryItemBase(oldItemID, remoteClient.AgentId);
  548. item = InventoryService.GetItem(item);
  549. if (item == null)
  550. {
  551. m_log.Error("[AGENT INVENTORY]: Failed to find item " + oldItemID.ToString());
  552. return;
  553. }
  554. if ((item.CurrentPermissions & (uint)PermissionMask.Copy) == 0)
  555. return;
  556. }
  557. AssetBase asset = AssetService.Get(item.AssetID.ToString());
  558. if (asset != null)
  559. {
  560. if (newName != String.Empty)
  561. {
  562. asset.Name = newName;
  563. }
  564. else
  565. {
  566. newName = item.Name;
  567. }
  568. if (remoteClient.AgentId == oldAgentID)
  569. {
  570. CreateNewInventoryItem(
  571. remoteClient, item.CreatorId, newFolderID, newName, item.Flags, callbackID, asset, (sbyte)item.InvType,
  572. item.BasePermissions, item.CurrentPermissions, item.EveryOnePermissions, item.NextPermissions, item.GroupPermissions, Util.UnixTimeSinceEpoch());
  573. }
  574. else
  575. {
  576. CreateNewInventoryItem(
  577. remoteClient, item.CreatorId, newFolderID, newName, item.Flags, callbackID, asset, (sbyte)item.InvType,
  578. item.NextPermissions, item.NextPermissions, item.EveryOnePermissions & item.NextPermissions, item.NextPermissions, item.GroupPermissions, Util.UnixTimeSinceEpoch());
  579. }
  580. }
  581. else
  582. {
  583. m_log.ErrorFormat(
  584. "[AGENT INVENTORY]: Could not copy item {0} since asset {1} could not be found",
  585. item.Name, item.AssetID);
  586. }
  587. }
  588. /// <summary>
  589. /// Create a new asset data structure.
  590. /// </summary>
  591. private AssetBase CreateAsset(string name, string description, sbyte assetType, byte[] data, UUID creatorID)
  592. {
  593. AssetBase asset = new AssetBase(UUID.Random(), name, assetType, creatorID.ToString());
  594. asset.Description = description;
  595. asset.Data = (data == null) ? new byte[1] : data;
  596. return asset;
  597. }
  598. /// <summary>
  599. /// Move an item within the agent's inventory.
  600. /// </summary>
  601. /// <param name="remoteClient"></param>
  602. /// <param name="folderID"></param>
  603. /// <param name="itemID"></param>
  604. /// <param name="length"></param>
  605. /// <param name="newName"></param>
  606. public void MoveInventoryItem(IClientAPI remoteClient, List<InventoryItemBase> items)
  607. {
  608. m_log.DebugFormat(
  609. "[AGENT INVENTORY]: Moving {0} items for user {1}", items.Count, remoteClient.AgentId);
  610. if (!InventoryService.MoveItems(remoteClient.AgentId, items))
  611. m_log.Warn("[AGENT INVENTORY]: Failed to move items for user " + remoteClient.AgentId);
  612. }
  613. /// <summary>
  614. /// Create a new inventory item.
  615. /// </summary>
  616. /// <param name="remoteClient"></param>
  617. /// <param name="folderID"></param>
  618. /// <param name="callbackID"></param>
  619. /// <param name="asset"></param>
  620. /// <param name="invType"></param>
  621. /// <param name="nextOwnerMask"></param>
  622. private void CreateNewInventoryItem(IClientAPI remoteClient, string creatorID, UUID folderID, string name, uint flags, uint callbackID,
  623. AssetBase asset, sbyte invType, uint nextOwnerMask, int creationDate)
  624. {
  625. CreateNewInventoryItem(
  626. remoteClient, creatorID, folderID, name, flags, callbackID, asset, invType,
  627. (uint)PermissionMask.All, (uint)PermissionMask.All, 0, nextOwnerMask, 0, creationDate);
  628. }
  629. /// <summary>
  630. /// Create a new Inventory Item
  631. /// </summary>
  632. /// <param name="remoteClient"></param>
  633. /// <param name="folderID"></param>
  634. /// <param name="callbackID"></param>
  635. /// <param name="asset"></param>
  636. /// <param name="invType"></param>
  637. /// <param name="nextOwnerMask"></param>
  638. /// <param name="creationDate"></param>
  639. private void CreateNewInventoryItem(
  640. IClientAPI remoteClient, string creatorID, UUID folderID, string name, uint flags, uint callbackID, AssetBase asset, sbyte invType,
  641. uint baseMask, uint currentMask, uint everyoneMask, uint nextOwnerMask, uint groupMask, int creationDate)
  642. {
  643. InventoryItemBase item = new InventoryItemBase();
  644. item.Owner = remoteClient.AgentId;
  645. item.CreatorId = creatorID;
  646. item.ID = UUID.Random();
  647. item.AssetID = asset.FullID;
  648. item.Description = asset.Description;
  649. item.Name = name;
  650. item.Flags = flags;
  651. item.AssetType = asset.Type;
  652. item.InvType = invType;
  653. item.Folder = folderID;
  654. item.CurrentPermissions = currentMask;
  655. item.NextPermissions = nextOwnerMask;
  656. item.EveryOnePermissions = everyoneMask;
  657. item.GroupPermissions = groupMask;
  658. item.BasePermissions = baseMask;
  659. item.CreationDate = creationDate;
  660. if (InventoryService.AddItem(item))
  661. remoteClient.SendInventoryItemCreateUpdate(item, callbackID);
  662. else
  663. {
  664. m_dialogModule.SendAlertToUser(remoteClient, "Failed to create item");
  665. m_log.WarnFormat(
  666. "Failed to add item for {0} in CreateNewInventoryItem!",
  667. remoteClient.Name);
  668. }
  669. }
  670. /// <summary>
  671. /// Create a new inventory item. Called when the client creates a new item directly within their
  672. /// inventory (e.g. by selecting a context inventory menu option).
  673. /// </summary>
  674. /// <param name="remoteClient"></param>
  675. /// <param name="transactionID"></param>
  676. /// <param name="folderID"></param>
  677. /// <param name="callbackID"></param>
  678. /// <param name="description"></param>
  679. /// <param name="name"></param>
  680. /// <param name="invType"></param>
  681. /// <param name="type"></param>
  682. /// <param name="wearableType"></param>
  683. /// <param name="nextOwnerMask"></param>
  684. public void CreateNewInventoryItem(IClientAPI remoteClient, UUID transactionID, UUID folderID,
  685. uint callbackID, string description, string name, sbyte invType,
  686. sbyte assetType,
  687. byte wearableType, uint nextOwnerMask, int creationDate)
  688. {
  689. m_log.DebugFormat("[AGENT INVENTORY]: Received request to create inventory item {0} in folder {1}", name, folderID);
  690. if (!Permissions.CanCreateUserInventory(invType, remoteClient.AgentId))
  691. return;
  692. if (transactionID == UUID.Zero)
  693. {
  694. ScenePresence presence;
  695. if (TryGetScenePresence(remoteClient.AgentId, out presence))
  696. {
  697. byte[] data = null;
  698. if (invType == (sbyte)InventoryType.Landmark && presence != null)
  699. {
  700. Vector3 pos = presence.AbsolutePosition;
  701. string strdata = String.Format(
  702. "Landmark version 2\nregion_id {0}\nlocal_pos {1} {2} {3}\nregion_handle {4}\n",
  703. presence.Scene.RegionInfo.RegionID,
  704. pos.X, pos.Y, pos.Z,
  705. presence.RegionHandle);
  706. data = Encoding.ASCII.GetBytes(strdata);
  707. }
  708. AssetBase asset = CreateAsset(name, description, assetType, data, remoteClient.AgentId);
  709. AssetService.Store(asset);
  710. CreateNewInventoryItem(remoteClient, remoteClient.AgentId.ToString(), folderID, asset.Name, 0, callbackID, asset, invType, nextOwnerMask, creationDate);
  711. }
  712. else
  713. {
  714. m_log.ErrorFormat(
  715. "ScenePresence for agent uuid {0} unexpectedly not found in CreateNewInventoryItem",
  716. remoteClient.AgentId);
  717. }
  718. }
  719. else
  720. {
  721. IAgentAssetTransactions agentTransactions = this.RequestModuleInterface<IAgentAssetTransactions>();
  722. if (agentTransactions != null)
  723. {
  724. agentTransactions.HandleItemCreationFromTransaction(
  725. remoteClient, transactionID, folderID, callbackID, description,
  726. name, invType, assetType, wearableType, nextOwnerMask);
  727. }
  728. }
  729. }
  730. private void HandleLinkInventoryItem(IClientAPI remoteClient, UUID transActionID, UUID folderID,
  731. uint callbackID, string description, string name,
  732. sbyte invType, sbyte type, UUID olditemID)
  733. {
  734. m_log.DebugFormat("[AGENT INVENTORY]: Received request to create inventory item link {0} in folder {1} pointing to {2}", name, folderID, olditemID);
  735. if (!Permissions.CanCreateUserInventory(invType, remoteClient.AgentId))
  736. return;
  737. ScenePresence presence;
  738. if (TryGetScenePresence(remoteClient.AgentId, out presence))
  739. {
  740. byte[] data = null;
  741. AssetBase asset = new AssetBase();
  742. asset.FullID = olditemID;
  743. asset.Type = type;
  744. asset.Name = name;
  745. asset.Description = description;
  746. CreateNewInventoryItem(remoteClient, remoteClient.AgentId.ToString(), folderID, name, 0, callbackID, asset, invType, (uint)PermissionMask.All, (uint)PermissionMask.All, (uint)PermissionMask.All, (uint)PermissionMask.All, (uint)PermissionMask.All, Util.UnixTimeSinceEpoch());
  747. }
  748. else
  749. {
  750. m_log.ErrorFormat(
  751. "ScenePresence for agent uuid {0} unexpectedly not found in HandleLinkInventoryItem",
  752. remoteClient.AgentId);
  753. }
  754. }
  755. /// <summary>
  756. /// Remove an inventory item for the client's inventory
  757. /// </summary>
  758. /// <param name="remoteClient"></param>
  759. /// <param name="itemID"></param>
  760. private void RemoveInventoryItem(IClientAPI remoteClient, List<UUID> itemIDs)
  761. {
  762. //m_log.Debug("[SCENE INVENTORY]: user " + remoteClient.AgentId);
  763. InventoryService.DeleteItems(remoteClient.AgentId, itemIDs);
  764. }
  765. /// <summary>
  766. /// Removes an inventory folder. This packet is sent when the user
  767. /// right-clicks a folder that's already in trash and chooses "purge"
  768. /// </summary>
  769. /// <param name="remoteClient"></param>
  770. /// <param name="folderID"></param>
  771. private void RemoveInventoryFolder(IClientAPI remoteClient, List<UUID> folderIDs)
  772. {
  773. m_log.DebugFormat("[SCENE INVENTORY]: RemoveInventoryFolders count {0}", folderIDs.Count);
  774. InventoryService.DeleteFolders(remoteClient.AgentId, folderIDs);
  775. }
  776. /// <summary>
  777. /// Send the details of a prim's inventory to the client.
  778. /// </summary>
  779. /// <param name="remoteClient"></param>
  780. /// <param name="primLocalID"></param>
  781. public void RequestTaskInventory(IClientAPI remoteClient, uint primLocalID)
  782. {
  783. SceneObjectGroup group = GetGroupByPrim(primLocalID);
  784. if (group != null)
  785. {
  786. bool fileChange = group.GetPartInventoryFileName(remoteClient, primLocalID);
  787. if (fileChange)
  788. {
  789. if (XferManager != null)
  790. {
  791. group.RequestInventoryFile(remoteClient, primLocalID, XferManager);
  792. }
  793. }
  794. }
  795. else
  796. {
  797. m_log.ErrorFormat(
  798. "[PRIM INVENTORY]: Inventory requested of prim {0} which doesn't exist", primLocalID);
  799. }
  800. }
  801. /// <summary>
  802. /// Remove an item from a prim (task) inventory
  803. /// </summary>
  804. /// <param name="remoteClient">Unused at the moment but retained since the avatar ID might
  805. /// be necessary for a permissions check at some stage.</param>
  806. /// <param name="itemID"></param>
  807. /// <param name="localID"></param>
  808. public void RemoveTaskInventory(IClientAPI remoteClient, UUID itemID, uint localID)
  809. {
  810. SceneObjectPart part = GetSceneObjectPart(localID);
  811. SceneObjectGroup group = part.ParentGroup;
  812. if (group != null)
  813. {
  814. TaskInventoryItem item = group.GetInventoryItem(localID, itemID);
  815. if (item == null)
  816. return;
  817. if (item.Type == 10)
  818. {
  819. part.RemoveScriptEvents(itemID);
  820. EventManager.TriggerRemoveScript(localID, itemID);
  821. }
  822. group.RemoveInventoryItem(localID, itemID);
  823. part.GetProperties(remoteClient);
  824. }
  825. else
  826. {
  827. m_log.ErrorFormat(
  828. "[PRIM INVENTORY]: " +
  829. "Removal of item {0} requested of prim {1} but this prim does not exist",
  830. itemID,
  831. localID);
  832. }
  833. }
  834. private InventoryItemBase CreateAgentInventoryItemFromTask(UUID destAgent, SceneObjectPart part, UUID itemId)
  835. {
  836. Console.WriteLine("CreateAgentInventoryItemFromTask");
  837. TaskInventoryItem taskItem = part.Inventory.GetInventoryItem(itemId);
  838. if (null == taskItem)
  839. {
  840. m_log.ErrorFormat(
  841. "[PRIM INVENTORY]: Tried to retrieve item ID {0} from prim {1}, {2} for creating an avatar"
  842. + " inventory item from a prim's inventory item "
  843. + " but the required item does not exist in the prim's inventory",
  844. itemId, part.Name, part.UUID);
  845. return null;
  846. }
  847. if ((destAgent != taskItem.OwnerID) && ((taskItem.CurrentPermissions & (uint)PermissionMask.Transfer) == 0))
  848. {
  849. return null;
  850. }
  851. InventoryItemBase agentItem = new InventoryItemBase();
  852. agentItem.ID = UUID.Random();
  853. agentItem.CreatorId = taskItem.CreatorID.ToString();
  854. agentItem.Owner = destAgent;
  855. agentItem.AssetID = taskItem.AssetID;
  856. agentItem.Description = taskItem.Description;
  857. agentItem.Name = taskItem.Name;
  858. agentItem.AssetType = taskItem.Type;
  859. agentItem.InvType = taskItem.InvType;
  860. agentItem.Flags = taskItem.Flags;
  861. if ((part.OwnerID != destAgent) && Permissions.PropagatePermissions())
  862. {
  863. agentItem.BasePermissions = taskItem.BasePermissions & (taskItem.NextPermissions | (uint)PermissionMask.Move);
  864. if (taskItem.InvType == (int)InventoryType.Object)
  865. agentItem.CurrentPermissions = agentItem.BasePermissions & (((taskItem.CurrentPermissions & 7) << 13) | (taskItem.CurrentPermissions & (uint)PermissionMask.Move));
  866. else
  867. agentItem.CurrentPermissions = agentItem.BasePermissions & taskItem.CurrentPermissions;
  868. agentItem.CurrentPermissions |= 16; // Slam
  869. agentItem.NextPermissions = taskItem.NextPermissions;
  870. agentItem.EveryOnePermissions = taskItem.EveryonePermissions & (taskItem.NextPermissions | (uint)PermissionMask.Move);
  871. agentItem.GroupPermissions = taskItem.GroupPermissions & taskItem.NextPermissions;
  872. }
  873. else
  874. {
  875. agentItem.BasePermissions = taskItem.BasePermissions;
  876. agentItem.CurrentPermissions = taskItem.CurrentPermissions;
  877. agentItem.NextPermissions = taskItem.NextPermissions;
  878. agentItem.EveryOnePermissions = taskItem.EveryonePermissions;
  879. agentItem.GroupPermissions = taskItem.GroupPermissions;
  880. }
  881. if (!Permissions.BypassPermissions())
  882. {
  883. if ((taskItem.CurrentPermissions & (uint)PermissionMask.Copy) == 0)
  884. part.Inventory.RemoveInventoryItem(itemId);
  885. }
  886. return agentItem;
  887. }
  888. /// <summary>
  889. /// Move the given item in the given prim to a folder in the client's inventory
  890. /// </summary>
  891. /// <param name="remoteClient"></param>
  892. /// <param name="folderID"></param>
  893. /// <param name="part"></param>
  894. /// <param name="itemID"></param>
  895. public InventoryItemBase MoveTaskInventoryItem(IClientAPI remoteClient, UUID folderId, SceneObjectPart part, UUID itemId)
  896. {
  897. m_log.Info("Adding task inventory");
  898. InventoryItemBase agentItem = CreateAgentInventoryItemFromTask(remoteClient.AgentId, part, itemId);
  899. if (agentItem == null)
  900. return null;
  901. agentItem.Folder = folderId;
  902. AddInventoryItem(remoteClient, agentItem);
  903. return agentItem;
  904. }
  905. /// <summary>
  906. /// <see>ClientMoveTaskInventoryItem</see>
  907. /// </summary>
  908. /// <param name="remoteClient"></param>
  909. /// <param name="folderID"></param>
  910. /// <param name="primLocalID"></param>
  911. /// <param name="itemID"></param>
  912. public void ClientMoveTaskInventoryItem(IClientAPI remoteClient, UUID folderId, uint primLocalId, UUID itemId)
  913. {
  914. SceneObjectPart part = GetSceneObjectPart(primLocalId);
  915. if (null == part)
  916. {
  917. m_log.WarnFormat(
  918. "[PRIM INVENTORY]: " +
  919. "Move of inventory item {0} from prim with local id {1} failed because the prim could not be found",
  920. itemId, primLocalId);
  921. return;
  922. }
  923. TaskInventoryItem taskItem = part.Inventory.GetInventoryItem(itemId);
  924. if (null == taskItem)
  925. {
  926. m_log.WarnFormat("[PRIM INVENTORY]: Move of inventory item {0} from prim with local id {1} failed"
  927. + " because the inventory item could not be found",
  928. itemId, primLocalId);
  929. return;
  930. }
  931. // Only owner can copy
  932. if (remoteClient.AgentId != taskItem.OwnerID)
  933. return;
  934. MoveTaskInventoryItem(remoteClient, folderId, part, itemId);
  935. }
  936. /// <summary>
  937. /// <see>MoveTaskInventoryItem</see>
  938. /// </summary>
  939. /// <param name="remoteClient"></param>
  940. /// <param name="folderID"></param>
  941. /// <param name="part"></param>
  942. /// <param name="itemID"></param>
  943. public InventoryItemBase MoveTaskInventoryItem(UUID avatarId, UUID folderId, SceneObjectPart part, UUID itemId)
  944. {
  945. ScenePresence avatar;
  946. if (TryGetScenePresence(avatarId, out avatar))
  947. {
  948. return MoveTaskInventoryItem(avatar.ControllingClient, folderId, part, itemId);
  949. }
  950. else
  951. {
  952. InventoryItemBase agentItem = CreateAgentInventoryItemFromTask(avatarId, part, itemId);
  953. if (agentItem == null)
  954. return null;
  955. agentItem.Folder = folderId;
  956. AddInventoryItem(avatarId, agentItem);
  957. return agentItem;
  958. }
  959. }
  960. /// <summary>
  961. /// Copy a task (prim) inventory item to another task (prim)
  962. /// </summary>
  963. /// <param name="destId"></param>
  964. /// <param name="part"></param>
  965. /// <param name="itemId"></param>
  966. public void MoveTaskInventoryItem(UUID destId, SceneObjectPart part, UUID itemId)
  967. {
  968. TaskInventoryItem srcTaskItem = part.Inventory.GetInventoryItem(itemId);
  969. if (srcTaskItem == null)
  970. {
  971. m_log.ErrorFormat(
  972. "[PRIM INVENTORY]: Tried to retrieve item ID {0} from prim {1}, {2} for moving"
  973. + " but the item does not exist in this inventory",
  974. itemId, part.Name, part.UUID);
  975. return;
  976. }
  977. SceneObjectPart destPart = GetSceneObjectPart(destId);
  978. if (destPart == null)
  979. {
  980. m_log.ErrorFormat(
  981. "[PRIM INVENTORY]: " +
  982. "Could not find prim for ID {0}",
  983. destId);
  984. return;
  985. }
  986. // Can't transfer this
  987. //
  988. if ((part.OwnerID != destPart.OwnerID) && ((srcTaskItem.CurrentPermissions & (uint)PermissionMask.Transfer) == 0))
  989. return;
  990. if (part.OwnerID != destPart.OwnerID && (part.GetEffectiveObjectFlags() & (uint)PrimFlags.AllowInventoryDrop) == 0)
  991. {
  992. // object cannot copy items to an object owned by a different owner
  993. // unless llAllowInventoryDrop has been called
  994. return;
  995. }
  996. // must have both move and modify permission to put an item in an object
  997. if ((part.OwnerMask & ((uint)PermissionMask.Move | (uint)PermissionMask.Modify)) == 0)
  998. {
  999. return;
  1000. }
  1001. TaskInventoryItem destTaskItem = new TaskInventoryItem();
  1002. destTaskItem.ItemID = UUID.Random();
  1003. destTaskItem.CreatorID = srcTaskItem.CreatorID;
  1004. destTaskItem.AssetID = srcTaskItem.AssetID;
  1005. destTaskItem.GroupID = destPart.GroupID;
  1006. destTaskItem.OwnerID = destPart.OwnerID;
  1007. destTaskItem.ParentID = destPart.UUID;
  1008. destTaskItem.ParentPartID = destPart.UUID;
  1009. destTaskItem.BasePermissions = srcTaskItem.BasePermissions;
  1010. destTaskItem.EveryonePermissions = srcTaskItem.EveryonePermissions;
  1011. destTaskItem.GroupPermissions = srcTaskItem.GroupPermissions;
  1012. destTaskItem.CurrentPermissions = srcTaskItem.CurrentPermissions;
  1013. destTaskItem.NextPermissions = srcTaskItem.NextPermissions;
  1014. destTaskItem.Flags = srcTaskItem.Flags;
  1015. if (destPart.OwnerID != part.OwnerID)
  1016. {
  1017. if (Permissions.PropagatePermissions())
  1018. {
  1019. destTaskItem.CurrentPermissions = srcTaskItem.CurrentPermissions &
  1020. (srcTaskItem.NextPermissions | (uint)PermissionMask.Move);
  1021. destTaskItem.GroupPermissions = srcTaskItem.GroupPermissions &
  1022. (srcTaskItem.NextPermissions | (uint)PermissionMask.Move);
  1023. destTaskItem.EveryonePermissions = srcTaskItem.EveryonePermissions &
  1024. (srcTaskItem.NextPermissions | (uint)PermissionMask.Move);
  1025. destTaskItem.BasePermissions = srcTaskItem.BasePermissions &
  1026. (srcTaskItem.NextPermissions | (uint)PermissionMask.Move);
  1027. destTaskItem.CurrentPermissions |= 16; // Slam!
  1028. }
  1029. }
  1030. destTaskItem.Description = srcTaskItem.Description;
  1031. destTaskItem.Name = srcTaskItem.Name;
  1032. destTaskItem.InvType = srcTaskItem.InvType;
  1033. destTaskItem.Type = srcTaskItem.Type;
  1034. destPart.Inventory.AddInventoryItem(destTaskItem, part.OwnerID != destPart.OwnerID);
  1035. if ((srcTaskItem.CurrentPermissions & (uint)PermissionMask.Copy) == 0)
  1036. part.Inventory.RemoveInventoryItem(itemId);
  1037. ScenePresence avatar;
  1038. if (TryGetScenePresence(srcTaskItem.OwnerID, out avatar))
  1039. {
  1040. destPart.GetProperties(avatar.ControllingClient);
  1041. }
  1042. }
  1043. public UUID MoveTaskInventoryItems(UUID destID, string category, SceneObjectPart host, List<UUID> items)
  1044. {
  1045. InventoryFolderBase rootFolder = InventoryService.GetRootFolder(destID);
  1046. UUID newFolderID = UUID.Random();
  1047. InventoryFolderBase newFolder = new InventoryFolderBase(newFolderID, category, destID, -1, rootFolder.ID, rootFolder.Version);
  1048. InventoryService.AddFolder(newFolder);
  1049. foreach (UUID itemID in items)
  1050. {
  1051. InventoryItemBase agentItem = CreateAgentInventoryItemFromTask(destID, host, itemID);
  1052. if (agentItem != null)
  1053. {
  1054. agentItem.Folder = newFolderID;
  1055. AddInventoryItem(destID, agentItem);
  1056. }
  1057. }
  1058. ScenePresence avatar = null;
  1059. if (TryGetScenePresence(destID, out avatar))
  1060. {
  1061. //profile.SendInventoryDecendents(avatar.ControllingClient,
  1062. // profile.RootFolder.ID, true, false);
  1063. //profile.SendInventoryDecendents(avatar.ControllingClient,
  1064. // newFolderID, false, true);
  1065. SendInventoryUpdate(avatar.ControllingClient, rootFolder, true, false);
  1066. SendInventoryUpdate(avatar.ControllingClient, newFolder, false, true);
  1067. }
  1068. return newFolderID;
  1069. }
  1070. private void SendInventoryUpdate(IClientAPI client, InventoryFolderBase folder, bool fetchFolders, bool fetchItems)
  1071. {
  1072. if (folder == null)
  1073. return;
  1074. // TODO: This code for looking in the folder for the library should be folded somewhere else
  1075. // so that this class doesn't have to know the details (and so that multiple libraries, etc.
  1076. // can be handled transparently).
  1077. InventoryFolderImpl fold = null;
  1078. if (LibraryService != null && LibraryService.LibraryRootFolder != null)
  1079. {
  1080. if ((fold = LibraryService.LibraryRootFolder.FindFolder(folder.ID)) != null)
  1081. {
  1082. client.SendInventoryFolderDetails(
  1083. fold.Owner, folder.ID, fold.RequestListOfItems(),
  1084. fold.RequestListOfFolders(), fold.Version, fetchFolders, fetchItems);
  1085. return;
  1086. }
  1087. }
  1088. // Fetch the folder contents
  1089. InventoryCollection contents = InventoryService.GetFolderContent(client.AgentId, folder.ID);
  1090. // Fetch the folder itself to get its current version
  1091. InventoryFolderBase containingFolder = new InventoryFolderBase(folder.ID, client.AgentId);
  1092. containingFolder = InventoryService.GetFolder(containingFolder);
  1093. //m_log.DebugFormat("[AGENT INVENTORY]: Sending inventory folder contents ({0} nodes) for \"{1}\" to {2} {3}",
  1094. // contents.Folders.Count + contents.Items.Count, containingFolder.Name, client.FirstName, client.LastName);
  1095. if (containingFolder != null && containingFolder != null)
  1096. client.SendInventoryFolderDetails(client.AgentId, folder.ID, contents.Items, contents.Folders, containingFolder.Version, fetchFolders, fetchItems);
  1097. }
  1098. /// <summary>
  1099. /// Update an item in a prim (task) inventory.
  1100. /// This method does not handle scripts, <see>RezScript(IClientAPI, UUID, unit)</see>
  1101. /// </summary>
  1102. /// <param name="remoteClient"></param>
  1103. /// <param name="transactionID"></param>
  1104. /// <param name="itemInfo"></param>
  1105. /// <param name="primLocalID"></param>
  1106. public void UpdateTaskInventory(IClientAPI remoteClient, UUID transactionID, TaskInventoryItem itemInfo,
  1107. uint primLocalID)
  1108. {
  1109. UUID itemID = itemInfo.ItemID;
  1110. // Find the prim we're dealing with
  1111. SceneObjectPart part = GetSceneObjectPart(primLocalID);
  1112. if (part != null)
  1113. {
  1114. TaskInventoryItem currentItem = part.Inventory.GetInventoryItem(itemID);
  1115. bool allowInventoryDrop = (part.GetEffectiveObjectFlags()
  1116. & (uint)PrimFlags.AllowInventoryDrop) != 0;
  1117. // Explicity allow anyone to add to the inventory if the
  1118. // AllowInventoryDrop flag has been set. Don't however let
  1119. // them update an item unless they pass the external checks
  1120. //
  1121. if (!Permissions.CanEditObjectInventory(part.UUID, remoteClient.AgentId)
  1122. && (currentItem != null || !allowInventoryDrop))
  1123. return;
  1124. if (currentItem == null)
  1125. {
  1126. UUID copyID = UUID.Random();
  1127. if (itemID != UUID.Zero)
  1128. {
  1129. InventoryItemBase item = new InventoryItemBase(itemID, remoteClient.AgentId);
  1130. item = InventoryService.GetItem(item);
  1131. // Try library
  1132. if (null == item && LibraryService != null && LibraryService.LibraryRootFolder != null)
  1133. {
  1134. item = LibraryService.LibraryRootFolder.FindItem(itemID);
  1135. }
  1136. // If we've found the item in the user's inventory or in the library
  1137. if (item != null)
  1138. {
  1139. part.ParentGroup.AddInventoryItem(remoteClient, primLocalID, item, copyID);
  1140. m_log.InfoFormat(
  1141. "[PRIM INVENTORY]: Update with item {0} requested of prim {1} for {2}",
  1142. item.Name, primLocalID, remoteClient.Name);
  1143. part.GetProperties(remoteClient);
  1144. if (!Permissions.BypassPermissions())
  1145. {
  1146. if ((item.CurrentPermissions & (uint)PermissionMask.Copy) == 0)
  1147. {
  1148. List<UUID> uuids = new List<UUID>();
  1149. uuids.Add(itemID);
  1150. RemoveInventoryItem(remoteClient, uuids);
  1151. }
  1152. }
  1153. }
  1154. else
  1155. {
  1156. m_log.ErrorFormat(
  1157. "[PRIM INVENTORY]: Could not find inventory item {0} to update for {1}!",
  1158. itemID, remoteClient.Name);
  1159. }
  1160. }
  1161. }
  1162. else // Updating existing item with new perms etc
  1163. {
  1164. IAgentAssetTransactions agentTransactions = this.RequestModuleInterface<IAgentAssetTransactions>();
  1165. if (agentTransactions != null)
  1166. {
  1167. agentTransactions.HandleTaskItemUpdateFromTransaction(
  1168. remoteClient, part, transactionID, currentItem);
  1169. }
  1170. if (part.Inventory.UpdateInventoryItem(itemInfo))
  1171. {
  1172. if ((InventoryType)itemInfo.InvType == InventoryType.Notecard)
  1173. remoteClient.SendAgentAlertMessage("Notecard saved", false);
  1174. else if ((InventoryType)itemInfo.InvType == InventoryType.LSL)
  1175. remoteClient.SendAgentAlertMessage("Script saved", false);
  1176. else
  1177. remoteClient.SendAgentAlertMessage("Item saved", false);
  1178. part.GetProperties(remoteClient);
  1179. }
  1180. }
  1181. }
  1182. else
  1183. {
  1184. m_log.WarnFormat(
  1185. "[PRIM INVENTORY]: " +
  1186. "Update with item {0} requested of prim {1} for {2} but this prim does not exist",
  1187. itemID, primLocalID, remoteClient.Name);
  1188. }
  1189. }
  1190. /// <summary>
  1191. /// Rez a script into a prim's inventory, either ex nihilo or from an existing avatar inventory
  1192. /// </summary>
  1193. /// <param name="remoteClient"></param>
  1194. /// <param name="itemID"> </param>
  1195. /// <param name="localID"></param>
  1196. public void RezScript(IClientAPI remoteClient, InventoryItemBase itemBase, UUID transactionID, uint localID)
  1197. {
  1198. UUID itemID = itemBase.ID;
  1199. UUID copyID = UUID.Random();
  1200. if (itemID != UUID.Zero) // transferred from an avatar inventory to the prim's inventory
  1201. {
  1202. InventoryItemBase item = new InventoryItemBase(itemID, remoteClient.AgentId);
  1203. item = InventoryService.GetItem(item);
  1204. // Try library
  1205. // XXX clumsy, possibly should be one call
  1206. if (null == item && LibraryService != null && LibraryService.LibraryRootFolder != null)
  1207. {
  1208. item = LibraryService.LibraryRootFolder.FindItem(itemID);
  1209. }
  1210. if (item != null)
  1211. {
  1212. SceneObjectPart part = GetSceneObjectPart(localID);
  1213. if (part != null)
  1214. {
  1215. if (!Permissions.CanEditObjectInventory(part.UUID, remoteClient.AgentId))
  1216. return;
  1217. part.ParentGroup.AddInventoryItem(remoteClient, localID, item, copyID);
  1218. // TODO: switch to posting on_rez here when scripts
  1219. // have state in inventory
  1220. part.Inventory.CreateScriptInstance(copyID, 0, false, DefaultScriptEngine, 0);
  1221. // m_log.InfoFormat("[PRIMINVENTORY]: " +
  1222. // "Rezzed script {0} into prim local ID {1} for user {2}",
  1223. // item.inventoryName, localID, remoteClient.Name);
  1224. part.GetProperties(remoteClient);
  1225. part.ParentGroup.ResumeScripts();
  1226. }
  1227. else
  1228. {
  1229. m_log.ErrorFormat(
  1230. "[PRIM INVENTORY]: " +
  1231. "Could not rez script {0} into prim local ID {1} for user {2}"
  1232. + " because the prim could not be found in the region!",
  1233. item.Name, localID, remoteClient.Name);
  1234. }
  1235. }
  1236. else
  1237. {
  1238. m_log.ErrorFormat(
  1239. "[PRIM INVENTORY]: Could not find script inventory item {0} to rez for {1}!",
  1240. itemID, remoteClient.Name);
  1241. }
  1242. }
  1243. else // script has been rezzed directly into a prim's inventory
  1244. {
  1245. SceneObjectPart part = GetSceneObjectPart(itemBase.Folder);
  1246. if (part == null)
  1247. return;
  1248. if (part.OwnerID != remoteClient.AgentId)
  1249. {
  1250. // Group permissions
  1251. if ((part.GroupID == UUID.Zero) || (remoteClient.GetGroupPowers(part.GroupID) == 0) || ((part.GroupMask & (uint)PermissionMask.Modify) == 0))
  1252. return;
  1253. } else {
  1254. if ((part.OwnerMask & (uint)PermissionMask.Modify) == 0)
  1255. return;
  1256. }
  1257. if (!Permissions.CanCreateObjectInventory(
  1258. itemBase.InvType, part.UUID, remoteClient.AgentId))
  1259. return;
  1260. AssetBase asset = CreateAsset(itemBase.Name, itemBase.Description, (sbyte)itemBase.AssetType,
  1261. Encoding.ASCII.GetBytes("default\n{\n state_entry()\n {\n llSay(0, \"Script running\");\n }\n}"),
  1262. remoteClient.AgentId);
  1263. AssetService.Store(asset);
  1264. TaskInventoryItem taskItem = new TaskInventoryItem();
  1265. taskItem.ResetIDs(itemBase.Folder);
  1266. taskItem.ParentID = itemBase.Folder;
  1267. taskItem.CreationDate = (uint)itemBase.CreationDate;
  1268. taskItem.Name = itemBase.Name;
  1269. taskItem.Description = itemBase.Description;
  1270. taskItem.Type = itemBase.AssetType;
  1271. taskItem.InvType = itemBase.InvType;
  1272. taskItem.OwnerID = itemBase.Owner;
  1273. taskItem.CreatorID = itemBase.CreatorIdAsUuid;
  1274. taskItem.BasePermissions = itemBase.BasePermissions;
  1275. taskItem.CurrentPermissions = itemBase.CurrentPermissions;
  1276. taskItem.EveryonePermissions = itemBase.EveryOnePermissions;
  1277. taskItem.GroupPermissions = itemBase.GroupPermissions;
  1278. taskItem.NextPermissions = itemBase.NextPermissions;
  1279. taskItem.GroupID = itemBase.GroupID;
  1280. taskItem.GroupPermissions = 0;
  1281. taskItem.Flags = itemBase.Flags;
  1282. taskItem.PermsGranter = UUID.Zero;
  1283. taskItem.PermsMask = 0;
  1284. taskItem.AssetID = asset.FullID;
  1285. part.Inventory.AddInventoryItem(taskItem, false);
  1286. part.GetProperties(remoteClient);
  1287. part.Inventory.CreateScriptInstance(taskItem, 0, false, DefaultScriptEngine, 0);
  1288. part.ParentGroup.ResumeScripts();
  1289. }
  1290. }
  1291. /// <summary>
  1292. /// Rez a script into a prim's inventory from another prim
  1293. /// </summary>
  1294. /// <param name="remoteClient"></param>
  1295. /// <param name="itemID"> </param>
  1296. /// <param name="localID"></param>
  1297. public void RezScript(UUID srcId, SceneObjectPart srcPart, UUID destId, int pin, int running, int start_param)
  1298. {
  1299. TaskInventoryItem srcTaskItem = srcPart.Inventory.GetInventoryItem(srcId);
  1300. if (srcTaskItem == null)
  1301. {
  1302. m_log.ErrorFormat(
  1303. "[PRIM INVENTORY]: Tried to retrieve item ID {0} from prim {1}, {2} for rezzing a script but the "
  1304. + " item does not exist in this inventory",
  1305. srcId, srcPart.Name, srcPart.UUID);
  1306. return;
  1307. }
  1308. SceneObjectPart destPart = GetSceneObjectPart(destId);
  1309. if (destPart == null)
  1310. {
  1311. m_log.ErrorFormat(
  1312. "[PRIM INVENTORY]: " +
  1313. "Could not find script for ID {0}",
  1314. destId);
  1315. return;
  1316. }
  1317. // Must own the object, and have modify rights
  1318. if (srcPart.OwnerID != destPart.OwnerID)
  1319. {
  1320. // Group permissions
  1321. if ((destPart.GroupID == UUID.Zero) || (destPart.GroupID != srcPart.GroupID) ||
  1322. ((destPart.GroupMask & (uint)PermissionMask.Modify) == 0))
  1323. return;
  1324. } else {
  1325. if ((destPart.OwnerMask & (uint)PermissionMask.Modify) == 0)
  1326. return;
  1327. }
  1328. if (destPart.ScriptAccessPin != pin)
  1329. {
  1330. m_log.WarnFormat(
  1331. "[PRIM INVENTORY]: " +
  1332. "Script in object {0} : {1}, attempted to load script {2} : {3} into object {4} : {5} with invalid pin {6}",
  1333. srcPart.Name, srcId, srcTaskItem.Name, srcTaskItem.ItemID, destPart.Name, destId, pin);
  1334. // the LSL Wiki says we are supposed to shout on the DEBUG_CHANNEL -
  1335. // "Object: Task Object trying to illegally load script onto task Other_Object!"
  1336. // How do we shout from in here?
  1337. return;
  1338. }
  1339. TaskInventoryItem destTaskItem = new TaskInventoryItem();
  1340. destTaskItem.ItemID = UUID.Random();
  1341. destTaskItem.CreatorID = srcTaskItem.CreatorID;
  1342. destTaskItem.AssetID = srcTaskItem.AssetID;
  1343. destTaskItem.GroupID = destPart.GroupID;
  1344. destTaskItem.OwnerID = destPart.OwnerID;
  1345. destTaskItem.ParentID = destPart.UUID;
  1346. destTaskItem.ParentPartID = destPart.UUID;
  1347. destTaskItem.BasePermissions = srcTaskItem.BasePermissions;
  1348. destTaskItem.EveryonePermissions = srcTaskItem.EveryonePermissions;
  1349. destTaskItem.GroupPermissions = srcTaskItem.GroupPermissions;
  1350. destTaskItem.CurrentPermissions = srcTaskItem.CurrentPermissions;
  1351. destTaskItem.NextPermissions = srcTaskItem.NextPermissions;
  1352. destTaskItem.Flags = srcTaskItem.Flags;
  1353. if (destPart.OwnerID != srcPart.OwnerID)
  1354. {
  1355. if (Permissions.PropagatePermissions())
  1356. {
  1357. destTaskItem.CurrentPermissions = srcTaskItem.CurrentPermissions &
  1358. srcTaskItem.NextPermissions;
  1359. destTaskItem.GroupPermissions = srcTaskItem.GroupPermissions &
  1360. srcTaskItem.NextPermissions;
  1361. destTaskItem.EveryonePermissions = srcTaskItem.EveryonePermissions &
  1362. srcTaskItem.NextPermissions;
  1363. destTaskItem.BasePermissions = srcTaskItem.BasePermissions &
  1364. srcTaskItem.NextPermissions;
  1365. destTaskItem.CurrentPermissions |= 16; // Slam!
  1366. }
  1367. }
  1368. destTaskItem.Description = srcTaskItem.Description;
  1369. destTaskItem.Name = srcTaskItem.Name;
  1370. destTaskItem.InvType = srcTaskItem.InvType;
  1371. destTaskItem.Type = srcTaskItem.Type;
  1372. destPart.Inventory.AddInventoryItemExclusive(destTaskItem, false);
  1373. if (running > 0)
  1374. {
  1375. destPart.Inventory.CreateScriptInstance(destTaskItem, start_param, false, DefaultScriptEngine, 0);
  1376. }
  1377. destPart.ParentGroup.ResumeScripts();
  1378. ScenePresence avatar;
  1379. if (TryGetScenePresence(srcTaskItem.OwnerID, out avatar))
  1380. {
  1381. destPart.GetProperties(avatar.ControllingClient);
  1382. }
  1383. }
  1384. /// <summary>
  1385. /// Called when one or more objects are removed from the environment into inventory.
  1386. /// </summary>
  1387. /// <param name="remoteClient"></param>
  1388. /// <param name="localID"></param>
  1389. /// <param name="groupID"></param>
  1390. /// <param name="action"></param>
  1391. /// <param name="destinationID"></param>
  1392. public virtual void DeRezObject(IClientAPI remoteClient, List<uint> localIDs,
  1393. UUID groupID, DeRezAction action, UUID destinationID)
  1394. {
  1395. foreach (uint localID in localIDs)
  1396. {
  1397. DeRezObject(remoteClient, localID, groupID, action, destinationID);
  1398. }
  1399. }
  1400. /// <summary>
  1401. /// Called when an object is removed from the environment into inventory.
  1402. /// </summary>
  1403. /// <param name="remoteClient"></param>
  1404. /// <param name="localID"></param>
  1405. /// <param name="groupID"></param>
  1406. /// <param name="action"></param>
  1407. /// <param name="destinationID"></param>
  1408. public virtual void DeRezObject(IClientAPI remoteClient, uint localID,
  1409. UUID groupID, DeRezAction action, UUID destinationID)
  1410. {
  1411. DeRezObjects(remoteClient, new List<uint>() { localID} , groupID, action, destinationID);
  1412. }
  1413. public virtual void DeRezObjects(IClientAPI remoteClient, List<uint> localIDs,
  1414. UUID groupID, DeRezAction action, UUID destinationID)
  1415. {
  1416. // First, see of we can perform the requested action and
  1417. // build a list of eligible objects
  1418. List<uint> deleteIDs = new List<uint>();
  1419. List<SceneObjectGroup> deleteGroups = new List<SceneObjectGroup>();
  1420. // Start with true for both, then remove the flags if objects
  1421. // that we can't derez are part of the selection
  1422. bool permissionToTake = true;
  1423. bool permissionToTakeCopy = true;
  1424. bool permissionToDelete = true;
  1425. foreach (uint localID in localIDs)
  1426. {
  1427. // Invalid id
  1428. SceneObjectPart part = GetSceneObjectPart(localID);
  1429. if (part == null)
  1430. continue;
  1431. // Already deleted by someone else
  1432. if (part.ParentGroup == null || part.ParentGroup.IsDeleted)
  1433. continue;
  1434. // Can't delete child prims
  1435. if (part != part.ParentGroup.RootPart)
  1436. continue;
  1437. SceneObjectGroup grp = part.ParentGroup;
  1438. deleteIDs.Add(localID);
  1439. deleteGroups.Add(grp);
  1440. // Force a database backup/update on this SceneObjectGroup
  1441. // So that we know the database is upto date,
  1442. // for when deleting the object from it
  1443. ForceSceneObjectBackup(grp);
  1444. if (remoteClient == null)
  1445. {
  1446. // Autoreturn has a null client. Nothing else does. So
  1447. // allow only returns
  1448. if (action != DeRezAction.Return)
  1449. return;
  1450. permissionToTakeCopy = false;
  1451. }
  1452. else
  1453. {
  1454. if (!Permissions.CanTakeCopyObject(grp.UUID, remoteClient.AgentId))
  1455. permissionToTakeCopy = false;
  1456. if (!Permissions.CanTakeObject(grp.UUID, remoteClient.AgentId))
  1457. permissionToTake = false;
  1458. if (!Permissions.CanDeleteObject(grp.UUID, remoteClient.AgentId))
  1459. permissionToDelete = false;
  1460. }
  1461. }
  1462. // Handle god perms
  1463. if ((remoteClient != null) && Permissions.IsGod(remoteClient.AgentId))
  1464. {
  1465. permissionToTake = true;
  1466. permissionToTakeCopy = true;
  1467. permissionToDelete = true;
  1468. }
  1469. // If we're re-saving, we don't even want to delete
  1470. if (action == DeRezAction.SaveToExistingUserInventoryItem)
  1471. permissionToDelete = false;
  1472. // if we want to take a copy, we also don't want to delete
  1473. // Note: after this point, the permissionToTakeCopy flag
  1474. // becomes irrelevant. It already includes the permissionToTake
  1475. // permission and after excluding no copy items here, we can
  1476. // just use that.
  1477. if (action == DeRezAction.TakeCopy)
  1478. {
  1479. // If we don't have permission, stop right here
  1480. if (!permissionToTakeCopy)
  1481. return;
  1482. permissionToTake = true;
  1483. // Don't delete
  1484. permissionToDelete = false;
  1485. }
  1486. if (action == DeRezAction.Return)
  1487. {
  1488. if (remoteClient != null)
  1489. {
  1490. if (Permissions.CanReturnObjects(
  1491. null,
  1492. remoteClient.AgentId,
  1493. deleteGroups))
  1494. {
  1495. permissionToTake = true;
  1496. permissionToDelete = true;
  1497. foreach (SceneObjectGroup g in deleteGroups)
  1498. {
  1499. AddReturn(g.OwnerID, g.Name, g.AbsolutePosition, "parcel owner return");
  1500. }
  1501. }
  1502. }
  1503. else // Auto return passes through here with null agent
  1504. {
  1505. permissionToTake = true;
  1506. permissionToDelete = true;
  1507. }
  1508. }
  1509. if (permissionToTake)
  1510. {
  1511. m_asyncSceneObjectDeleter.DeleteToInventory(
  1512. action, destinationID, deleteGroups, remoteClient,
  1513. permissionToDelete);
  1514. }
  1515. else if (permissionToDelete)
  1516. {
  1517. foreach (SceneObjectGroup g in deleteGroups)
  1518. DeleteSceneObject(g, false);
  1519. }
  1520. }
  1521. public void UpdateKnownItem(IClientAPI remoteClient, SceneObjectGroup grp, UUID itemID, UUID agentID)
  1522. {
  1523. SceneObjectGroup objectGroup = grp;
  1524. if (objectGroup != null)
  1525. {
  1526. if (!grp.HasGroupChanged)
  1527. {
  1528. m_log.InfoFormat("[ATTACHMENT]: Save request for {0} which is unchanged", grp.UUID);
  1529. return;
  1530. }
  1531. m_log.InfoFormat(
  1532. "[ATTACHMENT]: Updating asset for attachment {0}, attachpoint {1}",
  1533. grp.UUID, grp.GetAttachmentPoint());
  1534. string sceneObjectXml = SceneObjectSerializer.ToOriginalXmlFormat(objectGroup);
  1535. InventoryItemBase item = new InventoryItemBase(itemID, remoteClient.AgentId);
  1536. item = InventoryService.GetItem(item);
  1537. if (item != null)
  1538. {
  1539. AssetBase asset = CreateAsset(
  1540. objectGroup.GetPartName(objectGroup.LocalId),
  1541. objectGroup.GetPartDescription(objectGroup.LocalId),
  1542. (sbyte)AssetType.Object,
  1543. Utils.StringToBytes(sceneObjectXml),
  1544. remoteClient.AgentId);
  1545. AssetService.Store(asset);
  1546. item.AssetID = asset.FullID;
  1547. item.Description = asset.Description;
  1548. item.Name = asset.Name;
  1549. item.AssetType = asset.Type;
  1550. item.InvType = (int)InventoryType.Object;
  1551. InventoryService.UpdateItem(item);
  1552. // this gets called when the agent loggs off!
  1553. if (remoteClient != null)
  1554. {
  1555. remoteClient.SendInventoryItemCreateUpdate(item, 0);
  1556. }
  1557. }
  1558. }
  1559. }
  1560. public UUID attachObjectAssetStore(IClientAPI remoteClient, SceneObjectGroup grp, UUID AgentId, out UUID itemID)
  1561. {
  1562. itemID = UUID.Zero;
  1563. if (grp != null)
  1564. {
  1565. Vector3 inventoryStoredPosition = new Vector3
  1566. (((grp.AbsolutePosition.X > (int)Constants.RegionSize)
  1567. ? 250
  1568. : grp.AbsolutePosition.X)
  1569. ,
  1570. (grp.AbsolutePosition.X > (int)Constants.RegionSize)
  1571. ? 250
  1572. : grp.AbsolutePosition.X,
  1573. grp.AbsolutePosition.Z);
  1574. Vector3 originalPosition = grp.AbsolutePosition;
  1575. grp.AbsolutePosition = inventoryStoredPosition;
  1576. string sceneObjectXml = SceneObjectSerializer.ToOriginalXmlFormat(grp);
  1577. grp.AbsolutePosition = originalPosition;
  1578. AssetBase asset = CreateAsset(
  1579. grp.GetPartName(grp.LocalId),
  1580. grp.GetPartDescription(grp.LocalId),
  1581. (sbyte)AssetType.Object,
  1582. Utils.StringToBytes(sceneObjectXml),
  1583. remoteClient.AgentId);
  1584. AssetService.Store(asset);
  1585. InventoryItemBase item = new InventoryItemBase();
  1586. item.CreatorId = grp.RootPart.CreatorID.ToString();
  1587. item.Owner = remoteClient.AgentId;
  1588. item.ID = UUID.Random();
  1589. item.AssetID = asset.FullID;
  1590. item.Description = asset.Description;
  1591. item.Name = asset.Name;
  1592. item.AssetType = asset.Type;
  1593. item.InvType = (int)InventoryType.Object;
  1594. InventoryFolderBase folder = InventoryService.GetFolderForType(remoteClient.AgentId, AssetType.Object);
  1595. if (folder != null)
  1596. item.Folder = folder.ID;
  1597. else // oopsies
  1598. item.Folder = UUID.Zero;
  1599. if ((remoteClient.AgentId != grp.RootPart.OwnerID) && Permissions.PropagatePermissions())
  1600. {
  1601. item.BasePermissions = grp.RootPart.NextOwnerMask;
  1602. item.CurrentPermissions = grp.RootPart.NextOwnerMask;
  1603. item.NextPermissions = grp.RootPart.NextOwnerMask;
  1604. item.EveryOnePermissions = grp.RootPart.EveryoneMask & grp.RootPart.NextOwnerMask;
  1605. item.GroupPermissions = grp.RootPart.GroupMask & grp.RootPart.NextOwnerMask;
  1606. }
  1607. else
  1608. {
  1609. item.BasePermissions = grp.RootPart.BaseMask;
  1610. item.CurrentPermissions = grp.RootPart.OwnerMask;
  1611. item.NextPermissions = grp.RootPart.NextOwnerMask;
  1612. item.EveryOnePermissions = grp.RootPart.EveryoneMask;
  1613. item.GroupPermissions = grp.RootPart.GroupMask;
  1614. }
  1615. item.CreationDate = Util.UnixTimeSinceEpoch();
  1616. // sets itemID so client can show item as 'attached' in inventory
  1617. grp.SetFromItemID(item.ID);
  1618. if (InventoryService.AddItem(item))
  1619. remoteClient.SendInventoryItemCreateUpdate(item, 0);
  1620. else
  1621. m_dialogModule.SendAlertToUser(remoteClient, "Operation failed");
  1622. itemID = item.ID;
  1623. return item.AssetID;
  1624. }
  1625. return UUID.Zero;
  1626. }
  1627. /// <summary>
  1628. /// Event Handler Rez an object into a scene
  1629. /// Calls the non-void event handler
  1630. /// </summary>
  1631. /// <param name="remoteClient"></param>
  1632. /// <param name="itemID"></param>
  1633. /// <param name="RayEnd"></param>
  1634. /// <param name="RayStart"></param>
  1635. /// <param name="RayTargetID"></param>
  1636. /// <param name="BypassRayCast"></param>
  1637. /// <param name="RayEndIsIntersection"></param>
  1638. /// <param name="EveryoneMask"></param>
  1639. /// <param name="GroupMask"></param>
  1640. /// <param name="RezSelected"></param>
  1641. /// <param name="RemoveItem"></param>
  1642. /// <param name="fromTaskID"></param>
  1643. public virtual void RezObject(IClientAPI remoteClient, UUID itemID, Vector3 RayEnd, Vector3 RayStart,
  1644. UUID RayTargetID, byte BypassRayCast, bool RayEndIsIntersection,
  1645. bool RezSelected, bool RemoveItem, UUID fromTaskID)
  1646. {
  1647. IInventoryAccessModule invAccess = RequestModuleInterface<IInventoryAccessModule>();
  1648. if (invAccess != null)
  1649. invAccess.RezObject(
  1650. remoteClient, itemID, RayEnd, RayStart, RayTargetID, BypassRayCast, RayEndIsIntersection,
  1651. RezSelected, RemoveItem, fromTaskID, false);
  1652. }
  1653. /// <summary>
  1654. /// Rez an object into the scene from a prim's inventory.
  1655. /// </summary>
  1656. /// <param name="sourcePart"></param>
  1657. /// <param name="item"></param>
  1658. /// <param name="pos"></param>
  1659. /// <param name="rot"></param>
  1660. /// <param name="vel"></param>
  1661. /// <param name="param"></param>
  1662. /// <returns>The SceneObjectGroup rezzed or null if rez was unsuccessful</returns>
  1663. public virtual SceneObjectGroup RezObject(
  1664. SceneObjectPart sourcePart, TaskInventoryItem item,
  1665. Vector3 pos, Quaternion rot, Vector3 vel, int param)
  1666. {
  1667. // Rez object
  1668. if (item != null)
  1669. {
  1670. UUID ownerID = item.OwnerID;
  1671. AssetBase rezAsset = AssetService.Get(item.AssetID.ToString());
  1672. if (rezAsset != null)
  1673. {
  1674. string xmlData = Utils.BytesToString(rezAsset.Data);
  1675. SceneObjectGroup group = SceneObjectSerializer.FromOriginalXmlFormat(xmlData);
  1676. if (!Permissions.CanRezObject(group.Children.Count, ownerID, pos))
  1677. {
  1678. return null;
  1679. }
  1680. group.ResetIDs();
  1681. AddNewSceneObject(group, true);
  1682. // we set it's position in world.
  1683. group.AbsolutePosition = pos;
  1684. SceneObjectPart rootPart = group.GetChildPart(group.UUID);
  1685. // Since renaming the item in the inventory does not affect the name stored
  1686. // in the serialization, transfer the correct name from the inventory to the
  1687. // object itself before we rez.
  1688. rootPart.Name = item.Name;
  1689. rootPart.Description = item.Description;
  1690. List<SceneObjectPart> partList = new List<SceneObjectPart>(group.Children.Values);
  1691. group.SetGroup(sourcePart.GroupID, null);
  1692. if ((rootPart.OwnerID != item.OwnerID) || (item.CurrentPermissions & 16) != 0)
  1693. {
  1694. if (Permissions.PropagatePermissions())
  1695. {
  1696. foreach (SceneObjectPart part in partList)
  1697. {
  1698. part.EveryoneMask = item.EveryonePermissions;
  1699. part.NextOwnerMask = item.NextPermissions;
  1700. }
  1701. group.ApplyNextOwnerPermissions();
  1702. }
  1703. }
  1704. foreach (SceneObjectPart part in partList)
  1705. {
  1706. if ((part.OwnerID != item.OwnerID) || (item.CurrentPermissions & 16) != 0)
  1707. {
  1708. part.LastOwnerID = part.OwnerID;
  1709. part.OwnerID = item.OwnerID;
  1710. part.Inventory.ChangeInventoryOwner(item.OwnerID);
  1711. }
  1712. part.EveryoneMask = item.EveryonePermissions;
  1713. part.NextOwnerMask = item.NextPermissions;
  1714. }
  1715. rootPart.TrimPermissions();
  1716. if (group.RootPart.Shape.PCode == (byte)PCode.Prim)
  1717. {
  1718. group.ClearPartAttachmentData();
  1719. }
  1720. group.UpdateGroupRotationR(rot);
  1721. //group.ApplyPhysics(m_physicalPrim);
  1722. if (group.RootPart.PhysActor != null && group.RootPart.PhysActor.IsPhysical && vel != Vector3.Zero)
  1723. {
  1724. group.RootPart.ApplyImpulse((vel * group.GetMass()), false);
  1725. group.Velocity = vel;
  1726. rootPart.ScheduleFullUpdate();
  1727. }
  1728. group.CreateScriptInstances(param, true, DefaultScriptEngine, 2);
  1729. rootPart.ScheduleFullUpdate();
  1730. if (!Permissions.BypassPermissions())
  1731. {
  1732. if ((item.CurrentPermissions & (uint)PermissionMask.Copy) == 0)
  1733. sourcePart.Inventory.RemoveInventoryItem(item.ItemID);
  1734. }
  1735. return rootPart.ParentGroup;
  1736. }
  1737. }
  1738. return null;
  1739. }
  1740. public virtual bool returnObjects(SceneObjectGroup[] returnobjects, UUID AgentId)
  1741. {
  1742. foreach (SceneObjectGroup grp in returnobjects)
  1743. {
  1744. AddReturn(grp.OwnerID, grp.Name, grp.AbsolutePosition, "parcel owner return");
  1745. DeRezObject(null, grp.RootPart.LocalId,
  1746. grp.RootPart.GroupID, DeRezAction.Return, UUID.Zero);
  1747. }
  1748. return true;
  1749. }
  1750. public void SetScriptRunning(IClientAPI controllingClient, UUID objectID, UUID itemID, bool running)
  1751. {
  1752. SceneObjectPart part = GetSceneObjectPart(objectID);
  1753. if (part == null)
  1754. return;
  1755. if (running)
  1756. EventManager.TriggerStartScript(part.LocalId, itemID);
  1757. else
  1758. EventManager.TriggerStopScript(part.LocalId, itemID);
  1759. }
  1760. public void GetScriptRunning(IClientAPI controllingClient, UUID objectID, UUID itemID)
  1761. {
  1762. EventManager.TriggerGetScriptRunning(controllingClient, objectID, itemID);
  1763. }
  1764. void ObjectOwner(IClientAPI remoteClient, UUID ownerID, UUID groupID, List<uint> localIDs)
  1765. {
  1766. if (!Permissions.IsGod(remoteClient.AgentId))
  1767. {
  1768. if (ownerID != UUID.Zero)
  1769. return;
  1770. if (!Permissions.CanDeedObject(remoteClient.AgentId, groupID))
  1771. return;
  1772. }
  1773. List<SceneObjectGroup> groups = new List<SceneObjectGroup>();
  1774. foreach (uint localID in localIDs)
  1775. {
  1776. SceneObjectPart part = GetSceneObjectPart(localID);
  1777. if (!groups.Contains(part.ParentGroup))
  1778. groups.Add(part.ParentGroup);
  1779. }
  1780. foreach (SceneObjectGroup sog in groups)
  1781. {
  1782. if (ownerID != UUID.Zero)
  1783. {
  1784. sog.SetOwnerId(ownerID);
  1785. sog.SetGroup(groupID, remoteClient);
  1786. sog.ScheduleGroupForFullUpdate();
  1787. foreach (SceneObjectPart child in sog.Children.Values)
  1788. child.Inventory.ChangeInventoryOwner(ownerID);
  1789. }
  1790. else
  1791. {
  1792. if (!Permissions.CanEditObject(sog.UUID, remoteClient.AgentId))
  1793. continue;
  1794. if (sog.GroupID != groupID)
  1795. continue;
  1796. foreach (SceneObjectPart child in sog.Children.Values)
  1797. {
  1798. child.LastOwnerID = child.OwnerID;
  1799. child.Inventory.ChangeInventoryOwner(groupID);
  1800. }
  1801. sog.SetOwnerId(groupID);
  1802. sog.ApplyNextOwnerPermissions();
  1803. }
  1804. }
  1805. foreach (uint localID in localIDs)
  1806. {
  1807. SceneObjectPart part = GetSceneObjectPart(localID);
  1808. part.GetProperties(remoteClient);
  1809. }
  1810. }
  1811. public void DelinkObjects(List<uint> primIds, IClientAPI client)
  1812. {
  1813. List<SceneObjectPart> parts = new List<SceneObjectPart>();
  1814. foreach (uint localID in primIds)
  1815. {
  1816. SceneObjectPart part = GetSceneObjectPart(localID);
  1817. if (part == null)
  1818. continue;
  1819. if (Permissions.CanDelinkObject(client.AgentId, part.ParentGroup.RootPart.UUID))
  1820. parts.Add(part);
  1821. }
  1822. m_sceneGraph.DelinkObjects(parts);
  1823. }
  1824. public void LinkObjects(IClientAPI client, uint parentPrimId, List<uint> childPrimIds)
  1825. {
  1826. List<UUID> owners = new List<UUID>();
  1827. List<SceneObjectPart> children = new List<SceneObjectPart>();
  1828. SceneObjectPart root = GetSceneObjectPart(parentPrimId);
  1829. if (root == null)
  1830. {
  1831. m_log.DebugFormat("[LINK]: Can't find linkset root prim {0{", parentPrimId);
  1832. return;
  1833. }
  1834. if (!Permissions.CanLinkObject(client.AgentId, root.ParentGroup.RootPart.UUID))
  1835. {
  1836. m_log.DebugFormat("[LINK]: Refusing link. No permissions on root prim");
  1837. return;
  1838. }
  1839. foreach (uint localID in childPrimIds)
  1840. {
  1841. SceneObjectPart part = GetSceneObjectPart(localID);
  1842. if (part == null)
  1843. continue;
  1844. if (!owners.Contains(part.OwnerID))
  1845. owners.Add(part.OwnerID);
  1846. if (Permissions.CanLinkObject(client.AgentId, part.ParentGroup.RootPart.UUID))
  1847. children.Add(part);
  1848. }
  1849. // Must be all one owner
  1850. //
  1851. if (owners.Count > 1)
  1852. {
  1853. m_log.DebugFormat("[LINK]: Refusing link. Too many owners");
  1854. return;
  1855. }
  1856. if (children.Count == 0)
  1857. {
  1858. m_log.DebugFormat("[LINK]: Refusing link. No permissions to link any of the children");
  1859. return;
  1860. }
  1861. m_sceneGraph.LinkObjects(root, children);
  1862. }
  1863. }
  1864. }