HGMessageTransferModule.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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;
  29. using System.Collections.Generic;
  30. using System.Net;
  31. using System.Reflection;
  32. using log4net;
  33. using Nini.Config;
  34. using Nwc.XmlRpc;
  35. using Mono.Addins;
  36. using OpenMetaverse;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.Servers;
  39. using OpenSim.Region.Framework.Interfaces;
  40. using OpenSim.Region.Framework.Scenes;
  41. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  42. using PresenceInfo = OpenSim.Services.Interfaces.PresenceInfo;
  43. using OpenSim.Services.Interfaces;
  44. using OpenSim.Services.Connectors.InstantMessage;
  45. using OpenSim.Services.Connectors.Hypergrid;
  46. using OpenSim.Server.Handlers.Hypergrid;
  47. namespace OpenSim.Region.CoreModules.Avatar.InstantMessage
  48. {
  49. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "HGMessageTransferModule")]
  50. public class HGMessageTransferModule : ISharedRegionModule, IMessageTransferModule, IInstantMessageSimConnector
  51. {
  52. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  53. protected bool m_Enabled = false;
  54. protected List<Scene> m_Scenes = new List<Scene>();
  55. protected IInstantMessage m_IMService;
  56. protected Dictionary<UUID, object> m_UserLocationMap = new Dictionary<UUID, object>();
  57. public event UndeliveredMessage OnUndeliveredMessage;
  58. IUserManagement m_uMan;
  59. IUserManagement UserManagementModule
  60. {
  61. get
  62. {
  63. if (m_uMan == null)
  64. m_uMan = m_Scenes[0].RequestModuleInterface<IUserManagement>();
  65. return m_uMan;
  66. }
  67. }
  68. public virtual void Initialise(IConfigSource config)
  69. {
  70. IConfig cnf = config.Configs["Messaging"];
  71. if (cnf != null && cnf.GetString("MessageTransferModule", "MessageTransferModule") != Name)
  72. {
  73. m_log.Debug("[HG MESSAGE TRANSFER]: Disabled by configuration");
  74. return;
  75. }
  76. InstantMessageServerConnector imServer = new InstantMessageServerConnector(config, MainServer.Instance, this);
  77. m_IMService = imServer.GetService();
  78. m_Enabled = true;
  79. }
  80. public virtual void AddRegion(Scene scene)
  81. {
  82. if (!m_Enabled)
  83. return;
  84. lock (m_Scenes)
  85. {
  86. m_log.DebugFormat("[HG MESSAGE TRANSFER]: Message transfer module {0} active", Name);
  87. scene.RegisterModuleInterface<IMessageTransferModule>(this);
  88. m_Scenes.Add(scene);
  89. }
  90. }
  91. public virtual void PostInitialise()
  92. {
  93. }
  94. public virtual void RegionLoaded(Scene scene)
  95. {
  96. }
  97. public virtual void RemoveRegion(Scene scene)
  98. {
  99. if (!m_Enabled)
  100. return;
  101. lock (m_Scenes)
  102. {
  103. m_Scenes.Remove(scene);
  104. }
  105. }
  106. public virtual void Close()
  107. {
  108. }
  109. public virtual string Name
  110. {
  111. get { return "HGMessageTransferModule"; }
  112. }
  113. public virtual Type ReplaceableInterface
  114. {
  115. get { return null; }
  116. }
  117. public void SendInstantMessage(GridInstantMessage im, MessageResultNotification result)
  118. {
  119. UUID toAgentID = new UUID(im.toAgentID);
  120. if (toAgentID.IsZero())
  121. return;
  122. ScenePresence achildsp = null;
  123. // Try root avatar first
  124. foreach (Scene scene in m_Scenes)
  125. {
  126. //m_log.DebugFormat(
  127. // "[HG INSTANT MESSAGE]: Looking for root agent {0} in {1}",
  128. // toAgentID.ToString(), scene.RegionInfo.RegionName);
  129. ScenePresence sp = scene.GetScenePresence(toAgentID);
  130. if (sp != null && !sp.IsDeleted)
  131. {
  132. if(sp.IsChildAgent)
  133. achildsp = sp;
  134. else
  135. {
  136. // m_log.DebugFormat("[HG INSTANT MESSAGE]: Delivering IM to root agent {0} {1}", user.Name, toAgentID);
  137. sp.ControllingClient.SendInstantMessage(im);
  138. result(true);
  139. return;
  140. }
  141. }
  142. }
  143. if(achildsp != null)
  144. {
  145. // m_log.DebugFormat("[HG INSTANT MESSAGE]: Delivering IM to child agent {0} {1}", user.Name, toAgentID);
  146. achildsp.ControllingClient.SendInstantMessage(im);
  147. result(true);
  148. return;
  149. }
  150. // m_log.DebugFormat("[HG INSTANT MESSAGE]: Delivering IM to {0} via XMLRPC", im.toAgentID);
  151. // Is the user a local user?
  152. string url = string.Empty;
  153. bool foreigner = false;
  154. if (UserManagementModule != null) // foreign user
  155. {
  156. foreigner = !UserManagementModule.IsLocalGridUser(toAgentID);
  157. if(foreigner)
  158. url = UserManagementModule.GetUserServerURL(toAgentID, "IMServerURI");
  159. }
  160. Util.FireAndForget(delegate
  161. {
  162. UUID toDelAgentID = new UUID(im.toAgentID);
  163. bool success = false;
  164. if (foreigner && url.Length == 0) // we don't know about this user
  165. {
  166. string recipientUUI = TryGetRecipientUUI(new UUID(im.fromAgentID), toDelAgentID);
  167. //m_log.DebugFormat("[HG MESSAGE TRANSFER]: Got UUI {0}", recipientUUI);
  168. if (recipientUUI.Length > 0)
  169. {
  170. if (Util.ParseFullUniversalUserIdentifier(recipientUUI, out UUID id, out string tourl,
  171. out string first, out string last) && !string.IsNullOrEmpty(tourl))
  172. {
  173. success = m_IMService.OutgoingInstantMessage(im, tourl, true);
  174. if (success)
  175. UserManagementModule.AddUser(toDelAgentID, first, last, tourl);
  176. }
  177. }
  178. }
  179. else
  180. success = m_IMService.OutgoingInstantMessage(im, url, foreigner);
  181. if (!success && !foreigner)
  182. HandleUndeliverableMessage(im, result);
  183. else
  184. result(success);
  185. }, null, "HGMessageTransferModule.SendInstantMessage");
  186. }
  187. protected bool SendIMToScene(GridInstantMessage gim, UUID toAgentID)
  188. {
  189. Scene childScene = null;
  190. foreach (Scene scene in m_Scenes)
  191. {
  192. ScenePresence sp = scene.GetScenePresence(toAgentID);
  193. if(sp != null && !sp.IsDeleted)
  194. {
  195. if(sp.IsChildAgent)
  196. childScene = scene;
  197. else
  198. {
  199. scene.EventManager.TriggerIncomingInstantMessage(gim);
  200. return true;
  201. }
  202. }
  203. }
  204. if(childScene != null)
  205. {
  206. childScene.EventManager.TriggerIncomingInstantMessage(gim);
  207. return true;
  208. }
  209. // If the message can't be delivered to an agent, it
  210. // is likely to be a group IM. On a group IM, the
  211. // imSessionID = toAgentID = group id. Raise the
  212. // unhandled IM event to give the groups module
  213. // a chance to pick it up. We raise that in a random
  214. // scene, since the groups module is shared.
  215. //
  216. m_Scenes[0].EventManager.TriggerUnhandledInstantMessage(gim);
  217. return false;
  218. }
  219. public void HandleUndeliverableMessage(GridInstantMessage im, MessageResultNotification result)
  220. {
  221. UndeliveredMessage handlerUndeliveredMessage = OnUndeliveredMessage;
  222. // If this event has handlers, then an IM from an agent will be
  223. // considered delivered. This will suppress the error message.
  224. //
  225. if (handlerUndeliveredMessage != null)
  226. {
  227. handlerUndeliveredMessage(im);
  228. if (im.dialog == (byte)InstantMessageDialog.MessageFromAgent)
  229. result(true);
  230. else
  231. result(false);
  232. return;
  233. }
  234. //m_log.DebugFormat("[INSTANT MESSAGE]: Undeliverable");
  235. result(false);
  236. }
  237. private string TryGetRecipientUUI(UUID fromAgent, UUID toAgent)
  238. {
  239. // Let's call back the fromAgent's user agent service
  240. // Maybe that service knows about the toAgent
  241. AgentCircuitData circuit = m_Scenes[0].AuthenticateHandler.GetAgentCircuitData(fromAgent);
  242. if (circuit != null)
  243. {
  244. if (circuit.ServiceURLs.ContainsKey("HomeURI"))
  245. {
  246. string uasURL = circuit.ServiceURLs["HomeURI"].ToString();
  247. m_log.DebugFormat("[HG MESSAGE TRANSFER]: getting UUI of user {0} from {1}", toAgent, uasURL);
  248. UserAgentServiceConnector uasConn = new UserAgentServiceConnector(uasURL);
  249. string agentUUI = string.Empty;
  250. try
  251. {
  252. agentUUI = uasConn.GetUUI(fromAgent, toAgent);
  253. }
  254. catch (Exception e) {
  255. m_log.Debug("[HG MESSAGE TRANSFER]: GetUUI call failed ", e);
  256. }
  257. return agentUUI;
  258. }
  259. }
  260. return string.Empty;
  261. }
  262. /// <summary>
  263. /// Find the root client for a ID
  264. /// </summary>
  265. public IClientAPI LocateClientObject(UUID agentID)
  266. {
  267. lock (m_Scenes)
  268. {
  269. foreach (Scene scene in m_Scenes)
  270. {
  271. ScenePresence presence = scene.GetScenePresence(agentID);
  272. if (presence != null && !presence.IsChildAgent && !presence.IsDeleted)
  273. return presence.ControllingClient;
  274. }
  275. }
  276. return null;
  277. }
  278. #region IInstantMessageSimConnector
  279. public bool SendInstantMessage(GridInstantMessage im)
  280. {
  281. //m_log.DebugFormat("[XXX] Hook SendInstantMessage {0}", im.message);
  282. UUID agentID = new UUID(im.toAgentID);
  283. return SendIMToScene(im, agentID);
  284. }
  285. #endregion
  286. }
  287. }