HGMessageTransferModule.cs 12 KB

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