1
0

DynamicMenuModule.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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.Net;
  29. using System.Reflection;
  30. using System.Collections.Generic;
  31. using OpenMetaverse;
  32. using OpenMetaverse.StructuredData;
  33. using OpenSim.Region.Framework.Scenes;
  34. using OpenSim.Region.Framework.Interfaces;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Servers.HttpServer;
  37. using Nini.Config;
  38. using log4net;
  39. using Mono.Addins;
  40. using Caps = OpenSim.Framework.Capabilities.Caps;
  41. using OSDMap = OpenMetaverse.StructuredData.OSDMap;
  42. namespace OpenSim.Region.OptionalModules.ViewerSupport
  43. {
  44. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "DynamicMenu")]
  45. public class DynamicMenuModule : INonSharedRegionModule, IDynamicMenuModule
  46. {
  47. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  48. private bool m_Enabled = false;
  49. private class MenuItemData
  50. {
  51. public string Title;
  52. public UUID AgentID;
  53. public InsertLocation Location;
  54. public UserMode Mode;
  55. public CustomMenuHandler Handler;
  56. }
  57. private Dictionary<UUID, List<MenuItemData>> m_menuItems =
  58. new Dictionary<UUID, List<MenuItemData>>();
  59. private Scene m_scene;
  60. public string Name
  61. {
  62. get { return "DynamicMenuModule"; }
  63. }
  64. public Type ReplaceableInterface
  65. {
  66. get { return null; }
  67. }
  68. public void Initialise(IConfigSource config)
  69. {
  70. IConfig moduleConfig = config.Configs["DynamicMenuModule"];
  71. if (moduleConfig != null)
  72. {
  73. m_Enabled = moduleConfig.GetBoolean("enabled", false);
  74. }
  75. }
  76. public void Close()
  77. {
  78. }
  79. public void AddRegion(Scene scene)
  80. {
  81. if (m_Enabled)
  82. {
  83. m_scene = scene;
  84. scene.EventManager.OnRegisterCaps += OnRegisterCaps;
  85. m_scene.RegisterModuleInterface<IDynamicMenuModule>(this);
  86. }
  87. }
  88. public void RegionLoaded(Scene scene)
  89. {
  90. if (m_Enabled)
  91. {
  92. ISimulatorFeaturesModule featuresModule = m_scene.RequestModuleInterface<ISimulatorFeaturesModule>();
  93. if (featuresModule != null)
  94. featuresModule.OnSimulatorFeaturesRequest += OnSimulatorFeaturesRequest;
  95. }
  96. }
  97. public void RemoveRegion(Scene scene)
  98. {
  99. }
  100. private void OnSimulatorFeaturesRequest(UUID agentID, ref OSDMap features)
  101. {
  102. OSD menus = new OSDMap();
  103. if (features.ContainsKey("menus"))
  104. menus = features["menus"];
  105. OSDMap agent = new OSDMap();
  106. OSDMap world = new OSDMap();
  107. OSDMap tools = new OSDMap();
  108. OSDMap advanced = new OSDMap();
  109. OSDMap admin = new OSDMap();
  110. if (((OSDMap)menus).ContainsKey("agent"))
  111. agent = (OSDMap)((OSDMap)menus)["agent"];
  112. if (((OSDMap)menus).ContainsKey("world"))
  113. world = (OSDMap)((OSDMap)menus)["world"];
  114. if (((OSDMap)menus).ContainsKey("tools"))
  115. tools = (OSDMap)((OSDMap)menus)["tools"];
  116. if (((OSDMap)menus).ContainsKey("advanced"))
  117. advanced = (OSDMap)((OSDMap)menus)["advanced"];
  118. if (((OSDMap)menus).ContainsKey("admin"))
  119. admin = (OSDMap)((OSDMap)menus)["admin"];
  120. if (m_menuItems.ContainsKey(UUID.Zero))
  121. {
  122. foreach (MenuItemData d in m_menuItems[UUID.Zero])
  123. {
  124. if (!m_scene.Permissions.IsGod(agentID))
  125. {
  126. if (d.Mode == UserMode.RegionManager && (!m_scene.Permissions.IsAdministrator(agentID)))
  127. continue;
  128. }
  129. OSDMap loc = null;
  130. switch (d.Location)
  131. {
  132. case InsertLocation.Agent:
  133. loc = agent;
  134. break;
  135. case InsertLocation.World:
  136. loc = world;
  137. break;
  138. case InsertLocation.Tools:
  139. loc = tools;
  140. break;
  141. case InsertLocation.Advanced:
  142. loc = advanced;
  143. break;
  144. case InsertLocation.Admin:
  145. loc = admin;
  146. break;
  147. }
  148. if (loc == null)
  149. continue;
  150. loc[d.Title] = OSD.FromString(d.Title);
  151. }
  152. }
  153. if (m_menuItems.ContainsKey(agentID))
  154. {
  155. foreach (MenuItemData d in m_menuItems[agentID])
  156. {
  157. if (d.Mode == UserMode.God && (!m_scene.Permissions.IsGod(agentID)))
  158. continue;
  159. OSDMap loc = null;
  160. switch (d.Location)
  161. {
  162. case InsertLocation.Agent:
  163. loc = agent;
  164. break;
  165. case InsertLocation.World:
  166. loc = world;
  167. break;
  168. case InsertLocation.Tools:
  169. loc = tools;
  170. break;
  171. case InsertLocation.Advanced:
  172. loc = advanced;
  173. break;
  174. case InsertLocation.Admin:
  175. loc = admin;
  176. break;
  177. }
  178. if (loc == null)
  179. continue;
  180. loc[d.Title] = OSD.FromString(d.Title);
  181. }
  182. }
  183. ((OSDMap)menus)["agent"] = agent;
  184. ((OSDMap)menus)["world"] = world;
  185. ((OSDMap)menus)["tools"] = tools;
  186. ((OSDMap)menus)["advanced"] = advanced;
  187. ((OSDMap)menus)["admin"] = admin;
  188. features["menus"] = menus;
  189. }
  190. private void OnRegisterCaps(UUID agentID, Caps caps)
  191. {
  192. caps.RegisterSimpleHandler("CustomMenuAction", new MenuActionHandler("/" + UUID.Random(), "CustomMenuAction", agentID, this, m_scene));
  193. }
  194. internal void HandleMenuSelection(string action, UUID agentID, List<uint> selection)
  195. {
  196. if (m_menuItems.ContainsKey(agentID))
  197. {
  198. foreach (MenuItemData d in m_menuItems[agentID])
  199. {
  200. if (d.Title == action)
  201. d.Handler(action, agentID, selection);
  202. }
  203. }
  204. if (m_menuItems.ContainsKey(UUID.Zero))
  205. {
  206. foreach (MenuItemData d in m_menuItems[UUID.Zero])
  207. {
  208. if (d.Title == action)
  209. d.Handler(action, agentID, selection);
  210. }
  211. }
  212. }
  213. public void AddMenuItem(string title, InsertLocation location, UserMode mode, CustomMenuHandler handler)
  214. {
  215. AddMenuItem(UUID.Zero, title, location, mode, handler);
  216. }
  217. public void AddMenuItem(UUID agentID, string title, InsertLocation location, UserMode mode, CustomMenuHandler handler)
  218. {
  219. if (!m_menuItems.ContainsKey(agentID))
  220. m_menuItems[agentID] = new List<MenuItemData>();
  221. m_menuItems[agentID].Add(new MenuItemData() { Title = title, AgentID = agentID, Location = location, Mode = mode, Handler = handler });
  222. }
  223. public void RemoveMenuItem(string action)
  224. {
  225. foreach (KeyValuePair<UUID,List< MenuItemData>> kvp in m_menuItems)
  226. {
  227. List<MenuItemData> pendingDeletes = new List<MenuItemData>();
  228. foreach (MenuItemData d in kvp.Value)
  229. {
  230. if (d.Title == action)
  231. pendingDeletes.Add(d);
  232. }
  233. foreach (MenuItemData d in pendingDeletes)
  234. kvp.Value.Remove(d);
  235. }
  236. }
  237. }
  238. public class MenuActionHandler : SimpleOSDMapHandler
  239. {
  240. private static readonly ILog m_log =
  241. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  242. private UUID m_agentID;
  243. private Scene m_scene;
  244. private DynamicMenuModule m_module;
  245. public MenuActionHandler(string path, string name, UUID agentID, DynamicMenuModule module, Scene scene)
  246. :base("POST", path)
  247. {
  248. m_agentID = agentID;
  249. m_scene = scene;
  250. m_module = module;
  251. }
  252. protected override void ProcessRequest(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse, OSDMap osd)
  253. {
  254. string action = osd["action"].AsString();
  255. OSDArray selection = (OSDArray)osd["selection"];
  256. List<uint> sel = new List<uint>();
  257. for (int i = 0 ; i < selection.Count ; i++)
  258. sel.Add(selection[i].AsUInteger());
  259. Util.FireAndForget(
  260. x => { m_module.HandleMenuSelection(action, m_agentID, sel); }, null, "DynamicMenuModule.HandleMenuSelection");
  261. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  262. //httpResponse.RawBuffer = Util.UTF8NBGetbytes("<llsd></llsd>");
  263. }
  264. }
  265. }