HGFriendsModule.cs 36 KB

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