HGFriendsModule.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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. private int m_levelHGFriends = 0;
  51. IUserManagement m_uMan;
  52. public IUserManagement UserManagementModule
  53. {
  54. get
  55. {
  56. if (m_uMan == null)
  57. m_uMan = m_Scenes[0].RequestModuleInterface<IUserManagement>();
  58. return m_uMan;
  59. }
  60. }
  61. protected HGFriendsServicesConnector m_HGFriendsConnector = new HGFriendsServicesConnector();
  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 UUID(im.fromAgentID);
  114. UUID friendID = new UUID(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. ScenePresence avatar = null;
  120. ((Scene)client.Scene).TryGetScenePresence(principalID, out avatar);
  121. if (avatar == null)
  122. return;
  123. if (avatar.UserLevel < m_levelHGFriends)
  124. {
  125. client.SendAgentAlertMessage("Unable to send friendship invitation to foreigner. Insufficient permissions.", false);
  126. return;
  127. }
  128. }
  129. }
  130. base.OnInstantMessage(client, im);
  131. }
  132. protected override void OnApproveFriendRequest(IClientAPI client, UUID friendID, List<UUID> callingCardFolders)
  133. {
  134. // Update the local cache. Yes, we need to do it right here
  135. // because the HGFriendsService placed something on the DB
  136. // from under the sim
  137. base.OnApproveFriendRequest(client, friendID, callingCardFolders);
  138. }
  139. protected override bool CacheFriends(IClientAPI client)
  140. {
  141. // m_log.DebugFormat("[HGFRIENDS MODULE]: Entered CacheFriends for {0}", client.Name);
  142. if (base.CacheFriends(client))
  143. {
  144. UUID agentID = client.AgentId;
  145. // we do this only for the root agent
  146. if (m_Friends[agentID].Refcount == 1)
  147. {
  148. // We need to preload the user management cache with the names
  149. // of foreign friends, just like we do with SOPs' creators
  150. foreach (FriendInfo finfo in m_Friends[agentID].Friends)
  151. {
  152. if (finfo.TheirFlags != -1)
  153. {
  154. UUID id;
  155. if (!UUID.TryParse(finfo.Friend, out id))
  156. {
  157. string url = string.Empty, first = string.Empty, last = string.Empty, tmp = string.Empty;
  158. if (Util.ParseUniversalUserIdentifier(finfo.Friend, out id, out url, out first, out last, out tmp))
  159. {
  160. IUserManagement uMan = m_Scenes[0].RequestModuleInterface<IUserManagement>();
  161. uMan.AddUser(id, url + ";" + first + " " + last);
  162. }
  163. }
  164. }
  165. }
  166. // m_log.DebugFormat("[HGFRIENDS MODULE]: Exiting CacheFriends for {0} since detected root agent", client.Name);
  167. return true;
  168. }
  169. }
  170. // m_log.DebugFormat("[HGFRIENDS MODULE]: Exiting CacheFriends for {0} since detected not root agent", client.Name);
  171. return false;
  172. }
  173. public override bool SendFriendsOnlineIfNeeded(IClientAPI client)
  174. {
  175. // m_log.DebugFormat("[HGFRIENDS MODULE]: Entering SendFriendsOnlineIfNeeded for {0}", client.Name);
  176. if (base.SendFriendsOnlineIfNeeded(client))
  177. {
  178. AgentCircuitData aCircuit = ((Scene)client.Scene).AuthenticateHandler.GetAgentCircuitData(client.AgentId);
  179. if (aCircuit != null && (aCircuit.teleportFlags & (uint)Constants.TeleportFlags.ViaHGLogin) != 0)
  180. {
  181. UserAccount account = m_Scenes[0].UserAccountService.GetUserAccount(client.Scene.RegionInfo.ScopeID, client.AgentId);
  182. if (account == null) // foreign
  183. {
  184. FriendInfo[] friends = GetFriendsFromCache(client.AgentId);
  185. foreach (FriendInfo f in friends)
  186. {
  187. client.SendChangeUserRights(new UUID(f.Friend), client.AgentId, f.TheirFlags);
  188. }
  189. }
  190. }
  191. }
  192. // m_log.DebugFormat("[HGFRIENDS MODULE]: Exiting SendFriendsOnlineIfNeeded for {0}", client.Name);
  193. return false;
  194. }
  195. protected override void GetOnlineFriends(UUID userID, List<string> friendList, /*collector*/ List<UUID> online)
  196. {
  197. // m_log.DebugFormat("[HGFRIENDS MODULE]: Entering GetOnlineFriends for {0}", userID);
  198. List<string> fList = new List<string>();
  199. foreach (string s in friendList)
  200. {
  201. if (s.Length < 36)
  202. m_log.WarnFormat(
  203. "[HGFRIENDS MODULE]: Ignoring friend {0} ({1} chars) for {2} since identifier too short",
  204. s, s.Length, userID);
  205. else
  206. fList.Add(s.Substring(0, 36));
  207. }
  208. PresenceInfo[] presence = PresenceService.GetAgents(fList.ToArray());
  209. foreach (PresenceInfo pi in presence)
  210. {
  211. UUID presenceID;
  212. if (UUID.TryParse(pi.UserID, out presenceID))
  213. online.Add(presenceID);
  214. }
  215. // m_log.DebugFormat("[HGFRIENDS MODULE]: Exiting GetOnlineFriends for {0}", userID);
  216. }
  217. protected override void StatusNotify(List<FriendInfo> friendList, UUID userID, bool online)
  218. {
  219. // m_log.DebugFormat("[HGFRIENDS MODULE]: Entering StatusNotify for {0}", userID);
  220. // First, let's divide the friends on a per-domain basis
  221. Dictionary<string, List<FriendInfo>> friendsPerDomain = new Dictionary<string, List<FriendInfo>>();
  222. foreach (FriendInfo friend in friendList)
  223. {
  224. UUID friendID;
  225. if (UUID.TryParse(friend.Friend, out friendID))
  226. {
  227. if (!friendsPerDomain.ContainsKey("local"))
  228. friendsPerDomain["local"] = new List<FriendInfo>();
  229. friendsPerDomain["local"].Add(friend);
  230. }
  231. else
  232. {
  233. // it's a foreign friend
  234. string url = string.Empty, tmp = string.Empty;
  235. if (Util.ParseUniversalUserIdentifier(friend.Friend, out friendID, out url, out tmp, out tmp, out tmp))
  236. {
  237. // Let's try our luck in the local sim. Who knows, maybe it's here
  238. if (LocalStatusNotification(userID, friendID, online))
  239. continue;
  240. if (!friendsPerDomain.ContainsKey(url))
  241. friendsPerDomain[url] = new List<FriendInfo>();
  242. friendsPerDomain[url].Add(friend);
  243. }
  244. }
  245. }
  246. // For the local friends, just call the base method
  247. // Let's do this first of all
  248. if (friendsPerDomain.ContainsKey("local"))
  249. base.StatusNotify(friendsPerDomain["local"], userID, online);
  250. m_StatusNotifier.Notify(userID, friendsPerDomain, online);
  251. // m_log.DebugFormat("[HGFRIENDS MODULE]: Exiting StatusNotify for {0}", userID);
  252. }
  253. protected override bool GetAgentInfo(UUID scopeID, string fid, out UUID agentID, out string first, out string last)
  254. {
  255. first = "Unknown"; last = "User";
  256. if (base.GetAgentInfo(scopeID, fid, out agentID, out first, out last))
  257. return true;
  258. // fid is not a UUID...
  259. string url = string.Empty, tmp = string.Empty, f = string.Empty, l = string.Empty;
  260. if (Util.ParseUniversalUserIdentifier(fid, out agentID, out url, out f, out l, out tmp))
  261. {
  262. if (!agentID.Equals(UUID.Zero))
  263. {
  264. m_uMan.AddUser(agentID, f, l, url);
  265. string name = m_uMan.GetUserName(agentID);
  266. string[] parts = name.Trim().Split(new char[] { ' ' });
  267. if (parts.Length == 2)
  268. {
  269. first = parts[0];
  270. last = parts[1];
  271. }
  272. else
  273. {
  274. first = f;
  275. last = l;
  276. }
  277. return true;
  278. }
  279. }
  280. return false;
  281. }
  282. protected override string GetFriendshipRequesterName(UUID agentID)
  283. {
  284. return m_uMan.GetUserName(agentID);
  285. }
  286. protected override string FriendshipMessage(string friendID)
  287. {
  288. UUID id;
  289. if (UUID.TryParse(friendID, out id))
  290. return base.FriendshipMessage(friendID);
  291. return "Please confirm this friendship you made while you were away.";
  292. }
  293. protected override FriendInfo GetFriend(FriendInfo[] friends, UUID friendID)
  294. {
  295. foreach (FriendInfo fi in friends)
  296. {
  297. if (fi.Friend.StartsWith(friendID.ToString()))
  298. return fi;
  299. }
  300. return null;
  301. }
  302. public override FriendInfo[] GetFriendsFromService(IClientAPI client)
  303. {
  304. // m_log.DebugFormat("[HGFRIENDS MODULE]: Entering GetFriendsFromService for {0}", client.Name);
  305. Boolean agentIsLocal = true;
  306. if (UserManagementModule != null)
  307. agentIsLocal = UserManagementModule.IsLocalGridUser(client.AgentId);
  308. if (agentIsLocal)
  309. return base.GetFriendsFromService(client);
  310. FriendInfo[] finfos = new FriendInfo[0];
  311. // Foreigner
  312. AgentCircuitData agentClientCircuit = ((Scene)(client.Scene)).AuthenticateHandler.GetAgentCircuitData(client.CircuitCode);
  313. if (agentClientCircuit != null)
  314. {
  315. //[XXX] string agentUUI = Util.ProduceUserUniversalIdentifier(agentClientCircuit);
  316. finfos = FriendsService.GetFriends(client.AgentId.ToString());
  317. m_log.DebugFormat("[HGFRIENDS MODULE]: Fetched {0} local friends for visitor {1}", finfos.Length, client.AgentId.ToString());
  318. }
  319. // m_log.DebugFormat("[HGFRIENDS MODULE]: Exiting GetFriendsFromService for {0}", client.Name);
  320. return finfos;
  321. }
  322. protected override bool StoreRights(UUID agentID, UUID friendID, int rights)
  323. {
  324. Boolean agentIsLocal = true;
  325. Boolean friendIsLocal = true;
  326. if (UserManagementModule != null)
  327. {
  328. agentIsLocal = UserManagementModule.IsLocalGridUser(agentID);
  329. friendIsLocal = UserManagementModule.IsLocalGridUser(friendID);
  330. }
  331. // Are they both local users?
  332. if (agentIsLocal && friendIsLocal)
  333. {
  334. // local grid users
  335. return base.StoreRights(agentID, friendID, rights);
  336. }
  337. if (agentIsLocal) // agent is local, friend is foreigner
  338. {
  339. FriendInfo[] finfos = GetFriendsFromCache(agentID);
  340. FriendInfo finfo = GetFriend(finfos, friendID);
  341. if (finfo != null)
  342. {
  343. FriendsService.StoreFriend(agentID.ToString(), finfo.Friend, rights);
  344. return true;
  345. }
  346. }
  347. if (friendIsLocal) // agent is foreigner, friend is local
  348. {
  349. string agentUUI = GetUUI(friendID, agentID);
  350. if (agentUUI != string.Empty)
  351. {
  352. FriendsService.StoreFriend(agentUUI, friendID.ToString(), rights);
  353. return true;
  354. }
  355. }
  356. return false;
  357. }
  358. protected override void StoreBackwards(UUID friendID, UUID agentID)
  359. {
  360. bool agentIsLocal = true;
  361. // bool friendIsLocal = true;
  362. if (UserManagementModule != null)
  363. {
  364. agentIsLocal = UserManagementModule.IsLocalGridUser(agentID);
  365. // friendIsLocal = UserManagementModule.IsLocalGridUser(friendID);
  366. }
  367. // Is the requester a local user?
  368. if (agentIsLocal)
  369. {
  370. // local grid users
  371. m_log.DebugFormat("[HGFRIENDS MODULE]: Friendship requester is local. Storing backwards.");
  372. base.StoreBackwards(friendID, agentID);
  373. return;
  374. }
  375. // no provision for this temporary friendship state when user is not local
  376. //FriendsService.StoreFriend(friendID.ToString(), agentID.ToString(), 0);
  377. }
  378. protected override void StoreFriendships(UUID agentID, UUID friendID)
  379. {
  380. Boolean agentIsLocal = true;
  381. Boolean friendIsLocal = true;
  382. if (UserManagementModule != null)
  383. {
  384. agentIsLocal = UserManagementModule.IsLocalGridUser(agentID);
  385. friendIsLocal = UserManagementModule.IsLocalGridUser(friendID);
  386. }
  387. // Are they both local users?
  388. if (agentIsLocal && friendIsLocal)
  389. {
  390. // local grid users
  391. m_log.DebugFormat("[HGFRIENDS MODULE]: Users are both local");
  392. base.StoreFriendships(agentID, friendID);
  393. return;
  394. }
  395. // ok, at least one of them is foreigner, let's get their data
  396. IClientAPI agentClient = LocateClientObject(agentID);
  397. IClientAPI friendClient = LocateClientObject(friendID);
  398. AgentCircuitData agentClientCircuit = null;
  399. AgentCircuitData friendClientCircuit = null;
  400. string agentUUI = string.Empty;
  401. string friendUUI = string.Empty;
  402. string agentFriendService = string.Empty;
  403. string friendFriendService = string.Empty;
  404. if (agentClient != null)
  405. {
  406. agentClientCircuit = ((Scene)(agentClient.Scene)).AuthenticateHandler.GetAgentCircuitData(agentClient.CircuitCode);
  407. agentUUI = Util.ProduceUserUniversalIdentifier(agentClientCircuit);
  408. agentFriendService = agentClientCircuit.ServiceURLs["FriendsServerURI"].ToString();
  409. RecacheFriends(agentClient);
  410. }
  411. if (friendClient != null)
  412. {
  413. friendClientCircuit = ((Scene)(friendClient.Scene)).AuthenticateHandler.GetAgentCircuitData(friendClient.CircuitCode);
  414. friendUUI = Util.ProduceUserUniversalIdentifier(friendClientCircuit);
  415. friendFriendService = friendClientCircuit.ServiceURLs["FriendsServerURI"].ToString();
  416. RecacheFriends(friendClient);
  417. }
  418. m_log.DebugFormat("[HGFRIENDS MODULE] HG Friendship! thisUUI={0}; friendUUI={1}; foreignThisFriendService={2}; foreignFriendFriendService={3}",
  419. agentUUI, friendUUI, agentFriendService, friendFriendService);
  420. // Generate a random 8-character hex number that will sign this friendship
  421. string secret = UUID.Random().ToString().Substring(0, 8);
  422. string theFriendUUID = friendUUI + ";" + secret;
  423. string agentUUID = agentUUI + ";" + secret;
  424. if (agentIsLocal) // agent is local, 'friend' is foreigner
  425. {
  426. // This may happen when the agent returned home, in which case the friend is not there
  427. // We need to look for its information in the friends list itself
  428. FriendInfo[] finfos = null;
  429. bool confirming = false;
  430. if (friendUUI == string.Empty)
  431. {
  432. finfos = GetFriendsFromCache(agentID);
  433. foreach (FriendInfo finfo in finfos)
  434. {
  435. if (finfo.TheirFlags == -1)
  436. {
  437. if (finfo.Friend.StartsWith(friendID.ToString()))
  438. {
  439. friendUUI = finfo.Friend;
  440. theFriendUUID = friendUUI;
  441. UUID utmp = UUID.Zero;
  442. string url = String.Empty;
  443. string first = String.Empty;
  444. string last = String.Empty;
  445. // If it's confirming the friendship, we already have the full UUI with the secret
  446. if (Util.ParseUniversalUserIdentifier(theFriendUUID, out utmp, out url, out first, out last, out secret))
  447. {
  448. agentUUID = agentUUI + ";" + secret;
  449. m_uMan.AddUser(utmp, first, last, url);
  450. }
  451. confirming = true;
  452. break;
  453. }
  454. }
  455. }
  456. if (!confirming)
  457. {
  458. friendUUI = m_uMan.GetUserUUI(friendID);
  459. theFriendUUID = friendUUI + ";" + secret;
  460. }
  461. friendFriendService = m_uMan.GetUserServerURL(friendID, "FriendsServerURI");
  462. // m_log.DebugFormat("[HGFRIENDS MODULE] HG Friendship! thisUUI={0}; friendUUI={1}; foreignThisFriendService={2}; foreignFriendFriendService={3}",
  463. // agentUUI, friendUUI, agentFriendService, friendFriendService);
  464. }
  465. // Delete any previous friendship relations
  466. DeletePreviousRelations(agentID, friendID);
  467. // store in the local friends service a reference to the foreign friend
  468. FriendsService.StoreFriend(agentID.ToString(), theFriendUUID, 1);
  469. // and also the converse
  470. FriendsService.StoreFriend(theFriendUUID, agentID.ToString(), 1);
  471. //if (!confirming)
  472. //{
  473. // store in the foreign friends service a reference to the local agent
  474. HGFriendsServicesConnector friendsConn = null;
  475. if (friendClientCircuit != null) // the friend is here, validate session
  476. friendsConn = new HGFriendsServicesConnector(friendFriendService, friendClientCircuit.SessionID, friendClientCircuit.ServiceSessionID);
  477. else // the friend is not here, he initiated the request in his home world
  478. friendsConn = new HGFriendsServicesConnector(friendFriendService);
  479. friendsConn.NewFriendship(friendID, agentUUID);
  480. //}
  481. }
  482. else if (friendIsLocal) // 'friend' is local, agent is foreigner
  483. {
  484. // Delete any previous friendship relations
  485. DeletePreviousRelations(agentID, friendID);
  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. private void DeletePreviousRelations(UUID a1, UUID a2)
  514. {
  515. // Delete any previous friendship relations
  516. FriendInfo[] finfos = null;
  517. FriendInfo f = null;
  518. finfos = GetFriendsFromCache(a1);
  519. if (finfos != null)
  520. {
  521. f = GetFriend(finfos, a2);
  522. if (f != null)
  523. {
  524. FriendsService.Delete(a1, f.Friend);
  525. // and also the converse
  526. FriendsService.Delete(f.Friend, a1.ToString());
  527. }
  528. }
  529. finfos = GetFriendsFromCache(a2);
  530. if (finfos != null)
  531. {
  532. f = GetFriend(finfos, a1);
  533. if (f != null)
  534. {
  535. FriendsService.Delete(a2, f.Friend);
  536. // and also the converse
  537. FriendsService.Delete(f.Friend, a2.ToString());
  538. }
  539. }
  540. }
  541. protected override bool DeleteFriendship(UUID agentID, UUID exfriendID)
  542. {
  543. Boolean agentIsLocal = true;
  544. Boolean friendIsLocal = true;
  545. if (UserManagementModule != null)
  546. {
  547. agentIsLocal = UserManagementModule.IsLocalGridUser(agentID);
  548. friendIsLocal = UserManagementModule.IsLocalGridUser(exfriendID);
  549. }
  550. // Are they both local users?
  551. if (agentIsLocal && friendIsLocal)
  552. {
  553. // local grid users
  554. return base.DeleteFriendship(agentID, exfriendID);
  555. }
  556. // ok, at least one of them is foreigner, let's get their data
  557. string agentUUI = string.Empty;
  558. string friendUUI = string.Empty;
  559. if (agentIsLocal) // agent is local, 'friend' is foreigner
  560. {
  561. // We need to look for its information in the friends list itself
  562. FriendInfo[] finfos = GetFriendsFromCache(agentID);
  563. FriendInfo finfo = GetFriend(finfos, exfriendID);
  564. if (finfo != null)
  565. {
  566. friendUUI = finfo.Friend;
  567. // delete in the local friends service the reference to the foreign friend
  568. FriendsService.Delete(agentID, friendUUI);
  569. // and also the converse
  570. FriendsService.Delete(friendUUI, agentID.ToString());
  571. // notify the exfriend's service
  572. Util.FireAndForget(delegate { Delete(exfriendID, agentID, friendUUI); });
  573. m_log.DebugFormat("[HGFRIENDS MODULE]: {0} terminated {1}", agentID, friendUUI);
  574. return true;
  575. }
  576. }
  577. else if (friendIsLocal) // agent is foreigner, 'friend' is local
  578. {
  579. agentUUI = GetUUI(exfriendID, agentID);
  580. if (agentUUI != string.Empty)
  581. {
  582. // delete in the local friends service the reference to the foreign agent
  583. FriendsService.Delete(exfriendID, agentUUI);
  584. // and also the converse
  585. FriendsService.Delete(agentUUI, exfriendID.ToString());
  586. // notify the agent's service?
  587. Util.FireAndForget(delegate { Delete(agentID, exfriendID, agentUUI); });
  588. m_log.DebugFormat("[HGFRIENDS MODULE]: {0} terminated {1}", agentUUI, exfriendID);
  589. return true;
  590. }
  591. }
  592. //else They're both foreigners! Can't handle this
  593. return false;
  594. }
  595. private string GetUUI(UUID localUser, UUID foreignUser)
  596. {
  597. // Let's see if the user is here by any chance
  598. FriendInfo[] finfos = GetFriendsFromCache(localUser);
  599. if (finfos != EMPTY_FRIENDS) // friend is here, cool
  600. {
  601. FriendInfo finfo = GetFriend(finfos, foreignUser);
  602. if (finfo != null)
  603. {
  604. return finfo.Friend;
  605. }
  606. }
  607. else // user is not currently on this sim, need to get from the service
  608. {
  609. finfos = FriendsService.GetFriends(localUser);
  610. foreach (FriendInfo finfo in finfos)
  611. {
  612. if (finfo.Friend.StartsWith(foreignUser.ToString())) // found it!
  613. {
  614. return finfo.Friend;
  615. }
  616. }
  617. }
  618. return string.Empty;
  619. }
  620. private void Delete(UUID foreignUser, UUID localUser, string uui)
  621. {
  622. UUID id;
  623. string url = string.Empty, secret = string.Empty, tmp = string.Empty;
  624. if (Util.ParseUniversalUserIdentifier(uui, out id, out url, out tmp, out tmp, out secret))
  625. {
  626. m_log.DebugFormat("[HGFRIENDS MODULE]: Deleting friendship from {0}", url);
  627. HGFriendsServicesConnector friendConn = new HGFriendsServicesConnector(url);
  628. friendConn.DeleteFriendship(foreignUser, localUser, secret);
  629. }
  630. }
  631. protected override bool ForwardFriendshipOffer(UUID agentID, UUID friendID, GridInstantMessage im)
  632. {
  633. if (base.ForwardFriendshipOffer(agentID, friendID, im))
  634. return true;
  635. // OK, that didn't work, so let's try to find this user somewhere
  636. if (!m_uMan.IsLocalGridUser(friendID))
  637. {
  638. string friendsURL = m_uMan.GetUserServerURL(friendID, "FriendsServerURI");
  639. if (friendsURL != string.Empty)
  640. {
  641. m_log.DebugFormat("[HGFRIENDS MODULE]: Forwading friendship from {0} to {1} @ {2}", agentID, friendID, friendsURL);
  642. GridRegion region = new GridRegion();
  643. region.ServerURI = friendsURL;
  644. string name = im.fromAgentName;
  645. if (m_uMan.IsLocalGridUser(agentID))
  646. {
  647. IClientAPI agentClient = LocateClientObject(agentID);
  648. AgentCircuitData agentClientCircuit = ((Scene)(agentClient.Scene)).AuthenticateHandler.GetAgentCircuitData(agentClient.CircuitCode);
  649. string agentHomeService = string.Empty;
  650. try
  651. {
  652. agentHomeService = agentClientCircuit.ServiceURLs["HomeURI"].ToString();
  653. string lastname = "@" + new Uri(agentHomeService).Authority;
  654. string firstname = im.fromAgentName.Replace(" ", ".");
  655. name = firstname + lastname;
  656. }
  657. catch (KeyNotFoundException)
  658. {
  659. m_log.DebugFormat("[HGFRIENDS MODULE]: Key HomeURI not found for user {0}", agentID);
  660. return false;
  661. }
  662. catch (NullReferenceException)
  663. {
  664. m_log.DebugFormat("[HGFRIENDS MODULE]: Null HomeUri for local user {0}", agentID);
  665. return false;
  666. }
  667. catch (UriFormatException)
  668. {
  669. m_log.DebugFormat("[HGFRIENDS MODULE]: Malformed HomeUri {0} for local user {1}", agentHomeService, agentID);
  670. return false;
  671. }
  672. }
  673. m_HGFriendsConnector.FriendshipOffered(region, agentID, friendID, im.message, name);
  674. return true;
  675. }
  676. }
  677. return false;
  678. }
  679. public override bool LocalFriendshipOffered(UUID toID, GridInstantMessage im)
  680. {
  681. if (base.LocalFriendshipOffered(toID, im))
  682. {
  683. if (im.fromAgentName.Contains("@"))
  684. {
  685. string[] parts = im.fromAgentName.Split(new char[] { '@' });
  686. if (parts.Length == 2)
  687. {
  688. string[] fl = parts[0].Trim().Split(new char[] { '.' });
  689. if (fl.Length == 2)
  690. m_uMan.AddUser(new UUID(im.fromAgentID), fl[0], fl[1], "http://" + parts[1]);
  691. else
  692. m_uMan.AddUser(new UUID(im.fromAgentID), fl[0], "", "http://" + parts[1]);
  693. }
  694. }
  695. return true;
  696. }
  697. return false;
  698. }
  699. }
  700. }