diff options
Diffstat (limited to 'OpenSim/Services/Connectors/SimianGrid/SimianFriendsServiceConnector.cs')
-rw-r--r-- | OpenSim/Services/Connectors/SimianGrid/SimianFriendsServiceConnector.cs | 232 |
1 files changed, 232 insertions, 0 deletions
diff --git a/OpenSim/Services/Connectors/SimianGrid/SimianFriendsServiceConnector.cs b/OpenSim/Services/Connectors/SimianGrid/SimianFriendsServiceConnector.cs new file mode 100644 index 0000000..b3ecc7e --- /dev/null +++ b/OpenSim/Services/Connectors/SimianGrid/SimianFriendsServiceConnector.cs | |||
@@ -0,0 +1,232 @@ | |||
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; | ||
29 | using System.Collections.Generic; | ||
30 | using System.Collections.Specialized; | ||
31 | using System.Reflection; | ||
32 | using log4net; | ||
33 | using Mono.Addins; | ||
34 | using Nini.Config; | ||
35 | using OpenMetaverse; | ||
36 | using OpenMetaverse.StructuredData; | ||
37 | using OpenSim.Framework; | ||
38 | using OpenSim.Region.Framework.Interfaces; | ||
39 | using OpenSim.Region.Framework.Scenes; | ||
40 | using OpenSim.Services.Interfaces; | ||
41 | |||
42 | using FriendInfo = OpenSim.Services.Interfaces.FriendInfo; | ||
43 | |||
44 | namespace OpenSim.Services.Connectors.SimianGrid | ||
45 | { | ||
46 | /// <summary> | ||
47 | /// Stores and retrieves friend lists from the SimianGrid backend | ||
48 | /// </summary> | ||
49 | [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule")] | ||
50 | public class SimianFriendsServiceConnector : IFriendsService, ISharedRegionModule | ||
51 | { | ||
52 | private static readonly ILog m_log = | ||
53 | LogManager.GetLogger( | ||
54 | MethodBase.GetCurrentMethod().DeclaringType); | ||
55 | |||
56 | private string m_serverUrl = String.Empty; | ||
57 | |||
58 | #region ISharedRegionModule | ||
59 | |||
60 | public Type ReplaceableInterface { get { return null; } } | ||
61 | public void RegionLoaded(Scene scene) { } | ||
62 | public void PostInitialise() { } | ||
63 | public void Close() { } | ||
64 | |||
65 | public SimianFriendsServiceConnector() { } | ||
66 | public string Name { get { return "SimianFriendsServiceConnector"; } } | ||
67 | public void AddRegion(Scene scene) { if (!String.IsNullOrEmpty(m_serverUrl)) { scene.RegisterModuleInterface<IFriendsService>(this); } } | ||
68 | public void RemoveRegion(Scene scene) { if (!String.IsNullOrEmpty(m_serverUrl)) { scene.UnregisterModuleInterface<IFriendsService>(this); } } | ||
69 | |||
70 | #endregion ISharedRegionModule | ||
71 | |||
72 | public SimianFriendsServiceConnector(IConfigSource source) | ||
73 | { | ||
74 | Initialise(source); | ||
75 | } | ||
76 | |||
77 | public void Initialise(IConfigSource source) | ||
78 | { | ||
79 | if (Simian.IsSimianEnabled(source, "FriendsServices")) | ||
80 | { | ||
81 | IConfig assetConfig = source.Configs["FriendsService"]; | ||
82 | if (assetConfig == null) | ||
83 | { | ||
84 | m_log.Error("[FRIENDS CONNECTOR]: FriendsService missing from OpenSim.ini"); | ||
85 | throw new Exception("Friends connector init error"); | ||
86 | } | ||
87 | |||
88 | string serviceURI = assetConfig.GetString("FriendsServerURI"); | ||
89 | if (String.IsNullOrEmpty(serviceURI)) | ||
90 | { | ||
91 | m_log.Error("[FRIENDS CONNECTOR]: No Server URI named in section FriendsService"); | ||
92 | throw new Exception("Friends connector init error"); | ||
93 | } | ||
94 | |||
95 | m_serverUrl = serviceURI; | ||
96 | } | ||
97 | } | ||
98 | |||
99 | #region IFriendsService | ||
100 | |||
101 | public FriendInfo[] GetFriends(UUID principalID) | ||
102 | { | ||
103 | Dictionary<UUID, FriendInfo> friends = new Dictionary<UUID, FriendInfo>(); | ||
104 | |||
105 | OSDArray friendsArray = GetFriended(principalID); | ||
106 | OSDArray friendedMeArray = GetFriendedBy(principalID); | ||
107 | |||
108 | // Load the list of friends and their granted permissions | ||
109 | for (int i = 0; i < friendsArray.Count; i++) | ||
110 | { | ||
111 | OSDMap friendEntry = friendsArray[i] as OSDMap; | ||
112 | if (friendEntry != null) | ||
113 | { | ||
114 | UUID friendID = friendEntry["Key"].AsUUID(); | ||
115 | |||
116 | FriendInfo friend = new FriendInfo(); | ||
117 | friend.PrincipalID = principalID; | ||
118 | friend.Friend = friendID.ToString(); | ||
119 | friend.MyFlags = friendEntry["Value"].AsInteger(); | ||
120 | friend.TheirFlags = -1; | ||
121 | |||
122 | friends[friendID] = friend; | ||
123 | } | ||
124 | } | ||
125 | |||
126 | // Load the permissions those friends have granted to this user | ||
127 | for (int i = 0; i < friendedMeArray.Count; i++) | ||
128 | { | ||
129 | OSDMap friendedMeEntry = friendedMeArray[i] as OSDMap; | ||
130 | if (friendedMeEntry != null) | ||
131 | { | ||
132 | UUID friendID = friendedMeEntry["OwnerID"].AsUUID(); | ||
133 | |||
134 | FriendInfo friend; | ||
135 | if (friends.TryGetValue(friendID, out friend)) | ||
136 | friend.TheirFlags = friendedMeEntry["Value"].AsInteger(); | ||
137 | } | ||
138 | } | ||
139 | |||
140 | // Convert the dictionary of friends to an array and return it | ||
141 | FriendInfo[] array = new FriendInfo[friends.Count]; | ||
142 | int j = 0; | ||
143 | foreach (FriendInfo friend in friends.Values) | ||
144 | array[j++] = friend; | ||
145 | |||
146 | return array; | ||
147 | } | ||
148 | |||
149 | public bool StoreFriend(UUID principalID, string friend, int flags) | ||
150 | { | ||
151 | NameValueCollection requestArgs = new NameValueCollection | ||
152 | { | ||
153 | { "RequestMethod", "AddGeneric" }, | ||
154 | { "OwnerID", principalID.ToString() }, | ||
155 | { "Type", "Friend" }, | ||
156 | { "Key", friend }, | ||
157 | { "Value", flags.ToString() } | ||
158 | }; | ||
159 | |||
160 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
161 | bool success = response["Success"].AsBoolean(); | ||
162 | |||
163 | if (!success) | ||
164 | m_log.Error("[FRIENDS CONNECTOR]: Failed to store friend " + friend + " for user " + principalID + ": " + response["Message"].AsString()); | ||
165 | |||
166 | return success; | ||
167 | } | ||
168 | |||
169 | public bool Delete(UUID principalID, string friend) | ||
170 | { | ||
171 | NameValueCollection requestArgs = new NameValueCollection | ||
172 | { | ||
173 | { "RequestMethod", "RemoveGeneric" }, | ||
174 | { "OwnerID", principalID.ToString() }, | ||
175 | { "Type", "Friend" }, | ||
176 | { "Key", friend } | ||
177 | }; | ||
178 | |||
179 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
180 | bool success = response["Success"].AsBoolean(); | ||
181 | |||
182 | if (!success) | ||
183 | m_log.Error("[FRIENDS CONNECTOR]: Failed to remove friend " + friend + " for user " + principalID + ": " + response["Message"].AsString()); | ||
184 | |||
185 | return success; | ||
186 | } | ||
187 | |||
188 | #endregion IFriendsService | ||
189 | |||
190 | private OSDArray GetFriended(UUID ownerID) | ||
191 | { | ||
192 | NameValueCollection requestArgs = new NameValueCollection | ||
193 | { | ||
194 | { "RequestMethod", "GetGenerics" }, | ||
195 | { "OwnerID", ownerID.ToString() }, | ||
196 | { "Type", "Friend" } | ||
197 | }; | ||
198 | |||
199 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
200 | if (response["Success"].AsBoolean() && response["Entries"] is OSDArray) | ||
201 | { | ||
202 | return (OSDArray)response["Entries"]; | ||
203 | } | ||
204 | else | ||
205 | { | ||
206 | m_log.Warn("[FRIENDS CONNECTOR]: Failed to retrieve friends for user " + ownerID + ": " + response["Message"].AsString()); | ||
207 | return new OSDArray(0); | ||
208 | } | ||
209 | } | ||
210 | |||
211 | private OSDArray GetFriendedBy(UUID ownerID) | ||
212 | { | ||
213 | NameValueCollection requestArgs = new NameValueCollection | ||
214 | { | ||
215 | { "RequestMethod", "GetGenerics" }, | ||
216 | { "Key", ownerID.ToString() }, | ||
217 | { "Type", "Friend" } | ||
218 | }; | ||
219 | |||
220 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
221 | if (response["Success"].AsBoolean() && response["Entries"] is OSDArray) | ||
222 | { | ||
223 | return (OSDArray)response["Entries"]; | ||
224 | } | ||
225 | else | ||
226 | { | ||
227 | m_log.Warn("[FRIENDS CONNECTOR]: Failed to retrieve reverse friends for user " + ownerID + ": " + response["Message"].AsString()); | ||
228 | return new OSDArray(0); | ||
229 | } | ||
230 | } | ||
231 | } | ||
232 | } | ||