diff options
Diffstat (limited to 'OpenSim/Framework/Communications/OspResolver.cs')
-rw-r--r-- | OpenSim/Framework/Communications/OspResolver.cs | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/OpenSim/Framework/Communications/OspResolver.cs b/OpenSim/Framework/Communications/OspResolver.cs new file mode 100644 index 0000000..708bb84 --- /dev/null +++ b/OpenSim/Framework/Communications/OspResolver.cs | |||
@@ -0,0 +1,130 @@ | |||
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 | ||
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 = "ospi:"; | ||
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_KEY_VALUE_PAIR_SEPARATOR = "="; | ||
51 | |||
52 | /// <summary> | ||
53 | /// Resolve an osp string into the most suitable internal OpenSim identifier. | ||
54 | /// </summary> | ||
55 | /// | ||
56 | /// In some cases this will be a UUID if a suitable profile exists on the system. In other cases, this may | ||
57 | /// just return the same identifier after creating a temporary profile. | ||
58 | /// | ||
59 | /// <param name="ospa"></param> | ||
60 | /// <param name="commsManager"></param> | ||
61 | /// <returns> | ||
62 | /// A suitable internal OpenSim identifier. If the input string wasn't ospi data, then we simply | ||
63 | /// return that same string. If the input string was ospi data but no valid profile information has been found, | ||
64 | /// then returns null. | ||
65 | /// </returns> | ||
66 | public static string Resolve(string ospa, CommunicationsManager commsManager) | ||
67 | { | ||
68 | if (!ospa.StartsWith(OSPA_PREFIX)) | ||
69 | return ospa; | ||
70 | |||
71 | string ospaMeat = ospa.Substring(OSPA_PREFIX.Length); | ||
72 | string[] ospaTuples = ospaMeat.Split(OSPA_TUPLE_SEPARATOR_ARRAY); | ||
73 | |||
74 | foreach (string tuple in ospaTuples) | ||
75 | { | ||
76 | int tupleSeparatorIndex = tuple.IndexOf(OSPA_TUPLE_SEPARATOR); | ||
77 | |||
78 | if (tupleSeparatorIndex < 0) | ||
79 | { | ||
80 | m_log.WarnFormat("[OSPA RESOLVER]: Ignoring non-tuple component {0} in OSPA {1}", tuple, ospa); | ||
81 | continue; | ||
82 | } | ||
83 | |||
84 | string key = tuple.Remove(tupleSeparatorIndex).Trim(); | ||
85 | string value = tuple.Substring(tupleSeparatorIndex + 1).Trim(); | ||
86 | |||
87 | if (OSPA_NAME_KEY == key) | ||
88 | return ResolveOspaName(value, commsManager); | ||
89 | } | ||
90 | |||
91 | return null; | ||
92 | } | ||
93 | |||
94 | /// <summary> | ||
95 | /// Resolve an OSPI name by querying existing persistent user profiles. If there is no persistent user profile | ||
96 | /// then a temporary user profile is inserted in the cache. | ||
97 | /// </summary> | ||
98 | /// <param name="name"></param> | ||
99 | /// <param name="commsManager"></param> | ||
100 | /// <returns> | ||
101 | /// An OpenSim internal identifier for the name given. Returns null if the name was not valid | ||
102 | /// </returns> | ||
103 | protected static string ResolveOspaName(string name, CommunicationsManager commsManager) | ||
104 | { | ||
105 | int nameSeparatorIndex = name.IndexOf(OSPA_NAME_VALUE_SEPARATOR); | ||
106 | |||
107 | if (nameSeparatorIndex < 0) | ||
108 | { | ||
109 | m_log.WarnFormat("[OSPA RESOLVER]: Ignoring unseparated name {0}", name); | ||
110 | return null; | ||
111 | } | ||
112 | |||
113 | string firstName = name.Remove(nameSeparatorIndex).TrimEnd(); | ||
114 | string lastName = name.Substring(nameSeparatorIndex + 1).TrimStart(); | ||
115 | |||
116 | CachedUserInfo userInfo = commsManager.UserProfileCacheService.GetUserDetails(firstName, lastName); | ||
117 | if (userInfo != null) | ||
118 | return userInfo.UserProfile.ID.ToString(); | ||
119 | |||
120 | UserProfileData tempUserProfile = new UserProfileData(); | ||
121 | tempUserProfile.FirstName = firstName; | ||
122 | tempUserProfile.SurName = lastName; | ||
123 | tempUserProfile.ID = new UUID(Utils.MD5(Encoding.Unicode.GetBytes(tempUserProfile.Name)), 0); | ||
124 | |||
125 | commsManager.UserService.AddTemporaryUserProfile(tempUserProfile); | ||
126 | |||
127 | return tempUserProfile.ID.ToString(); | ||
128 | } | ||
129 | } | ||
130 | } | ||