ChatModule.cs 12 KB

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