EstateAccess.cs 8.9 KB

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