1
0

DynamicMenuModule.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 (d.Mode == UserMode.God && (!m_scene.Permissions.IsGod(agentID)))
  119. continue;
  120. OSDMap loc = null;
  121. switch (d.Location)
  122. {
  123. case InsertLocation.Agent:
  124. loc = agent;
  125. break;
  126. case InsertLocation.World:
  127. loc = world;
  128. break;
  129. case InsertLocation.Tools:
  130. loc = tools;
  131. break;
  132. case InsertLocation.Advanced:
  133. loc = advanced;
  134. break;
  135. case InsertLocation.Admin:
  136. loc = admin;
  137. break;
  138. }
  139. if (loc == null)
  140. continue;
  141. loc[d.Title] = OSD.FromString(d.Title);
  142. }
  143. }
  144. if (m_menuItems.ContainsKey(agentID))
  145. {
  146. foreach (MenuItemData d in m_menuItems[agentID])
  147. {
  148. if (d.Mode == UserMode.God && (!m_scene.Permissions.IsGod(agentID)))
  149. continue;
  150. OSDMap loc = null;
  151. switch (d.Location)
  152. {
  153. case InsertLocation.Agent:
  154. loc = agent;
  155. break;
  156. case InsertLocation.World:
  157. loc = world;
  158. break;
  159. case InsertLocation.Tools:
  160. loc = tools;
  161. break;
  162. case InsertLocation.Advanced:
  163. loc = advanced;
  164. break;
  165. case InsertLocation.Admin:
  166. loc = admin;
  167. break;
  168. }
  169. if (loc == null)
  170. continue;
  171. loc[d.Title] = OSD.FromString(d.Title);
  172. }
  173. }
  174. ((OSDMap)menus)["agent"] = agent;
  175. ((OSDMap)menus)["world"] = world;
  176. ((OSDMap)menus)["tools"] = tools;
  177. ((OSDMap)menus)["advanced"] = advanced;
  178. ((OSDMap)menus)["admin"] = admin;
  179. features["menus"] = menus;
  180. }
  181. private void OnRegisterCaps(UUID agentID, Caps caps)
  182. {
  183. string capUrl = "/CAPS/" + UUID.Random() + "/";
  184. capUrl = "/CAPS/" + UUID.Random() + "/";
  185. caps.RegisterHandler("CustomMenuAction", new MenuActionHandler(capUrl, "CustomMenuAction", agentID, this, m_scene));
  186. }
  187. internal void HandleMenuSelection(string action, UUID agentID, List<uint> selection)
  188. {
  189. if (m_menuItems.ContainsKey(agentID))
  190. {
  191. foreach (MenuItemData d in m_menuItems[agentID])
  192. {
  193. if (d.Title == action)
  194. d.Handler(action, agentID, selection);
  195. }
  196. }
  197. if (m_menuItems.ContainsKey(UUID.Zero))
  198. {
  199. foreach (MenuItemData d in m_menuItems[UUID.Zero])
  200. {
  201. if (d.Title == action)
  202. d.Handler(action, agentID, selection);
  203. }
  204. }
  205. }
  206. public void AddMenuItem(string title, InsertLocation location, UserMode mode, CustomMenuHandler handler)
  207. {
  208. AddMenuItem(UUID.Zero, title, location, mode, handler);
  209. }
  210. public void AddMenuItem(UUID agentID, string title, InsertLocation location, UserMode mode, CustomMenuHandler handler)
  211. {
  212. if (!m_menuItems.ContainsKey(agentID))
  213. m_menuItems[agentID] = new List<MenuItemData>();
  214. m_menuItems[agentID].Add(new MenuItemData() { Title = title, AgentID = agentID, Location = location, Mode = mode, Handler = handler });
  215. }
  216. public void RemoveMenuItem(string action)
  217. {
  218. foreach (KeyValuePair<UUID,List< MenuItemData>> kvp in m_menuItems)
  219. {
  220. List<MenuItemData> pendingDeletes = new List<MenuItemData>();
  221. foreach (MenuItemData d in kvp.Value)
  222. {
  223. if (d.Title == action)
  224. pendingDeletes.Add(d);
  225. }
  226. foreach (MenuItemData d in pendingDeletes)
  227. kvp.Value.Remove(d);
  228. }
  229. }
  230. }
  231. public class MenuActionHandler : BaseStreamHandler
  232. {
  233. private static readonly ILog m_log =
  234. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  235. private UUID m_agentID;
  236. private Scene m_scene;
  237. private DynamicMenuModule m_module;
  238. public MenuActionHandler(string path, string name, UUID agentID, DynamicMenuModule module, Scene scene)
  239. :base("POST", path, name, agentID.ToString())
  240. {
  241. m_agentID = agentID;
  242. m_scene = scene;
  243. m_module = module;
  244. }
  245. protected override byte[] ProcessRequest(string path, Stream request, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  246. {
  247. StreamReader reader = new StreamReader(request);
  248. string requestBody = reader.ReadToEnd();
  249. OSD osd = OSDParser.DeserializeLLSDXml(requestBody);
  250. string action = ((OSDMap)osd)["action"].AsString();
  251. OSDArray selection = (OSDArray)((OSDMap)osd)["selection"];
  252. List<uint> sel = new List<uint>();
  253. for (int i = 0 ; i < selection.Count ; i++)
  254. sel.Add(selection[i].AsUInteger());
  255. Util.FireAndForget(x => { m_module.HandleMenuSelection(action, m_agentID, sel); });
  256. Encoding encoding = Encoding.UTF8;
  257. return encoding.GetBytes(OSDParser.SerializeLLSDXmlString(new OSD()));
  258. }
  259. }
  260. }