InstantMessageModule.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. using System.Net;
  32. using System.Threading;
  33. using OpenMetaverse;
  34. using log4net;
  35. using Nini.Config;
  36. using Nwc.XmlRpc;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.Client;
  39. using OpenSim.Region.Interfaces;
  40. using OpenSim.Region.Environment.Interfaces;
  41. using OpenSim.Region.Environment.Scenes;
  42. namespace OpenSim.Region.Environment.Modules.Avatar.InstantMessage
  43. {
  44. public class InstantMessageModule : IRegionModule
  45. {
  46. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. private readonly List<Scene> m_scenes = new List<Scene>();
  48. #region IRegionModule Members
  49. //private bool gridmode = false;
  50. private IMessageTransferModule m_TransferModule = null;
  51. public void Initialise(Scene scene, IConfigSource config)
  52. {
  53. if (config.Configs["Messaging"] != null)
  54. {
  55. if (config.Configs["Messaging"].GetString(
  56. "InstantMessageModule", "InstantMessageModule") !=
  57. "InstantMessageModule")
  58. return;
  59. }
  60. lock (m_scenes)
  61. {
  62. if (!m_scenes.Contains(scene))
  63. {
  64. m_scenes.Add(scene);
  65. scene.EventManager.OnClientConnect += OnClientConnect;
  66. scene.EventManager.OnIncomingInstantMessage += OnGridInstantMessage;
  67. }
  68. }
  69. }
  70. void OnClientConnect(IClientCore client)
  71. {
  72. IClientIM clientIM;
  73. if (client.TryGet(out clientIM))
  74. {
  75. clientIM.OnInstantMessage += OnInstantMessage;
  76. }
  77. }
  78. public void PostInitialise()
  79. {
  80. m_TransferModule =
  81. m_scenes[0].RequestModuleInterface<IMessageTransferModule>();
  82. if (m_TransferModule == null)
  83. m_log.Error("[INSTANT MESSAGE]: No message transfer module, "+
  84. "IM will not work!");
  85. }
  86. public void Close()
  87. {
  88. }
  89. public string Name
  90. {
  91. get { return "InstantMessageModule"; }
  92. }
  93. public bool IsSharedModule
  94. {
  95. get { return true; }
  96. }
  97. #endregion
  98. public void OnInstantMessage(IClientAPI client, GridInstantMessage im)
  99. {
  100. byte dialog = im.dialog;
  101. if ( dialog != (byte)InstantMessageDialog.MessageFromAgent
  102. && dialog != (byte)InstantMessageDialog.StartTyping
  103. && dialog != (byte)InstantMessageDialog.StopTyping)
  104. {
  105. return;
  106. }
  107. if (m_TransferModule != null)
  108. {
  109. m_TransferModule.SendInstantMessage(im,
  110. delegate(bool success)
  111. {
  112. if (dialog == (uint)InstantMessageDialog.StartTyping ||
  113. dialog == (uint)InstantMessageDialog.StopTyping)
  114. {
  115. return;
  116. }
  117. if ((client != null) && !success)
  118. {
  119. client.SendInstantMessage(new UUID(im.toAgentID),
  120. "Unable to send instant message. "+
  121. "User is not logged in.",
  122. new UUID(im.fromAgentID), "System",
  123. (byte)InstantMessageDialog.BusyAutoResponse,
  124. (uint)Util.UnixTimeSinceEpoch());
  125. }
  126. }
  127. );
  128. }
  129. }
  130. /// <summary>
  131. ///
  132. /// </summary>
  133. /// <param name="msg"></param>
  134. private void OnGridInstantMessage(GridInstantMessage msg)
  135. {
  136. // Just call the Text IM handler above
  137. // This event won't be raised unless we have that agent,
  138. // so we can depend on the above not trying to send
  139. // via grid again
  140. //
  141. OnInstantMessage(null, msg);
  142. }
  143. }
  144. }