HGFriendsModule.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. using System.Threading;
  32. using log4net;
  33. using Nini.Config;
  34. using Nwc.XmlRpc;
  35. using Mono.Addins;
  36. using OpenMetaverse;
  37. using OpenSim.Framework;
  38. using OpenSim.Region.Framework.Interfaces;
  39. using OpenSim.Region.Framework.Scenes;
  40. using OpenSim.Services.Interfaces;
  41. using OpenSim.Services.Connectors.Hypergrid;
  42. using FriendInfo = OpenSim.Services.Interfaces.FriendInfo;
  43. using PresenceInfo = OpenSim.Services.Interfaces.PresenceInfo;
  44. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  45. namespace OpenSim.Region.CoreModules.Avatar.Friends
  46. {
  47. public class HGFriendsModule : FriendsModule, ISharedRegionModule, IFriendsModule, IFriendsSimConnector
  48. {
  49. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  50. IUserManagement m_uMan;
  51. IUserManagement UserManagementModule
  52. {
  53. get
  54. {
  55. if (m_uMan == null)
  56. m_uMan = m_Scenes[0].RequestModuleInterface<IUserManagement>();
  57. return m_uMan;
  58. }
  59. }
  60. #region ISharedRegionModule
  61. public override string Name
  62. {
  63. get { return "HGFriendsModule"; }
  64. }
  65. public override void AddRegion(Scene scene)
  66. {
  67. if (!m_Enabled)
  68. return;
  69. base.AddRegion(scene);
  70. scene.RegisterModuleInterface<IFriendsSimConnector>(this);
  71. }
  72. #endregion
  73. #region IFriendsSimConnector
  74. /// <summary>
  75. /// Notify the user that the friend's status changed
  76. /// </summary>
  77. /// <param name="userID">user to be notified</param>
  78. /// <param name="friendID">friend whose status changed</param>
  79. /// <param name="online">status</param>
  80. /// <returns></returns>
  81. public bool StatusNotify(UUID friendID, UUID userID, bool online)
  82. {
  83. return LocalStatusNotification(friendID, userID, online);
  84. }
  85. #endregion
  86. protected override bool CacheFriends(IClientAPI client)
  87. {
  88. // m_log.DebugFormat("[HGFRIENDS MODULE]: Entered CacheFriends for {0}", client.Name);
  89. if (base.CacheFriends(client))
  90. {
  91. UUID agentID = client.AgentId;
  92. // we do this only for the root agent
  93. if (m_Friends[agentID].Refcount == 1)
  94. {
  95. // We need to preload the user management cache with the names
  96. // of foreign friends, just like we do with SOPs' creators
  97. foreach (FriendInfo finfo in m_Friends[agentID].Friends)
  98. {
  99. if (finfo.TheirFlags != -1)
  100. {
  101. UUID id;
  102. if (!UUID.TryParse(finfo.Friend, out id))
  103. {
  104. string url = string.Empty, first = string.Empty, last = string.Empty, tmp = string.Empty;
  105. if (Util.ParseUniversalUserIdentifier(finfo.Friend, out id, out url, out first, out last, out tmp))
  106. {
  107. IUserManagement uMan = m_Scenes[0].RequestModuleInterface<IUserManagement>();
  108. uMan.AddUser(id, url + ";" + first + " " + last);
  109. }
  110. }
  111. }
  112. }
  113. // m_log.DebugFormat("[HGFRIENDS MODULE]: Exiting CacheFriends for {0} since detected root agent", client.Name);
  114. return true;
  115. }
  116. }
  117. // m_log.DebugFormat("[HGFRIENDS MODULE]: Exiting CacheFriends for {0} since detected not root agent", client.Name);
  118. return false;
  119. }
  120. public override bool SendFriendsOnlineIfNeeded(IClientAPI client)
  121. {
  122. // m_log.DebugFormat("[HGFRIENDS MODULE]: Entering SendFriendsOnlineIfNeeded for {0}", client.Name);
  123. if (base.SendFriendsOnlineIfNeeded(client))
  124. {
  125. AgentCircuitData aCircuit = ((Scene)client.Scene).AuthenticateHandler.GetAgentCircuitData(client.AgentId);
  126. if (aCircuit != null && (aCircuit.teleportFlags & (uint)Constants.TeleportFlags.ViaHGLogin) != 0)
  127. {
  128. UserAccount account = m_Scenes[0].UserAccountService.GetUserAccount(client.Scene.RegionInfo.ScopeID, client.AgentId);
  129. if (account == null) // foreign
  130. {
  131. FriendInfo[] friends = GetFriends(client.AgentId);
  132. foreach (FriendInfo f in friends)
  133. {
  134. client.SendChangeUserRights(new UUID(f.Friend), client.AgentId, f.TheirFlags);
  135. }
  136. }
  137. }
  138. }
  139. // m_log.DebugFormat("[HGFRIENDS MODULE]: Exiting SendFriendsOnlineIfNeeded for {0}", client.Name);
  140. return false;
  141. }
  142. protected override void GetOnlineFriends(UUID userID, List<string> friendList, /*collector*/ List<UUID> online)
  143. {
  144. // m_log.DebugFormat("[HGFRIENDS MODULE]: Entering GetOnlineFriends for {0}", userID);
  145. List<string> fList = new List<string>();
  146. foreach (string s in friendList)
  147. {
  148. if (s.Length < 36)
  149. m_log.WarnFormat(
  150. "[HGFRIENDS MODULE]: Ignoring friend {0} ({1} chars) for {2} since identifier too short",
  151. s, s.Length, userID);
  152. else
  153. fList.Add(s.Substring(0, 36));
  154. }
  155. PresenceInfo[] presence = PresenceService.GetAgents(fList.ToArray());
  156. foreach (PresenceInfo pi in presence)
  157. {
  158. UUID presenceID;
  159. if (UUID.TryParse(pi.UserID, out presenceID))
  160. online.Add(presenceID);
  161. }
  162. // m_log.DebugFormat("[HGFRIENDS MODULE]: Exiting GetOnlineFriends for {0}", userID);
  163. }
  164. //protected override void GetOnlineFriends(UUID userID, List<string> friendList, /*collector*/ List<UUID> online)
  165. //{
  166. // // Let's single out the UUIs
  167. // List<string> localFriends = new List<string>();
  168. // List<string> foreignFriends = new List<string>();
  169. // string tmp = string.Empty;
  170. // foreach (string s in friendList)
  171. // {
  172. // UUID id;
  173. // if (UUID.TryParse(s, out id))
  174. // localFriends.Add(s);
  175. // else if (Util.ParseUniversalUserIdentifier(s, out id, out tmp, out tmp, out tmp, out tmp))
  176. // {
  177. // foreignFriends.Add(s);
  178. // // add it here too, who knows maybe the foreign friends happens to be on this grid
  179. // localFriends.Add(id.ToString());
  180. // }
  181. // }
  182. // // OK, see who's present on this grid
  183. // List<string> toBeRemoved = new List<string>();
  184. // PresenceInfo[] presence = PresenceService.GetAgents(localFriends.ToArray());
  185. // foreach (PresenceInfo pi in presence)
  186. // {
  187. // UUID presenceID;
  188. // if (UUID.TryParse(pi.UserID, out presenceID))
  189. // {
  190. // online.Add(presenceID);
  191. // foreach (string s in foreignFriends)
  192. // if (s.StartsWith(pi.UserID))
  193. // toBeRemoved.Add(s);
  194. // }
  195. // }
  196. // foreach (string s in toBeRemoved)
  197. // foreignFriends.Remove(s);
  198. // // OK, let's send this up the stack, and leave a closure here
  199. // // collecting online friends in other grids
  200. // Util.FireAndForget(delegate { CollectOnlineFriendsElsewhere(userID, foreignFriends); });
  201. //}
  202. //private void CollectOnlineFriendsElsewhere(UUID userID, List<string> foreignFriends)
  203. //{
  204. // // let's divide the friends on a per-domain basis
  205. // Dictionary<string, List<string>> friendsPerDomain = new Dictionary<string, List<string>>();
  206. // foreach (string friend in foreignFriends)
  207. // {
  208. // UUID friendID;
  209. // if (!UUID.TryParse(friend, out friendID))
  210. // {
  211. // // it's a foreign friend
  212. // string url = string.Empty, tmp = string.Empty;
  213. // if (Util.ParseUniversalUserIdentifier(friend, out friendID, out url, out tmp, out tmp, out tmp))
  214. // {
  215. // if (!friendsPerDomain.ContainsKey(url))
  216. // friendsPerDomain[url] = new List<string>();
  217. // friendsPerDomain[url].Add(friend);
  218. // }
  219. // }
  220. // }
  221. // // Now, call those worlds
  222. // foreach (KeyValuePair<string, List<string>> kvp in friendsPerDomain)
  223. // {
  224. // List<string> ids = new List<string>();
  225. // foreach (string f in kvp.Value)
  226. // ids.Add(f);
  227. // UserAgentServiceConnector uConn = new UserAgentServiceConnector(kvp.Key);
  228. // List<UUID> online = uConn.GetOnlineFriends(userID, ids);
  229. // // Finally send the notifications to the user
  230. // // this whole process may take a while, so let's check at every
  231. // // iteration that the user is still here
  232. // IClientAPI client = LocateClientObject(userID);
  233. // if (client != null)
  234. // client.SendAgentOnline(online.ToArray());
  235. // else
  236. // break;
  237. // }
  238. //}
  239. protected override void StatusNotify(List<FriendInfo> friendList, UUID userID, bool online)
  240. {
  241. // m_log.DebugFormat("[HGFRIENDS MODULE]: Entering StatusNotify for {0}", userID);
  242. // First, let's divide the friends on a per-domain basis
  243. Dictionary<string, List<FriendInfo>> friendsPerDomain = new Dictionary<string, List<FriendInfo>>();
  244. foreach (FriendInfo friend in friendList)
  245. {
  246. UUID friendID;
  247. if (UUID.TryParse(friend.Friend, out friendID))
  248. {
  249. if (!friendsPerDomain.ContainsKey("local"))
  250. friendsPerDomain["local"] = new List<FriendInfo>();
  251. friendsPerDomain["local"].Add(friend);
  252. }
  253. else
  254. {
  255. // it's a foreign friend
  256. string url = string.Empty, tmp = string.Empty;
  257. if (Util.ParseUniversalUserIdentifier(friend.Friend, out friendID, out url, out tmp, out tmp, out tmp))
  258. {
  259. // Let's try our luck in the local sim. Who knows, maybe it's here
  260. if (LocalStatusNotification(userID, friendID, online))
  261. continue;
  262. if (!friendsPerDomain.ContainsKey(url))
  263. friendsPerDomain[url] = new List<FriendInfo>();
  264. friendsPerDomain[url].Add(friend);
  265. }
  266. }
  267. }
  268. // For the local friends, just call the base method
  269. // Let's do this first of all
  270. if (friendsPerDomain.ContainsKey("local"))
  271. base.StatusNotify(friendsPerDomain["local"], userID, online);
  272. foreach (KeyValuePair<string, List<FriendInfo>> kvp in friendsPerDomain)
  273. {
  274. if (kvp.Key != "local")
  275. {
  276. // For the others, call the user agent service
  277. List<string> ids = new List<string>();
  278. foreach (FriendInfo f in kvp.Value)
  279. ids.Add(f.Friend);
  280. UserAgentServiceConnector uConn = new UserAgentServiceConnector(kvp.Key);
  281. List<UUID> friendsOnline = uConn.StatusNotification(ids, userID, online);
  282. if (online && friendsOnline.Count > 0)
  283. {
  284. IClientAPI client = LocateClientObject(userID);
  285. if (client != null)
  286. client.SendAgentOnline(friendsOnline.ToArray());
  287. }
  288. }
  289. }
  290. // m_log.DebugFormat("[HGFRIENDS MODULE]: Exiting StatusNotify for {0}", userID);
  291. }
  292. protected override bool GetAgentInfo(UUID scopeID, string fid, out UUID agentID, out string first, out string last)
  293. {
  294. first = "Unknown"; last = "User";
  295. if (base.GetAgentInfo(scopeID, fid, out agentID, out first, out last))
  296. return true;
  297. // fid is not a UUID...
  298. string url = string.Empty, tmp = string.Empty;
  299. if (Util.ParseUniversalUserIdentifier(fid, out agentID, out url, out first, out last, out tmp))
  300. {
  301. IUserManagement userMan = m_Scenes[0].RequestModuleInterface<IUserManagement>();
  302. userMan.AddUser(agentID, first, last, url);
  303. return true;
  304. }
  305. return false;
  306. }
  307. protected override string GetFriendshipRequesterName(UUID agentID)
  308. {
  309. // For the time being we assume that HG friendship requests can only happen
  310. // when avies are on the same region.
  311. IClientAPI client = LocateClientObject(agentID);
  312. if (client != null)
  313. return client.FirstName + " " + client.LastName;
  314. else
  315. return base.GetFriendshipRequesterName(agentID);
  316. }
  317. protected override string FriendshipMessage(string friendID)
  318. {
  319. UUID id;
  320. if (UUID.TryParse(friendID, out id))
  321. return base.FriendshipMessage(friendID);
  322. return "Please confirm this friendship you made while you were away.";
  323. }
  324. protected override FriendInfo GetFriend(FriendInfo[] friends, UUID friendID)
  325. {
  326. foreach (FriendInfo fi in friends)
  327. {
  328. if (fi.Friend.StartsWith(friendID.ToString()))
  329. return fi;
  330. }
  331. return null;
  332. }
  333. protected override FriendInfo[] GetFriendsFromService(IClientAPI client)
  334. {
  335. // m_log.DebugFormat("[HGFRIENDS MODULE]: Entering GetFriendsFromService for {0}", client.Name);
  336. Boolean agentIsLocal = true;
  337. if (UserManagementModule != null)
  338. agentIsLocal = UserManagementModule.IsLocalGridUser(client.AgentId);
  339. if (agentIsLocal)
  340. return base.GetFriendsFromService(client);
  341. FriendInfo[] finfos = new FriendInfo[0];
  342. // Foreigner
  343. AgentCircuitData agentClientCircuit = ((Scene)(client.Scene)).AuthenticateHandler.GetAgentCircuitData(client.CircuitCode);
  344. if (agentClientCircuit != null)
  345. {
  346. string agentUUI = Util.ProduceUserUniversalIdentifier(agentClientCircuit);
  347. finfos = FriendsService.GetFriends(agentUUI);
  348. m_log.DebugFormat("[HGFRIENDS MODULE]: Fetched {0} local friends for visitor {1}", finfos.Length, agentUUI);
  349. }
  350. // m_log.DebugFormat("[HGFRIENDS MODULE]: Exiting GetFriendsFromService for {0}", client.Name);
  351. return finfos;
  352. }
  353. protected override bool StoreRights(UUID agentID, UUID friendID, int rights)
  354. {
  355. Boolean agentIsLocal = true;
  356. Boolean friendIsLocal = true;
  357. if (UserManagementModule != null)
  358. {
  359. agentIsLocal = UserManagementModule.IsLocalGridUser(agentID);
  360. friendIsLocal = UserManagementModule.IsLocalGridUser(friendID);
  361. }
  362. // Are they both local users?
  363. if (agentIsLocal && friendIsLocal)
  364. {
  365. // local grid users
  366. return base.StoreRights(agentID, friendID, rights);
  367. }
  368. if (agentIsLocal) // agent is local, friend is foreigner
  369. {
  370. FriendInfo[] finfos = GetFriends(agentID);
  371. FriendInfo finfo = GetFriend(finfos, friendID);
  372. if (finfo != null)
  373. {
  374. FriendsService.StoreFriend(agentID.ToString(), finfo.Friend, rights);
  375. return true;
  376. }
  377. }
  378. if (friendIsLocal) // agent is foreigner, friend is local
  379. {
  380. string agentUUI = GetUUI(friendID, agentID);
  381. if (agentUUI != string.Empty)
  382. {
  383. FriendsService.StoreFriend(agentUUI, friendID.ToString(), rights);
  384. return true;
  385. }
  386. }
  387. return false;
  388. }
  389. protected override void StoreBackwards(UUID friendID, UUID agentID)
  390. {
  391. Boolean agentIsLocal = true;
  392. Boolean friendIsLocal = true;
  393. if (UserManagementModule != null)
  394. {
  395. agentIsLocal = UserManagementModule.IsLocalGridUser(agentID);
  396. friendIsLocal = UserManagementModule.IsLocalGridUser(friendID);
  397. }
  398. // Are they both local users?
  399. if (agentIsLocal && friendIsLocal)
  400. {
  401. // local grid users
  402. m_log.DebugFormat("[HGFRIENDS MODULE]: Users are both local");
  403. base.StoreBackwards(friendID, agentID);
  404. return;
  405. }
  406. // no provision for this temporary friendship state
  407. //FriendsService.StoreFriend(friendID.ToString(), agentID.ToString(), 0);
  408. }
  409. protected override void StoreFriendships(UUID agentID, UUID friendID)
  410. {
  411. Boolean agentIsLocal = true;
  412. Boolean friendIsLocal = true;
  413. if (UserManagementModule != null)
  414. {
  415. agentIsLocal = UserManagementModule.IsLocalGridUser(agentID);
  416. friendIsLocal = UserManagementModule.IsLocalGridUser(friendID);
  417. }
  418. // Are they both local users?
  419. if (agentIsLocal && friendIsLocal)
  420. {
  421. // local grid users
  422. m_log.DebugFormat("[HGFRIENDS MODULE]: Users are both local");
  423. base.StoreFriendships(agentID, friendID);
  424. return;
  425. }
  426. // ok, at least one of them is foreigner, let's get their data
  427. IClientAPI agentClient = LocateClientObject(agentID);
  428. IClientAPI friendClient = LocateClientObject(friendID);
  429. AgentCircuitData agentClientCircuit = null;
  430. AgentCircuitData friendClientCircuit = null;
  431. string agentUUI = string.Empty;
  432. string friendUUI = string.Empty;
  433. string agentFriendService = string.Empty;
  434. string friendFriendService = string.Empty;
  435. if (agentClient != null)
  436. {
  437. agentClientCircuit = ((Scene)(agentClient.Scene)).AuthenticateHandler.GetAgentCircuitData(agentClient.CircuitCode);
  438. agentUUI = Util.ProduceUserUniversalIdentifier(agentClientCircuit);
  439. agentFriendService = agentClientCircuit.ServiceURLs["FriendsServerURI"].ToString();
  440. }
  441. if (friendClient != null)
  442. {
  443. friendClientCircuit = ((Scene)(friendClient.Scene)).AuthenticateHandler.GetAgentCircuitData(friendClient.CircuitCode);
  444. friendUUI = Util.ProduceUserUniversalIdentifier(friendClientCircuit);
  445. friendFriendService = friendClientCircuit.ServiceURLs["FriendsServerURI"].ToString();
  446. }
  447. m_log.DebugFormat("[HGFRIENDS MODULE] HG Friendship! thisUUI={0}; friendUUI={1}; foreignThisFriendService={2}; foreignFriendFriendService={3}",
  448. agentUUI, friendUUI, agentFriendService, friendFriendService);
  449. // Generate a random 8-character hex number that will sign this friendship
  450. string secret = UUID.Random().ToString().Substring(0, 8);
  451. if (agentIsLocal) // agent is local, 'friend' is foreigner
  452. {
  453. // This may happen when the agent returned home, in which case the friend is not there
  454. // We need to look for its information in the friends list itself
  455. bool confirming = false;
  456. if (friendUUI == string.Empty)
  457. {
  458. FriendInfo[] finfos = GetFriends(agentID);
  459. foreach (FriendInfo finfo in finfos)
  460. {
  461. if (finfo.TheirFlags == -1)
  462. {
  463. if (finfo.Friend.StartsWith(friendID.ToString()))
  464. {
  465. friendUUI = finfo.Friend;
  466. confirming = true;
  467. }
  468. }
  469. }
  470. }
  471. // If it's confirming the friendship, we already have the full friendUUI with the secret
  472. string theFriendUUID = confirming ? friendUUI : friendUUI + ";" + secret;
  473. // store in the local friends service a reference to the foreign friend
  474. FriendsService.StoreFriend(agentID.ToString(), theFriendUUID, 1);
  475. // and also the converse
  476. FriendsService.StoreFriend(theFriendUUID, agentID.ToString(), 1);
  477. if (!confirming && friendClientCircuit != null)
  478. {
  479. // store in the foreign friends service a reference to the local agent
  480. HGFriendsServicesConnector friendsConn = new HGFriendsServicesConnector(friendFriendService, friendClientCircuit.SessionID, friendClientCircuit.ServiceSessionID);
  481. friendsConn.NewFriendship(friendID, agentUUI + ";" + secret);
  482. }
  483. }
  484. else if (friendIsLocal) // 'friend' is local, agent is foreigner
  485. {
  486. // store in the local friends service a reference to the foreign agent
  487. FriendsService.StoreFriend(friendID.ToString(), agentUUI + ";" + secret, 1);
  488. // and also the converse
  489. FriendsService.StoreFriend(agentUUI + ";" + secret, friendID.ToString(), 1);
  490. if (agentClientCircuit != null)
  491. {
  492. // store in the foreign friends service a reference to the local agent
  493. HGFriendsServicesConnector friendsConn = new HGFriendsServicesConnector(agentFriendService, agentClientCircuit.SessionID, agentClientCircuit.ServiceSessionID);
  494. friendsConn.NewFriendship(agentID, friendUUI + ";" + secret);
  495. }
  496. }
  497. else // They're both foreigners!
  498. {
  499. HGFriendsServicesConnector friendsConn;
  500. if (agentClientCircuit != null)
  501. {
  502. friendsConn = new HGFriendsServicesConnector(agentFriendService, agentClientCircuit.SessionID, agentClientCircuit.ServiceSessionID);
  503. friendsConn.NewFriendship(agentID, friendUUI + ";" + secret);
  504. }
  505. if (friendClientCircuit != null)
  506. {
  507. friendsConn = new HGFriendsServicesConnector(friendFriendService, friendClientCircuit.SessionID, friendClientCircuit.ServiceSessionID);
  508. friendsConn.NewFriendship(friendID, agentUUI + ";" + secret);
  509. }
  510. }
  511. // my brain hurts now
  512. }
  513. protected override bool DeleteFriendship(UUID agentID, UUID exfriendID)
  514. {
  515. Boolean agentIsLocal = true;
  516. Boolean friendIsLocal = true;
  517. if (UserManagementModule != null)
  518. {
  519. agentIsLocal = UserManagementModule.IsLocalGridUser(agentID);
  520. friendIsLocal = UserManagementModule.IsLocalGridUser(exfriendID);
  521. }
  522. // Are they both local users?
  523. if (agentIsLocal && friendIsLocal)
  524. {
  525. // local grid users
  526. return base.DeleteFriendship(agentID, exfriendID);
  527. }
  528. // ok, at least one of them is foreigner, let's get their data
  529. string agentUUI = string.Empty;
  530. string friendUUI = string.Empty;
  531. if (agentIsLocal) // agent is local, 'friend' is foreigner
  532. {
  533. // We need to look for its information in the friends list itself
  534. FriendInfo[] finfos = GetFriends(agentID);
  535. FriendInfo finfo = GetFriend(finfos, exfriendID);
  536. if (finfo != null)
  537. {
  538. friendUUI = finfo.Friend;
  539. // delete in the local friends service the reference to the foreign friend
  540. FriendsService.Delete(agentID, friendUUI);
  541. // and also the converse
  542. FriendsService.Delete(friendUUI, agentID.ToString());
  543. // notify the exfriend's service
  544. Util.FireAndForget(delegate { Delete(exfriendID, agentID, friendUUI); });
  545. m_log.DebugFormat("[HGFRIENDS MODULE]: {0} terminated {1}", agentID, friendUUI);
  546. return true;
  547. }
  548. }
  549. else if (friendIsLocal) // agent is foreigner, 'friend' is local
  550. {
  551. agentUUI = GetUUI(exfriendID, agentID);
  552. if (agentUUI != string.Empty)
  553. {
  554. // delete in the local friends service the reference to the foreign agent
  555. FriendsService.Delete(exfriendID, agentUUI);
  556. // and also the converse
  557. FriendsService.Delete(agentUUI, exfriendID.ToString());
  558. // notify the agent's service?
  559. Util.FireAndForget(delegate { Delete(agentID, exfriendID, agentUUI); });
  560. m_log.DebugFormat("[HGFRIENDS MODULE]: {0} terminated {1}", agentUUI, exfriendID);
  561. return true;
  562. }
  563. }
  564. //else They're both foreigners! Can't handle this
  565. return false;
  566. }
  567. private string GetUUI(UUID localUser, UUID foreignUser)
  568. {
  569. // Let's see if the user is here by any chance
  570. FriendInfo[] finfos = GetFriends(localUser);
  571. if (finfos != EMPTY_FRIENDS) // friend is here, cool
  572. {
  573. FriendInfo finfo = GetFriend(finfos, foreignUser);
  574. if (finfo != null)
  575. {
  576. return finfo.Friend;
  577. }
  578. }
  579. else // user is not currently on this sim, need to get from the service
  580. {
  581. finfos = FriendsService.GetFriends(localUser);
  582. foreach (FriendInfo finfo in finfos)
  583. {
  584. if (finfo.Friend.StartsWith(foreignUser.ToString())) // found it!
  585. {
  586. return finfo.Friend;
  587. }
  588. }
  589. }
  590. return string.Empty;
  591. }
  592. private void Delete(UUID foreignUser, UUID localUser, string uui)
  593. {
  594. UUID id;
  595. string url = string.Empty, secret = string.Empty, tmp = string.Empty;
  596. if (Util.ParseUniversalUserIdentifier(uui, out id, out url, out tmp, out tmp, out secret))
  597. {
  598. m_log.DebugFormat("[HGFRIENDS MODULE]: Deleting friendship from {0}", url);
  599. HGFriendsServicesConnector friendConn = new HGFriendsServicesConnector(url);
  600. friendConn.DeleteFriendship(foreignUser, localUser, secret);
  601. }
  602. }
  603. }
  604. }