AgentPreferencesModule.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. }
  67. public void RegionLoaded(Scene scene)
  68. {
  69. scene.EventManager.OnRegisterCaps += RegisterCaps;
  70. ISimulatorFeaturesModule simFeatures = scene.RequestModuleInterface<ISimulatorFeaturesModule>();
  71. simFeatures?.AddFeature("AvatarHoverHeightEnabled",OSD.FromBoolean(true));
  72. }
  73. public void PostInitialise() {}
  74. public void Close() {}
  75. public string Name { get { return "AgentPreferencesModule"; } }
  76. public Type ReplaceableInterface
  77. {
  78. get { return null; }
  79. }
  80. public void RegisterCaps(UUID agent, Caps caps)
  81. {
  82. string capPath = "/" + UUID.Random().ToString();
  83. caps.RegisterSimpleHandler("AgentPreferences",
  84. new SimpleStreamHandler(capPath, delegate(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  85. {
  86. UpdateAgentPreferences(httpRequest, httpResponse, agent);
  87. }));
  88. caps.RegisterSimpleHandler("UpdateAgentLanguage",
  89. new SimpleStreamHandler( capPath, delegate(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  90. {
  91. UpdateAgentPreferences(httpRequest, httpResponse, agent);
  92. }), false);
  93. caps.RegisterSimpleHandler("UpdateAgentInformation",
  94. new SimpleStreamHandler(capPath, delegate(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  95. {
  96. UpdateAgentPreferences(httpRequest, httpResponse, agent);
  97. }), false);
  98. }
  99. public void UpdateAgentPreferences(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse, UUID agent)
  100. {
  101. if (httpRequest.HttpMethod != "POST")
  102. {
  103. httpResponse.StatusCode = (int)HttpStatusCode.NotFound;
  104. return;
  105. }
  106. //m_log.DebugFormat("[AgentPrefs]: UpdateAgentPreferences for {0}", agent.ToString());
  107. OSDMap req;
  108. try
  109. {
  110. req = (OSDMap)OSDParser.DeserializeLLSDXml(httpRequest.InputStream);
  111. }
  112. catch
  113. {
  114. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  115. return;
  116. }
  117. IAgentPreferencesService aps = m_scenes[0].AgentPreferencesService;
  118. AgentPrefs data = null;
  119. if(aps != null)
  120. data = aps.GetAgentPreferences(agent);
  121. if (data == null)
  122. data = new AgentPrefs(agent);
  123. bool changed = false;
  124. OSD tmp;
  125. if (req.TryGetValue("access_prefs", out tmp) && tmp is OSDMap)
  126. {
  127. OSDMap accessPrefs = (OSDMap)tmp; // We could check with ContainsKey...
  128. data.AccessPrefs = accessPrefs["max"].AsString();
  129. changed = true;
  130. }
  131. if (req.TryGetValue("default_object_perm_masks", out tmp) && tmp is OSDMap)
  132. {
  133. OSDMap permsMap = (OSDMap)tmp;
  134. data.PermEveryone = permsMap["Everyone"].AsInteger();
  135. data.PermGroup = permsMap["Group"].AsInteger();
  136. data.PermNextOwner = permsMap["NextOwner"].AsInteger();
  137. changed = true;
  138. }
  139. if (req.TryGetValue("hover_height", out tmp))
  140. {
  141. data.HoverHeight = (float)tmp.AsReal();
  142. changed = true;
  143. }
  144. if (req.TryGetValue("language", out tmp))
  145. {
  146. data.Language = tmp.AsString();
  147. changed = true;
  148. }
  149. if (req.TryGetValue("language_is_public", out tmp))
  150. {
  151. data.LanguageIsPublic = tmp.AsBoolean();
  152. changed = true;
  153. }
  154. if(changed)
  155. aps?.StoreAgentPreferences(data);
  156. IAvatarFactoryModule afm = m_scenes[0].RequestModuleInterface<IAvatarFactoryModule>();
  157. afm?.SetPreferencesHoverZ(agent, (float)data.HoverHeight);
  158. OSDMap resp = new OSDMap();
  159. OSDMap respAccessPrefs = new OSDMap();
  160. respAccessPrefs["max"] = data.AccessPrefs;
  161. resp["access_prefs"] = respAccessPrefs;
  162. OSDMap respDefaultPerms = new OSDMap();
  163. respDefaultPerms["Everyone"] = data.PermEveryone;
  164. respDefaultPerms["Group"] = data.PermGroup;
  165. respDefaultPerms["NextOwner"] = data.PermNextOwner;
  166. resp["default_object_perm_masks"] = respDefaultPerms;
  167. resp["god_level"] = 0; // *TODO: Add this
  168. resp["hover_height"] = data.HoverHeight;
  169. resp["language"] = data.Language;
  170. resp["language_is_public"] = data.LanguageIsPublic;
  171. httpResponse.RawBuffer = OSDParser.SerializeLLSDXmlBytes(resp);
  172. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  173. }
  174. #endregion Region module
  175. }
  176. }