1
0

OfflineIMService.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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.IO;
  30. using System.Text;
  31. using System.Xml;
  32. using System.Xml.Serialization;
  33. using log4net;
  34. using Nini.Config;
  35. using OpenMetaverse;
  36. using OpenSim.Data;
  37. using OpenSim.Framework;
  38. using OpenSim.Services.Base;
  39. using OpenSim.Services.Interfaces;
  40. namespace OpenSim.OfflineIM
  41. {
  42. public class OfflineIMService : ServiceBase, IOfflineIMService
  43. {
  44. //private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. private IOfflineIMData m_Database = null;
  46. private int m_MaxOfflineIMs = 25;
  47. private XmlSerializer m_serializer;
  48. private static bool m_Initialized = false;
  49. public OfflineIMService(IConfigSource config) : base(config)
  50. {
  51. string dllName = string.Empty;
  52. string connString = string.Empty;
  53. string realm = "im_offline";
  54. //
  55. // Try reading the [DatabaseService] section, if it exists
  56. //
  57. IConfig dbConfig = config.Configs["DatabaseService"];
  58. if (dbConfig is not null)
  59. {
  60. if (dllName.Length == 0)
  61. dllName = dbConfig.GetString("StorageProvider", string.Empty);
  62. if (connString.Length == 0)
  63. connString = dbConfig.GetString("ConnectionString", string.Empty);
  64. }
  65. //
  66. // [Messaging] section overrides [DatabaseService], if it exists
  67. //
  68. IConfig imConfig = config.Configs["Messaging"];
  69. if (imConfig is not null)
  70. {
  71. dllName = imConfig.GetString("StorageProvider", dllName);
  72. connString = imConfig.GetString("ConnectionString", connString);
  73. realm = imConfig.GetString("Realm", realm);
  74. m_MaxOfflineIMs = imConfig.GetInt("MaxOfflineIMs", m_MaxOfflineIMs);
  75. }
  76. //
  77. // We tried, but this doesn't exist. We can't proceed.
  78. //
  79. if (string.IsNullOrEmpty(dllName))
  80. throw new Exception("No StorageProvider configured");
  81. m_Database = LoadPlugin<IOfflineIMData>(dllName, new Object[] { connString, realm });
  82. if (m_Database is null)
  83. throw new Exception("Could not find a storage interface in the given module " + dllName);
  84. m_serializer = new XmlSerializer(typeof(GridInstantMessage));
  85. if (!m_Initialized)
  86. {
  87. m_Database.DeleteOld();
  88. m_Initialized = true;
  89. }
  90. }
  91. public List<GridInstantMessage> GetMessages(UUID principalID)
  92. {
  93. List<GridInstantMessage> ims = new List<GridInstantMessage>();
  94. OfflineIMData[] messages = m_Database.Get("PrincipalID", principalID.ToString());
  95. if (messages is null || messages.Length == 0)
  96. return ims;
  97. foreach (OfflineIMData m in messages)
  98. {
  99. using (MemoryStream mstream = new MemoryStream(Encoding.UTF8.GetBytes(m.Data["Message"])))
  100. {
  101. GridInstantMessage im = (GridInstantMessage)m_serializer.Deserialize(mstream);
  102. ims.Add(im);
  103. }
  104. }
  105. // Then, delete them
  106. m_Database.Delete("PrincipalID", principalID.ToString());
  107. return ims;
  108. }
  109. public bool StoreMessage(GridInstantMessage im, out string reason)
  110. {
  111. reason = string.Empty;
  112. // Check limits
  113. UUID principalID = new UUID(im.toAgentID);
  114. long count = m_Database.GetCount("PrincipalID", principalID.ToString());
  115. if (count >= m_MaxOfflineIMs)
  116. {
  117. reason = "Number of offline IMs has maxed out";
  118. return false;
  119. }
  120. string imXml;
  121. using (MemoryStream mstream = new MemoryStream())
  122. {
  123. XmlWriterSettings settings = new XmlWriterSettings();
  124. settings.Encoding = Util.UTF8NoBomEncoding;
  125. using (XmlWriter writer = XmlWriter.Create(mstream, settings))
  126. {
  127. m_serializer.Serialize(writer, im);
  128. writer.Flush();
  129. imXml = Util.UTF8NoBomEncoding.GetString(mstream.ToArray());
  130. }
  131. }
  132. OfflineIMData data = new OfflineIMData();
  133. data.PrincipalID = principalID;
  134. data.FromID = new UUID(im.fromAgentID);
  135. data.Data = new Dictionary<string, string>();
  136. data.Data["Message"] = imXml;
  137. return m_Database.Store(data);
  138. }
  139. public void DeleteMessages(UUID userID)
  140. {
  141. m_Database.Delete("PrincipalID", userID.ToString());
  142. m_Database.Delete("FromID", userID.ToString());
  143. }
  144. }
  145. }