Scene.Inventory.cs 114 KB

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