HGEntityTransferModule.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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. using Mono.Addins;
  42. namespace OpenSim.Region.CoreModules.Framework.EntityTransfer
  43. {
  44. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "HGEntityTransferModule")]
  45. public class HGEntityTransferModule
  46. : EntityTransferModule, INonSharedRegionModule, IEntityTransferModule, IUserAgentVerificationModule
  47. {
  48. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. private int m_levelHGTeleport = 0;
  50. private GatekeeperServiceConnector m_GatekeeperConnector;
  51. private IUserAgentService m_UAS;
  52. protected bool m_RestrictAppearanceAbroad;
  53. protected string m_AccountName;
  54. protected List<AvatarAppearance> m_ExportedAppearances;
  55. protected List<AvatarAttachment> m_Attachs;
  56. protected List<AvatarAppearance> ExportedAppearance
  57. {
  58. get
  59. {
  60. if (m_ExportedAppearances != null)
  61. return m_ExportedAppearances;
  62. m_ExportedAppearances = new List<AvatarAppearance>();
  63. m_Attachs = new List<AvatarAttachment>();
  64. string[] names = m_AccountName.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  65. foreach (string name in names)
  66. {
  67. string[] parts = name.Trim().Split();
  68. if (parts.Length != 2)
  69. {
  70. m_log.WarnFormat("[HG ENTITY TRANSFER MODULE]: Wrong user account name format {0}. Specify 'First Last'", name);
  71. return null;
  72. }
  73. UserAccount account = Scene.UserAccountService.GetUserAccount(UUID.Zero, parts[0], parts[1]);
  74. if (account == null)
  75. {
  76. m_log.WarnFormat("[HG ENTITY TRANSFER MODULE]: Unknown account {0}", m_AccountName);
  77. return null;
  78. }
  79. AvatarAppearance a = Scene.AvatarService.GetAppearance(account.PrincipalID);
  80. if (a != null)
  81. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Successfully retrieved appearance for {0}", name);
  82. foreach (AvatarAttachment att in a.GetAttachments())
  83. {
  84. InventoryItemBase item = new InventoryItemBase(att.ItemID, account.PrincipalID);
  85. item = Scene.InventoryService.GetItem(item);
  86. if (item != null)
  87. a.SetAttachment(att.AttachPoint, att.ItemID, item.AssetID);
  88. else
  89. m_log.WarnFormat("[HG ENTITY TRANSFER MODULE]: Unable to retrieve item {0} from inventory {1}", att.ItemID, name);
  90. }
  91. m_ExportedAppearances.Add(a);
  92. m_Attachs.AddRange(a.GetAttachments());
  93. }
  94. return m_ExportedAppearances;
  95. }
  96. }
  97. /// <summary>
  98. /// Used for processing analysis of incoming attachments in a controlled fashion.
  99. /// </summary>
  100. private HGIncomingSceneObjectEngine m_incomingSceneObjectEngine;
  101. #region ISharedRegionModule
  102. public override string Name
  103. {
  104. get { return "HGEntityTransferModule"; }
  105. }
  106. public override void Initialise(IConfigSource source)
  107. {
  108. IConfig moduleConfig = source.Configs["Modules"];
  109. if (moduleConfig != null)
  110. {
  111. string name = moduleConfig.GetString("EntityTransferModule", "");
  112. if (name == Name)
  113. {
  114. IConfig transferConfig = source.Configs["EntityTransfer"];
  115. if (transferConfig != null)
  116. {
  117. m_levelHGTeleport = transferConfig.GetInt("LevelHGTeleport", 0);
  118. m_RestrictAppearanceAbroad = transferConfig.GetBoolean("RestrictAppearanceAbroad", false);
  119. if (m_RestrictAppearanceAbroad)
  120. {
  121. m_AccountName = transferConfig.GetString("AccountForAppearance", string.Empty);
  122. if (m_AccountName == string.Empty)
  123. m_log.WarnFormat("[HG ENTITY TRANSFER MODULE]: RestrictAppearanceAbroad is on, but no account has been given for avatar appearance!");
  124. }
  125. }
  126. InitialiseCommon(source);
  127. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: {0} enabled.", Name);
  128. }
  129. }
  130. }
  131. public override void AddRegion(Scene scene)
  132. {
  133. base.AddRegion(scene);
  134. if (m_Enabled)
  135. {
  136. scene.RegisterModuleInterface<IUserAgentVerificationModule>(this);
  137. //scene.EventManager.OnIncomingSceneObject += OnIncomingSceneObject;
  138. m_incomingSceneObjectEngine = new HGIncomingSceneObjectEngine(scene.Name);
  139. m_incomingSceneObjectEngine.Start();
  140. }
  141. }
  142. protected override void OnNewClient(IClientAPI client)
  143. {
  144. client.OnTeleportHomeRequest += TriggerTeleportHome;
  145. client.OnTeleportLandmarkRequest += RequestTeleportLandmark;
  146. client.OnConnectionClosed += new Action<IClientAPI>(OnConnectionClosed);
  147. }
  148. public override void RegionLoaded(Scene scene)
  149. {
  150. base.RegionLoaded(scene);
  151. if (m_Enabled)
  152. {
  153. m_GatekeeperConnector = new GatekeeperServiceConnector(scene.AssetService);
  154. m_UAS = scene.RequestModuleInterface<IUserAgentService>();
  155. if (m_UAS == null)
  156. m_UAS = new UserAgentServiceConnector(m_ThisHomeURI);
  157. }
  158. }
  159. public override void RemoveRegion(Scene scene)
  160. {
  161. base.RemoveRegion(scene);
  162. if (m_Enabled)
  163. {
  164. scene.UnregisterModuleInterface<IUserAgentVerificationModule>(this);
  165. m_incomingSceneObjectEngine.Stop();
  166. }
  167. }
  168. #endregion
  169. #region HG overrides of IEntityTransferModule
  170. protected override GridRegion GetFinalDestination(GridRegion region, UUID agentID, string agentHomeURI, out string message)
  171. {
  172. int flags = Scene.GridService.GetRegionFlags(Scene.RegionInfo.ScopeID, region.RegionID);
  173. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: region {0} flags: {1}", region.RegionName, flags);
  174. message = null;
  175. if ((flags & (int)OpenSim.Framework.RegionFlags.Hyperlink) != 0)
  176. {
  177. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Destination region is hyperlink");
  178. GridRegion real_destination = m_GatekeeperConnector.GetHyperlinkRegion(region, region.RegionID, agentID, agentHomeURI, out message);
  179. if (real_destination != null)
  180. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: GetFinalDestination: ServerURI={0}", real_destination.ServerURI);
  181. else
  182. m_log.WarnFormat("[HG ENTITY TRANSFER MODULE]: GetHyperlinkRegion of region {0} from Gatekeeper {1} failed: {2}", region.RegionID, region.ServerURI, message);
  183. return real_destination;
  184. }
  185. return region;
  186. }
  187. protected override bool NeedsClosing(float drawdist, uint oldRegionX, uint newRegionX, uint oldRegionY, uint newRegionY, GridRegion reg)
  188. {
  189. if (base.NeedsClosing(drawdist, oldRegionX, newRegionX, oldRegionY, newRegionY, reg))
  190. return true;
  191. int flags = Scene.GridService.GetRegionFlags(Scene.RegionInfo.ScopeID, reg.RegionID);
  192. if (flags == -1 /* no region in DB */ || (flags & (int)OpenSim.Framework.RegionFlags.Hyperlink) != 0)
  193. return true;
  194. return false;
  195. }
  196. protected override void AgentHasMovedAway(ScenePresence sp, bool logout)
  197. {
  198. base.AgentHasMovedAway(sp, logout);
  199. if (logout)
  200. {
  201. // Log them out of this grid
  202. Scene.PresenceService.LogoutAgent(sp.ControllingClient.SessionId);
  203. string userId = Scene.UserManagementModule.GetUserUUI(sp.UUID);
  204. Scene.GridUserService.LoggedOut(userId, UUID.Zero, Scene.RegionInfo.RegionID, sp.AbsolutePosition, sp.Lookat);
  205. }
  206. }
  207. protected override bool CreateAgent(ScenePresence sp, GridRegion reg, GridRegion finalDestination, AgentCircuitData agentCircuit, uint teleportFlags, out string reason, out bool logout)
  208. {
  209. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: CreateAgent {0} {1}", reg.ServerURI, finalDestination.ServerURI);
  210. reason = string.Empty;
  211. logout = false;
  212. int flags = Scene.GridService.GetRegionFlags(Scene.RegionInfo.ScopeID, reg.RegionID);
  213. if (flags == -1 /* no region in DB */ || (flags & (int)OpenSim.Framework.RegionFlags.Hyperlink) != 0)
  214. {
  215. // this user is going to another grid
  216. // for local users, check if HyperGrid teleport is allowed, based on user level
  217. if (Scene.UserManagementModule.IsLocalGridUser(sp.UUID) && sp.UserLevel < m_levelHGTeleport)
  218. {
  219. m_log.WarnFormat("[HG ENTITY TRANSFER MODULE]: Unable to HG teleport agent due to insufficient UserLevel.");
  220. reason = "Hypergrid teleport not allowed";
  221. return false;
  222. }
  223. if (agentCircuit.ServiceURLs.ContainsKey("HomeURI"))
  224. {
  225. string userAgentDriver = agentCircuit.ServiceURLs["HomeURI"].ToString();
  226. IUserAgentService connector;
  227. if (userAgentDriver.Equals(m_ThisHomeURI) && m_UAS != null)
  228. connector = m_UAS;
  229. else
  230. connector = new UserAgentServiceConnector(userAgentDriver);
  231. GridRegion source = new GridRegion(Scene.RegionInfo);
  232. source.RawServerURI = m_GatekeeperURI;
  233. bool success = connector.LoginAgentToGrid(source, agentCircuit, reg, finalDestination, false, out reason);
  234. logout = success; // flag for later logout from this grid; this is an HG TP
  235. if (success)
  236. Scene.EventManager.TriggerTeleportStart(sp.ControllingClient, reg, finalDestination, teleportFlags, logout);
  237. return success;
  238. }
  239. else
  240. {
  241. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Agent does not have a HomeURI address");
  242. return false;
  243. }
  244. }
  245. return base.CreateAgent(sp, reg, finalDestination, agentCircuit, teleportFlags, out reason, out logout);
  246. }
  247. protected override bool ValidateGenericConditions(ScenePresence sp, GridRegion reg, GridRegion finalDestination, uint teleportFlags, out string reason)
  248. {
  249. reason = "Please wear your grid's allowed appearance before teleporting to another grid";
  250. if (!m_RestrictAppearanceAbroad)
  251. return true;
  252. // The rest is only needed for controlling appearance
  253. int flags = Scene.GridService.GetRegionFlags(Scene.RegionInfo.ScopeID, reg.RegionID);
  254. if (flags == -1 /* no region in DB */ || (flags & (int)OpenSim.Framework.RegionFlags.Hyperlink) != 0)
  255. {
  256. // this user is going to another grid
  257. if (Scene.UserManagementModule.IsLocalGridUser(sp.UUID))
  258. {
  259. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: RestrictAppearanceAbroad is ON. Checking generic appearance");
  260. // Check wearables
  261. for (int i = 0; i < AvatarWearable.MAX_WEARABLES; i++)
  262. {
  263. for (int j = 0; j < sp.Appearance.Wearables[i].Count; j++)
  264. {
  265. if (sp.Appearance.Wearables[i] == null)
  266. continue;
  267. bool found = false;
  268. foreach (AvatarAppearance a in ExportedAppearance)
  269. if (a.Wearables[i] != null)
  270. {
  271. found = true;
  272. break;
  273. }
  274. if (!found)
  275. {
  276. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Wearable not allowed to go outside {0}", i);
  277. return false;
  278. }
  279. found = false;
  280. foreach (AvatarAppearance a in ExportedAppearance)
  281. if (sp.Appearance.Wearables[i][j].AssetID == a.Wearables[i][j].AssetID)
  282. {
  283. found = true;
  284. break;
  285. }
  286. if (!found)
  287. {
  288. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Wearable not allowed to go outside {0}", i);
  289. return false;
  290. }
  291. }
  292. }
  293. // Check attachments
  294. foreach (AvatarAttachment att in sp.Appearance.GetAttachments())
  295. {
  296. bool found = false;
  297. foreach (AvatarAttachment att2 in m_Attachs)
  298. {
  299. if (att2.AssetID == att.AssetID)
  300. {
  301. found = true;
  302. break;
  303. }
  304. }
  305. if (!found)
  306. {
  307. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Attachment not allowed to go outside {0}", att.AttachPoint);
  308. return false;
  309. }
  310. }
  311. }
  312. }
  313. reason = string.Empty;
  314. return true;
  315. }
  316. //protected override bool UpdateAgent(GridRegion reg, GridRegion finalDestination, AgentData agentData, ScenePresence sp)
  317. //{
  318. // int flags = Scene.GridService.GetRegionFlags(Scene.RegionInfo.ScopeID, reg.RegionID);
  319. // if (flags == -1 /* no region in DB */ || (flags & (int)OpenSim.Data.RegionFlags.Hyperlink) != 0)
  320. // {
  321. // // this user is going to another grid
  322. // if (m_RestrictAppearanceAbroad && Scene.UserManagementModule.IsLocalGridUser(agentData.AgentID))
  323. // {
  324. // // We need to strip the agent off its appearance
  325. // m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: RestrictAppearanceAbroad is ON. Sending generic appearance");
  326. // // Delete existing npc attachments
  327. // Scene.AttachmentsModule.DeleteAttachmentsFromScene(sp, false);
  328. // // XXX: We can't just use IAvatarFactoryModule.SetAppearance() yet since it doesn't transfer attachments
  329. // AvatarAppearance newAppearance = new AvatarAppearance(ExportedAppearance, true);
  330. // sp.Appearance = newAppearance;
  331. // // Rez needed npc attachments
  332. // Scene.AttachmentsModule.RezAttachments(sp);
  333. // IAvatarFactoryModule module = Scene.RequestModuleInterface<IAvatarFactoryModule>();
  334. // //module.SendAppearance(sp.UUID);
  335. // module.RequestRebake(sp, false);
  336. // Scene.AttachmentsModule.CopyAttachments(sp, agentData);
  337. // agentData.Appearance = sp.Appearance;
  338. // }
  339. // }
  340. // foreach (AvatarAttachment a in agentData.Appearance.GetAttachments())
  341. // m_log.DebugFormat("[XXX]: {0}-{1}", a.ItemID, a.AssetID);
  342. // return base.UpdateAgent(reg, finalDestination, agentData, sp);
  343. //}
  344. public override void TriggerTeleportHome(UUID id, IClientAPI client)
  345. {
  346. TeleportHome(id, client);
  347. }
  348. public override bool TeleportHome(UUID id, IClientAPI client)
  349. {
  350. m_log.DebugFormat(
  351. "[ENTITY TRANSFER MODULE]: Request to teleport {0} {1} home", client.Name, client.AgentId);
  352. // Let's find out if this is a foreign user or a local user
  353. IUserManagement uMan = Scene.RequestModuleInterface<IUserManagement>();
  354. if (uMan != null && uMan.IsLocalGridUser(id))
  355. {
  356. // local grid user
  357. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: User is local");
  358. return base.TeleportHome(id, client);
  359. }
  360. // Foreign user wants to go home
  361. //
  362. AgentCircuitData aCircuit = ((Scene)(client.Scene)).AuthenticateHandler.GetAgentCircuitData(client.CircuitCode);
  363. if (aCircuit == null || (aCircuit != null && !aCircuit.ServiceURLs.ContainsKey("HomeURI")))
  364. {
  365. client.SendTeleportFailed("Your information has been lost");
  366. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Unable to locate agent's gateway information");
  367. return false;
  368. }
  369. IUserAgentService userAgentService = new UserAgentServiceConnector(aCircuit.ServiceURLs["HomeURI"].ToString());
  370. Vector3 position = Vector3.UnitY, lookAt = Vector3.UnitY;
  371. GridRegion finalDestination = null;
  372. try
  373. {
  374. finalDestination = userAgentService.GetHomeRegion(aCircuit.AgentID, out position, out lookAt);
  375. }
  376. catch (Exception e)
  377. {
  378. m_log.Debug("[HG ENTITY TRANSFER MODULE]: GetHomeRegion call failed ", e);
  379. }
  380. if (finalDestination == null)
  381. {
  382. client.SendTeleportFailed("Your home region could not be found");
  383. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Agent's home region not found");
  384. return false;
  385. }
  386. ScenePresence sp = ((Scene)(client.Scene)).GetScenePresence(client.AgentId);
  387. if (sp == null)
  388. {
  389. client.SendTeleportFailed("Internal error");
  390. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Agent not found in the scene where it is supposed to be");
  391. return false;
  392. }
  393. GridRegion homeGatekeeper = MakeRegion(aCircuit);
  394. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: teleporting user {0} {1} home to {2} via {3}:{4}",
  395. aCircuit.firstname, aCircuit.lastname, finalDestination.RegionName, homeGatekeeper.ServerURI, homeGatekeeper.RegionName);
  396. DoTeleport(
  397. sp, homeGatekeeper, finalDestination,
  398. position, lookAt, (uint)(Constants.TeleportFlags.SetLastToTarget | Constants.TeleportFlags.ViaHome));
  399. return true;
  400. }
  401. /// <summary>
  402. /// Tries to teleport agent to landmark.
  403. /// </summary>
  404. /// <param name="remoteClient"></param>
  405. /// <param name="regionHandle"></param>
  406. /// <param name="position"></param>
  407. public override void RequestTeleportLandmark(IClientAPI remoteClient, AssetLandmark lm)
  408. {
  409. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Teleporting agent via landmark to {0} region {1} position {2}",
  410. (lm.Gatekeeper == string.Empty) ? "local" : lm.Gatekeeper, lm.RegionID, lm.Position);
  411. if (lm.Gatekeeper == string.Empty)
  412. {
  413. base.RequestTeleportLandmark(remoteClient, lm);
  414. return;
  415. }
  416. GridRegion info = Scene.GridService.GetRegionByUUID(UUID.Zero, lm.RegionID);
  417. // Local region?
  418. if (info != null)
  419. {
  420. Scene.RequestTeleportLocation(
  421. remoteClient, info.RegionHandle, lm.Position,
  422. Vector3.Zero, (uint)(Constants.TeleportFlags.SetLastToTarget | Constants.TeleportFlags.ViaLandmark));
  423. }
  424. else
  425. {
  426. // Foreign region
  427. GatekeeperServiceConnector gConn = new GatekeeperServiceConnector();
  428. GridRegion gatekeeper = new GridRegion();
  429. gatekeeper.ServerURI = lm.Gatekeeper;
  430. string homeURI = Scene.GetAgentHomeURI(remoteClient.AgentId);
  431. string message;
  432. GridRegion finalDestination = gConn.GetHyperlinkRegion(gatekeeper, new UUID(lm.RegionID), remoteClient.AgentId, homeURI, out message);
  433. if (finalDestination != null)
  434. {
  435. ScenePresence sp = Scene.GetScenePresence(remoteClient.AgentId);
  436. if (sp != null)
  437. {
  438. if (message != null)
  439. sp.ControllingClient.SendAgentAlertMessage(message, true);
  440. // Validate assorted conditions
  441. string reason = string.Empty;
  442. if (!ValidateGenericConditions(sp, gatekeeper, finalDestination, 0, out reason))
  443. {
  444. sp.ControllingClient.SendTeleportFailed(reason);
  445. return;
  446. }
  447. DoTeleport(
  448. sp, gatekeeper, finalDestination, lm.Position, Vector3.UnitX,
  449. (uint)(Constants.TeleportFlags.SetLastToTarget | Constants.TeleportFlags.ViaLandmark));
  450. }
  451. }
  452. else
  453. {
  454. remoteClient.SendTeleportFailed(message);
  455. }
  456. }
  457. }
  458. private void RemoveIncomingSceneObjectJobs(string commonIdToRemove)
  459. {
  460. List<Job> jobsToReinsert = new List<Job>();
  461. int jobsRemoved = 0;
  462. Job job;
  463. while ((job = m_incomingSceneObjectEngine.RemoveNextRequest()) != null)
  464. {
  465. if (job.CommonId != commonIdToRemove)
  466. jobsToReinsert.Add(job);
  467. else
  468. jobsRemoved++;
  469. }
  470. m_log.DebugFormat(
  471. "[HG ENTITY TRANSFER]: Removing {0} jobs with common ID {1} and reinserting {2} other jobs",
  472. jobsRemoved, commonIdToRemove, jobsToReinsert.Count);
  473. if (jobsToReinsert.Count > 0)
  474. {
  475. foreach (Job jobToReinsert in jobsToReinsert)
  476. m_incomingSceneObjectEngine.QueueRequest(jobToReinsert);
  477. }
  478. }
  479. public override bool HandleIncomingSceneObject(SceneObjectGroup so, Vector3 newPosition)
  480. {
  481. // FIXME: We must make it so that we can use SOG.IsAttachment here. At the moment it is always null!
  482. if (!so.IsAttachmentCheckFull())
  483. return base.HandleIncomingSceneObject(so, newPosition);
  484. // Equally, we can't use so.AttachedAvatar here.
  485. if (so.OwnerID == UUID.Zero || Scene.UserManagementModule.IsLocalGridUser(so.OwnerID))
  486. return base.HandleIncomingSceneObject(so, newPosition);
  487. // foreign user
  488. AgentCircuitData aCircuit = Scene.AuthenticateHandler.GetAgentCircuitData(so.OwnerID);
  489. if (aCircuit != null)
  490. {
  491. if ((aCircuit.teleportFlags & (uint)Constants.TeleportFlags.ViaHGLogin) == 0)
  492. {
  493. // We have already pulled the necessary attachments from the source grid.
  494. base.HandleIncomingSceneObject(so, newPosition);
  495. }
  496. else
  497. {
  498. if (aCircuit.ServiceURLs != null && aCircuit.ServiceURLs.ContainsKey("AssetServerURI"))
  499. {
  500. m_incomingSceneObjectEngine.QueueRequest(
  501. string.Format("HG UUID Gather for attachment {0} for {1}", so.Name, aCircuit.Name),
  502. so.OwnerID.ToString(),
  503. o =>
  504. {
  505. string url = aCircuit.ServiceURLs["AssetServerURI"].ToString();
  506. // m_log.DebugFormat(
  507. // "[HG ENTITY TRANSFER MODULE]: Incoming attachment {0} for HG user {1} with asset service {2}",
  508. // so.Name, so.AttachedAvatar, url);
  509. IDictionary<UUID, sbyte> ids = new Dictionary<UUID, sbyte>();
  510. IteratingHGUuidGatherer uuidGatherer
  511. = new IteratingHGUuidGatherer(Scene.AssetService, url, ids);
  512. uuidGatherer.AddForInspection(so);
  513. while (!uuidGatherer.Complete)
  514. {
  515. int tickStart = Util.EnvironmentTickCount();
  516. UUID? nextUuid = uuidGatherer.NextUuidToInspect;
  517. uuidGatherer.GatherNext();
  518. // m_log.DebugFormat(
  519. // "[HG ENTITY TRANSFER]: Gathered attachment asset uuid {0} for object {1} for HG user {2} took {3} ms with asset service {4}",
  520. // nextUuid, so.Name, so.OwnerID, Util.EnvironmentTickCountSubtract(tickStart), url);
  521. int ticksElapsed = Util.EnvironmentTickCountSubtract(tickStart);
  522. if (ticksElapsed > 30000)
  523. {
  524. m_log.WarnFormat(
  525. "[HG ENTITY TRANSFER]: Removing incoming scene object jobs for HG user {0} as gather of {1} from {2} took {3} ms to respond (> {4} ms)",
  526. so.OwnerID, so.Name, url, ticksElapsed, 30000);
  527. RemoveIncomingSceneObjectJobs(so.OwnerID.ToString());
  528. return;
  529. }
  530. }
  531. // m_log.DebugFormat(
  532. // "[HG ENTITY TRANSFER]: Fetching {0} assets for attachment {1} for HG user {2} with asset service {3}",
  533. // ids.Count, so.Name, so.OwnerID, url);
  534. foreach (KeyValuePair<UUID, sbyte> kvp in ids)
  535. {
  536. int tickStart = Util.EnvironmentTickCount();
  537. uuidGatherer.FetchAsset(kvp.Key);
  538. int ticksElapsed = Util.EnvironmentTickCountSubtract(tickStart);
  539. if (ticksElapsed > 30000)
  540. {
  541. m_log.WarnFormat(
  542. "[HG ENTITY TRANSFER]: Removing incoming scene object jobs for HG user {0} as fetch of {1} from {2} took {3} ms to respond (> {4} ms)",
  543. so.OwnerID, kvp.Key, url, ticksElapsed, 30000);
  544. RemoveIncomingSceneObjectJobs(so.OwnerID.ToString());
  545. return;
  546. }
  547. }
  548. base.HandleIncomingSceneObject(so, newPosition);
  549. // m_log.DebugFormat(
  550. // "[HG ENTITY TRANSFER MODULE]: Completed incoming attachment {0} for HG user {1} with asset server {2}",
  551. // so.Name, so.OwnerID, url);
  552. },
  553. null);
  554. }
  555. }
  556. }
  557. return true;
  558. }
  559. #endregion
  560. #region IUserAgentVerificationModule
  561. public bool VerifyClient(AgentCircuitData aCircuit, string token)
  562. {
  563. if (aCircuit.ServiceURLs.ContainsKey("HomeURI"))
  564. {
  565. string url = aCircuit.ServiceURLs["HomeURI"].ToString();
  566. IUserAgentService security = new UserAgentServiceConnector(url);
  567. return security.VerifyClient(aCircuit.SessionID, token);
  568. }
  569. else
  570. {
  571. m_log.DebugFormat(
  572. "[HG ENTITY TRANSFER MODULE]: Agent {0} {1} does not have a HomeURI OH NO!",
  573. aCircuit.firstname, aCircuit.lastname);
  574. }
  575. return false;
  576. }
  577. void OnConnectionClosed(IClientAPI obj)
  578. {
  579. if (obj.SceneAgent.IsChildAgent)
  580. return;
  581. // Let's find out if this is a foreign user or a local user
  582. IUserManagement uMan = Scene.RequestModuleInterface<IUserManagement>();
  583. // UserAccount account = Scene.UserAccountService.GetUserAccount(Scene.RegionInfo.ScopeID, obj.AgentId);
  584. if (uMan != null && uMan.IsLocalGridUser(obj.AgentId))
  585. {
  586. // local grid user
  587. m_UAS.LogoutAgent(obj.AgentId, obj.SessionId);
  588. return;
  589. }
  590. AgentCircuitData aCircuit = ((Scene)(obj.Scene)).AuthenticateHandler.GetAgentCircuitData(obj.CircuitCode);
  591. if (aCircuit != null && aCircuit.ServiceURLs != null && aCircuit.ServiceURLs.ContainsKey("HomeURI"))
  592. {
  593. string url = aCircuit.ServiceURLs["HomeURI"].ToString();
  594. IUserAgentService security = new UserAgentServiceConnector(url);
  595. security.LogoutAgent(obj.AgentId, obj.SessionId);
  596. //m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: Sent logout call to UserAgentService @ {0}", url);
  597. }
  598. else
  599. {
  600. m_log.DebugFormat("[HG ENTITY TRANSFER MODULE]: HomeURI not found for agent {0} logout", obj.AgentId);
  601. }
  602. }
  603. #endregion
  604. private GridRegion MakeRegion(AgentCircuitData aCircuit)
  605. {
  606. GridRegion region = new GridRegion();
  607. Uri uri = null;
  608. if (!aCircuit.ServiceURLs.ContainsKey("HomeURI") ||
  609. (aCircuit.ServiceURLs.ContainsKey("HomeURI") && !Uri.TryCreate(aCircuit.ServiceURLs["HomeURI"].ToString(), UriKind.Absolute, out uri)))
  610. return null;
  611. region.ExternalHostName = uri.Host;
  612. region.HttpPort = (uint)uri.Port;
  613. region.ServerURI = aCircuit.ServiceURLs["HomeURI"].ToString();
  614. region.RegionName = string.Empty;
  615. region.InternalEndPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("0.0.0.0"), (int)0);
  616. return region;
  617. }
  618. }
  619. }