diff options
Diffstat (limited to 'OpenSim/Services/Connectors/SimianGrid/SimianFriendsServiceConnector.cs')
-rw-r--r-- | OpenSim/Services/Connectors/SimianGrid/SimianFriendsServiceConnector.cs | 240 |
1 files changed, 240 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..89f3594 --- /dev/null +++ b/OpenSim/Services/Connectors/SimianGrid/SimianFriendsServiceConnector.cs | |||
@@ -0,0 +1,240 @@ | |||
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 | bool isSimianEnabled = false; | ||
80 | |||
81 | if (source.Configs["Friends"] != null) | ||
82 | { | ||
83 | string module = source.Configs["Friends"].GetString("Connector"); | ||
84 | isSimianEnabled = !String.IsNullOrEmpty(module) && module.EndsWith(this.Name); | ||
85 | } | ||
86 | |||
87 | if (isSimianEnabled) | ||
88 | { | ||
89 | IConfig assetConfig = source.Configs["FriendsService"]; | ||
90 | if (assetConfig == null) | ||
91 | { | ||
92 | m_log.Error("[SIMIAN FRIENDS CONNECTOR]: FriendsService missing from OpenSim.ini"); | ||
93 | throw new Exception("Friends connector init error"); | ||
94 | } | ||
95 | |||
96 | string serviceURI = assetConfig.GetString("FriendsServerURI"); | ||
97 | if (String.IsNullOrEmpty(serviceURI)) | ||
98 | { | ||
99 | m_log.Error("[SIMIAN FRIENDS CONNECTOR]: No Server URI named in section FriendsService"); | ||
100 | throw new Exception("Friends connector init error"); | ||
101 | } | ||
102 | |||
103 | m_serverUrl = serviceURI; | ||
104 | } | ||
105 | } | ||
106 | |||
107 | #region IFriendsService | ||
108 | |||
109 | public FriendInfo[] GetFriends(UUID principalID) | ||
110 | { | ||
111 | Dictionary<UUID, FriendInfo> friends = new Dictionary<UUID, FriendInfo>(); | ||
112 | |||
113 | OSDArray friendsArray = GetFriended(principalID); | ||
114 | OSDArray friendedMeArray = GetFriendedBy(principalID); | ||
115 | |||
116 | // Load the list of friends and their granted permissions | ||
117 | for (int i = 0; i < friendsArray.Count; i++) | ||
118 | { | ||
119 | OSDMap friendEntry = friendsArray[i] as OSDMap; | ||
120 | if (friendEntry != null) | ||
121 | { | ||
122 | UUID friendID = friendEntry["Key"].AsUUID(); | ||
123 | |||
124 | FriendInfo friend = new FriendInfo(); | ||
125 | friend.PrincipalID = principalID; | ||
126 | friend.Friend = friendID.ToString(); | ||
127 | friend.MyFlags = friendEntry["Value"].AsInteger(); | ||
128 | friend.TheirFlags = -1; | ||
129 | |||
130 | friends[friendID] = friend; | ||
131 | } | ||
132 | } | ||
133 | |||
134 | // Load the permissions those friends have granted to this user | ||
135 | for (int i = 0; i < friendedMeArray.Count; i++) | ||
136 | { | ||
137 | OSDMap friendedMeEntry = friendedMeArray[i] as OSDMap; | ||
138 | if (friendedMeEntry != null) | ||
139 | { | ||
140 | UUID friendID = friendedMeEntry["OwnerID"].AsUUID(); | ||
141 | |||
142 | FriendInfo friend; | ||
143 | if (friends.TryGetValue(friendID, out friend)) | ||
144 | friend.TheirFlags = friendedMeEntry["Value"].AsInteger(); | ||
145 | } | ||
146 | } | ||
147 | |||
148 | // Convert the dictionary of friends to an array and return it | ||
149 | FriendInfo[] array = new FriendInfo[friends.Count]; | ||
150 | int j = 0; | ||
151 | foreach (FriendInfo friend in friends.Values) | ||
152 | array[j++] = friend; | ||
153 | |||
154 | return array; | ||
155 | } | ||
156 | |||
157 | public bool StoreFriend(UUID principalID, string friend, int flags) | ||
158 | { | ||
159 | NameValueCollection requestArgs = new NameValueCollection | ||
160 | { | ||
161 | { "RequestMethod", "AddGeneric" }, | ||
162 | { "OwnerID", principalID.ToString() }, | ||
163 | { "Type", "Friend" }, | ||
164 | { "Key", friend }, | ||
165 | { "Value", flags.ToString() } | ||
166 | }; | ||
167 | |||
168 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
169 | bool success = response["Success"].AsBoolean(); | ||
170 | |||
171 | if (!success) | ||
172 | m_log.Error("[SIMIAN FRIENDS CONNECTOR]: Failed to store friend " + friend + " for user " + principalID + ": " + response["Message"].AsString()); | ||
173 | |||
174 | return success; | ||
175 | } | ||
176 | |||
177 | public bool Delete(UUID principalID, string friend) | ||
178 | { | ||
179 | NameValueCollection requestArgs = new NameValueCollection | ||
180 | { | ||
181 | { "RequestMethod", "RemoveGeneric" }, | ||
182 | { "OwnerID", principalID.ToString() }, | ||
183 | { "Type", "Friend" }, | ||
184 | { "Key", friend } | ||
185 | }; | ||
186 | |||
187 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
188 | bool success = response["Success"].AsBoolean(); | ||
189 | |||
190 | if (!success) | ||
191 | m_log.Error("[SIMIAN FRIENDS CONNECTOR]: Failed to remove friend " + friend + " for user " + principalID + ": " + response["Message"].AsString()); | ||
192 | |||
193 | return success; | ||
194 | } | ||
195 | |||
196 | #endregion IFriendsService | ||
197 | |||
198 | private OSDArray GetFriended(UUID ownerID) | ||
199 | { | ||
200 | NameValueCollection requestArgs = new NameValueCollection | ||
201 | { | ||
202 | { "RequestMethod", "GetGenerics" }, | ||
203 | { "OwnerID", ownerID.ToString() }, | ||
204 | { "Type", "Friend" } | ||
205 | }; | ||
206 | |||
207 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
208 | if (response["Success"].AsBoolean() && response["Entries"] is OSDArray) | ||
209 | { | ||
210 | return (OSDArray)response["Entries"]; | ||
211 | } | ||
212 | else | ||
213 | { | ||
214 | m_log.Warn("[SIMIAN FRIENDS CONNECTOR]: Failed to retrieve friends for user " + ownerID + ": " + response["Message"].AsString()); | ||
215 | return new OSDArray(0); | ||
216 | } | ||
217 | } | ||
218 | |||
219 | private OSDArray GetFriendedBy(UUID ownerID) | ||
220 | { | ||
221 | NameValueCollection requestArgs = new NameValueCollection | ||
222 | { | ||
223 | { "RequestMethod", "GetGenerics" }, | ||
224 | { "Key", ownerID.ToString() }, | ||
225 | { "Type", "Friend" } | ||
226 | }; | ||
227 | |||
228 | OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs); | ||
229 | if (response["Success"].AsBoolean() && response["Entries"] is OSDArray) | ||
230 | { | ||
231 | return (OSDArray)response["Entries"]; | ||
232 | } | ||
233 | else | ||
234 | { | ||
235 | m_log.Warn("[SIMIAN FRIENDS CONNECTOR]: Failed to retrieve reverse friends for user " + ownerID + ": " + response["Message"].AsString()); | ||
236 | return new OSDArray(0); | ||
237 | } | ||
238 | } | ||
239 | } | ||
240 | } | ||