DynamicFloaterModule.cs 8.3 KB

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