EstateRequestHandler.cs 10 KB

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