GodController.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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.Xml;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. using System.Threading;
  32. using System.Timers;
  33. using Timer = System.Timers.Timer;
  34. using OpenMetaverse;
  35. using OpenMetaverse.StructuredData;
  36. using log4net;
  37. using Nini.Config;
  38. using OpenSim.Framework;
  39. using OpenSim.Framework.Client;
  40. using OpenSim.Framework.Monitoring;
  41. using OpenSim.Region.Framework.Interfaces;
  42. using OpenSim.Region.Framework.Scenes.Types;
  43. using OpenSim.Services.Interfaces;
  44. namespace OpenSim.Region.Framework.Scenes
  45. {
  46. public class GodController
  47. {
  48. public enum ImplicitGodLevels : int
  49. {
  50. EstateManager = 210, // estate manager implicit god level
  51. RegionOwner = 220 // region owner implicit god level should be >= than estate
  52. }
  53. ScenePresence m_scenePresence;
  54. Scene m_scene;
  55. protected bool m_allowGridGods;
  56. protected bool m_forceGridGodsOnly;
  57. protected bool m_regionOwnerIsGod;
  58. protected bool m_regionManagerIsGod;
  59. protected bool m_forceGodModeAlwaysOn;
  60. protected bool m_allowGodActionsWithoutGodMode;
  61. protected int m_userLevel = 0;
  62. // the god level from local or grid user rights
  63. protected int m_rightsGodLevel = 0;
  64. // the level seen by viewers
  65. protected int m_viewergodlevel = 0;
  66. // new level that can be fixed or equal to godlevel, acording to options
  67. protected int m_godlevel = 0;
  68. protected int m_lastLevelToViewer = 0;
  69. public GodController(Scene scene, ScenePresence sp, int userlevel)
  70. {
  71. m_scene = scene;
  72. m_scenePresence = sp;
  73. m_userLevel = userlevel;
  74. IConfigSource config = scene.Config;
  75. string[] sections = new string[] { "Startup", "Permissions" };
  76. // God level is based on UserLevel. Gods will have that
  77. // level grid-wide. Others may become god locally but grid
  78. // gods are god everywhere.
  79. m_allowGridGods =
  80. Util.GetConfigVarFromSections<bool>(config,
  81. "allow_grid_gods", sections, false);
  82. // If grid gods are active, dont allow any other gods
  83. m_forceGridGodsOnly =
  84. Util.GetConfigVarFromSections<bool>(config,
  85. "force_grid_gods_only", sections, false);
  86. if(!m_forceGridGodsOnly)
  87. {
  88. // The owner of a region is a god in his region only.
  89. m_regionOwnerIsGod =
  90. Util.GetConfigVarFromSections<bool>(config,
  91. "region_owner_is_god", sections, true);
  92. // Region managers are gods in the regions they manage.
  93. m_regionManagerIsGod =
  94. Util.GetConfigVarFromSections<bool>(config,
  95. "region_manager_is_god", sections, false);
  96. }
  97. else
  98. m_allowGridGods = true; // reduce potencial user mistakes
  99. // God mode should be turned on in the viewer whenever
  100. // the user has god rights somewhere. They may choose
  101. // to turn it off again, though.
  102. m_forceGodModeAlwaysOn =
  103. Util.GetConfigVarFromSections<bool>(config,
  104. "automatic_gods", sections, false);
  105. // The user can execute any and all god functions, as
  106. // permitted by the viewer UI, without actually "godding
  107. // up". This is the default state in 0.8.2.
  108. m_allowGodActionsWithoutGodMode =
  109. Util.GetConfigVarFromSections<bool>(config,
  110. "implicit_gods", sections, false);
  111. m_rightsGodLevel = CalcRightsGodLevel();
  112. if(m_allowGodActionsWithoutGodMode)
  113. {
  114. m_godlevel = m_rightsGodLevel;
  115. m_forceGodModeAlwaysOn = false;
  116. }
  117. else if(m_forceGodModeAlwaysOn)
  118. {
  119. m_viewergodlevel = m_rightsGodLevel;
  120. m_godlevel = m_rightsGodLevel;
  121. }
  122. m_scenePresence.IsGod = (m_godlevel >= 200);
  123. m_scenePresence.IsViewerUIGod = (m_viewergodlevel >= 200);
  124. }
  125. // calculates god level at sp creation from local and grid user god rights
  126. // for now this is assumed static until user leaves region.
  127. // later estate and gride level updates may update this
  128. protected int CalcRightsGodLevel()
  129. {
  130. int level = 0;
  131. if (m_allowGridGods && m_userLevel >= 200)
  132. level = m_userLevel;
  133. if(m_forceGridGodsOnly || level >= (int)ImplicitGodLevels.RegionOwner)
  134. return level;
  135. if (m_regionOwnerIsGod && m_scene.RegionInfo.EstateSettings.IsEstateOwner(m_scenePresence.UUID))
  136. level = (int)ImplicitGodLevels.RegionOwner;
  137. if(level >= (int)ImplicitGodLevels.EstateManager)
  138. return level;
  139. if (m_regionManagerIsGod && m_scene.Permissions.IsEstateManager(m_scenePresence.UUID))
  140. level = (int)ImplicitGodLevels.EstateManager;
  141. return level;
  142. }
  143. protected bool CanBeGod()
  144. {
  145. return m_rightsGodLevel >= 200;
  146. }
  147. protected void UpdateGodLevels(bool viewerState)
  148. {
  149. if(!CanBeGod())
  150. {
  151. m_viewergodlevel = 0;
  152. m_godlevel = 0;
  153. m_scenePresence.IsGod = false;
  154. m_scenePresence.IsViewerUIGod = false;
  155. return;
  156. }
  157. // legacy some are controled by viewer, others are static
  158. if(m_allowGodActionsWithoutGodMode)
  159. {
  160. if(viewerState)
  161. m_viewergodlevel = m_rightsGodLevel;
  162. else
  163. m_viewergodlevel = 0;
  164. m_godlevel = m_rightsGodLevel;
  165. }
  166. else
  167. {
  168. // new all change with viewer
  169. if(viewerState)
  170. {
  171. m_viewergodlevel = m_rightsGodLevel;
  172. m_godlevel = m_rightsGodLevel;
  173. }
  174. else
  175. {
  176. m_viewergodlevel = 0;
  177. m_godlevel = 0;
  178. }
  179. }
  180. m_scenePresence.IsGod = (m_godlevel >= 200);
  181. m_scenePresence.IsViewerUIGod = (m_viewergodlevel >= 200);
  182. }
  183. public void SyncViewerState()
  184. {
  185. if(m_lastLevelToViewer == m_viewergodlevel)
  186. return;
  187. m_lastLevelToViewer = m_viewergodlevel;
  188. if(m_scenePresence.IsChildAgent)
  189. return;
  190. m_scenePresence.ControllingClient.SendAdminResponse(UUID.Zero, (uint)m_viewergodlevel);
  191. }
  192. public void RequestGodMode(bool god)
  193. {
  194. UpdateGodLevels(god);
  195. if(m_lastLevelToViewer != m_viewergodlevel)
  196. {
  197. m_scenePresence.ControllingClient.SendAdminResponse(UUID.Zero, (uint)m_viewergodlevel);
  198. m_lastLevelToViewer = m_viewergodlevel;
  199. }
  200. }
  201. public OSD State()
  202. {
  203. OSDMap godMap = new OSDMap(2);
  204. bool m_viewerUiIsGod = m_viewergodlevel >= 200;
  205. godMap.Add("ViewerUiIsGod", OSD.FromBoolean(m_viewerUiIsGod));
  206. return godMap;
  207. }
  208. public void SetState(OSD state)
  209. {
  210. bool newstate = false;
  211. if(m_forceGodModeAlwaysOn)
  212. newstate = m_viewergodlevel >= 200;
  213. if(state != null)
  214. {
  215. OSDMap s = (OSDMap)state;
  216. if (s.ContainsKey("ViewerUiIsGod"))
  217. newstate = s["ViewerUiIsGod"].AsBoolean();
  218. m_lastLevelToViewer = m_viewergodlevel; // we are not changing viewer level by default
  219. }
  220. UpdateGodLevels(newstate);
  221. }
  222. public void HasMovedAway()
  223. {
  224. m_lastLevelToViewer = 0;
  225. if(m_forceGodModeAlwaysOn)
  226. {
  227. m_viewergodlevel = m_rightsGodLevel;
  228. m_godlevel = m_rightsGodLevel;
  229. }
  230. }
  231. public int UserLevel
  232. {
  233. get { return m_userLevel; }
  234. set { m_userLevel = value; }
  235. }
  236. public int ViwerUIGodLevel
  237. {
  238. get { return m_viewergodlevel; }
  239. }
  240. public int GodLevel
  241. {
  242. get { return m_godlevel; }
  243. }
  244. }
  245. }