EstateAccess.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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;
  29. using System.Collections.Generic;
  30. using System.Globalization;
  31. using System.Net;
  32. using System.Text;
  33. using log4net;
  34. using Nini.Config;
  35. using OpenMetaverse;
  36. using OpenMetaverse.StructuredData;
  37. using Mono.Addins;
  38. using OpenSim.Framework;
  39. using OpenSim.Framework.Servers.HttpServer;
  40. using OpenSim.Region.Framework.Interfaces;
  41. using OpenSim.Region.Framework.Scenes;
  42. using Caps=OpenSim.Framework.Capabilities.Caps;
  43. namespace OpenSim.Region.ClientStack.Linden
  44. {
  45. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "EstateAcessCapModule")]
  46. public class EstateAccessCapModule : INonSharedRegionModule
  47. {
  48. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. private Scene m_scene;
  50. private bool m_Enabled = false;
  51. private string m_capUrl;
  52. //IEstateModule m_EstateModule;
  53. #region INonSharedRegionModule Members
  54. public void Initialise(IConfigSource pSource)
  55. {
  56. IConfig config = pSource.Configs["ClientStack.LindenCaps"];
  57. if (config == null)
  58. return;
  59. m_capUrl = config.GetString("Cap_EstateAccess", string.Empty);
  60. if (!String.IsNullOrEmpty(m_capUrl) && m_capUrl.Equals("localhost"))
  61. m_Enabled = true;
  62. }
  63. public void AddRegion(Scene scene)
  64. {
  65. if (!m_Enabled)
  66. return;
  67. m_scene = scene;
  68. }
  69. public void RemoveRegion(Scene scene)
  70. {
  71. if (!m_Enabled)
  72. return;
  73. if (m_scene == scene)
  74. {
  75. m_scene.EventManager.OnRegisterCaps -= RegisterCaps;
  76. m_scene = null;
  77. }
  78. }
  79. public void RegionLoaded(Scene scene)
  80. {
  81. if (!m_Enabled)
  82. return;
  83. if (scene.RegionInfo == null || scene.RegionInfo.EstateSettings == null)
  84. {
  85. m_Enabled = false;
  86. return;
  87. }
  88. IEstateModule m_EstateModule = scene.RequestModuleInterface<IEstateModule>();
  89. if(m_EstateModule == null)
  90. {
  91. m_Enabled = false;
  92. return;
  93. }
  94. scene.EventManager.OnRegisterCaps += RegisterCaps;
  95. }
  96. public void Close()
  97. {
  98. }
  99. public string Name
  100. {
  101. get { return "EstateAccessCapModule"; }
  102. }
  103. public Type ReplaceableInterface
  104. {
  105. get { return null; }
  106. }
  107. #endregion
  108. public void RegisterCaps(UUID agentID, Caps caps)
  109. {
  110. caps.RegisterSimpleHandler("EstateAccess",
  111. new SimpleStreamHandler("/" + UUID.Random(),
  112. delegate(IOSHttpRequest request, IOSHttpResponse response)
  113. {
  114. ProcessRequest(request, response, agentID);
  115. }));
  116. }
  117. public void ProcessRequest(IOSHttpRequest request, IOSHttpResponse response, UUID AgentId)
  118. {
  119. if(request.HttpMethod != "GET")
  120. {
  121. response.StatusCode = (int)HttpStatusCode.NotFound;
  122. return;
  123. }
  124. ScenePresence avatar;
  125. if (!m_scene.TryGetScenePresence(AgentId, out avatar) || m_scene.RegionInfo == null || m_scene.RegionInfo.EstateSettings == null)
  126. {
  127. response.StatusCode = (int)HttpStatusCode.Gone;
  128. return;
  129. }
  130. if (!m_scene.Permissions.CanIssueEstateCommand(AgentId, false))
  131. {
  132. response.StatusCode = (int)HttpStatusCode.Unauthorized;
  133. return;
  134. }
  135. EstateSettings regionSettings = m_scene.RegionInfo.EstateSettings;
  136. UUID[] managers = regionSettings.EstateManagers;
  137. UUID[] allowed = regionSettings.EstateAccess;
  138. UUID[] groups = regionSettings.EstateGroups;
  139. EstateBan[] EstateBans = regionSettings.EstateBans;
  140. osUTF8 sb = LLSDxmlEncode2.Start();
  141. LLSDxmlEncode2.AddMap(sb);
  142. if (allowed != null && allowed.Length > 0)
  143. {
  144. LLSDxmlEncode2.AddArray("AllowedAgents", sb);
  145. for (int i = 0; i < allowed.Length; ++i)
  146. {
  147. UUID id = allowed[i];
  148. if (id.IsZero())
  149. continue;
  150. LLSDxmlEncode2.AddMap(sb);
  151. LLSDxmlEncode2.AddElem("id", id, sb);
  152. LLSDxmlEncode2.AddEndMap(sb);
  153. }
  154. LLSDxmlEncode2.AddEndArray(sb);
  155. }
  156. else
  157. LLSDxmlEncode2.AddEmptyArray("AllowedAgents", sb);
  158. if (groups != null && groups.Length > 0)
  159. {
  160. LLSDxmlEncode2.AddArray("AllowedGroups", sb);
  161. for (int i = 0; i < groups.Length; ++i)
  162. {
  163. UUID id = groups[i];
  164. if (id.IsZero())
  165. continue;
  166. LLSDxmlEncode2.AddMap(sb);
  167. LLSDxmlEncode2.AddElem("id", id, sb);
  168. LLSDxmlEncode2.AddEndMap(sb);
  169. }
  170. LLSDxmlEncode2.AddEndArray(sb);
  171. }
  172. else
  173. LLSDxmlEncode2.AddEmptyArray("AllowedGroups", sb);
  174. if (EstateBans != null && EstateBans.Length > 0)
  175. {
  176. LLSDxmlEncode2.AddArray("BannedAgents", sb);
  177. for (int i = 0; i < EstateBans.Length; ++i)
  178. {
  179. EstateBan ban = EstateBans[i];
  180. UUID id = ban.BannedUserID;
  181. if (id.IsZero())
  182. continue;
  183. LLSDxmlEncode2.AddMap(sb);
  184. LLSDxmlEncode2.AddElem("id", id, sb);
  185. LLSDxmlEncode2.AddElem("banning_id", ban.BanningUserID, sb);
  186. LLSDxmlEncode2.AddElem("last_login_date", "na", sb); // We will not have this. This information is far at grid
  187. if (ban.BanTime == 0)
  188. LLSDxmlEncode2.AddElem("ban_date", "0000-00-00 00:00", sb);
  189. else
  190. LLSDxmlEncode2.AddElem("ban_date", (Util.ToDateTime(ban.BanTime)).ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture), sb);
  191. LLSDxmlEncode2.AddEndMap(sb);
  192. }
  193. LLSDxmlEncode2.AddEndArray(sb);
  194. }
  195. else
  196. LLSDxmlEncode2.AddEmptyArray("BannedAgents", sb);
  197. if (managers != null && managers.Length > 0)
  198. {
  199. LLSDxmlEncode2.AddArray("Managers", sb);
  200. for (int i = 0; i < managers.Length; ++i)
  201. {
  202. LLSDxmlEncode2.AddMap(sb);
  203. LLSDxmlEncode2.AddElem("agent_id", managers[i], sb);
  204. LLSDxmlEncode2.AddEndMap(sb);
  205. }
  206. LLSDxmlEncode2.AddEndArray(sb);
  207. }
  208. else
  209. LLSDxmlEncode2.AddEmptyArray("Managers", sb);
  210. LLSDxmlEncode2.AddEndMap(sb);
  211. response.RawBuffer = LLSDxmlEncode2.EndToBytes(sb);
  212. response.StatusCode = (int)HttpStatusCode.OK;
  213. }
  214. }
  215. }