HGMessageTransferModule.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. if (!m_Enabled)
  94. return;
  95. }
  96. public virtual void RegionLoaded(Scene scene)
  97. {
  98. }
  99. public virtual void RemoveRegion(Scene scene)
  100. {
  101. if (!m_Enabled)
  102. return;
  103. lock (m_Scenes)
  104. {
  105. m_Scenes.Remove(scene);
  106. }
  107. }
  108. public virtual void Close()
  109. {
  110. }
  111. public virtual string Name
  112. {
  113. get { return "HGMessageTransferModule"; }
  114. }
  115. public virtual Type ReplaceableInterface
  116. {
  117. get { return null; }
  118. }
  119. public void SendInstantMessage(GridInstantMessage im, MessageResultNotification result)
  120. {
  121. UUID toAgentID = new UUID(im.toAgentID);
  122. if (toAgentID.IsZero())
  123. return;
  124. ScenePresence achildsp = null;
  125. // Try root avatar first
  126. foreach (Scene scene in m_Scenes)
  127. {
  128. //m_log.DebugFormat(
  129. // "[HG INSTANT MESSAGE]: Looking for root agent {0} in {1}",
  130. // toAgentID.ToString(), scene.RegionInfo.RegionName);
  131. ScenePresence sp = scene.GetScenePresence(toAgentID);
  132. if (sp != null && !sp.IsDeleted)
  133. {
  134. if(sp.IsChildAgent)
  135. achildsp = sp;
  136. else
  137. {
  138. // m_log.DebugFormat("[HG INSTANT MESSAGE]: Delivering IM to root agent {0} {1}", user.Name, toAgentID);
  139. sp.ControllingClient.SendInstantMessage(im);
  140. result(true);
  141. return;
  142. }
  143. }
  144. }
  145. if(achildsp != null)
  146. {
  147. // m_log.DebugFormat("[HG INSTANT MESSAGE]: Delivering IM to child agent {0} {1}", user.Name, toAgentID);
  148. achildsp.ControllingClient.SendInstantMessage(im);
  149. result(true);
  150. return;
  151. }
  152. // m_log.DebugFormat("[HG INSTANT MESSAGE]: Delivering IM to {0} via XMLRPC", im.toAgentID);
  153. // Is the user a local user?
  154. string url = string.Empty;
  155. bool foreigner = false;
  156. if (UserManagementModule != null) // foreign user
  157. {
  158. foreigner = !UserManagementModule.IsLocalGridUser(toAgentID);
  159. if(foreigner)
  160. url = UserManagementModule.GetUserServerURL(toAgentID, "IMServerURI");
  161. }
  162. Util.FireAndForget(delegate
  163. {
  164. UUID toDelAgentID = new UUID(im.toAgentID);
  165. bool success = false;
  166. if (foreigner && url.Length == 0) // we don't know about this user
  167. {
  168. string recipientUUI = TryGetRecipientUUI(new UUID(im.fromAgentID), toDelAgentID);
  169. //m_log.DebugFormat("[HG MESSAGE TRANSFER]: Got UUI {0}", recipientUUI);
  170. if (recipientUUI.Length > 0)
  171. {
  172. if (Util.ParseFullUniversalUserIdentifier(recipientUUI, out UUID id, out string tourl,
  173. out string first, out string last) && !string.IsNullOrEmpty(tourl))
  174. {
  175. success = m_IMService.OutgoingInstantMessage(im, tourl, true);
  176. if (success)
  177. UserManagementModule.AddUser(toDelAgentID, first, last, tourl);
  178. }
  179. }
  180. }
  181. else
  182. success = m_IMService.OutgoingInstantMessage(im, url, foreigner);
  183. if (!success && !foreigner)
  184. HandleUndeliverableMessage(im, result);
  185. else
  186. result(success);
  187. }, null, "HGMessageTransferModule.SendInstantMessage");
  188. }
  189. protected bool SendIMToScene(GridInstantMessage gim, UUID toAgentID)
  190. {
  191. Scene childScene = null;
  192. foreach (Scene scene in m_Scenes)
  193. {
  194. ScenePresence sp = scene.GetScenePresence(toAgentID);
  195. if(sp != null && !sp.IsDeleted)
  196. {
  197. if(sp.IsChildAgent)
  198. childScene = scene;
  199. else
  200. {
  201. scene.EventManager.TriggerIncomingInstantMessage(gim);
  202. return true;
  203. }
  204. }
  205. }
  206. if(childScene != null)
  207. {
  208. childScene.EventManager.TriggerIncomingInstantMessage(gim);
  209. return true;
  210. }
  211. // If the message can't be delivered to an agent, it
  212. // is likely to be a group IM. On a group IM, the
  213. // imSessionID = toAgentID = group id. Raise the
  214. // unhandled IM event to give the groups module
  215. // a chance to pick it up. We raise that in a random
  216. // scene, since the groups module is shared.
  217. //
  218. m_Scenes[0].EventManager.TriggerUnhandledInstantMessage(gim);
  219. return false;
  220. }
  221. public void HandleUndeliverableMessage(GridInstantMessage im, MessageResultNotification result)
  222. {
  223. UndeliveredMessage handlerUndeliveredMessage = OnUndeliveredMessage;
  224. // If this event has handlers, then an IM from an agent will be
  225. // considered delivered. This will suppress the error message.
  226. //
  227. if (handlerUndeliveredMessage != null)
  228. {
  229. handlerUndeliveredMessage(im);
  230. if (im.dialog == (byte)InstantMessageDialog.MessageFromAgent)
  231. result(true);
  232. else
  233. result(false);
  234. return;
  235. }
  236. //m_log.DebugFormat("[INSTANT MESSAGE]: Undeliverable");
  237. result(false);
  238. }
  239. private string TryGetRecipientUUI(UUID fromAgent, UUID toAgent)
  240. {
  241. // Let's call back the fromAgent's user agent service
  242. // Maybe that service knows about the toAgent
  243. AgentCircuitData circuit = m_Scenes[0].AuthenticateHandler.GetAgentCircuitData(fromAgent);
  244. if (circuit != null)
  245. {
  246. if (circuit.ServiceURLs.ContainsKey("HomeURI"))
  247. {
  248. string uasURL = circuit.ServiceURLs["HomeURI"].ToString();
  249. m_log.DebugFormat("[HG MESSAGE TRANSFER]: getting UUI of user {0} from {1}", toAgent, uasURL);
  250. UserAgentServiceConnector uasConn = new UserAgentServiceConnector(uasURL);
  251. string agentUUI = string.Empty;
  252. try
  253. {
  254. agentUUI = uasConn.GetUUI(fromAgent, toAgent);
  255. }
  256. catch (Exception e) {
  257. m_log.Debug("[HG MESSAGE TRANSFER]: GetUUI call failed ", e);
  258. }
  259. return agentUUI;
  260. }
  261. }
  262. return string.Empty;
  263. }
  264. /// <summary>
  265. /// Find the root client for a ID
  266. /// </summary>
  267. public IClientAPI LocateClientObject(UUID agentID)
  268. {
  269. lock (m_Scenes)
  270. {
  271. foreach (Scene scene in m_Scenes)
  272. {
  273. ScenePresence presence = scene.GetScenePresence(agentID);
  274. if (presence != null && !presence.IsChildAgent && !presence.IsDeleted)
  275. return presence.ControllingClient;
  276. }
  277. }
  278. return null;
  279. }
  280. #region IInstantMessageSimConnector
  281. public bool SendInstantMessage(GridInstantMessage im)
  282. {
  283. //m_log.DebugFormat("[XXX] Hook SendInstantMessage {0}", im.message);
  284. UUID agentID = new UUID(im.toAgentID);
  285. return SendIMToScene(im, agentID);
  286. }
  287. #endregion
  288. }
  289. }