1
0

MuteListServerPostHandler.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 Nini.Config;
  28. using log4net;
  29. using System;
  30. using System.Reflection;
  31. using System.IO;
  32. using System.Net;
  33. using System.Text;
  34. using System.Text.RegularExpressions;
  35. using System.Xml;
  36. using System.Xml.Serialization;
  37. using System.Collections.Generic;
  38. using OpenSim.Server.Base;
  39. using OpenSim.Services.Interfaces;
  40. using OpenSim.Framework;
  41. using OpenSim.Framework.ServiceAuth;
  42. using OpenSim.Framework.Servers.HttpServer;
  43. using OpenMetaverse;
  44. namespace OpenSim.Server.Handlers.GridUser
  45. {
  46. public class MuteListServerPostHandler : BaseStreamHandler
  47. {
  48. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. private IMuteListService m_service;
  50. public MuteListServerPostHandler(IMuteListService service, IServiceAuth auth) :
  51. base("POST", "/mutelist", auth)
  52. {
  53. m_service = service;
  54. }
  55. protected override byte[] ProcessRequest(string path, Stream requestData,
  56. IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  57. {
  58. string body;
  59. using(StreamReader sr = new StreamReader(requestData))
  60. body = sr.ReadToEnd();
  61. body = body.Trim();
  62. //m_log.DebugFormat("[XXX]: query String: {0}", body);
  63. string method = string.Empty;
  64. try
  65. {
  66. Dictionary<string, object> request =
  67. ServerUtils.ParseQueryString(body);
  68. if (!request.ContainsKey("METHOD"))
  69. return FailureResult();
  70. method = request["METHOD"].ToString();
  71. switch (method)
  72. {
  73. case "get":
  74. return getmutes(request);
  75. case "update":
  76. return updatemute(request);
  77. case "delete":
  78. return deletemute(request);
  79. }
  80. m_log.DebugFormat("[MUTELIST HANDLER]: unknown method request: {0}", method);
  81. }
  82. catch (Exception e)
  83. {
  84. m_log.DebugFormat("[MUTELIST HANDLER]: Exception in method {0}: {1}", method, e);
  85. }
  86. return FailureResult();
  87. }
  88. byte[] getmutes(Dictionary<string, object> request)
  89. {
  90. if(!request.ContainsKey("agentid") || !request.ContainsKey("mutecrc"))
  91. return FailureResult();
  92. UUID agentID;
  93. if(!UUID.TryParse(request["agentid"].ToString(), out agentID))
  94. return FailureResult();
  95. uint mutecrc;
  96. if(!UInt32.TryParse(request["mutecrc"].ToString(), out mutecrc))
  97. return FailureResult();
  98. byte[] data = m_service.MuteListRequest(agentID, mutecrc);
  99. Dictionary<string, object> result = new Dictionary<string, object>();
  100. result["result"] = Convert.ToBase64String(data);
  101. string xmlString = ServerUtils.BuildXmlResponse(result);
  102. //m_log.DebugFormat("[GRID USER HANDLER]: resp string: {0}", xmlString);
  103. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  104. }
  105. byte[] updatemute(Dictionary<string, object> request)
  106. {
  107. if(!request.ContainsKey("agentid") || !request.ContainsKey("muteid"))
  108. return FailureResult();
  109. MuteData mute = new MuteData();
  110. if( !UUID.TryParse(request["agentid"].ToString(), out mute.AgentID))
  111. return FailureResult();
  112. if(!UUID.TryParse(request["muteid"].ToString(), out mute.MuteID))
  113. return FailureResult();
  114. if(request.ContainsKey("mutename"))
  115. {
  116. mute.MuteName = request["mutename"].ToString();
  117. }
  118. else
  119. mute.MuteName = String.Empty;
  120. if(request.ContainsKey("mutetype"))
  121. {
  122. if(!Int32.TryParse(request["mutetype"].ToString(), out mute.MuteType))
  123. return FailureResult();
  124. }
  125. else
  126. mute.MuteType = 0;
  127. if(request.ContainsKey("muteflags"))
  128. {
  129. if(!Int32.TryParse(request["muteflags"].ToString(), out mute.MuteFlags))
  130. return FailureResult();
  131. }
  132. else
  133. mute.MuteFlags = 0;
  134. if(request.ContainsKey("mutestamp"))
  135. {
  136. if(!Int32.TryParse(request["mutestamp"].ToString(), out mute.Stamp))
  137. return FailureResult();
  138. }
  139. else
  140. mute.Stamp = Util.UnixTimeSinceEpoch();
  141. return m_service.UpdateMute(mute) ? SuccessResult() : FailureResult();
  142. }
  143. byte[] deletemute(Dictionary<string, object> request)
  144. {
  145. if(!request.ContainsKey("agentid") || !request.ContainsKey("muteid"))
  146. return FailureResult();
  147. UUID agentID;
  148. if( !UUID.TryParse(request["agentid"].ToString(), out agentID))
  149. return FailureResult();
  150. UUID muteID;
  151. if(!UUID.TryParse(request["muteid"].ToString(), out muteID))
  152. return FailureResult();
  153. string muteName;
  154. if(request.ContainsKey("mutename"))
  155. {
  156. muteName = request["mutename"].ToString();
  157. }
  158. else
  159. muteName = String.Empty;
  160. return m_service.RemoveMute(agentID, muteID, muteName) ? SuccessResult() : FailureResult();
  161. }
  162. private byte[] SuccessResult()
  163. {
  164. XmlDocument doc = new XmlDocument();
  165. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  166. "", "");
  167. doc.AppendChild(xmlnode);
  168. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  169. "");
  170. doc.AppendChild(rootElement);
  171. XmlElement result = doc.CreateElement("", "result", "");
  172. result.AppendChild(doc.CreateTextNode("Success"));
  173. rootElement.AppendChild(result);
  174. return Util.DocToBytes(doc);
  175. }
  176. private byte[] FailureResult()
  177. {
  178. XmlDocument doc = new XmlDocument();
  179. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  180. "", "");
  181. doc.AppendChild(xmlnode);
  182. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  183. "");
  184. doc.AppendChild(rootElement);
  185. XmlElement result = doc.CreateElement("", "result", "");
  186. result.AppendChild(doc.CreateTextNode("Failure"));
  187. rootElement.AppendChild(result);
  188. return Util.DocToBytes(doc);
  189. }
  190. }
  191. }