diff options
author | Justin Clarke Casey | 2009-05-04 17:32:20 +0000 |
---|---|---|
committer | Justin Clarke Casey | 2009-05-04 17:32:20 +0000 |
commit | 3a27a056073afb9f1fb285df136d2800f4f9c1a3 (patch) | |
tree | a4ca943ac817663be49ef24bfff968248c2620de /OpenSim/Framework/Communications/Osp | |
parent | * Enhance some internal inventory data plugin behaviour to match what was pro... (diff) | |
download | opensim-SC_OLD-3a27a056073afb9f1fb285df136d2800f4f9c1a3.zip opensim-SC_OLD-3a27a056073afb9f1fb285df136d2800f4f9c1a3.tar.gz opensim-SC_OLD-3a27a056073afb9f1fb285df136d2800f4f9c1a3.tar.bz2 opensim-SC_OLD-3a27a056073afb9f1fb285df136d2800f4f9c1a3.tar.xz |
* refactor: move OspResolver to a different namespace
Diffstat (limited to 'OpenSim/Framework/Communications/Osp')
-rw-r--r-- | OpenSim/Framework/Communications/Osp/OspResolver.cs | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/OpenSim/Framework/Communications/Osp/OspResolver.cs b/OpenSim/Framework/Communications/Osp/OspResolver.cs new file mode 100644 index 0000000..a62e1c0 --- /dev/null +++ b/OpenSim/Framework/Communications/Osp/OspResolver.cs | |||
@@ -0,0 +1,170 @@ | |||
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 OpenSim 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.Framework.Communications.Cache; | ||
34 | |||
35 | namespace OpenSim.Framework.Communications.Osp | ||
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, CommunicationsManager commsManager) | ||
59 | { | ||
60 | CachedUserInfo userInfo = commsManager.UserProfileCacheService.GetUserDetails(userId); | ||
61 | if (userInfo != null) | ||
62 | return MakeOspa(userInfo.UserProfile.FirstName, userInfo.UserProfile.SurName); | ||
63 | |||
64 | return null; | ||
65 | } | ||
66 | |||
67 | /// <summary> | ||
68 | /// Make an OSPA given a user name | ||
69 | /// </summary> | ||
70 | /// <param name="name"></param> | ||
71 | /// <returns></returns> | ||
72 | public static string MakeOspa(string firstName, string lastName) | ||
73 | { | ||
74 | return | ||
75 | OSPA_PREFIX + OSPA_NAME_KEY + OSPA_PAIR_SEPARATOR + firstName + OSPA_NAME_VALUE_SEPARATOR + lastName; | ||
76 | } | ||
77 | |||
78 | /// <summary> | ||
79 | /// Resolve an osp string into the most suitable internal OpenSim identifier. | ||
80 | /// </summary> | ||
81 | /// | ||
82 | /// In some cases this will be a UUID if a suitable profile exists on the system. In other cases, this may | ||
83 | /// just return the same identifier after creating a temporary profile. | ||
84 | /// | ||
85 | /// <param name="ospa"></param> | ||
86 | /// <param name="commsManager"></param> | ||
87 | /// <returns> | ||
88 | /// A suitable internal OpenSim identifier. If the input string wasn't ospi data, then we simply | ||
89 | /// return that same string. If the input string was ospi data but no valid profile information has been found, | ||
90 | /// then returns null. | ||
91 | /// </returns> | ||
92 | public static string ResolveOspa(string ospa, CommunicationsManager commsManager) | ||
93 | { | ||
94 | m_log.DebugFormat("[OSP RESOLVER]: Resolving {0}", ospa); | ||
95 | |||
96 | if (!ospa.StartsWith(OSPA_PREFIX)) | ||
97 | return ospa; | ||
98 | |||
99 | string ospaMeat = ospa.Substring(OSPA_PREFIX.Length); | ||
100 | string[] ospaTuples = ospaMeat.Split(OSPA_TUPLE_SEPARATOR_ARRAY); | ||
101 | |||
102 | foreach (string tuple in ospaTuples) | ||
103 | { | ||
104 | int tupleSeparatorIndex = tuple.IndexOf(OSPA_PAIR_SEPARATOR); | ||
105 | |||
106 | if (tupleSeparatorIndex < 0) | ||
107 | { | ||
108 | m_log.WarnFormat("[OSP RESOLVER]: Ignoring non-tuple component {0} in OSPA {1}", tuple, ospa); | ||
109 | continue; | ||
110 | } | ||
111 | |||
112 | string key = tuple.Remove(tupleSeparatorIndex).Trim(); | ||
113 | string value = tuple.Substring(tupleSeparatorIndex + 1).Trim(); | ||
114 | |||
115 | if (OSPA_NAME_KEY == key) | ||
116 | return ResolveOspaName(value, commsManager); | ||
117 | } | ||
118 | |||
119 | return null; | ||
120 | } | ||
121 | |||
122 | /// <summary> | ||
123 | /// Hash a profile name into a UUID | ||
124 | /// </summary> | ||
125 | /// <param name="name"></param> | ||
126 | /// <returns></returns> | ||
127 | public static UUID HashName(string name) | ||
128 | { | ||
129 | return new UUID(Utils.MD5(Encoding.Unicode.GetBytes(name)), 0); | ||
130 | } | ||
131 | |||
132 | /// <summary> | ||
133 | /// Resolve an OSPI name by querying existing persistent user profiles. If there is no persistent user profile | ||
134 | /// then a temporary user profile is inserted in the cache. | ||
135 | /// </summary> | ||
136 | /// <param name="name"></param> | ||
137 | /// <param name="commsManager"></param> | ||
138 | /// <returns> | ||
139 | /// An OpenSim internal identifier for the name given. Returns null if the name was not valid | ||
140 | /// </returns> | ||
141 | protected static string ResolveOspaName(string name, CommunicationsManager commsManager) | ||
142 | { | ||
143 | int nameSeparatorIndex = name.IndexOf(OSPA_NAME_VALUE_SEPARATOR); | ||
144 | |||
145 | if (nameSeparatorIndex < 0) | ||
146 | { | ||
147 | m_log.WarnFormat("[OSP RESOLVER]: Ignoring unseparated name {0}", name); | ||
148 | return null; | ||
149 | } | ||
150 | |||
151 | string firstName = name.Remove(nameSeparatorIndex).TrimEnd(); | ||
152 | string lastName = name.Substring(nameSeparatorIndex + 1).TrimStart(); | ||
153 | |||
154 | CachedUserInfo userInfo = commsManager.UserProfileCacheService.GetUserDetails(firstName, lastName); | ||
155 | if (userInfo != null) | ||
156 | return userInfo.UserProfile.ID.ToString(); | ||
157 | |||
158 | UserProfileData tempUserProfile = new UserProfileData(); | ||
159 | tempUserProfile.FirstName = firstName; | ||
160 | tempUserProfile.SurName = lastName; | ||
161 | tempUserProfile.ID = HashName(tempUserProfile.Name); | ||
162 | |||
163 | m_log.DebugFormat( | ||
164 | "[OSP RESOLVER]: Adding temporary user profile for {0} {1}", tempUserProfile.Name, tempUserProfile.ID); | ||
165 | commsManager.UserService.AddTemporaryUserProfile(tempUserProfile); | ||
166 | |||
167 | return tempUserProfile.ID.ToString(); | ||
168 | } | ||
169 | } | ||
170 | } | ||