diff options
Diffstat (limited to 'OpenSim/Framework/Serialization/External/OspResolver.cs')
-rw-r--r-- | OpenSim/Framework/Serialization/External/OspResolver.cs | 183 |
1 files changed, 183 insertions, 0 deletions
diff --git a/OpenSim/Framework/Serialization/External/OspResolver.cs b/OpenSim/Framework/Serialization/External/OspResolver.cs new file mode 100644 index 0000000..7e3dd1b --- /dev/null +++ b/OpenSim/Framework/Serialization/External/OspResolver.cs | |||
@@ -0,0 +1,183 @@ | |||
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 | { | ||
37 | /// <summary> | ||
38 | /// Resolves OpenSim Profile Anchors (OSPA). An OSPA is a string used to provide information for | ||
39 | /// identifying user profiles or supplying a simple name if no profile is available. | ||
40 | /// </summary> | ||
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 | |||
52 | /// <summary> | ||
53 | /// Make an OSPA given a user UUID | ||
54 | /// </summary> | ||
55 | /// <param name="userId"></param> | ||
56 | /// <param name="commsManager"></param> | ||
57 | /// <returns>The OSPA. Null if a user with the given UUID could not be found.</returns> | ||
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 | return MakeOspa(account.FirstName, account.LastName); | ||
69 | |||
70 | return null; | ||
71 | } | ||
72 | |||
73 | /// <summary> | ||
74 | /// Make an OSPA given a user name | ||
75 | /// </summary> | ||
76 | /// <param name="name"></param> | ||
77 | /// <returns></returns> | ||
78 | public static string MakeOspa(string firstName, string lastName) | ||
79 | { | ||
80 | return | ||
81 | OSPA_PREFIX + OSPA_NAME_KEY + OSPA_PAIR_SEPARATOR + firstName + OSPA_NAME_VALUE_SEPARATOR + lastName; | ||
82 | } | ||
83 | |||
84 | /// <summary> | ||
85 | /// Resolve an osp string into the most suitable internal OpenSim identifier. | ||
86 | /// </summary> | ||
87 | /// | ||
88 | /// In some cases this will be a UUID if a suitable profile exists on the system. In other cases, this may | ||
89 | /// just return the same identifier after creating a temporary profile. | ||
90 | /// | ||
91 | /// <param name="ospa"></param> | ||
92 | /// <param name="commsManager"></param> | ||
93 | /// <returns> | ||
94 | /// A suitable UUID for use in Second Life client communication. If the string was not a valid ospa, then UUID.Zero | ||
95 | /// is returned. | ||
96 | /// </returns> | ||
97 | public static UUID ResolveOspa(string ospa, IUserAccountService userService) | ||
98 | { | ||
99 | if (!ospa.StartsWith(OSPA_PREFIX)) | ||
100 | return UUID.Zero; | ||
101 | |||
102 | // m_log.DebugFormat("[OSP RESOLVER]: Resolving {0}", ospa); | ||
103 | |||
104 | string ospaMeat = ospa.Substring(OSPA_PREFIX.Length); | ||
105 | string[] ospaTuples = ospaMeat.Split(OSPA_TUPLE_SEPARATOR_ARRAY); | ||
106 | |||
107 | foreach (string tuple in ospaTuples) | ||
108 | { | ||
109 | int tupleSeparatorIndex = tuple.IndexOf(OSPA_PAIR_SEPARATOR); | ||
110 | |||
111 | if (tupleSeparatorIndex < 0) | ||
112 | { | ||
113 | m_log.WarnFormat("[OSP RESOLVER]: Ignoring non-tuple component {0} in OSPA {1}", tuple, ospa); | ||
114 | continue; | ||
115 | } | ||
116 | |||
117 | string key = tuple.Remove(tupleSeparatorIndex).Trim(); | ||
118 | string value = tuple.Substring(tupleSeparatorIndex + 1).Trim(); | ||
119 | |||
120 | if (OSPA_NAME_KEY == key) | ||
121 | return ResolveOspaName(value, userService); | ||
122 | } | ||
123 | |||
124 | return UUID.Zero; | ||
125 | } | ||
126 | |||
127 | /// <summary> | ||
128 | /// Hash a profile name into a UUID | ||
129 | /// </summary> | ||
130 | /// <param name="name"></param> | ||
131 | /// <returns></returns> | ||
132 | public static UUID HashName(string name) | ||
133 | { | ||
134 | return new UUID(Utils.MD5(Encoding.Unicode.GetBytes(name)), 0); | ||
135 | } | ||
136 | |||
137 | /// <summary> | ||
138 | /// Resolve an OSPI name by querying existing persistent user profiles. If there is no persistent user profile | ||
139 | /// then a temporary user profile is inserted in the cache. | ||
140 | /// </summary> | ||
141 | /// <param name="name"></param> | ||
142 | /// <param name="commsManager"></param> | ||
143 | /// <returns> | ||
144 | /// An OpenSim internal identifier for the name given. Returns null if the name was not valid | ||
145 | /// </returns> | ||
146 | protected static UUID ResolveOspaName(string name, IUserAccountService userService) | ||
147 | { | ||
148 | if (userService == null) | ||
149 | return UUID.Zero; | ||
150 | |||
151 | int nameSeparatorIndex = name.IndexOf(OSPA_NAME_VALUE_SEPARATOR); | ||
152 | |||
153 | if (nameSeparatorIndex < 0) | ||
154 | { | ||
155 | m_log.WarnFormat("[OSP RESOLVER]: Ignoring unseparated name {0}", name); | ||
156 | return UUID.Zero; | ||
157 | } | ||
158 | |||
159 | string firstName = name.Remove(nameSeparatorIndex).TrimEnd(); | ||
160 | string lastName = name.Substring(nameSeparatorIndex + 1).TrimStart(); | ||
161 | |||
162 | UserAccount account = userService.GetUserAccount(UUID.Zero, firstName, lastName); | ||
163 | if (account != null) | ||
164 | return account.PrincipalID; | ||
165 | |||
166 | // XXX: Disable temporary user profile creation for now as implementation is incomplete - justincc | ||
167 | /* | ||
168 | UserProfileData tempUserProfile = new UserProfileData(); | ||
169 | tempUserProfile.FirstName = firstName; | ||
170 | tempUserProfile.SurName = lastName; | ||
171 | tempUserProfile.ID = HashName(tempUserProfile.Name); | ||
172 | |||
173 | m_log.DebugFormat( | ||
174 | "[OSP RESOLVER]: Adding temporary user profile for {0} {1}", tempUserProfile.Name, tempUserProfile.ID); | ||
175 | commsManager.UserService.AddTemporaryUserProfile(tempUserProfile); | ||
176 | |||
177 | return tempUserProfile.ID; | ||
178 | */ | ||
179 | |||
180 | return UUID.Zero; | ||
181 | } | ||
182 | } | ||
183 | } | ||