OfflineIMService.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.Reflection;
  31. using System.Runtime.Serialization;
  32. using System.Text;
  33. using System.Timers;
  34. using System.Xml;
  35. using System.Xml.Serialization;
  36. using log4net;
  37. using Nini.Config;
  38. using OpenMetaverse;
  39. using OpenSim.Data;
  40. using OpenSim.Framework;
  41. using OpenSim.Services.Interfaces;
  42. namespace OpenSim.OfflineIM
  43. {
  44. public class OfflineIMService : OfflineIMServiceBase, IOfflineIMService
  45. {
  46. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. private const int MAX_IM = 25;
  48. private XmlSerializer m_serializer;
  49. private static bool m_Initialized = false;
  50. public OfflineIMService(IConfigSource config)
  51. : base(config)
  52. {
  53. m_serializer = new XmlSerializer(typeof(GridInstantMessage));
  54. if (!m_Initialized)
  55. {
  56. m_Database.DeleteOld();
  57. m_Initialized = true;
  58. }
  59. }
  60. public List<GridInstantMessage> GetMessages(UUID principalID)
  61. {
  62. List<GridInstantMessage> ims = new List<GridInstantMessage>();
  63. OfflineIMData[] messages = m_Database.Get("PrincipalID", principalID.ToString());
  64. if (messages == null || (messages != null && messages.Length == 0))
  65. return ims;
  66. foreach (OfflineIMData m in messages)
  67. {
  68. using (MemoryStream mstream = new MemoryStream(Encoding.UTF8.GetBytes(m.Data["Message"])))
  69. {
  70. GridInstantMessage im = (GridInstantMessage)m_serializer.Deserialize(mstream);
  71. ims.Add(im);
  72. }
  73. }
  74. // Then, delete them
  75. m_Database.Delete("PrincipalID", principalID.ToString());
  76. return ims;
  77. }
  78. public bool StoreMessage(GridInstantMessage im, out string reason)
  79. {
  80. reason = string.Empty;
  81. // Check limits
  82. UUID principalID = new UUID(im.toAgentID);
  83. long count = m_Database.GetCount("PrincipalID", principalID.ToString());
  84. if (count >= MAX_IM)
  85. {
  86. reason = "Number of offline IMs has maxed out";
  87. return false;
  88. }
  89. string imXml;
  90. using (MemoryStream mstream = new MemoryStream())
  91. {
  92. XmlWriterSettings settings = new XmlWriterSettings();
  93. settings.Encoding = Util.UTF8NoBomEncoding;
  94. using (XmlWriter writer = XmlWriter.Create(mstream, settings))
  95. {
  96. m_serializer.Serialize(writer, im);
  97. writer.Flush();
  98. imXml = Util.UTF8NoBomEncoding.GetString(mstream.ToArray());
  99. }
  100. }
  101. OfflineIMData data = new OfflineIMData();
  102. data.PrincipalID = principalID;
  103. data.FromID = new UUID(im.fromAgentID);
  104. data.Data = new Dictionary<string, string>();
  105. data.Data["Message"] = imXml;
  106. return m_Database.Store(data);
  107. }
  108. public void DeleteMessages(UUID userID)
  109. {
  110. m_Database.Delete("PrincipalID", userID.ToString());
  111. m_Database.Delete("FromID", userID.ToString());
  112. }
  113. }
  114. }