EnvironmentModule.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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.Reflection;
  29. using OpenMetaverse;
  30. using OpenSim.Framework;
  31. using OpenSim.Framework.Capabilities;
  32. using OpenSim.Framework.Servers.HttpServer;
  33. using OpenSim.Region.Framework.Interfaces;
  34. using OpenSim.Region.Framework.Scenes;
  35. using log4net;
  36. using Nini.Config;
  37. using Mono.Addins;
  38. using Caps = OpenSim.Framework.Capabilities.Caps;
  39. namespace OpenSim.Region.CoreModules.World.LightShare
  40. {
  41. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "EnvironmentModule")]
  42. public class EnvironmentModule : INonSharedRegionModule, IEnvironmentModule
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. private Scene m_scene = null;
  46. private UUID regionID = UUID.Zero;
  47. private static bool Enabled = false;
  48. private static readonly string capsName = "EnvironmentSettings";
  49. private static readonly string capsBase = "/CAPS/0020/";
  50. private LLSDEnvironmentSetResponse setResponse = null;
  51. #region INonSharedRegionModule
  52. public void Initialise(IConfigSource source)
  53. {
  54. IConfig config = source.Configs["ClientStack.LindenCaps"];
  55. if (null == config)
  56. return;
  57. if (config.GetString("Cap_EnvironmentSettings", String.Empty) != "localhost")
  58. {
  59. m_log.InfoFormat("[{0}]: Module is disabled.", Name);
  60. return;
  61. }
  62. Enabled = true;
  63. m_log.InfoFormat("[{0}]: Module is enabled.", Name);
  64. }
  65. public void Close()
  66. {
  67. }
  68. public string Name
  69. {
  70. get { return "EnvironmentModule"; }
  71. }
  72. public Type ReplaceableInterface
  73. {
  74. get { return null; }
  75. }
  76. public void AddRegion(Scene scene)
  77. {
  78. if (!Enabled)
  79. return;
  80. scene.RegisterModuleInterface<IEnvironmentModule>(this);
  81. m_scene = scene;
  82. regionID = scene.RegionInfo.RegionID;
  83. }
  84. public void RegionLoaded(Scene scene)
  85. {
  86. if (!Enabled)
  87. return;
  88. setResponse = new LLSDEnvironmentSetResponse();
  89. scene.EventManager.OnRegisterCaps += OnRegisterCaps;
  90. }
  91. public void RemoveRegion(Scene scene)
  92. {
  93. if (Enabled)
  94. return;
  95. scene.EventManager.OnRegisterCaps -= OnRegisterCaps;
  96. m_scene = null;
  97. }
  98. #endregion
  99. #region IEnvironmentModule
  100. public void ResetEnvironmentSettings(UUID regionUUID)
  101. {
  102. if (!Enabled)
  103. return;
  104. m_scene.SimulationDataService.RemoveRegionEnvironmentSettings(regionUUID);
  105. }
  106. #endregion
  107. #region Events
  108. private void OnRegisterCaps(UUID agentID, Caps caps)
  109. {
  110. // m_log.DebugFormat("[{0}]: Register capability for agentID {1} in region {2}",
  111. // Name, agentID, caps.RegionName);
  112. string capsPath = capsBase + UUID.Random();
  113. // Get handler
  114. caps.RegisterHandler(
  115. capsName,
  116. new RestStreamHandler(
  117. "GET",
  118. capsPath,
  119. (request, path, param, httpRequest, httpResponse)
  120. => GetEnvironmentSettings(request, path, param, agentID, caps),
  121. capsName,
  122. agentID.ToString()));
  123. // Set handler
  124. caps.HttpListener.AddStreamHandler(
  125. new RestStreamHandler(
  126. "POST",
  127. capsPath,
  128. (request, path, param, httpRequest, httpResponse)
  129. => SetEnvironmentSettings(request, path, param, agentID, caps),
  130. capsName,
  131. agentID.ToString()));
  132. }
  133. #endregion
  134. private string GetEnvironmentSettings(string request, string path, string param,
  135. UUID agentID, Caps caps)
  136. {
  137. // m_log.DebugFormat("[{0}]: Environment GET handle for agentID {1} in region {2}",
  138. // Name, agentID, caps.RegionName);
  139. string env = String.Empty;
  140. try
  141. {
  142. env = m_scene.SimulationDataService.LoadRegionEnvironmentSettings(regionID);
  143. }
  144. catch (Exception e)
  145. {
  146. m_log.ErrorFormat("[{0}]: Unable to load environment settings for region {1}, Exception: {2} - {3}",
  147. Name, caps.RegionName, e.Message, e.StackTrace);
  148. }
  149. if (String.IsNullOrEmpty(env))
  150. env = EnvironmentSettings.EmptySettings(UUID.Zero, regionID);
  151. return env;
  152. }
  153. private string SetEnvironmentSettings(string request, string path, string param,
  154. UUID agentID, Caps caps)
  155. {
  156. // m_log.DebugFormat("[{0}]: Environment SET handle from agentID {1} in region {2}",
  157. // Name, agentID, caps.RegionName);
  158. setResponse.regionID = regionID;
  159. setResponse.success = false;
  160. if (!m_scene.Permissions.CanIssueEstateCommand(agentID, false))
  161. {
  162. setResponse.fail_reason = "Insufficient estate permissions, settings has not been saved.";
  163. return LLSDHelpers.SerialiseLLSDReply(setResponse);
  164. }
  165. try
  166. {
  167. m_scene.SimulationDataService.StoreRegionEnvironmentSettings(regionID, request);
  168. setResponse.success = true;
  169. m_log.InfoFormat("[{0}]: New Environment settings has been saved from agentID {1} in region {2}",
  170. Name, agentID, caps.RegionName);
  171. }
  172. catch (Exception e)
  173. {
  174. m_log.ErrorFormat("[{0}]: Environment settings has not been saved for region {1}, Exception: {2} - {3}",
  175. Name, caps.RegionName, e.Message, e.StackTrace);
  176. setResponse.success = false;
  177. setResponse.fail_reason = String.Format("Environment Set for region {0} has failed, settings has not been saved.", caps.RegionName);
  178. }
  179. return LLSDHelpers.SerialiseLLSDReply(setResponse);
  180. }
  181. }
  182. }