InstantMessageModule.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 System.Timers;
  31. using log4net;
  32. using Mono.Addins;
  33. using Nini.Config;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  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. protected bool m_enabled = false;
  49. protected readonly List<Scene> m_scenes = new List<Scene>();
  50. #region Region Module interface
  51. protected IMessageTransferModule m_TransferModule = null;
  52. public virtual 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 virtual void AddRegion(Scene scene)
  64. {
  65. if (!m_enabled)
  66. return;
  67. lock (m_scenes)
  68. m_scenes.Add(scene);
  69. }
  70. public virtual void RegionLoaded(Scene scene)
  71. {
  72. if (!m_enabled)
  73. return;
  74. if (m_TransferModule == null)
  75. {
  76. m_TransferModule = scene.RequestModuleInterface<IMessageTransferModule>();
  77. if (m_TransferModule == null)
  78. {
  79. m_log.Error("[INSTANT MESSAGE]: No message transfer module, IM will not work!");
  80. scene.EventManager.OnNewClient -= OnClientConnect;
  81. scene.EventManager.OnIncomingInstantMessage -= OnGridInstantMessage;
  82. m_scenes.Clear();
  83. m_enabled = false;
  84. }
  85. }
  86. scene.EventManager.OnNewClient += OnClientConnect;
  87. scene.EventManager.OnIncomingInstantMessage += OnGridInstantMessage;
  88. }
  89. public virtual void RemoveRegion(Scene scene)
  90. {
  91. if (!m_enabled)
  92. return;
  93. lock (m_scenes)
  94. {
  95. m_scenes.Remove(scene);
  96. }
  97. }
  98. protected virtual void OnClientConnect(IClientAPI client)
  99. {
  100. client.OnInstantMessage += OnInstantMessage;
  101. }
  102. public virtual void PostInitialise()
  103. {
  104. }
  105. public virtual void Close()
  106. {
  107. }
  108. public virtual string Name
  109. {
  110. get { return "InstantMessageModule"; }
  111. }
  112. public virtual Type ReplaceableInterface
  113. {
  114. get { return null; }
  115. }
  116. #endregion
  117. /*
  118. public virtual void OnViewerInstantMessage(IClientAPI client, GridInstantMessage im)
  119. {
  120. im.fromAgentName = client.FirstName + " " + client.LastName;
  121. OnInstantMessage(client, im);
  122. }
  123. */
  124. public virtual void OnInstantMessage(IClientAPI client, GridInstantMessage im)
  125. {
  126. if (m_TransferModule == null)
  127. return;
  128. switch(im.dialog)
  129. {
  130. case (byte)InstantMessageDialog.MessageFromAgent:
  131. case (byte)InstantMessageDialog.StartTyping:
  132. case (byte)InstantMessageDialog.StopTyping:
  133. case (byte)InstantMessageDialog.BusyAutoResponse:
  134. case (byte)InstantMessageDialog.MessageFromObject:
  135. break;
  136. default:
  137. return;
  138. }
  139. if (client != null)
  140. im.offline = 0;
  141. if (im.offline == 0)
  142. im.timestamp = (uint)Util.UnixTimeSinceEpoch();
  143. m_TransferModule.SendInstantMessage(im,
  144. delegate(bool success)
  145. {
  146. if(success || client == null)
  147. return;
  148. switch (im.dialog)
  149. {
  150. case (byte)InstantMessageDialog.StartTyping:
  151. case (byte)InstantMessageDialog.StopTyping:
  152. case (byte)InstantMessageDialog.MessageFromObject:
  153. return;
  154. default:
  155. break;
  156. }
  157. client.SendInstantMessage(new GridInstantMessage(
  158. null, new UUID(im.fromAgentID), "System",
  159. new UUID(im.toAgentID),
  160. (byte)InstantMessageDialog.BusyAutoResponse,
  161. "Unable to send instant message. User is not logged in.",
  162. false, new Vector3())
  163. );
  164. }
  165. );
  166. }
  167. /// <summary>
  168. ///
  169. /// </summary>
  170. /// <param name="msg"></param>
  171. protected virtual void OnGridInstantMessage(GridInstantMessage msg)
  172. {
  173. // Just call the Text IM handler above
  174. // This event won't be raised unless we have that agent,
  175. // so we can depend on the above not trying to send
  176. // via grid again
  177. //
  178. OnInstantMessage(null, msg);
  179. }
  180. }
  181. }