29 using System.Collections.Generic;
31 using System.Reflection;
33 using OpenSim.Framework;
34 using OpenSim.Services.Connectors.Friends;
35 using OpenSim.Services.Connectors.Hypergrid;
36 using OpenSim.Services.Interfaces;
37 using OpenSim.Services.Connectors.InstantMessage;
39 using OpenSim.Server.Base;
46 namespace OpenSim.Services.HypergridService
53 private static readonly ILog m_log =
55 MethodBase.GetCurrentMethod().DeclaringType);
57 private const double CACHE_EXPIRATION_SECONDS = 120000.0;
59 static bool m_Initialized =
false;
68 protected static Dictionary<UUID, object> m_UserLocationMap =
new Dictionary<UUID, object>();
69 private static ExpiringCache<UUID, GridRegion> m_RegionCache;
71 private static bool m_ForwardOfflineGroupMessages;
72 private static bool m_InGatekeeper;
81 if (imConnector != null)
82 m_IMSimConnector = imConnector;
88 IConfig serverConfig = config.Configs[
"HGInstantMessageService"];
89 if (serverConfig == null)
90 throw new Exception(String.Format(
"No section HGInstantMessageService in config file"));
92 string gridService = serverConfig.GetString(
"GridService", String.Empty);
93 string presenceService = serverConfig.GetString(
"PresenceService", String.Empty);
94 string userAgentService = serverConfig.GetString(
"UserAgentService", String.Empty);
95 m_InGatekeeper = serverConfig.GetBoolean(
"InGatekeeper",
false);
96 m_log.DebugFormat(
"[HG IM SERVICE]: Starting... InRobust? {0}", m_InGatekeeper);
98 if (gridService ==
string.Empty || presenceService ==
string.Empty)
99 throw new Exception(String.Format(
"Incomplete specifications, InstantMessage Service cannot function."));
101 Object[] args =
new Object[] { config };
102 m_GridService = ServerUtils.LoadPlugin<
IGridService>(gridService, args);
103 m_PresenceService = ServerUtils.LoadPlugin<
IPresenceService>(presenceService, args);
106 m_UserAgentService = ServerUtils.LoadPlugin<
IUserAgentService>(userAgentService, args);
110 m_log.WarnFormat(
"[HG IM SERVICE]: Unable to create User Agent Service. Missing config var in [HGInstantMessageService]?");
113 m_RegionCache =
new ExpiringCache<UUID, GridRegion>();
115 IConfig cnf = config.Configs[
"Messaging"];
121 m_ForwardOfflineGroupMessages = cnf.GetBoolean(
"ForwardOfflineGroupMessages",
false);
125 string offlineIMService = cnf.GetString(
"OfflineIMService", string.Empty);
126 if (offlineIMService !=
string.Empty)
127 m_OfflineIMService = ServerUtils.LoadPlugin<
IOfflineIMService>(offlineIMService, args);
137 bool success =
false;
138 if (m_IMSimConnector != null)
141 success = m_IMSimConnector.SendInstantMessage(im);
145 success = TrySendInstantMessage(im,
"",
true,
false);
148 if (!success && m_InGatekeeper)
157 if (url !=
string.Empty)
158 return TrySendInstantMessage(im, url,
true, foreigner);
162 upd.RegionID = UUID.Zero;
163 return TrySendInstantMessage(im, upd,
true, foreigner);
173 string url = string.Empty;
175 bool lookupAgent =
false;
177 lock (m_UserLocationMap)
179 if (m_UserLocationMap.ContainsKey(toAgentID))
181 object o = m_UserLocationMap[toAgentID];
184 else if (o is
string)
207 PresenceInfo[] presences = m_PresenceService.GetAgents(
new string[] { toAgentID.ToString() });
208 if (presences != null && presences.Length > 0)
221 if (upd == null && !foreigner)
224 m_log.DebugFormat(
"[HG IM SERVICE]: User is not present. Checking location with User Agent service");
227 url = m_UserAgentService.LocateUser(toAgentID);
231 m_log.Warn(
"[HG IM SERVICE]: LocateUser call failed ", e);
240 (previousLocation is
string && upd == null && previousLocation.Equals(url))))
243 m_log.DebugFormat(
"[HG IM SERVICE]: Fail 2 {0} {1}", previousLocation, url);
254 return SendIMToRegion(upd, im, toAgentID, foreigner);
256 else if (url !=
string.Empty)
261 return ForwardIMToGrid(url, im, toAgentID, foreigner);
263 else if (firstTime && previousLocation is
string && (
string)previousLocation !=
string.Empty)
265 return ForwardIMToGrid((
string)previousLocation, im, toAgentID, foreigner);
268 m_log.DebugFormat(
"[HG IM SERVICE]: Unable to locate user {0}", toAgentID);
274 bool imresult =
false;
276 if (!m_RegionCache.TryGetValue(upd.
RegionID, out reginfo))
278 reginfo = m_GridService.GetRegionByUUID(UUID.Zero , upd.RegionID);
280 m_RegionCache.AddOrUpdate(upd.RegionID, reginfo, CACHE_EXPIRATION_SECONDS);
285 imresult = InstantMessageServiceConnector.SendInstantMessage(reginfo.ServerURI, im);
289 m_log.DebugFormat(
"[HG IM SERVICE]: Failed to deliver message to {0}", reginfo.ServerURI);
296 lock (m_UserLocationMap)
298 if (m_UserLocationMap.ContainsKey(toAgentID))
300 m_UserLocationMap[toAgentID] = upd;
304 m_UserLocationMap.Add(toAgentID, upd);
318 return TrySendInstantMessage(im, upd,
false, foreigner);
322 bool ForwardIMToGrid(
string url,
GridInstantMessage im, UUID toAgentID,
bool foreigner)
327 lock (m_UserLocationMap)
329 if (m_UserLocationMap.ContainsKey(toAgentID))
331 m_UserLocationMap[toAgentID] = url;
335 m_UserLocationMap.Add(toAgentID, url);
346 return TrySendInstantMessage(im, url,
false, foreigner);
352 if (m_OfflineIMService == null)
355 if (im.
dialog != (byte)InstantMessageDialog.MessageFromObject &&
356 im.
dialog != (byte)InstantMessageDialog.MessageFromAgent &&
357 im.
dialog != (byte)InstantMessageDialog.GroupNotice &&
358 im.
dialog != (byte)InstantMessageDialog.GroupInvitation &&
359 im.
dialog != (byte)InstantMessageDialog.InventoryOffered)
364 if (!m_ForwardOfflineGroupMessages)
366 if (im.
dialog == (byte)InstantMessageDialog.GroupNotice ||
367 im.
dialog == (byte)InstantMessageDialog.GroupInvitation)
372 string reason = string.Empty;
373 return m_OfflineIMService.StoreMessage(im, out reason);
bool OutgoingInstantMessage(GridInstantMessage im, string url, bool foreigner)
delegate void UndeliveredMessage(GridInstantMessage im)
bool IncomingInstantMessage(GridInstantMessage im)
static bool SendInstantMessage(string url, GridInstantMessage im)
This actually does the XMLRPC Request
HGInstantMessageService(IConfigSource config, IInstantMessageSimConnector imConnector)
OpenSim.Services.Interfaces.GridRegion GridRegion
static IInstantMessageSimConnector m_IMSimConnector
bool TrySendInstantMessage(GridInstantMessage im, object previousLocation, bool firstTime, bool foreigner)
static IGridService m_GridService
OpenSim.Services.Interfaces.FriendInfo FriendInfo
static IPresenceService m_PresenceService
OpenSim.Services.Interfaces.PresenceInfo PresenceInfo
HGInstantMessageService(IConfigSource config)
static IUserAgentService m_UserAgentService
static IOfflineIMService m_OfflineIMService