ChatModule.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 Type ReplaceableInterface
  109. {
  110. get { return null; }
  111. }
  112. public virtual string Name
  113. {
  114. get { return "ChatModule"; }
  115. }
  116. #endregion
  117. public virtual void OnNewClient(IClientAPI client)
  118. {
  119. client.OnChatFromClient += OnChatFromClient;
  120. }
  121. protected OSChatMessage FixPositionOfChatMessage(OSChatMessage c)
  122. {
  123. ScenePresence avatar;
  124. Scene scene = (Scene)c.Scene;
  125. if ((avatar = scene.GetScenePresence(c.Sender.AgentId)) != null)
  126. c.Position = avatar.AbsolutePosition;
  127. return c;
  128. }
  129. public virtual void OnChatFromClient(Object sender, OSChatMessage c)
  130. {
  131. c = FixPositionOfChatMessage(c);
  132. // redistribute to interested subscribers
  133. Scene scene = (Scene)c.Scene;
  134. scene.EventManager.TriggerOnChatFromClient(sender, c);
  135. // early return if not on public or debug channel
  136. if (c.Channel != 0 && c.Channel != DEBUG_CHANNEL) return;
  137. // sanity check:
  138. if (c.Sender == null)
  139. {
  140. m_log.ErrorFormat("[CHAT] OnChatFromClient from {0} has empty Sender field!", sender);
  141. return;
  142. }
  143. DeliverChatToAvatars(ChatSourceType.Agent, c);
  144. }
  145. public virtual void OnChatFromWorld(Object sender, OSChatMessage c)
  146. {
  147. // early return if not on public or debug channel
  148. if (c.Channel != 0 && c.Channel != DEBUG_CHANNEL) return;
  149. DeliverChatToAvatars(ChatSourceType.Object, c);
  150. }
  151. protected virtual void DeliverChatToAvatars(ChatSourceType sourceType, OSChatMessage c)
  152. {
  153. string fromName = c.From;
  154. UUID fromID = UUID.Zero;
  155. string message = c.Message;
  156. IScene scene = c.Scene;
  157. Vector3 fromPos = c.Position;
  158. Vector3 regionPos = new Vector3(scene.RegionInfo.RegionLocX * Constants.RegionSize,
  159. scene.RegionInfo.RegionLocY * Constants.RegionSize, 0);
  160. if (c.Channel == DEBUG_CHANNEL) c.Type = ChatTypeEnum.DebugChannel;
  161. switch (sourceType)
  162. {
  163. case ChatSourceType.Agent:
  164. if (!(scene is Scene))
  165. {
  166. m_log.WarnFormat("[CHAT]: scene {0} is not a Scene object, cannot obtain scene presence for {1}",
  167. scene.RegionInfo.RegionName, c.Sender.AgentId);
  168. return;
  169. }
  170. ScenePresence avatar = (scene as Scene).GetScenePresence(c.Sender.AgentId);
  171. fromPos = avatar.AbsolutePosition;
  172. fromName = avatar.Name;
  173. fromID = c.Sender.AgentId;
  174. break;
  175. case ChatSourceType.Object:
  176. fromID = c.SenderUUID;
  177. break;
  178. }
  179. // TODO: iterate over message
  180. if (message.Length >= 1000) // libomv limit
  181. message = message.Substring(0, 1000);
  182. // m_log.DebugFormat("[CHAT]: DCTA: fromID {0} fromName {1}, cType {2}, sType {3}", fromID, fromName, c.Type, sourceType);
  183. foreach (Scene s in m_scenes)
  184. {
  185. s.ForEachScenePresence(delegate(ScenePresence presence)
  186. {
  187. TrySendChatMessage(presence, fromPos, regionPos, fromID, fromName,
  188. c.Type, message, sourceType);
  189. });
  190. }
  191. }
  192. static private Vector3 CenterOfRegion = new Vector3(128, 128, 30);
  193. public virtual void OnChatBroadcast(Object sender, OSChatMessage c)
  194. {
  195. // unless the chat to be broadcast is of type Region, we
  196. // drop it if its channel is neither 0 nor DEBUG_CHANNEL
  197. if (c.Channel != 0 && c.Channel != DEBUG_CHANNEL && c.Type != ChatTypeEnum.Region) return;
  198. ChatTypeEnum cType = c.Type;
  199. if (c.Channel == DEBUG_CHANNEL)
  200. cType = ChatTypeEnum.DebugChannel;
  201. if (cType == ChatTypeEnum.Region)
  202. cType = ChatTypeEnum.Say;
  203. if (c.Message.Length > 1100)
  204. c.Message = c.Message.Substring(0, 1000);
  205. // broadcast chat works by redistributing every incoming chat
  206. // message to each avatar in the scene.
  207. string fromName = c.From;
  208. UUID fromID = UUID.Zero;
  209. ChatSourceType sourceType = ChatSourceType.Object;
  210. if (null != c.Sender)
  211. {
  212. ScenePresence avatar = (c.Scene as Scene).GetScenePresence(c.Sender.AgentId);
  213. fromID = c.Sender.AgentId;
  214. fromName = avatar.Name;
  215. sourceType = ChatSourceType.Agent;
  216. }
  217. // m_log.DebugFormat("[CHAT] Broadcast: fromID {0} fromName {1}, cType {2}, sType {3}", fromID, fromName, cType, sourceType);
  218. ((Scene)c.Scene).ForEachScenePresence(
  219. delegate(ScenePresence presence)
  220. {
  221. // ignore chat from child agents
  222. if (presence.IsChildAgent) return;
  223. IClientAPI client = presence.ControllingClient;
  224. // don't forward SayOwner chat from objects to
  225. // non-owner agents
  226. if ((c.Type == ChatTypeEnum.Owner) &&
  227. (null != c.SenderObject) &&
  228. (((SceneObjectPart)c.SenderObject).OwnerID != client.AgentId))
  229. return;
  230. client.SendChatMessage(c.Message, (byte)cType, CenterOfRegion, fromName, fromID,
  231. (byte)sourceType, (byte)ChatAudibleLevel.Fully);
  232. });
  233. }
  234. protected virtual void TrySendChatMessage(ScenePresence presence, Vector3 fromPos, Vector3 regionPos,
  235. UUID fromAgentID, string fromName, ChatTypeEnum type,
  236. string message, ChatSourceType src)
  237. {
  238. // don't send stuff to child agents
  239. if (presence.IsChildAgent) return;
  240. Vector3 fromRegionPos = fromPos + regionPos;
  241. Vector3 toRegionPos = presence.AbsolutePosition +
  242. new Vector3(presence.Scene.RegionInfo.RegionLocX * Constants.RegionSize,
  243. presence.Scene.RegionInfo.RegionLocY * Constants.RegionSize, 0);
  244. int dis = (int)Util.GetDistanceTo(toRegionPos, fromRegionPos);
  245. if (type == ChatTypeEnum.Whisper && dis > m_whisperdistance ||
  246. type == ChatTypeEnum.Say && dis > m_saydistance ||
  247. type == ChatTypeEnum.Shout && dis > m_shoutdistance)
  248. {
  249. return;
  250. }
  251. // TODO: should change so the message is sent through the avatar rather than direct to the ClientView
  252. presence.ControllingClient.SendChatMessage(message, (byte) type, fromPos, fromName,
  253. fromAgentID,(byte)src,(byte)ChatAudibleLevel.Fully);
  254. }
  255. }
  256. }