GodController.cs 9.7 KB

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