InstantMessageModule.cs 6.7 KB

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