ChatModule.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 OpenSim 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.Collections.Generic;
  29. using System.IO;
  30. using System.Net.Sockets;
  31. using System.Reflection;
  32. using System.Text.RegularExpressions;
  33. using System.Threading;
  34. using OpenMetaverse;
  35. using log4net;
  36. using Nini.Config;
  37. using OpenSim.Framework;
  38. using OpenSim.Region.Environment.Interfaces;
  39. using OpenSim.Region.Environment.Scenes;
  40. namespace OpenSim.Region.Environment.Modules.Avatar.Chat
  41. {
  42. public class ChatModule : IRegionModule
  43. {
  44. private static readonly ILog m_log =
  45. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. private const int DEBUG_CHANNEL = 2147483647;
  47. private bool m_enabled = true;
  48. private int m_saydistance = 30;
  49. private int m_shoutdistance = 100;
  50. private int m_whisperdistance = 10;
  51. private List<Scene> m_scenes = new List<Scene>();
  52. internal object m_syncInit = new object();
  53. #region IRegionModule Members
  54. public virtual void Initialise(Scene scene, IConfigSource config)
  55. {
  56. // wrap this in a try block so that defaults will work if
  57. // the config file doesn't specify otherwise.
  58. try
  59. {
  60. m_enabled = config.Configs["Chat"].GetBoolean("enabled", m_enabled);
  61. if (!m_enabled) return;
  62. m_whisperdistance = config.Configs["Chat"].GetInt("whisper_distance", m_whisperdistance);
  63. m_saydistance = config.Configs["Chat"].GetInt("say_distance", m_saydistance);
  64. m_shoutdistance = config.Configs["Chat"].GetInt("shout_distance", m_shoutdistance);
  65. }
  66. catch (Exception)
  67. {
  68. }
  69. lock (m_syncInit)
  70. {
  71. if (!m_scenes.Contains(scene))
  72. {
  73. m_scenes.Add(scene);
  74. scene.EventManager.OnNewClient += OnNewClient;
  75. scene.EventManager.OnChatFromWorld += OnChatFromWorld;
  76. scene.EventManager.OnChatBroadcast += OnChatBroadcast;
  77. }
  78. }
  79. m_log.InfoFormat("[CHAT] initialized for {0} w:{1} s:{2} S:{3}", scene.RegionInfo.RegionName,
  80. m_whisperdistance, m_saydistance, m_shoutdistance);
  81. }
  82. public virtual void PostInitialise()
  83. {
  84. }
  85. public virtual void Close()
  86. {
  87. }
  88. public virtual string Name
  89. {
  90. get { return "ChatModule"; }
  91. }
  92. public virtual bool IsSharedModule
  93. {
  94. get { return true; }
  95. }
  96. #endregion
  97. public virtual void OnNewClient(IClientAPI client)
  98. {
  99. client.OnChatFromClient += OnChatFromClient;
  100. }
  101. protected OSChatMessage FixPositionOfChatMessage(OSChatMessage c)
  102. {
  103. ScenePresence avatar;
  104. Scene scene = (Scene)c.Scene;
  105. if ((avatar = scene.GetScenePresence(c.Sender.AgentId)) != null)
  106. c.Position = avatar.AbsolutePosition;
  107. return c;
  108. }
  109. public virtual void OnChatFromClient(Object sender, OSChatMessage c)
  110. {
  111. c = FixPositionOfChatMessage(c);
  112. // redistribute to interested subscribers
  113. Scene scene = (Scene)c.Scene;
  114. scene.EventManager.TriggerOnChatFromClient(sender, c);
  115. // early return if not on public or debug channel
  116. if (c.Channel != 0 && c.Channel != DEBUG_CHANNEL) return;
  117. // sanity check:
  118. if (c.Sender == null)
  119. {
  120. m_log.ErrorFormat("[CHAT] OnChatFromClient from {0} has empty Sender field!", sender);
  121. return;
  122. }
  123. DeliverChatToAvatars(ChatSourceType.Agent, c);
  124. }
  125. public virtual void OnChatFromWorld(Object sender, OSChatMessage c)
  126. {
  127. // early return if not on public or debug channel
  128. if (c.Channel != 0 && c.Channel != DEBUG_CHANNEL) return;
  129. DeliverChatToAvatars(ChatSourceType.Object, c);
  130. }
  131. protected virtual void DeliverChatToAvatars(ChatSourceType sourceType, OSChatMessage c)
  132. {
  133. string fromName = c.From;
  134. UUID fromID = UUID.Zero;
  135. string message = c.Message;
  136. IScene scene = c.Scene;
  137. Vector3 fromPos = c.Position;
  138. Vector3 regionPos = new Vector3(scene.RegionInfo.RegionLocX * Constants.RegionSize,
  139. scene.RegionInfo.RegionLocY * Constants.RegionSize, 0);
  140. if (c.Channel == DEBUG_CHANNEL) c.Type = ChatTypeEnum.DebugChannel;
  141. switch (sourceType)
  142. {
  143. case ChatSourceType.Agent:
  144. if (!(scene is Scene))
  145. {
  146. m_log.WarnFormat("[CHAT] scene {0} is not a Scene object, cannot obtain scene presence for {1}",
  147. scene.RegionInfo.RegionName, c.Sender.AgentId);
  148. return;
  149. }
  150. ScenePresence avatar = (scene as Scene).GetScenePresence(c.Sender.AgentId);
  151. fromPos = avatar.AbsolutePosition;
  152. fromName = avatar.Name;
  153. fromID = c.Sender.AgentId;
  154. break;
  155. case ChatSourceType.Object:
  156. fromID = c.SenderUUID;
  157. break;
  158. }
  159. // TODO: iterate over message
  160. if (message.Length >= 1000) // libomv limit
  161. message = message.Substring(0, 1000);
  162. // m_log.DebugFormat("[CHAT] DCTA: fromID {0} fromName {1}, cType {2}, sType {3}", fromID, fromName, c.Type, sourceType);
  163. foreach (Scene s in m_scenes)
  164. {
  165. s.ForEachScenePresence(delegate(ScenePresence presence)
  166. {
  167. TrySendChatMessage(presence, fromPos, regionPos, fromID, fromName,
  168. c.Type, message, sourceType);
  169. });
  170. }
  171. }
  172. static private Vector3 CenterOfRegion = new Vector3(128, 128, 30);
  173. public virtual void OnChatBroadcast(Object sender, OSChatMessage c)
  174. {
  175. // unless the chat to be broadcast is of type Region, we
  176. // drop it if its channel is neither 0 nor DEBUG_CHANNEL
  177. if (c.Channel != 0 && c.Channel != DEBUG_CHANNEL && c.Type != ChatTypeEnum.Region) return;
  178. ChatTypeEnum cType = c.Type;
  179. if (c.Channel == DEBUG_CHANNEL)
  180. cType = ChatTypeEnum.DebugChannel;
  181. if (cType == ChatTypeEnum.Region)
  182. cType = ChatTypeEnum.Say;
  183. if (c.Message.Length > 1100)
  184. c.Message = c.Message.Substring(0, 1000);
  185. // broadcast chat works by redistributing every incoming chat
  186. // message to each avatar in the scene.
  187. string fromName = c.From;
  188. UUID fromID = UUID.Zero;
  189. ChatSourceType sourceType = ChatSourceType.Object;
  190. if (null != c.Sender)
  191. {
  192. ScenePresence avatar = (c.Scene as Scene).GetScenePresence(c.Sender.AgentId);
  193. fromID = c.Sender.AgentId;
  194. fromName = avatar.Name;
  195. sourceType = ChatSourceType.Agent;
  196. }
  197. // m_log.DebugFormat("[CHAT] Broadcast: fromID {0} fromName {1}, cType {2}, sType {3}", fromID, fromName, cType, sourceType);
  198. ((Scene)c.Scene).ForEachScenePresence(
  199. delegate(ScenePresence presence)
  200. {
  201. // ignore chat from child agents
  202. if (presence.IsChildAgent) return;
  203. IClientAPI client = presence.ControllingClient;
  204. // don't forward SayOwner chat from objects to
  205. // non-owner agents
  206. if ((c.Type == ChatTypeEnum.Owner) &&
  207. (null != c.SenderObject) &&
  208. (((SceneObjectPart)c.SenderObject).OwnerID != client.AgentId))
  209. return;
  210. client.SendChatMessage(c.Message, (byte)cType, CenterOfRegion, fromName, fromID,
  211. (byte)sourceType, (byte)ChatAudibleLevel.Fully);
  212. });
  213. }
  214. protected virtual void TrySendChatMessage(ScenePresence presence, Vector3 fromPos, Vector3 regionPos,
  215. UUID fromAgentID, string fromName, ChatTypeEnum type,
  216. string message, ChatSourceType src)
  217. {
  218. // don't send stuff to child agents
  219. if (presence.IsChildAgent) return;
  220. Vector3 fromRegionPos = fromPos + regionPos;
  221. Vector3 toRegionPos = presence.AbsolutePosition +
  222. new Vector3(presence.Scene.RegionInfo.RegionLocX * Constants.RegionSize,
  223. presence.Scene.RegionInfo.RegionLocY * Constants.RegionSize, 0);
  224. int dis = Math.Abs((int) Util.GetDistanceTo(toRegionPos, fromRegionPos));
  225. if (type == ChatTypeEnum.Whisper && dis > m_whisperdistance ||
  226. type == ChatTypeEnum.Say && dis > m_saydistance ||
  227. type == ChatTypeEnum.Shout && dis > m_shoutdistance)
  228. {
  229. return;
  230. }
  231. // TODO: should change so the message is sent through the avatar rather than direct to the ClientView
  232. presence.ControllingClient.SendChatMessage(message, (byte) type, fromPos, fromName,
  233. fromAgentID,(byte)src,(byte)ChatAudibleLevel.Fully);
  234. }
  235. }
  236. }