aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/CoreModules/Avatar/Friends/FriendsModule.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/CoreModules/Avatar/Friends/FriendsModule.cs')
-rw-r--r--OpenSim/Region/CoreModules/Avatar/Friends/FriendsModule.cs1064
1 files changed, 13 insertions, 1051 deletions
diff --git a/OpenSim/Region/CoreModules/Avatar/Friends/FriendsModule.cs b/OpenSim/Region/CoreModules/Avatar/Friends/FriendsModule.cs
index d6a82ef..f5d30b7 100644
--- a/OpenSim/Region/CoreModules/Avatar/Friends/FriendsModule.cs
+++ b/OpenSim/Region/CoreModules/Avatar/Friends/FriendsModule.cs
@@ -44,1088 +44,50 @@ using GridRegion = OpenSim.Services.Interfaces.GridRegion;
44 44
45namespace OpenSim.Region.CoreModules.Avatar.Friends 45namespace OpenSim.Region.CoreModules.Avatar.Friends
46{ 46{
47 /* 47 public class FriendsModule : ISharedRegionModule, IFriendsModule
48 This module handles adding/removing friends, and the the presence
49 notification process for login/logoff of friends.
50
51 The presence notification works as follows:
52 - After the user initially connects to a region (so we now have a UDP
53 connection to work with), this module fetches the friends of user
54 (those are cached), their on-/offline status, and info about the
55 region they are in from the MessageServer.
56 - (*) It then informs the user about the on-/offline status of her friends.
57 - It then informs all online friends currently on this region-server about
58 user's new online status (this will save some network traffic, as local
59 messages don't have to be transferred inter-region, and it will be all
60 that has to be done in Standalone Mode).
61 - For the rest of the online friends (those not on this region-server),
62 this module uses the provided region-information to map users to
63 regions, and sends one notification to every region containing the
64 friends to inform on that server.
65 - The region-server will handle that in the following way:
66 - If it finds the friend, it informs her about the user being online.
67 - If it doesn't find the friend (maybe she TPed away in the meantime),
68 it stores that information.
69 - After it processed all friends, it returns the list of friends it
70 couldn't find.
71 - If this list isn't empty, the FriendsModule re-requests information
72 about those online friends that have been missed and starts at (*)
73 again until all friends have been found, or until it tried 3 times
74 (to prevent endless loops due to some uncaught error).
75
76 NOTE: Online/Offline notifications don't need to be sent on region change.
77
78 We implement two XMLRpc handlers here, handling all the inter-region things
79 we have to handle:
80 - On-/Offline-Notifications (bulk)
81 - Terminate Friendship messages (single)
82 */
83
84 public class FriendsModule : IRegionModule, IFriendsModule
85 { 48 {
86 private class Transaction 49 public void Initialise(IConfigSource config)
87 { 50 {
88 public UUID agentID;
89 public string agentName;
90 public uint count;
91
92 public Transaction(UUID agentID, string agentName)
93 {
94 this.agentID = agentID;
95 this.agentName = agentName;
96 this.count = 1;
97 }
98 }
99
100 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
101
102 private Cache m_friendLists = new Cache(CacheFlags.AllowUpdate);
103
104 private Dictionary<UUID, ulong> m_rootAgents = new Dictionary<UUID, ulong>();
105
106 private Dictionary<UUID, UUID> m_pendingCallingcardRequests = new Dictionary<UUID,UUID>();
107
108 private Scene m_initialScene; // saves a lookup if we don't have a specific scene
109 private Dictionary<ulong, Scene> m_scenes = new Dictionary<ulong,Scene>();
110 private IMessageTransferModule m_TransferModule = null;
111
112 private IGridService m_gridServices = null;
113
114 #region IRegionModule Members
115
116 public void Initialise(Scene scene, IConfigSource config)
117 {
118 lock (m_scenes)
119 {
120 if (m_scenes.Count == 0)
121 {
122 MainServer.Instance.AddXmlRPCHandler("presence_update_bulk", processPresenceUpdateBulk);
123 MainServer.Instance.AddXmlRPCHandler("terminate_friend", processTerminateFriend);
124 m_friendLists.DefaultTTL = new TimeSpan(1, 0, 0); // store entries for one hour max
125 m_initialScene = scene;
126 }
127
128 if (!m_scenes.ContainsKey(scene.RegionInfo.RegionHandle))
129 m_scenes[scene.RegionInfo.RegionHandle] = scene;
130 }
131
132 scene.RegisterModuleInterface<IFriendsModule>(this);
133
134 scene.EventManager.OnNewClient += OnNewClient;
135 scene.EventManager.OnIncomingInstantMessage += OnGridInstantMessage;
136 scene.EventManager.OnAvatarEnteringNewParcel += AvatarEnteringParcel;
137 scene.EventManager.OnMakeChildAgent += MakeChildAgent;
138 scene.EventManager.OnClientClosed += ClientClosed;
139 } 51 }
140 52
141 public void PostInitialise() 53 public void PostInitialise()
142 { 54 {
143 if (m_scenes.Count > 0)
144 {
145 m_TransferModule = m_initialScene.RequestModuleInterface<IMessageTransferModule>();
146 m_gridServices = m_initialScene.GridService;
147 }
148 if (m_TransferModule == null)
149 m_log.Error("[FRIENDS]: Unable to find a message transfer module, friendship offers will not work");
150 }
151
152 public void Close()
153 {
154 }
155
156 public string Name
157 {
158 get { return "FriendsModule"; }
159 }
160
161 public bool IsSharedModule
162 {
163 get { return true; }
164 }
165
166 #endregion
167
168 #region IInterregionFriendsComms
169
170 public List<UUID> InformFriendsInOtherRegion(UUID agentId, ulong destRegionHandle, List<UUID> friends, bool online)
171 {
172 List<UUID> tpdAway = new List<UUID>();
173
174 // destRegionHandle is a region on another server
175 uint x = 0, y = 0;
176 Utils.LongToUInts(destRegionHandle, out x, out y);
177 GridRegion info = m_gridServices.GetRegionByPosition(m_initialScene.RegionInfo.ScopeID, (int)x, (int)y);
178 if (info != null)
179 {
180 string httpServer = "http://" + info.ExternalEndPoint.Address + ":" + info.HttpPort + "/presence_update_bulk";
181
182 Hashtable reqParams = new Hashtable();
183 reqParams["agentID"] = agentId.ToString();
184 reqParams["agentOnline"] = online;
185 int count = 0;
186 foreach (UUID uuid in friends)
187 {
188 reqParams["friendID_" + count++] = uuid.ToString();
189 }
190 reqParams["friendCount"] = count;
191
192 IList parameters = new ArrayList();
193 parameters.Add(reqParams);
194 try
195 {
196 XmlRpcRequest request = new XmlRpcRequest("presence_update_bulk", parameters);
197 XmlRpcResponse response = request.Send(httpServer, 5000);
198 Hashtable respData = (Hashtable)response.Value;
199
200 count = (int)respData["friendCount"];
201 for (int i = 0; i < count; ++i)
202 {
203 UUID uuid;
204 if (UUID.TryParse((string)respData["friendID_" + i], out uuid)) tpdAway.Add(uuid);
205 }
206 }
207 catch (WebException e)
208 {
209 // Ignore connect failures, simulators come and go
210 //
211 if (!e.Message.Contains("ConnectFailure"))
212 {
213 m_log.Error("[OGS1 GRID SERVICES]: InformFriendsInOtherRegion XMLRPC failure: ", e);
214 }
215 }
216 catch (Exception e)
217 {
218 m_log.Error("[OGS1 GRID SERVICES]: InformFriendsInOtherRegion XMLRPC failure: ", e);
219 }
220 }
221 else m_log.WarnFormat("[OGS1 GRID SERVICES]: Couldn't find region {0}???", destRegionHandle);
222
223 return tpdAway;
224 }
225
226 public bool TriggerTerminateFriend(ulong destRegionHandle, UUID agentID, UUID exFriendID)
227 {
228 // destRegionHandle is a region on another server
229 uint x = 0, y = 0;
230 Utils.LongToUInts(destRegionHandle, out x, out y);
231 GridRegion info = m_gridServices.GetRegionByPosition(m_initialScene.RegionInfo.ScopeID, (int)x, (int)y);
232 if (info == null)
233 {
234 m_log.WarnFormat("[OGS1 GRID SERVICES]: Couldn't find region {0}", destRegionHandle);
235 return false; // region not found???
236 }
237
238 string httpServer = "http://" + info.ExternalEndPoint.Address + ":" + info.HttpPort + "/presence_update_bulk";
239
240 Hashtable reqParams = new Hashtable();
241 reqParams["agentID"] = agentID.ToString();
242 reqParams["friendID"] = exFriendID.ToString();
243
244 IList parameters = new ArrayList();
245 parameters.Add(reqParams);
246 try
247 {
248 XmlRpcRequest request = new XmlRpcRequest("terminate_friend", parameters);
249 XmlRpcResponse response = request.Send(httpServer, 5000);
250 Hashtable respData = (Hashtable)response.Value;
251
252 return (bool)respData["success"];
253 }
254 catch (Exception e)
255 {
256 m_log.Error("[OGS1 GRID SERVICES]: InformFriendsInOtherRegion XMLRPC failure: ", e);
257 return false;
258 }
259 }
260
261 #endregion
262
263 #region Incoming XMLRPC messages
264 /// <summary>
265 /// Receive presence information changes about clients in other regions.
266 /// </summary>
267 /// <param name="req"></param>
268 /// <returns></returns>
269 public XmlRpcResponse processPresenceUpdateBulk(XmlRpcRequest req, IPEndPoint remoteClient)
270 {
271 Hashtable requestData = (Hashtable)req.Params[0];
272
273 List<UUID> friendsNotHere = new List<UUID>();
274
275 // this is called with the expectation that all the friends in the request are on this region-server.
276 // But as some time passed since we checked (on the other region-server, via the MessagingServer),
277 // some of the friends might have teleported away.
278 // Actually, even now, between this line and the sending below, some people could TP away. So,
279 // we'll have to lock the m_rootAgents list for the duration to prevent/delay that.
280 lock (m_rootAgents)
281 {
282 List<ScenePresence> friendsHere = new List<ScenePresence>();
283
284 try
285 {
286 UUID agentID = new UUID((string)requestData["agentID"]);
287 bool agentOnline = (bool)requestData["agentOnline"];
288 int count = (int)requestData["friendCount"];
289 for (int i = 0; i < count; ++i)
290 {
291 UUID uuid;
292 if (UUID.TryParse((string)requestData["friendID_" + i], out uuid))
293 {
294 if (m_rootAgents.ContainsKey(uuid)) friendsHere.Add(GetRootPresenceFromAgentID(uuid));
295 else friendsNotHere.Add(uuid);
296 }
297 }
298
299 // now send, as long as they are still here...
300 UUID[] agentUUID = new UUID[] { agentID };
301 if (agentOnline)
302 {
303 foreach (ScenePresence agent in friendsHere)
304 {
305 agent.ControllingClient.SendAgentOnline(agentUUID);
306 }
307 }
308 else
309 {
310 foreach (ScenePresence agent in friendsHere)
311 {
312 agent.ControllingClient.SendAgentOffline(agentUUID);
313 }
314 }
315 }
316 catch(Exception e)
317 {
318 m_log.Warn("[FRIENDS]: Got exception while parsing presence_update_bulk request:", e);
319 }
320 }
321
322 // no need to lock anymore; if TPs happen now, worst case is that we have an additional agent in this region,
323 // which should be caught on the next iteration...
324 Hashtable result = new Hashtable();
325 int idx = 0;
326 foreach (UUID uuid in friendsNotHere)
327 {
328 result["friendID_" + idx++] = uuid.ToString();
329 }
330 result["friendCount"] = idx;
331
332 XmlRpcResponse response = new XmlRpcResponse();
333 response.Value = result;
334
335 return response;
336 }
337
338 public XmlRpcResponse processTerminateFriend(XmlRpcRequest req, IPEndPoint remoteClient)
339 {
340 Hashtable requestData = (Hashtable)req.Params[0];
341
342 bool success = false;
343
344 UUID agentID;
345 UUID friendID;
346 if (requestData.ContainsKey("agentID") && UUID.TryParse((string)requestData["agentID"], out agentID) &&
347 requestData.ContainsKey("friendID") && UUID.TryParse((string)requestData["friendID"], out friendID))
348 {
349 // try to find it and if it is there, prevent it to vanish before we sent the message
350 lock (m_rootAgents)
351 {
352 if (m_rootAgents.ContainsKey(agentID))
353 {
354 m_log.DebugFormat("[FRIEND]: Sending terminate friend {0} to agent {1}", friendID, agentID);
355 GetRootPresenceFromAgentID(agentID).ControllingClient.SendTerminateFriend(friendID);
356 success = true;
357 }
358 }
359 }
360
361 // return whether we were successful
362 Hashtable result = new Hashtable();
363 result["success"] = success;
364
365 XmlRpcResponse response = new XmlRpcResponse();
366 response.Value = result;
367 return response;
368 }
369
370 #endregion
371
372 #region Scene events
373
374 private void OnNewClient(IClientAPI client)
375 {
376 // All friends establishment protocol goes over instant message
377 // There's no way to send a message from the sim
378 // to a user to 'add a friend' without causing dialog box spam
379
380 // Subscribe to instant messages
381 client.OnInstantMessage += OnInstantMessage;
382
383 // Friend list management
384 client.OnApproveFriendRequest += OnApproveFriendRequest;
385 client.OnDenyFriendRequest += OnDenyFriendRequest;
386 client.OnTerminateFriendship += OnTerminateFriendship;
387
388 // ... calling card handling...
389 client.OnOfferCallingCard += OnOfferCallingCard;
390 client.OnAcceptCallingCard += OnAcceptCallingCard;
391 client.OnDeclineCallingCard += OnDeclineCallingCard;
392
393 // we need this one exactly once per agent session (see comments in the handler below)
394 client.OnEconomyDataRequest += OnEconomyDataRequest;
395
396 // if it leaves, we want to know, too
397 client.OnLogout += OnLogout;
398 client.OnGrantUserRights += GrantUserFriendRights;
399
400 }
401
402 private void ClientClosed(UUID AgentId, Scene scene)
403 {
404 // agent's client was closed. As we handle logout in OnLogout, this here has only to handle
405 // TPing away (root agent is closed) or TPing/crossing in a region far enough away (client
406 // agent is closed).
407 // NOTE: In general, this doesn't mean that the agent logged out, just that it isn't around
408 // in one of the regions here anymore.
409 lock (m_rootAgents)
410 {
411 if (m_rootAgents.ContainsKey(AgentId))
412 {
413 m_rootAgents.Remove(AgentId);
414 }
415 }
416 }
417
418 private void AvatarEnteringParcel(ScenePresence avatar, int localLandID, UUID regionID)
419 {
420 lock (m_rootAgents)
421 {
422 m_rootAgents[avatar.UUID] = avatar.RegionHandle;
423 // Claim User! my user! Mine mine mine!
424 }
425 }
426
427 private void MakeChildAgent(ScenePresence avatar)
428 {
429 lock (m_rootAgents)
430 {
431 if (m_rootAgents.ContainsKey(avatar.UUID))
432 {
433 // only delete if the region matches. As this is a shared module, the avatar could be
434 // root agent in another region on this server.
435 if (m_rootAgents[avatar.UUID] == avatar.RegionHandle)
436 {
437 m_rootAgents.Remove(avatar.UUID);
438// m_log.Debug("[FRIEND]: Removing " + avatar.Firstname + " " + avatar.Lastname + " as a root agent");
439 }
440 }
441 }
442 }
443 #endregion
444
445 private ScenePresence GetRootPresenceFromAgentID(UUID AgentID)
446 {
447 ScenePresence returnAgent = null;
448 lock (m_scenes)
449 {
450 ScenePresence queryagent = null;
451 foreach (Scene scene in m_scenes.Values)
452 {
453 queryagent = scene.GetScenePresence(AgentID);
454 if (queryagent != null)
455 {
456 if (!queryagent.IsChildAgent)
457 {
458 returnAgent = queryagent;
459 break;
460 }
461 }
462 }
463 }
464 return returnAgent;
465 }
466
467 private ScenePresence GetAnyPresenceFromAgentID(UUID AgentID)
468 {
469 ScenePresence returnAgent = null;
470 lock (m_scenes)
471 {
472 ScenePresence queryagent = null;
473 foreach (Scene scene in m_scenes.Values)
474 {
475 queryagent = scene.GetScenePresence(AgentID);
476 if (queryagent != null)
477 {
478 returnAgent = queryagent;
479 break;
480 }
481 }
482 }
483 return returnAgent;
484 }
485
486 public void OfferFriendship(UUID fromUserId, IClientAPI toUserClient, string offerMessage)
487 {
488 CachedUserInfo userInfo = m_initialScene.CommsManager.UserProfileCacheService.GetUserDetails(fromUserId);
489
490 if (userInfo != null)
491 {
492 GridInstantMessage msg = new GridInstantMessage(
493 toUserClient.Scene, fromUserId, userInfo.UserProfile.Name, toUserClient.AgentId,
494 (byte)InstantMessageDialog.FriendshipOffered, offerMessage, false, Vector3.Zero);
495
496 FriendshipOffered(msg);
497 }
498 else
499 {
500 m_log.ErrorFormat("[FRIENDS]: No user found for id {0} in OfferFriendship()", fromUserId);
501 }
502 }
503
504 #region FriendRequestHandling
505
506 private void OnInstantMessage(IClientAPI client, GridInstantMessage im)
507 {
508 // Friend Requests go by Instant Message.. using the dialog param
509 // https://wiki.secondlife.com/wiki/ImprovedInstantMessage
510
511 if (im.dialog == (byte)InstantMessageDialog.FriendshipOffered) // 38
512 {
513 // fromAgentName is the *destination* name (the friend we offer friendship to)
514 ScenePresence initiator = GetAnyPresenceFromAgentID(new UUID(im.fromAgentID));
515 im.fromAgentName = initiator != null ? initiator.Name : "(hippo)";
516
517 FriendshipOffered(im);
518 }
519 else if (im.dialog == (byte)InstantMessageDialog.FriendshipAccepted) // 39
520 {
521 FriendshipAccepted(client, im);
522 }
523 else if (im.dialog == (byte)InstantMessageDialog.FriendshipDeclined) // 40
524 {
525 FriendshipDeclined(client, im);
526 }
527 }
528
529 /// <summary>
530 /// Invoked when a user offers a friendship.
531 /// </summary>
532 ///
533 /// <param name="im"></param>
534 /// <param name="client"></param>
535 private void FriendshipOffered(GridInstantMessage im)
536 {
537 // this is triggered by the initiating agent:
538 // A local agent offers friendship to some possibly remote friend.
539 // A IM is triggered, processed here and sent to the friend (possibly in a remote region).
540
541 m_log.DebugFormat("[FRIEND]: Offer(38) - From: {0}, FromName: {1} To: {2}, Session: {3}, Message: {4}, Offline {5}",
542 im.fromAgentID, im.fromAgentName, im.toAgentID, im.imSessionID, im.message, im.offline);
543
544 // 1.20 protocol sends an UUID in the message field, instead of the friendship offer text.
545 // For interoperability, we have to clear that
546 if (Util.isUUID(im.message)) im.message = "";
547
548 // be sneeky and use the initiator-UUID as transactionID. This means we can be stateless.
549 // we have to look up the agent name on friendship-approval, though.
550 im.imSessionID = im.fromAgentID;
551
552 if (m_TransferModule != null)
553 {
554 // Send it to whoever is the destination.
555 // If new friend is local, it will send an IM to the viewer.
556 // If new friend is remote, it will cause a OnGridInstantMessage on the remote server
557 m_TransferModule.SendInstantMessage(
558 im,
559 delegate(bool success)
560 {
561 m_log.DebugFormat("[FRIEND]: sending IM success = {0}", success);
562 }
563 );
564 }
565 }
566
567 /// <summary>
568 /// Invoked when a user accepts a friendship offer.
569 /// </summary>
570 /// <param name="im"></param>
571 /// <param name="client"></param>
572 private void FriendshipAccepted(IClientAPI client, GridInstantMessage im)
573 {
574 m_log.DebugFormat("[FRIEND]: 39 - from client {0}, agent {2} {3}, imsession {4} to {5}: {6} (dialog {7})",
575 client.AgentId, im.fromAgentID, im.fromAgentName, im.imSessionID, im.toAgentID, im.message, im.dialog);
576 }
577
578 /// <summary>
579 /// Invoked when a user declines a friendship offer.
580 /// </summary>
581 /// May not currently be used - see OnDenyFriendRequest() instead
582 /// <param name="im"></param>
583 /// <param name="client"></param>
584 private void FriendshipDeclined(IClientAPI client, GridInstantMessage im)
585 {
586 UUID fromAgentID = new UUID(im.fromAgentID);
587 UUID toAgentID = new UUID(im.toAgentID);
588
589 // declining the friendship offer causes a type 40 IM being sent to the (possibly remote) initiator
590 // toAgentID is initiator, fromAgentID declined friendship
591 m_log.DebugFormat("[FRIEND]: 40 - from client {0}, agent {1} {2}, imsession {3} to {4}: {5} (dialog {6})",
592 client != null ? client.AgentId.ToString() : "<null>",
593 fromAgentID, im.fromAgentName, im.imSessionID, im.toAgentID, im.message, im.dialog);
594
595 // Send the decline to whoever is the destination.
596 GridInstantMessage msg
597 = new GridInstantMessage(
598 client.Scene, fromAgentID, client.Name, toAgentID,
599 im.dialog, im.message, im.offline != 0, im.Position);
600
601 // If new friend is local, it will send an IM to the viewer.
602 // If new friend is remote, it will cause a OnGridInstantMessage on the remote server
603 m_TransferModule.SendInstantMessage(msg,
604 delegate(bool success) {
605 m_log.DebugFormat("[FRIEND]: sending IM success = {0}", success);
606 }
607 );
608 }
609
610 private void OnGridInstantMessage(GridInstantMessage msg)
611 {
612 // This event won't be raised unless we have that agent,
613 // so we can depend on the above not trying to send
614 // via grid again
615 //m_log.DebugFormat("[FRIEND]: Got GridIM from {0}, to {1}, imSession {2}, message {3}, dialog {4}",
616 // msg.fromAgentID, msg.toAgentID, msg.imSessionID, msg.message, msg.dialog);
617
618 if (msg.dialog == (byte)InstantMessageDialog.FriendshipOffered ||
619 msg.dialog == (byte)InstantMessageDialog.FriendshipAccepted ||
620 msg.dialog == (byte)InstantMessageDialog.FriendshipDeclined)
621 {
622 // this should succeed as we *know* the root agent is here.
623 m_TransferModule.SendInstantMessage(msg,
624 delegate(bool success) {
625 //m_log.DebugFormat("[FRIEND]: sending IM success = {0}", success);
626 }
627 );
628 }
629
630 if (msg.dialog == (byte)InstantMessageDialog.FriendshipAccepted)
631 {
632 // for accept friendship, we have to do a bit more
633 ApproveFriendship(new UUID(msg.fromAgentID), new UUID(msg.toAgentID), msg.fromAgentName);
634 }
635 }
636
637 private void ApproveFriendship(UUID fromAgentID, UUID toAgentID, string fromName)
638 {
639 m_log.DebugFormat("[FRIEND]: Approve friendship from {0} (ID: {1}) to {2}",
640 fromAgentID, fromName, toAgentID);
641
642 // a new friend was added in the initiator's and friend's data, so the cache entries are wrong now.
643 lock (m_friendLists)
644 {
645 m_friendLists.Invalidate(fromAgentID.ToString());
646 m_friendLists.Invalidate(toAgentID.ToString());
647 }
648
649 // now send presence update and add a calling card for the new friend
650
651 ScenePresence initiator = GetAnyPresenceFromAgentID(toAgentID);
652 if (initiator == null)
653 {
654 // quite wrong. Shouldn't happen.
655 m_log.WarnFormat("[FRIEND]: Coudn't find initiator of friend request {0}", toAgentID);
656 return;
657 }
658
659 m_log.DebugFormat("[FRIEND]: Tell {0} that {1} is online",
660 initiator.Name, fromName);
661 // tell initiator that friend is online
662 initiator.ControllingClient.SendAgentOnline(new UUID[] { fromAgentID });
663
664 // find the folder for the friend...
665 //InventoryFolderImpl folder =
666 // initiator.Scene.CommsManager.UserProfileCacheService.GetUserDetails(toAgentID).FindFolderForType((int)InventoryType.CallingCard);
667 IInventoryService invService = initiator.Scene.InventoryService;
668 InventoryFolderBase folder = invService.GetFolderForType(toAgentID, AssetType.CallingCard);
669 if (folder != null)
670 {
671 // ... and add the calling card
672 CreateCallingCard(initiator.ControllingClient, fromAgentID, folder.ID, fromName);
673 }
674 } 55 }
675 56
676 private void OnApproveFriendRequest(IClientAPI client, UUID agentID, UUID friendID, List<UUID> callingCardFolders) 57 public void AddRegion(Scene scene)
677 { 58 {
678 m_log.DebugFormat("[FRIEND]: Got approve friendship from {0} {1}, agentID {2}, tid {3}",
679 client.Name, client.AgentId, agentID, friendID);
680
681 // store the new friend persistently for both avatars
682 m_initialScene.StoreAddFriendship(friendID, agentID, (uint) FriendRights.CanSeeOnline);
683
684 // The cache entries aren't valid anymore either, as we just added a friend to both sides.
685 lock (m_friendLists)
686 {
687 m_friendLists.Invalidate(agentID.ToString());
688 m_friendLists.Invalidate(friendID.ToString());
689 }
690
691 // if it's a local friend, we don't have to do the lookup
692 ScenePresence friendPresence = GetAnyPresenceFromAgentID(friendID);
693
694 if (friendPresence != null)
695 {
696 m_log.Debug("[FRIEND]: Local agent detected.");
697
698 // create calling card
699 CreateCallingCard(client, friendID, callingCardFolders[0], friendPresence.Name);
700
701 // local message means OnGridInstantMessage won't be triggered, so do the work here.
702 friendPresence.ControllingClient.SendInstantMessage(
703 new GridInstantMessage(client.Scene, agentID,
704 client.Name, friendID,
705 (byte)InstantMessageDialog.FriendshipAccepted,
706 agentID.ToString(), false, Vector3.Zero));
707 ApproveFriendship(agentID, friendID, client.Name);
708 }
709 else
710 {
711 m_log.Debug("[FRIEND]: Remote agent detected.");
712
713 // fetch the friend's name for the calling card.
714 CachedUserInfo info = m_initialScene.CommsManager.UserProfileCacheService.GetUserDetails(friendID);
715
716 // create calling card
717 CreateCallingCard(client, friendID, callingCardFolders[0],
718 info.UserProfile.FirstName + " " + info.UserProfile.SurName);
719
720 // Compose (remote) response to friend.
721 GridInstantMessage msg = new GridInstantMessage(client.Scene, agentID, client.Name, friendID,
722 (byte)InstantMessageDialog.FriendshipAccepted,
723 agentID.ToString(), false, Vector3.Zero);
724 if (m_TransferModule != null)
725 {
726 m_TransferModule.SendInstantMessage(msg,
727 delegate(bool success) {
728 m_log.DebugFormat("[FRIEND]: sending IM success = {0}", success);
729 }
730 );
731 }
732 }
733
734 // tell client that new friend is online
735 client.SendAgentOnline(new UUID[] { friendID });
736 } 59 }
737 60
738 private void OnDenyFriendRequest(IClientAPI client, UUID agentID, UUID friendID, List<UUID> callingCardFolders) 61 public void RegionLoaded(Scene scene)
739 { 62 {
740 m_log.DebugFormat("[FRIEND]: Got deny friendship from {0} {1}, agentID {2}, tid {3}",
741 client.Name, client.AgentId, agentID, friendID);
742
743 // Compose response to other agent.
744 GridInstantMessage msg = new GridInstantMessage(client.Scene, agentID, client.Name, friendID,
745 (byte)InstantMessageDialog.FriendshipDeclined,
746 agentID.ToString(), false, Vector3.Zero);
747 // send decline to initiator
748 if (m_TransferModule != null)
749 {
750 m_TransferModule.SendInstantMessage(msg,
751 delegate(bool success) {
752 m_log.DebugFormat("[FRIEND]: sending IM success = {0}", success);
753 }
754 );
755 }
756 } 63 }
757 64
758 private void OnTerminateFriendship(IClientAPI client, UUID agentID, UUID exfriendID) 65 public void RemoveRegion(Scene scene)
759 { 66 {
760 // client.AgentId == agentID!
761
762 // this removes the friends from the stored friendlists. After the next login, they will be gone...
763 m_initialScene.StoreRemoveFriendship(agentID, exfriendID);
764
765 // ... now tell the two involved clients that they aren't friends anymore.
766
767 // I don't know why we have to tell <agent>, as this was caused by her, but that's how it works in SL...
768 client.SendTerminateFriend(exfriendID);
769
770 // now send the friend, if online
771 ScenePresence presence = GetAnyPresenceFromAgentID(exfriendID);
772 if (presence != null)
773 {
774 m_log.DebugFormat("[FRIEND]: Sending terminate friend {0} to agent {1}", agentID, exfriendID);
775 presence.ControllingClient.SendTerminateFriend(agentID);
776 }
777 else
778 {
779 // retry 3 times, in case the agent TPed from the last known region...
780 for (int retry = 0; retry < 3; ++retry)
781 {
782 // wasn't sent, so ex-friend wasn't around on this region-server. Fetch info and try to send
783 UserAgentData data = m_initialScene.CommsManager.UserService.GetAgentByUUID(exfriendID);
784
785 if (null == data)
786 break;
787
788 if (!data.AgentOnline)
789 {
790 m_log.DebugFormat("[FRIEND]: {0} is offline, so not sending TerminateFriend", exfriendID);
791 break; // if ex-friend isn't online, we don't need to send
792 }
793
794 m_log.DebugFormat("[FRIEND]: Sending remote terminate friend {0} to agent {1}@{2}",
795 agentID, exfriendID, data.Handle);
796
797 // try to send to foreign region, retry if it fails (friend TPed away, for example)
798 if (TriggerTerminateFriend(data.Handle, exfriendID, agentID)) break;
799 }
800 }
801
802 // clean up cache: FriendList is wrong now...
803 lock (m_friendLists)
804 {
805 m_friendLists.Invalidate(agentID.ToString());
806 m_friendLists.Invalidate(exfriendID.ToString());
807 }
808 } 67 }
809 68
810 #endregion 69 public void Close()
811
812 #region CallingCards
813
814 private void OnOfferCallingCard(IClientAPI client, UUID destID, UUID transactionID)
815 {
816 m_log.DebugFormat("[CALLING CARD]: got offer from {0} for {1}, transaction {2}",
817 client.AgentId, destID, transactionID);
818 // This might be slightly wrong. On a multi-region server, we might get the child-agent instead of the root-agent
819 // (or the root instead of the child)
820 ScenePresence destAgent = GetAnyPresenceFromAgentID(destID);
821 if (destAgent == null)
822 {
823 client.SendAlertMessage("The person you have offered a card to can't be found anymore.");
824 return;
825 }
826
827 lock (m_pendingCallingcardRequests)
828 {
829 m_pendingCallingcardRequests[transactionID] = client.AgentId;
830 }
831 // inform the destination agent about the offer
832 destAgent.ControllingClient.SendOfferCallingCard(client.AgentId, transactionID);
833 }
834
835 private void CreateCallingCard(IClientAPI client, UUID creator, UUID folder, string name)
836 {
837 InventoryItemBase item = new InventoryItemBase();
838 item.AssetID = UUID.Zero;
839 item.AssetType = (int)AssetType.CallingCard;
840 item.BasePermissions = (uint)PermissionMask.Copy;
841 item.CreationDate = Util.UnixTimeSinceEpoch();
842 item.CreatorId = creator.ToString();
843 item.CurrentPermissions = item.BasePermissions;
844 item.Description = "";
845 item.EveryOnePermissions = (uint)PermissionMask.None;
846 item.Flags = 0;
847 item.Folder = folder;
848 item.GroupID = UUID.Zero;
849 item.GroupOwned = false;
850 item.ID = UUID.Random();
851 item.InvType = (int)InventoryType.CallingCard;
852 item.Name = name;
853 item.NextPermissions = item.EveryOnePermissions;
854 item.Owner = client.AgentId;
855 item.SalePrice = 10;
856 item.SaleType = (byte)SaleType.Not;
857 ((Scene)client.Scene).AddInventoryItem(client, item);
858 }
859
860 private void OnAcceptCallingCard(IClientAPI client, UUID transactionID, UUID folderID)
861 {
862 m_log.DebugFormat("[CALLING CARD]: User {0} ({1} {2}) accepted tid {3}, folder {4}",
863 client.AgentId,
864 client.FirstName, client.LastName,
865 transactionID, folderID);
866 UUID destID;
867 lock (m_pendingCallingcardRequests)
868 {
869 if (!m_pendingCallingcardRequests.TryGetValue(transactionID, out destID))
870 {
871 m_log.WarnFormat("[CALLING CARD]: Got a AcceptCallingCard from {0} without an offer before.",
872 client.Name);
873 return;
874 }
875 // else found pending calling card request with that transaction.
876 m_pendingCallingcardRequests.Remove(transactionID);
877 }
878
879
880 ScenePresence destAgent = GetAnyPresenceFromAgentID(destID);
881 // inform sender of the card that destination declined the offer
882 if (destAgent != null) destAgent.ControllingClient.SendAcceptCallingCard(transactionID);
883
884 // put a calling card into the inventory of receiver
885 CreateCallingCard(client, destID, folderID, destAgent.Name);
886 }
887
888 private void OnDeclineCallingCard(IClientAPI client, UUID transactionID)
889 { 70 {
890 m_log.DebugFormat("[CALLING CARD]: User {0} (ID:{1}) declined card, tid {2}",
891 client.Name, client.AgentId, transactionID);
892 UUID destID;
893 lock (m_pendingCallingcardRequests)
894 {
895 if (!m_pendingCallingcardRequests.TryGetValue(transactionID, out destID))
896 {
897 m_log.WarnFormat("[CALLING CARD]: Got a AcceptCallingCard from {0} without an offer before.",
898 client.Name);
899 return;
900 }
901 // else found pending calling card request with that transaction.
902 m_pendingCallingcardRequests.Remove(transactionID);
903 }
904
905 ScenePresence destAgent = GetAnyPresenceFromAgentID(destID);
906 // inform sender of the card that destination declined the offer
907 if (destAgent != null) destAgent.ControllingClient.SendDeclineCallingCard(transactionID);
908 } 71 }
909 72
910 /// <summary> 73 public string Name
911 /// Send presence information about a client to other clients in both this region and others.
912 /// </summary>
913 /// <param name="client"></param>
914 /// <param name="friendList"></param>
915 /// <param name="iAmOnline"></param>
916 private void SendPresenceState(IClientAPI client, List<FriendListItem> friendList, bool iAmOnline)
917 { 74 {
918 //m_log.DebugFormat("[FRIEND]: {0} logged {1}; sending presence updates", client.Name, iAmOnline ? "in" : "out"); 75 get { return "FriendsModule"; }
919
920 if (friendList == null || friendList.Count == 0)
921 {
922 //m_log.DebugFormat("[FRIEND]: {0} doesn't have friends.", client.Name);
923 return; // nothing we can do if she doesn't have friends...
924 }
925
926 // collect sets of friendIDs; to send to (online and offline), and to receive from
927 // TODO: If we ever switch to .NET >= 3, replace those Lists with HashSets.
928 // I can't believe that we have Dictionaries, but no Sets, considering Java introduced them years ago...
929 List<UUID> friendIDsToSendTo = new List<UUID>();
930 List<UUID> candidateFriendIDsToReceive = new List<UUID>();
931
932 foreach (FriendListItem item in friendList)
933 {
934 if (((item.FriendListOwnerPerms | item.FriendPerms) & (uint)FriendRights.CanSeeOnline) != 0)
935 {
936 // friend is allowed to see my presence => add
937 if ((item.FriendListOwnerPerms & (uint)FriendRights.CanSeeOnline) != 0)
938 friendIDsToSendTo.Add(item.Friend);
939
940 if ((item.FriendPerms & (uint)FriendRights.CanSeeOnline) != 0)
941 candidateFriendIDsToReceive.Add(item.Friend);
942 }
943 }
944
945 // we now have a list of "interesting" friends (which we have to find out on-/offline state for),
946 // friends we want to send our online state to (if *they* are online, too), and
947 // friends we want to receive online state for (currently unknown whether online or not)
948
949 // as this processing might take some time and friends might TP away, we try up to three times to
950 // reach them. Most of the time, we *will* reach them, and this loop won't loop
951 int retry = 0;
952 do
953 {
954 // build a list of friends to look up region-information and on-/offline-state for
955 List<UUID> friendIDsToLookup = new List<UUID>(friendIDsToSendTo);
956 foreach (UUID uuid in candidateFriendIDsToReceive)
957 {
958 if (!friendIDsToLookup.Contains(uuid)) friendIDsToLookup.Add(uuid);
959 }
960
961 m_log.DebugFormat(
962 "[FRIEND]: {0} to lookup, {1} to send to, {2} candidates to receive from for agent {3}",
963 friendIDsToLookup.Count, friendIDsToSendTo.Count, candidateFriendIDsToReceive.Count, client.Name);
964
965 // we have to fetch FriendRegionInfos, as the (cached) FriendListItems don't
966 // necessarily contain the correct online state...
967 Dictionary<UUID, FriendRegionInfo> friendRegions = m_initialScene.GetFriendRegionInfos(friendIDsToLookup);
968 m_log.DebugFormat(
969 "[FRIEND]: Found {0} regionInfos for {1} friends of {2}",
970 friendRegions.Count, friendIDsToLookup.Count, client.Name);
971
972 // argument for SendAgentOn/Offline; we shouldn't generate that repeatedly within loops.
973 UUID[] agentArr = new UUID[] { client.AgentId };
974
975 // first, send to friend presence state to me, if I'm online...
976 if (iAmOnline)
977 {
978 List<UUID> friendIDsToReceive = new List<UUID>();
979
980 for (int i = candidateFriendIDsToReceive.Count - 1; i >= 0; --i)
981 {
982 UUID uuid = candidateFriendIDsToReceive[i];
983 FriendRegionInfo info;
984 if (friendRegions.TryGetValue(uuid, out info) && info != null && info.isOnline)
985 {
986 friendIDsToReceive.Add(uuid);
987 }
988 }
989
990 m_log.DebugFormat(
991 "[FRIEND]: Sending {0} online friends to {1}", friendIDsToReceive.Count, client.Name);
992
993 if (friendIDsToReceive.Count > 0)
994 client.SendAgentOnline(friendIDsToReceive.ToArray());
995
996 // clear them for a possible second iteration; we don't have to repeat this
997 candidateFriendIDsToReceive.Clear();
998 }
999
1000 // now, send my presence state to my friends
1001 for (int i = friendIDsToSendTo.Count - 1; i >= 0; --i)
1002 {
1003 UUID uuid = friendIDsToSendTo[i];
1004 FriendRegionInfo info;
1005 if (friendRegions.TryGetValue(uuid, out info) && info != null && info.isOnline)
1006 {
1007 // any client is good enough, root or child...
1008 ScenePresence agent = GetAnyPresenceFromAgentID(uuid);
1009 if (agent != null)
1010 {
1011 //m_log.DebugFormat("[FRIEND]: Found local agent {0}", agent.Name);
1012
1013 // friend is online and on this server...
1014 if (iAmOnline) agent.ControllingClient.SendAgentOnline(agentArr);
1015 else agent.ControllingClient.SendAgentOffline(agentArr);
1016
1017 // done, remove it
1018 friendIDsToSendTo.RemoveAt(i);
1019 }
1020 }
1021 else
1022 {
1023 //m_log.DebugFormat("[FRIEND]: Friend {0} ({1}) is offline; not sending.", uuid, i);
1024
1025 // friend is offline => no need to try sending
1026 friendIDsToSendTo.RemoveAt(i);
1027 }
1028 }
1029
1030 m_log.DebugFormat("[FRIEND]: Have {0} friends to contact via inter-region comms.", friendIDsToSendTo.Count);
1031
1032 // we now have all the friends left that are online (we think), but not on this region-server
1033 if (friendIDsToSendTo.Count > 0)
1034 {
1035 // sort them into regions
1036 Dictionary<ulong, List<UUID>> friendsInRegion = new Dictionary<ulong,List<UUID>>();
1037 foreach (UUID uuid in friendIDsToSendTo)
1038 {
1039 ulong handle = friendRegions[uuid].regionHandle; // this can't fail as we filtered above already
1040 List<UUID> friends;
1041 if (!friendsInRegion.TryGetValue(handle, out friends))
1042 {
1043 friends = new List<UUID>();
1044 friendsInRegion[handle] = friends;
1045 }
1046 friends.Add(uuid);
1047 }
1048 m_log.DebugFormat("[FRIEND]: Found {0} regions to send to.", friendRegions.Count);
1049
1050 // clear uuids list and collect missed friends in it for the next retry
1051 friendIDsToSendTo.Clear();
1052
1053 // send bulk updates to the region
1054 foreach (KeyValuePair<ulong, List<UUID>> pair in friendsInRegion)
1055 {
1056 //m_log.DebugFormat("[FRIEND]: Inform {0} friends in region {1} that user {2} is {3}line",
1057 // pair.Value.Count, pair.Key, client.Name, iAmOnline ? "on" : "off");
1058
1059 friendIDsToSendTo.AddRange(InformFriendsInOtherRegion(client.AgentId, pair.Key, pair.Value, iAmOnline));
1060 }
1061 }
1062 // now we have in friendIDsToSendTo only the agents left that TPed away while we tried to contact them.
1063 // In most cases, it will be empty, and it won't loop here. But sometimes, we have to work harder and try again...
1064 }
1065 while (++retry < 3 && friendIDsToSendTo.Count > 0);
1066 } 76 }
1067 77
1068 private void OnEconomyDataRequest(UUID agentID) 78 public Type ReplaceableInterface
1069 { 79 {
1070 // KLUDGE: This is the only way I found to get a message (only) after login was completed and the 80 get { return null; }
1071 // client is connected enough to receive UDP packets).
1072 // This packet seems to be sent only once, just after connection was established to the first
1073 // region after login.
1074 // We use it here to trigger a presence update; the old update-on-login was never be heard by
1075 // the freshly logged in viewer, as it wasn't connected to the region at that time.
1076 // TODO: Feel free to replace this by a better solution if you find one.
1077
1078 // get the agent. This should work every time, as we just got a packet from it
1079 //ScenePresence agent = GetRootPresenceFromAgentID(agentID);
1080 // KLUDGE 2: As this is sent quite early, the avatar isn't here as root agent yet. So, we have to cheat a bit
1081 ScenePresence agent = GetAnyPresenceFromAgentID(agentID);
1082
1083 // just to be paranoid...
1084 if (agent == null)
1085 {
1086 m_log.ErrorFormat("[FRIEND]: Got a packet from agent {0} who can't be found anymore!?", agentID);
1087 return;
1088 }
1089
1090 List<FriendListItem> fl;
1091 lock (m_friendLists)
1092 {
1093 fl = (List<FriendListItem>)m_friendLists.Get(agent.ControllingClient.AgentId.ToString(),
1094 m_initialScene.GetFriendList);
1095 }
1096
1097 // tell everyone that we are online
1098 SendPresenceState(agent.ControllingClient, fl, true);
1099 } 81 }
1100 82
1101 private void OnLogout(IClientAPI remoteClient) 83 public void OfferFriendship(UUID fromUserId, IClientAPI toUserClient,
84 string offerMessage)
1102 { 85 {
1103 List<FriendListItem> fl;
1104 lock (m_friendLists)
1105 {
1106 fl = (List<FriendListItem>)m_friendLists.Get(remoteClient.AgentId.ToString(),
1107 m_initialScene.GetFriendList);
1108 }
1109
1110 // tell everyone that we are offline
1111 SendPresenceState(remoteClient, fl, false);
1112 }
1113 private void GrantUserFriendRights(IClientAPI remoteClient, UUID requester, UUID target, int rights)
1114 {
1115 ((Scene)remoteClient.Scene).CommsManager.UpdateUserFriendPerms(requester, target, (uint)rights);
1116 } 86 }
1117 87
1118 public List<FriendListItem> GetUserFriends(UUID agentID) 88 public List<FriendListItem> GetUserFriends(UUID agentID)
1119 { 89 {
1120 List<FriendListItem> fl; 90 return null;
1121 lock (m_friendLists)
1122 {
1123 fl = (List<FriendListItem>)m_friendLists.Get(agentID.ToString(),
1124 m_initialScene.GetFriendList);
1125 }
1126
1127 return fl;
1128 } 91 }
1129 } 92 }
1130 #endregion
1131} 93}