OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
OspResolver.cs
Go to the documentation of this file.
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 
28 using System.Reflection;
29 using System.Text;
30 using log4net;
31 using OpenMetaverse;
32 using OpenSim.Framework;
33 using OpenSim.Services.Interfaces;
34 
35 namespace OpenSim.Framework.Serialization
36 {
41  public class OspResolver
42  {
43  private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
44 
45  public const string OSPA_PREFIX = "ospa:";
46  public const string OSPA_NAME_KEY = "n";
47  public const string OSPA_NAME_VALUE_SEPARATOR = " ";
48  public const string OSPA_TUPLE_SEPARATOR = "|";
49  public static readonly char[] OSPA_TUPLE_SEPARATOR_ARRAY = OSPA_TUPLE_SEPARATOR.ToCharArray();
50  public const string OSPA_PAIR_SEPARATOR = "=";
51 
58  public static string MakeOspa(UUID userId, IUserAccountService userService)
59  {
60  if (userService == null)
61  {
62  m_log.Warn("[OSP RESOLVER]: UserService is null");
63  return userId.ToString();
64  }
65 
66  UserAccount account = userService.GetUserAccount(UUID.Zero, userId);
67  if (account != null)
68  {
69  return MakeOspa(account.FirstName, account.LastName);
70  }
71 // else
72 // {
73 // m_log.WarnFormat("[OSP RESOLVER]: No user account for {0}", userId);
74 // System.Console.WriteLine("[OSP RESOLVER]: No user account for {0}", userId);
75 // }
76 
77  return null;
78  }
79 
85  public static string MakeOspa(string firstName, string lastName)
86  {
87  string ospa
88  = OSPA_PREFIX + OSPA_NAME_KEY + OSPA_PAIR_SEPARATOR + firstName + OSPA_NAME_VALUE_SEPARATOR + lastName;
89 
90 // m_log.DebugFormat("[OSP RESOLVER]: Made OSPA {0} for {1} {2}", ospa, firstName, lastName);
91 // System.Console.WriteLine("[OSP RESOLVER]: Made OSPA {0} for {1} {2}", ospa, firstName, lastName);
92 
93  return ospa;
94  }
95 
109  public static UUID ResolveOspa(string ospa, IUserAccountService userService)
110  {
111  if (!ospa.StartsWith(OSPA_PREFIX))
112  {
113 // m_log.DebugFormat("[OSP RESOLVER]: ResolveOspa() got unrecognized format [{0}]", ospa);
114  return UUID.Zero;
115  }
116 
117 // m_log.DebugFormat("[OSP RESOLVER]: Resolving {0}", ospa);
118 
119  string ospaMeat = ospa.Substring(OSPA_PREFIX.Length);
120  string[] ospaTuples = ospaMeat.Split(OSPA_TUPLE_SEPARATOR_ARRAY);
121 
122  foreach (string tuple in ospaTuples)
123  {
124  int tupleSeparatorIndex = tuple.IndexOf(OSPA_PAIR_SEPARATOR);
125 
126  if (tupleSeparatorIndex < 0)
127  {
128  m_log.WarnFormat("[OSP RESOLVER]: Ignoring non-tuple component {0} in OSPA {1}", tuple, ospa);
129  continue;
130  }
131 
132  string key = tuple.Remove(tupleSeparatorIndex).Trim();
133  string value = tuple.Substring(tupleSeparatorIndex + 1).Trim();
134 
135  if (OSPA_NAME_KEY == key)
136  return ResolveOspaName(value, userService);
137  }
138 
139  return UUID.Zero;
140  }
141 
147  public static UUID HashName(string name)
148  {
149  return new UUID(Utils.MD5(Encoding.Unicode.GetBytes(name)), 0);
150  }
151 
161  protected static UUID ResolveOspaName(string name, IUserAccountService userService)
162  {
163  if (userService == null)
164  return UUID.Zero;
165 
166  int nameSeparatorIndex = name.IndexOf(OSPA_NAME_VALUE_SEPARATOR);
167 
168  if (nameSeparatorIndex < 0)
169  {
170  m_log.WarnFormat("[OSP RESOLVER]: Ignoring unseparated name {0}", name);
171  return UUID.Zero;
172  }
173 
174  string firstName = name.Remove(nameSeparatorIndex).TrimEnd();
175  string lastName = name.Substring(nameSeparatorIndex + 1).TrimStart();
176 
177  UserAccount account = userService.GetUserAccount(UUID.Zero, firstName, lastName);
178  if (account != null)
179  {
180 // m_log.DebugFormat(
181 // "[OSP RESOLVER]: Found user account with uuid {0} for {1} {2}",
182 // account.PrincipalID, firstName, lastName);
183 
184  return account.PrincipalID;
185  }
186 // else
187 // {
188 // m_log.DebugFormat("[OSP RESOLVER]: No resolved OSPA user account for {0}", name);
189 // }
190 
191  // XXX: Disable temporary user profile creation for now as implementation is incomplete - justincc
192  /*
193  UserProfileData tempUserProfile = new UserProfileData();
194  tempUserProfile.FirstName = firstName;
195  tempUserProfile.SurName = lastName;
196  tempUserProfile.ID = HashName(tempUserProfile.Name);
197 
198  m_log.DebugFormat(
199  "[OSP RESOLVER]: Adding temporary user profile for {0} {1}", tempUserProfile.Name, tempUserProfile.ID);
200  commsManager.UserService.AddTemporaryUserProfile(tempUserProfile);
201 
202  return tempUserProfile.ID;
203  */
204 
205  return UUID.Zero;
206  }
207  }
208 }
static UUID ResolveOspa(string ospa, IUserAccountService userService)
Resolve an osp string into the most suitable internal OpenSim identifier.
Definition: OspResolver.cs:109
static UUID HashName(string name)
Hash a profile name into a UUID
Definition: OspResolver.cs:147
OpenSim.Server.Handlers.Simulation.Utils Utils
static string MakeOspa(UUID userId, IUserAccountService userService)
Make an OSPA given a user UUID
Definition: OspResolver.cs:58
OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString key
Definition: ICM_Api.cs:31
static string MakeOspa(string firstName, string lastName)
Make an OSPA given a user name
Definition: OspResolver.cs:85
Resolves OpenSim Profile Anchors (OSPA). An OSPA is a string used to provide information for identify...
Definition: OspResolver.cs:41
static UUID ResolveOspaName(string name, IUserAccountService userService)
Resolve an OSPI name by querying existing persistent user profiles. If there is no persistent user pr...
Definition: OspResolver.cs:161