OfflineMessageModule.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. namespace OpenSim.Region.CoreModules.Avatar.InstantMessage
  40. {
  41. public struct SendReply
  42. {
  43. public bool Success;
  44. public string Message;
  45. public int Disposition;
  46. }
  47. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "OfflineMessageModule")]
  48. public class OfflineMessageModule : ISharedRegionModule
  49. {
  50. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  51. private bool enabled = true;
  52. private bool m_UseNewAvnCode = false;
  53. private List<Scene> m_SceneList = new List<Scene>();
  54. private string m_RestURL = String.Empty;
  55. IMessageTransferModule m_TransferModule = null;
  56. private bool m_ForwardOfflineGroupMessages = true;
  57. private Dictionary<IClientAPI, List<UUID>> m_repliesSent= new Dictionary<IClientAPI, List<UUID>>();
  58. public void Initialise(IConfigSource config)
  59. {
  60. IConfig cnf = config.Configs["Messaging"];
  61. if (cnf == null)
  62. {
  63. enabled = false;
  64. return;
  65. }
  66. if (cnf != null && cnf.GetString("OfflineMessageModule", "None") !=
  67. "OfflineMessageModule")
  68. {
  69. enabled = false;
  70. return;
  71. }
  72. m_RestURL = cnf.GetString("OfflineMessageURL", "");
  73. if (m_RestURL == "")
  74. {
  75. m_log.Error("[OFFLINE MESSAGING] Module was enabled, but no URL is given, disabling");
  76. enabled = false;
  77. return;
  78. }
  79. m_ForwardOfflineGroupMessages = cnf.GetBoolean("ForwardOfflineGroupMessages", m_ForwardOfflineGroupMessages);
  80. m_UseNewAvnCode = cnf.GetBoolean("UseNewAvnCode", m_UseNewAvnCode);
  81. }
  82. public void AddRegion(Scene scene)
  83. {
  84. if (!enabled)
  85. return;
  86. lock (m_SceneList)
  87. {
  88. m_SceneList.Add(scene);
  89. scene.EventManager.OnNewClient += OnNewClient;
  90. }
  91. }
  92. public void RegionLoaded(Scene scene)
  93. {
  94. if (!enabled)
  95. return;
  96. if (m_TransferModule == null)
  97. {
  98. m_TransferModule = scene.RequestModuleInterface<IMessageTransferModule>();
  99. if (m_TransferModule == null)
  100. {
  101. scene.EventManager.OnNewClient -= OnNewClient;
  102. enabled = false;
  103. m_SceneList.Clear();
  104. m_log.Error("[OFFLINE MESSAGING] No message transfer module is enabled. Diabling offline messages");
  105. }
  106. m_TransferModule.OnUndeliveredMessage += UndeliveredMessage;
  107. }
  108. }
  109. public void RemoveRegion(Scene scene)
  110. {
  111. if (!enabled)
  112. return;
  113. lock (m_SceneList)
  114. {
  115. m_SceneList.Remove(scene);
  116. }
  117. }
  118. public void PostInitialise()
  119. {
  120. if (!enabled)
  121. return;
  122. m_log.Debug("[OFFLINE MESSAGING] Offline messages enabled");
  123. }
  124. public string Name
  125. {
  126. get { return "OfflineMessageModule"; }
  127. }
  128. public Type ReplaceableInterface
  129. {
  130. get { return null; }
  131. }
  132. public void Close()
  133. {
  134. }
  135. private Scene FindScene(UUID agentID)
  136. {
  137. foreach (Scene s in m_SceneList)
  138. {
  139. ScenePresence presence = s.GetScenePresence(agentID);
  140. if (presence != null && !presence.IsChildAgent)
  141. return s;
  142. }
  143. return null;
  144. }
  145. private IClientAPI FindClient(UUID agentID)
  146. {
  147. foreach (Scene s in m_SceneList)
  148. {
  149. ScenePresence presence = s.GetScenePresence(agentID);
  150. if (presence != null && !presence.IsChildAgent)
  151. return presence.ControllingClient;
  152. }
  153. return null;
  154. }
  155. private void OnNewClient(IClientAPI client)
  156. {
  157. client.OnRetrieveInstantMessages += RetrieveInstantMessages;
  158. client.OnLogout += OnClientLoggedOut;
  159. }
  160. public void OnClientLoggedOut(IClientAPI client)
  161. {
  162. m_repliesSent.Remove(client);
  163. }
  164. private void RetrieveInstantMessages(IClientAPI client)
  165. {
  166. if (m_RestURL == String.Empty)
  167. {
  168. return;
  169. }
  170. else
  171. {
  172. m_log.DebugFormat("[OFFLINE MESSAGING]: Retrieving stored messages for {0}", client.AgentId);
  173. List<GridInstantMessage> msglist
  174. = SynchronousRestObjectRequester.MakeRequest<UUID, List<GridInstantMessage>>(
  175. "POST", m_RestURL + "/RetrieveMessages/", client.AgentId);
  176. if (msglist != null)
  177. {
  178. foreach (GridInstantMessage im in msglist)
  179. {
  180. if (im.dialog == (byte)InstantMessageDialog.InventoryOffered)
  181. // send it directly or else the item will be given twice
  182. client.SendInstantMessage(im);
  183. else
  184. {
  185. // Send through scene event manager so all modules get a chance
  186. // to look at this message before it gets delivered.
  187. //
  188. // Needed for proper state management for stored group
  189. // invitations
  190. //
  191. im.offline = 1;
  192. Scene s = FindScene(client.AgentId);
  193. if (s != null)
  194. s.EventManager.TriggerIncomingInstantMessage(im);
  195. }
  196. }
  197. }
  198. }
  199. }
  200. private void UndeliveredMessage(GridInstantMessage im)
  201. {
  202. if (im.dialog != (byte)InstantMessageDialog.MessageFromObject &&
  203. im.dialog != (byte)InstantMessageDialog.MessageFromAgent &&
  204. im.dialog != (byte)InstantMessageDialog.GroupNotice &&
  205. im.dialog != (byte)InstantMessageDialog.GroupInvitation &&
  206. im.dialog != (byte)InstantMessageDialog.InventoryOffered &&
  207. im.dialog != (byte)InstantMessageDialog.TaskInventoryOffered)
  208. {
  209. return;
  210. }
  211. if (!m_ForwardOfflineGroupMessages)
  212. {
  213. if (im.dialog == (byte)InstantMessageDialog.GroupNotice ||
  214. im.dialog == (byte)InstantMessageDialog.GroupInvitation)
  215. return;
  216. }
  217. if(m_UseNewAvnCode)
  218. {
  219. Scene scene = FindScene(new UUID(im.fromAgentID));
  220. if (scene == null)
  221. scene = m_SceneList[0];
  222. UUID scopeID = scene.RegionInfo.ScopeID;
  223. SendReply reply = SynchronousRestObjectRequester.MakeRequest<GridInstantMessage, SendReply>(
  224. "POST", m_RestURL+"/SaveMessage/?scope=" + scopeID.ToString(), im, 20000);
  225. if (im.dialog == (byte)InstantMessageDialog.MessageFromAgent)
  226. {
  227. IClientAPI client = FindClient(new UUID(im.fromAgentID));
  228. if (client == null)
  229. return;
  230. if (string.IsNullOrEmpty(reply.Message))
  231. reply.Message = "User is not logged in. " + (reply.Success ? "Message saved." : "Message not saved");
  232. bool sendReply = true;
  233. switch (reply.Disposition)
  234. {
  235. case 0: // Normal
  236. break;
  237. case 1: // Only once per user
  238. if (m_repliesSent.ContainsKey(client) && m_repliesSent[client].Contains(new UUID(im.toAgentID)))
  239. sendReply = false;
  240. else
  241. {
  242. if (!m_repliesSent.ContainsKey(client))
  243. m_repliesSent[client] = new List<UUID>();
  244. m_repliesSent[client].Add(new UUID(im.toAgentID));
  245. }
  246. break;
  247. }
  248. if (sendReply)
  249. {
  250. client.SendInstantMessage(new GridInstantMessage(
  251. null, new UUID(im.toAgentID),
  252. "System", new UUID(im.fromAgentID),
  253. (byte)InstantMessageDialog.MessageFromAgent,
  254. reply.Message,
  255. false, new Vector3()));
  256. }
  257. }
  258. }
  259. else
  260. {
  261. bool success = SynchronousRestObjectRequester.MakeRequest<GridInstantMessage, bool>(
  262. "POST", m_RestURL+"/SaveMessage/", im, 20000);
  263. if (im.dialog == (byte)InstantMessageDialog.MessageFromAgent)
  264. {
  265. IClientAPI client = FindClient(new UUID(im.fromAgentID));
  266. if (client == null)
  267. return;
  268. client.SendInstantMessage(new GridInstantMessage(
  269. null, new UUID(im.toAgentID),
  270. "System", new UUID(im.fromAgentID),
  271. (byte)InstantMessageDialog.MessageFromAgent,
  272. "User is not logged in. "+
  273. (success ? "Message saved." : "Message not saved"),
  274. false, new Vector3()));
  275. }
  276. }
  277. }
  278. }
  279. }