InstantMessageModule.cs 5.3 KB

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