EstateRequestHandler.cs 11 KB

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