InstantMessageModule.cs 5.6 KB

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