diff options
author | Diva Canto | 2012-03-20 17:14:19 -0700 |
---|---|---|
committer | Diva Canto | 2012-03-20 17:14:19 -0700 |
commit | d08ad6459a03a6a5a6a551fd2b275f1c7da94d8e (patch) | |
tree | 62075048f4773a00593c658285b269459e9fcdfb /OpenSim/Services/HypergridService/HGFriendsService.cs | |
parent | Added GetUUID(first, last) on UserAgentsService so that we can finally make d... (diff) | |
download | opensim-SC-d08ad6459a03a6a5a6a551fd2b275f1c7da94d8e.zip opensim-SC-d08ad6459a03a6a5a6a551fd2b275f1c7da94d8e.tar.gz opensim-SC-d08ad6459a03a6a5a6a551fd2b275f1c7da94d8e.tar.bz2 opensim-SC-d08ad6459a03a6a5a6a551fd2b275f1c7da94d8e.tar.xz |
HG Friends: allow the establishment of HG friendships without requiring co-presence in the same sim. Using avatar picker, users can now search for names such as "first.last@grid.com:9000", find them, and request friendship. Friendship requests are stored if target user is offline. TESTED ON STANDALONE ONLY.
Diffstat (limited to 'OpenSim/Services/HypergridService/HGFriendsService.cs')
-rw-r--r-- | OpenSim/Services/HypergridService/HGFriendsService.cs | 301 |
1 files changed, 301 insertions, 0 deletions
diff --git a/OpenSim/Services/HypergridService/HGFriendsService.cs b/OpenSim/Services/HypergridService/HGFriendsService.cs new file mode 100644 index 0000000..19ee3e2 --- /dev/null +++ b/OpenSim/Services/HypergridService/HGFriendsService.cs | |||
@@ -0,0 +1,301 @@ | |||
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.Net; | ||
31 | using System.Reflection; | ||
32 | |||
33 | using OpenSim.Framework; | ||
34 | using OpenSim.Services.Connectors.Friends; | ||
35 | using OpenSim.Services.Connectors.Hypergrid; | ||
36 | using OpenSim.Services.Interfaces; | ||
37 | using GridRegion = OpenSim.Services.Interfaces.GridRegion; | ||
38 | using OpenSim.Server.Base; | ||
39 | using FriendInfo = OpenSim.Services.Interfaces.FriendInfo; | ||
40 | |||
41 | using OpenMetaverse; | ||
42 | using log4net; | ||
43 | using Nini.Config; | ||
44 | |||
45 | namespace OpenSim.Services.HypergridService | ||
46 | { | ||
47 | /// <summary> | ||
48 | /// W2W social networking | ||
49 | /// </summary> | ||
50 | public class HGFriendsService : IHGFriendsService | ||
51 | { | ||
52 | private static readonly ILog m_log = | ||
53 | LogManager.GetLogger( | ||
54 | MethodBase.GetCurrentMethod().DeclaringType); | ||
55 | |||
56 | static bool m_Initialized = false; | ||
57 | |||
58 | protected static IGridUserService m_GridUserService; | ||
59 | protected static IGridService m_GridService; | ||
60 | protected static IGatekeeperService m_GatekeeperService; | ||
61 | protected static IFriendsService m_FriendsService; | ||
62 | protected static IPresenceService m_PresenceService; | ||
63 | protected static IUserAccountService m_UserAccountService; | ||
64 | protected static IFriendsSimConnector m_FriendsLocalSimConnector; // standalone, points to HGFriendsModule | ||
65 | protected static FriendsSimConnector m_FriendsSimConnector; // grid | ||
66 | |||
67 | private static string m_ConfigName = "HGFriendsService"; | ||
68 | |||
69 | public HGFriendsService(IConfigSource config, String configName, IFriendsSimConnector localSimConn) | ||
70 | { | ||
71 | if (m_FriendsLocalSimConnector == null) | ||
72 | m_FriendsLocalSimConnector = localSimConn; | ||
73 | |||
74 | if (!m_Initialized) | ||
75 | { | ||
76 | m_Initialized = true; | ||
77 | |||
78 | if (configName != String.Empty) | ||
79 | m_ConfigName = configName; | ||
80 | |||
81 | Object[] args = new Object[] { config }; | ||
82 | |||
83 | IConfig serverConfig = config.Configs[m_ConfigName]; | ||
84 | if (serverConfig == null) | ||
85 | throw new Exception(String.Format("No section {0} in config file", m_ConfigName)); | ||
86 | |||
87 | string theService = serverConfig.GetString("FriendsService", string.Empty); | ||
88 | if (theService == String.Empty) | ||
89 | throw new Exception("No FriendsService in config file " + m_ConfigName); | ||
90 | m_FriendsService = ServerUtils.LoadPlugin<IFriendsService>(theService, args); | ||
91 | |||
92 | theService = serverConfig.GetString("UserAccountService", string.Empty); | ||
93 | if (theService == String.Empty) | ||
94 | throw new Exception("No UserAccountService in " + m_ConfigName); | ||
95 | m_UserAccountService = ServerUtils.LoadPlugin<IUserAccountService>(theService, args); | ||
96 | |||
97 | theService = serverConfig.GetString("GridService", string.Empty); | ||
98 | if (theService == String.Empty) | ||
99 | throw new Exception("No GridService in " + m_ConfigName); | ||
100 | m_GridService = ServerUtils.LoadPlugin<IGridService>(theService, args); | ||
101 | |||
102 | theService = serverConfig.GetString("PresenceService", string.Empty); | ||
103 | if (theService == String.Empty) | ||
104 | throw new Exception("No PresenceService in " + m_ConfigName); | ||
105 | m_PresenceService = ServerUtils.LoadPlugin<IPresenceService>(theService, args); | ||
106 | |||
107 | m_FriendsSimConnector = new FriendsSimConnector(); | ||
108 | |||
109 | m_log.DebugFormat("[HGFRIENDS SERVICE]: Starting..."); | ||
110 | |||
111 | } | ||
112 | } | ||
113 | |||
114 | #region IHGFriendsService | ||
115 | |||
116 | public int GetFriendPerms(UUID userID, UUID friendID) | ||
117 | { | ||
118 | FriendInfo[] friendsInfo = m_FriendsService.GetFriends(userID); | ||
119 | foreach (FriendInfo finfo in friendsInfo) | ||
120 | { | ||
121 | if (finfo.Friend.StartsWith(friendID.ToString())) | ||
122 | return finfo.TheirFlags; | ||
123 | } | ||
124 | return -1; | ||
125 | } | ||
126 | |||
127 | public bool NewFriendship(FriendInfo friend, bool verified) | ||
128 | { | ||
129 | UUID friendID; | ||
130 | string tmp = string.Empty, url = String.Empty, first = String.Empty, last = String.Empty; | ||
131 | if (!Util.ParseUniversalUserIdentifier(friend.Friend, out friendID, out url, out first, out last, out tmp)) | ||
132 | return false; | ||
133 | |||
134 | m_log.DebugFormat("[HGFRIENDS SERVICE]: New friendship {0} {1} ({2})", friend.PrincipalID, friend.Friend, verified); | ||
135 | |||
136 | // Does the friendship already exist? | ||
137 | FriendInfo[] finfos = m_FriendsService.GetFriends(friend.PrincipalID); | ||
138 | foreach (FriendInfo finfo in finfos) | ||
139 | { | ||
140 | if (finfo.Friend.StartsWith(friendID.ToString())) | ||
141 | return false; | ||
142 | } | ||
143 | // Verified user session. But the user needs to confirm friendship when he gets home | ||
144 | if (verified) | ||
145 | return m_FriendsService.StoreFriend(friend.PrincipalID.ToString(), friend.Friend, 0); | ||
146 | |||
147 | // Does the reverted friendship exist? meaning that this user initiated the request | ||
148 | finfos = m_FriendsService.GetFriends(friendID); | ||
149 | bool userInitiatedOffer = false; | ||
150 | foreach (FriendInfo finfo in finfos) | ||
151 | { | ||
152 | if (friend.Friend.StartsWith(finfo.PrincipalID.ToString()) && finfo.Friend.StartsWith(friend.PrincipalID.ToString()) && finfo.TheirFlags == -1) | ||
153 | { | ||
154 | userInitiatedOffer = true; | ||
155 | // Let's delete the existing friendship relations that was stored | ||
156 | m_FriendsService.Delete(friendID, finfo.Friend); | ||
157 | break; | ||
158 | } | ||
159 | } | ||
160 | |||
161 | if (userInitiatedOffer) | ||
162 | { | ||
163 | m_FriendsService.StoreFriend(friend.PrincipalID.ToString(), friend.Friend, 1); | ||
164 | m_FriendsService.StoreFriend(friend.Friend, friend.PrincipalID.ToString(), 1); | ||
165 | // notify the user | ||
166 | ForwardToSim("ApproveFriendshipRequest", friendID, Util.UniversalName(first, last, url), "", friend.PrincipalID, ""); | ||
167 | return true; | ||
168 | } | ||
169 | return false; | ||
170 | } | ||
171 | |||
172 | public bool DeleteFriendship(FriendInfo friend, string secret) | ||
173 | { | ||
174 | FriendInfo[] finfos = m_FriendsService.GetFriends(friend.PrincipalID); | ||
175 | foreach (FriendInfo finfo in finfos) | ||
176 | { | ||
177 | // We check the secret here. Or if the friendship request was initiated here, and was declined | ||
178 | if (finfo.Friend.StartsWith(friend.Friend) && finfo.Friend.EndsWith(secret)) | ||
179 | { | ||
180 | m_log.DebugFormat("[HGFRIENDS SERVICE]: Delete friendship {0} {1}", friend.PrincipalID, friend.Friend); | ||
181 | m_FriendsService.Delete(friend.PrincipalID, finfo.Friend); | ||
182 | m_FriendsService.Delete(finfo.Friend, friend.PrincipalID.ToString()); | ||
183 | |||
184 | return true; | ||
185 | } | ||
186 | } | ||
187 | |||
188 | return false; | ||
189 | } | ||
190 | |||
191 | public bool FriendshipOffered(UUID fromID, string fromName, UUID toID, string message) | ||
192 | { | ||
193 | UserAccount account = m_UserAccountService.GetUserAccount(UUID.Zero, toID); | ||
194 | if (account == null) | ||
195 | return false; | ||
196 | |||
197 | // OK, we have that user here. | ||
198 | // So let's send back the call, but start a thread to continue | ||
199 | // with the verification and the actual action. | ||
200 | |||
201 | Util.FireAndForget(delegate { ProcessFriendshipOffered(fromID, fromName, toID, message); }); | ||
202 | |||
203 | return true; | ||
204 | } | ||
205 | |||
206 | public bool ValidateFriendshipOffered(UUID fromID, UUID toID) | ||
207 | { | ||
208 | FriendInfo[] finfos = m_FriendsService.GetFriends(toID.ToString()); | ||
209 | foreach (FriendInfo fi in finfos) | ||
210 | { | ||
211 | if (fi.Friend.StartsWith(fromID.ToString()) && fi.TheirFlags == -1) | ||
212 | return true; | ||
213 | } | ||
214 | return false; | ||
215 | } | ||
216 | |||
217 | #endregion IHGFriendsService | ||
218 | |||
219 | #region Aux | ||
220 | |||
221 | private void ProcessFriendshipOffered(UUID fromID, String fromName, UUID toID, String message) | ||
222 | { | ||
223 | // Great, it's a genuine request. Let's proceed. | ||
224 | // But now we need to confirm that the requester is who he says he is | ||
225 | // before we act on the friendship request. | ||
226 | |||
227 | if (!fromName.Contains("@")) | ||
228 | return; | ||
229 | |||
230 | string[] parts = fromName.Split(new char[] {'@'}); | ||
231 | if (parts.Length != 2) | ||
232 | return; | ||
233 | |||
234 | string uriStr = "http://" + parts[1]; | ||
235 | try | ||
236 | { | ||
237 | new Uri(uriStr); | ||
238 | } | ||
239 | catch (UriFormatException) | ||
240 | { | ||
241 | return; | ||
242 | } | ||
243 | |||
244 | UserAgentServiceConnector uasConn = new UserAgentServiceConnector(uriStr); | ||
245 | Dictionary<string, object> servers = uasConn.GetServerURLs(fromID); | ||
246 | if (!servers.ContainsKey("FriendsServerURI")) | ||
247 | return; | ||
248 | |||
249 | HGFriendsServicesConnector friendsConn = new HGFriendsServicesConnector(servers["FriendsServerURI"].ToString()); | ||
250 | if (!friendsConn.ValidateFriendshipOffered(fromID, toID)) | ||
251 | { | ||
252 | m_log.WarnFormat("[HGFRIENDS SERVICE]: Friendship request from {0} to {1} is invalid. Impersonations?", fromID, toID); | ||
253 | return; | ||
254 | } | ||
255 | |||
256 | string fromUUI = Util.UniversalIdentifier(fromID, parts[0], "@" + parts[1], uriStr); | ||
257 | // OK, we're good! | ||
258 | ForwardToSim("FriendshipOffered", fromID, fromName, fromUUI, toID, message); | ||
259 | } | ||
260 | |||
261 | private bool ForwardToSim(string op, UUID fromID, string name, String fromUUI, UUID toID, string message) | ||
262 | { | ||
263 | PresenceInfo session = null; | ||
264 | GridRegion region = null; | ||
265 | PresenceInfo[] sessions = m_PresenceService.GetAgents(new string[] { toID.ToString() }); | ||
266 | if (sessions != null && sessions.Length > 0) | ||
267 | session = sessions[0]; | ||
268 | if (session != null) | ||
269 | region = m_GridService.GetRegionByUUID(UUID.Zero, session.RegionID); | ||
270 | |||
271 | switch (op) | ||
272 | { | ||
273 | case "FriendshipOffered": | ||
274 | // Let's store backwards | ||
275 | string secret = UUID.Random().ToString().Substring(0, 8); | ||
276 | m_FriendsService.StoreFriend(toID.ToString(), fromUUI + ";" + secret, 0); | ||
277 | if (m_FriendsLocalSimConnector != null) // standalone | ||
278 | { | ||
279 | GridInstantMessage im = new GridInstantMessage(null, fromID, name, toID, | ||
280 | (byte)InstantMessageDialog.FriendshipOffered, message, false, Vector3.Zero); | ||
281 | // !! HACK | ||
282 | im.imSessionID = im.fromAgentID; | ||
283 | return m_FriendsLocalSimConnector.LocalFriendshipOffered(toID, im); | ||
284 | } | ||
285 | else if (region != null) // grid | ||
286 | return m_FriendsSimConnector.FriendshipOffered(region, fromID, toID, message, name); | ||
287 | break; | ||
288 | case "ApproveFriendshipRequest": | ||
289 | if (m_FriendsLocalSimConnector != null) // standalone | ||
290 | return m_FriendsLocalSimConnector.LocalFriendshipApproved(fromID, name, toID); | ||
291 | else if (region != null) //grid | ||
292 | return m_FriendsSimConnector.FriendshipApproved(region, fromID, name, toID); | ||
293 | break; | ||
294 | } | ||
295 | |||
296 | return false; | ||
297 | } | ||
298 | |||
299 | #endregion Aux | ||
300 | } | ||
301 | } | ||