DynamicMenuModule.cs 11 KB

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