OfflineIMRegionModule.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. client.OnMuteListRequest -= OnMuteListRequest;
  102. });
  103. }
  104. public void PostInitialise()
  105. {
  106. }
  107. public string Name
  108. {
  109. get { return "Offline Message Module V2"; }
  110. }
  111. public Type ReplaceableInterface
  112. {
  113. get { return null; }
  114. }
  115. public void Close()
  116. {
  117. m_SceneList.Clear();
  118. }
  119. private Scene FindScene(UUID agentID)
  120. {
  121. foreach (Scene s in m_SceneList)
  122. {
  123. ScenePresence presence = s.GetScenePresence(agentID);
  124. if (presence != null && !presence.IsChildAgent)
  125. return s;
  126. }
  127. return null;
  128. }
  129. private IClientAPI FindClient(UUID agentID)
  130. {
  131. foreach (Scene s in m_SceneList)
  132. {
  133. ScenePresence presence = s.GetScenePresence(agentID);
  134. if (presence != null && !presence.IsChildAgent)
  135. return presence.ControllingClient;
  136. }
  137. return null;
  138. }
  139. private void OnNewClient(IClientAPI client)
  140. {
  141. client.OnRetrieveInstantMessages += RetrieveInstantMessages;
  142. client.OnMuteListRequest += OnMuteListRequest;
  143. }
  144. private void RetrieveInstantMessages(IClientAPI client)
  145. {
  146. m_log.DebugFormat("[OfflineIM.V2]: Retrieving stored messages for {0}", client.AgentId);
  147. List<GridInstantMessage> msglist = m_OfflineIMService.GetMessages(client.AgentId);
  148. if (msglist == null)
  149. m_log.DebugFormat("[OfflineIM.V2]: WARNING null message list.");
  150. foreach (GridInstantMessage im in msglist)
  151. {
  152. if (im.dialog == (byte)InstantMessageDialog.InventoryOffered)
  153. // send it directly or else the item will be given twice
  154. client.SendInstantMessage(im);
  155. else
  156. {
  157. // Send through scene event manager so all modules get a chance
  158. // to look at this message before it gets delivered.
  159. //
  160. // Needed for proper state management for stored group
  161. // invitations
  162. //
  163. Scene s = FindScene(client.AgentId);
  164. if (s != null)
  165. s.EventManager.TriggerIncomingInstantMessage(im);
  166. }
  167. }
  168. }
  169. // Apparently this is needed in order for the viewer to request the IMs.
  170. private void OnMuteListRequest(IClientAPI client, uint crc)
  171. {
  172. m_log.DebugFormat("[OfflineIM.V2] Got mute list request for crc {0}", crc);
  173. string filename = "mutes" + client.AgentId.ToString();
  174. IXfer xfer = client.Scene.RequestModuleInterface<IXfer>();
  175. if (xfer != null)
  176. {
  177. xfer.AddNewFile(filename, new Byte[0]);
  178. client.SendMuteListUpdate(filename);
  179. }
  180. }
  181. private void UndeliveredMessage(GridInstantMessage im)
  182. {
  183. if (im.dialog != (byte)InstantMessageDialog.MessageFromObject &&
  184. im.dialog != (byte)InstantMessageDialog.MessageFromAgent &&
  185. im.dialog != (byte)InstantMessageDialog.GroupNotice &&
  186. im.dialog != (byte)InstantMessageDialog.GroupInvitation &&
  187. im.dialog != (byte)InstantMessageDialog.InventoryOffered)
  188. {
  189. return;
  190. }
  191. if (!m_ForwardOfflineGroupMessages)
  192. {
  193. if (im.dialog == (byte)InstantMessageDialog.GroupNotice ||
  194. im.dialog == (byte)InstantMessageDialog.GroupInvitation)
  195. return;
  196. }
  197. string reason = string.Empty;
  198. bool success = m_OfflineIMService.StoreMessage(im, out reason);
  199. if (im.dialog == (byte)InstantMessageDialog.MessageFromAgent)
  200. {
  201. IClientAPI client = FindClient(new UUID(im.fromAgentID));
  202. if (client == null)
  203. return;
  204. client.SendInstantMessage(new GridInstantMessage(
  205. null, new UUID(im.toAgentID),
  206. "System", new UUID(im.fromAgentID),
  207. (byte)InstantMessageDialog.MessageFromAgent,
  208. "User is not logged in. " +
  209. (success ? "Message saved." : "Message not saved: " + reason),
  210. false, new Vector3()));
  211. }
  212. }
  213. #region IOfflineIM
  214. public List<GridInstantMessage> GetMessages(UUID principalID)
  215. {
  216. return m_OfflineIMService.GetMessages(principalID);
  217. }
  218. public bool StoreMessage(GridInstantMessage im, out string reason)
  219. {
  220. return m_OfflineIMService.StoreMessage(im, out reason);
  221. }
  222. public void DeleteMessages(UUID userID)
  223. {
  224. m_OfflineIMService.DeleteMessages(userID);
  225. }
  226. #endregion
  227. }
  228. }