InstantMessageModule.cs 7.1 KB

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