InstantMessageModule.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. byte dialog = im.dialog;
  127. if (dialog != (byte)InstantMessageDialog.MessageFromAgent
  128. && dialog != (byte)InstantMessageDialog.StartTyping
  129. && dialog != (byte)InstantMessageDialog.StopTyping
  130. && dialog != (byte)InstantMessageDialog.BusyAutoResponse
  131. && dialog != (byte)InstantMessageDialog.MessageFromObject)
  132. {
  133. return;
  134. }
  135. //DateTime dt = DateTime.UtcNow;
  136. // Ticks from UtcNow, but make it look like local. Evil, huh?
  137. //dt = DateTime.SpecifyKind(dt, DateTimeKind.Local);
  138. //try
  139. //{
  140. // // Convert that to the PST timezone
  141. // TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("America/Los_Angeles");
  142. // dt = TimeZoneInfo.ConvertTime(dt, timeZoneInfo);
  143. //}
  144. //catch
  145. //{
  146. // //m_log.Info("[OFFLINE MESSAGING]: No PST timezone found on this machine. Saving with local timestamp.");
  147. //}
  148. //// And make it look local again to fool the unix time util
  149. //dt = DateTime.SpecifyKind(dt, DateTimeKind.Utc);
  150. // If client is null, this message comes from storage and IS offline
  151. if (client != null)
  152. im.offline = 0;
  153. if (im.offline == 0)
  154. im.timestamp = (uint)Util.UnixTimeSinceEpoch();
  155. if (m_TransferModule != null)
  156. {
  157. m_TransferModule.SendInstantMessage(im,
  158. delegate(bool success)
  159. {
  160. if (dialog == (uint)InstantMessageDialog.StartTyping ||
  161. dialog == (uint)InstantMessageDialog.StopTyping ||
  162. dialog == (uint)InstantMessageDialog.MessageFromObject)
  163. {
  164. return;
  165. }
  166. if ((client != null) && !success)
  167. {
  168. client.SendInstantMessage(
  169. new GridInstantMessage(
  170. null, new UUID(im.fromAgentID), "System",
  171. new UUID(im.toAgentID),
  172. (byte)InstantMessageDialog.BusyAutoResponse,
  173. "Unable to send instant message. "+
  174. "User is not logged in.", false,
  175. new Vector3()));
  176. }
  177. }
  178. );
  179. }
  180. }
  181. /// <summary>
  182. ///
  183. /// </summary>
  184. /// <param name="msg"></param>
  185. protected virtual void OnGridInstantMessage(GridInstantMessage msg)
  186. {
  187. // Just call the Text IM handler above
  188. // This event won't be raised unless we have that agent,
  189. // so we can depend on the above not trying to send
  190. // via grid again
  191. //
  192. OnInstantMessage(null, msg);
  193. }
  194. }
  195. }