InstantMessageModule.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 OpenSimulator 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 System.Reflection;
  29. using log4net;
  30. using Nini.Config;
  31. using OpenMetaverse;
  32. using OpenSim.Framework;
  33. using OpenSim.Framework.Client;
  34. using OpenSim.Region.Framework.Interfaces;
  35. using OpenSim.Region.Framework.Scenes;
  36. namespace OpenSim.Region.CoreModules.Avatar.InstantMessage
  37. {
  38. public class InstantMessageModule : IRegionModule
  39. {
  40. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  41. /// <value>
  42. /// Is this module enabled?
  43. /// </value>
  44. private bool m_enabled = false;
  45. private readonly List<Scene> m_scenes = new List<Scene>();
  46. #region IRegionModule Members
  47. private IMessageTransferModule m_TransferModule = null;
  48. public void Initialise(Scene scene, IConfigSource config)
  49. {
  50. if (config.Configs["Messaging"] != null)
  51. {
  52. if (config.Configs["Messaging"].GetString(
  53. "InstantMessageModule", "InstantMessageModule") !=
  54. "InstantMessageModule")
  55. return;
  56. }
  57. m_enabled = true;
  58. lock (m_scenes)
  59. {
  60. if (!m_scenes.Contains(scene))
  61. {
  62. m_scenes.Add(scene);
  63. scene.EventManager.OnClientConnect += OnClientConnect;
  64. scene.EventManager.OnIncomingInstantMessage += OnGridInstantMessage;
  65. }
  66. }
  67. }
  68. void OnClientConnect(IClientCore client)
  69. {
  70. IClientIM clientIM;
  71. if (client.TryGet(out clientIM))
  72. {
  73. clientIM.OnInstantMessage += OnInstantMessage;
  74. }
  75. }
  76. public void PostInitialise()
  77. {
  78. if (!m_enabled)
  79. return;
  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. && dialog != (byte)InstantMessageDialog.MessageFromObject)
  105. {
  106. return;
  107. }
  108. if (m_TransferModule != null)
  109. {
  110. m_TransferModule.SendInstantMessage(im,
  111. delegate(bool success)
  112. {
  113. if (dialog == (uint)InstantMessageDialog.StartTyping ||
  114. dialog == (uint)InstantMessageDialog.StopTyping)
  115. {
  116. return;
  117. }
  118. if ((client != null) && !success)
  119. {
  120. client.SendInstantMessage(
  121. new GridInstantMessage(
  122. null, new UUID(im.fromAgentID), "System",
  123. new UUID(im.toAgentID),
  124. (byte)InstantMessageDialog.BusyAutoResponse,
  125. "Unable to send instant message. "+
  126. "User is not logged in.", false,
  127. new Vector3()));
  128. }
  129. }
  130. );
  131. }
  132. }
  133. /// <summary>
  134. ///
  135. /// </summary>
  136. /// <param name="msg"></param>
  137. private void OnGridInstantMessage(GridInstantMessage msg)
  138. {
  139. // Just call the Text IM handler above
  140. // This event won't be raised unless we have that agent,
  141. // so we can depend on the above not trying to send
  142. // via grid again
  143. //
  144. OnInstantMessage(null, msg);
  145. }
  146. }
  147. }