DynamicFloaterModule.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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.Servers;
  41. using OpenSim.Framework.Servers.HttpServer;
  42. using Nini.Config;
  43. using log4net;
  44. using Mono.Addins;
  45. using Caps = OpenSim.Framework.Capabilities.Caps;
  46. using OSDMap = OpenMetaverse.StructuredData.OSDMap;
  47. namespace OpenSim.Region.OptionalModules.ViewerSupport
  48. {
  49. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "DynamicFloater")]
  50. public class DynamicFloaterModule : INonSharedRegionModule, IDynamicFloaterModule
  51. {
  52. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  53. private Scene m_scene;
  54. private bool m_Enabled = false;
  55. private Dictionary<UUID, Dictionary<int, FloaterData>> m_floaters = new Dictionary<UUID, Dictionary<int, FloaterData>>();
  56. public string Name
  57. {
  58. get { return "DynamicFloaterModule"; }
  59. }
  60. public Type ReplaceableInterface
  61. {
  62. get { return null; }
  63. }
  64. public void Initialise(IConfigSource config)
  65. {
  66. IConfig moduleConfig = config.Configs["DynamicFloaterModule"];
  67. if (moduleConfig != null)
  68. {
  69. m_Enabled = moduleConfig.GetBoolean("enabled", false);
  70. }
  71. }
  72. public void Close()
  73. {
  74. }
  75. public void AddRegion(Scene scene)
  76. {
  77. if(m_Enabled)
  78. {
  79. m_scene = scene;
  80. scene.EventManager.OnNewClient += OnNewClient;
  81. scene.EventManager.OnClientClosed += OnClientClosed;
  82. m_scene.RegisterModuleInterface<IDynamicFloaterModule>(this);
  83. }
  84. }
  85. public void RegionLoaded(Scene scene)
  86. {
  87. }
  88. public void RemoveRegion(Scene scene)
  89. {
  90. }
  91. private void OnNewClient(IClientAPI client)
  92. {
  93. client.OnChatFromClient += OnChatFromClient;
  94. }
  95. private void OnClientClosed(UUID agentID, Scene scene)
  96. {
  97. m_floaters.Remove(agentID);
  98. }
  99. private void SendToClient(ScenePresence sp, string msg)
  100. {
  101. sp.ControllingClient.SendChatMessage(msg,
  102. (byte)ChatTypeEnum.Owner,
  103. sp.AbsolutePosition,
  104. "Server",
  105. UUID.Zero,
  106. UUID.Zero,
  107. (byte)ChatSourceType.Object,
  108. (byte)ChatAudibleLevel.Fully);
  109. }
  110. public void DoUserFloater(UUID agentID, FloaterData dialogData, string configuration)
  111. {
  112. ScenePresence sp = m_scene.GetScenePresence(agentID);
  113. if (sp == null || sp.IsChildAgent)
  114. return;
  115. if (!m_floaters.ContainsKey(agentID))
  116. m_floaters[agentID] = new Dictionary<int, FloaterData>();
  117. if (m_floaters[agentID].ContainsKey(dialogData.Channel))
  118. return;
  119. m_floaters[agentID].Add(dialogData.Channel, dialogData);
  120. string xml;
  121. if (dialogData.XmlText != null && dialogData.XmlText != String.Empty)
  122. {
  123. xml = dialogData.XmlText;
  124. }
  125. else
  126. {
  127. using (FileStream fs = File.Open(dialogData.XmlName + ".xml", FileMode.Open, FileAccess.Read))
  128. {
  129. using (StreamReader sr = new StreamReader(fs))
  130. xml = sr.ReadToEnd().Replace("\n", "");
  131. }
  132. }
  133. List<string> xparts = new List<string>();
  134. while (xml.Length > 0)
  135. {
  136. string x = xml;
  137. if (x.Length > 600)
  138. {
  139. x = x.Substring(0, 600);
  140. xml = xml.Substring(600);
  141. }
  142. else
  143. {
  144. xml = String.Empty;
  145. }
  146. xparts.Add(x);
  147. }
  148. for (int i = 0 ; i < xparts.Count ; i++)
  149. SendToClient(sp, String.Format("># floater {2} create {0}/{1} " + xparts[i], i + 1, xparts.Count, dialogData.FloaterName));
  150. SendToClient(sp, String.Format("># floater {0} {{notify:1}} {{channel: {1}}} {{node:cancel {{notify:1}}}} {{node:ok {{notify:1}}}} {2}", dialogData.FloaterName, (uint)dialogData.Channel, configuration));
  151. }
  152. private void OnChatFromClient(object sender, OSChatMessage msg)
  153. {
  154. if (msg.Sender == null)
  155. return;
  156. //m_log.DebugFormat("chan {0} msg {1}", msg.Channel, msg.Message);
  157. IClientAPI client = msg.Sender;
  158. if (!m_floaters.ContainsKey(client.AgentId))
  159. return;
  160. string[] parts = msg.Message.Split(new char[] {':'});
  161. if (parts.Length == 0)
  162. return;
  163. ScenePresence sp = m_scene.GetScenePresence(client.AgentId);
  164. if (sp == null || sp.IsChildAgent)
  165. return;
  166. Dictionary<int, FloaterData> d = m_floaters[client.AgentId];
  167. // Work around a viewer bug - VALUE from any
  168. // dialog can appear on this channel and needs to
  169. // be dispatched to ALL open dialogs for the user
  170. if (msg.Channel == 427169570)
  171. {
  172. if (parts[0] == "VALUE")
  173. {
  174. foreach (FloaterData dd in d.Values)
  175. {
  176. if(dd.Handler(client, dd, parts))
  177. {
  178. m_floaters[client.AgentId].Remove(dd.Channel);
  179. SendToClient(sp, String.Format("># floater {0} destroy", dd.FloaterName));
  180. break;
  181. }
  182. }
  183. }
  184. return;
  185. }
  186. if (!d.ContainsKey(msg.Channel))
  187. return;
  188. FloaterData data = d[msg.Channel];
  189. if (parts[0] == "NOTIFY")
  190. {
  191. if (parts[1] == "cancel" || parts[1] == data.FloaterName)
  192. {
  193. m_floaters[client.AgentId].Remove(data.Channel);
  194. SendToClient(sp, String.Format("># floater {0} destroy", data.FloaterName));
  195. }
  196. }
  197. if (data.Handler != null && data.Handler(client, data, parts))
  198. {
  199. m_floaters[client.AgentId].Remove(data.Channel);
  200. SendToClient(sp, String.Format("># floater {0} destroy", data.FloaterName));
  201. }
  202. }
  203. public void FloaterControl(ScenePresence sp, FloaterData d, string msg)
  204. {
  205. string sendData = String.Format("># floater {0} {1}", d.FloaterName, msg);
  206. SendToClient(sp, sendData);
  207. }
  208. }
  209. }