HGFriendsModule.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "HGFriendsModule")]
  48. public class HGFriendsModule : FriendsModule, ISharedRegionModule, IFriendsModule, IFriendsSimConnector
  49. {
  50. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  51. private int m_levelHGFriends = 0;
  52. IUserManagement m_uMan;
  53. public IUserManagement UserManagementModule
  54. {
  55. get
  56. {
  57. m_uMan ??= m_Scenes[0].RequestModuleInterface<IUserManagement>();
  58. return m_uMan;
  59. }
  60. }
  61. protected HGFriendsServicesConnector m_HGFriendsConnector = new();
  62. protected HGStatusNotifier m_StatusNotifier;
  63. #region ISharedRegionModule
  64. public override string Name
  65. {
  66. get { return "HGFriendsModule"; }
  67. }
  68. public override void AddRegion(Scene scene)
  69. {
  70. if (!m_Enabled)
  71. return;
  72. base.AddRegion(scene);
  73. scene.RegisterModuleInterface<IFriendsSimConnector>(this);
  74. }
  75. public override void RegionLoaded(Scene scene)
  76. {
  77. if (!m_Enabled)
  78. return;
  79. if (m_StatusNotifier == null)
  80. m_StatusNotifier = new HGStatusNotifier(this);
  81. }
  82. protected override void InitModule(IConfigSource config)
  83. {
  84. base.InitModule(config);
  85. // Additionally to the base method
  86. IConfig friendsConfig = config.Configs["HGFriendsModule"];
  87. if (friendsConfig != null)
  88. {
  89. m_levelHGFriends = friendsConfig.GetInt("LevelHGFriends", 0);
  90. // TODO: read in all config variables pertaining to
  91. // HG friendship permissions
  92. }
  93. }
  94. #endregion
  95. #region IFriendsSimConnector
  96. /// <summary>
  97. /// Notify the user that the friend's status changed
  98. /// </summary>
  99. /// <param name="userID">user to be notified</param>
  100. /// <param name="friendID">friend whose status changed</param>
  101. /// <param name="online">status</param>
  102. /// <returns></returns>
  103. public bool StatusNotify(UUID friendID, UUID userID, bool online)
  104. {
  105. return LocalStatusNotification(friendID, userID, online);
  106. }
  107. #endregion
  108. protected override void OnInstantMessage(IClientAPI client, GridInstantMessage im)
  109. {
  110. if ((InstantMessageDialog)im.dialog == InstantMessageDialog.FriendshipOffered)
  111. {
  112. // we got a friendship offer
  113. UUID principalID = new(im.fromAgentID);
  114. UUID friendID = new(im.toAgentID);
  115. // Check if friendID is foreigner and if principalID has the permission
  116. // to request friendships with foreigners. If not, return immediately.
  117. if (!UserManagementModule.IsLocalGridUser(friendID))
  118. {
  119. ((Scene)client.Scene).TryGetScenePresence(principalID, out ScenePresence avatar);
  120. if (avatar is null)
  121. return;
  122. if (avatar.GodController.UserLevel < m_levelHGFriends)
  123. {
  124. client.SendAgentAlertMessage("Unable to send friendship invitation to foreigner. Insufficient permissions.", false);
  125. return;
  126. }
  127. }
  128. }
  129. base.OnInstantMessage(client, im);
  130. }
  131. protected override void OnApproveFriendRequest(IClientAPI client, UUID friendID, List<UUID> callingCardFolders)
  132. {
  133. // Update the local cache. Yes, we need to do it right here
  134. // because the HGFriendsService placed something on the DB
  135. // from under the sim
  136. base.OnApproveFriendRequest(client, friendID, callingCardFolders);
  137. }
  138. protected override bool CacheFriends(IClientAPI client)
  139. {
  140. //m_log.DebugFormat("[HGFRIENDS MODULE]: Entered CacheFriends for {0}", client.Name);
  141. if (base.CacheFriends(client))
  142. {
  143. // we do this only for the root agent
  144. UserFriendData FriendData = m_Friends[client.AgentId];
  145. if (FriendData.Refcount == 1)
  146. {
  147. IUserManagement uMan = m_Scenes[0].RequestModuleInterface<IUserManagement>();
  148. if(uMan == null)
  149. return true;
  150. // We need to preload the user management cache with the names
  151. // of foreign friends, just like we do with SOPs' creators
  152. foreach (FriendInfo finfo in FriendData.Friends)
  153. {
  154. if (finfo.TheirFlags != -1)
  155. {
  156. if (Util.ParseFullUniversalUserIdentifier(finfo.Friend, out UUID id, out string url, out string first, out string last))
  157. {
  158. //m_log.DebugFormat("[HGFRIENDS MODULE]: caching {0}", finfo.Friend);
  159. uMan.AddUser(id,first,last, url);
  160. }
  161. }
  162. }
  163. //m_log.DebugFormat("[HGFRIENDS MODULE]: Exiting CacheFriends for {0} since detected root agent", client.Name);
  164. return true;
  165. }
  166. }
  167. //m_log.DebugFormat("[HGFRIENDS MODULE]: Exiting CacheFriends for {0} since detected not root agent", client.Name);
  168. return false;
  169. }
  170. public override bool SendFriendsOnlineIfNeeded(IClientAPI client)
  171. {
  172. //m_log.DebugFormat("[HGFRIENDS MODULE]: Entering SendFriendsOnlineIfNeeded for {0}", client.Name);
  173. if (base.SendFriendsOnlineIfNeeded(client))
  174. {
  175. AgentCircuitData aCircuit = ((Scene)client.Scene).AuthenticateHandler.GetAgentCircuitData(client.AgentId);
  176. if (aCircuit is not null && (aCircuit.teleportFlags & (uint)Constants.TeleportFlags.ViaHGLogin) != 0)
  177. {
  178. UserAccount account = m_Scenes[0].UserAccountService.GetUserAccount(client.Scene.RegionInfo.ScopeID, client.AgentId);
  179. if (account is null) // foreign
  180. {
  181. FriendInfo[] friends = GetFriendsFromCache(client.AgentId);
  182. foreach (FriendInfo f in friends)
  183. {
  184. int rights = f.TheirFlags;
  185. if(rights != -1 )
  186. client.SendChangeUserRights(new UUID(f.Friend), client.AgentId, rights);
  187. }
  188. }
  189. }
  190. }
  191. //m_log.DebugFormat("[HGFRIENDS MODULE]: Exiting SendFriendsOnlineIfNeeded for {0}", client.Name);
  192. return false;
  193. }
  194. protected override void GetOnlineFriends(UUID userID, List<string> friendList, /*collector*/ List<UUID> online)
  195. {
  196. //m_log.DebugFormat("[HGFRIENDS MODULE]: Entering GetOnlineFriends for {0}", userID);
  197. List<string> fList = new();
  198. foreach (string s in friendList)
  199. {
  200. if (s.Length < 36)
  201. m_log.WarnFormat(
  202. "[HGFRIENDS MODULE]: Ignoring friend {0} ({1} chars) for {2} since identifier too short",
  203. s, s.Length, userID);
  204. else
  205. fList.Add(s.Substring(0, 36));
  206. }
  207. // FIXME: also query the presence status of friends in other grids (like in HGStatusNotifier.Notify())
  208. PresenceInfo[] presence = PresenceService.GetAgents(fList.ToArray());
  209. if (presence.Length == 0)
  210. return;
  211. if (!m_OnlineFriendsCache.TryGetValue(userID, out HashSet<UUID> friends))
  212. {
  213. friends = new HashSet<UUID>();
  214. m_OnlineFriendsCache[userID] = friends;
  215. }
  216. foreach (PresenceInfo pi in presence)
  217. {
  218. if (UUID.TryParse(pi.UserID, out UUID presenceID))
  219. {
  220. online.Add(presenceID);
  221. friends.Add(presenceID);
  222. }
  223. }
  224. //m_log.DebugFormat("[HGFRIENDS MODULE]: Exiting GetOnlineFriends for {0}", userID);
  225. }
  226. protected override void StatusNotify(List<FriendInfo> friendList, UUID userID, bool online)
  227. {
  228. //m_log.DebugFormat("[HGFRIENDS MODULE]: Entering StatusNotify for {0}", userID);
  229. // First, let's divide the friends on a per-domain basis
  230. List<FriendInfo> locallst = new(friendList.Count);
  231. Dictionary<string, List<FriendInfo>> friendsPerDomain = new Dictionary<string, List<FriendInfo>>();
  232. foreach (FriendInfo friend in friendList)
  233. {
  234. if (UUID.TryParse(friend.Friend, out UUID friendID))
  235. {
  236. if (LocalStatusNotification(userID, friendID, online))
  237. continue;
  238. locallst.Add(friend);
  239. }
  240. else
  241. {
  242. // it's a foreign friend
  243. if (Util.ParseUniversalUserIdentifier(friend.Friend, out friendID, out string url))
  244. {
  245. // Let's try our luck in the local sim. Who knows, maybe it's here
  246. if (LocalStatusNotification(userID, friendID, online))
  247. continue;
  248. if (!friendsPerDomain.TryGetValue(url, out List<FriendInfo> lst))
  249. {
  250. lst = new List<FriendInfo>();
  251. friendsPerDomain[url] = lst;
  252. }
  253. lst.Add(friend);
  254. }
  255. }
  256. }
  257. // For the local friends, just call the base method
  258. // Let's do this first of all
  259. if (locallst.Count > 0)
  260. base.StatusNotify(locallst, userID, online);
  261. if(friendsPerDomain.Count > 0)
  262. m_StatusNotifier.Notify(userID, friendsPerDomain, online);
  263. //m_log.DebugFormat("[HGFRIENDS MODULE]: Exiting StatusNotify for {0}", userID);
  264. }
  265. protected override bool GetAgentInfo(UUID scopeID, string fid, out UUID agentID, out string first, out string last)
  266. {
  267. first = "Unknown"; last = "UserHGGAI";
  268. if (base.GetAgentInfo(scopeID, fid, out agentID, out first, out last))
  269. return true;
  270. // fid is not a UUID...
  271. if (Util.ParseFullUniversalUserIdentifier(fid, out agentID, out string url, out string f, out string l))
  272. {
  273. if (agentID.IsNotZero())
  274. {
  275. m_uMan.AddUser(agentID, f, l, url);
  276. string name = m_uMan.GetUserName(agentID);
  277. string[] parts = name.Trim().Split();
  278. if (parts.Length == 2)
  279. {
  280. first = parts[0];
  281. last = parts[1];
  282. }
  283. else
  284. {
  285. first = f;
  286. last = l;
  287. }
  288. return true;
  289. }
  290. }
  291. return false;
  292. }
  293. protected override string GetFriendshipRequesterName(UUID agentID)
  294. {
  295. return m_uMan.GetUserName(agentID);
  296. }
  297. protected override string FriendshipMessage(string friendID)
  298. {
  299. if (UUID.TryParse(friendID, out UUID _))
  300. return base.FriendshipMessage(friendID);
  301. return "Please confirm this friendship you made while you were on another HG grid";
  302. }
  303. protected override FriendInfo GetFriend(FriendInfo[] friends, UUID friendID)
  304. {
  305. if(friends.Length > 0)
  306. {
  307. string friendIDstr = friendID.ToString();
  308. foreach (FriendInfo fi in friends)
  309. {
  310. if (fi.Friend.StartsWith(friendIDstr))
  311. return fi;
  312. }
  313. }
  314. return null;
  315. }
  316. public override FriendInfo[] GetFriendsFromService(IClientAPI client)
  317. {
  318. //m_log.DebugFormat("[HGFRIENDS MODULE]: Entering GetFriendsFromService for {0}", client.Name);
  319. bool agentIsLocal = true;
  320. if (UserManagementModule is not null)
  321. agentIsLocal = UserManagementModule.IsLocalGridUser(client.AgentId);
  322. if (agentIsLocal)
  323. return base.GetFriendsFromService(client);
  324. // Foreigner
  325. AgentCircuitData agentClientCircuit = ((Scene)(client.Scene)).AuthenticateHandler.GetAgentCircuitData(client.CircuitCode);
  326. if (agentClientCircuit is not null)
  327. {
  328. // Note that this is calling a different interface than base; this one calls with a string param!
  329. FriendInfo[] finfos = FriendsService.GetFriends(client.AgentId.ToString());
  330. m_log.DebugFormat("[HGFRIENDS MODULE]: Fetched {0} local friends for visitor {1}", finfos.Length, client.AgentId.ToString());
  331. return finfos;
  332. }
  333. else
  334. return Array.Empty<FriendInfo>();
  335. }
  336. protected override bool StoreRights(UUID agentID, UUID friendID, int rights)
  337. {
  338. bool agentIsLocal = true;
  339. bool friendIsLocal = true;
  340. if (UserManagementModule != null)
  341. {
  342. agentIsLocal = UserManagementModule.IsLocalGridUser(agentID);
  343. friendIsLocal = UserManagementModule.IsLocalGridUser(friendID);
  344. }
  345. // Are they both local users?
  346. if (agentIsLocal && friendIsLocal)
  347. {
  348. // local grid users
  349. return base.StoreRights(agentID, friendID, rights);
  350. }
  351. if (agentIsLocal) // agent is local, friend is foreigner
  352. {
  353. FriendInfo[] finfos = GetFriendsFromCache(agentID);
  354. FriendInfo finfo = GetFriend(finfos, friendID);
  355. if (finfo != null)
  356. {
  357. FriendsService.StoreFriend(agentID.ToString(), finfo.Friend, rights);
  358. return true;
  359. }
  360. }
  361. if (friendIsLocal) // agent is foreigner, friend is local
  362. {
  363. string agentUUI = GetUUI(friendID, agentID);
  364. if (agentUUI != string.Empty)
  365. {
  366. FriendsService.StoreFriend(agentUUI, friendID.ToString(), rights);
  367. return true;
  368. }
  369. }
  370. return false;
  371. }
  372. protected override void StoreBackwards(UUID friendID, UUID agentID)
  373. {
  374. bool agentIsLocal = true;
  375. //bool friendIsLocal = true;
  376. if (UserManagementModule != null)
  377. {
  378. agentIsLocal = UserManagementModule.IsLocalGridUser(agentID);
  379. //friendIsLocal = UserManagementModule.IsLocalGridUser(friendID);
  380. }
  381. // Is the requester a local user?
  382. if (agentIsLocal)
  383. {
  384. // local grid users
  385. m_log.DebugFormat("[HGFRIENDS MODULE]: Friendship requester is local. Storing backwards.");
  386. base.StoreBackwards(friendID, agentID);
  387. return;
  388. }
  389. // no provision for this temporary friendship state when user is not local
  390. //FriendsService.StoreFriend(friendID.ToString(), agentID.ToString(), 0);
  391. }
  392. protected override void StoreFriendships(UUID agentID, UUID friendID)
  393. {
  394. bool agentIsLocal = true;
  395. bool friendIsLocal = true;
  396. if (UserManagementModule != null)
  397. {
  398. agentIsLocal = UserManagementModule.IsLocalGridUser(agentID);
  399. friendIsLocal = UserManagementModule.IsLocalGridUser(friendID);
  400. }
  401. // Are they both local users?
  402. if (agentIsLocal && friendIsLocal)
  403. {
  404. // local grid users
  405. m_log.DebugFormat("[HGFRIENDS MODULE]: Users are both local");
  406. DeletePreviousHGRelations(agentID, friendID);
  407. base.StoreFriendships(agentID, friendID);
  408. return;
  409. }
  410. // ok, at least one of them is foreigner, let's get their data
  411. IClientAPI agentClient = LocateClientObject(agentID);
  412. IClientAPI friendClient = LocateClientObject(friendID);
  413. AgentCircuitData agentClientCircuit = null;
  414. AgentCircuitData friendClientCircuit = null;
  415. string agentUUI = string.Empty;
  416. string friendUUI = string.Empty;
  417. string agentFriendService = string.Empty;
  418. string friendFriendService = string.Empty;
  419. if (agentClient is not null)
  420. {
  421. agentClientCircuit = ((Scene)(agentClient.Scene)).AuthenticateHandler.GetAgentCircuitData(agentClient.CircuitCode);
  422. agentUUI = Util.ProduceUserUniversalIdentifier(agentClientCircuit);
  423. agentFriendService = agentClientCircuit.ServiceURLs["FriendsServerURI"].ToString();
  424. RecacheFriends(agentClient);
  425. }
  426. if (friendClient is not null)
  427. {
  428. friendClientCircuit = ((Scene)(friendClient.Scene)).AuthenticateHandler.GetAgentCircuitData(friendClient.CircuitCode);
  429. friendUUI = Util.ProduceUserUniversalIdentifier(friendClientCircuit);
  430. friendFriendService = friendClientCircuit.ServiceURLs["FriendsServerURI"].ToString();
  431. RecacheFriends(friendClient);
  432. }
  433. m_log.DebugFormat("[HGFRIENDS MODULE] HG Friendship! thisUUI={0}; friendUUI={1}; foreignThisFriendService={2}; foreignFriendFriendService={3}",
  434. agentUUI, friendUUI, agentFriendService, friendFriendService);
  435. // Generate a random 8-character hex number that will sign this friendship
  436. string secret = UUID.Random().ToString().Substring(0, 8);
  437. string theFriendUUID = friendUUI + ";" + secret;
  438. string agentUUID = agentUUI + ";" + secret;
  439. if (agentIsLocal) // agent is local, 'friend' is foreigner
  440. {
  441. // This may happen when the agent returned home, in which case the friend is not there
  442. // We need to look for its information in the friends list itself
  443. FriendInfo[] finfos = null;
  444. bool confirming = false;
  445. if (friendUUI.Length == 0)
  446. {
  447. finfos = GetFriendsFromCache(agentID);
  448. foreach (FriendInfo finfo in finfos)
  449. {
  450. if (finfo.TheirFlags == -1)
  451. {
  452. if (finfo.Friend.StartsWith(friendID.ToString()))
  453. {
  454. friendUUI = finfo.Friend;
  455. theFriendUUID = friendUUI;
  456. // If it's confirming the friendship, we already have the full UUI with the secret
  457. if (Util.ParseFullUniversalUserIdentifier(theFriendUUID, out UUID utmp, out string url,
  458. out string first, out string last))
  459. {
  460. agentUUID = agentUUI + ";" + secret;
  461. m_uMan.AddUser(utmp, first, last, url);
  462. }
  463. confirming = true;
  464. break;
  465. }
  466. }
  467. }
  468. if (!confirming)
  469. {
  470. friendUUI = m_uMan.GetUserUUI(friendID);
  471. theFriendUUID = friendUUI + ";" + secret;
  472. }
  473. friendFriendService = m_uMan.GetUserServerURL(friendID, "FriendsServerURI");
  474. //m_log.DebugFormat("[HGFRIENDS MODULE] HG Friendship! thisUUI={0}; friendUUI={1}; foreignThisFriendService={2}; foreignFriendFriendService={3}",
  475. // agentUUI, friendUUI, agentFriendService, friendFriendService);
  476. }
  477. // Delete any previous friendship relations
  478. DeletePreviousRelations(agentID, friendID);
  479. // store in the local friends service a reference to the foreign friend
  480. FriendsService.StoreFriend(agentID.ToString(), theFriendUUID, 1);
  481. // and also the converse
  482. FriendsService.StoreFriend(theFriendUUID, agentID.ToString(), 1);
  483. //if (!confirming)
  484. //{
  485. // store in the foreign friends service a reference to the local agent
  486. HGFriendsServicesConnector friendsConn = null;
  487. if (friendClientCircuit != null) // the friend is here, validate session
  488. friendsConn = new HGFriendsServicesConnector(friendFriendService, friendClientCircuit.SessionID, friendClientCircuit.ServiceSessionID);
  489. else // the friend is not here, he initiated the request in his home world
  490. friendsConn = new HGFriendsServicesConnector(friendFriendService);
  491. friendsConn.NewFriendship(friendID, agentUUID);
  492. //}
  493. }
  494. else if (friendIsLocal) // 'friend' is local, agent is foreigner
  495. {
  496. // Delete any previous friendship relations
  497. DeletePreviousRelations(agentID, friendID);
  498. // store in the local friends service a reference to the foreign agent
  499. FriendsService.StoreFriend(friendID.ToString(), agentUUI + ";" + secret, 1);
  500. // and also the converse
  501. FriendsService.StoreFriend(agentUUI + ";" + secret, friendID.ToString(), 1);
  502. if (agentClientCircuit is not null)
  503. {
  504. // store in the foreign friends service a reference to the local agent
  505. HGFriendsServicesConnector friendsConn = new HGFriendsServicesConnector(agentFriendService, agentClientCircuit.SessionID, agentClientCircuit.ServiceSessionID);
  506. friendsConn.NewFriendship(agentID, friendUUI + ";" + secret);
  507. }
  508. }
  509. else // They're both foreigners!
  510. {
  511. HGFriendsServicesConnector friendsConn;
  512. if (agentClientCircuit is not null)
  513. {
  514. friendsConn = new HGFriendsServicesConnector(agentFriendService, agentClientCircuit.SessionID, agentClientCircuit.ServiceSessionID);
  515. friendsConn.NewFriendship(agentID, friendUUI + ";" + secret);
  516. }
  517. if (friendClientCircuit is not null)
  518. {
  519. friendsConn = new HGFriendsServicesConnector(friendFriendService, friendClientCircuit.SessionID, friendClientCircuit.ServiceSessionID);
  520. friendsConn.NewFriendship(friendID, agentUUI + ";" + secret);
  521. }
  522. }
  523. // my brain hurts now
  524. }
  525. private void DeletePreviousRelations(UUID a1, UUID a2)
  526. {
  527. // Delete any previous friendship relations
  528. FriendInfo f;
  529. FriendInfo[] finfos = GetFriendsFromCache(a1);
  530. if (finfos is not null)
  531. {
  532. f = GetFriend(finfos, a2);
  533. if (f is not null)
  534. {
  535. FriendsService.Delete(a1, f.Friend);
  536. // and also the converse
  537. FriendsService.Delete(f.Friend, a1.ToString());
  538. }
  539. }
  540. finfos = GetFriendsFromCache(a2);
  541. if (finfos is not null)
  542. {
  543. f = GetFriend(finfos, a1);
  544. if (f is not null)
  545. {
  546. FriendsService.Delete(a2, f.Friend);
  547. // and also the converse
  548. FriendsService.Delete(f.Friend, a2.ToString());
  549. }
  550. }
  551. }
  552. private void DeletePreviousHGRelations(UUID a1, UUID a2)
  553. {
  554. // Delete any previous friendship relations
  555. FriendInfo[] finfos = GetFriendsFromCache(a1);
  556. if (finfos is not null)
  557. {
  558. string a1str = a1.ToString();
  559. string a2str = a2.ToString();
  560. foreach (FriendInfo f in finfos)
  561. {
  562. if (f.TheirFlags == -1)
  563. {
  564. if (f.Friend.StartsWith(a2str))
  565. {
  566. FriendsService.Delete(a1, f.Friend);
  567. // and also the converse
  568. FriendsService.Delete(f.Friend, a1str);
  569. }
  570. }
  571. }
  572. }
  573. finfos = GetFriendsFromCache(a1);
  574. if (finfos is not null)
  575. {
  576. string a1str2 = a1.ToString();
  577. string a2str2 = a2.ToString();
  578. foreach (FriendInfo f in finfos)
  579. {
  580. if (f.TheirFlags == -1)
  581. {
  582. if (f.Friend.StartsWith(a1str2))
  583. {
  584. FriendsService.Delete(a2, f.Friend);
  585. // and also the converse
  586. FriendsService.Delete(f.Friend, a2str2);
  587. }
  588. }
  589. }
  590. }
  591. }
  592. protected override bool DeleteFriendship(UUID agentID, UUID exfriendID)
  593. {
  594. Boolean agentIsLocal = true;
  595. Boolean friendIsLocal = true;
  596. if (UserManagementModule != null)
  597. {
  598. agentIsLocal = UserManagementModule.IsLocalGridUser(agentID);
  599. friendIsLocal = UserManagementModule.IsLocalGridUser(exfriendID);
  600. }
  601. // Are they both local users?
  602. if (agentIsLocal && friendIsLocal)
  603. {
  604. // local grid users
  605. return base.DeleteFriendship(agentID, exfriendID);
  606. }
  607. // ok, at least one of them is foreigner, let's get their data
  608. string agentUUI = string.Empty;
  609. string friendUUI = string.Empty;
  610. if (agentIsLocal) // agent is local, 'friend' is foreigner
  611. {
  612. // We need to look for its information in the friends list itself
  613. FriendInfo[] finfos = GetFriendsFromCache(agentID);
  614. FriendInfo finfo = GetFriend(finfos, exfriendID);
  615. if (finfo != null)
  616. {
  617. friendUUI = finfo.Friend;
  618. // delete in the local friends service the reference to the foreign friend
  619. FriendsService.Delete(agentID, friendUUI);
  620. // and also the converse
  621. FriendsService.Delete(friendUUI, agentID.ToString());
  622. // notify the exfriend's service
  623. Util.FireAndForget(
  624. delegate { Delete(exfriendID, agentID, friendUUI); }, null, "HGFriendsModule.DeleteFriendshipForeignFriend");
  625. m_log.DebugFormat("[HGFRIENDS MODULE]: {0} terminated {1}", agentID, friendUUI);
  626. return true;
  627. }
  628. }
  629. else if (friendIsLocal) // agent is foreigner, 'friend' is local
  630. {
  631. agentUUI = GetUUI(exfriendID, agentID);
  632. if (agentUUI != string.Empty)
  633. {
  634. // delete in the local friends service the reference to the foreign agent
  635. FriendsService.Delete(exfriendID, agentUUI);
  636. // and also the converse
  637. FriendsService.Delete(agentUUI, exfriendID.ToString());
  638. // notify the agent's service?
  639. Util.FireAndForget(
  640. delegate { Delete(agentID, exfriendID, agentUUI); }, null, "HGFriendsModule.DeleteFriendshipLocalFriend");
  641. m_log.DebugFormat("[HGFRIENDS MODULE]: {0} terminated {1}", agentUUI, exfriendID);
  642. return true;
  643. }
  644. }
  645. //else They're both foreigners! Can't handle this
  646. return false;
  647. }
  648. private string GetUUI(UUID localUser, UUID foreignUser)
  649. {
  650. // Let's see if the user is here by any chance
  651. FriendInfo[] finfos = GetFriendsFromCache(localUser);
  652. if (finfos != EMPTY_FRIENDS) // friend is here, cool
  653. {
  654. FriendInfo finfo = GetFriend(finfos, foreignUser);
  655. if (finfo != null)
  656. {
  657. return finfo.Friend;
  658. }
  659. }
  660. else // user is not currently on this sim, need to get from the service
  661. {
  662. finfos = FriendsService.GetFriends(localUser);
  663. foreach (FriendInfo finfo in finfos)
  664. {
  665. if (finfo.Friend.StartsWith(foreignUser.ToString())) // found it!
  666. {
  667. return finfo.Friend;
  668. }
  669. }
  670. }
  671. return string.Empty;
  672. }
  673. private void Delete(UUID foreignUser, UUID localUser, string uui)
  674. {
  675. if (Util.ParseFullUniversalUserIdentifier(uui, out UUID _, out string _, out string _, out string url, out string secret))
  676. {
  677. m_log.DebugFormat("[HGFRIENDS MODULE]: Deleting friendship from {0}", url);
  678. HGFriendsServicesConnector friendConn = new HGFriendsServicesConnector(url);
  679. friendConn.DeleteFriendship(foreignUser, localUser, secret);
  680. }
  681. }
  682. protected override bool ForwardFriendshipOffer(UUID agentID, UUID friendID, GridInstantMessage im)
  683. {
  684. if (base.ForwardFriendshipOffer(agentID, friendID, im))
  685. return true;
  686. // OK, that didn't work, so let's try to find this user somewhere
  687. if (!m_uMan.IsLocalGridUser(friendID))
  688. {
  689. string friendsURL = m_uMan.GetUserServerURL(friendID, "FriendsServerURI");
  690. if (!string.IsNullOrEmpty(friendsURL))
  691. {
  692. m_log.DebugFormat("[HGFRIENDS MODULE]: Forwading friendship from {0} to {1} @ {2}", agentID, friendID, friendsURL);
  693. GridRegion region = new GridRegion();
  694. region.ServerURI = friendsURL;
  695. string name = im.fromAgentName;
  696. if (m_uMan.IsLocalGridUser(agentID))
  697. {
  698. IClientAPI agentClient = LocateClientObject(agentID);
  699. AgentCircuitData agentClientCircuit = ((Scene)(agentClient.Scene)).AuthenticateHandler.GetAgentCircuitData(agentClient.CircuitCode);
  700. string agentHomeService = string.Empty;
  701. try
  702. {
  703. agentHomeService = agentClientCircuit.ServiceURLs["HomeURI"].ToString();
  704. string lastname = "@" + new Uri(agentHomeService).Authority;
  705. string firstname = im.fromAgentName.Replace(" ", ".");
  706. name = firstname + lastname;
  707. }
  708. catch (KeyNotFoundException)
  709. {
  710. m_log.DebugFormat("[HGFRIENDS MODULE]: Key HomeURI not found for user {0}", agentID);
  711. return false;
  712. }
  713. catch (NullReferenceException)
  714. {
  715. m_log.DebugFormat("[HGFRIENDS MODULE]: Null HomeUri for local user {0}", agentID);
  716. return false;
  717. }
  718. catch (UriFormatException)
  719. {
  720. m_log.DebugFormat("[HGFRIENDS MODULE]: Malformed HomeUri {0} for local user {1}", agentHomeService, agentID);
  721. return false;
  722. }
  723. }
  724. m_HGFriendsConnector.FriendshipOffered(region, agentID, friendID, im.message, name);
  725. return true;
  726. }
  727. }
  728. return false;
  729. }
  730. public override bool LocalFriendshipOffered(UUID toID, GridInstantMessage im)
  731. {
  732. if (base.LocalFriendshipOffered(toID, im))
  733. {
  734. if (im.fromAgentName.Contains("@"))
  735. {
  736. string[] parts = im.fromAgentName.Split(new char[] { '@' });
  737. if (parts.Length == 2)
  738. {
  739. string[] fl = parts[0].Trim().Split(Util.SplitDotArray);
  740. if (fl.Length == 2)
  741. m_uMan.AddUser(new UUID(im.fromAgentID), fl[0], fl[1], "http://" + parts[1]);
  742. else
  743. m_uMan.AddUser(new UUID(im.fromAgentID), fl[0], "", "http://" + parts[1]);
  744. }
  745. }
  746. return true;
  747. }
  748. return false;
  749. }
  750. }
  751. }