1
0

FriendsModule.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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 OpenSim 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.Generic;
  29. using System.Reflection;
  30. using libsecondlife;
  31. using libsecondlife.Packets;
  32. using log4net;
  33. using Nini.Config;
  34. using Nwc.XmlRpc;
  35. using OpenSim.Framework;
  36. using OpenSim.Region.Environment.Interfaces;
  37. using OpenSim.Region.Environment.Scenes;
  38. namespace OpenSim.Region.Environment.Modules.Avatar.Friends
  39. {
  40. public class FriendsModule : IRegionModule
  41. {
  42. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. private Dictionary<LLUUID, List<FriendListItem>> FriendLists = new Dictionary<LLUUID, List<FriendListItem>>();
  44. private Dictionary<LLUUID, LLUUID> m_pendingFriendRequests = new Dictionary<LLUUID, LLUUID>();
  45. private Dictionary<LLUUID, ulong> m_rootAgents = new Dictionary<LLUUID, ulong>();
  46. private List<Scene> m_scene = new List<Scene>();
  47. #region IRegionModule Members
  48. public void Initialise(Scene scene, IConfigSource config)
  49. {
  50. lock (m_scene)
  51. {
  52. if (m_scene.Count == 0)
  53. {
  54. scene.AddXmlRPCHandler("presence_update", processPresenceUpdate);
  55. }
  56. if (!m_scene.Contains(scene))
  57. m_scene.Add(scene);
  58. }
  59. scene.EventManager.OnNewClient += OnNewClient;
  60. scene.EventManager.OnGridInstantMessageToFriendsModule += OnGridInstantMessage;
  61. scene.EventManager.OnAvatarEnteringNewParcel += AvatarEnteringParcel;
  62. scene.EventManager.OnMakeChildAgent += MakeChildAgent;
  63. scene.EventManager.OnClientClosed += ClientLoggedOut;
  64. }
  65. public void PostInitialise()
  66. {
  67. }
  68. public void Close()
  69. {
  70. }
  71. public string Name
  72. {
  73. get { return "FriendsModule"; }
  74. }
  75. public bool IsSharedModule
  76. {
  77. get { return true; }
  78. }
  79. #endregion
  80. public XmlRpcResponse processPresenceUpdate(XmlRpcRequest req)
  81. {
  82. m_log.Info("[FRIENDS]: Got Notification about a user! OMG");
  83. return new XmlRpcResponse();
  84. }
  85. private void OnNewClient(IClientAPI client)
  86. {
  87. // All friends establishment protocol goes over instant message
  88. // There's no way to send a message from the sim
  89. // to a user to 'add a friend' without causing dialog box spam
  90. //
  91. // The base set of friends are added when the user signs on in their XMLRPC response
  92. // Generated by LoginService. The friends are retreived from the database by the UserManager
  93. // Subscribe to instant messages
  94. client.OnInstantMessage += OnInstantMessage;
  95. client.OnApproveFriendRequest += OnApprovedFriendRequest;
  96. client.OnDenyFriendRequest += OnDenyFriendRequest;
  97. client.OnTerminateFriendship += OnTerminateFriendship;
  98. List<FriendListItem> fl = new List<FriendListItem>();
  99. //bool addFLback = false;
  100. lock (FriendLists)
  101. {
  102. if (FriendLists.ContainsKey(client.AgentId))
  103. {
  104. fl = FriendLists[client.AgentId];
  105. }
  106. else
  107. {
  108. fl = m_scene[0].GetFriendList(client.AgentId);
  109. //lock (FriendLists)
  110. //{
  111. if (!FriendLists.ContainsKey(client.AgentId))
  112. FriendLists.Add(client.AgentId, fl);
  113. //}
  114. }
  115. }
  116. List<LLUUID> UpdateUsers = new List<LLUUID>();
  117. foreach (FriendListItem f in fl)
  118. {
  119. if (m_rootAgents.ContainsKey(f.Friend))
  120. {
  121. if (f.onlinestatus == false)
  122. {
  123. UpdateUsers.Add(f.Friend);
  124. f.onlinestatus = true;
  125. }
  126. }
  127. }
  128. foreach (LLUUID user in UpdateUsers)
  129. {
  130. ScenePresence av = GetPresenceFromAgentID(user);
  131. if (av != null)
  132. {
  133. List<FriendListItem> usrfl = new List<FriendListItem>();
  134. lock (FriendLists)
  135. {
  136. usrfl = FriendLists[user];
  137. }
  138. lock (usrfl)
  139. {
  140. foreach (FriendListItem fli in usrfl)
  141. {
  142. if (fli.Friend == client.AgentId)
  143. {
  144. fli.onlinestatus = true;
  145. LLUUID[] Agents = new LLUUID[1];
  146. Agents[0] = client.AgentId;
  147. av.ControllingClient.SendAgentOnline(Agents);
  148. }
  149. }
  150. }
  151. }
  152. }
  153. if (UpdateUsers.Count > 0)
  154. {
  155. client.SendAgentOnline(UpdateUsers.ToArray());
  156. }
  157. }
  158. private void ClientLoggedOut(LLUUID AgentId)
  159. {
  160. lock (m_rootAgents)
  161. {
  162. if (m_rootAgents.ContainsKey(AgentId))
  163. {
  164. m_rootAgents.Remove(AgentId);
  165. m_log.Info("[FRIEND]: Removing " + AgentId + ". Agent logged out.");
  166. }
  167. }
  168. List<FriendListItem> lfli = new List<FriendListItem>();
  169. lock (FriendLists)
  170. {
  171. if (FriendLists.ContainsKey(AgentId))
  172. {
  173. lfli = FriendLists[AgentId];
  174. }
  175. }
  176. List<LLUUID> updateUsers = new List<LLUUID>();
  177. foreach (FriendListItem fli in lfli)
  178. {
  179. if (fli.onlinestatus == true)
  180. {
  181. updateUsers.Add(fli.Friend);
  182. }
  183. }
  184. lock (updateUsers)
  185. {
  186. for (int i = 0; i < updateUsers.Count; i++)
  187. {
  188. List<FriendListItem> flfli = new List<FriendListItem>();
  189. try
  190. {
  191. lock (FriendLists)
  192. {
  193. if (FriendLists.ContainsKey(updateUsers[i]))
  194. flfli = FriendLists[updateUsers[i]];
  195. }
  196. }
  197. catch (IndexOutOfRangeException)
  198. {
  199. // Ignore the index out of range exception.
  200. // This causes friend lists to get out of sync slightly.. however
  201. // prevents a sim crash.
  202. m_log.Info("[FRIEND]: Unable to enumerate last friendlist user. User logged off");
  203. }
  204. catch (ArgumentOutOfRangeException)
  205. {
  206. // Ignore the index out of range exception.
  207. // This causes friend lists to get out of sync slightly.. however
  208. // prevents a sim crash.
  209. m_log.Info("[FRIEND]: Unable to enumerate last friendlist user. User logged off");
  210. }
  211. for (int j = 0; j < flfli.Count; j++)
  212. {
  213. try
  214. {
  215. if (flfli[i].Friend == AgentId)
  216. {
  217. flfli[i].onlinestatus = false;
  218. }
  219. }
  220. catch (IndexOutOfRangeException)
  221. {
  222. // Ignore the index out of range exception.
  223. // This causes friend lists to get out of sync slightly.. however
  224. // prevents a sim crash.
  225. m_log.Info("[FRIEND]: Unable to enumerate last friendlist user. User logged off");
  226. }
  227. catch (ArgumentOutOfRangeException)
  228. {
  229. // Ignore the index out of range exception.
  230. // This causes friend lists to get out of sync slightly.. however
  231. // prevents a sim crash.
  232. m_log.Info("[FRIEND]: Unable to enumerate last friendlist user. User logged off");
  233. }
  234. }
  235. }
  236. for (int i = 0; i < updateUsers.Count; i++)
  237. {
  238. ScenePresence av = GetPresenceFromAgentID(updateUsers[i]);
  239. if (av != null)
  240. {
  241. LLUUID[] agents = new LLUUID[1];
  242. agents[0] = AgentId;
  243. av.ControllingClient.SendAgentOffline(agents);
  244. }
  245. }
  246. }
  247. lock (FriendLists)
  248. {
  249. FriendLists.Remove(AgentId);
  250. }
  251. }
  252. private void AvatarEnteringParcel(ScenePresence avatar, int localLandID, LLUUID regionID)
  253. {
  254. lock (m_rootAgents)
  255. {
  256. if (m_rootAgents.ContainsKey(avatar.UUID))
  257. {
  258. if (avatar.RegionHandle != m_rootAgents[avatar.UUID])
  259. {
  260. m_rootAgents[avatar.UUID] = avatar.RegionHandle;
  261. m_log.Info("[FRIEND]: Claiming " + avatar.Firstname + " " + avatar.Lastname + " in region:" + avatar.RegionHandle + ".");
  262. if (avatar.JID.Length > 0)
  263. {
  264. JId avatarID = new JId(avatar.JID);
  265. // REST Post XMPP Stanzas!
  266. }
  267. // Claim User! my user! Mine mine mine!
  268. }
  269. }
  270. else
  271. {
  272. m_rootAgents.Add(avatar.UUID, avatar.RegionHandle);
  273. m_log.Info("[FRIEND]: Claiming " + avatar.Firstname + " " + avatar.Lastname + " in region:" + avatar.RegionHandle + ".");
  274. }
  275. }
  276. //m_log.Info("[FRIEND]: " + avatar.Name + " status:" + (!avatar.IsChildAgent).ToString());
  277. }
  278. private void MakeChildAgent(ScenePresence avatar)
  279. {
  280. lock (m_rootAgents)
  281. {
  282. if (m_rootAgents.ContainsKey(avatar.UUID))
  283. {
  284. if (m_rootAgents[avatar.UUID] == avatar.RegionHandle)
  285. {
  286. m_rootAgents.Remove(avatar.UUID);
  287. m_log.Info("[FRIEND]: Removing " + avatar.Firstname + " " + avatar.Lastname + " as a root agent");
  288. }
  289. }
  290. }
  291. }
  292. private ScenePresence GetPresenceFromAgentID(LLUUID AgentID)
  293. {
  294. ScenePresence returnAgent = null;
  295. lock (m_scene)
  296. {
  297. ScenePresence queryagent = null;
  298. for (int i = 0; i < m_scene.Count; i++)
  299. {
  300. queryagent = m_scene[i].GetScenePresence(AgentID);
  301. if (queryagent != null)
  302. {
  303. if (!queryagent.IsChildAgent)
  304. {
  305. returnAgent = queryagent;
  306. break;
  307. }
  308. }
  309. }
  310. }
  311. return returnAgent;
  312. }
  313. #region FriendRequestHandling
  314. private void OnInstantMessage(IClientAPI client, LLUUID fromAgentID,
  315. LLUUID fromAgentSession, LLUUID toAgentID,
  316. LLUUID imSessionID, uint timestamp, string fromAgentName,
  317. string message, byte dialog, bool fromGroup, byte offline,
  318. uint ParentEstateID, LLVector3 Position, LLUUID RegionID,
  319. byte[] binaryBucket)
  320. {
  321. // Friend Requests go by Instant Message.. using the dialog param
  322. // https://wiki.secondlife.com/wiki/ImprovedInstantMessage
  323. // 38 == Offer friendship
  324. if (dialog == (byte) 38)
  325. {
  326. LLUUID friendTransactionID = LLUUID.Random();
  327. m_pendingFriendRequests.Add(friendTransactionID, fromAgentID);
  328. m_log.Info("[FRIEND]: 38 - From:" + fromAgentID.ToString() + " To: " + toAgentID.ToString() + " Session:" + imSessionID.ToString() + " Message:" +
  329. message);
  330. GridInstantMessage msg = new GridInstantMessage();
  331. msg.fromAgentID = fromAgentID.UUID;
  332. msg.fromAgentSession = fromAgentSession.UUID;
  333. msg.toAgentID = toAgentID.UUID;
  334. msg.imSessionID = friendTransactionID.UUID; // This is the item we're mucking with here
  335. m_log.Info("[FRIEND]: Filling Session: " + msg.imSessionID.ToString());
  336. msg.timestamp = timestamp;
  337. if (client != null)
  338. {
  339. msg.fromAgentName = client.Name; // fromAgentName;
  340. }
  341. else
  342. {
  343. msg.fromAgentName = "(hippos)"; // Added for posterity. This means that we can't figure out who sent it
  344. }
  345. msg.message = message;
  346. msg.dialog = dialog;
  347. msg.fromGroup = fromGroup;
  348. msg.offline = offline;
  349. msg.ParentEstateID = ParentEstateID;
  350. msg.Position = new sLLVector3(Position);
  351. msg.RegionID = RegionID.UUID;
  352. msg.binaryBucket = binaryBucket;
  353. // We don't really care which scene we pipe it through.
  354. m_scene[0].TriggerGridInstantMessage(msg, InstantMessageReceiver.IMModule);
  355. }
  356. // 39 == Accept Friendship
  357. if (dialog == (byte) 39)
  358. {
  359. m_log.Info("[FRIEND]: 39 - From:" + fromAgentID.ToString() + " To: " + toAgentID.ToString() + " Session:" + imSessionID.ToString() + " Message:" +
  360. message);
  361. }
  362. // 40 == Decline Friendship
  363. if (dialog == (byte) 40)
  364. {
  365. m_log.Info("[FRIEND]: 40 - From:" + fromAgentID.ToString() + " To: " + toAgentID.ToString() + " Session:" + imSessionID.ToString() + " Message:" +
  366. message);
  367. }
  368. }
  369. private void OnApprovedFriendRequest(IClientAPI client, LLUUID agentID, LLUUID transactionID, List<LLUUID> callingCardFolders)
  370. {
  371. if (m_pendingFriendRequests.ContainsKey(transactionID))
  372. {
  373. // Found Pending Friend Request with that Transaction..
  374. Scene SceneAgentIn = m_scene[0];
  375. // Found Pending Friend Request with that Transaction..
  376. ScenePresence agentpresence = GetPresenceFromAgentID(agentID);
  377. if (agentpresence != null)
  378. {
  379. SceneAgentIn = agentpresence.Scene;
  380. }
  381. // Compose response to other agent.
  382. GridInstantMessage msg = new GridInstantMessage();
  383. msg.toAgentID = m_pendingFriendRequests[transactionID].UUID;
  384. msg.fromAgentID = agentID.UUID;
  385. msg.fromAgentName = client.Name;
  386. msg.fromAgentSession = client.SessionId.UUID;
  387. msg.fromGroup = false;
  388. msg.imSessionID = transactionID.UUID;
  389. msg.message = agentID.UUID.ToString();
  390. msg.ParentEstateID = 0;
  391. msg.timestamp = (uint) Util.UnixTimeSinceEpoch();
  392. msg.RegionID = SceneAgentIn.RegionInfo.RegionID.UUID;
  393. msg.dialog = (byte) 39; // Approved friend request
  394. msg.Position = new sLLVector3();
  395. msg.offline = (byte) 0;
  396. msg.binaryBucket = new byte[0];
  397. // We don't really care which scene we pipe it through, it goes to the shared IM Module and/or the database
  398. SceneAgentIn.TriggerGridInstantMessage(msg, InstantMessageReceiver.IMModule);
  399. SceneAgentIn.StoreAddFriendship(m_pendingFriendRequests[transactionID], agentID, (uint) 1);
  400. m_pendingFriendRequests.Remove(transactionID);
  401. // TODO: Inform agent that the friend is online
  402. }
  403. }
  404. private void OnDenyFriendRequest(IClientAPI client, LLUUID agentID, LLUUID transactionID, List<LLUUID> callingCardFolders)
  405. {
  406. if (m_pendingFriendRequests.ContainsKey(transactionID))
  407. {
  408. Scene SceneAgentIn = m_scene[0];
  409. // Found Pending Friend Request with that Transaction..
  410. ScenePresence agentpresence = GetPresenceFromAgentID(agentID);
  411. if (agentpresence != null)
  412. {
  413. SceneAgentIn = agentpresence.Scene;
  414. }
  415. // Compose response to other agent.
  416. GridInstantMessage msg = new GridInstantMessage();
  417. msg.toAgentID = m_pendingFriendRequests[transactionID].UUID;
  418. msg.fromAgentID = agentID.UUID;
  419. msg.fromAgentName = client.Name;
  420. msg.fromAgentSession = client.SessionId.UUID;
  421. msg.fromGroup = false;
  422. msg.imSessionID = transactionID.UUID;
  423. msg.message = agentID.UUID.ToString();
  424. msg.ParentEstateID = 0;
  425. msg.timestamp = (uint) Util.UnixTimeSinceEpoch();
  426. msg.RegionID = SceneAgentIn.RegionInfo.RegionID.UUID;
  427. msg.dialog = (byte) 40; // Deny friend request
  428. msg.Position = new sLLVector3();
  429. msg.offline = (byte) 0;
  430. msg.binaryBucket = new byte[0];
  431. SceneAgentIn.TriggerGridInstantMessage(msg, InstantMessageReceiver.IMModule);
  432. m_pendingFriendRequests.Remove(transactionID);
  433. }
  434. }
  435. private void OnTerminateFriendship(IClientAPI client, LLUUID agent, LLUUID exfriendID)
  436. {
  437. m_scene[0].StoreRemoveFriendship(agent, exfriendID);
  438. // TODO: Inform the client that the ExFriend is offline
  439. }
  440. private void OnGridInstantMessage(GridInstantMessage msg)
  441. {
  442. // Trigger the above event handler
  443. OnInstantMessage(null, new LLUUID(msg.fromAgentID), new LLUUID(msg.fromAgentSession),
  444. new LLUUID(msg.toAgentID), new LLUUID(msg.imSessionID), msg.timestamp, msg.fromAgentName,
  445. msg.message, msg.dialog, msg.fromGroup, msg.offline, msg.ParentEstateID,
  446. new LLVector3(msg.Position.x, msg.Position.y, msg.Position.z), new LLUUID(msg.RegionID),
  447. msg.binaryBucket);
  448. }
  449. #endregion
  450. }
  451. }