HGMessageTransferModule.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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)
  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. // try child avatar second
  141. foreach (Scene scene in m_Scenes)
  142. {
  143. // m_log.DebugFormat(
  144. // "[HG INSTANT MESSAGE]: Looking for child of {0} in {1}",
  145. // toAgentID, scene.RegionInfo.RegionName);
  146. ScenePresence sp = scene.GetScenePresence(toAgentID);
  147. if (sp != null)
  148. {
  149. // Local message
  150. // m_log.DebugFormat("[HG INSTANT MESSAGE]: Delivering IM to child agent {0} {1}", user.Name, toAgentID);
  151. sp.ControllingClient.SendInstantMessage(im);
  152. // Message sent
  153. result(true);
  154. return;
  155. }
  156. }
  157. // m_log.DebugFormat("[HG INSTANT MESSAGE]: Delivering IM to {0} via XMLRPC", im.toAgentID);
  158. // Is the user a local user?
  159. string url = string.Empty;
  160. bool foreigner = false;
  161. if (UserManagementModule != null && !UserManagementModule.IsLocalGridUser(toAgentID)) // foreign user
  162. {
  163. url = UserManagementModule.GetUserServerURL(toAgentID, "IMServerURI");
  164. foreigner = true;
  165. }
  166. Util.FireAndForget(delegate
  167. {
  168. bool success = false;
  169. if (foreigner && url == string.Empty) // we don't know about this user
  170. {
  171. string recipientUUI = TryGetRecipientUUI(new UUID(im.fromAgentID), toAgentID);
  172. m_log.DebugFormat("[HG MESSAGE TRANSFER]: Got UUI {0}", recipientUUI);
  173. if (recipientUUI != string.Empty)
  174. {
  175. UUID id; string u = string.Empty, first = string.Empty, last = string.Empty, secret = string.Empty;
  176. if (Util.ParseUniversalUserIdentifier(recipientUUI, out id, out u, out first, out last, out secret))
  177. {
  178. success = m_IMService.OutgoingInstantMessage(im, u, true);
  179. if (success)
  180. UserManagementModule.AddUser(toAgentID, u + ";" + first + " " + last);
  181. }
  182. }
  183. }
  184. else
  185. success = m_IMService.OutgoingInstantMessage(im, url, foreigner);
  186. if (!success && !foreigner)
  187. HandleUndeliveredMessage(im, result);
  188. else
  189. result(success);
  190. });
  191. return;
  192. }
  193. protected bool SendIMToScene(GridInstantMessage gim, UUID toAgentID)
  194. {
  195. bool successful = false;
  196. foreach (Scene scene in m_Scenes)
  197. {
  198. ScenePresence sp = scene.GetScenePresence(toAgentID);
  199. if(sp != null && !sp.IsChildAgent)
  200. {
  201. scene.EventManager.TriggerIncomingInstantMessage(gim);
  202. successful = true;
  203. }
  204. }
  205. if (!successful)
  206. {
  207. // If the message can't be delivered to an agent, it
  208. // is likely to be a group IM. On a group IM, the
  209. // imSessionID = toAgentID = group id. Raise the
  210. // unhandled IM event to give the groups module
  211. // a chance to pick it up. We raise that in a random
  212. // scene, since the groups module is shared.
  213. //
  214. m_Scenes[0].EventManager.TriggerUnhandledInstantMessage(gim);
  215. }
  216. return successful;
  217. }
  218. protected void HandleUndeliveredMessage(GridInstantMessage im, MessageResultNotification result)
  219. {
  220. UndeliveredMessage handlerUndeliveredMessage = OnUndeliveredMessage;
  221. // If this event has handlers, then an IM from an agent will be
  222. // considered delivered. This will suppress the error message.
  223. //
  224. if (handlerUndeliveredMessage != null)
  225. {
  226. handlerUndeliveredMessage(im);
  227. if (im.dialog == (byte)InstantMessageDialog.MessageFromAgent)
  228. result(true);
  229. else
  230. result(false);
  231. return;
  232. }
  233. //m_log.DebugFormat("[INSTANT MESSAGE]: Undeliverable");
  234. result(false);
  235. }
  236. private string TryGetRecipientUUI(UUID fromAgent, UUID toAgent)
  237. {
  238. // Let's call back the fromAgent's user agent service
  239. // Maybe that service knows about the toAgent
  240. IClientAPI client = LocateClientObject(fromAgent);
  241. if (client != null)
  242. {
  243. AgentCircuitData circuit = m_Scenes[0].AuthenticateHandler.GetAgentCircuitData(client.AgentId);
  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. }
  263. return string.Empty;
  264. }
  265. /// <summary>
  266. /// Find the root client for a ID
  267. /// </summary>
  268. public IClientAPI LocateClientObject(UUID agentID)
  269. {
  270. lock (m_Scenes)
  271. {
  272. foreach (Scene scene in m_Scenes)
  273. {
  274. ScenePresence presence = scene.GetScenePresence(agentID);
  275. if (presence != null && !presence.IsChildAgent)
  276. return presence.ControllingClient;
  277. }
  278. }
  279. return null;
  280. }
  281. #region IInstantMessageSimConnector
  282. public bool SendInstantMessage(GridInstantMessage im)
  283. {
  284. //m_log.DebugFormat("[XXX] Hook SendInstantMessage {0}", im.message);
  285. UUID agentID = new UUID(im.toAgentID);
  286. return SendIMToScene(im, agentID);
  287. }
  288. #endregion
  289. }
  290. }