public bool GetUserPreferences(ref UserPreferences pref, ref string result)
 {
     string query = string.Empty;
     
     query += "SELECT imviaemail,visible,email FROM ";
     query += "usersettings WHERE ";
     query += "useruuid = :Id";
     
     OSDArray data = new OSDArray();
     
     try
     {
         using (NpgsqlConnection dbcon = new NpgsqlConnection(ConnectionString))
         {
             dbcon.Open();
             using (NpgsqlCommand cmd = new NpgsqlCommand(query, dbcon))
             {
                 cmd.Parameters.AddWithValue("Id", pref.UserId.ToString());
                 
                 using (NpgsqlDataReader reader = cmd.ExecuteReader())
                 {
                     if(reader.HasRows)
                     {
                         reader.Read();
                         bool.TryParse((string)reader["imviaemail"], out pref.IMViaEmail);
                         bool.TryParse((string)reader["visible"], out pref.Visible);
                         pref.EMail = (string)reader["email"];
                     }
                     else
                     {
                         using (NpgsqlCommand put = new NpgsqlCommand(query, dbcon))
                         {
                             query = "INSERT INTO usersettings VALUES ";
                             query += "(:Id,'false','false', '')";
                             
                             put.ExecuteNonQuery();
                         }
                     }
                 }
             }
         }
     }
     catch (Exception e)
     {
         m_log.DebugFormat("[PROFILES_DATA]" +
                          ": Get preferences exception {0}", e.Message);
         result = e.Message;
     }
     return true;
 }
 public bool UpdateUserPreferences(ref UserPreferences pref,  ref string result)
 {           
     string query = string.Empty;
     
     query += "UPDATE usersettings SET ";
     query += "imviaemail=:ImViaEmail, ";
     query += "visible=:Visible,";
     query += "WHERE useruuid=:uuid";
     
     try
     {
         using (NpgsqlConnection dbcon = new NpgsqlConnection(ConnectionString))
         {
             dbcon.Open();
             using (NpgsqlCommand cmd = new NpgsqlCommand(query, dbcon))
             {
                 cmd.Parameters.AddWithValue("ImViaEmail", pref.IMViaEmail.ToString().ToLower ());
                 cmd.Parameters.AddWithValue("Visible", pref.Visible.ToString().ToLower ());
                 cmd.Parameters.AddWithValue("uuid", pref.UserId.ToString());
                 
                 cmd.ExecuteNonQuery();
             }
         }
     }
     catch (Exception e)
     {
         m_log.DebugFormat("[PROFILES_DATA]" +
                          ": AgentInterestsUpdate exception {0}", e.Message);
         result = e.Message;
         return false;
     }
     return true;
 }
 public bool UpdateUserPreferences(ref UserPreferences pref, ref string result)
 {           
     string query = string.Empty;
     
     query += "UPDATE usersettings SET ";
     query += "imviaemail=:ImViaEmail, ";
     query += "visible=:Visible ";
     query += "WHERE useruuid=:uuid";
     
     try
     {
         using (SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand())
         {
             cmd.CommandText = query;
             cmd.Parameters.AddWithValue(":ImViaEmail", pref.IMViaEmail);
             cmd.Parameters.AddWithValue(":Visible", pref.Visible);
             cmd.Parameters.AddWithValue(":uuid", pref.UserId.ToString());
                 
             cmd.ExecuteNonQuery();
         }
     }
     catch (Exception e)
     {
         m_log.DebugFormat("[PROFILES_DATA]" +
                           ": AgentInterestsUpdate exception {0}", e.Message);
         result = e.Message;
         return false;
     }
     return true;
 }
 public bool GetUserPreferences(ref UserPreferences pref, ref string result)
 {
     IDataReader reader = null;
     string query = string.Empty;
     
     query += "SELECT imviaemail,visible,email FROM ";
     query += "usersettings WHERE ";
     query += "useruuid = :Id";
     
     OSDArray data = new OSDArray();
     
     try
     {
         using (SqliteCommand cmd = (SqliteCommand)m_connection.CreateCommand())
         {
             cmd.CommandText = query;
             cmd.Parameters.AddWithValue("?Id", pref.UserId.ToString());
                 
             using (reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
             {
                 if(reader.Read())
                 {
                     bool.TryParse((string)reader["imviaemail"], out pref.IMViaEmail);
                     bool.TryParse((string)reader["visible"], out pref.Visible);
                     pref.EMail = (string)reader["email"];
                  }
                  else
                  {
                     query = "INSERT INTO usersettings VALUES ";
                     query += "(:Id,'false','false', :Email)";
                     
                     using (SqliteCommand put = (SqliteCommand)m_connection.CreateCommand())
                     {
                         put.Parameters.AddWithValue(":Id", pref.UserId.ToString());
                         put.Parameters.AddWithValue(":Email", pref.EMail);
                         put.ExecuteNonQuery();
                             
                     }
                 }
             }
         }
     }
     catch (Exception e)
     {
         m_log.DebugFormat("[PROFILES_DATA]" +
                           ": Get preferences exception {0}", e.Message);
         result = e.Message;
         return false;
     }
     return true;
 }
        /// <summary>
        /// Users the preferences request.
        /// </summary>
        /// <param name='remoteClient'>
        /// Remote client.
        /// </param>
        public void UserPreferencesRequest(IClientAPI remoteClient)
        {
            UserPreferences pref = new UserPreferences();

            pref.UserId = remoteClient.AgentId;

            string serverURI = string.Empty;
            bool foreign = GetUserProfileServerURI(remoteClient.AgentId, out serverURI);


            object Pref = (object)pref;
            if(!rpc.JsonRpcRequest(ref Pref, "user_preferences_request", serverURI, UUID.Random().ToString()))
            {
                m_log.InfoFormat("[PROFILES]: UserPreferences request error");
                remoteClient.SendAgentAlertMessage("Error requesting preferences", false);
                return;
            }
            pref = (UserPreferences) Pref;

            remoteClient.SendUserInfoReply(pref.IMViaEmail, pref.Visible, pref.EMail);
       
        }
        public bool UpdateUserPreferences(ref UserPreferences pref,  ref string result)
        {           
            string query = string.Empty;
            
            query += "UPDATE usersettings SET ";
            query += "imviaemail=:ImViaEmail, ";
            query += "visible=:Visible, ";
            query += "email=:Email ";
            query += "WHERE useruuid=:uuid";
            
            try
            {
                using (NpgsqlConnection dbcon = new NpgsqlConnection(ConnectionString))
                {
                    dbcon.Open();
                    using (NpgsqlCommand cmd = new NpgsqlCommand(query, dbcon))
                    {
                        cmd.Parameters.Add(m_database.CreateParameter("ImViaEmail", pref.IMViaEmail));
                        cmd.Parameters.Add(m_database.CreateParameter("Visible", pref.Visible));
                        cmd.Parameters.Add(m_database.CreateParameter("EMail", pref.EMail.ToString().ToLower()));
                        cmd.Parameters.Add(m_database.CreateParameter("uuid", pref.UserId));

                        cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception e)
            {
                m_log.Error("[PROFILES_DATA]: UpdateUserPreferences exception ", e);
                result = e.Message;
                return false;
            }

            return true;
        }
        /// <summary>
        /// Updates the user preferences.
        /// </summary>
        /// <param name='imViaEmail'>
        /// Im via email.
        /// </param>
        /// <param name='visible'>
        /// Visible.
        /// </param>
        /// <param name='remoteClient'>
        /// Remote client.
        /// </param>
        public void UpdateUserPreferences(bool imViaEmail, bool visible, IClientAPI remoteClient)
        {
            UserPreferences pref = new UserPreferences();

            pref.UserId = remoteClient.AgentId;
            pref.IMViaEmail = imViaEmail;
            pref.Visible = visible;

            string serverURI = string.Empty;
            bool foreign = GetUserProfileServerURI(remoteClient.AgentId, out serverURI);

            object Pref = pref;
            if(!rpc.JsonRpcRequest(ref Pref, "user_preferences_update", serverURI, UUID.Random().ToString()))
            {
                m_log.InfoFormat("[PROFILES]: UserPreferences update error");
                remoteClient.SendAgentAlertMessage("Error updating preferences", false);
                return;   
            }
        }
 public bool UserPreferenecesUpdate(OSDMap json, ref JsonRpcResponse response)
 {
     if(!json.ContainsKey("params"))
     {
         response.Error.Code = ErrorCode.ParseError;
         response.Error.Message = "no parameters supplied";
         m_log.DebugFormat ("User Preferences Update Request");
         return false;
     }
     
     string result = string.Empty;
     UserPreferences prefs = new UserPreferences();
     object Prefs = (object)prefs;
     OSD.DeserializeMembers(ref Prefs, (OSDMap)json["params"]);
     if(Service.UserPreferencesUpdate(ref prefs, ref result))
     {
         response.Result = OSD.SerializeMembers(prefs);
         return true;
     }
     
     response.Error.Code = ErrorCode.InternalError;
     response.Error.Message = string.Format("{0}", result);
     m_log.InfoFormat("[PROFILES]: User preferences update error - {0}", response.Error.Message);
     return false;
 }
 public bool UserPreferencesRequest(ref UserPreferences pref, ref string result)
 {
     if(string.IsNullOrEmpty(pref.EMail))
     {
         UserAccount account = new UserAccount();
         if(userAccounts is UserAccountService.UserAccountService)
         {
             try
             {
                 account = userAccounts.GetUserAccount(UUID.Zero, pref.UserId);
                 if(string.IsNullOrEmpty(account.Email))
                 {
                     result = "No Email address on record!";
                     return false;
                 }
                 else
                     pref.EMail = account.Email;
             }
             catch
             {
                 m_log.Info ("[PROFILES]: UserAccountService Exception: Could not get user account");
                 result = "Missing Email address!";
                 return false;
             }
         }
         else
         {
             m_log.Info ("[PROFILES]: UserAccountService: Could not get user account");
             result = "Missing Email address!";
             return false;
         }
     }
     return ProfilesData.GetUserPreferences(ref pref, ref result);
 }
        public bool UserPreferencesRequest(ref UserPreferences pref, ref string result)
        {
            if (!ProfilesData.GetUserPreferences(ref pref, ref result))
                return false;

            if(string.IsNullOrEmpty(pref.EMail))
            {
                UserAccount account = new UserAccount();
                if(userAccounts is UserAccountService.UserAccountService)
                {
                    try
                    {
                        account = userAccounts.GetUserAccount(UUID.Zero, pref.UserId);
                        if(string.IsNullOrEmpty(account.Email))
                        {
                            pref.EMail = string.Empty;
                        }
                        else
                        {
                            pref.EMail = account.Email;
                            UserPreferencesUpdate(ref pref, ref result);
                        }
                    }
                    catch
                    {
                        m_log.Error ("[PROFILES SERVICE]: UserAccountService Exception: Could not get user account");
                        result = "UserAccountService settings error in UserProfileService!";
                        return false;
                    }
                }
                else
                {
                    m_log.Error ("[PROFILES SERVICE]: UserAccountService: Could not get user account");
                    result = "UserAccountService settings error in UserProfileService!";
                    return false;
                }
            }

            if(string.IsNullOrEmpty(pref.EMail))
                pref.EMail = "No Email Address On Record";

            return true;
        }
        public bool UpdateUserPreferences(ref UserPreferences pref, ref string result)
        {
            string query = string.Empty;

            query += "UPDATE usersettings SET ";
            query += "imviaemail=?ImViaEmail, ";
            query += "visible=?Visible, ";
            query += "email=?EMail ";
            query += "WHERE useruuid=?uuid";

            try
            {
                using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
                {
                    dbcon.Open();
                    using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
                    {
                        cmd.Parameters.AddWithValue("?ImViaEmail", pref.IMViaEmail.ToString().ToLower());
                        cmd.Parameters.AddWithValue("?Visible", pref.Visible.ToString().ToLower());
                        cmd.Parameters.AddWithValue("?uuid", pref.UserId.ToString());
                        cmd.Parameters.AddWithValue("?EMail", pref.EMail.ToString().ToLower());

                        cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception e)
            {
                m_log.ErrorFormat("[PROFILES_DATA]" +
                    ": UserPreferencesUpdate exception {0} {1}", e.Message, e.InnerException);
                result = e.Message;
                return false;
            }
            return true;
        }