Scene.Inventory.cs 94 KB

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