HGInstantMessageService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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.Reflection;
  29. using OpenSim.Framework;
  30. using OpenSim.Services.Interfaces;
  31. using OpenSim.Services.Connectors.InstantMessage;
  32. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  33. using OpenSim.Server.Base;
  34. using OpenMetaverse;
  35. using log4net;
  36. using Nini.Config;
  37. namespace OpenSim.Services.HypergridService
  38. {
  39. /// <summary>
  40. /// HG IM Service
  41. /// </summary>
  42. public class HGInstantMessageService : IInstantMessage
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger( MethodBase.GetCurrentMethod().DeclaringType);
  45. static bool m_Initialized = false;
  46. protected static IGridService m_GridService;
  47. protected static IPresenceService m_PresenceService;
  48. protected static IUserAgentService m_UserAgentService;
  49. protected static IOfflineIMService m_OfflineIMService;
  50. protected static IInstantMessageSimConnector m_IMSimConnector;
  51. protected static readonly ExpiringCacheOS<UUID, string> m_UserLocationMap = new ExpiringCacheOS<UUID, string>(10000);
  52. protected static readonly ExpiringCacheOS<UUID, string> m_RegionsCache = new ExpiringCacheOS<UUID, string>(60000);
  53. private static bool m_ForwardOfflineGroupMessages;
  54. private static bool m_InGatekeeper;
  55. private string m_messageKey;
  56. public HGInstantMessageService(IConfigSource config) : this(config, null)
  57. {
  58. }
  59. public HGInstantMessageService(IConfigSource config, IInstantMessageSimConnector imConnector)
  60. {
  61. if (imConnector != null)
  62. m_IMSimConnector = imConnector;
  63. if (!m_Initialized)
  64. {
  65. m_Initialized = true;
  66. IConfig serverConfig = config.Configs["HGInstantMessageService"];
  67. if (serverConfig == null)
  68. throw new Exception("No section HGInstantMessageService in config file");
  69. string gridService = serverConfig.GetString("GridService", string.Empty);
  70. if (string.IsNullOrEmpty(gridService))
  71. throw new Exception("[HG IM SERVICE]: GridService not set in [HGInstantMessageService]");
  72. string presenceService = serverConfig.GetString("PresenceService", string.Empty);
  73. if (string.IsNullOrEmpty(presenceService))
  74. throw new Exception("[HG IM SERVICE]: PresenceService not set in [HGInstantMessageService]");
  75. string userAgentService = serverConfig.GetString("UserAgentService", string.Empty);
  76. if (string.IsNullOrEmpty(userAgentService))
  77. m_log.WarnFormat("[HG IM SERVICE]: UserAgentService not set in [HGInstantMessageService]");
  78. object[] args = [ config ];
  79. try
  80. {
  81. m_GridService = ServerUtils.LoadPlugin<IGridService>(gridService, args);
  82. }
  83. catch
  84. {
  85. throw new Exception("[HG IM SERVICE]: Unable to load GridService");
  86. }
  87. try
  88. {
  89. m_PresenceService = ServerUtils.LoadPlugin<IPresenceService>(presenceService, args);
  90. }
  91. catch
  92. {
  93. throw new Exception("[HG IM SERVICE]: Unable to load PresenceService");
  94. }
  95. try
  96. {
  97. m_UserAgentService = ServerUtils.LoadPlugin<IUserAgentService>(userAgentService, args);
  98. }
  99. catch
  100. {
  101. m_log.WarnFormat("[HG IM SERVICE]: Unable to load PresenceService");
  102. }
  103. m_InGatekeeper = serverConfig.GetBoolean("InGatekeeper", false);
  104. IConfig cnf = config.Configs["Messaging"];
  105. if (cnf == null)
  106. {
  107. m_log.Debug("[HG IM SERVICE]: Starting (without [MEssaging])");
  108. return;
  109. }
  110. m_messageKey = cnf.GetString("MessageKey", string.Empty);
  111. m_ForwardOfflineGroupMessages = cnf.GetBoolean("ForwardOfflineGroupMessages", false);
  112. if (m_InGatekeeper)
  113. {
  114. m_log.Debug("[HG IM SERVICE]: Starting In Robust GateKeeper");
  115. string offlineIMService = cnf.GetString("OfflineIMService", string.Empty);
  116. if (offlineIMService != string.Empty)
  117. m_OfflineIMService = ServerUtils.LoadPlugin<IOfflineIMService>(offlineIMService, args);
  118. }
  119. else
  120. m_log.Debug("[HG IM SERVICE]: Starting");
  121. }
  122. }
  123. public bool IncomingInstantMessage(GridInstantMessage im)
  124. {
  125. //m_log.DebugFormat("[HG IM SERVICE]: Received message from {0} to {1}", im.fromAgentID, im.toAgentID);
  126. //UUID toAgentID = new UUID(im.toAgentID);
  127. bool success = false;
  128. if (m_IMSimConnector != null)
  129. {
  130. //m_log.DebugFormat("[XXX] SendIMToRegion local im connector");
  131. success = m_IMSimConnector.SendInstantMessage(im);
  132. }
  133. else
  134. {
  135. success = TrySendInstantMessage(im, "", true, false);
  136. }
  137. if (!success && m_InGatekeeper) // we do this only in the Gatekeeper IM service
  138. UndeliveredMessage(im);
  139. return success;
  140. }
  141. public bool OutgoingInstantMessage(GridInstantMessage im, string url, bool foreigner)
  142. {
  143. //m_log.DebugFormat("[HG IM SERVICE]: Sending message from {0} to {1}@{2}", im.fromAgentID, im.toAgentID, url);
  144. return TrySendInstantMessage(im, url, true, foreigner);
  145. }
  146. protected bool TrySendInstantMessage(GridInstantMessage im, string foreignerkurl, bool firstTime, bool foreigner)
  147. {
  148. UUID toAgentID = new UUID(im.toAgentID);
  149. string url = null;
  150. // first try cache
  151. if (m_UserLocationMap.TryGetValue(toAgentID, out url))
  152. {
  153. if (ForwardIMToGrid(url, im, toAgentID))
  154. return true;
  155. }
  156. // try the provided url (for a foreigner)
  157. if(!string.IsNullOrEmpty(foreignerkurl))
  158. {
  159. if (ForwardIMToGrid(foreignerkurl, im, toAgentID))
  160. return true;
  161. }
  162. //try to find it in local grid
  163. PresenceInfo[] presences = m_PresenceService.GetAgents(new string[] { toAgentID.ToString() });
  164. if (presences != null && presences.Length > 0)
  165. {
  166. foreach (PresenceInfo p in presences)
  167. {
  168. if (!p.RegionID.IsZero())
  169. {
  170. //m_log.DebugFormat("[XXX]: Found presence in {0}", p.RegionID);
  171. // stupid service does not cache region, even in region code
  172. if(m_RegionsCache.TryGetValue(p.RegionID, out url))
  173. break;
  174. GridRegion reginfo = m_GridService.GetRegionByUUID(UUID.Zero, p.RegionID);
  175. if (reginfo != null)
  176. {
  177. url = reginfo.ServerURI;
  178. m_RegionsCache.AddOrUpdate(p.RegionID, url, 300);
  179. break;
  180. }
  181. }
  182. }
  183. }
  184. if (string.IsNullOrEmpty(url) && !foreigner && m_UserAgentService != null)
  185. {
  186. // Let's check with the UAS if the user is elsewhere in HG
  187. m_log.DebugFormat("[HG IM SERVICE]: User is not present. Checking location with User Agent service");
  188. try
  189. {
  190. url = m_UserAgentService.LocateUser(toAgentID);
  191. }
  192. catch (Exception e)
  193. {
  194. m_log.Warn("[HG IM SERVICE]: LocateUser call failed ", e);
  195. url = string.Empty;
  196. }
  197. }
  198. if (string.IsNullOrEmpty(url))
  199. {
  200. m_log.DebugFormat("[HG IM SERVICE]: Unable to locate user {0}", toAgentID);
  201. return false;
  202. }
  203. // check if we've tried this before..
  204. if (!string.IsNullOrEmpty(foreignerkurl) && url.Equals(foreignerkurl, StringComparison.InvariantCultureIgnoreCase))
  205. {
  206. // m_log.Error("[GRID INSTANT MESSAGE]: Unable to deliver an instant message");
  207. m_log.DebugFormat("[HG IM SERVICE]: Unable to send to user {0}, at {1}", toAgentID, foreignerkurl);
  208. return false;
  209. }
  210. // ok, the user is around somewhere. Let's send back the reply with "success"
  211. // even though the IM may still fail. Just don't keep the caller waiting for
  212. // the entire time we're trying to deliver the IM
  213. return ForwardIMToGrid(url, im, toAgentID);
  214. }
  215. bool ForwardIMToGrid(string url, GridInstantMessage im, UUID toAgentID)
  216. {
  217. if (InstantMessageServiceConnector.SendInstantMessage(url, im, m_messageKey))
  218. {
  219. // IM delivery successful, so store the Agent's location in our local cache.
  220. m_UserLocationMap.AddOrUpdate(toAgentID, url, 120);
  221. return true;
  222. }
  223. else
  224. m_UserLocationMap.Remove(toAgentID);
  225. return false;
  226. }
  227. private bool UndeliveredMessage(GridInstantMessage im)
  228. {
  229. if (m_OfflineIMService == null)
  230. return false;
  231. if (m_ForwardOfflineGroupMessages)
  232. {
  233. switch (im.dialog)
  234. {
  235. case (byte)InstantMessageDialog.MessageFromObject:
  236. case (byte)InstantMessageDialog.MessageFromAgent:
  237. case (byte)InstantMessageDialog.GroupNotice:
  238. case (byte)InstantMessageDialog.GroupInvitation:
  239. case (byte)InstantMessageDialog.InventoryOffered:
  240. break;
  241. default:
  242. return false;
  243. }
  244. }
  245. else
  246. {
  247. switch (im.dialog)
  248. {
  249. case (byte)InstantMessageDialog.MessageFromObject:
  250. case (byte)InstantMessageDialog.MessageFromAgent:
  251. case (byte)InstantMessageDialog.InventoryOffered:
  252. break;
  253. default:
  254. return false;
  255. }
  256. }
  257. //m_log.DebugFormat("[HG IM SERVICE]: Message saved");
  258. return m_OfflineIMService.StoreMessage(im, out string reason);
  259. }
  260. }
  261. }