/// <summary>
        /// Locates the user.
        /// This is a sensitive operation, only authorized IP addresses can perform it.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="remoteClient"></param>
        /// <returns></returns>
        public XmlRpcResponse LocateUser(XmlRpcRequest request, IPEndPoint remoteClient)
        {
            Hashtable hash = new Hashtable();

            bool authorized = true;

            if (m_VerifyCallers)
            {
                authorized = false;
                foreach (string s in m_AuthorizedCallers)
                {
                    if (s == remoteClient.Address.ToString())
                    {
                        authorized = true;
                        break;
                    }
                }
            }

            if (authorized)
            {
                Hashtable requestData = (Hashtable)request.Params[0];
                //string host = (string)requestData["host"];
                //string portstr = (string)requestData["port"];
                if (requestData.ContainsKey("userID"))
                {
                    string userID_str = (string)requestData["userID"];
                    UUID   userID     = UUID.Zero;
                    UUID.TryParse(userID_str, out userID);

                    string url = m_HomeUsersService.LocateUser(userID);
                    if (url != string.Empty)
                    {
                        hash["URL"] = url;
                    }
                    else
                    {
                        hash["result"] = "Unable to locate user";
                    }
                }
            }

            XmlRpcResponse response = new XmlRpcResponse();

            response.Value = hash;
            return(response);
        }
        protected bool TrySendInstantMessage(GridInstantMessage im, object previousLocation, bool firstTime, bool foreigner)
        {
            UUID toAgentID = new UUID(im.toAgentID);

            PresenceInfo upd = null;
            string       url = string.Empty;

            bool lookupAgent = false;

            lock (m_UserLocationMap)
            {
                if (m_UserLocationMap.ContainsKey(toAgentID))
                {
                    object o = m_UserLocationMap[toAgentID];
                    if (o is PresenceInfo)
                    {
                        upd = (PresenceInfo)o;
                    }
                    else if (o is string)
                    {
                        url = (string)o;
                    }

                    // We need to compare the current location with the previous
                    // or the recursive loop will never end because it will never try to lookup the agent again
                    if (!firstTime)
                    {
                        lookupAgent = true;
                        upd         = null;
                    }
                }
                else
                {
                    lookupAgent = true;
                }
            }

            //m_log.DebugFormat("[XXX] Neeed lookup ? {0}", (lookupAgent ? "yes" : "no"));

            // Are we needing to look-up an agent?
            if (lookupAgent)
            {
                // Non-cached user agent lookup.
                PresenceInfo[] presences = m_PresenceService.GetAgents(new string[] { toAgentID.ToString() });
                if (presences != null && presences.Length > 0)
                {
                    foreach (PresenceInfo p in presences)
                    {
                        if (p.RegionID != UUID.Zero)
                        {
                            //m_log.DebugFormat("[XXX]: Found presence in {0}", p.RegionID);
                            upd = p;
                            break;
                        }
                    }
                }

                if (upd == null && !foreigner)
                {
                    // Let's check with the UAS if the user is elsewhere
                    m_log.DebugFormat("[HG IM SERVICE]: User is not present. Checking location with User Agent service");
                    try
                    {
                        url = m_UserAgentService.LocateUser(toAgentID);
                    }
                    catch (Exception e)
                    {
                        m_log.Warn("[HG IM SERVICE]: LocateUser call failed ", e);
                        url = string.Empty;
                    }
                }

                // check if we've tried this before..
                // This is one way to end the recursive loop
                //
                if (!firstTime && ((previousLocation is PresenceInfo && upd != null && upd.RegionID == ((PresenceInfo)previousLocation).RegionID) ||
                                   (previousLocation is string && upd == null && previousLocation.Equals(url))))
                {
                    // m_log.Error("[GRID INSTANT MESSAGE]: Unable to deliver an instant message");
                    m_log.DebugFormat("[HG IM SERVICE]: Fail 2 {0} {1}", previousLocation, url);

                    return(false);
                }
            }

            if (upd != null)
            {
                // ok, the user is around somewhere. Let's send back the reply with "success"
                // even though the IM may still fail. Just don't keep the caller waiting for
                // the entire time we're trying to deliver the IM
                return(SendIMToRegion(upd, im, toAgentID, foreigner));
            }
            else if (url != string.Empty)
            {
                // ok, the user is around somewhere. Let's send back the reply with "success"
                // even though the IM may still fail. Just don't keep the caller waiting for
                // the entire time we're trying to deliver the IM
                return(ForwardIMToGrid(url, im, toAgentID, foreigner));
            }
            else if (firstTime && previousLocation is string && (string)previousLocation != string.Empty)
            {
                return(ForwardIMToGrid((string)previousLocation, im, toAgentID, foreigner));
            }
            else
            {
                m_log.DebugFormat("[HG IM SERVICE]: Unable to locate user {0}", toAgentID);
            }
            return(false);
        }