public FriendInfo[] GetFriends(UUID principalID)
        {
            List <FriendInfo> infos = new List <FriendInfo>();

            QueryTables tables = new QueryTables();

            tables.AddTable(m_realm, "my");
            tables.AddTable(m_realm, "his", JoinType.Inner, new[, ] {
                { "my.Friend", "his.PrincipalID" }, { "my.PrincipalID", "his.Friend" }
            });
            QueryFilter filter = new QueryFilter();

            filter.andFilters["my.PrincipalID"] = principalID;
            List <string> query = GD.Query(new string[] {
                "my.Friend",
                "my.Flags",
                "his.Flags"
            }, tables, filter, null, null, null);

            //These are used to get the other flags below

            for (int i = 0; i < query.Count; i += 3)
            {
                FriendInfo info = new FriendInfo {
                    PrincipalID = principalID,
                    Friend      = query[i],
                    MyFlags     = int.Parse(query[i + 1]),
                    TheirFlags  = int.Parse(query[i + 2])
                };
                infos.Add(info);
            }
            return(infos.ToArray());
        }
        public bool DeleteFriendship(UUID PrincipalID, UUID Friend, string secret)
        {
            FriendInfo finfo = new FriendInfo();

            finfo.PrincipalID = PrincipalID;
            finfo.Friend      = Friend.ToString();

            Dictionary <string, object> sendData = finfo.ToKeyValuePairs();

            sendData["METHOD"] = "deletefriendship";
            sendData["SECRET"] = secret;

            string reply = string.Empty;
            string uri   = m_ServerURI + "/hgfriends";

            try
            {
                reply = SynchronousRestFormsRequester.MakeRequest("POST",
                                                                  uri,
                                                                  ServerUtils.BuildQueryString(sendData));
            }
            catch (Exception e)
            {
                m_log.DebugFormat("[HGFRIENDS CONNECTOR]: Exception when contacting friends server at {0}: {1}", uri, e.Message);
                return(false);
            }

            if (reply != string.Empty)
            {
                Dictionary <string, object> replyData = ServerUtils.ParseXmlResponse(reply);

                if (replyData.ContainsKey("RESULT"))
                {
                    if (replyData["RESULT"].ToString().ToLower() == "true")
                    {
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    m_log.DebugFormat("[HGFRIENDS CONNECTOR]: reply data does not contain result field");
                }
            }
            else
            {
                m_log.DebugFormat("[HGFRIENDS CONNECTOR]: received empty reply");
            }

            return(false);
        }
        public bool NewFriendship(UUID PrincipalID, string Friend)
        {
            FriendInfo finfo = new FriendInfo();

            finfo.PrincipalID = PrincipalID;
            finfo.Friend      = Friend;

            Dictionary <string, object> sendData = finfo.ToKeyValuePairs();

            sendData["METHOD"]    = "newfriendship";
            sendData["KEY"]       = m_ServiceKey;
            sendData["SESSIONID"] = m_SessionID.ToString();

            string reply = string.Empty;
            string uri   = m_ServerURI + "/hgfriends";

            try
            {
                reply = SynchronousRestFormsRequester.MakeRequest("POST",
                                                                  uri,
                                                                  ServerUtils.BuildQueryString(sendData));
            }
            catch (Exception e)
            {
                m_log.DebugFormat("[HGFRIENDS CONNECTOR]: Exception when contacting friends server at {0}: {1}", uri, e.Message);
                return(false);
            }

            if (reply != string.Empty)
            {
                Dictionary <string, object> replyData = ServerUtils.ParseXmlResponse(reply);

                if ((replyData != null) && replyData.ContainsKey("Result") && (replyData["Result"] != null))
                {
                    bool success = false;
                    Boolean.TryParse(replyData["Result"].ToString(), out success);
                    return(success);
                }
                else
                {
                    m_log.DebugFormat("[HGFRIENDS CONNECTOR]: StoreFriend {0} {1} received null response",
                                      PrincipalID, Friend);
                }
            }
            else
            {
                m_log.DebugFormat("[HGFRIENDS CONNECTOR]: StoreFriend received null reply");
            }

            return(false);
        }
        public FriendInfo[] GetFriends(UUID principalID)
        {
            List <FriendInfo> infos  = new List <FriendInfo>();
            QueryFilter       filter = new QueryFilter();

            filter.andFilters["PrincipalID"] = principalID;
            List <string> query = GD.Query(new string[] {
                "Friend",
                "Flags"
            }, m_realm, filter, null, null, null);

            //These are used to get the other flags below
            List <string> keys   = new List <string>();
            List <object> values = new List <object>();

            for (int i = 0; i < query.Count; i += 2)
            {
                FriendInfo info = new FriendInfo {
                    PrincipalID = principalID,
                    Friend      = query[i],
                    MyFlags     = int.Parse(query[i + 1])
                };
                infos.Add(info);

                Dictionary <string, object> where = new Dictionary <string, object>(2);
                where["PrincipalID"] = info.Friend;
                where["Friend"]      = info.PrincipalID;

                List <string> query2 = GD.Query(new string[1] {
                    "Flags"
                }, m_realm, new QueryFilter {
                    andFilters = where
                }, null, null, null);

                if (query2.Count >= 1)
                {
                    infos[infos.Count - 1].TheirFlags = int.Parse(query2[0]);
                }

                keys   = new List <string>();
                values = new List <object>();
            }
            return(infos.ToArray());
        }
        public bool DeleteFriendship(UUID PrincipalID, UUID Friend, string secret)
        {
            FriendInfo finfo = new FriendInfo {PrincipalID = PrincipalID, Friend = Friend.ToString()};

            Dictionary<string, object> sendData = finfo.ToKVP();

            sendData["METHOD"] = "deletefriendship";
            sendData["SECRET"] = secret;

            string reply = string.Empty;
            try
            {
                reply = SynchronousRestFormsRequester.MakeRequest ("POST",
                        m_ServerURI + "/hgfriends",
                        WebUtils.BuildQueryString (sendData));
            }
            catch (Exception e)
            {
                MainConsole.Instance.DebugFormat ("[HGFRIENDS CONNECTOR]: Exception when contacting friends server: {0}", e.Message);
                return false;
            }

            if (reply != string.Empty)
            {
                Dictionary<string, object> replyData = WebUtils.ParseXmlResponse (reply);

                if ((replyData != null) && replyData.ContainsKey ("Result") && (replyData["Result"] != null))
                {
                    bool success = false;
                    Boolean.TryParse (replyData["Result"].ToString (), out success);
                    return success;
                }
                MainConsole.Instance.DebugFormat ("[HGFRIENDS CONNECTOR]: Delete {0} {1} received null response",
                                                  PrincipalID, Friend);
            }
            else
                MainConsole.Instance.DebugFormat ("[HGFRIENDS CONNECTOR]: DeleteFriend received null reply");

            return false;
        }
        public FriendInfo[] GetFriends(UUID principalID)
        {
            List<FriendInfo> infos = new List<FriendInfo>();

            QueryTables tables = new QueryTables();
            tables.AddTable(m_realm, "my");
            tables.AddTable(m_realm, "his", JoinType.Inner, new[,] { { "my.Friend", "his.PrincipalID" }, { "my.PrincipalID", "his.Friend" } });
            QueryFilter filter = new QueryFilter();
            filter.andFilters["my.PrincipalID"] = principalID;
            List<string> query = GD.Query(new string[]{
                "my.Friend",
                "my.Flags",
                "his.Flags"
            }, tables, filter, null, null, null);

            //These are used to get the other flags below

            for (int i = 0; i < query.Count; i += 3)
            {
                FriendInfo info = new FriendInfo{
                    PrincipalID = principalID,
                    Friend = query[i],
                    MyFlags = int.Parse(query[i + 1]),
                    TheirFlags = int.Parse(query[i + 2])
                };
                infos.Add(info);
            }
            return infos.ToArray();
        }
        public FriendInfo[] GetFriends(UUID principalID)
        {
            List<FriendInfo> infos = new List<FriendInfo>();
            QueryFilter filter = new QueryFilter();
            filter.andFilters["PrincipalID"] = principalID;
            List<string> query = GD.Query(new string[]{
                "Friend",
                "Flags"
            }, m_realm, filter, null, null, null);

            //These are used to get the other flags below
            List<string> keys = new List<string>();
            List<object> values = new List<object>();

            for (int i = 0; i < query.Count; i += 2)
            {
                FriendInfo info = new FriendInfo{
                    PrincipalID = principalID,
                    Friend = query[i],
                    MyFlags = int.Parse(query[i + 1])
                };
                infos.Add(info);

                Dictionary<string, object> where = new Dictionary<string, object>(2);
                where["PrincipalID"] = info.Friend;
                where["Friend"] = info.PrincipalID;

                List<string> query2 = GD.Query(new string[1] { "Flags" }, m_realm, new QueryFilter{
                    andFilters = where
                }, null, null, null);

                if (query2.Count >= 1)
                {
                    infos[infos.Count - 1].TheirFlags = int.Parse(query2[0]);
                }

                keys = new List<string>();
                values = new List<object>();
            }
            return infos.ToArray();
        }
        public FriendInfo[] GetFriends(UUID principalID)
        {
            List<FriendInfo> infos = new List<FriendInfo>();
            List<string> query = GD.Query("PrincipalID", principalID, m_realm, "Friend,Flags");

            //These are used to get the other flags below
            List<string> keys = new List<string>();
            List<object> values = new List<object>();

            for (int i = 0; i < query.Count; i += 2)
            {
                FriendInfo info = new FriendInfo
                                      {PrincipalID = principalID, Friend = query[i], MyFlags = int.Parse(query[i + 1])};
                infos.Add(info);

                keys.Add("PrincipalID");
                keys.Add("Friend");
                values.Add(info.Friend);
                values.Add(info.PrincipalID);

                List<string> query2 = GD.Query(keys.ToArray(), values.ToArray(), m_realm, "Flags");
                if (query2.Count >= 1) infos[infos.Count - 1].TheirFlags = int.Parse(query2[0]);

                keys = new List<string>();
                values = new List<object>();
            }
            return infos.ToArray();
        }
        public bool ValidateFriendshipOffered(UUID fromID, UUID toID)
        {
            FriendInfo finfo = new FriendInfo();
            finfo.PrincipalID = fromID;
            finfo.Friend = toID.ToString();

            Dictionary<string, object> sendData = finfo.ToKeyValuePairs();

            sendData["METHOD"] = "validate_friendship_offered";

            string reply = string.Empty;
            string uri = m_ServerURI + "/hgfriends";
            try
            {
                reply = SynchronousRestFormsRequester.MakeRequest("POST",
                        uri,
                        ServerUtils.BuildQueryString(sendData));
            }
            catch (Exception e)
            {
                m_log.DebugFormat("[HGFRIENDS CONNECTOR]: Exception when contacting friends server at {0}: {1}", uri, e.Message);
                return false;
            }

            if (reply != string.Empty)
            {
                Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);

                if (replyData.ContainsKey("RESULT"))
                {
                    if (replyData["RESULT"].ToString().ToLower() == "true")
                        return true;
                    else
                        return false;
                }
                else
                    m_log.DebugFormat("[HGFRIENDS CONNECTOR]: reply data does not contain result field");

            }
            else
                m_log.DebugFormat("[HGFRIENDS CONNECTOR]: received empty reply");

            return false;

        }
        public bool NewFriendship(UUID PrincipalID, string Friend)
        {
            FriendInfo finfo = new FriendInfo();
            finfo.PrincipalID = PrincipalID;
            finfo.Friend = Friend;

            Dictionary<string, object> sendData = finfo.ToKeyValuePairs();

            sendData["METHOD"] = "newfriendship";
            sendData["KEY"] = m_ServiceKey;
            sendData["SESSIONID"] = m_SessionID.ToString();

            string reply = string.Empty;
            string uri = m_ServerURI + "/hgfriends";
            try
            {
                reply = SynchronousRestFormsRequester.MakeRequest("POST",
                        uri,
                        ServerUtils.BuildQueryString(sendData));
            }
            catch (Exception e)
            {
                m_log.DebugFormat("[HGFRIENDS CONNECTOR]: Exception when contacting friends server at {0}: {1}", uri, e.Message);
                return false;
            }

            if (reply != string.Empty)
            {
                Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);

                if ((replyData != null) && replyData.ContainsKey("Result") && (replyData["Result"] != null))
                {
                    bool success = false;
                    Boolean.TryParse(replyData["Result"].ToString(), out success);
                    return success;
                }
                else
                    m_log.DebugFormat("[HGFRIENDS CONNECTOR]: StoreFriend {0} {1} received null response",
                        PrincipalID, Friend);
            }
            else
                m_log.DebugFormat("[HGFRIENDS CONNECTOR]: StoreFriend received null reply");

            return false;

        }