OfflineIMRegionModule.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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.Servers;
  36. using OpenSim.Framework.Client;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Region.Framework.Scenes;
  39. using OpenSim.Services.Interfaces;
  40. namespace OpenSim.OfflineIM
  41. {
  42. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "OfflineIMConnectorModule")]
  43. public class OfflineIMRegionModule : ISharedRegionModule, IOfflineIMService
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. private bool m_Enabled = false;
  47. private List<Scene> m_SceneList = new List<Scene>();
  48. IMessageTransferModule m_TransferModule = null;
  49. private bool m_ForwardOfflineGroupMessages = true;
  50. private IOfflineIMService m_OfflineIMService;
  51. public void Initialise(IConfigSource config)
  52. {
  53. IConfig cnf = config.Configs["Messaging"];
  54. if (cnf == null)
  55. return;
  56. if (cnf != null && cnf.GetString("OfflineMessageModule", string.Empty) != Name)
  57. return;
  58. m_Enabled = true;
  59. string serviceLocation = cnf.GetString("OfflineMessageURL", string.Empty);
  60. if (serviceLocation == string.Empty)
  61. m_OfflineIMService = new OfflineIMService(config);
  62. else
  63. m_OfflineIMService = new OfflineIMServiceRemoteConnector(config);
  64. m_ForwardOfflineGroupMessages = cnf.GetBoolean("ForwardOfflineGroupMessages", m_ForwardOfflineGroupMessages);
  65. m_log.DebugFormat("[OfflineIM.V2]: Offline messages enabled by {0}", Name);
  66. }
  67. public void AddRegion(Scene scene)
  68. {
  69. if (!m_Enabled)
  70. return;
  71. scene.RegisterModuleInterface<IOfflineIMService>(this);
  72. m_SceneList.Add(scene);
  73. scene.EventManager.OnNewClient += OnNewClient;
  74. }
  75. public void RegionLoaded(Scene scene)
  76. {
  77. if (!m_Enabled)
  78. return;
  79. if (m_TransferModule == null)
  80. {
  81. m_TransferModule = scene.RequestModuleInterface<IMessageTransferModule>();
  82. if (m_TransferModule == null)
  83. {
  84. scene.EventManager.OnNewClient -= OnNewClient;
  85. m_SceneList.Clear();
  86. m_log.Error("[OfflineIM.V2]: No message transfer module is enabled. Disabling offline messages");
  87. }
  88. m_TransferModule.OnUndeliveredMessage += UndeliveredMessage;
  89. }
  90. }
  91. public void RemoveRegion(Scene scene)
  92. {
  93. if (!m_Enabled)
  94. return;
  95. m_SceneList.Remove(scene);
  96. scene.EventManager.OnNewClient -= OnNewClient;
  97. m_TransferModule.OnUndeliveredMessage -= UndeliveredMessage;
  98. scene.ForEachClient(delegate(IClientAPI client)
  99. {
  100. client.OnRetrieveInstantMessages -= RetrieveInstantMessages;
  101. });
  102. }
  103. public void PostInitialise()
  104. {
  105. }
  106. public string Name
  107. {
  108. get { return "Offline Message Module V2"; }
  109. }
  110. public Type ReplaceableInterface
  111. {
  112. get { return null; }
  113. }
  114. public void Close()
  115. {
  116. m_SceneList.Clear();
  117. }
  118. private Scene FindScene(UUID agentID)
  119. {
  120. foreach (Scene s in m_SceneList)
  121. {
  122. ScenePresence presence = s.GetScenePresence(agentID);
  123. if (presence != null && !presence.IsChildAgent)
  124. return s;
  125. }
  126. return null;
  127. }
  128. private IClientAPI FindClient(UUID agentID)
  129. {
  130. foreach (Scene s in m_SceneList)
  131. {
  132. ScenePresence presence = s.GetScenePresence(agentID);
  133. if (presence != null && !presence.IsChildAgent)
  134. return presence.ControllingClient;
  135. }
  136. return null;
  137. }
  138. private void OnNewClient(IClientAPI client)
  139. {
  140. client.OnRetrieveInstantMessages += RetrieveInstantMessages;
  141. }
  142. private void RetrieveInstantMessages(IClientAPI client)
  143. {
  144. m_log.DebugFormat("[OfflineIM.V2]: Retrieving stored messages for {0}", client.AgentId);
  145. List<GridInstantMessage> msglist = m_OfflineIMService.GetMessages(client.AgentId);
  146. if (msglist == null)
  147. m_log.DebugFormat("[OfflineIM.V2]: WARNING null message list.");
  148. foreach (GridInstantMessage im in msglist)
  149. {
  150. if (im.dialog == (byte)InstantMessageDialog.InventoryOffered)
  151. // send it directly or else the item will be given twice
  152. client.SendInstantMessage(im);
  153. else
  154. {
  155. // Send through scene event manager so all modules get a chance
  156. // to look at this message before it gets delivered.
  157. //
  158. // Needed for proper state management for stored group
  159. // invitations
  160. //
  161. Scene s = FindScene(client.AgentId);
  162. if (s != null)
  163. s.EventManager.TriggerIncomingInstantMessage(im);
  164. }
  165. }
  166. }
  167. private void UndeliveredMessage(GridInstantMessage im)
  168. {
  169. if (im.dialog != (byte)InstantMessageDialog.MessageFromObject &&
  170. im.dialog != (byte)InstantMessageDialog.MessageFromAgent &&
  171. im.dialog != (byte)InstantMessageDialog.GroupNotice &&
  172. im.dialog != (byte)InstantMessageDialog.GroupInvitation &&
  173. im.dialog != (byte)InstantMessageDialog.InventoryOffered)
  174. {
  175. return;
  176. }
  177. if (!m_ForwardOfflineGroupMessages)
  178. {
  179. if (im.dialog == (byte)InstantMessageDialog.GroupNotice ||
  180. im.dialog == (byte)InstantMessageDialog.GroupInvitation)
  181. return;
  182. }
  183. string reason = string.Empty;
  184. bool success = m_OfflineIMService.StoreMessage(im, out reason);
  185. if (im.dialog == (byte)InstantMessageDialog.MessageFromAgent)
  186. {
  187. IClientAPI client = FindClient(new UUID(im.fromAgentID));
  188. if (client == null)
  189. return;
  190. client.SendInstantMessage(new GridInstantMessage(
  191. null, new UUID(im.toAgentID),
  192. "System", new UUID(im.fromAgentID),
  193. (byte)InstantMessageDialog.MessageFromAgent,
  194. "User is not logged in. " +
  195. (success ? "Message saved." : "Message not saved: " + reason),
  196. false, new Vector3()));
  197. }
  198. }
  199. #region IOfflineIM
  200. public List<GridInstantMessage> GetMessages(UUID principalID)
  201. {
  202. return m_OfflineIMService.GetMessages(principalID);
  203. }
  204. public bool StoreMessage(GridInstantMessage im, out string reason)
  205. {
  206. return m_OfflineIMService.StoreMessage(im, out reason);
  207. }
  208. public void DeleteMessages(UUID userID)
  209. {
  210. m_OfflineIMService.DeleteMessages(userID);
  211. }
  212. #endregion
  213. }
  214. }