Scene.Inventory.cs 130 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025
  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.Threading;
  33. using System.Timers;
  34. using System.Xml;
  35. using OpenMetaverse;
  36. using OpenMetaverse.Packets;
  37. using log4net;
  38. using OpenSim.Framework;
  39. using OpenSim.Framework.Serialization.External;
  40. using OpenSim.Region.Framework;
  41. using OpenSim.Region.Framework.Interfaces;
  42. using OpenSim.Region.Framework.Scenes.Serialization;
  43. using OpenSim.Services.Interfaces;
  44. using PermissionMask = OpenSim.Framework.PermissionMask;
  45. namespace OpenSim.Region.Framework.Scenes
  46. {
  47. public partial class Scene
  48. {
  49. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  50. //private static readonly string LogHeader = "[SCENE INVENTORY]";
  51. /// <summary>
  52. /// Allows asynchronous derezzing of objects from the scene into a client's inventory.
  53. /// </summary>
  54. protected AsyncSceneObjectGroupDeleter m_asyncSceneObjectDeleter;
  55. /// <summary>
  56. /// Allows inventory details to be sent to clients asynchronously
  57. /// </summary>
  58. protected AsyncInventorySender m_asyncInventorySender;
  59. /// <summary>
  60. /// Creates all the scripts in the scene which should be started.
  61. /// </summary>
  62. /// <returns>
  63. /// Number of scripts that were valid for starting. This does not guarantee that all these scripts
  64. /// were actually started, but just that the start could be attempt (e.g. the asset data for the script could be found)
  65. /// </returns>
  66. public int CreateScriptInstances()
  67. {
  68. m_log.InfoFormat("[SCENE]: Initializing script instances in {0}", RegionInfo.RegionName);
  69. int scriptsValidForStarting = 0;
  70. EntityBase[] entities = Entities.GetEntities();
  71. foreach (EntityBase group in entities)
  72. {
  73. if (group is SceneObjectGroup)
  74. {
  75. scriptsValidForStarting += ((SceneObjectGroup) group).CreateScriptInstances(0, false, DefaultScriptEngine, 0);
  76. ((SceneObjectGroup) group).ResumeScripts();
  77. }
  78. }
  79. m_log.InfoFormat(
  80. "[SCENE]: Initialized {0} script instances in {1}",
  81. scriptsValidForStarting, RegionInfo.RegionName);
  82. return scriptsValidForStarting;
  83. }
  84. /// <summary>
  85. /// Lets the script engines start processing scripts.
  86. /// </summary>
  87. public void StartScripts()
  88. {
  89. // m_log.InfoFormat("[SCENE]: Starting scripts in {0}, please wait.", RegionInfo.RegionName);
  90. IScriptModule[] engines = RequestModuleInterfaces<IScriptModule>();
  91. foreach (IScriptModule engine in engines)
  92. engine.StartProcessing();
  93. }
  94. public void AddUploadedInventoryItem(UUID agentID, InventoryItemBase item, uint cost)
  95. {
  96. IMoneyModule money = RequestModuleInterface<IMoneyModule>();
  97. if (money != null)
  98. {
  99. money.ApplyUploadCharge(agentID, (int)cost, "Asset upload");
  100. }
  101. AddInventoryItem(item);
  102. }
  103. public bool AddInventoryItemReturned(UUID AgentId, InventoryItemBase item)
  104. {
  105. if (AddInventoryItem(item))
  106. return true;
  107. else
  108. {
  109. m_log.WarnFormat(
  110. "[AGENT INVENTORY]: Unable to add item {1} to agent {2} inventory", item.Name, AgentId);
  111. return false;
  112. }
  113. }
  114. public bool AddInventoryItem(InventoryItemBase item)
  115. {
  116. return AddInventoryItem(item, true);
  117. }
  118. /// <summary>
  119. /// Add the given inventory item to a user's inventory.
  120. /// </summary>
  121. /// <param name="item"></param>
  122. public bool AddInventoryItem(InventoryItemBase item, bool trigger)
  123. {
  124. if (item.Folder != UUID.Zero && InventoryService.AddItem(item))
  125. {
  126. int userlevel = 0;
  127. if (Permissions.IsGod(item.Owner))
  128. {
  129. userlevel = 1;
  130. }
  131. if (trigger)
  132. EventManager.TriggerOnNewInventoryItemUploadComplete(item, userlevel);
  133. return true;
  134. }
  135. // OK so either the viewer didn't send a folderID or AddItem failed
  136. UUID originalFolder = item.Folder;
  137. InventoryFolderBase f = null;
  138. if (Enum.IsDefined(typeof(FolderType), (sbyte)item.AssetType))
  139. f = InventoryService.GetFolderForType(item.Owner, (FolderType)item.AssetType);
  140. if (f != null)
  141. {
  142. m_log.DebugFormat(
  143. "[AGENT INVENTORY]: Found folder {0} type {1} for item {2}",
  144. f.Name, (AssetType)f.Type, item.Name);
  145. item.Folder = f.ID;
  146. }
  147. else
  148. {
  149. f = InventoryService.GetRootFolder(item.Owner);
  150. if (f != null)
  151. {
  152. item.Folder = f.ID;
  153. }
  154. else
  155. {
  156. m_log.WarnFormat(
  157. "[AGENT INVENTORY]: Could not find root folder for {0} when trying to add item {1} with no parent folder specified",
  158. item.Owner, item.Name);
  159. return false;
  160. }
  161. }
  162. if (InventoryService.AddItem(item))
  163. {
  164. int userlevel = 0;
  165. if (Permissions.IsGod(item.Owner))
  166. {
  167. userlevel = 1;
  168. }
  169. if (trigger)
  170. EventManager.TriggerOnNewInventoryItemUploadComplete(item, userlevel);
  171. if (originalFolder != UUID.Zero)
  172. {
  173. // Tell the viewer that the item didn't go there
  174. ChangePlacement(item, f);
  175. }
  176. return true;
  177. }
  178. else
  179. {
  180. m_log.WarnFormat(
  181. "[AGENT INVENTORY]: Agent {0} could not add item {1} {2}",
  182. item.Owner, item.Name, item.ID);
  183. return false;
  184. }
  185. }
  186. private void ChangePlacement(InventoryItemBase item, InventoryFolderBase f)
  187. {
  188. ScenePresence sp = GetScenePresence(item.Owner);
  189. if (sp != null && sp.ControllingClient != null && sp.ControllingClient.IsActive)
  190. {
  191. IClientAPI cli = sp.ControllingClient;
  192. InventoryFolderBase parent = InventoryService.GetFolder(f.Owner, f.ParentID);
  193. cli.SendRemoveInventoryItems(new UUID[] { item.ID });
  194. cli.SendBulkUpdateInventory(new InventoryFolderBase[0], new InventoryItemBase[] { item });
  195. string message = "The item was placed in folder " + f.Name;
  196. if (parent != null)
  197. message += " under " + parent.Name;
  198. cli.SendAgentAlertMessage(message, false);
  199. }
  200. }
  201. /// <summary>
  202. /// Add the given inventory item to a user's inventory.
  203. /// </summary>
  204. /// <param name="AgentID">
  205. /// A <see cref="UUID"/>
  206. /// </param>
  207. /// <param name="item">
  208. /// A <see cref="InventoryItemBase"/>
  209. /// </param>
  210. [Obsolete("Use AddInventoryItem(InventoryItemBase item) instead. This was deprecated in OpenSim 0.7.1")]
  211. public void AddInventoryItem(UUID AgentID, InventoryItemBase item)
  212. {
  213. AddInventoryItem(item);
  214. }
  215. /// <summary>
  216. /// Add an inventory item to an avatar's inventory.
  217. /// </summary>
  218. /// <param name="remoteClient">The remote client controlling the avatar</param>
  219. /// <param name="item">The item. This structure contains all the item metadata, including the folder
  220. /// in which the item is to be placed.</param>
  221. public void AddInventoryItem(IClientAPI remoteClient, InventoryItemBase item)
  222. {
  223. AddInventoryItem(item);
  224. remoteClient.SendInventoryItemCreateUpdate(item, 0);
  225. }
  226. /// <summary>
  227. /// <see>CapsUpdatedInventoryItemAsset(IClientAPI, UUID, byte[])</see>
  228. /// </summary>
  229. public UUID CapsUpdateItemAsset(UUID avatarId, UUID itemID, UUID objectID, byte[] data)
  230. {
  231. if (!TryGetScenePresence(avatarId, out ScenePresence avatar))
  232. {
  233. m_log.ErrorFormat("[CapsUpdateItemAsset]: Avatar {0} cannot be found to update item asset", avatarId);
  234. return UUID.Zero;
  235. }
  236. if (objectID == UUID.Zero)
  237. {
  238. IInventoryAccessModule invAccess = RequestModuleInterface<IInventoryAccessModule>();
  239. if (invAccess != null)
  240. return invAccess.CapsUpdateInventoryItemAsset(avatar.ControllingClient, itemID, data);
  241. else
  242. return UUID.Zero;
  243. }
  244. SceneObjectPart sop = GetSceneObjectPart(objectID);
  245. if(sop == null || sop.ParentGroup.IsDeleted)
  246. {
  247. m_log.ErrorFormat("[CapsUpdateItemAsset]: Object {0} cannot be found to update item asset", objectID);
  248. return UUID.Zero;
  249. }
  250. TaskInventoryItem item = sop.Inventory.GetInventoryItem(itemID);
  251. if (item == null)
  252. {
  253. m_log.ErrorFormat("[CapsUpdateItemAsset]: Could not find item {0} for asset update", itemID);
  254. return UUID.Zero;
  255. }
  256. if (item.OwnerID != avatarId)
  257. return UUID.Zero;
  258. InventoryType itemType = (InventoryType)item.InvType;
  259. switch (itemType)
  260. {
  261. case InventoryType.Notecard:
  262. {
  263. if (!Permissions.CanEditNotecard(itemID, objectID, avatarId))
  264. {
  265. avatar.ControllingClient.SendAgentAlertMessage("Insufficient permissions to edit notecard", false);
  266. return UUID.Zero;
  267. }
  268. avatar.ControllingClient.SendAlertMessage("Notecard updated");
  269. break;
  270. }
  271. case (InventoryType)CustomInventoryType.AnimationSet:
  272. {
  273. AnimationSet animSet = new AnimationSet(data);
  274. uint res = animSet.Validate(x => {
  275. const int required = (int)(PermissionMask.Transfer | PermissionMask.Copy);
  276. int perms = InventoryService.GetAssetPermissions(avatarId, x);
  277. // enforce previus perm rule
  278. if ((perms & required) != required)
  279. return 0;
  280. return (uint)perms;
  281. });
  282. if (res == 0)
  283. {
  284. avatar.ControllingClient.SendAgentAlertMessage("Not enought permissions on asset(s) referenced by animation set '{0}', update failed", false);
  285. return UUID.Zero;
  286. }
  287. break;
  288. }
  289. case InventoryType.Gesture:
  290. {
  291. if ((item.CurrentPermissions & (uint)PermissionMask.Modify) == 0)
  292. {
  293. avatar.ControllingClient.SendAgentAlertMessage("Insufficient permissions to edit gesture", false);
  294. return UUID.Zero;
  295. }
  296. avatar.ControllingClient.SendAlertMessage("Gesture updated");
  297. break;
  298. }
  299. case InventoryType.Settings:
  300. {
  301. if ((item.CurrentPermissions & (uint)PermissionMask.Modify) == 0)
  302. {
  303. avatar.ControllingClient.SendAgentAlertMessage("Insufficient permissions to edit setting", false);
  304. return UUID.Zero;
  305. }
  306. avatar.ControllingClient.SendAlertMessage("Setting updated");
  307. break;
  308. }
  309. }
  310. AssetBase asset = CreateAsset(item.Name, item.Description, (sbyte)item.Type, data, avatarId);
  311. item.AssetID = asset.FullID;
  312. AssetService.Store(asset);
  313. sop.Inventory.UpdateInventoryItem(item);
  314. // remoteClient.SendInventoryItemCreateUpdate(item);
  315. return asset.FullID;
  316. }
  317. /// <summary>
  318. /// Capability originating call to update the asset of a script in a prim's (task's) inventory
  319. /// </summary>
  320. /// <param name="remoteClient"></param>
  321. /// <param name="itemID"></param>
  322. /// <param name="primID">The prim which contains the item to update</param>
  323. /// <param name="isScriptRunning">Indicates whether the script to update is currently running</param>
  324. /// <param name="data"></param>
  325. public ArrayList CapsUpdateTaskInventoryScriptAsset(IClientAPI remoteClient, UUID itemId,
  326. UUID primId, bool isScriptRunning, byte[] data)
  327. {
  328. if (!Permissions.CanEditScript(itemId, primId, remoteClient.AgentId))
  329. {
  330. remoteClient.SendAgentAlertMessage("Insufficient permissions to edit script", false);
  331. return new ArrayList();
  332. }
  333. // Retrieve group
  334. SceneObjectPart part = GetSceneObjectPart(primId);
  335. if (part == null)
  336. return new ArrayList();
  337. SceneObjectGroup group = part.ParentGroup;
  338. // Retrieve item
  339. TaskInventoryItem item = group.GetInventoryItem(part.LocalId, itemId);
  340. if (null == item)
  341. {
  342. m_log.ErrorFormat(
  343. "[PRIM INVENTORY]: Tried to retrieve item ID {0} from prim {1}, {2} for caps script update "
  344. + " but the item does not exist in this inventory",
  345. itemId, part.Name, part.UUID);
  346. return new ArrayList();
  347. }
  348. AssetBase asset = CreateAsset(item.Name, item.Description, (sbyte)AssetType.LSLText, data, remoteClient.AgentId);
  349. AssetService.Store(asset);
  350. // m_log.DebugFormat(
  351. // "[PRIM INVENTORY]: Stored asset {0} when updating item {1} in prim {2} for {3}",
  352. // asset.ID, item.Name, part.Name, remoteClient.Name);
  353. if (isScriptRunning)
  354. {
  355. part.Inventory.RemoveScriptInstance(item.ItemID, false);
  356. }
  357. // Update item with new asset
  358. item.AssetID = asset.FullID;
  359. group.UpdateInventoryItem(item);
  360. group.InvalidateEffectivePerms();
  361. part.SendPropertiesToClient(remoteClient);
  362. // Trigger rerunning of script (use TriggerRezScript event, see RezScript)
  363. ArrayList errors = new ArrayList();
  364. if (isScriptRunning)
  365. {
  366. // Needs to determine which engine was running it and use that
  367. //
  368. errors = part.Inventory.CreateScriptInstanceEr(item.ItemID, 0, false, DefaultScriptEngine, 1);
  369. }
  370. // Tell anyone managing scripts that a script has been reloaded/changed
  371. EventManager.TriggerUpdateScript(remoteClient.AgentId, itemId, primId, isScriptRunning, item.AssetID);
  372. part.ParentGroup.ResumeScripts();
  373. return errors;
  374. }
  375. /// <summary>
  376. /// <see>CapsUpdateTaskInventoryScriptAsset(IClientAPI, UUID, UUID, bool, byte[])</see>
  377. /// </summary>
  378. public ArrayList CapsUpdateTaskInventoryScriptAsset(UUID avatarId, UUID itemId,
  379. UUID primId, bool isScriptRunning, byte[] data)
  380. {
  381. ScenePresence avatar;
  382. if (TryGetScenePresence(avatarId, out avatar))
  383. {
  384. return CapsUpdateTaskInventoryScriptAsset(
  385. avatar.ControllingClient, itemId, primId, isScriptRunning, data);
  386. }
  387. else
  388. {
  389. m_log.ErrorFormat(
  390. "[PRIM INVENTORY]: " +
  391. "Avatar {0} cannot be found to update its prim item asset",
  392. avatarId);
  393. return new ArrayList();
  394. }
  395. }
  396. /// <summary>
  397. /// Update an item which is either already in the client's inventory or is within
  398. /// a transaction
  399. /// </summary>
  400. /// <param name="remoteClient"></param>
  401. /// <param name="transactionID">The transaction ID. If this is UUID.Zero we will
  402. /// assume that we are not in a transaction</param>
  403. /// <param name="itemID">The ID of the updated item</param>
  404. /// <param name="name">The name of the updated item</param>
  405. /// <param name="description">The description of the updated item</param>
  406. /// <param name="nextOwnerMask">The permissions of the updated item</param>
  407. /* public void UpdateInventoryItemAsset(IClientAPI remoteClient, UUID transactionID,
  408. UUID itemID, string name, string description,
  409. uint nextOwnerMask)*/
  410. public void UpdateInventoryItemAsset(IClientAPI remoteClient, UUID transactionID,
  411. UUID itemID, InventoryItemBase itemUpd)
  412. {
  413. // m_log.DebugFormat(
  414. // "[USER INVENTORY]: Updating asset for item {0} {1}, transaction ID {2} for {3}",
  415. // itemID, itemUpd.Name, transactionID, remoteClient.Name);
  416. // This one will let people set next perms on items in agent
  417. // inventory. Rut-Roh. Whatever. Make this secure. Yeah.
  418. //
  419. // Passing something to another avatar or a an object will already
  420. InventoryItemBase item = InventoryService.GetItem(remoteClient.AgentId, itemID);
  421. if (item != null)
  422. {
  423. if (item.Owner != remoteClient.AgentId)
  424. return;
  425. item.Flags = (item.Flags & ~(uint)255) | (itemUpd.Flags & (uint)255);
  426. item.Name = itemUpd.Name;
  427. item.Description = itemUpd.Description;
  428. // m_log.DebugFormat(
  429. // "[USER INVENTORY]: itemUpd {0} {1} {2} {3}, item {4} {5} {6} {7}",
  430. // itemUpd.NextPermissions, itemUpd.GroupPermissions, itemUpd.EveryOnePermissions, item.Flags,
  431. // item.NextPermissions, item.GroupPermissions, item.EveryOnePermissions, item.CurrentPermissions);
  432. bool sendUpdate = false;
  433. if (itemUpd.NextPermissions != 0) // Use this to determine validity. Can never be 0 if valid
  434. {
  435. // Create a set of base permissions that will not include export if the user
  436. // is not allowed to change the export flag.
  437. bool denyExportChange = false;
  438. // m_log.DebugFormat("[XXX]: B: {0} O: {1} E: {2}", itemUpd.BasePermissions, itemUpd.CurrentPermissions, itemUpd.EveryOnePermissions);
  439. // If the user is not the creator or doesn't have "E" in both "B" and "O", deny setting export
  440. if ((item.BasePermissions & (uint)(PermissionMask.All | PermissionMask.Export)) != (uint)(PermissionMask.All | PermissionMask.Export) || (item.CurrentPermissions & (uint)PermissionMask.Export) == 0 || item.CreatorIdAsUuid != item.Owner)
  441. denyExportChange = true;
  442. // m_log.DebugFormat("[XXX]: Deny Export Update {0}", denyExportChange);
  443. // If it is already set, force it set and also force full perm
  444. // else prevent setting it. It can and should never be set unless
  445. // set in base, so the condition above is valid
  446. if (denyExportChange)
  447. {
  448. // If we are not allowed to change it, then force it to the
  449. // original item's setting and if it was on, also force full perm
  450. if ((item.EveryOnePermissions & (uint)PermissionMask.Export) != 0)
  451. {
  452. itemUpd.NextPermissions = (uint)(PermissionMask.All);
  453. itemUpd.EveryOnePermissions |= (uint)PermissionMask.Export;
  454. }
  455. else
  456. {
  457. itemUpd.EveryOnePermissions &= ~(uint)PermissionMask.Export;
  458. }
  459. }
  460. else
  461. {
  462. // If the new state is exportable, force full perm
  463. if ((itemUpd.EveryOnePermissions & (uint)PermissionMask.Export) != 0)
  464. {
  465. // m_log.DebugFormat("[XXX]: Force full perm");
  466. itemUpd.NextPermissions = (uint)(PermissionMask.All);
  467. }
  468. }
  469. if (item.NextPermissions != (itemUpd.NextPermissions & item.BasePermissions))
  470. item.Flags |= (uint)InventoryItemFlags.ObjectOverwriteNextOwner;
  471. item.NextPermissions = itemUpd.NextPermissions & item.BasePermissions;
  472. if (item.EveryOnePermissions != (itemUpd.EveryOnePermissions & item.BasePermissions))
  473. item.Flags |= (uint)InventoryItemFlags.ObjectOverwriteEveryone;
  474. item.EveryOnePermissions = itemUpd.EveryOnePermissions & item.BasePermissions;
  475. if (item.GroupPermissions != (itemUpd.GroupPermissions & item.BasePermissions))
  476. item.Flags |= (uint)InventoryItemFlags.ObjectOverwriteGroup;
  477. item.GroupPermissions = itemUpd.GroupPermissions & item.BasePermissions;
  478. item.GroupID = itemUpd.GroupID;
  479. item.GroupOwned = itemUpd.GroupOwned;
  480. item.CreationDate = itemUpd.CreationDate;
  481. // The client sends zero if its newly created?
  482. if (itemUpd.CreationDate == 0)
  483. item.CreationDate = Util.UnixTimeSinceEpoch();
  484. else
  485. item.CreationDate = itemUpd.CreationDate;
  486. // TODO: Check if folder changed and move item
  487. //item.NextPermissions = itemUpd.Folder;
  488. item.InvType = itemUpd.InvType;
  489. if (item.SalePrice != itemUpd.SalePrice ||
  490. item.SaleType != itemUpd.SaleType)
  491. item.Flags |= (uint)InventoryItemFlags.ObjectSlamSale;
  492. item.SalePrice = itemUpd.SalePrice;
  493. item.SaleType = itemUpd.SaleType;
  494. if (item.InvType == (int)InventoryType.Wearable && (item.Flags & 0xf) == 0 && (itemUpd.Flags & 0xf) != 0)
  495. {
  496. item.Flags = (uint)(item.Flags & 0xfffffff0) | (itemUpd.Flags & 0xf);
  497. sendUpdate = true;
  498. }
  499. InventoryService.UpdateItem(item);
  500. }
  501. if (UUID.Zero != transactionID)
  502. {
  503. if (AgentTransactionsModule != null)
  504. {
  505. AgentTransactionsModule.HandleItemUpdateFromTransaction(remoteClient, transactionID, item);
  506. }
  507. }
  508. else
  509. {
  510. // This MAY be problematic, if it is, another solution
  511. // needs to be found. If inventory item flags are updated
  512. // the viewer's notion of the item needs to be refreshed.
  513. //
  514. // In other situations we cannot send out a bulk update here, since this will cause editing of clothing to start
  515. // failing frequently. Possibly this is a race with a separate transaction that uploads the asset.
  516. if (sendUpdate)
  517. remoteClient.SendBulkUpdateInventory(item);
  518. }
  519. }
  520. else
  521. {
  522. m_log.ErrorFormat(
  523. "[AGENTINVENTORY]: Item id {0} not found for an inventory item update for {1}.",
  524. itemID, remoteClient.Name);
  525. }
  526. }
  527. /// <summary>
  528. /// Give an inventory item from one user to another
  529. /// </summary>
  530. /// <param name="recipientClient"></param>
  531. /// <param name="senderId">ID of the sender of the item</param>
  532. /// <param name="itemId"></param>
  533. public virtual void GiveInventoryItem(IClientAPI recipientClient, UUID senderId, UUID itemId, out string message)
  534. {
  535. InventoryItemBase itemCopy = GiveInventoryItem(recipientClient.AgentId, senderId, itemId, out message);
  536. if (itemCopy != null)
  537. recipientClient.SendBulkUpdateInventory(itemCopy);
  538. }
  539. /// <summary>
  540. /// Give an inventory item from one user to another
  541. /// </summary>
  542. /// <param name="recipient"></param>
  543. /// <param name="senderId">ID of the sender of the item</param>
  544. /// <param name="itemId"></param>
  545. /// <returns>The inventory item copy given, null if the give was unsuccessful</returns>
  546. public virtual InventoryItemBase GiveInventoryItem(UUID recipient, UUID senderId, UUID itemId, out string message)
  547. {
  548. return GiveInventoryItem(recipient, senderId, itemId, UUID.Zero, out message);
  549. }
  550. /// <summary>
  551. /// Give an inventory item from one user to another
  552. /// </summary>
  553. /// <param name="recipient"></param>
  554. /// <param name="senderId">ID of the sender of the item</param>
  555. /// <param name="itemId"></param>
  556. /// <param name="recipientFolderId">
  557. /// The id of the folder in which the copy item should go. If UUID.Zero then the item is placed in the most
  558. /// appropriate default folder.
  559. /// </param>
  560. /// <returns>
  561. /// The inventory item copy given, null if the give was unsuccessful
  562. /// </returns>
  563. public virtual InventoryItemBase GiveInventoryItem(
  564. UUID recipient, UUID senderId, UUID itemId, UUID recipientFolderId, out string message)
  565. {
  566. //Console.WriteLine("Scene.Inventory.cs: GiveInventoryItem");
  567. if (!Permissions.CanTransferUserInventory(itemId, senderId, recipient))
  568. {
  569. message = "Not allowed to transfer this item.";
  570. return null;
  571. }
  572. InventoryItemBase item = InventoryService.GetItem(senderId, itemId);
  573. if (item == null)
  574. {
  575. m_log.WarnFormat(
  576. "[AGENT INVENTORY]: Failed to find item {0} sent by {1} to {2}", itemId, senderId, recipient);
  577. message = string.Format("Item not found: {0}.", itemId);
  578. return null;
  579. }
  580. if (item.AssetType == (int)AssetType.Link || item.AssetType == (int)AssetType.LinkFolder)
  581. {
  582. message = "inventory links not supported.";
  583. return null;
  584. }
  585. if (item.Owner != senderId)
  586. {
  587. m_log.WarnFormat(
  588. "[AGENT INVENTORY]: Attempt to send item {0} {1} to {2} failed because sender {3} did not match item owner {4}",
  589. item.Name, item.ID, recipient, senderId, item.Owner);
  590. message = "Sender did not match item owner.";
  591. return null;
  592. }
  593. IUserManagement uman = RequestModuleInterface<IUserManagement>();
  594. if (uman != null)
  595. uman.AddUser(item.CreatorIdAsUuid, item.CreatorData);
  596. if (!Permissions.BypassPermissions())
  597. {
  598. if ((item.CurrentPermissions & (uint)PermissionMask.Transfer) == 0)
  599. {
  600. message = "Item doesn't have the Transfer permission.";
  601. return null;
  602. }
  603. }
  604. // Insert a copy of the item into the recipient
  605. InventoryItemBase itemCopy = new InventoryItemBase();
  606. itemCopy.Owner = recipient;
  607. itemCopy.CreatorId = item.CreatorId;
  608. itemCopy.CreatorData = item.CreatorData;
  609. itemCopy.ID = UUID.Random();
  610. itemCopy.AssetID = item.AssetID;
  611. itemCopy.Description = item.Description;
  612. itemCopy.Name = item.Name;
  613. itemCopy.AssetType = item.AssetType;
  614. itemCopy.InvType = item.InvType;
  615. itemCopy.Folder = recipientFolderId;
  616. itemCopy.Flags = item.Flags;
  617. if (Permissions.PropagatePermissions() && recipient != senderId)
  618. {
  619. // Trying to do this right this time. This is evil. If
  620. // you believe in Good, go elsewhere. Vampires and other
  621. // evil creatores only beyond this point. You have been
  622. // warned.
  623. // We're going to mask a lot of things by the next perms
  624. // Tweak the next perms to be nicer to our data
  625. //
  626. // In this mask, all the bits we do NOT want to mess
  627. // with are set. These are:
  628. //
  629. // Transfer
  630. // Copy
  631. // Modify
  632. uint permsMask = ~ ((uint)PermissionMask.Copy |
  633. (uint)PermissionMask.Transfer |
  634. (uint)PermissionMask.Modify |
  635. (uint)PermissionMask.Export);
  636. // Now, reduce the next perms to the mask bits
  637. // relevant to the operation
  638. uint nextPerms = permsMask | (item.NextPermissions &
  639. ((uint)PermissionMask.Copy |
  640. (uint)PermissionMask.Transfer |
  641. (uint)PermissionMask.Modify));
  642. // nextPerms now has all bits set, except for the actual
  643. // next permission bits.
  644. // This checks for no mod, no copy, no trans.
  645. // This indicates an error or messed up item. Do it like
  646. // SL and assume trans
  647. if (nextPerms == permsMask)
  648. nextPerms |= (uint)PermissionMask.Transfer;
  649. // Inventory owner perms are the logical AND of the
  650. // folded perms and the root prim perms, however, if
  651. // the root prim is mod, the inventory perms will be
  652. // mod. This happens on "take" and is of little concern
  653. // here, save for preventing escalation
  654. // This hack ensures that items previously permalocked
  655. // get unlocked when they're passed or rezzed
  656. uint basePerms = item.BasePermissions |
  657. (uint)PermissionMask.Move;
  658. uint ownerPerms = item.CurrentPermissions;
  659. // These will be applied to the root prim at next rez.
  660. // The legacy slam bit (bit 3) and folded permission (bits 0-2)
  661. // are preserved due to the above mangling
  662. // ownerPerms &= nextPerms;
  663. // Mask the base permissions. This is a conservative
  664. // approach altering only the three main perms
  665. // basePerms &= nextPerms;
  666. // Mask out the folded portion of the base mask.
  667. // While the owner mask carries the actual folded
  668. // permissions, the base mask carries the original
  669. // base mask, before masking with the folded perms.
  670. // We need this later for rezzing.
  671. // basePerms &= ~(uint)PermissionMask.FoldedMask;
  672. // basePerms |= ((basePerms >> 13) & 7) | (((basePerms & (uint)PermissionMask.Export) != 0) ? (uint)PermissionMask.FoldedExport : 0);
  673. // If this is an object, root prim perms may be more
  674. // permissive than folded perms. Use folded perms as
  675. // a mask
  676. uint foldedPerms = (item.CurrentPermissions & (uint)PermissionMask.FoldedMask) << (int)PermissionMask.FoldingShift;
  677. if (foldedPerms != 0 && item.InvType == (int)InventoryType.Object)
  678. {
  679. foldedPerms |= permsMask;
  680. bool isRootMod = (item.CurrentPermissions &
  681. (uint)PermissionMask.Modify) != 0 ?
  682. true : false;
  683. // Mask the owner perms to the folded perms
  684. // Note that this is only to satisfy the viewer.
  685. // The effect of this will be reversed on rez.
  686. ownerPerms &= foldedPerms;
  687. basePerms &= foldedPerms;
  688. // If the root was mod, let the mask reflect that
  689. // We also need to adjust the base here, because
  690. // we should be able to edit in-inventory perms
  691. // for the root prim, if it's mod.
  692. if (isRootMod)
  693. {
  694. ownerPerms |= (uint)PermissionMask.Modify;
  695. basePerms |= (uint)PermissionMask.Modify;
  696. }
  697. }
  698. // move here so nextperms are mandatory
  699. ownerPerms &= nextPerms;
  700. basePerms &= nextPerms;
  701. basePerms &= ~(uint)PermissionMask.FoldedMask;
  702. basePerms |= ((basePerms >> 13) & 7) | (((basePerms & (uint)PermissionMask.Export) != 0) ? (uint)PermissionMask.FoldedExport : 0);
  703. // Assign to the actual item. Make sure the slam bit is
  704. // set, if it wasn't set before.
  705. itemCopy.BasePermissions = basePerms;
  706. itemCopy.CurrentPermissions = ownerPerms;
  707. itemCopy.Flags |= (uint)InventoryItemFlags.ObjectSlamPerm;
  708. // Need to clear the other inventory slam options.
  709. // That is so we can handle the case where the recipient
  710. // changes the bits in inventory before rezzing
  711. itemCopy.Flags &= ~(uint)(InventoryItemFlags.ObjectOverwriteBase | InventoryItemFlags.ObjectOverwriteOwner | InventoryItemFlags.ObjectOverwriteGroup | InventoryItemFlags.ObjectOverwriteEveryone | InventoryItemFlags.ObjectOverwriteNextOwner);
  712. itemCopy.NextPermissions = item.NextPermissions;
  713. // This preserves "everyone can move"
  714. itemCopy.EveryOnePermissions = item.EveryOnePermissions &
  715. nextPerms;
  716. // Intentionally killing "share with group" here, as
  717. // the recipient will not have the group this is
  718. // set to
  719. itemCopy.GroupPermissions = 0;
  720. }
  721. else
  722. {
  723. itemCopy.CurrentPermissions = item.CurrentPermissions;
  724. itemCopy.NextPermissions = item.NextPermissions;
  725. itemCopy.EveryOnePermissions = item.EveryOnePermissions & item.NextPermissions;
  726. itemCopy.GroupPermissions = item.GroupPermissions & item.NextPermissions;
  727. itemCopy.BasePermissions = item.BasePermissions;
  728. }
  729. if (itemCopy.Folder == UUID.Zero)
  730. {
  731. InventoryFolderBase folder = null;
  732. if (Enum.IsDefined(typeof(FolderType), (sbyte)item.AssetType))
  733. folder = InventoryService.GetFolderForType(recipient, (FolderType)itemCopy.AssetType);
  734. if (folder != null)
  735. {
  736. itemCopy.Folder = folder.ID;
  737. }
  738. else
  739. {
  740. InventoryFolderBase root = InventoryService.GetRootFolder(recipient);
  741. if (root != null)
  742. {
  743. itemCopy.Folder = root.ID;
  744. }
  745. else
  746. {
  747. message = "Can't find a folder to add the item to.";
  748. return null;
  749. }
  750. }
  751. }
  752. itemCopy.GroupID = UUID.Zero;
  753. itemCopy.GroupOwned = false;
  754. itemCopy.SalePrice = 0; //item.SalePrice;
  755. itemCopy.SaleType = 0; //item.SaleType;
  756. IInventoryAccessModule invAccess = RequestModuleInterface<IInventoryAccessModule>();
  757. if (invAccess != null)
  758. invAccess.TransferInventoryAssets(itemCopy, senderId, recipient);
  759. AddInventoryItem(itemCopy, false);
  760. if (!Permissions.BypassPermissions())
  761. {
  762. if ((item.CurrentPermissions & (uint)PermissionMask.Copy) == 0)
  763. {
  764. List<UUID> items = new List<UUID>();
  765. items.Add(itemId);
  766. InventoryService.DeleteItems(senderId, items);
  767. }
  768. }
  769. message = null;
  770. return itemCopy;
  771. }
  772. private readonly HashSet<short> denyGiveFolderTypes = new HashSet<short>()
  773. {
  774. (short)FolderType.Trash,
  775. (short)FolderType.LostAndFound,
  776. (short)FolderType.CurrentOutfit,
  777. (short)FolderType.Inbox,
  778. (short)FolderType.Outbox,
  779. (short)FolderType.Suitcase,
  780. (short)FolderType.Root
  781. };
  782. /// <summary>
  783. /// Give an entire inventory folder from one user to another. The entire contents (including all descendent
  784. /// folders) is given.
  785. /// </summary>
  786. /// <param name="recipientId"></param>
  787. /// <param name="senderId">ID of the sender of the item</param>
  788. /// <param name="folderId"></param>
  789. /// <param name="recipientParentFolderId">
  790. /// The id of the receipient folder in which the send folder should be placed. If UUID.Zero then the
  791. /// recipient folder is the root folder
  792. /// </param>
  793. /// <returns>
  794. /// The inventory folder copy given, null if the copy was unsuccessful
  795. /// </returns>
  796. public virtual InventoryFolderBase GiveInventoryFolder(IClientAPI client,
  797. UUID recipientId, UUID senderId, UUID folderId, UUID recipientParentFolderId)
  798. {
  799. //// Retrieve the folder from the sender
  800. InventoryFolderBase folder = InventoryService.GetFolder(senderId, folderId);
  801. if (folder == null)
  802. {
  803. m_log.ErrorFormat("[AGENT INVENTORY]: Could not find inventory folder {0} to give", folderId);
  804. return null;
  805. }
  806. if (denyGiveFolderTypes.Contains(folder.Type))
  807. {
  808. m_log.ErrorFormat("[AGENT INVENTORY]: can not give inventory folder {0}", folderId);
  809. return null;
  810. }
  811. if (recipientParentFolderId == UUID.Zero)
  812. {
  813. InventoryFolderBase recipientRootFolder = InventoryService.GetRootFolder(recipientId);
  814. if (recipientRootFolder != null)
  815. recipientParentFolderId = recipientRootFolder.ID;
  816. else
  817. {
  818. m_log.WarnFormat("[AGENT INVENTORY]: Unable to find root folder for receiving agent");
  819. return null;
  820. }
  821. }
  822. UUID newFolderId = UUID.Random();
  823. InventoryFolderBase newFolder = new InventoryFolderBase(
  824. newFolderId, folder.Name, recipientId, folder.Type, recipientParentFolderId, folder.Version);
  825. InventoryService.AddFolder(newFolder);
  826. // Give all the subfolders
  827. InventoryCollection contents = InventoryService.GetFolderContent(senderId, folderId);
  828. // Give all the items (first, this is recursive call)
  829. foreach (InventoryItemBase item in contents.Items)
  830. {
  831. string message;
  832. if (GiveInventoryItem(recipientId, senderId, item.ID, newFolder.ID, out message) == null)
  833. {
  834. if (client != null)
  835. client.SendAgentAlertMessage(message, false);
  836. }
  837. }
  838. contents.Items = null;
  839. foreach (InventoryFolderBase childFolder in contents.Folders)
  840. {
  841. GiveInventoryFolder(client, recipientId, senderId, childFolder.ID, newFolder.ID);
  842. }
  843. return newFolder;
  844. }
  845. public virtual InventoryFolderBase GiveInventoryFolder(IClientAPI client,
  846. UUID recipientId, UUID senderId, UUID folderId, UUID recipientParentFolderId, Dictionary<UUID, AssetType> ids)
  847. {
  848. //// Retrieve the folder from the sender
  849. InventoryFolderBase folder = InventoryService.GetFolder(senderId, folderId);
  850. if (folder == null)
  851. {
  852. m_log.ErrorFormat("[AGENT INVENTORY]: Could not find inventory folder {0} to give", folderId);
  853. return null;
  854. }
  855. if(!ids.Remove(folder.ID))
  856. return null;
  857. if (denyGiveFolderTypes.Contains(folder.Type))
  858. {
  859. m_log.ErrorFormat("[AGENT INVENTORY]: can not give inventory folder {0}", folderId);
  860. return null;
  861. }
  862. if (recipientParentFolderId == UUID.Zero)
  863. {
  864. InventoryFolderBase recipientRootFolder = InventoryService.GetRootFolder(recipientId);
  865. if (recipientRootFolder != null)
  866. recipientParentFolderId = recipientRootFolder.ID;
  867. else
  868. {
  869. m_log.WarnFormat("[AGENT INVENTORY]: Unable to find root folder for receiving agent");
  870. return null;
  871. }
  872. }
  873. UUID newFolderId = UUID.Random();
  874. InventoryFolderBase newFolder = new InventoryFolderBase(
  875. newFolderId, folder.Name, recipientId, folder.Type, recipientParentFolderId, folder.Version);
  876. InventoryService.AddFolder(newFolder);
  877. InventoryCollection contents = InventoryService.GetFolderContent(senderId, folderId);
  878. if(client == null)
  879. {
  880. foreach (InventoryItemBase item in contents.Items)
  881. {
  882. if (ids.Remove(item.ID))
  883. GiveInventoryItem(recipientId, senderId, item.ID, newFolder.ID, out string message);
  884. }
  885. }
  886. else
  887. {
  888. foreach (InventoryItemBase item in contents.Items)
  889. {
  890. if (ids.Remove(item.ID))
  891. {
  892. if (GiveInventoryItem(recipientId, senderId, item.ID, newFolder.ID, out string message) == null)
  893. client.SendAgentAlertMessage(message, false);
  894. }
  895. }
  896. }
  897. contents.Items = null;
  898. foreach (InventoryFolderBase childFolder in contents.Folders)
  899. {
  900. GiveInventoryFolder(client, recipientId, senderId, childFolder.ID, newFolder.ID);
  901. }
  902. return newFolder;
  903. }
  904. public void CopyInventoryItem(IClientAPI remoteClient, uint callbackID, UUID oldAgentID, UUID oldItemID,
  905. UUID newFolderID, string newName)
  906. {
  907. m_log.DebugFormat(
  908. "[AGENT INVENTORY]: CopyInventoryItem received by {0} with oldAgentID {1}, oldItemID {2}, new FolderID {3}, newName {4}",
  909. remoteClient.AgentId, oldAgentID, oldItemID, newFolderID, newName);
  910. InventoryItemBase item = null;
  911. if (LibraryService != null && LibraryService.LibraryRootFolder != null)
  912. item = LibraryService.LibraryRootFolder.FindItem(oldItemID);
  913. if (item == null)
  914. {
  915. item = InventoryService.GetItem(remoteClient.AgentId, oldItemID);
  916. if (item == null)
  917. {
  918. m_log.Error("[AGENT INVENTORY]: Failed to find item " + oldItemID.ToString());
  919. return;
  920. }
  921. if ((item.CurrentPermissions & (uint)PermissionMask.Copy) == 0)
  922. return;
  923. }
  924. if (newName == String.Empty)
  925. newName = item.Name;
  926. AssetBase asset = AssetService.Get(item.AssetID.ToString());
  927. if (asset != null)
  928. {
  929. if (newName != String.Empty)
  930. {
  931. asset.Name = newName;
  932. }
  933. else
  934. {
  935. newName = item.Name;
  936. }
  937. if (remoteClient.AgentId == oldAgentID
  938. || (LibraryService != null
  939. && LibraryService.LibraryRootFolder != null
  940. && oldAgentID == LibraryService.LibraryRootFolder.Owner))
  941. {
  942. CreateNewInventoryItem(
  943. remoteClient, item.CreatorId, item.CreatorData, newFolderID,
  944. newName, item.Description, item.Flags, callbackID, item.AssetID, (sbyte)item.AssetType, (sbyte)item.InvType,
  945. item.BasePermissions, item.CurrentPermissions, item.EveryOnePermissions,
  946. item.NextPermissions, item.GroupPermissions, Util.UnixTimeSinceEpoch(), false);
  947. }
  948. else
  949. {
  950. // If item is transfer or permissions are off or calling agent is allowed to copy item owner's inventory item.
  951. if (((item.CurrentPermissions & (uint)PermissionMask.Transfer) != 0)
  952. && (m_permissions.BypassPermissions()
  953. || m_permissions.CanCopyUserInventory(remoteClient.AgentId, oldItemID)))
  954. {
  955. CreateNewInventoryItem(
  956. remoteClient, item.CreatorId, item.CreatorData, newFolderID, newName, item.Description, item.Flags, callbackID,
  957. item.AssetID, (sbyte)item.AssetType, (sbyte) item.InvType,
  958. item.NextPermissions, item.NextPermissions, item.EveryOnePermissions & item.NextPermissions,
  959. item.NextPermissions, item.GroupPermissions, Util.UnixTimeSinceEpoch(), false);
  960. }
  961. }
  962. }
  963. else
  964. {
  965. m_log.ErrorFormat(
  966. "[AGENT INVENTORY]: Could not copy item {0} since asset {1} could not be found",
  967. item.Name, item.AssetID);
  968. }
  969. }
  970. /// <summary>
  971. /// Create a new asset data structure.
  972. /// </summary>
  973. public AssetBase CreateAsset(string name, string description, sbyte assetType, byte[] data, UUID creatorID)
  974. {
  975. AssetBase asset = new AssetBase(UUID.Random(), name, assetType, creatorID.ToString());
  976. asset.Description = description;
  977. asset.Data = (data == null) ? new byte[1] : data;
  978. return asset;
  979. }
  980. /// <summary>
  981. /// Move an item within the agent's inventory, and leave a copy (used in making a new outfit)
  982. /// </summary>
  983. public void MoveInventoryItemsLeaveCopy(IClientAPI remoteClient, List<InventoryItemBase> items, UUID destfolder)
  984. {
  985. List<InventoryItemBase> moveitems = new List<InventoryItemBase>();
  986. foreach (InventoryItemBase b in items)
  987. {
  988. CopyInventoryItem(remoteClient, 0, remoteClient.AgentId, b.ID, b.Folder, null);
  989. InventoryItemBase n = InventoryService.GetItem(b.Owner, b.ID);
  990. n.Folder = destfolder;
  991. moveitems.Add(n);
  992. remoteClient.SendInventoryItemCreateUpdate(n, 0);
  993. }
  994. MoveInventoryItem(remoteClient, moveitems);
  995. }
  996. /// <summary>
  997. /// Move an item within the agent's inventory.
  998. /// </summary>
  999. /// <param name="remoteClient"></param>
  1000. /// <param name="folderID"></param>
  1001. /// <param name="itemID"></param>
  1002. /// <param name="length"></param>
  1003. /// <param name="newName"></param>
  1004. public void MoveInventoryItem(IClientAPI remoteClient, List<InventoryItemBase> items)
  1005. {
  1006. m_log.DebugFormat(
  1007. "[AGENT INVENTORY]: Moving {0} items for user {1}", items.Count, remoteClient.AgentId);
  1008. if (!InventoryService.MoveItems(remoteClient.AgentId, items))
  1009. m_log.Warn("[AGENT INVENTORY]: Failed to move items for user " + remoteClient.AgentId);
  1010. }
  1011. /// <summary>
  1012. /// Create a new Inventory Item
  1013. /// </summary>
  1014. /// <param name="remoteClient">Client creating this inventory item.</param>
  1015. /// <param name="creatorID"></param>
  1016. /// <param name="creatorData"></param>
  1017. /// <param name="folderID">UUID of folder in which this item should be placed.</param>
  1018. /// <param name="name">Item name.</para>
  1019. /// <param name="description">Item description.</param>
  1020. /// <param name="flags">Item flags</param>
  1021. /// <param name="callbackID">Generated by the client.</para>
  1022. /// <param name="asset">Asset to which this item refers.</param>
  1023. /// <param name="invType">Type of inventory item.</param>
  1024. /// <param name="baseMask">Base permissions mask.</param>
  1025. /// <param name="currentMask">Current permissions mask.</param>
  1026. /// <param name="everyoneMask">Everyone permissions mask.</param>
  1027. /// <param name="nextOwnerMask">Next owner pemrissions mask.</param>
  1028. /// <param name="groupMask">Group permissions mask.</param>
  1029. /// <param name="creationDate">Unix timestamp at which this item was created.</param>
  1030. public void CreateNewInventoryItem(
  1031. IClientAPI remoteClient, string creatorID, string creatorData, UUID folderID,
  1032. string name, string description, uint flags, uint callbackID, UUID assetID, sbyte assetType, sbyte invType,
  1033. uint baseMask, uint currentMask, uint everyoneMask, uint nextOwnerMask, uint groupMask, int creationDate,
  1034. bool assetUpload)
  1035. {
  1036. InventoryItemBase item = new InventoryItemBase();
  1037. item.Owner = remoteClient.AgentId;
  1038. item.CreatorId = creatorID;
  1039. item.CreatorData = creatorData;
  1040. item.ID = UUID.Random();
  1041. item.AssetID = assetID;
  1042. item.Name = name;
  1043. item.Description = description;
  1044. item.Flags = flags;
  1045. item.AssetType = assetType;
  1046. item.InvType = invType;
  1047. item.Folder = folderID;
  1048. item.CurrentPermissions = currentMask;
  1049. item.NextPermissions = nextOwnerMask;
  1050. item.EveryOnePermissions = everyoneMask;
  1051. item.GroupPermissions = groupMask;
  1052. item.BasePermissions = baseMask;
  1053. item.CreationDate = creationDate;
  1054. // special AnimationSet case
  1055. if (item.InvType == (int)CustomInventoryType.AnimationSet)
  1056. AnimationSet.enforceItemPermitions(item,true);
  1057. if (AddInventoryItem(item))
  1058. {
  1059. remoteClient.SendInventoryItemCreateUpdate(item, callbackID);
  1060. }
  1061. else
  1062. {
  1063. m_dialogModule.SendAlertToUser(remoteClient, "Failed to create item");
  1064. m_log.WarnFormat(
  1065. "Failed to add item for {0} in CreateNewInventoryItem!",
  1066. remoteClient.Name);
  1067. }
  1068. }
  1069. /// <summary>
  1070. /// Link an inventory item to an existing item.
  1071. /// </summary>
  1072. /// <remarks>
  1073. /// The linkee item id is placed in the asset id slot. This appears to be what the viewer expects when
  1074. /// it receives inventory information.
  1075. /// </remarks>
  1076. /// <param name="remoteClient"></param>
  1077. /// <param name="transActionID"></param>
  1078. /// <param name="folderID"></param>
  1079. /// <param name="callbackID"></param>
  1080. /// <param name="description"></param>
  1081. /// <param name="name"></param>
  1082. /// <param name="invType"></param>
  1083. /// <param name="type">/param>
  1084. /// <param name="olditemID"></param>
  1085. private void HandleLinkInventoryItem(IClientAPI remoteClient, UUID transActionID, UUID folderID,
  1086. uint callbackID, string description, string name,
  1087. sbyte invType, sbyte type, UUID olditemID)
  1088. {
  1089. // m_log.DebugFormat(
  1090. // "[AGENT INVENTORY]: Received request from {0} to create inventory item link {1} in folder {2} pointing to {3}, assetType {4}, inventoryType {5}",
  1091. // remoteClient.Name, name, folderID, olditemID, (AssetType)type, (InventoryType)invType);
  1092. if (!Permissions.CanCreateUserInventory(invType, remoteClient.AgentId))
  1093. return;
  1094. ScenePresence presence;
  1095. if (TryGetScenePresence(remoteClient.AgentId, out presence))
  1096. {
  1097. // Disabled the check for duplicate links.
  1098. //
  1099. // When outfits are being adjusted, the viewer rapidly sends delete link messages followed by
  1100. // create links. However, since these are handled asynchronously, the deletes do not complete before
  1101. // the creates are handled. Therefore, we cannot enforce a duplicate link check.
  1102. // InventoryItemBase existingLink = null;
  1103. // List<InventoryItemBase> existingItems = InventoryService.GetFolderItems(remoteClient.AgentId, folderID);
  1104. // foreach (InventoryItemBase item in existingItems)
  1105. // if (item.AssetID == olditemID)
  1106. // existingLink = item;
  1107. //
  1108. // if (existingLink != null)
  1109. // {
  1110. // m_log.WarnFormat(
  1111. // "[AGENT INVENTORY]: Ignoring request from {0} to create item link {1} in folder {2} pointing to {3} since a link named {4} with id {5} already exists",
  1112. // remoteClient.Name, name, folderID, olditemID, existingLink.Name, existingLink.ID);
  1113. //
  1114. // return;
  1115. // }
  1116. CreateNewInventoryItem(
  1117. remoteClient, remoteClient.AgentId.ToString(), string.Empty, folderID,
  1118. name, description, 0, callbackID, olditemID, type, invType,
  1119. (uint)PermissionMask.All | (uint)PermissionMask.Export, (uint)PermissionMask.All | (uint)PermissionMask.Export, (uint)PermissionMask.All,
  1120. (uint)PermissionMask.All | (uint)PermissionMask.Export, (uint)PermissionMask.All | (uint)PermissionMask.Export, Util.UnixTimeSinceEpoch(),
  1121. false);
  1122. }
  1123. else
  1124. {
  1125. m_log.ErrorFormat(
  1126. "ScenePresence for agent uuid {0} unexpectedly not found in HandleLinkInventoryItem",
  1127. remoteClient.AgentId);
  1128. }
  1129. }
  1130. /// <summary>
  1131. /// Remove an inventory item for the client's inventory
  1132. /// </summary>
  1133. /// <param name="remoteClient"></param>
  1134. /// <param name="itemID"></param>
  1135. private void RemoveInventoryItem(IClientAPI remoteClient, List<UUID> itemIDs)
  1136. {
  1137. // m_log.DebugFormat(
  1138. // "[AGENT INVENTORY]: Removing inventory items {0} for {1}",
  1139. // string.Join(",", itemIDs.ConvertAll<string>(uuid => uuid.ToString()).ToArray()),
  1140. // remoteClient.Name);
  1141. InventoryService.DeleteItems(remoteClient.AgentId, itemIDs);
  1142. }
  1143. /// <summary>
  1144. /// Removes an inventory folder. This packet is sent when the user
  1145. /// right-clicks a folder that's already in trash and chooses "purge"
  1146. /// </summary>
  1147. /// <param name="remoteClient"></param>
  1148. /// <param name="folderID"></param>
  1149. private void RemoveInventoryFolder(IClientAPI remoteClient, List<UUID> folderIDs)
  1150. {
  1151. m_log.DebugFormat("[SCENE INVENTORY]: RemoveInventoryFolders count {0}", folderIDs.Count);
  1152. InventoryService.DeleteFolders(remoteClient.AgentId, folderIDs);
  1153. }
  1154. /// <summary>
  1155. /// Send the details of a prim's inventory to the client.
  1156. /// </summary>
  1157. /// <param name="remoteClient"></param>
  1158. /// <param name="primLocalID"></param>
  1159. public void RequestTaskInventory(IClientAPI remoteClient, uint primLocalID)
  1160. {
  1161. SceneObjectPart part = GetSceneObjectPart(primLocalID);
  1162. if (part == null)
  1163. return;
  1164. if (XferManager != null)
  1165. part.Inventory.RequestInventoryFile(remoteClient, XferManager);
  1166. }
  1167. /// <summary>
  1168. /// Remove an item from a prim (task) inventory
  1169. /// </summary>
  1170. /// <param name="remoteClient">Unused at the moment but retained since the avatar ID might
  1171. /// be necessary for a permissions check at some stage.</param>
  1172. /// <param name="itemID"></param>
  1173. /// <param name="localID"></param>
  1174. public void RemoveTaskInventory(IClientAPI remoteClient, UUID itemID, uint localID)
  1175. {
  1176. SceneObjectPart part = GetSceneObjectPart(localID);
  1177. SceneObjectGroup group = null;
  1178. if (part != null)
  1179. {
  1180. group = part.ParentGroup;
  1181. }
  1182. if (part != null && group != null)
  1183. {
  1184. if (!Permissions.CanEditObjectInventory(part.UUID, remoteClient.AgentId))
  1185. return;
  1186. TaskInventoryItem item = group.GetInventoryItem(localID, itemID);
  1187. if (item == null)
  1188. return;
  1189. InventoryFolderBase destFolder = InventoryService.GetFolderForType(remoteClient.AgentId, FolderType.Trash);
  1190. // Move the item to trash. If this is a copyable item, only
  1191. // a copy will be moved and we will still need to delete
  1192. // the item from the prim. If it was no copy, it will be
  1193. // deleted by this method.
  1194. string message;
  1195. InventoryItemBase item2 = MoveTaskInventoryItem(remoteClient, destFolder.ID, part, itemID, out message);
  1196. if (item2 == null)
  1197. {
  1198. m_log.WarnFormat("[SCENE INVENTORY]: RemoveTaskInventory of item {0} failed: {1}", itemID, message);
  1199. remoteClient.SendAgentAlertMessage(message, false);
  1200. return;
  1201. }
  1202. if (group.GetInventoryItem(localID, itemID) != null)
  1203. {
  1204. if (item.Type == (int)InventoryType.LSL)
  1205. {
  1206. part.RemoveScriptEvents(itemID);
  1207. part.ParentGroup.AddActiveScriptCount(-1);
  1208. }
  1209. group.RemoveInventoryItem(localID, itemID);
  1210. group.InvalidateEffectivePerms();
  1211. }
  1212. part.SendPropertiesToClient(remoteClient);
  1213. }
  1214. }
  1215. /// <summary>
  1216. /// Creates (in memory only) a user inventory item that will contain a copy of a task inventory item.
  1217. /// </summary>
  1218. private InventoryItemBase CreateAgentInventoryItemFromTask(UUID destAgent, SceneObjectPart part, UUID itemId, out string message)
  1219. {
  1220. TaskInventoryItem taskItem = part.Inventory.GetInventoryItem(itemId);
  1221. if (null == taskItem)
  1222. {
  1223. m_log.ErrorFormat(
  1224. "[PRIM INVENTORY]: Tried to retrieve item ID {0} from prim {1}, {2} for creating an avatar"
  1225. + " inventory item from a prim's inventory item "
  1226. + " but the required item does not exist in the prim's inventory",
  1227. itemId, part.Name, part.UUID);
  1228. message = "Item not found: " + itemId;
  1229. return null;
  1230. }
  1231. if ((destAgent != taskItem.OwnerID) && ((taskItem.CurrentPermissions & (uint)PermissionMask.Transfer) == 0))
  1232. {
  1233. message = "Item doesn't have the Transfer permission.";
  1234. return null;
  1235. }
  1236. InventoryItemBase agentItem = new InventoryItemBase();
  1237. agentItem.ID = UUID.Random();
  1238. agentItem.CreatorId = taskItem.CreatorID.ToString();
  1239. agentItem.CreatorData = taskItem.CreatorData;
  1240. agentItem.Owner = destAgent;
  1241. agentItem.AssetID = taskItem.AssetID;
  1242. agentItem.Description = taskItem.Description;
  1243. agentItem.Name = taskItem.Name;
  1244. agentItem.AssetType = taskItem.Type;
  1245. agentItem.InvType = taskItem.InvType;
  1246. agentItem.Flags = taskItem.Flags;
  1247. // The code below isn't OK. It doesn't account for flags being changed
  1248. // in the object inventory, so it will break when you do it. That
  1249. // is the previous behaviour, so no matter at this moment. However, there is a lot
  1250. // TODO: Fix this after the inventory fixer exists and has beenr run
  1251. if ((part.OwnerID != destAgent) && Permissions.PropagatePermissions())
  1252. {
  1253. uint perms = taskItem.BasePermissions & taskItem.NextPermissions;
  1254. if (taskItem.InvType == (int)InventoryType.Object)
  1255. {
  1256. PermissionsUtil.ApplyFoldedPermissions(taskItem.CurrentPermissions, ref perms );
  1257. perms = PermissionsUtil.FixAndFoldPermissions(perms);
  1258. }
  1259. else
  1260. perms &= taskItem.CurrentPermissions;
  1261. // always unlock
  1262. perms |= (uint)PermissionMask.Move;
  1263. agentItem.BasePermissions = perms;
  1264. agentItem.CurrentPermissions = perms;
  1265. agentItem.NextPermissions = perms & taskItem.NextPermissions;
  1266. agentItem.EveryOnePermissions = perms & taskItem.EveryonePermissions;
  1267. agentItem.GroupPermissions = perms & taskItem.GroupPermissions;
  1268. agentItem.Flags |= (uint)InventoryItemFlags.ObjectSlamPerm;
  1269. agentItem.Flags &= ~(uint)(InventoryItemFlags.ObjectOverwriteBase | InventoryItemFlags.ObjectOverwriteOwner | InventoryItemFlags.ObjectOverwriteGroup | InventoryItemFlags.ObjectOverwriteEveryone | InventoryItemFlags.ObjectOverwriteNextOwner);
  1270. }
  1271. else
  1272. {
  1273. agentItem.BasePermissions = taskItem.BasePermissions;
  1274. agentItem.CurrentPermissions = taskItem.CurrentPermissions;
  1275. agentItem.NextPermissions = taskItem.NextPermissions;
  1276. agentItem.EveryOnePermissions = taskItem.EveryonePermissions;
  1277. agentItem.GroupPermissions = taskItem.GroupPermissions;
  1278. }
  1279. message = null;
  1280. return agentItem;
  1281. }
  1282. /// <summary>
  1283. /// If the task item is not-copyable then remove it from the prim.
  1284. /// </summary>
  1285. private void RemoveNonCopyTaskItemFromPrim(SceneObjectPart part, UUID itemId)
  1286. {
  1287. TaskInventoryItem taskItem = part.Inventory.GetInventoryItem(itemId);
  1288. if (taskItem == null)
  1289. return;
  1290. if (!Permissions.BypassPermissions())
  1291. {
  1292. if ((taskItem.CurrentPermissions & (uint)PermissionMask.Copy) == 0)
  1293. {
  1294. if (taskItem.Type == (int)AssetType.LSLText)
  1295. {
  1296. part.RemoveScriptEvents(itemId);
  1297. part.ParentGroup.AddActiveScriptCount(-1);
  1298. }
  1299. part.Inventory.RemoveInventoryItem(itemId);
  1300. }
  1301. }
  1302. }
  1303. /// <summary>
  1304. /// Move the given item in the given prim to a folder in the client's inventory
  1305. /// </summary>
  1306. /// <param name="remoteClient"></param>
  1307. /// <param name="folderID"></param>
  1308. /// <param name="part"></param>
  1309. /// <param name="itemID"></param>
  1310. public InventoryItemBase MoveTaskInventoryItem(IClientAPI remoteClient, UUID folderId, SceneObjectPart part, UUID itemId, out string message)
  1311. {
  1312. m_log.DebugFormat(
  1313. "[PRIM INVENTORY]: Adding item {0} from {1} to folder {2} for {3}",
  1314. itemId, part.Name, folderId, remoteClient.Name);
  1315. InventoryItemBase agentItem = CreateAgentInventoryItemFromTask(remoteClient.AgentId, part, itemId, out message);
  1316. if (agentItem == null)
  1317. return null;
  1318. agentItem.Folder = folderId;
  1319. AddInventoryItem(remoteClient, agentItem);
  1320. RemoveNonCopyTaskItemFromPrim(part, itemId);
  1321. message = null;
  1322. return agentItem;
  1323. }
  1324. /// <summary>
  1325. /// <see>ClientMoveTaskInventoryItem</see>
  1326. /// </summary>
  1327. /// <param name="remoteClient"></param>
  1328. /// <param name="folderID"></param>
  1329. /// <param name="primLocalID"></param>
  1330. /// <param name="itemID"></param>
  1331. public void ClientMoveTaskInventoryItem(IClientAPI remoteClient, UUID folderId, uint primLocalId, UUID itemId)
  1332. {
  1333. SceneObjectPart part = GetSceneObjectPart(primLocalId);
  1334. // Can't move a null item
  1335. if (itemId == UUID.Zero)
  1336. return;
  1337. if (null == part)
  1338. {
  1339. m_log.WarnFormat(
  1340. "[PRIM INVENTORY]: " +
  1341. "Move of inventory item {0} from prim with local id {1} failed because the prim could not be found",
  1342. itemId, primLocalId);
  1343. return;
  1344. }
  1345. TaskInventoryItem taskItem = part.Inventory.GetInventoryItem(itemId);
  1346. if (null == taskItem)
  1347. {
  1348. m_log.WarnFormat("[PRIM INVENTORY]: Move of inventory item {0} from prim with local id {1} failed"
  1349. + " because the inventory item could not be found",
  1350. itemId, primLocalId);
  1351. return;
  1352. }
  1353. if (!Permissions.CanCopyObjectInventory(itemId, part.UUID, remoteClient.AgentId))
  1354. return;
  1355. string message;
  1356. InventoryItemBase item = MoveTaskInventoryItem(remoteClient, folderId, part, itemId, out message);
  1357. if (item == null)
  1358. remoteClient.SendAgentAlertMessage(message, false);
  1359. }
  1360. /// <summary>
  1361. /// <see>MoveTaskInventoryItem</see>
  1362. /// </summary>
  1363. /// <param name="remoteClient"></param>
  1364. /// <param name="folderID">
  1365. /// The user inventory folder to move (or copy) the item to. If null, then the most
  1366. /// suitable system folder is used (e.g. the Objects folder for objects). If there is no suitable folder, then
  1367. /// the item is placed in the user's root inventory folder
  1368. /// </param>
  1369. /// <param name="part"></param>
  1370. /// <param name="itemID"></param>
  1371. public InventoryItemBase MoveTaskInventoryItem(UUID avatarId, UUID folderId, SceneObjectPart part, UUID itemId, out string message)
  1372. {
  1373. ScenePresence avatar;
  1374. if (TryGetScenePresence(avatarId, out avatar))
  1375. {
  1376. return MoveTaskInventoryItem(avatar.ControllingClient, folderId, part, itemId, out message);
  1377. }
  1378. else
  1379. {
  1380. InventoryItemBase agentItem = CreateAgentInventoryItemFromTask(avatarId, part, itemId, out message);
  1381. if (agentItem == null)
  1382. return null;
  1383. agentItem.Folder = folderId;
  1384. AddInventoryItem(agentItem);
  1385. RemoveNonCopyTaskItemFromPrim(part, itemId);
  1386. return agentItem;
  1387. }
  1388. }
  1389. /// <summary>
  1390. /// Copy a task (prim) inventory item to another task (prim)
  1391. /// </summary>
  1392. /// <param name="destId">ID of destination part</param>
  1393. /// <param name="part">Source part</param>
  1394. /// <param name="itemId">Source item id to transfer</param>
  1395. public void MoveTaskInventoryItem(UUID destId, SceneObjectPart part, UUID itemId)
  1396. {
  1397. TaskInventoryItem srcTaskItem = part.Inventory.GetInventoryItem(itemId);
  1398. if (srcTaskItem == null)
  1399. {
  1400. m_log.ErrorFormat(
  1401. "[PRIM INVENTORY]: Tried to retrieve item ID {0} from prim {1}, {2} for moving"
  1402. + " but the item does not exist in this inventory",
  1403. itemId, part.Name, part.UUID);
  1404. return;
  1405. }
  1406. SceneObjectPart destPart = GetSceneObjectPart(destId);
  1407. if (destPart == null)
  1408. {
  1409. m_log.ErrorFormat(
  1410. "[PRIM INVENTORY]: " +
  1411. "Could not find prim for ID {0}",
  1412. destId);
  1413. return;
  1414. }
  1415. if(!Permissions.CanDoObjectInvToObjectInv(srcTaskItem, part, destPart))
  1416. return;
  1417. TaskInventoryItem destTaskItem = new TaskInventoryItem();
  1418. destTaskItem.ItemID = UUID.Random();
  1419. destTaskItem.CreatorID = srcTaskItem.CreatorID;
  1420. destTaskItem.CreatorData = srcTaskItem.CreatorData;
  1421. destTaskItem.AssetID = srcTaskItem.AssetID;
  1422. destTaskItem.GroupID = destPart.GroupID;
  1423. destTaskItem.OwnerID = destPart.OwnerID;
  1424. destTaskItem.ParentID = destPart.UUID;
  1425. destTaskItem.ParentPartID = destPart.UUID;
  1426. destTaskItem.BasePermissions = srcTaskItem.BasePermissions;
  1427. destTaskItem.EveryonePermissions = srcTaskItem.EveryonePermissions;
  1428. destTaskItem.GroupPermissions = srcTaskItem.GroupPermissions;
  1429. destTaskItem.CurrentPermissions = srcTaskItem.CurrentPermissions;
  1430. destTaskItem.NextPermissions = srcTaskItem.NextPermissions;
  1431. destTaskItem.Flags = srcTaskItem.Flags;
  1432. if (destPart.OwnerID != part.OwnerID)
  1433. {
  1434. if (Permissions.PropagatePermissions())
  1435. {
  1436. destTaskItem.CurrentPermissions = srcTaskItem.CurrentPermissions &
  1437. (srcTaskItem.NextPermissions | (uint)PermissionMask.Move);
  1438. destTaskItem.GroupPermissions = srcTaskItem.GroupPermissions &
  1439. (srcTaskItem.NextPermissions | (uint)PermissionMask.Move);
  1440. destTaskItem.EveryonePermissions = srcTaskItem.EveryonePermissions &
  1441. (srcTaskItem.NextPermissions | (uint)PermissionMask.Move);
  1442. destTaskItem.BasePermissions = srcTaskItem.BasePermissions &
  1443. (srcTaskItem.NextPermissions | (uint)PermissionMask.Move);
  1444. destTaskItem.Flags |= (uint)InventoryItemFlags.ObjectSlamPerm;
  1445. }
  1446. }
  1447. destTaskItem.Description = srcTaskItem.Description;
  1448. destTaskItem.Name = srcTaskItem.Name;
  1449. destTaskItem.InvType = srcTaskItem.InvType;
  1450. destTaskItem.Type = srcTaskItem.Type;
  1451. destPart.Inventory.AddInventoryItem(destTaskItem, part.OwnerID != destPart.OwnerID);
  1452. if ((srcTaskItem.CurrentPermissions & (uint)PermissionMask.Copy) == 0)
  1453. {
  1454. part.Inventory.RemoveInventoryItem(itemId);
  1455. }
  1456. ScenePresence avatar;
  1457. if (TryGetScenePresence(srcTaskItem.OwnerID, out avatar))
  1458. {
  1459. destPart.SendPropertiesToClient(avatar.ControllingClient);
  1460. }
  1461. }
  1462. public UUID MoveTaskInventoryItems(UUID destID, string category, SceneObjectPart host, List<UUID> items, bool sendUpdates = true)
  1463. {
  1464. ScenePresence avatar;
  1465. IClientAPI remoteClient = null;
  1466. if (TryGetScenePresence(destID, out avatar))
  1467. remoteClient = avatar.ControllingClient;
  1468. SceneObjectPart destPart = GetSceneObjectPart(destID);
  1469. if (destPart != null) // Move into a prim
  1470. {
  1471. foreach(UUID itemID in items)
  1472. MoveTaskInventoryItem(destID, host, itemID);
  1473. return destID; // Prim folder ID == prim ID
  1474. }
  1475. // move to a avatar inventory
  1476. if(remoteClient == null)
  1477. return UUID.Zero;
  1478. InventoryFolderBase rootFolder = InventoryService.GetRootFolder(destID);
  1479. if(rootFolder == null)
  1480. return UUID.Zero;
  1481. UUID newFolderID = UUID.Random();
  1482. InventoryFolderBase newFolder = new InventoryFolderBase(newFolderID, category, destID, -1, rootFolder.ID, rootFolder.Version);
  1483. InventoryService.AddFolder(newFolder);
  1484. foreach (UUID itemID in items)
  1485. {
  1486. string message;
  1487. InventoryItemBase agentItem = CreateAgentInventoryItemFromTask(destID, host, itemID, out message);
  1488. if (agentItem != null)
  1489. {
  1490. agentItem.Folder = newFolderID;
  1491. AddInventoryItem(agentItem);
  1492. RemoveNonCopyTaskItemFromPrim(host, itemID);
  1493. }
  1494. else
  1495. {
  1496. remoteClient.SendAgentAlertMessage(message, false);
  1497. }
  1498. }
  1499. if(sendUpdates)
  1500. {
  1501. SendInventoryUpdate(remoteClient, rootFolder, true, false);
  1502. SendInventoryUpdate(remoteClient, newFolder, false, true);
  1503. }
  1504. return newFolderID;
  1505. }
  1506. public void SendInventoryUpdate(IClientAPI client, InventoryFolderBase folder, bool fetchFolders, bool fetchItems)
  1507. {
  1508. if (folder == null)
  1509. return;
  1510. // TODO: This code for looking in the folder for the library should be folded somewhere else
  1511. // so that this class doesn't have to know the details (and so that multiple libraries, etc.
  1512. // can be handled transparently).
  1513. InventoryFolderImpl fold = null;
  1514. if (LibraryService != null && LibraryService.LibraryRootFolder != null)
  1515. {
  1516. if ((fold = LibraryService.LibraryRootFolder.FindFolder(folder.ID)) != null)
  1517. {
  1518. List<InventoryItemBase> its = fold.RequestListOfItems();
  1519. List<InventoryFolderBase> fds = fold.RequestListOfFolders();
  1520. client.SendInventoryFolderDetails(
  1521. fold.Owner, folder.ID, its, fds,
  1522. fold.Version, its.Count + fds.Count, fetchFolders, fetchItems);
  1523. return;
  1524. }
  1525. }
  1526. // Fetch the folder contents
  1527. InventoryCollection contents = InventoryService.GetFolderContent(client.AgentId, folder.ID);
  1528. // Fetch the folder itself to get its current version
  1529. InventoryFolderBase containingFolder = InventoryService.GetFolder(client.AgentId, folder.ID);
  1530. // m_log.DebugFormat("[AGENT INVENTORY]: Sending inventory folder contents ({0} nodes) for \"{1}\" to {2} {3}",
  1531. // contents.Folders.Count + contents.Items.Count, containingFolder.Name, client.FirstName, client.LastName);
  1532. if (containingFolder != null )
  1533. {
  1534. int descendents = contents.Folders.Count + contents.Items.Count;
  1535. if(fetchItems && contents.Items.Count > 0)
  1536. {
  1537. var linksIDs = new HashSet<UUID>();
  1538. var links = new List<InventoryItemBase>();
  1539. for (int i = 0; i < contents.Items.Count; ++i)
  1540. {
  1541. InventoryItemBase item = contents.Items[i];
  1542. if (item.AssetType == (int)AssetType.Link)
  1543. {
  1544. UUID linkid = item.AssetID;
  1545. if(linksIDs.Contains(linkid))
  1546. continue;
  1547. InventoryItemBase linkedItem = InventoryService.GetItem(item.Owner, linkid);
  1548. if (linkedItem != null && linkedItem.AssetType != (int)AssetType.Link && linkedItem.AssetType != (int)AssetType.LinkFolder)
  1549. {
  1550. links.Add(linkedItem);
  1551. linksIDs.Add(linkedItem.ID);
  1552. }
  1553. }
  1554. }
  1555. if(links.Count > 0)
  1556. {
  1557. links.AddRange(contents.Items);
  1558. contents.Items = links;
  1559. }
  1560. }
  1561. client.SendInventoryFolderDetails(
  1562. client.AgentId, folder.ID, contents.Items, contents.Folders,
  1563. containingFolder.Version, descendents, fetchFolders, fetchItems);
  1564. }
  1565. }
  1566. /// <summary>
  1567. /// Update an item in a prim (task) inventory.
  1568. /// This method does not handle scripts, <see>RezScript(IClientAPI, UUID, unit)</see>
  1569. /// </summary>
  1570. /// <param name="remoteClient"></param>
  1571. /// <param name="transactionID"></param>
  1572. /// <param name="itemInfo"></param>
  1573. /// <param name="primLocalID"></param>
  1574. public void UpdateTaskInventory(IClientAPI remoteClient, UUID transactionID, TaskInventoryItem itemInfo,
  1575. uint primLocalID)
  1576. {
  1577. UUID itemID = itemInfo.ItemID;
  1578. if (itemID == UUID.Zero)
  1579. {
  1580. m_log.ErrorFormat(
  1581. "[PRIM INVENTORY]: UpdateTaskInventory called with item ID Zero on update for {1}!",
  1582. remoteClient.Name);
  1583. return;
  1584. }
  1585. // Find the prim we're dealing with
  1586. SceneObjectPart part = GetSceneObjectPart(primLocalID);
  1587. if(part == null)
  1588. {
  1589. m_log.WarnFormat(
  1590. "[PRIM INVENTORY]: " +
  1591. "Update with item {0} requested of prim {1} for {2} but this prim does not exist",
  1592. itemID, primLocalID, remoteClient.Name);
  1593. return;
  1594. }
  1595. TaskInventoryItem currentItem = part.Inventory.GetInventoryItem(itemID);
  1596. if (currentItem == null)
  1597. {
  1598. InventoryItemBase item = InventoryService.GetItem(remoteClient.AgentId, itemID);
  1599. // if not found Try library
  1600. if (item == null && LibraryService != null && LibraryService.LibraryRootFolder != null)
  1601. item = LibraryService.LibraryRootFolder.FindItem(itemID);
  1602. if(item == null)
  1603. {
  1604. m_log.ErrorFormat(
  1605. "[PRIM INVENTORY]: Could not find inventory item {0} to update for {1}!",
  1606. itemID, remoteClient.Name);
  1607. return;
  1608. }
  1609. if (!Permissions.CanDropInObjectInv(item, remoteClient, part))
  1610. return;
  1611. UUID copyID = UUID.Random();
  1612. bool modrights = Permissions.CanEditObject(part.ParentGroup, remoteClient);
  1613. part.ParentGroup.AddInventoryItem(remoteClient.AgentId, primLocalID, item, copyID, modrights);
  1614. m_log.InfoFormat(
  1615. "[PRIM INVENTORY]: Update with item {0} requested of prim {1} for {2}",
  1616. item.Name, primLocalID, remoteClient.Name);
  1617. part.SendPropertiesToClient(remoteClient);
  1618. if (!Permissions.BypassPermissions())
  1619. {
  1620. if ((item.CurrentPermissions & (uint)PermissionMask.Copy) == 0)
  1621. {
  1622. List<UUID> uuids = new List<UUID>();
  1623. uuids.Add(itemID);
  1624. RemoveInventoryItem(remoteClient, uuids);
  1625. }
  1626. }
  1627. }
  1628. else // Updating existing item with new perms etc
  1629. {
  1630. // m_log.DebugFormat(
  1631. // "[PRIM INVENTORY]: Updating item {0} in {1} for UpdateTaskInventory()",
  1632. // currentItem.Name, part.Name);
  1633. if (!Permissions.CanEditObjectInventory(part.UUID, remoteClient.AgentId))
  1634. return;
  1635. // Only look for an uploaded updated asset if we are passed a transaction ID. This is only the
  1636. // case for updates uploded through UDP. Updates uploaded via a capability (e.g. a script update)
  1637. // will not pass in a transaction ID in the update message.
  1638. if (transactionID != UUID.Zero && AgentTransactionsModule != null)
  1639. {
  1640. AgentTransactionsModule.HandleTaskItemUpdateFromTransaction(
  1641. remoteClient, part, transactionID, currentItem);
  1642. // if ((InventoryType)itemInfo.InvType == InventoryType.Notecard)
  1643. // remoteClient.SendAgentAlertMessage("Notecard saved", false);
  1644. // else if ((InventoryType)itemInfo.InvType == InventoryType.LSL)
  1645. // remoteClient.SendAgentAlertMessage("Script saved", false);
  1646. // else
  1647. // remoteClient.SendAgentAlertMessage("Item saved", false);
  1648. }
  1649. // Base ALWAYS has move
  1650. currentItem.BasePermissions |= (uint)PermissionMask.Move;
  1651. itemInfo.Flags = currentItem.Flags;
  1652. // Check if we're allowed to mess with permissions
  1653. if (!Permissions.IsGod(remoteClient.AgentId)) // Not a god
  1654. {
  1655. bool noChange;
  1656. if (remoteClient.AgentId != part.OwnerID) // Not owner
  1657. {
  1658. noChange = true;
  1659. if(itemInfo.OwnerID == UUID.Zero && itemInfo.GroupID != UUID.Zero)
  1660. {
  1661. if(remoteClient.IsGroupMember(itemInfo.GroupID))
  1662. {
  1663. ulong powers = remoteClient.GetGroupPowers(itemInfo.GroupID);
  1664. if((powers & (ulong)GroupPowers.ObjectManipulate) != 0)
  1665. noChange = false;
  1666. }
  1667. }
  1668. }
  1669. else
  1670. noChange = false;
  1671. if(noChange)
  1672. {
  1673. // Friends and group members can't change any perms
  1674. itemInfo.BasePermissions = currentItem.BasePermissions;
  1675. itemInfo.EveryonePermissions = currentItem.EveryonePermissions;
  1676. itemInfo.GroupPermissions = currentItem.GroupPermissions;
  1677. itemInfo.NextPermissions = currentItem.NextPermissions;
  1678. itemInfo.CurrentPermissions = currentItem.CurrentPermissions;
  1679. }
  1680. else
  1681. {
  1682. // Owner can't change base, and can change other
  1683. // only up to base
  1684. itemInfo.BasePermissions = currentItem.BasePermissions;
  1685. if (itemInfo.EveryonePermissions != currentItem.EveryonePermissions)
  1686. itemInfo.Flags |= (uint)InventoryItemFlags.ObjectOverwriteEveryone;
  1687. if (itemInfo.GroupPermissions != currentItem.GroupPermissions)
  1688. itemInfo.Flags |= (uint)InventoryItemFlags.ObjectOverwriteGroup;
  1689. if (itemInfo.CurrentPermissions != currentItem.CurrentPermissions)
  1690. itemInfo.Flags |= (uint)InventoryItemFlags.ObjectOverwriteOwner;
  1691. if (itemInfo.NextPermissions != currentItem.NextPermissions)
  1692. itemInfo.Flags |= (uint)InventoryItemFlags.ObjectOverwriteNextOwner;
  1693. itemInfo.EveryonePermissions &= currentItem.BasePermissions;
  1694. itemInfo.GroupPermissions &= currentItem.BasePermissions;
  1695. itemInfo.CurrentPermissions &= currentItem.BasePermissions;
  1696. itemInfo.NextPermissions &= currentItem.BasePermissions;
  1697. }
  1698. }
  1699. else
  1700. {
  1701. if (itemInfo.BasePermissions != currentItem.BasePermissions)
  1702. itemInfo.Flags |= (uint)InventoryItemFlags.ObjectOverwriteBase;
  1703. if (itemInfo.EveryonePermissions != currentItem.EveryonePermissions)
  1704. itemInfo.Flags |= (uint)InventoryItemFlags.ObjectOverwriteEveryone;
  1705. if (itemInfo.GroupPermissions != currentItem.GroupPermissions)
  1706. itemInfo.Flags |= (uint)InventoryItemFlags.ObjectOverwriteGroup;
  1707. if (itemInfo.CurrentPermissions != currentItem.CurrentPermissions)
  1708. itemInfo.Flags |= (uint)InventoryItemFlags.ObjectOverwriteOwner;
  1709. if (itemInfo.NextPermissions != currentItem.NextPermissions)
  1710. itemInfo.Flags |= (uint)InventoryItemFlags.ObjectOverwriteNextOwner;
  1711. }
  1712. // Next ALWAYS has move
  1713. itemInfo.NextPermissions |= (uint)PermissionMask.Move;
  1714. if (part.Inventory.UpdateInventoryItem(itemInfo))
  1715. {
  1716. part.SendPropertiesToClient(remoteClient);
  1717. }
  1718. }
  1719. }
  1720. /// <summary>
  1721. /// Rez a script into a prim's inventory, either ex nihilo or from an existing avatar inventory
  1722. /// </summary>
  1723. /// <param name="remoteClient"></param>
  1724. /// <param name="itemBase"> </param>
  1725. /// <param name="transactionID"></param>
  1726. /// <param name="localID"></param>
  1727. public void RezScript(IClientAPI remoteClient, InventoryItemBase itemBase, UUID transactionID, uint localID)
  1728. {
  1729. SceneObjectPart partWhereRezzed;
  1730. if (itemBase.ID != UUID.Zero)
  1731. partWhereRezzed = RezScriptFromAgentInventory(remoteClient.AgentId, itemBase.ID, localID);
  1732. else
  1733. partWhereRezzed = RezNewScript(remoteClient.AgentId, itemBase);
  1734. if (partWhereRezzed != null)
  1735. partWhereRezzed.SendPropertiesToClient(remoteClient);
  1736. }
  1737. /// <summary>
  1738. /// Rez a script into a prim from an agent inventory.
  1739. /// </summary>
  1740. /// <param name="agentID"></param>
  1741. /// <param name="fromItemID"></param>
  1742. /// <param name="localID"></param>
  1743. /// <returns>The part where the script was rezzed if successful. False otherwise.</returns>
  1744. public SceneObjectPart RezScriptFromAgentInventory(UUID agentID, UUID fromItemID, uint localID)
  1745. {
  1746. UUID copyID = UUID.Random();
  1747. InventoryItemBase item = InventoryService.GetItem(agentID, fromItemID);
  1748. // Try library
  1749. // XXX clumsy, possibly should be one call
  1750. if (null == item && LibraryService != null && LibraryService.LibraryRootFolder != null)
  1751. {
  1752. item = LibraryService.LibraryRootFolder.FindItem(fromItemID);
  1753. }
  1754. if (item != null)
  1755. {
  1756. SceneObjectPart part = GetSceneObjectPart(localID);
  1757. if (part != null)
  1758. {
  1759. if (!Permissions.CanEditObjectInventory(part.UUID, agentID))
  1760. return null;
  1761. part.ParentGroup.AddInventoryItem(agentID, localID, item, copyID);
  1762. // TODO: switch to posting on_rez here when scripts
  1763. // have state in inventory
  1764. part.Inventory.CreateScriptInstance(copyID, 0, false, DefaultScriptEngine, 0);
  1765. // tell anyone watching that there is a new script in town
  1766. EventManager.TriggerNewScript(agentID, part, copyID);
  1767. // m_log.InfoFormat("[PRIMINVENTORY]: " +
  1768. // "Rezzed script {0} into prim local ID {1} for user {2}",
  1769. // item.inventoryName, localID, remoteClient.Name);
  1770. part.ParentGroup.ResumeScripts();
  1771. return part;
  1772. }
  1773. else
  1774. {
  1775. m_log.ErrorFormat(
  1776. "[PRIM INVENTORY]: " +
  1777. "Could not rez script {0} into prim local ID {1} for user {2}"
  1778. + " because the prim could not be found in the region!",
  1779. item.Name, localID, agentID);
  1780. }
  1781. }
  1782. else
  1783. {
  1784. m_log.ErrorFormat(
  1785. "[PRIM INVENTORY]: Could not find script inventory item {0} to rez for {1}!",
  1786. fromItemID, agentID);
  1787. }
  1788. return null;
  1789. }
  1790. /// <summary>
  1791. /// Rez a new script from nothing.
  1792. /// </summary>
  1793. /// <param name="remoteClient"></param>
  1794. /// <param name="itemBase"></param>
  1795. /// <returns>The part where the script was rezzed if successful. False otherwise.</returns>
  1796. public SceneObjectPart RezNewScript(UUID agentID, InventoryItemBase itemBase)
  1797. {
  1798. return RezNewScript(
  1799. agentID,
  1800. itemBase,
  1801. "default\n{\n state_entry()\n {\n llSay(0, \"Script running\");\n }\n}");
  1802. }
  1803. /// <summary>
  1804. /// Rez a new script from nothing with given script text.
  1805. /// </summary>
  1806. /// <param name="remoteClient"></param>
  1807. /// <param name="itemBase">Template item.</param>
  1808. /// <param name="scriptText"></param>
  1809. /// <returns>The part where the script was rezzed if successful. False otherwise.</returns>
  1810. public SceneObjectPart RezNewScript(UUID agentID, InventoryItemBase itemBase, string scriptText)
  1811. {
  1812. // The part ID is the folder ID!
  1813. SceneObjectPart part = GetSceneObjectPart(itemBase.Folder);
  1814. if (part == null)
  1815. {
  1816. // m_log.DebugFormat(
  1817. // "[SCENE INVENTORY]: Could not find part with id {0} for {1} to rez new script",
  1818. // itemBase.Folder, agentID);
  1819. return null;
  1820. }
  1821. if (!Permissions.CanCreateObjectInventory(itemBase.InvType, part.UUID, agentID))
  1822. {
  1823. // m_log.DebugFormat(
  1824. // "[SCENE INVENTORY]: No permission to create new script in {0} for {1}", part.Name, agentID);
  1825. return null;
  1826. }
  1827. AssetBase asset
  1828. = CreateAsset(
  1829. itemBase.Name,
  1830. itemBase.Description,
  1831. (sbyte)itemBase.AssetType,
  1832. Encoding.ASCII.GetBytes(scriptText),
  1833. agentID);
  1834. AssetService.Store(asset);
  1835. TaskInventoryItem taskItem = new TaskInventoryItem();
  1836. taskItem.ResetIDs(itemBase.Folder);
  1837. taskItem.ParentID = itemBase.Folder;
  1838. taskItem.CreationDate = (uint)itemBase.CreationDate;
  1839. taskItem.Name = itemBase.Name;
  1840. taskItem.Description = itemBase.Description;
  1841. taskItem.Type = itemBase.AssetType;
  1842. taskItem.InvType = itemBase.InvType;
  1843. taskItem.OwnerID = itemBase.Owner;
  1844. taskItem.CreatorID = itemBase.CreatorIdAsUuid;
  1845. taskItem.BasePermissions = itemBase.BasePermissions;
  1846. taskItem.CurrentPermissions = itemBase.CurrentPermissions;
  1847. taskItem.EveryonePermissions = itemBase.EveryOnePermissions;
  1848. taskItem.GroupPermissions = itemBase.GroupPermissions;
  1849. taskItem.NextPermissions = itemBase.NextPermissions;
  1850. taskItem.GroupID = itemBase.GroupID;
  1851. taskItem.GroupPermissions = 0;
  1852. taskItem.Flags = itemBase.Flags;
  1853. taskItem.PermsGranter = UUID.Zero;
  1854. taskItem.PermsMask = 0;
  1855. taskItem.AssetID = asset.FullID;
  1856. part.Inventory.AddInventoryItem(taskItem, false);
  1857. part.Inventory.CreateScriptInstance(taskItem, 0, false, DefaultScriptEngine, 0);
  1858. part.ParentGroup.InvalidateEffectivePerms();
  1859. // tell anyone managing scripts that a new script exists
  1860. EventManager.TriggerNewScript(agentID, part, taskItem.ItemID);
  1861. part.ParentGroup.ResumeScripts();
  1862. return part;
  1863. }
  1864. /// <summary>
  1865. /// Rez a script into a prim's inventory from another prim
  1866. /// </summary>
  1867. /// <param name="remoteClient"></param>
  1868. /// <param name="srcPart"> </param>
  1869. /// <param name="destId"> </param>
  1870. /// <param name="pin"></param>
  1871. /// <param name="running"></param>
  1872. /// <param name="start_param"></param>
  1873. public void RezScriptFromPrim(UUID srcId, SceneObjectPart srcPart, UUID destId, int pin, int running, int start_param)
  1874. {
  1875. TaskInventoryItem srcTaskItem = srcPart.Inventory.GetInventoryItem(srcId);
  1876. if (srcTaskItem == null)
  1877. {
  1878. m_log.ErrorFormat(
  1879. "[PRIM INVENTORY]: Tried to retrieve item ID {0} from prim {1}, {2} for rezzing a script but the "
  1880. + " item does not exist in this inventory",
  1881. srcId, srcPart.Name, srcPart.UUID);
  1882. return;
  1883. }
  1884. SceneObjectPart destPart = GetSceneObjectPart(destId);
  1885. if (destPart == null)
  1886. {
  1887. m_log.ErrorFormat(
  1888. "[PRIM INVENTORY]: Could not find part {0} to insert script item {1} from {2} {3} in {4}",
  1889. destId, srcId, srcPart.Name, srcPart.UUID, Name);
  1890. return;
  1891. }
  1892. // Must own the object, and have modify rights
  1893. if (srcPart.OwnerID != destPart.OwnerID)
  1894. {
  1895. // Group permissions
  1896. if ((destPart.GroupID == UUID.Zero) || (destPart.GroupID != srcPart.GroupID) ||
  1897. ((destPart.GroupMask & (uint)PermissionMask.Modify) == 0))
  1898. return;
  1899. }
  1900. else
  1901. {
  1902. if ((destPart.OwnerMask & (uint)PermissionMask.Modify) == 0)
  1903. return;
  1904. }
  1905. if (destPart.ScriptAccessPin == 0 || destPart.ScriptAccessPin != pin)
  1906. {
  1907. m_log.WarnFormat(
  1908. "[PRIM INVENTORY]: " +
  1909. "Script in object {0} : {1}, attempted to load script {2} : {3} into object {4} : {5} with invalid pin {6}",
  1910. srcPart.Name, srcId, srcTaskItem.Name, srcTaskItem.ItemID, destPart.Name, destId, pin);
  1911. // the LSL Wiki says we are supposed to shout on the DEBUG_CHANNEL -
  1912. // "Object: Task Object trying to illegally load script onto task Other_Object!"
  1913. // How do we shout from in here?
  1914. return;
  1915. }
  1916. TaskInventoryItem destTaskItem = new TaskInventoryItem();
  1917. destTaskItem.ItemID = UUID.Random();
  1918. destTaskItem.CreatorID = srcTaskItem.CreatorID;
  1919. destTaskItem.CreatorData = srcTaskItem.CreatorData;
  1920. destTaskItem.AssetID = srcTaskItem.AssetID;
  1921. destTaskItem.GroupID = destPart.GroupID;
  1922. destTaskItem.OwnerID = destPart.OwnerID;
  1923. destTaskItem.ParentID = destPart.UUID;
  1924. destTaskItem.ParentPartID = destPart.UUID;
  1925. destTaskItem.BasePermissions = srcTaskItem.BasePermissions;
  1926. destTaskItem.EveryonePermissions = srcTaskItem.EveryonePermissions;
  1927. destTaskItem.GroupPermissions = srcTaskItem.GroupPermissions;
  1928. destTaskItem.CurrentPermissions = srcTaskItem.CurrentPermissions;
  1929. destTaskItem.NextPermissions = srcTaskItem.NextPermissions;
  1930. destTaskItem.Flags = srcTaskItem.Flags;
  1931. if (destPart.OwnerID != srcPart.OwnerID)
  1932. {
  1933. if (Permissions.PropagatePermissions())
  1934. {
  1935. destTaskItem.CurrentPermissions = srcTaskItem.CurrentPermissions &
  1936. srcTaskItem.NextPermissions;
  1937. destTaskItem.GroupPermissions = srcTaskItem.GroupPermissions &
  1938. srcTaskItem.NextPermissions;
  1939. destTaskItem.EveryonePermissions = srcTaskItem.EveryonePermissions &
  1940. srcTaskItem.NextPermissions;
  1941. destTaskItem.BasePermissions = srcTaskItem.BasePermissions &
  1942. srcTaskItem.NextPermissions;
  1943. destTaskItem.Flags |= (uint)InventoryItemFlags.ObjectSlamPerm;
  1944. }
  1945. }
  1946. destTaskItem.Description = srcTaskItem.Description;
  1947. destTaskItem.Name = srcTaskItem.Name;
  1948. destTaskItem.InvType = srcTaskItem.InvType;
  1949. destTaskItem.Type = srcTaskItem.Type;
  1950. destPart.Inventory.AddInventoryItemExclusive(destTaskItem, false);
  1951. if (running > 0)
  1952. {
  1953. destPart.Inventory.CreateScriptInstance(destTaskItem, start_param, false, DefaultScriptEngine, 0);
  1954. }
  1955. destPart.ParentGroup.ResumeScripts();
  1956. ScenePresence avatar;
  1957. if (TryGetScenePresence(srcTaskItem.OwnerID, out avatar))
  1958. {
  1959. destPart.SendPropertiesToClient(avatar.ControllingClient);
  1960. }
  1961. }
  1962. /// <summary>
  1963. /// Derez one or more objects from the scene.
  1964. /// </summary>
  1965. /// <remarks>
  1966. /// Won't actually remove the scene object in the case where the object is being copied to a user inventory.
  1967. /// </remarks>
  1968. /// <param name='remoteClient'>Client requesting derez</param>
  1969. /// <param name='localIDs'>Local ids of root parts of objects to delete.</param>
  1970. /// <param name='groupID'>Not currently used. Here because the client passes this to us.</param>
  1971. /// <param name='action'>DeRezAction</param>
  1972. /// <param name='destinationID'>User folder ID to place derezzed object</param>
  1973. public virtual void DeRezObjects(
  1974. IClientAPI remoteClient, List<uint> localIDs, UUID groupID, DeRezAction action, UUID destinationID, bool AddToReturns = true)
  1975. {
  1976. // First, see of we can perform the requested action and
  1977. // build a list of eligible objects
  1978. List<uint> deleteIDs = new List<uint>();
  1979. List<SceneObjectGroup> deleteGroups = new List<SceneObjectGroup>();
  1980. List<SceneObjectGroup> takeCopyGroups = new List<SceneObjectGroup>();
  1981. List<SceneObjectGroup> takeDeleteGroups = new List<SceneObjectGroup>();
  1982. ScenePresence sp = null;
  1983. if(remoteClient != null)
  1984. sp = remoteClient.SceneAgent as ScenePresence;
  1985. else if(action != DeRezAction.Return)
  1986. return; // only Return can be called without a client
  1987. // this is not as 0.8x code
  1988. // 0.8x did refuse all operation is not allowed on all objects
  1989. // this will do it on allowed objects
  1990. // current viewers only ask if all allowed
  1991. foreach (uint localID in localIDs)
  1992. {
  1993. // Invalid id
  1994. SceneObjectPart part = GetSceneObjectPart(localID);
  1995. if (part == null)
  1996. {
  1997. //Client still thinks the object exists, kill it
  1998. deleteIDs.Add(localID);
  1999. continue;
  2000. }
  2001. SceneObjectGroup grp = part.ParentGroup;
  2002. if (grp == null || grp.IsDeleted)
  2003. {
  2004. //Client still thinks the object exists, kill it
  2005. deleteIDs.Add(localID);
  2006. continue;
  2007. }
  2008. // Can't delete child prims
  2009. if (part != grp.RootPart)
  2010. continue;
  2011. if (grp.IsAttachment)
  2012. {
  2013. if(!sp.IsGod || action != DeRezAction.Return || action != DeRezAction.Delete)
  2014. continue;
  2015. // this may break the attachment, but its a security action
  2016. // viewers don't allow it anyways
  2017. }
  2018. // If child prims have invalid perms, fix them
  2019. grp.AdjustChildPrimPermissions(false);
  2020. switch (action)
  2021. {
  2022. case DeRezAction.SaveToExistingUserInventoryItem:
  2023. {
  2024. if (Permissions.CanTakeCopyObject(grp, sp))
  2025. takeCopyGroups.Add(grp);
  2026. break;
  2027. }
  2028. case DeRezAction.TakeCopy:
  2029. {
  2030. if (Permissions.CanTakeCopyObject(grp, sp))
  2031. takeCopyGroups.Add(grp);
  2032. break;
  2033. }
  2034. case DeRezAction.Take:
  2035. {
  2036. if (Permissions.CanTakeObject(grp, sp))
  2037. takeDeleteGroups.Add(grp);
  2038. break;
  2039. }
  2040. case DeRezAction.GodTakeCopy:
  2041. {
  2042. if((remoteClient != null) && Permissions.IsGod(remoteClient.AgentId))
  2043. takeCopyGroups.Add(grp);
  2044. break;
  2045. }
  2046. case DeRezAction.Delete:
  2047. {
  2048. if (Permissions.CanDeleteObject(grp, remoteClient))
  2049. {
  2050. if(m_useTrashOnDelete || (sp.IsGod && grp.OwnerID != sp.UUID))
  2051. takeDeleteGroups.Add(grp);
  2052. else
  2053. deleteGroups.Add(grp);
  2054. }
  2055. break;
  2056. }
  2057. case DeRezAction.Return:
  2058. {
  2059. if (remoteClient != null)
  2060. {
  2061. if (Permissions.CanReturnObjects( null, remoteClient, new List<SceneObjectGroup>() {grp}))
  2062. {
  2063. takeDeleteGroups.Add(grp);
  2064. if (AddToReturns)
  2065. AddReturn(grp.OwnerID == grp.GroupID ? grp.LastOwnerID : grp.OwnerID, grp.Name, grp.AbsolutePosition,
  2066. "parcel owner return");
  2067. }
  2068. }
  2069. else // Auto return passes through here with null agent
  2070. {
  2071. takeDeleteGroups.Add(grp);
  2072. }
  2073. break;
  2074. }
  2075. default:
  2076. break;
  2077. }
  2078. }
  2079. if(deleteIDs.Count > 0)
  2080. SendKillObject(deleteIDs);
  2081. if (takeDeleteGroups.Count > 0)
  2082. {
  2083. m_asyncSceneObjectDeleter.DeleteToInventory(action, destinationID, takeDeleteGroups,
  2084. remoteClient, true);
  2085. }
  2086. if (takeCopyGroups.Count > 0)
  2087. {
  2088. m_asyncSceneObjectDeleter.DeleteToInventory(action, destinationID, takeCopyGroups,
  2089. remoteClient, false);
  2090. }
  2091. if (deleteGroups.Count > 0)
  2092. {
  2093. foreach (SceneObjectGroup g in deleteGroups)
  2094. DeleteSceneObject(g, false);
  2095. }
  2096. }
  2097. public UUID attachObjectAssetStore(IClientAPI remoteClient, SceneObjectGroup grp, UUID AgentId, out UUID itemID)
  2098. {
  2099. itemID = UUID.Zero;
  2100. if (grp != null)
  2101. {
  2102. Vector3 inventoryStoredPosition = new Vector3(
  2103. Math.Min(grp.AbsolutePosition.X, RegionInfo.RegionSizeX - 6),
  2104. Math.Min(grp.AbsolutePosition.Y, RegionInfo.RegionSizeY - 6),
  2105. grp.AbsolutePosition.Z);
  2106. Vector3 originalPosition = grp.AbsolutePosition;
  2107. grp.AbsolutePosition = inventoryStoredPosition;
  2108. string sceneObjectXml = SceneObjectSerializer.ToOriginalXmlFormat(grp);
  2109. grp.AbsolutePosition = originalPosition;
  2110. AssetBase asset = CreateAsset(
  2111. grp.GetPartName(grp.LocalId),
  2112. grp.GetPartDescription(grp.LocalId),
  2113. (sbyte)AssetType.Object,
  2114. Utils.StringToBytes(sceneObjectXml),
  2115. remoteClient.AgentId);
  2116. AssetService.Store(asset);
  2117. InventoryItemBase item = new InventoryItemBase();
  2118. item.CreatorId = grp.RootPart.CreatorID.ToString();
  2119. item.CreatorData = grp.RootPart.CreatorData;
  2120. item.Owner = remoteClient.AgentId;
  2121. item.ID = UUID.Random();
  2122. item.AssetID = asset.FullID;
  2123. item.Description = asset.Description;
  2124. item.Name = asset.Name;
  2125. item.AssetType = asset.Type;
  2126. item.InvType = (int)InventoryType.Object;
  2127. InventoryFolderBase folder = InventoryService.GetFolderForType(remoteClient.AgentId, FolderType.Object);
  2128. if (folder != null)
  2129. item.Folder = folder.ID;
  2130. else // oopsies
  2131. item.Folder = UUID.Zero;
  2132. // Set up base perms properly
  2133. uint permsBase = (uint)(PermissionMask.Move | PermissionMask.Copy | PermissionMask.Transfer | PermissionMask.Modify);
  2134. permsBase &= grp.RootPart.BaseMask;
  2135. permsBase |= (uint)PermissionMask.Move;
  2136. // Make sure we don't lock it
  2137. grp.RootPart.NextOwnerMask |= (uint)PermissionMask.Move;
  2138. if ((remoteClient.AgentId != grp.RootPart.OwnerID) && Permissions.PropagatePermissions())
  2139. {
  2140. item.BasePermissions = permsBase & grp.RootPart.NextOwnerMask;
  2141. item.CurrentPermissions = permsBase & grp.RootPart.NextOwnerMask;
  2142. item.NextPermissions = permsBase & grp.RootPart.NextOwnerMask;
  2143. item.EveryOnePermissions = permsBase & grp.RootPart.EveryoneMask & grp.RootPart.NextOwnerMask;
  2144. item.GroupPermissions = permsBase & grp.RootPart.GroupMask & grp.RootPart.NextOwnerMask;
  2145. }
  2146. else
  2147. {
  2148. item.BasePermissions = permsBase;
  2149. item.CurrentPermissions = permsBase & grp.RootPart.OwnerMask;
  2150. item.NextPermissions = permsBase & grp.RootPart.NextOwnerMask;
  2151. item.EveryOnePermissions = permsBase & grp.RootPart.EveryoneMask;
  2152. item.GroupPermissions = permsBase & grp.RootPart.GroupMask;
  2153. }
  2154. item.CreationDate = Util.UnixTimeSinceEpoch();
  2155. // sets itemID so client can show item as 'attached' in inventory
  2156. grp.FromItemID = item.ID;
  2157. if (AddInventoryItem(item))
  2158. remoteClient.SendInventoryItemCreateUpdate(item, 0);
  2159. else
  2160. m_dialogModule.SendAlertToUser(remoteClient, "Operation failed");
  2161. itemID = item.ID;
  2162. return item.AssetID;
  2163. }
  2164. return UUID.Zero;
  2165. }
  2166. /// <summary>
  2167. /// Returns the list of Scene Objects in an asset.
  2168. /// </summary>
  2169. /// <remarks>
  2170. /// Returns one object if the asset is a regular object, and multiple objects for a coalesced object.
  2171. /// </remarks>
  2172. /// <param name="assetData">Asset data</param>
  2173. /// <param name="isAttachment">True if the object is an attachment.</param>
  2174. /// <param name="objlist">The objects included in the asset</param>
  2175. /// <param name="veclist">Relative positions of the objects</param>
  2176. /// <param name="bbox">Bounding box of all the objects</param>
  2177. /// <param name="offsetHeight">Offset in the Z axis from the centre of the bounding box
  2178. /// to the centre of the root prim (relevant only when returning a single object)</param>
  2179. /// <returns>
  2180. /// true if returning a single object or deserialization fails, false if returning the coalesced
  2181. /// list of objects
  2182. /// </returns>
  2183. public bool GetObjectsToRez(
  2184. byte[] assetData, bool isAttachment, out List<SceneObjectGroup> objlist, out List<Vector3> veclist,
  2185. out Vector3 bbox, out float offsetHeight)
  2186. {
  2187. objlist = new List<SceneObjectGroup>();
  2188. veclist = new List<Vector3>();
  2189. bbox = Vector3.Zero;
  2190. offsetHeight = 0;
  2191. string xmlData = ExternalRepresentationUtils.SanitizeXml(Utils.BytesToString(assetData));
  2192. try
  2193. {
  2194. using (XmlTextReader wrappedReader = new XmlTextReader(xmlData, XmlNodeType.Element, null))
  2195. {
  2196. using (XmlReader reader = XmlReader.Create(wrappedReader, new XmlReaderSettings() { IgnoreWhitespace = true, ConformanceLevel = ConformanceLevel.Fragment}))
  2197. {
  2198. reader.Read();
  2199. bool isSingleObject = reader.Name != "CoalescedObject";
  2200. if (isSingleObject || isAttachment)
  2201. {
  2202. SceneObjectGroup g;
  2203. try
  2204. {
  2205. g = SceneObjectSerializer.FromOriginalXmlFormat(reader);
  2206. }
  2207. catch (Exception e)
  2208. {
  2209. m_log.Error("[AGENT INVENTORY]: Deserialization of xml failed ", e);
  2210. Util.LogFailedXML("[AGENT INVENTORY]:", xmlData);
  2211. g = null;
  2212. }
  2213. if (g != null)
  2214. {
  2215. objlist.Add(g);
  2216. veclist.Add(Vector3.Zero);
  2217. bbox = g.GetAxisAlignedBoundingBox(out offsetHeight);
  2218. }
  2219. return true;
  2220. }
  2221. else
  2222. {
  2223. XmlDocument doc = new XmlDocument();
  2224. doc.LoadXml(xmlData);
  2225. XmlElement e = (XmlElement)doc.SelectSingleNode("/CoalescedObject");
  2226. XmlElement coll = (XmlElement)e;
  2227. float bx = Convert.ToSingle(coll.GetAttribute("x"));
  2228. float by = Convert.ToSingle(coll.GetAttribute("y"));
  2229. float bz = Convert.ToSingle(coll.GetAttribute("z"));
  2230. bbox = new Vector3(bx, by, bz);
  2231. offsetHeight = 0;
  2232. XmlNodeList groups = e.SelectNodes("SceneObjectGroup");
  2233. foreach (XmlNode n in groups)
  2234. {
  2235. SceneObjectGroup g = SceneObjectSerializer.FromOriginalXmlFormat(n.OuterXml);
  2236. if (g != null)
  2237. {
  2238. objlist.Add(g);
  2239. XmlElement el = (XmlElement)n;
  2240. string rawX = el.GetAttribute("offsetx");
  2241. string rawY = el.GetAttribute("offsety");
  2242. string rawZ = el.GetAttribute("offsetz");
  2243. float x = Convert.ToSingle(rawX);
  2244. float y = Convert.ToSingle(rawY);
  2245. float z = Convert.ToSingle(rawZ);
  2246. veclist.Add(new Vector3(x, y, z));
  2247. }
  2248. }
  2249. return false;
  2250. }
  2251. }
  2252. }
  2253. }
  2254. catch (Exception e)
  2255. {
  2256. m_log.Error("[AGENT INVENTORY]: Deserialization of xml failed when looking for CoalescedObject tag ", e);
  2257. Util.LogFailedXML("[AGENT INVENTORY]:", xmlData);
  2258. }
  2259. return true;
  2260. }
  2261. /// <summary>
  2262. /// Event Handler Rez an object into a scene
  2263. /// Calls the non-void event handler
  2264. /// </summary>
  2265. /// <param name="remoteClient"></param>
  2266. /// <param name="itemID"></param>
  2267. /// <param name="rezGroupID"></param>
  2268. /// <param name="RayEnd"></param>
  2269. /// <param name="RayStart"></param>
  2270. /// <param name="RayTargetID"></param>
  2271. /// <param name="BypassRayCast"></param>
  2272. /// <param name="RayEndIsIntersection"></param>
  2273. /// <param name="EveryoneMask"></param>
  2274. /// <param name="GroupMask"></param>
  2275. /// <param name="RezSelected"></param>
  2276. /// <param name="RemoveItem"></param>
  2277. /// <param name="fromTaskID"></param>
  2278. public virtual void RezObject(IClientAPI remoteClient, UUID itemID, UUID rezGroupID,
  2279. Vector3 RayEnd, Vector3 RayStart,
  2280. UUID RayTargetID, byte BypassRayCast, bool RayEndIsIntersection,
  2281. bool RezSelected, bool RemoveItem, UUID fromTaskID)
  2282. {
  2283. // m_log.DebugFormat(
  2284. // "[PRIM INVENTORY]: RezObject from {0} for item {1} from task id {2}",
  2285. // remoteClient.Name, itemID, fromTaskID);
  2286. if (fromTaskID == UUID.Zero)
  2287. {
  2288. IInventoryAccessModule invAccess = RequestModuleInterface<IInventoryAccessModule>();
  2289. if (invAccess != null)
  2290. invAccess.RezObject(
  2291. remoteClient, itemID, rezGroupID, RayEnd, RayStart, RayTargetID, BypassRayCast, RayEndIsIntersection,
  2292. RezSelected, RemoveItem, fromTaskID, false);
  2293. }
  2294. else
  2295. {
  2296. SceneObjectPart part = GetSceneObjectPart(fromTaskID);
  2297. if (part == null)
  2298. {
  2299. m_log.ErrorFormat(
  2300. "[TASK INVENTORY]: {0} tried to rez item id {1} from object id {2} but there is no such scene object",
  2301. remoteClient.Name, itemID, fromTaskID);
  2302. return;
  2303. }
  2304. TaskInventoryItem item = part.Inventory.GetInventoryItem(itemID);
  2305. if (item == null)
  2306. {
  2307. m_log.ErrorFormat(
  2308. "[TASK INVENTORY]: {0} tried to rez item id {1} from object id {2} but there is no such item",
  2309. remoteClient.Name, itemID, fromTaskID);
  2310. return;
  2311. }
  2312. byte bRayEndIsIntersection = (byte)(RayEndIsIntersection ? 1 : 0);
  2313. Vector3 scale = new Vector3(0.5f, 0.5f, 0.5f);
  2314. Vector3 pos = GetNewRezLocation(
  2315. RayStart, RayEnd, RayTargetID, Quaternion.Identity,
  2316. BypassRayCast, bRayEndIsIntersection, true, scale, false);
  2317. RezObject(part, item, pos, null, Vector3.Zero, 0, false);
  2318. }
  2319. }
  2320. /// <summary>
  2321. /// Rez an object into the scene from a prim's inventory.
  2322. /// </summary>
  2323. /// <param name="sourcePart"></param>
  2324. /// <param name="item"></param>
  2325. /// <param name="pos">The position of the rezzed object.</param>
  2326. /// <param name="rot">The rotation of the rezzed object. If null, then the rotation stored with the object
  2327. /// will be used if it exists.</param>
  2328. /// <param name="vel">The velocity of the rezzed object.</param>
  2329. /// <param name="param"></param>
  2330. /// <returns>The SceneObjectGroup(s) rezzed, or null if rez was unsuccessful</returns>
  2331. public virtual List<SceneObjectGroup> RezObject(SceneObjectPart sourcePart, TaskInventoryItem item,
  2332. Vector3 pos, Quaternion? rot, Vector3 vel, int param, bool atRoot, bool rezSelected = false)
  2333. {
  2334. if (null == item)
  2335. return null;
  2336. List<SceneObjectGroup> objlist;
  2337. List<Vector3> veclist;
  2338. Vector3 bbox;
  2339. float offsetHeight;
  2340. bool success = sourcePart.Inventory.GetRezReadySceneObjects(item, out objlist, out veclist,out bbox, out offsetHeight);
  2341. if (!success)
  2342. return null;
  2343. int totalPrims = 0;
  2344. foreach (SceneObjectGroup group in objlist)
  2345. totalPrims += group.PrimCount;
  2346. if (!Permissions.CanRezObject(totalPrims, item.OwnerID, pos))
  2347. return null;
  2348. if (!Permissions.BypassPermissions())
  2349. {
  2350. if ((item.CurrentPermissions & (uint)PermissionMask.Copy) == 0)
  2351. sourcePart.Inventory.RemoveInventoryItem(item.ItemID);
  2352. }
  2353. SceneObjectGroup sog;
  2354. bool fixrot = false;
  2355. Quaternion netRot = Quaternion.Identity;
  2356. // position adjust
  2357. if (totalPrims > 1) // nothing to do on a single prim
  2358. {
  2359. if (objlist.Count == 1)
  2360. {
  2361. // current object position is root position
  2362. if(!atRoot)
  2363. {
  2364. sog = objlist[0];
  2365. Quaternion orot;
  2366. if (rot == null)
  2367. orot = sog.RootPart.GetWorldRotation();
  2368. else
  2369. orot = rot.Value;
  2370. // possible should be bbox, but geometric center looks better
  2371. Vector3 off = sog.GetGeometricCenter();
  2372. // Vector3 off = bbox * 0.5f;
  2373. off *= orot;
  2374. pos -= off;
  2375. }
  2376. }
  2377. else
  2378. {
  2379. //veclist[] are relative to bbox corner with min X,Y and Z
  2380. // rez at root, and rot will be referenced to first object in list
  2381. if (rot == null)
  2382. {
  2383. // use original rotations
  2384. if (atRoot)
  2385. pos -= veclist[0];
  2386. else
  2387. pos -= bbox / 2;
  2388. }
  2389. else
  2390. {
  2391. fixrot = true;
  2392. sog = objlist[0];
  2393. netRot = Quaternion.Conjugate(sog.RootPart.GetWorldRotation());
  2394. netRot = netRot * rot.Value;
  2395. Vector3 off;
  2396. if (atRoot)
  2397. off = veclist[0];
  2398. else
  2399. off = bbox / 2;
  2400. off *= netRot;
  2401. pos -= off;
  2402. }
  2403. }
  2404. }
  2405. for (int i = 0; i < objlist.Count; i++)
  2406. {
  2407. SceneObjectGroup group = objlist[i];
  2408. Vector3 curpos;
  2409. if(fixrot)
  2410. curpos = pos + veclist[i] * netRot;
  2411. else
  2412. curpos = pos + veclist[i];
  2413. if (group.IsAttachment == false && group.RootPart.Shape.State != 0)
  2414. {
  2415. group.RootPart.AttachedPos = group.AbsolutePosition;
  2416. group.RootPart.Shape.LastAttachPoint = (byte)group.AttachmentPoint;
  2417. }
  2418. group.RezzerID = sourcePart.UUID;
  2419. if (rezSelected)
  2420. {
  2421. group.IsSelected = true;
  2422. group.RootPart.CreateSelected = true;
  2423. }
  2424. if ( i == 0)
  2425. AddNewSceneObject(group, true, curpos, rot, vel);
  2426. else
  2427. {
  2428. Quaternion crot = objlist[i].RootPart.GetWorldRotation();
  2429. if (fixrot)
  2430. {
  2431. crot *= netRot;
  2432. }
  2433. AddNewSceneObject(group, true, curpos, crot, vel);
  2434. }
  2435. // We can only call this after adding the scene object, since the scene object references the scene
  2436. // to find out if scripts should be activated at all.
  2437. group.InvalidateEffectivePerms();
  2438. group.CreateScriptInstances(param, true, DefaultScriptEngine, 3);
  2439. group.ScheduleGroupForFullUpdate();
  2440. }
  2441. return objlist;
  2442. }
  2443. public virtual bool returnObjects(SceneObjectGroup[] returnobjects,
  2444. IClientAPI client)
  2445. {
  2446. List<uint> localIDs = new List<uint>();
  2447. foreach (SceneObjectGroup grp in returnobjects)
  2448. {
  2449. AddReturn(grp.OwnerID, grp.Name, grp.AbsolutePosition,
  2450. "parcel owner return");
  2451. localIDs.Add(grp.RootPart.LocalId);
  2452. }
  2453. DeRezObjects(client, localIDs, UUID.Zero, DeRezAction.Return,
  2454. UUID.Zero, false);
  2455. return true;
  2456. }
  2457. public void SetScriptRunning(IClientAPI controllingClient, UUID objectID, UUID itemID, bool running)
  2458. {
  2459. if (!Permissions.CanEditScript(itemID, objectID, controllingClient.AgentId))
  2460. return;
  2461. SceneObjectPart part = GetSceneObjectPart(objectID);
  2462. if (part == null)
  2463. return;
  2464. if (running)
  2465. EventManager.TriggerStartScript(part.LocalId, itemID);
  2466. else
  2467. EventManager.TriggerStopScript(part.LocalId, itemID);
  2468. }
  2469. public void GetScriptRunning(IClientAPI controllingClient, UUID objectID, UUID itemID)
  2470. {
  2471. EventManager.TriggerGetScriptRunning(controllingClient, objectID, itemID);
  2472. }
  2473. void ObjectOwner(IClientAPI remoteClient, UUID ownerID, UUID groupID, List<uint> localIDs)
  2474. {
  2475. if (!Permissions.IsGod(remoteClient.AgentId))
  2476. {
  2477. if (ownerID != UUID.Zero)
  2478. return;
  2479. }
  2480. List<SceneObjectGroup> groups = new List<SceneObjectGroup>();
  2481. foreach (uint localID in localIDs)
  2482. {
  2483. SceneObjectPart part = GetSceneObjectPart(localID);
  2484. if (part == null)
  2485. continue;
  2486. if (!groups.Contains(part.ParentGroup))
  2487. groups.Add(part.ParentGroup);
  2488. }
  2489. foreach (SceneObjectGroup sog in groups)
  2490. {
  2491. if (ownerID != UUID.Zero)
  2492. {
  2493. sog.SetOwnerId(ownerID);
  2494. sog.SetGroup(groupID, remoteClient);
  2495. sog.ScheduleGroupForFullUpdate();
  2496. SceneObjectPart[] partList = sog.Parts;
  2497. foreach (SceneObjectPart child in partList)
  2498. {
  2499. child.Inventory.ChangeInventoryOwner(ownerID);
  2500. child.TriggerScriptChangedEvent(Changed.OWNER);
  2501. }
  2502. }
  2503. else // The object deeded to the group
  2504. {
  2505. if (!Permissions.CanDeedObject(remoteClient, sog, groupID))
  2506. continue;
  2507. sog.SetOwnerId(groupID);
  2508. // this is wrong, GroupMask is used for group sharing, still possible to set
  2509. // this whould give owner rights to users that are member of group but don't have role powers to edit
  2510. // sog.RootPart.GroupMask = sog.RootPart.OwnerMask;
  2511. // we should keep all permissions on deed to group
  2512. // and with this comented code, if user does not set next permissions on the object
  2513. // and on ALL contents of ALL prims, he may loose rights, making the object useless
  2514. sog.ApplyNextOwnerPermissions();
  2515. sog.InvalidateEffectivePerms();
  2516. sog.ScheduleGroupForFullUpdate();
  2517. SceneObjectPart[] partList = sog.Parts;
  2518. foreach (SceneObjectPart child in partList)
  2519. {
  2520. child.Inventory.ChangeInventoryOwner(groupID);
  2521. child.TriggerScriptChangedEvent(Changed.OWNER);
  2522. }
  2523. }
  2524. }
  2525. foreach (uint localID in localIDs)
  2526. {
  2527. SceneObjectPart part = GetSceneObjectPart(localID);
  2528. if (part == null)
  2529. continue;
  2530. part.SendPropertiesToClient(remoteClient);
  2531. }
  2532. }
  2533. public void DelinkObjects(List<uint> primIds, IClientAPI client)
  2534. {
  2535. List<SceneObjectPart> parts = new List<SceneObjectPart>();
  2536. foreach (uint localID in primIds)
  2537. {
  2538. SceneObjectPart part = GetSceneObjectPart(localID);
  2539. if (part == null)
  2540. continue;
  2541. if (Permissions.CanDelinkObject(client.AgentId, part.ParentGroup.RootPart.UUID))
  2542. parts.Add(part);
  2543. }
  2544. m_sceneGraph.DelinkObjects(parts);
  2545. }
  2546. /// <summary>
  2547. /// Link the scene objects containing the indicated parts to a root object.
  2548. /// </summary>
  2549. /// <param name="client"></param>
  2550. /// <param name="parentPrimId">A root prim id of the object which will be the root prim of the resulting linkset.</param>
  2551. /// <param name="childPrimIds">A list of child prims for the objects that should be linked in.</param>
  2552. public void LinkObjects(IClientAPI client, uint parentPrimId, List<uint> childPrimIds)
  2553. {
  2554. LinkObjects(client.AgentId, parentPrimId, childPrimIds);
  2555. }
  2556. /// <summary>
  2557. /// Link the scene objects containing the indicated parts to a root object.
  2558. /// </summary>
  2559. /// <param name="agentId">The ID of the user linking.</param>
  2560. /// <param name="parentPrimId">A root prim id of the object which will be the root prim of the resulting linkset.</param>
  2561. /// <param name="childPrimIds">A list of child prims for the objects that should be linked in.</param>
  2562. public void LinkObjects(UUID agentId, uint parentPrimId, List<uint> childPrimIds)
  2563. {
  2564. List<UUID> owners = new List<UUID>();
  2565. List<SceneObjectPart> children = new List<SceneObjectPart>();
  2566. SceneObjectPart root = GetSceneObjectPart(parentPrimId);
  2567. if (root == null)
  2568. {
  2569. m_log.DebugFormat("[LINK]: Can't find linkset root prim {0}", parentPrimId);
  2570. return;
  2571. }
  2572. if (!Permissions.CanLinkObject(agentId, root.ParentGroup.RootPart.UUID))
  2573. {
  2574. m_log.DebugFormat("[LINK]: Refusing link. No permissions on root prim");
  2575. return;
  2576. }
  2577. foreach (uint localID in childPrimIds)
  2578. {
  2579. SceneObjectPart part = GetSceneObjectPart(localID);
  2580. if (part == null)
  2581. continue;
  2582. if (!owners.Contains(part.OwnerID))
  2583. owners.Add(part.OwnerID);
  2584. if (Permissions.CanLinkObject(agentId, part.ParentGroup.RootPart.UUID))
  2585. children.Add(part);
  2586. }
  2587. // Must be all one owner
  2588. //
  2589. if (owners.Count > 1)
  2590. {
  2591. m_log.DebugFormat("[LINK]: Refusing link. Too many owners");
  2592. return;
  2593. }
  2594. if (children.Count == 0)
  2595. {
  2596. m_log.DebugFormat("[LINK]: Refusing link. No permissions to link any of the children");
  2597. return;
  2598. }
  2599. bool oldUsePhysics = (root.Flags & PrimFlags.Physics) != 0;
  2600. m_sceneGraph.LinkObjects(root, children);
  2601. ScenePresence sp;
  2602. if (TryGetScenePresence(agentId, out sp))
  2603. {
  2604. root.SendPropertiesToClient(sp.ControllingClient);
  2605. if (oldUsePhysics && (root.Flags & PrimFlags.Physics) == 0)
  2606. {
  2607. sp.ControllingClient.SendAlertMessage("Object physics cancelled");
  2608. }
  2609. }
  2610. }
  2611. private string PermissionString(uint permissions)
  2612. {
  2613. PermissionMask perms = (PermissionMask)permissions &
  2614. (PermissionMask.Move |
  2615. PermissionMask.Copy |
  2616. PermissionMask.Transfer |
  2617. PermissionMask.Modify);
  2618. return perms.ToString();
  2619. }
  2620. }
  2621. }