AgentPreferencesModule.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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.Net;
  30. using System.Reflection;
  31. using System.Text;
  32. using log4net;
  33. using Mono.Addins;
  34. using Nini.Config;
  35. using OpenMetaverse;
  36. using OpenMetaverse.StructuredData;
  37. using OpenSim.Framework.Console;
  38. using OpenSim.Framework.Servers;
  39. using OpenSim.Framework.Servers.HttpServer;
  40. using OpenSim.Region.Framework.Interfaces;
  41. using OpenSim.Region.Framework.Scenes;
  42. using OpenSim.Services.Interfaces;
  43. using Caps = OpenSim.Framework.Capabilities.Caps;
  44. using OpenSim.Capabilities.Handlers;
  45. namespace OpenSim.Region.ClientStack.LindenCaps
  46. {
  47. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "AgentPreferencesModule")]
  48. public class AgentPreferencesModule : ISharedRegionModule
  49. {
  50. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  51. private List<Scene> m_scenes = new List<Scene>();
  52. public void Initialise(IConfigSource source)
  53. {
  54. }
  55. #region Region module
  56. public void AddRegion(Scene scene)
  57. {
  58. lock (m_scenes)
  59. m_scenes.Add(scene);
  60. }
  61. public void RemoveRegion(Scene scene)
  62. {
  63. lock (m_scenes)
  64. m_scenes.Remove(scene);
  65. scene.EventManager.OnRegisterCaps -= RegisterCaps;
  66. scene = null;
  67. }
  68. public void RegionLoaded(Scene scene)
  69. {
  70. scene.EventManager.OnRegisterCaps += RegisterCaps;
  71. ISimulatorFeaturesModule simFeatures = scene.RequestModuleInterface<ISimulatorFeaturesModule>();
  72. if(simFeatures != null)
  73. simFeatures.AddFeature("AvatarHoverHeightEnabled",OSD.FromBoolean(true));
  74. }
  75. public void PostInitialise() {}
  76. public void Close() {}
  77. public string Name { get { return "AgentPreferencesModule"; } }
  78. public Type ReplaceableInterface
  79. {
  80. get { return null; }
  81. }
  82. public void RegisterCaps(UUID agent, Caps caps)
  83. {
  84. string capPath = "/" + UUID.Random().ToString();
  85. caps.RegisterSimpleHandler("AgentPreferences",
  86. new SimpleStreamHandler(capPath, delegate(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  87. {
  88. UpdateAgentPreferences(httpRequest, httpResponse, agent);
  89. }));
  90. caps.RegisterSimpleHandler("UpdateAgentLanguage",
  91. new SimpleStreamHandler( capPath, delegate(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  92. {
  93. UpdateAgentPreferences(httpRequest, httpResponse, agent);
  94. }), false);
  95. caps.RegisterSimpleHandler("UpdateAgentInformation",
  96. new SimpleStreamHandler(capPath, delegate(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  97. {
  98. UpdateAgentPreferences(httpRequest, httpResponse, agent);
  99. }), false);
  100. }
  101. public void UpdateAgentPreferences(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse, UUID agent)
  102. {
  103. if (httpRequest.HttpMethod != "POST")
  104. {
  105. httpResponse.StatusCode = (int)HttpStatusCode.NotFound;
  106. return;
  107. }
  108. //m_log.DebugFormat("[AgentPrefs]: UpdateAgentPreferences for {0}", agent.ToString());
  109. OSDMap req;
  110. try
  111. {
  112. req = (OSDMap)OSDParser.DeserializeLLSDXml(httpRequest.InputStream);
  113. }
  114. catch
  115. {
  116. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  117. return;
  118. }
  119. IAgentPreferencesService aps = m_scenes[0].AgentPreferencesService;
  120. AgentPrefs data = null;
  121. if(aps != null)
  122. data = aps.GetAgentPreferences(agent);
  123. if (data == null)
  124. data = new AgentPrefs(agent);
  125. bool changed = false;
  126. OSD tmp;
  127. if (req.TryGetValue("access_prefs", out tmp) && tmp is OSDMap)
  128. {
  129. OSDMap accessPrefs = (OSDMap)tmp; // We could check with ContainsKey...
  130. data.AccessPrefs = accessPrefs["max"].AsString();
  131. changed = true;
  132. }
  133. if (req.TryGetValue("default_object_perm_masks", out tmp) && tmp is OSDMap)
  134. {
  135. OSDMap permsMap = (OSDMap)tmp;
  136. data.PermEveryone = permsMap["Everyone"].AsInteger();
  137. data.PermGroup = permsMap["Group"].AsInteger();
  138. data.PermNextOwner = permsMap["NextOwner"].AsInteger();
  139. changed = true;
  140. }
  141. if (req.TryGetValue("hover_height", out tmp))
  142. {
  143. data.HoverHeight = (float)tmp.AsReal();
  144. changed = true;
  145. }
  146. if (req.TryGetValue("language", out tmp))
  147. {
  148. data.Language = tmp.AsString();
  149. changed = true;
  150. }
  151. if (req.TryGetValue("language_is_public", out tmp))
  152. {
  153. data.LanguageIsPublic = tmp.AsBoolean();
  154. changed = true;
  155. }
  156. if(changed)
  157. aps?.StoreAgentPreferences(data);
  158. IAvatarFactoryModule afm = m_scenes[0].RequestModuleInterface<IAvatarFactoryModule>();
  159. afm?.SetPreferencesHoverZ(agent, (float)data.HoverHeight);
  160. OSDMap resp = new OSDMap();
  161. OSDMap respAccessPrefs = new OSDMap();
  162. respAccessPrefs["max"] = data.AccessPrefs;
  163. resp["access_prefs"] = respAccessPrefs;
  164. OSDMap respDefaultPerms = new OSDMap();
  165. respDefaultPerms["Everyone"] = data.PermEveryone;
  166. respDefaultPerms["Group"] = data.PermGroup;
  167. respDefaultPerms["NextOwner"] = data.PermNextOwner;
  168. resp["default_object_perm_masks"] = respDefaultPerms;
  169. resp["god_level"] = 0; // *TODO: Add this
  170. resp["hover_height"] = data.HoverHeight;
  171. resp["language"] = data.Language;
  172. resp["language_is_public"] = data.LanguageIsPublic;
  173. httpResponse.RawBuffer = OSDParser.SerializeLLSDXmlBytes(resp);
  174. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  175. }
  176. #endregion Region module
  177. }
  178. }