diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Region/CoreModules/Avatar/Friends/FriendsModule.cs | 353 |
1 files changed, 238 insertions, 115 deletions
diff --git a/OpenSim/Region/CoreModules/Avatar/Friends/FriendsModule.cs b/OpenSim/Region/CoreModules/Avatar/Friends/FriendsModule.cs index 5baf078..f82716d 100644 --- a/OpenSim/Region/CoreModules/Avatar/Friends/FriendsModule.cs +++ b/OpenSim/Region/CoreModules/Avatar/Friends/FriendsModule.cs | |||
@@ -49,6 +49,8 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends | |||
49 | { | 49 | { |
50 | public class FriendsModule : ISharedRegionModule, IFriendsModule | 50 | public class FriendsModule : ISharedRegionModule, IFriendsModule |
51 | { | 51 | { |
52 | protected bool m_Enabled = false; | ||
53 | |||
52 | protected class UserFriendData | 54 | protected class UserFriendData |
53 | { | 55 | { |
54 | public UUID PrincipalID; | 56 | public UUID PrincipalID; |
@@ -67,7 +69,7 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends | |||
67 | } | 69 | } |
68 | } | 70 | } |
69 | 71 | ||
70 | private static readonly FriendInfo[] EMPTY_FRIENDS = new FriendInfo[0]; | 72 | protected static readonly FriendInfo[] EMPTY_FRIENDS = new FriendInfo[0]; |
71 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | 73 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
72 | 74 | ||
73 | protected List<Scene> m_Scenes = new List<Scene>(); | 75 | protected List<Scene> m_Scenes = new List<Scene>(); |
@@ -130,8 +132,25 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends | |||
130 | } | 132 | } |
131 | } | 133 | } |
132 | 134 | ||
135 | #region ISharedRegionModule | ||
133 | public void Initialise(IConfigSource config) | 136 | public void Initialise(IConfigSource config) |
134 | { | 137 | { |
138 | IConfig moduleConfig = config.Configs["Modules"]; | ||
139 | if (moduleConfig != null) | ||
140 | { | ||
141 | string name = moduleConfig.GetString("FriendsModule", "FriendsModule"); | ||
142 | if (name == Name) | ||
143 | { | ||
144 | InitModule(config); | ||
145 | |||
146 | m_Enabled = true; | ||
147 | m_log.InfoFormat("[FRIENDS MODULE]: {0} enabled.", Name); | ||
148 | } | ||
149 | } | ||
150 | } | ||
151 | |||
152 | protected void InitModule(IConfigSource config) | ||
153 | { | ||
135 | IConfig friendsConfig = config.Configs["Friends"]; | 154 | IConfig friendsConfig = config.Configs["Friends"]; |
136 | if (friendsConfig != null) | 155 | if (friendsConfig != null) |
137 | { | 156 | { |
@@ -153,7 +172,6 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends | |||
153 | m_log.Error("[FRIENDS]: No Connector defined in section Friends, or failed to load, cannot continue"); | 172 | m_log.Error("[FRIENDS]: No Connector defined in section Friends, or failed to load, cannot continue"); |
154 | throw new Exception("Connector load error"); | 173 | throw new Exception("Connector load error"); |
155 | } | 174 | } |
156 | |||
157 | } | 175 | } |
158 | 176 | ||
159 | public void PostInitialise() | 177 | public void PostInitialise() |
@@ -164,8 +182,12 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends | |||
164 | { | 182 | { |
165 | } | 183 | } |
166 | 184 | ||
167 | public void AddRegion(Scene scene) | 185 | public virtual void AddRegion(Scene scene) |
168 | { | 186 | { |
187 | if (!m_Enabled) | ||
188 | return; | ||
189 | m_log.DebugFormat("[FRIENDS MODULE]: AddRegion on {0}", Name); | ||
190 | |||
169 | m_Scenes.Add(scene); | 191 | m_Scenes.Add(scene); |
170 | scene.RegisterModuleInterface<IFriendsModule>(this); | 192 | scene.RegisterModuleInterface<IFriendsModule>(this); |
171 | 193 | ||
@@ -181,10 +203,13 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends | |||
181 | 203 | ||
182 | public void RemoveRegion(Scene scene) | 204 | public void RemoveRegion(Scene scene) |
183 | { | 205 | { |
206 | if (!m_Enabled) | ||
207 | return; | ||
208 | |||
184 | m_Scenes.Remove(scene); | 209 | m_Scenes.Remove(scene); |
185 | } | 210 | } |
186 | 211 | ||
187 | public string Name | 212 | public virtual string Name |
188 | { | 213 | { |
189 | get { return "FriendsModule"; } | 214 | get { return "FriendsModule"; } |
190 | } | 215 | } |
@@ -194,13 +219,15 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends | |||
194 | get { return null; } | 219 | get { return null; } |
195 | } | 220 | } |
196 | 221 | ||
197 | public uint GetFriendPerms(UUID principalID, UUID friendID) | 222 | #endregion |
223 | |||
224 | public virtual uint GetFriendPerms(UUID principalID, UUID friendID) | ||
198 | { | 225 | { |
199 | FriendInfo[] friends = GetFriends(principalID); | 226 | FriendInfo[] friends = GetFriends(principalID); |
200 | foreach (FriendInfo fi in friends) | 227 | FriendInfo finfo = GetFriend(friends, friendID); |
228 | if (finfo != null) | ||
201 | { | 229 | { |
202 | if (fi.Friend == friendID.ToString()) | 230 | return (uint)finfo.TheirFlags; |
203 | return (uint)fi.TheirFlags; | ||
204 | } | 231 | } |
205 | 232 | ||
206 | return 0; | 233 | return 0; |
@@ -214,30 +241,34 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends | |||
214 | client.OnTerminateFriendship += OnTerminateFriendship; | 241 | client.OnTerminateFriendship += OnTerminateFriendship; |
215 | client.OnGrantUserRights += OnGrantUserRights; | 242 | client.OnGrantUserRights += OnGrantUserRights; |
216 | 243 | ||
217 | // Asynchronously fetch the friends list or increment the refcount for the existing | 244 | Util.FireAndForget(delegate { FetchFriendslist(client); }); |
218 | // friends list | 245 | } |
219 | Util.FireAndForget( | 246 | |
220 | delegate(object o) | 247 | /// Fetch the friends list or increment the refcount for the existing |
248 | /// friends list | ||
249 | /// Returns true if the list was fetched, false if it wasn't | ||
250 | protected virtual bool FetchFriendslist(IClientAPI client) | ||
251 | { | ||
252 | UUID agentID = client.AgentId; | ||
253 | lock (m_Friends) | ||
254 | { | ||
255 | UserFriendData friendsData; | ||
256 | if (m_Friends.TryGetValue(agentID, out friendsData)) | ||
221 | { | 257 | { |
222 | lock (m_Friends) | 258 | friendsData.Refcount++; |
223 | { | 259 | return false; |
224 | UserFriendData friendsData; | 260 | } |
225 | if (m_Friends.TryGetValue(client.AgentId, out friendsData)) | 261 | else |
226 | { | 262 | { |
227 | friendsData.Refcount++; | 263 | friendsData = new UserFriendData(); |
228 | } | 264 | friendsData.PrincipalID = agentID; |
229 | else | 265 | friendsData.Friends = GetFriendsFromService(client); |
230 | { | 266 | friendsData.Refcount = 1; |
231 | friendsData = new UserFriendData(); | ||
232 | friendsData.PrincipalID = client.AgentId; | ||
233 | friendsData.Friends = FriendsService.GetFriends(client.AgentId); | ||
234 | friendsData.Refcount = 1; | ||
235 | 267 | ||
236 | m_Friends[client.AgentId] = friendsData; | 268 | m_Friends[agentID] = friendsData; |
237 | } | 269 | return true; |
238 | } | ||
239 | } | 270 | } |
240 | ); | 271 | } |
241 | } | 272 | } |
242 | 273 | ||
243 | private void OnClientClosed(UUID agentID, Scene scene) | 274 | private void OnClientClosed(UUID agentID, Scene scene) |
@@ -263,14 +294,14 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends | |||
263 | 294 | ||
264 | private void OnMakeRootAgent(ScenePresence sp) | 295 | private void OnMakeRootAgent(ScenePresence sp) |
265 | { | 296 | { |
266 | UUID agentID = sp.ControllingClient.AgentId; | 297 | RefetchFriends(sp.ControllingClient); |
267 | UpdateFriendsCache(agentID); | ||
268 | } | 298 | } |
269 | 299 | ||
270 | private void OnClientLogin(IClientAPI client) | 300 | private void OnClientLogin(IClientAPI client) |
271 | { | 301 | { |
272 | UUID agentID = client.AgentId; | 302 | UUID agentID = client.AgentId; |
273 | 303 | ||
304 | //m_log.DebugFormat("[XXX]: OnClientLogin!"); | ||
274 | // Inform the friends that this user is online | 305 | // Inform the friends that this user is online |
275 | StatusChange(agentID, true); | 306 | StatusChange(agentID, true); |
276 | 307 | ||
@@ -279,7 +310,7 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends | |||
279 | m_NeedsListOfFriends.Add(agentID); | 310 | m_NeedsListOfFriends.Add(agentID); |
280 | } | 311 | } |
281 | 312 | ||
282 | public void SendFriendsOnlineIfNeeded(IClientAPI client) | 313 | public virtual bool SendFriendsOnlineIfNeeded(IClientAPI client) |
283 | { | 314 | { |
284 | UUID agentID = client.AgentId; | 315 | UUID agentID = client.AgentId; |
285 | 316 | ||
@@ -287,7 +318,7 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends | |||
287 | lock (m_NeedsListOfFriends) | 318 | lock (m_NeedsListOfFriends) |
288 | { | 319 | { |
289 | if (!m_NeedsListOfFriends.Remove(agentID)) | 320 | if (!m_NeedsListOfFriends.Remove(agentID)) |
290 | return; | 321 | return false; |
291 | } | 322 | } |
292 | 323 | ||
293 | // Send the friends online | 324 | // Send the friends online |
@@ -313,10 +344,12 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends | |||
313 | foreach (string fid in outstanding) | 344 | foreach (string fid in outstanding) |
314 | { | 345 | { |
315 | UUID fromAgentID; | 346 | UUID fromAgentID; |
316 | if (!UUID.TryParse(fid, out fromAgentID)) | 347 | string firstname = "Unknown", lastname = "User"; |
348 | if (!GetAgentInfo(client.Scene.RegionInfo.ScopeID, fid, out fromAgentID, out firstname, out lastname)) | ||
349 | { | ||
350 | m_log.DebugFormat("[FRIENDS MODULE]: skipping malformed friend {0}", fid); | ||
317 | continue; | 351 | continue; |
318 | 352 | } | |
319 | UserAccount account = m_Scenes[0].UserAccountService.GetUserAccount(client.Scene.RegionInfo.ScopeID, fromAgentID); | ||
320 | 353 | ||
321 | PresenceInfo presence = null; | 354 | PresenceInfo presence = null; |
322 | PresenceInfo[] presences = PresenceService.GetAgents(new string[] { fid }); | 355 | PresenceInfo[] presences = PresenceService.GetAgents(new string[] { fid }); |
@@ -326,13 +359,37 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends | |||
326 | im.offline = 0; | 359 | im.offline = 0; |
327 | 360 | ||
328 | im.fromAgentID = fromAgentID.Guid; | 361 | im.fromAgentID = fromAgentID.Guid; |
329 | im.fromAgentName = account.FirstName + " " + account.LastName; | 362 | im.fromAgentName = firstname + " " + lastname; |
330 | im.offline = (byte)((presence == null) ? 1 : 0); | 363 | im.offline = (byte)((presence == null) ? 1 : 0); |
331 | im.imSessionID = im.fromAgentID; | 364 | im.imSessionID = im.fromAgentID; |
365 | im.message = FriendshipMessage(fid); | ||
332 | 366 | ||
333 | // Finally | 367 | // Finally |
334 | LocalFriendshipOffered(agentID, im); | 368 | LocalFriendshipOffered(agentID, im); |
335 | } | 369 | } |
370 | |||
371 | return true; | ||
372 | } | ||
373 | |||
374 | protected virtual string FriendshipMessage(string friendID) | ||
375 | { | ||
376 | return "Will you be my friend?"; | ||
377 | } | ||
378 | |||
379 | protected virtual bool GetAgentInfo(UUID scopeID, string fid, out UUID agentID, out string first, out string last) | ||
380 | { | ||
381 | first = "Unknown"; last = "User"; | ||
382 | if (!UUID.TryParse(fid, out agentID)) | ||
383 | return false; | ||
384 | |||
385 | UserAccount account = m_Scenes[0].UserAccountService.GetUserAccount(scopeID, agentID); | ||
386 | if (account != null) | ||
387 | { | ||
388 | first = account.FirstName; | ||
389 | last = account.LastName; | ||
390 | } | ||
391 | |||
392 | return true; | ||
336 | } | 393 | } |
337 | 394 | ||
338 | List<UUID> GetOnlineFriends(UUID userID) | 395 | List<UUID> GetOnlineFriends(UUID userID) |
@@ -348,19 +405,22 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends | |||
348 | } | 405 | } |
349 | 406 | ||
350 | if (friendList.Count > 0) | 407 | if (friendList.Count > 0) |
351 | { | 408 | GetOnlineFriends(userID, friendList, online); |
352 | PresenceInfo[] presence = PresenceService.GetAgents(friendList.ToArray()); | ||
353 | foreach (PresenceInfo pi in presence) | ||
354 | { | ||
355 | UUID presenceID; | ||
356 | if (UUID.TryParse(pi.UserID, out presenceID)) | ||
357 | online.Add(presenceID); | ||
358 | } | ||
359 | } | ||
360 | 409 | ||
361 | return online; | 410 | return online; |
362 | } | 411 | } |
363 | 412 | ||
413 | protected virtual void GetOnlineFriends(UUID userID, List<string> friendList, /*collector*/ List<UUID> online) | ||
414 | { | ||
415 | PresenceInfo[] presence = PresenceService.GetAgents(friendList.ToArray()); | ||
416 | foreach (PresenceInfo pi in presence) | ||
417 | { | ||
418 | UUID presenceID; | ||
419 | if (UUID.TryParse(pi.UserID, out presenceID)) | ||
420 | online.Add(presenceID); | ||
421 | } | ||
422 | } | ||
423 | |||
364 | /// <summary> | 424 | /// <summary> |
365 | /// Find the client for a ID | 425 | /// Find the client for a ID |
366 | /// </summary> | 426 | /// </summary> |
@@ -415,51 +475,51 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends | |||
415 | Util.FireAndForget( | 475 | Util.FireAndForget( |
416 | delegate | 476 | delegate |
417 | { | 477 | { |
418 | foreach (FriendInfo fi in friendList) | 478 | m_log.DebugFormat("[FRIENDS MODULE]: Notifying {0} friends", friendList.Count); |
419 | { | 479 | // Notify about this user status |
420 | //m_log.DebugFormat("[FRIENDS]: Notifying {0}", fi.PrincipalID); | 480 | StatusNotify(friendList, agentID, online); |
421 | // Notify about this user status | ||
422 | StatusNotify(fi, agentID, online); | ||
423 | } | ||
424 | } | 481 | } |
425 | ); | 482 | ); |
426 | } | 483 | } |
427 | } | 484 | } |
428 | 485 | ||
429 | private void StatusNotify(FriendInfo friend, UUID userID, bool online) | 486 | protected virtual void StatusNotify(List<FriendInfo> friendList, UUID userID, bool online) |
430 | { | 487 | { |
431 | UUID friendID; | 488 | foreach (FriendInfo friend in friendList) |
432 | if (UUID.TryParse(friend.Friend, out friendID)) | ||
433 | { | 489 | { |
434 | // Try local | 490 | UUID friendID; |
435 | if (LocalStatusNotification(userID, friendID, online)) | 491 | if (UUID.TryParse(friend.Friend, out friendID)) |
436 | return; | ||
437 | |||
438 | // The friend is not here [as root]. Let's forward. | ||
439 | PresenceInfo[] friendSessions = PresenceService.GetAgents(new string[] { friendID.ToString() }); | ||
440 | if (friendSessions != null && friendSessions.Length > 0) | ||
441 | { | 492 | { |
442 | PresenceInfo friendSession = null; | 493 | // Try local |
443 | foreach (PresenceInfo pinfo in friendSessions) | 494 | if (LocalStatusNotification(userID, friendID, online)) |
444 | if (pinfo.RegionID != UUID.Zero) // let's guard against sessions-gone-bad | 495 | return; |
445 | { | ||
446 | friendSession = pinfo; | ||
447 | break; | ||
448 | } | ||
449 | 496 | ||
450 | if (friendSession != null) | 497 | // The friend is not here [as root]. Let's forward. |
498 | PresenceInfo[] friendSessions = PresenceService.GetAgents(new string[] { friendID.ToString() }); | ||
499 | if (friendSessions != null && friendSessions.Length > 0) | ||
451 | { | 500 | { |
452 | GridRegion region = GridService.GetRegionByUUID(m_Scenes[0].RegionInfo.ScopeID, friendSession.RegionID); | 501 | PresenceInfo friendSession = null; |
453 | //m_log.DebugFormat("[FRIENDS]: Remote Notify to region {0}", region.RegionName); | 502 | foreach (PresenceInfo pinfo in friendSessions) |
454 | m_FriendsSimConnector.StatusNotify(region, userID, friendID, online); | 503 | if (pinfo.RegionID != UUID.Zero) // let's guard against sessions-gone-bad |
504 | { | ||
505 | friendSession = pinfo; | ||
506 | break; | ||
507 | } | ||
508 | |||
509 | if (friendSession != null) | ||
510 | { | ||
511 | GridRegion region = GridService.GetRegionByUUID(m_Scenes[0].RegionInfo.ScopeID, friendSession.RegionID); | ||
512 | //m_log.DebugFormat("[FRIENDS]: Remote Notify to region {0}", region.RegionName); | ||
513 | m_FriendsSimConnector.StatusNotify(region, userID, friendID, online); | ||
514 | } | ||
455 | } | 515 | } |
456 | } | ||
457 | 516 | ||
458 | // Friend is not online. Ignore. | 517 | // Friend is not online. Ignore. |
459 | } | 518 | } |
460 | else | 519 | else |
461 | { | 520 | { |
462 | m_log.WarnFormat("[FRIENDS]: Error parsing friend ID {0}", friend.Friend); | 521 | m_log.WarnFormat("[FRIENDS]: Error parsing friend ID {0}", friend.Friend); |
522 | } | ||
463 | } | 523 | } |
464 | } | 524 | } |
465 | 525 | ||
@@ -475,7 +535,7 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends | |||
475 | 535 | ||
476 | // This user wants to be friends with the other user. | 536 | // This user wants to be friends with the other user. |
477 | // Let's add the relation backwards, in case the other is not online | 537 | // Let's add the relation backwards, in case the other is not online |
478 | FriendsService.StoreFriend(friendID, principalID.ToString(), 0); | 538 | StoreBackwards(friendID, principalID); |
479 | 539 | ||
480 | // Now let's ask the other user to be friends with this user | 540 | // Now let's ask the other user to be friends with this user |
481 | ForwardFriendshipOffer(principalID, friendID, im); | 541 | ForwardFriendshipOffer(principalID, friendID, im); |
@@ -487,11 +547,9 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends | |||
487 | // !!!!!!!! This is a hack so that we don't have to keep state (transactionID/imSessionID) | 547 | // !!!!!!!! This is a hack so that we don't have to keep state (transactionID/imSessionID) |
488 | // We stick this agent's ID as imSession, so that it's directly available on the receiving end | 548 | // We stick this agent's ID as imSession, so that it's directly available on the receiving end |
489 | im.imSessionID = im.fromAgentID; | 549 | im.imSessionID = im.fromAgentID; |
550 | im.fromAgentName = GetFriendshipRequesterName(agentID); | ||
490 | 551 | ||
491 | // Try the local sim | 552 | // Try the local sim |
492 | UserAccount account = UserAccountService.GetUserAccount(UUID.Zero, agentID); | ||
493 | im.fromAgentName = (account == null) ? "Unknown" : account.FirstName + " " + account.LastName; | ||
494 | |||
495 | if (LocalFriendshipOffered(friendID, im)) | 553 | if (LocalFriendshipOffered(friendID, im)) |
496 | return; | 554 | return; |
497 | 555 | ||
@@ -509,15 +567,20 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends | |||
509 | // If the prospective friend is not online, he'll get the message upon login. | 567 | // If the prospective friend is not online, he'll get the message upon login. |
510 | } | 568 | } |
511 | 569 | ||
570 | protected virtual string GetFriendshipRequesterName(UUID agentID) | ||
571 | { | ||
572 | UserAccount account = UserAccountService.GetUserAccount(UUID.Zero, agentID); | ||
573 | return (account == null) ? "Unknown" : account.FirstName + " " + account.LastName; | ||
574 | } | ||
575 | |||
512 | private void OnApproveFriendRequest(IClientAPI client, UUID agentID, UUID friendID, List<UUID> callingCardFolders) | 576 | private void OnApproveFriendRequest(IClientAPI client, UUID agentID, UUID friendID, List<UUID> callingCardFolders) |
513 | { | 577 | { |
514 | m_log.DebugFormat("[FRIENDS]: {0} accepted friendship from {1}", agentID, friendID); | 578 | m_log.DebugFormat("[FRIENDS]: {0} accepted friendship from {1}", agentID, friendID); |
515 | 579 | ||
516 | FriendsService.StoreFriend(agentID, friendID.ToString(), 1); | 580 | StoreFriendships(agentID, friendID); |
517 | FriendsService.StoreFriend(friendID, agentID.ToString(), 1); | ||
518 | 581 | ||
519 | // Update the local cache | 582 | // Update the local cache |
520 | UpdateFriendsCache(agentID); | 583 | RefetchFriends(client); |
521 | 584 | ||
522 | // | 585 | // |
523 | // Notify the friend | 586 | // Notify the friend |
@@ -548,8 +611,7 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends | |||
548 | { | 611 | { |
549 | m_log.DebugFormat("[FRIENDS]: {0} denied friendship to {1}", agentID, friendID); | 612 | m_log.DebugFormat("[FRIENDS]: {0} denied friendship to {1}", agentID, friendID); |
550 | 613 | ||
551 | FriendsService.Delete(agentID, friendID.ToString()); | 614 | DeleteFriendship(agentID, friendID); |
552 | FriendsService.Delete(friendID, agentID.ToString()); | ||
553 | 615 | ||
554 | // | 616 | // |
555 | // Notify the friend | 617 | // Notify the friend |
@@ -576,11 +638,11 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends | |||
576 | 638 | ||
577 | private void OnTerminateFriendship(IClientAPI client, UUID agentID, UUID exfriendID) | 639 | private void OnTerminateFriendship(IClientAPI client, UUID agentID, UUID exfriendID) |
578 | { | 640 | { |
579 | FriendsService.Delete(agentID, exfriendID.ToString()); | 641 | if (!DeleteFriendship(agentID, exfriendID)) |
580 | FriendsService.Delete(exfriendID, agentID.ToString()); | 642 | client.SendAlertMessage("Unable to terminate friendship on this sim."); |
581 | 643 | ||
582 | // Update local cache | 644 | // Update local cache |
583 | UpdateFriendsCache(agentID); | 645 | RefetchFriends(client); |
584 | 646 | ||
585 | client.SendTerminateFriend(exfriendID); | 647 | client.SendTerminateFriend(exfriendID); |
586 | 648 | ||
@@ -606,23 +668,25 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends | |||
606 | 668 | ||
607 | private void OnGrantUserRights(IClientAPI remoteClient, UUID requester, UUID target, int rights) | 669 | private void OnGrantUserRights(IClientAPI remoteClient, UUID requester, UUID target, int rights) |
608 | { | 670 | { |
671 | m_log.DebugFormat("[FRIENDS MODULE]: User {0} changing rights to {1} for friend {2}", requester, rights, target); | ||
672 | |||
609 | FriendInfo[] friends = GetFriends(remoteClient.AgentId); | 673 | FriendInfo[] friends = GetFriends(remoteClient.AgentId); |
610 | if (friends.Length == 0) | 674 | if (friends.Length == 0) |
675 | { | ||
611 | return; | 676 | return; |
677 | } | ||
612 | 678 | ||
613 | m_log.DebugFormat("[FRIENDS MODULE]: User {0} changing rights to {1} for friend {2}", requester, rights, target); | ||
614 | // Let's find the friend in this user's friend list | 679 | // Let's find the friend in this user's friend list |
615 | FriendInfo friend = null; | 680 | FriendInfo friend = GetFriend(friends, target); |
616 | foreach (FriendInfo fi in friends) | ||
617 | { | ||
618 | if (fi.Friend == target.ToString()) | ||
619 | friend = fi; | ||
620 | } | ||
621 | 681 | ||
622 | if (friend != null) // Found it | 682 | if (friend != null) // Found it |
623 | { | 683 | { |
624 | // Store it on the DB | 684 | // Store it on the DB |
625 | FriendsService.StoreFriend(requester, target.ToString(), rights); | 685 | if (!StoreRights(requester, target, rights)) |
686 | { | ||
687 | remoteClient.SendAlertMessage("Unable to grant rights."); | ||
688 | return; | ||
689 | } | ||
626 | 690 | ||
627 | // Store it in the local cache | 691 | // Store it in the local cache |
628 | int myFlags = friend.MyFlags; | 692 | int myFlags = friend.MyFlags; |
@@ -652,6 +716,18 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends | |||
652 | } | 716 | } |
653 | } | 717 | } |
654 | } | 718 | } |
719 | else | ||
720 | m_log.DebugFormat("[FRIENDS MODULE]: friend {0} not found for {1}", target, requester); | ||
721 | } | ||
722 | |||
723 | protected virtual FriendInfo GetFriend(FriendInfo[] friends, UUID friendID) | ||
724 | { | ||
725 | foreach (FriendInfo fi in friends) | ||
726 | { | ||
727 | if (fi.Friend == friendID.ToString()) | ||
728 | return fi; | ||
729 | } | ||
730 | return null; | ||
655 | } | 731 | } |
656 | 732 | ||
657 | #region Local | 733 | #region Local |
@@ -680,7 +756,7 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends | |||
680 | friendClient.SendInstantMessage(im); | 756 | friendClient.SendInstantMessage(im); |
681 | 757 | ||
682 | // Update the local cache | 758 | // Update the local cache |
683 | UpdateFriendsCache(friendID); | 759 | RefetchFriends(friendClient); |
684 | 760 | ||
685 | // we're done | 761 | // we're done |
686 | return true; | 762 | return true; |
@@ -713,7 +789,7 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends | |||
713 | // the friend in this sim as root agent | 789 | // the friend in this sim as root agent |
714 | friendClient.SendTerminateFriend(exfriendID); | 790 | friendClient.SendTerminateFriend(exfriendID); |
715 | // update local cache | 791 | // update local cache |
716 | UpdateFriendsCache(exfriendID); | 792 | RefetchFriends(friendClient); |
717 | // we're done | 793 | // we're done |
718 | return true; | 794 | return true; |
719 | } | 795 | } |
@@ -743,15 +819,7 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends | |||
743 | } | 819 | } |
744 | 820 | ||
745 | // Update local cache | 821 | // Update local cache |
746 | lock (m_Friends) | 822 | UpdateLocalCache(userID, friendID, rights); |
747 | { | ||
748 | FriendInfo[] friends = GetFriends(friendID); | ||
749 | foreach (FriendInfo finfo in friends) | ||
750 | { | ||
751 | if (finfo.Friend == userID.ToString()) | ||
752 | finfo.TheirFlags = rights; | ||
753 | } | ||
754 | } | ||
755 | 823 | ||
756 | return true; | 824 | return true; |
757 | } | 825 | } |
@@ -780,7 +848,13 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends | |||
780 | 848 | ||
781 | #endregion | 849 | #endregion |
782 | 850 | ||
783 | private FriendInfo[] GetFriends(UUID agentID) | 851 | #region Get / Set friends in several flavours |
852 | /// <summary> | ||
853 | /// Get friends from local cache only | ||
854 | /// </summary> | ||
855 | /// <param name="agentID"></param> | ||
856 | /// <returns></returns> | ||
857 | protected FriendInfo[] GetFriends(UUID agentID) | ||
784 | { | 858 | { |
785 | UserFriendData friendsData; | 859 | UserFriendData friendsData; |
786 | 860 | ||
@@ -793,14 +867,63 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends | |||
793 | return EMPTY_FRIENDS; | 867 | return EMPTY_FRIENDS; |
794 | } | 868 | } |
795 | 869 | ||
796 | private void UpdateFriendsCache(UUID agentID) | 870 | /// <summary> |
871 | /// Update loca cache only | ||
872 | /// </summary> | ||
873 | /// <param name="userID"></param> | ||
874 | /// <param name="friendID"></param> | ||
875 | /// <param name="rights"></param> | ||
876 | protected void UpdateLocalCache(UUID userID, UUID friendID, int rights) | ||
877 | { | ||
878 | // Update local cache | ||
879 | lock (m_Friends) | ||
880 | { | ||
881 | FriendInfo[] friends = GetFriends(friendID); | ||
882 | FriendInfo finfo = GetFriend(friends, userID); | ||
883 | finfo.TheirFlags = rights; | ||
884 | } | ||
885 | } | ||
886 | |||
887 | protected virtual FriendInfo[] GetFriendsFromService(IClientAPI client) | ||
888 | { | ||
889 | return FriendsService.GetFriends(client.AgentId); | ||
890 | } | ||
891 | |||
892 | private void RefetchFriends(IClientAPI client) | ||
797 | { | 893 | { |
894 | UUID agentID = client.AgentId; | ||
798 | lock (m_Friends) | 895 | lock (m_Friends) |
799 | { | 896 | { |
800 | UserFriendData friendsData; | 897 | UserFriendData friendsData; |
801 | if (m_Friends.TryGetValue(agentID, out friendsData)) | 898 | if (m_Friends.TryGetValue(agentID, out friendsData)) |
802 | friendsData.Friends = FriendsService.GetFriends(agentID); | 899 | friendsData.Friends = GetFriendsFromService(client); |
803 | } | 900 | } |
804 | } | 901 | } |
902 | |||
903 | protected virtual bool StoreRights(UUID agentID, UUID friendID, int rights) | ||
904 | { | ||
905 | FriendsService.StoreFriend(agentID.ToString(), friendID.ToString(), rights); | ||
906 | return true; | ||
907 | } | ||
908 | |||
909 | protected virtual void StoreBackwards(UUID friendID, UUID agentID) | ||
910 | { | ||
911 | FriendsService.StoreFriend(friendID.ToString(), agentID.ToString(), 0); | ||
912 | } | ||
913 | |||
914 | protected virtual void StoreFriendships(UUID agentID, UUID friendID) | ||
915 | { | ||
916 | FriendsService.StoreFriend(agentID.ToString(), friendID.ToString(), 1); | ||
917 | FriendsService.StoreFriend(friendID.ToString(), agentID.ToString(), 1); | ||
918 | } | ||
919 | |||
920 | protected virtual bool DeleteFriendship(UUID agentID, UUID exfriendID) | ||
921 | { | ||
922 | FriendsService.Delete(agentID, exfriendID.ToString()); | ||
923 | FriendsService.Delete(exfriendID, agentID.ToString()); | ||
924 | return true; | ||
925 | } | ||
926 | |||
927 | #endregion | ||
805 | } | 928 | } |
806 | } | 929 | } |