29 using System.Collections.Generic;
31 using System.Reflection;
33 using OpenSim.Framework;
34 using OpenSim.Services.Connectors.Friends;
35 using OpenSim.Services.Connectors.Hypergrid;
36 using OpenSim.Services.Interfaces;
38 using OpenSim.Server.Base;
45 namespace OpenSim.Services.HypergridService
52 private static readonly ILog m_log =
54 MethodBase.GetCurrentMethod().DeclaringType);
56 static bool m_Initialized =
false;
67 private static string m_ConfigName =
"HGFriendsService";
71 if (m_FriendsLocalSimConnector == null)
72 m_FriendsLocalSimConnector = localSimConn;
78 if (configName != String.Empty)
79 m_ConfigName = configName;
81 Object[] args =
new Object[] { config };
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));
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);
92 theService = serverConfig.GetString(
"UserAccountService", string.Empty);
93 if (theService == String.Empty)
94 throw new Exception(
"No UserAccountService in " + m_ConfigName);
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);
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);
109 m_log.DebugFormat(
"[HGFRIENDS SERVICE]: Starting...");
114 #region IHGFriendsService
118 FriendInfo[] friendsInfo = m_FriendsService.GetFriends(userID);
121 if (finfo.
Friend.StartsWith(friendID.ToString()))
122 return finfo.TheirFlags;
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))
134 m_log.DebugFormat(
"[HGFRIENDS SERVICE]: New friendship {0} {1} ({2})", friend.PrincipalID, friend.Friend, verified);
137 FriendInfo[] finfos = m_FriendsService.GetFriends(friend.PrincipalID);
140 if (finfo.
Friend.StartsWith(friendID.ToString()))
145 return m_FriendsService.StoreFriend(friend.PrincipalID.ToString(),
friend.Friend, 0);
148 finfos = m_FriendsService.GetFriends(friendID);
149 bool userInitiatedOffer =
false;
152 if (
friend.Friend.StartsWith(finfo.
PrincipalID.ToString()) && finfo.
Friend.StartsWith(
friend.PrincipalID.ToString()) && finfo.
TheirFlags == -1)
154 userInitiatedOffer =
true;
156 m_FriendsService.Delete(friendID, finfo.Friend);
161 if (userInitiatedOffer)
163 m_FriendsService.StoreFriend(friend.PrincipalID.ToString(),
friend.Friend, 1);
164 m_FriendsService.StoreFriend(friend.Friend, friend.PrincipalID.ToString(), 1);
166 ForwardToSim(
"ApproveFriendshipRequest", friendID, Util.UniversalName(first, last, url),
"", friend.PrincipalID,
"");
174 FriendInfo[] finfos = m_FriendsService.GetFriends(friend.PrincipalID);
178 if (finfo.
Friend.StartsWith(
friend.Friend) && finfo.Friend.EndsWith(secret))
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());
193 UserAccount account = m_UserAccountService.GetUserAccount(UUID.Zero, toID);
202 o => ProcessFriendshipOffered(fromID, fromName, toID, message), null,
"HGFriendsService.ProcessFriendshipOffered");
209 FriendInfo[] finfos = m_FriendsService.GetFriends(toID.ToString());
220 if (m_FriendsService == null || m_PresenceService == null)
222 m_log.WarnFormat(
"[HGFRIENDS SERVICE]: Unable to perform status notifications because friends or presence services are missing");
223 return new List<UUID>();
228 List<UUID> localFriendsOnline =
new List<UUID>();
230 m_log.DebugFormat(
"[HGFRIENDS SERVICE]: Status notification: foreign user {0} wants to notify {1} local friends of {2} status",
231 foreignUserID, friends.Count, (online ?
"online" :
"offline"));
235 List<string> usersToBeNotified =
new List<string>();
236 foreach (
string uui
in friends)
239 string secret = string.Empty, tmp = string.Empty;
240 if (Util.ParseUniversalUserIdentifier(uui, out localUserID, out tmp, out tmp, out tmp, out secret))
242 FriendInfo[] friendInfos = m_FriendsService.GetFriends(localUserID);
245 if (finfo.
Friend.StartsWith(foreignUserID.ToString()) && finfo.
Friend.EndsWith(secret))
248 usersToBeNotified.Add(localUserID.ToString());
258 PresenceInfo[] friendSessions = m_PresenceService.GetAgents(usersToBeNotified.ToArray());
259 if (friendSessions != null && friendSessions.Length > 0)
265 friendSession = pinfo;
269 if (friendSession != null)
271 ForwardStatusNotificationToSim(friendSession.RegionID, foreignUserID, friendSession.UserID, online);
272 usersToBeNotified.Remove(friendSession.UserID.ToString());
274 if (UUID.TryParse(friendSession.UserID, out
id))
275 localFriendsOnline.Add(
id);
297 return localFriendsOnline;
300 return new List<UUID>();
303 #endregion IHGFriendsService
307 private void ProcessFriendshipOffered(UUID fromID, String fromName, UUID toID, String message)
313 if (!fromName.Contains(
"@"))
316 string[] parts = fromName.Split(
new char[] {
'@'});
317 if (parts.Length != 2)
320 string uriStr =
"http://" + parts[1];
325 catch (UriFormatException)
331 Dictionary<string, object> servers = uasConn.GetServerURLs(fromID);
332 if (!servers.ContainsKey(
"FriendsServerURI"))
338 m_log.WarnFormat(
"[HGFRIENDS SERVICE]: Friendship request from {0} to {1} is invalid. Impersonations?", fromID, toID);
342 string fromUUI = Util.UniversalIdentifier(fromID, parts[0],
"@" + parts[1], uriStr);
344 ForwardToSim(
"FriendshipOffered", fromID, fromName, fromUUI, toID, message);
347 private bool ForwardToSim(
string op, UUID fromID,
string name, String fromUUI, UUID toID,
string message)
351 PresenceInfo[] sessions = m_PresenceService.GetAgents(
new string[] { toID.ToString() });
352 if (sessions != null && sessions.Length > 0)
353 session = sessions[0];
355 region = m_GridService.GetRegionByUUID(UUID.Zero, session.RegionID);
359 case "FriendshipOffered":
361 string secret = UUID.Random().ToString().Substring(0, 8);
362 m_FriendsService.StoreFriend(toID.ToString(), fromUUI +
";" + secret, 0);
363 if (m_FriendsLocalSimConnector != null)
366 (byte)InstantMessageDialog.FriendshipOffered, message,
false, Vector3.Zero);
368 im.imSessionID = im.fromAgentID;
369 return m_FriendsLocalSimConnector.LocalFriendshipOffered(toID, im);
371 else if (region != null)
372 return m_FriendsSimConnector.FriendshipOffered(region, fromID, toID, message, name);
374 case "ApproveFriendshipRequest":
375 if (m_FriendsLocalSimConnector != null)
376 return m_FriendsLocalSimConnector.LocalFriendshipApproved(fromID, name, toID);
377 else if (region != null)
378 return m_FriendsSimConnector.FriendshipApproved(region, fromID, name, toID);
388 if (UUID.TryParse(user, out userID))
390 if (m_FriendsLocalSimConnector != null)
392 m_log.DebugFormat(
"[HGFRIENDS SERVICE]: Local Notify, user {0} is {1}", foreignUserID, (online ?
"online" :
"offline"));
393 m_FriendsLocalSimConnector.StatusNotify(foreignUserID, userID, online);
397 GridRegion region = m_GridService.GetRegionByUUID(UUID.Zero , regionID);
400 m_log.DebugFormat(
"[HGFRIENDS SERVICE]: Remote Notify to region {0}, user {1} is {2}", region.RegionName, foreignUserID, (online ?
"online" :
"offline"));
401 m_FriendsSimConnector.StatusNotify(region, foreignUserID, userID.ToString(), online);
int GetFriendPerms(UUID userID, UUID friendID)
static IGridService m_GridService
static IFriendsSimConnector m_FriendsLocalSimConnector
OpenSim.Services.Interfaces.GridRegion GridRegion
static IPresenceService m_PresenceService
static IGridUserService m_GridUserService
bool ValidateFriendshipOffered(UUID fromID, UUID toID)
static IGatekeeperService m_GatekeeperService
void ForwardStatusNotificationToSim(UUID regionID, UUID foreignUserID, string user, bool online)
bool DeleteFriendship(FriendInfo friend, string secret)
bool FriendshipOffered(UUID fromID, string fromName, UUID toID, string message)
bool ValidateFriendshipOffered(UUID fromID, UUID toID)
OpenSim.Services.Interfaces.FriendInfo FriendInfo
int TheirFlags
The permissions that the friend has granted to this user.
bool NewFriendship(FriendInfo friend, bool verified)
static IFriendsService m_FriendsService
static IUserAccountService m_UserAccountService
HGFriendsService(IConfigSource config, String configName, IFriendsSimConnector localSimConn)
List< UUID > StatusNotification(List< string > friends, UUID foreignUserID, bool online)
static FriendsSimConnector m_FriendsSimConnector