EnvironmentModule.cs 8.5 KB

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