FriendsModule.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. */
  28. using Nini.Config;
  29. using System;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. using OpenSim.Framework;
  33. using OpenSim.Framework.Console;
  34. using OpenSim.Region.Environment.Interfaces;
  35. using OpenSim.Region.Environment.Scenes;
  36. using libsecondlife;
  37. namespace OpenSim.Region.Environment.Modules
  38. {
  39. public class FriendsModule : IRegionModule
  40. {
  41. private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  42. private Scene m_scene;
  43. Dictionary<LLUUID, LLUUID> m_pendingFriendRequests = new Dictionary<LLUUID, LLUUID>();
  44. public void Initialise(Scene scene, IConfigSource config)
  45. {
  46. m_scene = scene;
  47. scene.EventManager.OnNewClient += OnNewClient;
  48. scene.EventManager.OnGridInstantMessageToFriendsModule += OnGridInstantMessage;
  49. }
  50. private void OnNewClient(IClientAPI client)
  51. {
  52. // All friends establishment protocol goes over instant message
  53. // There's no way to send a message from the sim
  54. // to a user to 'add a friend' without causing dialog box spam
  55. //
  56. // The base set of friends are added when the user signs on in their XMLRPC response
  57. // Generated by LoginService. The friends are retreived from the database by the UserManager
  58. // Subscribe to instant messages
  59. client.OnInstantMessage += OnInstantMessage;
  60. client.OnApproveFriendRequest += OnApprovedFriendRequest;
  61. client.OnDenyFriendRequest += OnDenyFriendRequest;
  62. client.OnTerminateFriendship += OnTerminateFriendship;
  63. }
  64. private void OnInstantMessage(IClientAPI client,LLUUID fromAgentID,
  65. LLUUID fromAgentSession, LLUUID toAgentID,
  66. LLUUID imSessionID, uint timestamp, string fromAgentName,
  67. string message, byte dialog, bool fromGroup, byte offline,
  68. uint ParentEstateID, LLVector3 Position, LLUUID RegionID,
  69. byte[] binaryBucket)
  70. {
  71. // Friend Requests go by Instant Message.. using the dialog param
  72. // https://wiki.secondlife.com/wiki/ImprovedInstantMessage
  73. // 38 == Offer friendship
  74. if (dialog == (byte)38)
  75. {
  76. LLUUID friendTransactionID = LLUUID.Random();
  77. m_pendingFriendRequests.Add(friendTransactionID, fromAgentID);
  78. m_log.Info("[FRIEND]: 38 - From:" + fromAgentID.ToString() + " To: " + toAgentID.ToString() + " Session:" + imSessionID.ToString() + " Message:" + message);
  79. GridInstantMessage msg = new GridInstantMessage();
  80. msg.fromAgentID = fromAgentID.UUID;
  81. msg.fromAgentSession = fromAgentSession.UUID;
  82. msg.toAgentID = toAgentID.UUID;
  83. msg.imSessionID = friendTransactionID.UUID; // This is the item we're mucking with here
  84. m_log.Info("[FRIEND]: Filling Session: " + msg.imSessionID.ToString());
  85. msg.timestamp = timestamp;
  86. if (client != null)
  87. {
  88. msg.fromAgentName = client.FirstName + " " + client.LastName;// fromAgentName;
  89. }
  90. else
  91. {
  92. msg.fromAgentName = "(hippos)";// Added for posterity. This means that we can't figure out who sent it
  93. }
  94. msg.message = message;
  95. msg.dialog = dialog;
  96. msg.fromGroup = fromGroup;
  97. msg.offline = offline;
  98. msg.ParentEstateID = ParentEstateID;
  99. msg.Position = new sLLVector3(Position);
  100. msg.RegionID = RegionID.UUID;
  101. msg.binaryBucket = binaryBucket;
  102. m_scene.TriggerGridInstantMessage(msg, InstantMessageReceiver.IMModule);
  103. }
  104. // 39 == Accept Friendship
  105. if (dialog == (byte)39)
  106. {
  107. m_log.Info("[FRIEND]: 39 - From:" + fromAgentID.ToString() + " To: " + toAgentID.ToString() + " Session:" + imSessionID.ToString() + " Message:" + message);
  108. }
  109. // 40 == Decline Friendship
  110. if (dialog == (byte)40)
  111. {
  112. m_log.Info("[FRIEND]: 40 - From:" + fromAgentID.ToString() + " To: " + toAgentID.ToString() + " Session:" + imSessionID.ToString() + " Message:" + message);
  113. }
  114. }
  115. private void OnApprovedFriendRequest(IClientAPI client, LLUUID agentID, LLUUID transactionID, List<LLUUID> callingCardFolders)
  116. {
  117. if (m_pendingFriendRequests.ContainsKey(transactionID))
  118. {
  119. // Found Pending Friend Request with that Transaction..
  120. // Compose response to other agent.
  121. GridInstantMessage msg = new GridInstantMessage();
  122. msg.toAgentID = m_pendingFriendRequests[transactionID].UUID;
  123. msg.fromAgentID = agentID.UUID;
  124. msg.fromAgentName = client.FirstName + " " + client.LastName;
  125. msg.fromAgentSession = client.SessionId.UUID;
  126. msg.fromGroup = false;
  127. msg.imSessionID = transactionID.UUID;
  128. msg.message = agentID.UUID.ToString();
  129. msg.ParentEstateID = 0;
  130. msg.timestamp = (uint)Util.UnixTimeSinceEpoch();
  131. msg.RegionID = m_scene.RegionInfo.RegionID.UUID;
  132. msg.dialog = (byte)39;// Approved friend request
  133. msg.Position = new sLLVector3();
  134. msg.offline = (byte)0;
  135. msg.binaryBucket = new byte[0];
  136. m_scene.TriggerGridInstantMessage(msg, InstantMessageReceiver.IMModule);
  137. m_scene.StoreAddFriendship(m_pendingFriendRequests[transactionID], agentID, (uint)1);
  138. m_pendingFriendRequests.Remove(transactionID);
  139. // TODO: Inform agent that the friend is online
  140. }
  141. }
  142. private void OnDenyFriendRequest(IClientAPI client, LLUUID agentID, LLUUID transactionID, List<LLUUID> callingCardFolders)
  143. {
  144. if (m_pendingFriendRequests.ContainsKey(transactionID))
  145. {
  146. // Found Pending Friend Request with that Transaction..
  147. // Compose response to other agent.
  148. GridInstantMessage msg = new GridInstantMessage();
  149. msg.toAgentID = m_pendingFriendRequests[transactionID].UUID;
  150. msg.fromAgentID = agentID.UUID;
  151. msg.fromAgentName = client.FirstName + " " + client.LastName;
  152. msg.fromAgentSession = client.SessionId.UUID;
  153. msg.fromGroup = false;
  154. msg.imSessionID = transactionID.UUID;
  155. msg.message = agentID.UUID.ToString();
  156. msg.ParentEstateID = 0;
  157. msg.timestamp = (uint)Util.UnixTimeSinceEpoch();
  158. msg.RegionID = m_scene.RegionInfo.RegionID.UUID;
  159. msg.dialog = (byte)40;// Deny friend request
  160. msg.Position = new sLLVector3();
  161. msg.offline = (byte)0;
  162. msg.binaryBucket = new byte[0];
  163. m_scene.TriggerGridInstantMessage(msg, InstantMessageReceiver.IMModule);
  164. m_pendingFriendRequests.Remove(transactionID);
  165. }
  166. }
  167. private void OnTerminateFriendship(IClientAPI client, LLUUID agent, LLUUID exfriendID)
  168. {
  169. m_scene.StoreRemoveFriendship(agent, exfriendID);
  170. // TODO: Inform the client that the ExFriend is offline
  171. }
  172. private void OnGridInstantMessage(GridInstantMessage msg)
  173. {
  174. // Trigger the above event handler
  175. OnInstantMessage(null,new LLUUID(msg.fromAgentID), new LLUUID(msg.fromAgentSession),
  176. new LLUUID(msg.toAgentID), new LLUUID(msg.imSessionID), msg.timestamp, msg.fromAgentName,
  177. msg.message, msg.dialog, msg.fromGroup, msg.offline, msg.ParentEstateID,
  178. new LLVector3(msg.Position.x, msg.Position.y, msg.Position.z), new LLUUID(msg.RegionID),
  179. msg.binaryBucket);
  180. }
  181. public void PostInitialise()
  182. {
  183. }
  184. public void Close()
  185. {
  186. }
  187. public string Name
  188. {
  189. get { return "FriendsModule"; }
  190. }
  191. public bool IsSharedModule
  192. {
  193. get { return false; }
  194. }
  195. }
  196. }