DynamicMenuModule.cs 11 KB

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