XEstateRequestHandler.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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.Xml;
  32. using OpenSim.Framework;
  33. using OpenSim.Server.Base;
  34. using OpenSim.Framework.Servers.HttpServer;
  35. using OpenSim.Region.Framework.Scenes;
  36. using OpenSim.Region.Framework.Interfaces;
  37. using OpenMetaverse;
  38. using log4net;
  39. namespace OpenSim.Region.CoreModules.World.Estate
  40. {
  41. public class EstateRequestHandler : BaseStreamHandler
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. protected XEstateModule m_EstateModule;
  45. protected Object m_RequestLock = new Object();
  46. public EstateRequestHandler(XEstateModule fmodule)
  47. : base("POST", "/estate")
  48. {
  49. m_EstateModule = fmodule;
  50. }
  51. protected override byte[] ProcessRequest(string path, Stream requestData,
  52. IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  53. {
  54. StreamReader sr = new StreamReader(requestData);
  55. string body = sr.ReadToEnd();
  56. sr.Close();
  57. body = body.Trim();
  58. m_log.DebugFormat("[XESTATE HANDLER]: query String: {0}", body);
  59. try
  60. {
  61. lock (m_RequestLock)
  62. {
  63. Dictionary<string, object> request =
  64. ServerUtils.ParseQueryString(body);
  65. if (!request.ContainsKey("METHOD"))
  66. return FailureResult();
  67. string method = request["METHOD"].ToString();
  68. request.Remove("METHOD");
  69. try
  70. {
  71. m_EstateModule.InInfoUpdate = false;
  72. switch (method)
  73. {
  74. case "update_covenant":
  75. return UpdateCovenant(request);
  76. case "update_estate":
  77. return UpdateEstate(request);
  78. case "estate_message":
  79. return EstateMessage(request);
  80. case "teleport_home_one_user":
  81. return TeleportHomeOneUser(request);
  82. case "teleport_home_all_users":
  83. return TeleportHomeAllUsers(request);
  84. }
  85. }
  86. finally
  87. {
  88. m_EstateModule.InInfoUpdate = false;
  89. }
  90. }
  91. }
  92. catch (Exception e)
  93. {
  94. m_log.Debug("[XESTATE]: Exception {0}" + e.ToString());
  95. }
  96. return FailureResult();
  97. }
  98. byte[] TeleportHomeAllUsers(Dictionary<string, object> request)
  99. {
  100. UUID PreyID = UUID.Zero;
  101. int EstateID = 0;
  102. if (!request.ContainsKey("EstateID"))
  103. return FailureResult();
  104. if (!Int32.TryParse(request["EstateID"].ToString(), out EstateID))
  105. return FailureResult();
  106. foreach (Scene s in m_EstateModule.Scenes)
  107. {
  108. if (s.RegionInfo.EstateSettings.EstateID == EstateID)
  109. {
  110. s.ForEachScenePresence(delegate(ScenePresence p) {
  111. if (p != null && !p.IsChildAgent)
  112. {
  113. p.ControllingClient.SendTeleportStart(16);
  114. s.TeleportClientHome(p.ControllingClient.AgentId, p.ControllingClient);
  115. }
  116. });
  117. }
  118. }
  119. return SuccessResult();
  120. }
  121. byte[] TeleportHomeOneUser(Dictionary<string, object> request)
  122. {
  123. UUID PreyID = UUID.Zero;
  124. int EstateID = 0;
  125. if (!request.ContainsKey("PreyID") ||
  126. !request.ContainsKey("EstateID"))
  127. {
  128. return FailureResult();
  129. }
  130. if (!UUID.TryParse(request["PreyID"].ToString(), out PreyID))
  131. return FailureResult();
  132. if (!Int32.TryParse(request["EstateID"].ToString(), out EstateID))
  133. return FailureResult();
  134. foreach (Scene s in m_EstateModule.Scenes)
  135. {
  136. if (s.RegionInfo.EstateSettings.EstateID == EstateID)
  137. {
  138. ScenePresence p = s.GetScenePresence(PreyID);
  139. if (p != null && !p.IsChildAgent)
  140. {
  141. p.ControllingClient.SendTeleportStart(16);
  142. s.TeleportClientHome(PreyID, p.ControllingClient);
  143. }
  144. }
  145. }
  146. return SuccessResult();
  147. }
  148. byte[] EstateMessage(Dictionary<string, object> request)
  149. {
  150. UUID FromID = UUID.Zero;
  151. string FromName = String.Empty;
  152. string Message = String.Empty;
  153. int EstateID = 0;
  154. if (!request.ContainsKey("FromID") ||
  155. !request.ContainsKey("FromName") ||
  156. !request.ContainsKey("Message") ||
  157. !request.ContainsKey("EstateID"))
  158. {
  159. return FailureResult();
  160. }
  161. if (!UUID.TryParse(request["FromID"].ToString(), out FromID))
  162. return FailureResult();
  163. if (!Int32.TryParse(request["EstateID"].ToString(), out EstateID))
  164. return FailureResult();
  165. FromName = request["FromName"].ToString();
  166. Message = request["Message"].ToString();
  167. foreach (Scene s in m_EstateModule.Scenes)
  168. {
  169. if (s.RegionInfo.EstateSettings.EstateID == EstateID)
  170. {
  171. IDialogModule dm = s.RequestModuleInterface<IDialogModule>();
  172. if (dm != null)
  173. {
  174. dm.SendNotificationToUsersInRegion(FromID, FromName,
  175. Message);
  176. }
  177. }
  178. }
  179. return SuccessResult();
  180. }
  181. byte[] UpdateCovenant(Dictionary<string, object> request)
  182. {
  183. UUID CovenantID = UUID.Zero;
  184. int EstateID = 0;
  185. if (!request.ContainsKey("CovenantID") || !request.ContainsKey("EstateID"))
  186. return FailureResult();
  187. if (!UUID.TryParse(request["CovenantID"].ToString(), out CovenantID))
  188. return FailureResult();
  189. if (!Int32.TryParse(request["EstateID"].ToString(), out EstateID))
  190. return FailureResult();
  191. foreach (Scene s in m_EstateModule.Scenes)
  192. {
  193. if (s.RegionInfo.EstateSettings.EstateID == (uint)EstateID)
  194. s.RegionInfo.RegionSettings.Covenant = CovenantID;
  195. }
  196. return SuccessResult();
  197. }
  198. byte[] UpdateEstate(Dictionary<string, object> request)
  199. {
  200. int EstateID = 0;
  201. if (!request.ContainsKey("EstateID"))
  202. return FailureResult();
  203. if (!Int32.TryParse(request["EstateID"].ToString(), out EstateID))
  204. return FailureResult();
  205. foreach (Scene s in m_EstateModule.Scenes)
  206. {
  207. if (s.RegionInfo.EstateSettings.EstateID == (uint)EstateID)
  208. s.ReloadEstateData();
  209. }
  210. return SuccessResult();
  211. }
  212. private byte[] FailureResult()
  213. {
  214. return BoolResult(false);
  215. }
  216. private byte[] SuccessResult()
  217. {
  218. return BoolResult(true);
  219. }
  220. private byte[] BoolResult(bool value)
  221. {
  222. XmlDocument doc = new XmlDocument();
  223. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  224. "", "");
  225. doc.AppendChild(xmlnode);
  226. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  227. "");
  228. doc.AppendChild(rootElement);
  229. XmlElement result = doc.CreateElement("", "RESULT", "");
  230. result.AppendChild(doc.CreateTextNode(value.ToString()));
  231. rootElement.AppendChild(result);
  232. return DocToBytes(doc);
  233. }
  234. private byte[] DocToBytes(XmlDocument doc)
  235. {
  236. MemoryStream ms = new MemoryStream();
  237. XmlTextWriter xw = new XmlTextWriter(ms, null);
  238. xw.Formatting = Formatting.Indented;
  239. doc.WriteTo(xw);
  240. xw.Flush();
  241. return ms.ToArray();
  242. }
  243. }
  244. }