Scene.Inventory.cs 93 KB

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