MuteListServicesConnector.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 log4net;
  28. using System;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Reflection;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.ServiceAuth;
  35. using OpenSim.Services.Interfaces;
  36. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  37. using OpenSim.Server.Base;
  38. using OpenMetaverse;
  39. namespace OpenSim.Services.Connectors
  40. {
  41. public class MuteListServicesConnector : BaseServiceConnector, IMuteListService
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. private string m_ServerURI = String.Empty;
  45. public MuteListServicesConnector()
  46. {
  47. }
  48. public MuteListServicesConnector(string serverURI)
  49. {
  50. m_ServerURI = serverURI.TrimEnd('/') + "/mutelist";
  51. }
  52. public MuteListServicesConnector(IConfigSource source)
  53. {
  54. Initialise(source);
  55. }
  56. public virtual void Initialise(IConfigSource source)
  57. {
  58. IConfig gridConfig = source.Configs["MuteListService"];
  59. if (gridConfig == null)
  60. {
  61. m_log.Error("[MUTELIST CONNECTOR]: MuteListService missing from configuration");
  62. throw new Exception("MuteList connector init error");
  63. }
  64. string serviceURI = gridConfig.GetString("MuteListServerURI",
  65. String.Empty);
  66. if (serviceURI == String.Empty)
  67. {
  68. m_log.Error("[MUTELIST CONNECTOR]: No Server URI named in section GridUserService");
  69. throw new Exception("MuteList connector init error");
  70. }
  71. m_ServerURI = serviceURI + "/mutelist";;
  72. base.Initialise(source, "MuteListService");
  73. }
  74. #region IMuteListService
  75. public Byte[] MuteListRequest(UUID agentID, uint crc)
  76. {
  77. Dictionary<string, object> sendData = new Dictionary<string, object>();
  78. sendData["METHOD"] = "get";
  79. sendData["agentid"] = agentID.ToString();
  80. sendData["mutecrc"] = crc.ToString();
  81. try
  82. {
  83. string reply = SynchronousRestFormsRequester.MakeRequest("POST", m_ServerURI,
  84. ServerUtils.BuildQueryString(sendData), m_Auth);
  85. if (reply != string.Empty)
  86. {
  87. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  88. if (replyData.ContainsKey("result"))
  89. {
  90. string datastr = replyData["result"].ToString();
  91. if(String.IsNullOrWhiteSpace(datastr))
  92. return null;
  93. return Convert.FromBase64String(datastr);
  94. }
  95. else
  96. m_log.DebugFormat("[MUTELIST CONNECTOR]: get reply data does not contain result field");
  97. }
  98. else
  99. m_log.DebugFormat("[MUTELIST CONNECTOR]: get received empty reply");
  100. }
  101. catch (Exception e)
  102. {
  103. m_log.DebugFormat("[MUTELIST CONNECTOR]: Exception when contacting server at {0}: {1}", m_ServerURI, e.Message);
  104. }
  105. return null;
  106. }
  107. public bool UpdateMute(MuteData mute)
  108. {
  109. Dictionary<string, object> sendData = new Dictionary<string, object>();
  110. sendData["METHOD"] = "update";
  111. sendData["agentid"] = mute.AgentID.ToString();
  112. sendData["muteid"] = mute.MuteID.ToString();
  113. if(mute.MuteType != 0)
  114. sendData["mutetype"] = mute.MuteType.ToString();
  115. if(mute.MuteFlags != 0)
  116. sendData["muteflags"] = mute.MuteFlags.ToString();
  117. sendData["mutestamp"] = mute.Stamp.ToString();
  118. if(!String.IsNullOrEmpty(mute.MuteName))
  119. sendData["mutename"] = mute.MuteName;
  120. return doSimplePost(ServerUtils.BuildQueryString(sendData), "update");
  121. }
  122. public bool RemoveMute(UUID agentID, UUID muteID, string muteName)
  123. {
  124. Dictionary<string, object> sendData = new Dictionary<string, object>();
  125. sendData["METHOD"] = "delete";
  126. sendData["agentid"] = agentID.ToString();
  127. sendData["muteid"] = muteID.ToString();
  128. if(!String.IsNullOrEmpty(muteName))
  129. sendData["mutename"] = muteName;
  130. return doSimplePost(ServerUtils.BuildQueryString(sendData), "remove");
  131. }
  132. #endregion IMuteListService
  133. private bool doSimplePost(string reqString, string meth)
  134. {
  135. try
  136. {
  137. string reply = SynchronousRestFormsRequester.MakeRequest("POST", m_ServerURI, reqString, m_Auth);
  138. if (reply != string.Empty)
  139. {
  140. int indx = reply.IndexOf("success", StringComparison.InvariantCultureIgnoreCase);
  141. if (indx > 0)
  142. return true;
  143. return false;
  144. }
  145. else
  146. m_log.DebugFormat("[MUTELIST CONNECTOR]: {0} received empty reply", meth);
  147. }
  148. catch (Exception e)
  149. {
  150. m_log.DebugFormat("[MUTELIST CONNECTOR]: Exception when contacting server at {0}: {1}", m_ServerURI, e.Message);
  151. }
  152. return false;
  153. }
  154. }
  155. }