InstantMessageModule.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.Collections.Generic;
  28. using libsecondlife;
  29. using Nini.Config;
  30. using OpenSim.Framework;
  31. using OpenSim.Region.Environment.Interfaces;
  32. using OpenSim.Region.Environment.Scenes;
  33. namespace OpenSim.Region.Environment.Modules.Avatar.InstantMessage
  34. {
  35. public class InstantMessageModule : IRegionModule
  36. {
  37. private readonly List<Scene> m_scenes = new List<Scene>();
  38. #region IRegionModule Members
  39. public void Initialise(Scene scene, IConfigSource config)
  40. {
  41. lock (m_scenes)
  42. {
  43. if (m_scenes.Count == 0)
  44. {
  45. //scene.AddXmlRPCHandler("avatar_location_update", processPresenceUpdate);
  46. }
  47. if (!m_scenes.Contains(scene))
  48. {
  49. m_scenes.Add(scene);
  50. scene.EventManager.OnNewClient += OnNewClient;
  51. scene.EventManager.OnGridInstantMessageToIMModule += OnGridInstantMessage;
  52. }
  53. }
  54. }
  55. public void PostInitialise()
  56. {
  57. }
  58. public void Close()
  59. {
  60. }
  61. public string Name
  62. {
  63. get { return "InstantMessageModule"; }
  64. }
  65. public bool IsSharedModule
  66. {
  67. get { return true; }
  68. }
  69. #endregion
  70. private void OnNewClient(IClientAPI client)
  71. {
  72. client.OnInstantMessage += OnInstantMessage;
  73. }
  74. private void OnInstantMessage(IClientAPI client, LLUUID fromAgentID,
  75. LLUUID fromAgentSession, LLUUID toAgentID,
  76. LLUUID imSessionID, uint timestamp, string fromAgentName,
  77. string message, byte dialog, bool fromGroup, byte offline,
  78. uint ParentEstateID, LLVector3 Position, LLUUID RegionID,
  79. byte[] binaryBucket)
  80. {
  81. bool dialogHandledElsewhere
  82. = ((dialog == 38) || (dialog == 39) || (dialog == 40)
  83. || dialog == (byte) InstantMessageDialog.InventoryOffered
  84. || dialog == (byte) InstantMessageDialog.InventoryAccepted
  85. || dialog == (byte) InstantMessageDialog.InventoryDeclined);
  86. // IM dialogs need to be pre-processed and have their sessionID filled by the server
  87. // so the sim can match the transaction on the return packet.
  88. // Don't send a Friend Dialog IM with a LLUUID.Zero session.
  89. if (!(dialogHandledElsewhere && imSessionID == LLUUID.Zero))
  90. {
  91. // Try root avatar only first
  92. foreach (Scene scene in m_scenes)
  93. {
  94. if (scene.Entities.ContainsKey(toAgentID) && scene.Entities[toAgentID] is ScenePresence)
  95. {
  96. // Local message
  97. ScenePresence user = (ScenePresence) scene.Entities[toAgentID];
  98. if (!user.IsChildAgent)
  99. {
  100. user.ControllingClient.SendInstantMessage(fromAgentID, fromAgentSession, message,
  101. toAgentID, imSessionID, fromAgentName, dialog,
  102. timestamp);
  103. // Message sent
  104. return;
  105. }
  106. }
  107. }
  108. // try child avatar second
  109. foreach (Scene scene in m_scenes)
  110. {
  111. if (scene.Entities.ContainsKey(toAgentID) && scene.Entities[toAgentID] is ScenePresence)
  112. {
  113. // Local message
  114. ScenePresence user = (ScenePresence) scene.Entities[toAgentID];
  115. user.ControllingClient.SendInstantMessage(fromAgentID, fromAgentSession, message,
  116. toAgentID, imSessionID, fromAgentName, dialog,
  117. timestamp);
  118. // Message sent
  119. return;
  120. }
  121. }
  122. }
  123. // Still here, try send via Grid
  124. // TODO
  125. }
  126. // Trusty OSG1 called method. This method also gets called from the FriendsModule
  127. // Turns out the sim has to send an instant message to the user to get it to show an accepted friend.
  128. private void OnGridInstantMessage(GridInstantMessage msg)
  129. {
  130. // Trigger the above event handler
  131. OnInstantMessage(null, new LLUUID(msg.fromAgentID), new LLUUID(msg.fromAgentSession),
  132. new LLUUID(msg.toAgentID), new LLUUID(msg.imSessionID), msg.timestamp, msg.fromAgentName,
  133. msg.message, msg.dialog, msg.fromGroup, msg.offline, msg.ParentEstateID,
  134. new LLVector3(msg.Position.x, msg.Position.y, msg.Position.z), new LLUUID(msg.RegionID),
  135. msg.binaryBucket);
  136. }
  137. }
  138. }