HGInventoryAccessModule.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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.Reflection;
  30. using OpenSim.Framework;
  31. using OpenSim.Framework.Client;
  32. using OpenSim.Region.Framework.Interfaces;
  33. using OpenSim.Region.Framework.Scenes;
  34. using OpenSim.Services.Connectors.Hypergrid;
  35. using OpenSim.Services.Interfaces;
  36. using OpenSim.Server.Base;
  37. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  38. using OpenMetaverse;
  39. using log4net;
  40. using Nini.Config;
  41. namespace OpenSim.Region.CoreModules.Framework.InventoryAccess
  42. {
  43. public class HGInventoryAccessModule : BasicInventoryAccessModule, INonSharedRegionModule, IInventoryAccessModule
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. private static HGAssetMapper m_assMapper;
  47. public static HGAssetMapper AssetMapper
  48. {
  49. get { return m_assMapper; }
  50. }
  51. private string m_HomeURI;
  52. private bool m_OutboundPermission;
  53. private string m_ThisGatekeeper;
  54. private bool m_RestrictInventoryAccessAbroad;
  55. // private bool m_Initialized = false;
  56. #region INonSharedRegionModule
  57. public override string Name
  58. {
  59. get { return "HGInventoryAccessModule"; }
  60. }
  61. public override void Initialise(IConfigSource source)
  62. {
  63. IConfig moduleConfig = source.Configs["Modules"];
  64. if (moduleConfig != null)
  65. {
  66. string name = moduleConfig.GetString("InventoryAccessModule", "");
  67. if (name == Name)
  68. {
  69. m_Enabled = true;
  70. InitialiseCommon(source);
  71. m_log.InfoFormat("[HG INVENTORY ACCESS MODULE]: {0} enabled.", Name);
  72. IConfig thisModuleConfig = source.Configs["HGInventoryAccessModule"];
  73. if (thisModuleConfig != null)
  74. {
  75. // legacy configuration [obsolete]
  76. m_HomeURI = thisModuleConfig.GetString("ProfileServerURI", string.Empty);
  77. // preferred
  78. m_HomeURI = thisModuleConfig.GetString("HomeURI", m_HomeURI);
  79. m_OutboundPermission = thisModuleConfig.GetBoolean("OutboundPermission", true);
  80. m_ThisGatekeeper = thisModuleConfig.GetString("Gatekeeper", string.Empty);
  81. m_RestrictInventoryAccessAbroad = thisModuleConfig.GetBoolean("RestrictInventoryAccessAbroad", true);
  82. }
  83. else
  84. m_log.Warn("[HG INVENTORY ACCESS MODULE]: HGInventoryAccessModule configs not found. ProfileServerURI not set!");
  85. }
  86. }
  87. }
  88. public override void AddRegion(Scene scene)
  89. {
  90. if (!m_Enabled)
  91. return;
  92. base.AddRegion(scene);
  93. m_assMapper = new HGAssetMapper(scene, m_HomeURI);
  94. scene.EventManager.OnNewInventoryItemUploadComplete += UploadInventoryItem;
  95. scene.EventManager.OnTeleportStart += TeleportStart;
  96. scene.EventManager.OnTeleportFail += TeleportFail;
  97. }
  98. #endregion
  99. #region Event handlers
  100. protected override void OnNewClient(IClientAPI client)
  101. {
  102. base.OnNewClient(client);
  103. client.OnCompleteMovementToRegion += new Action<IClientAPI, bool>(OnCompleteMovementToRegion);
  104. }
  105. protected void OnCompleteMovementToRegion(IClientAPI client, bool arg2)
  106. {
  107. //m_log.DebugFormat("[HG INVENTORY ACCESS MODULE]: OnCompleteMovementToRegion of user {0}", client.Name);
  108. object sp = null;
  109. if (client.Scene.TryGetScenePresence(client.AgentId, out sp))
  110. {
  111. if (sp is ScenePresence)
  112. {
  113. AgentCircuitData aCircuit = ((ScenePresence)sp).Scene.AuthenticateHandler.GetAgentCircuitData(client.AgentId);
  114. if ((aCircuit.teleportFlags & (uint)Constants.TeleportFlags.ViaHGLogin) != 0)
  115. {
  116. if (m_RestrictInventoryAccessAbroad)
  117. {
  118. IUserManagement uMan = m_Scene.RequestModuleInterface<IUserManagement>();
  119. if (uMan.IsLocalGridUser(client.AgentId))
  120. ProcessInventoryForComingHome(client);
  121. else
  122. ProcessInventoryForArriving(client);
  123. }
  124. }
  125. }
  126. }
  127. }
  128. protected void TeleportStart(IClientAPI client, GridRegion destination, GridRegion finalDestination, uint teleportFlags, bool gridLogout)
  129. {
  130. if (gridLogout && m_RestrictInventoryAccessAbroad)
  131. {
  132. IUserManagement uMan = m_Scene.RequestModuleInterface<IUserManagement>();
  133. if (uMan != null && uMan.IsLocalGridUser(client.AgentId))
  134. {
  135. // local grid user
  136. ProcessInventoryForHypergriding(client);
  137. }
  138. else
  139. {
  140. // Foreigner
  141. ProcessInventoryForLeaving(client);
  142. }
  143. }
  144. }
  145. protected void TeleportFail(IClientAPI client, bool gridLogout)
  146. {
  147. if (gridLogout && m_RestrictInventoryAccessAbroad)
  148. {
  149. IUserManagement uMan = m_Scene.RequestModuleInterface<IUserManagement>();
  150. if (uMan.IsLocalGridUser(client.AgentId))
  151. {
  152. ProcessInventoryForComingHome(client);
  153. }
  154. else
  155. {
  156. ProcessInventoryForArriving(client);
  157. }
  158. }
  159. }
  160. public void UploadInventoryItem(UUID avatarID, UUID assetID, string name, int userlevel)
  161. {
  162. string userAssetServer = string.Empty;
  163. if (IsForeignUser(avatarID, out userAssetServer) && userAssetServer != string.Empty && m_OutboundPermission)
  164. {
  165. m_assMapper.Post(assetID, avatarID, userAssetServer);
  166. }
  167. }
  168. #endregion
  169. #region Overrides of Basic Inventory Access methods
  170. protected override string GenerateLandmark(ScenePresence presence, out string prefix, out string suffix)
  171. {
  172. if (UserManagementModule != null && !UserManagementModule.IsLocalGridUser(presence.UUID))
  173. prefix = "HG ";
  174. else
  175. prefix = string.Empty;
  176. suffix = " @ " + m_ThisGatekeeper;
  177. Vector3 pos = presence.AbsolutePosition;
  178. return String.Format("Landmark version 2\nregion_id {0}\nlocal_pos {1} {2} {3}\nregion_handle {4}\ngatekeeper {5}\n",
  179. presence.Scene.RegionInfo.RegionID,
  180. pos.X, pos.Y, pos.Z,
  181. presence.RegionHandle,
  182. m_ThisGatekeeper);
  183. }
  184. ///
  185. /// CapsUpdateInventoryItemAsset
  186. ///
  187. public override UUID CapsUpdateInventoryItemAsset(IClientAPI remoteClient, UUID itemID, byte[] data)
  188. {
  189. UUID newAssetID = base.CapsUpdateInventoryItemAsset(remoteClient, itemID, data);
  190. UploadInventoryItem(remoteClient.AgentId, newAssetID, "", 0);
  191. return newAssetID;
  192. }
  193. ///
  194. /// Used in DeleteToInventory
  195. ///
  196. protected override void ExportAsset(UUID agentID, UUID assetID)
  197. {
  198. if (!assetID.Equals(UUID.Zero))
  199. UploadInventoryItem(agentID, assetID, "", 0);
  200. else
  201. m_log.Debug("[HGScene]: Scene.Inventory did not create asset");
  202. }
  203. ///
  204. /// RezObject
  205. ///
  206. public override SceneObjectGroup RezObject(IClientAPI remoteClient, UUID itemID, Vector3 RayEnd, Vector3 RayStart,
  207. UUID RayTargetID, byte BypassRayCast, bool RayEndIsIntersection,
  208. bool RezSelected, bool RemoveItem, UUID fromTaskID, bool attachment)
  209. {
  210. m_log.DebugFormat("[HGScene] RezObject itemID={0} fromTaskID={1}", itemID, fromTaskID);
  211. //if (fromTaskID.Equals(UUID.Zero))
  212. //{
  213. InventoryItemBase item = new InventoryItemBase(itemID);
  214. item.Owner = remoteClient.AgentId;
  215. item = m_Scene.InventoryService.GetItem(item);
  216. //if (item == null)
  217. //{ // Fetch the item
  218. // item = new InventoryItemBase();
  219. // item.Owner = remoteClient.AgentId;
  220. // item.ID = itemID;
  221. // item = m_assMapper.Get(item, userInfo.RootFolder.ID, userInfo);
  222. //}
  223. string userAssetServer = string.Empty;
  224. if (item != null && IsForeignUser(remoteClient.AgentId, out userAssetServer))
  225. {
  226. m_assMapper.Get(item.AssetID, remoteClient.AgentId, userAssetServer);
  227. }
  228. //}
  229. // OK, we're done fetching. Pass it up to the default RezObject
  230. SceneObjectGroup sog = base.RezObject(remoteClient, itemID, RayEnd, RayStart, RayTargetID, BypassRayCast, RayEndIsIntersection,
  231. RezSelected, RemoveItem, fromTaskID, attachment);
  232. if (sog == null)
  233. remoteClient.SendAgentAlertMessage("Unable to rez: problem accessing inventory or locating assets", false);
  234. return sog;
  235. }
  236. public override void TransferInventoryAssets(InventoryItemBase item, UUID sender, UUID receiver)
  237. {
  238. string userAssetServer = string.Empty;
  239. if (IsForeignUser(sender, out userAssetServer) && userAssetServer != string.Empty)
  240. m_assMapper.Get(item.AssetID, sender, userAssetServer);
  241. if (IsForeignUser(receiver, out userAssetServer) && userAssetServer != string.Empty && m_OutboundPermission)
  242. m_assMapper.Post(item.AssetID, receiver, userAssetServer);
  243. }
  244. public override bool IsForeignUser(UUID userID, out string assetServerURL)
  245. {
  246. assetServerURL = string.Empty;
  247. if (UserManagementModule != null && !UserManagementModule.IsLocalGridUser(userID))
  248. { // foreign
  249. ScenePresence sp = null;
  250. if (m_Scene.TryGetScenePresence(userID, out sp))
  251. {
  252. AgentCircuitData aCircuit = m_Scene.AuthenticateHandler.GetAgentCircuitData(sp.ControllingClient.CircuitCode);
  253. if (aCircuit.ServiceURLs.ContainsKey("AssetServerURI"))
  254. {
  255. assetServerURL = aCircuit.ServiceURLs["AssetServerURI"].ToString();
  256. assetServerURL = assetServerURL.Trim(new char[] { '/' });
  257. }
  258. }
  259. else
  260. {
  261. assetServerURL = UserManagementModule.GetUserServerURL(userID, "AssetServerURI");
  262. assetServerURL = assetServerURL.Trim(new char[] { '/' });
  263. }
  264. return true;
  265. }
  266. return false;
  267. }
  268. protected override InventoryItemBase GetItem(UUID agentID, UUID itemID)
  269. {
  270. InventoryItemBase item = base.GetItem(agentID, itemID);
  271. if (item == null)
  272. return null;
  273. string userAssetServer = string.Empty;
  274. if (IsForeignUser(agentID, out userAssetServer))
  275. m_assMapper.Get(item.AssetID, agentID, userAssetServer);
  276. return item;
  277. }
  278. #endregion
  279. #region Inventory manipulation upon arriving/leaving
  280. //
  281. // These 2 are for local and foreign users coming back, respectively
  282. //
  283. private void ProcessInventoryForComingHome(IClientAPI client)
  284. {
  285. m_log.DebugFormat("[HG INVENTORY ACCESS MODULE]: Restoring root folder for local user {0}", client.Name);
  286. if (client is IClientCore)
  287. {
  288. IClientCore core = (IClientCore)client;
  289. IClientInventory inv;
  290. if (core.TryGet<IClientInventory>(out inv))
  291. {
  292. InventoryFolderBase root = m_Scene.InventoryService.GetRootFolder(client.AgentId);
  293. InventoryCollection content = m_Scene.InventoryService.GetFolderContent(client.AgentId, root.ID);
  294. inv.SendBulkUpdateInventory(content.Folders.ToArray(), content.Items.ToArray());
  295. }
  296. }
  297. }
  298. private void ProcessInventoryForArriving(IClientAPI client)
  299. {
  300. // No-op for now, but we may need to do something for freign users inventory
  301. }
  302. //
  303. // These 2 are for local and foreign users going away respectively
  304. //
  305. private void ProcessInventoryForHypergriding(IClientAPI client)
  306. {
  307. if (client is IClientCore)
  308. {
  309. IClientCore core = (IClientCore)client;
  310. IClientInventory inv;
  311. if (core.TryGet<IClientInventory>(out inv))
  312. {
  313. InventoryFolderBase root = m_Scene.InventoryService.GetRootFolder(client.AgentId);
  314. if (root != null)
  315. {
  316. m_log.DebugFormat("[HG INVENTORY ACCESS MODULE]: Changing root inventory for user {0}", client.Name);
  317. InventoryCollection content = m_Scene.InventoryService.GetFolderContent(client.AgentId, root.ID);
  318. List<InventoryFolderBase> keep = new List<InventoryFolderBase>();
  319. foreach (InventoryFolderBase f in content.Folders)
  320. {
  321. if (f.Name != "My Suitcase")
  322. {
  323. f.Name = f.Name + " (Unavailable)";
  324. keep.Add(f);
  325. }
  326. }
  327. // items directly under the root folder
  328. foreach (InventoryItemBase it in content.Items)
  329. it.Name = it.Name + " (Unavailable)"; ;
  330. // Send the new names
  331. inv.SendBulkUpdateInventory(keep.ToArray(), content.Items.ToArray());
  332. }
  333. }
  334. }
  335. }
  336. private void ProcessInventoryForLeaving(IClientAPI client)
  337. {
  338. // No-op for now
  339. }
  340. #endregion
  341. }
  342. }