InstantMessageModule.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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.Framework.Client;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Region.Framework.Scenes;
  39. namespace OpenSim.Region.CoreModules.Avatar.InstantMessage
  40. {
  41. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "InstantMessageModule")]
  42. public class InstantMessageModule : ISharedRegionModule
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger(
  45. MethodBase.GetCurrentMethod().DeclaringType);
  46. /// <value>
  47. /// Is this module enabled?
  48. /// </value>
  49. protected bool m_enabled = false;
  50. protected readonly List<Scene> m_scenes = new List<Scene>();
  51. #region Region Module interface
  52. protected IMessageTransferModule m_TransferModule = null;
  53. public virtual void Initialise(IConfigSource config)
  54. {
  55. if (config.Configs["Messaging"] != null)
  56. {
  57. if (config.Configs["Messaging"].GetString(
  58. "InstantMessageModule", "InstantMessageModule") !=
  59. "InstantMessageModule")
  60. return;
  61. }
  62. m_enabled = true;
  63. }
  64. public virtual void AddRegion(Scene scene)
  65. {
  66. if (!m_enabled)
  67. return;
  68. lock (m_scenes)
  69. {
  70. if (!m_scenes.Contains(scene))
  71. {
  72. m_scenes.Add(scene);
  73. scene.EventManager.OnClientConnect += OnClientConnect;
  74. scene.EventManager.OnIncomingInstantMessage += OnGridInstantMessage;
  75. }
  76. }
  77. }
  78. public virtual void RegionLoaded(Scene scene)
  79. {
  80. if (!m_enabled)
  81. return;
  82. if (m_TransferModule == null)
  83. {
  84. m_TransferModule =
  85. scene.RequestModuleInterface<IMessageTransferModule>();
  86. if (m_TransferModule == null)
  87. {
  88. m_log.Error("[INSTANT MESSAGE]: No message transfer module, IM will not work!");
  89. scene.EventManager.OnClientConnect -= OnClientConnect;
  90. scene.EventManager.OnIncomingInstantMessage -= OnGridInstantMessage;
  91. m_scenes.Clear();
  92. m_enabled = false;
  93. }
  94. }
  95. }
  96. public virtual void RemoveRegion(Scene scene)
  97. {
  98. if (!m_enabled)
  99. return;
  100. lock (m_scenes)
  101. {
  102. m_scenes.Remove(scene);
  103. }
  104. }
  105. protected virtual void OnClientConnect(IClientCore client)
  106. {
  107. IClientIM clientIM;
  108. if (client.TryGet(out clientIM))
  109. {
  110. clientIM.OnInstantMessage += OnInstantMessage;
  111. }
  112. }
  113. public virtual void PostInitialise()
  114. {
  115. }
  116. public virtual void Close()
  117. {
  118. }
  119. public virtual string Name
  120. {
  121. get { return "InstantMessageModule"; }
  122. }
  123. public virtual Type ReplaceableInterface
  124. {
  125. get { return null; }
  126. }
  127. #endregion
  128. /*
  129. public virtual void OnViewerInstantMessage(IClientAPI client, GridInstantMessage im)
  130. {
  131. im.fromAgentName = client.FirstName + " " + client.LastName;
  132. OnInstantMessage(client, im);
  133. }
  134. */
  135. public virtual void OnInstantMessage(IClientAPI client, GridInstantMessage im)
  136. {
  137. byte dialog = im.dialog;
  138. if (dialog != (byte)InstantMessageDialog.MessageFromAgent
  139. && dialog != (byte)InstantMessageDialog.StartTyping
  140. && dialog != (byte)InstantMessageDialog.StopTyping
  141. && dialog != (byte)InstantMessageDialog.BusyAutoResponse
  142. && dialog != (byte)InstantMessageDialog.MessageFromObject)
  143. {
  144. return;
  145. }
  146. //DateTime dt = DateTime.UtcNow;
  147. // Ticks from UtcNow, but make it look like local. Evil, huh?
  148. //dt = DateTime.SpecifyKind(dt, DateTimeKind.Local);
  149. //try
  150. //{
  151. // // Convert that to the PST timezone
  152. // TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("America/Los_Angeles");
  153. // dt = TimeZoneInfo.ConvertTime(dt, timeZoneInfo);
  154. //}
  155. //catch
  156. //{
  157. // //m_log.Info("[OFFLINE MESSAGING]: No PST timezone found on this machine. Saving with local timestamp.");
  158. //}
  159. //// And make it look local again to fool the unix time util
  160. //dt = DateTime.SpecifyKind(dt, DateTimeKind.Utc);
  161. // If client is null, this message comes from storage and IS offline
  162. if (client != null)
  163. im.offline = 0;
  164. if (im.offline == 0)
  165. im.timestamp = (uint)Util.UnixTimeSinceEpoch();
  166. if (m_TransferModule != null)
  167. {
  168. m_TransferModule.SendInstantMessage(im,
  169. delegate(bool success)
  170. {
  171. if (dialog == (uint)InstantMessageDialog.StartTyping ||
  172. dialog == (uint)InstantMessageDialog.StopTyping ||
  173. dialog == (uint)InstantMessageDialog.MessageFromObject)
  174. {
  175. return;
  176. }
  177. if ((client != null) && !success)
  178. {
  179. client.SendInstantMessage(
  180. new GridInstantMessage(
  181. null, new UUID(im.fromAgentID), "System",
  182. new UUID(im.toAgentID),
  183. (byte)InstantMessageDialog.BusyAutoResponse,
  184. "Unable to send instant message. "+
  185. "User is not logged in.", false,
  186. new Vector3()));
  187. }
  188. }
  189. );
  190. }
  191. }
  192. /// <summary>
  193. ///
  194. /// </summary>
  195. /// <param name="msg"></param>
  196. protected virtual void OnGridInstantMessage(GridInstantMessage msg)
  197. {
  198. // Just call the Text IM handler above
  199. // This event won't be raised unless we have that agent,
  200. // so we can depend on the above not trying to send
  201. // via grid again
  202. //
  203. OnInstantMessage(null, msg);
  204. }
  205. }
  206. }