OfflineMessageModule.cs 9.0 KB

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