1
0

Scene.Inventory.cs 93 KB

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