Scene.Inventory.cs 110 KB

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