HGMessageTransferModule.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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.Region.Framework.Interfaces;
  39. using OpenSim.Region.Framework.Scenes;
  40. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  41. using PresenceInfo = OpenSim.Services.Interfaces.PresenceInfo;
  42. using OpenSim.Services.Interfaces;
  43. using OpenSim.Services.Connectors.InstantMessage;
  44. using OpenSim.Services.Connectors.Hypergrid;
  45. using OpenSim.Server.Handlers.Hypergrid;
  46. namespace OpenSim.Region.CoreModules.Avatar.InstantMessage
  47. {
  48. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule")]
  49. public class HGMessageTransferModule : ISharedRegionModule, IMessageTransferModule, IInstantMessageSimConnector
  50. {
  51. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  52. protected bool m_Enabled = false;
  53. protected List<Scene> m_Scenes = new List<Scene>();
  54. protected IInstantMessage m_IMService;
  55. protected Dictionary<UUID, object> m_UserLocationMap = new Dictionary<UUID, object>();
  56. public event UndeliveredMessage OnUndeliveredMessage;
  57. IUserManagement m_uMan;
  58. IUserManagement UserManagementModule
  59. {
  60. get
  61. {
  62. if (m_uMan == null)
  63. m_uMan = m_Scenes[0].RequestModuleInterface<IUserManagement>();
  64. return m_uMan;
  65. }
  66. }
  67. public virtual void Initialise(IConfigSource config)
  68. {
  69. IConfig cnf = config.Configs["Messaging"];
  70. if (cnf != null && cnf.GetString(
  71. "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. // Try root avatar only first
  123. foreach (Scene scene in m_Scenes)
  124. {
  125. if (scene.Entities.ContainsKey(toAgentID) &&
  126. scene.Entities[toAgentID] is ScenePresence)
  127. {
  128. // m_log.DebugFormat(
  129. // "[INSTANT MESSAGE]: Looking for root agent {0} in {1}",
  130. // toAgentID.ToString(), scene.RegionInfo.RegionName);
  131. ScenePresence user = (ScenePresence) scene.Entities[toAgentID];
  132. if (!user.IsChildAgent)
  133. {
  134. // Local message
  135. // m_log.DebugFormat("[INSTANT MESSAGE]: Delivering IM to root agent {0} {1}", user.Name, toAgentID);
  136. user.ControllingClient.SendInstantMessage(im);
  137. // Message sent
  138. result(true);
  139. return;
  140. }
  141. }
  142. }
  143. // try child avatar second
  144. foreach (Scene scene in m_Scenes)
  145. {
  146. // m_log.DebugFormat(
  147. // "[INSTANT MESSAGE]: Looking for child of {0} in {1}", toAgentID, scene.RegionInfo.RegionName);
  148. if (scene.Entities.ContainsKey(toAgentID) &&
  149. scene.Entities[toAgentID] is ScenePresence)
  150. {
  151. // Local message
  152. ScenePresence user = (ScenePresence) scene.Entities[toAgentID];
  153. // m_log.DebugFormat("[INSTANT MESSAGE]: Delivering IM to child agent {0} {1}", user.Name, toAgentID);
  154. user.ControllingClient.SendInstantMessage(im);
  155. // Message sent
  156. result(true);
  157. return;
  158. }
  159. }
  160. // m_log.DebugFormat("[INSTANT MESSAGE]: Delivering IM to {0} via XMLRPC", im.toAgentID);
  161. // Is the user a local user?
  162. UserAccount account = m_Scenes[0].UserAccountService.GetUserAccount(m_Scenes[0].RegionInfo.ScopeID, toAgentID);
  163. string url = string.Empty;
  164. bool foreigner = false;
  165. if (account == null) // foreign user
  166. {
  167. url = UserManagementModule.GetUserServerURL(toAgentID, "IMServerURI");
  168. foreigner = true;
  169. }
  170. Util.FireAndForget(delegate
  171. {
  172. bool success = false;
  173. if (foreigner && url == string.Empty) // we don't know about this user
  174. {
  175. string recipientUUI = TryGetRecipientUUI(new UUID(im.fromAgentID), toAgentID);
  176. m_log.DebugFormat("[HG MESSAGE TRANSFER]: Got UUI {0}", recipientUUI);
  177. if (recipientUUI != string.Empty)
  178. {
  179. UUID id; string u = string.Empty, first = string.Empty, last = string.Empty, secret = string.Empty;
  180. if (Util.ParseUniversalUserIdentifier(recipientUUI, out id, out u, out first, out last, out secret))
  181. {
  182. success = m_IMService.OutgoingInstantMessage(im, u, true);
  183. if (success)
  184. UserManagementModule.AddUser(toAgentID, u + ";" + first + " " + last);
  185. }
  186. }
  187. }
  188. else
  189. success = m_IMService.OutgoingInstantMessage(im, url, foreigner);
  190. if (!success && !foreigner)
  191. HandleUndeliveredMessage(im, result);
  192. else
  193. result(success);
  194. });
  195. return;
  196. }
  197. protected bool SendIMToScene(GridInstantMessage gim, UUID toAgentID)
  198. {
  199. bool successful = false;
  200. foreach (Scene scene in m_Scenes)
  201. {
  202. if (scene.Entities.ContainsKey(toAgentID) &&
  203. scene.Entities[toAgentID] is ScenePresence)
  204. {
  205. ScenePresence user =
  206. (ScenePresence)scene.Entities[toAgentID];
  207. if (!user.IsChildAgent)
  208. {
  209. scene.EventManager.TriggerIncomingInstantMessage(gim);
  210. successful = true;
  211. }
  212. }
  213. }
  214. if (!successful)
  215. {
  216. // If the message can't be delivered to an agent, it
  217. // is likely to be a group IM. On a group IM, the
  218. // imSessionID = toAgentID = group id. Raise the
  219. // unhandled IM event to give the groups module
  220. // a chance to pick it up. We raise that in a random
  221. // scene, since the groups module is shared.
  222. //
  223. m_Scenes[0].EventManager.TriggerUnhandledInstantMessage(gim);
  224. }
  225. return successful;
  226. }
  227. protected void HandleUndeliveredMessage(GridInstantMessage im, MessageResultNotification result)
  228. {
  229. UndeliveredMessage handlerUndeliveredMessage = OnUndeliveredMessage;
  230. // If this event has handlers, then an IM from an agent will be
  231. // considered delivered. This will suppress the error message.
  232. //
  233. if (handlerUndeliveredMessage != null)
  234. {
  235. handlerUndeliveredMessage(im);
  236. if (im.dialog == (byte)InstantMessageDialog.MessageFromAgent)
  237. result(true);
  238. else
  239. result(false);
  240. return;
  241. }
  242. //m_log.DebugFormat("[INSTANT MESSAGE]: Undeliverable");
  243. result(false);
  244. }
  245. private string TryGetRecipientUUI(UUID fromAgent, UUID toAgent)
  246. {
  247. // Let's call back the fromAgent's user agent service
  248. // Maybe that service knows about the toAgent
  249. IClientAPI client = LocateClientObject(fromAgent);
  250. if (client != null)
  251. {
  252. AgentCircuitData circuit = m_Scenes[0].AuthenticateHandler.GetAgentCircuitData(client.AgentId);
  253. if (circuit != null)
  254. {
  255. if (circuit.ServiceURLs.ContainsKey("HomeURI"))
  256. {
  257. string uasURL = circuit.ServiceURLs["HomeURI"].ToString();
  258. m_log.DebugFormat("[HG MESSAGE TRANSFER]: getting UUI of user {0} from {1}", toAgent, uasURL);
  259. UserAgentServiceConnector uasConn = new UserAgentServiceConnector(uasURL);
  260. return uasConn.GetUUI(fromAgent, toAgent);
  261. }
  262. }
  263. }
  264. return string.Empty;
  265. }
  266. /// <summary>
  267. /// Find the scene for an agent
  268. /// </summary>
  269. private Scene GetClientScene(UUID agentId)
  270. {
  271. lock (m_Scenes)
  272. {
  273. foreach (Scene scene in m_Scenes)
  274. {
  275. ScenePresence presence = scene.GetScenePresence(agentId);
  276. if (presence != null && !presence.IsChildAgent)
  277. return scene;
  278. }
  279. }
  280. return null;
  281. }
  282. /// <summary>
  283. /// Find the client for a ID
  284. /// </summary>
  285. public IClientAPI LocateClientObject(UUID agentID)
  286. {
  287. Scene scene = GetClientScene(agentID);
  288. if (scene != null)
  289. {
  290. ScenePresence presence = scene.GetScenePresence(agentID);
  291. if (presence != null)
  292. return presence.ControllingClient;
  293. }
  294. return null;
  295. }
  296. #region IInstantMessageSimConnector
  297. public bool SendInstantMessage(GridInstantMessage im)
  298. {
  299. //m_log.DebugFormat("[XXX] Hook SendInstantMessage {0}", im.message);
  300. UUID agentID = new UUID(im.toAgentID);
  301. return SendIMToScene(im, agentID);
  302. }
  303. #endregion
  304. }
  305. }