ChatModule.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 Mono.Addins;
  33. using OpenMetaverse;
  34. using OpenSim.Framework;
  35. using OpenSim.Region.Framework.Interfaces;
  36. using OpenSim.Region.Framework.Scenes;
  37. namespace OpenSim.Region.CoreModules.Avatar.Chat
  38. {
  39. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "ChatModule")]
  40. public class ChatModule : ISharedRegionModule
  41. {
  42. private static readonly ILog m_log =
  43. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. private const int DEBUG_CHANNEL = 2147483647;
  45. private bool m_enabled = true;
  46. private int m_saydistance = 20;
  47. private int m_shoutdistance = 100;
  48. private int m_whisperdistance = 10;
  49. internal object m_syncy = new object();
  50. internal IConfig m_config;
  51. #region ISharedRegionModule Members
  52. public virtual void Initialise(IConfigSource config)
  53. {
  54. m_config = config.Configs["Chat"];
  55. if (null == m_config)
  56. {
  57. m_log.Info("[CHAT]: no config found, plugin disabled");
  58. m_enabled = false;
  59. return;
  60. }
  61. if (!m_config.GetBoolean("enabled", true))
  62. {
  63. m_log.Info("[CHAT]: plugin disabled by configuration");
  64. m_enabled = false;
  65. return;
  66. }
  67. m_whisperdistance = config.Configs["Chat"].GetInt("whisper_distance", m_whisperdistance);
  68. m_saydistance = config.Configs["Chat"].GetInt("say_distance", m_saydistance);
  69. m_shoutdistance = config.Configs["Chat"].GetInt("shout_distance", m_shoutdistance);
  70. }
  71. public virtual void AddRegion(Scene scene)
  72. {
  73. if (!m_enabled)
  74. return;
  75. scene.EventManager.OnNewClient += OnNewClient;
  76. scene.EventManager.OnChatFromWorld += OnChatFromWorld;
  77. scene.EventManager.OnChatBroadcast += OnChatBroadcast;
  78. m_log.InfoFormat("[CHAT]: Initialized for {0} w:{1} s:{2} S:{3}", scene.RegionInfo.RegionName,
  79. m_whisperdistance, m_saydistance, m_shoutdistance);
  80. }
  81. public virtual void RegionLoaded(Scene scene)
  82. {
  83. }
  84. public virtual void RemoveRegion(Scene scene)
  85. {
  86. if (!m_enabled)
  87. return;
  88. scene.EventManager.OnNewClient -= OnNewClient;
  89. scene.EventManager.OnChatFromWorld -= OnChatFromWorld;
  90. scene.EventManager.OnChatBroadcast -= OnChatBroadcast;
  91. }
  92. public virtual void Close()
  93. {
  94. }
  95. public virtual void PostInitialise()
  96. {
  97. }
  98. public Type ReplaceableInterface
  99. {
  100. get { return null; }
  101. }
  102. public virtual string Name
  103. {
  104. get { return "ChatModule"; }
  105. }
  106. #endregion
  107. public virtual void OnNewClient(IClientAPI client)
  108. {
  109. client.OnChatFromClient += OnChatFromClient;
  110. }
  111. protected OSChatMessage FixPositionOfChatMessage(OSChatMessage c)
  112. {
  113. ScenePresence avatar;
  114. Scene scene = (Scene)c.Scene;
  115. if ((avatar = scene.GetScenePresence(c.Sender.AgentId)) != null)
  116. c.Position = avatar.AbsolutePosition;
  117. return c;
  118. }
  119. public virtual void OnChatFromClient(Object sender, OSChatMessage c)
  120. {
  121. c = FixPositionOfChatMessage(c);
  122. // redistribute to interested subscribers
  123. Scene scene = (Scene)c.Scene;
  124. scene.EventManager.TriggerOnChatFromClient(sender, c);
  125. // early return if not on public or debug channel
  126. if (c.Channel != 0 && c.Channel != DEBUG_CHANNEL) return;
  127. // sanity check:
  128. if (c.Sender == null)
  129. {
  130. m_log.ErrorFormat("[CHAT]: OnChatFromClient from {0} has empty Sender field!", sender);
  131. return;
  132. }
  133. DeliverChatToAvatars(ChatSourceType.Agent, c);
  134. }
  135. public virtual void OnChatFromWorld(Object sender, OSChatMessage c)
  136. {
  137. // early return if not on public or debug channel
  138. if (c.Channel != 0 && c.Channel != DEBUG_CHANNEL) return;
  139. DeliverChatToAvatars(ChatSourceType.Object, c);
  140. }
  141. protected virtual void DeliverChatToAvatars(ChatSourceType sourceType, OSChatMessage c)
  142. {
  143. string fromName = c.From;
  144. UUID fromID = UUID.Zero;
  145. UUID ownerID = UUID.Zero;
  146. UUID targetID = c.TargetUUID;
  147. string message = c.Message;
  148. Scene scene = (Scene)c.Scene;
  149. Vector3 fromPos = c.Position;
  150. Vector3 regionPos = new Vector3(scene.RegionInfo.RegionLocX * Constants.RegionSize,
  151. scene.RegionInfo.RegionLocY * Constants.RegionSize, 0);
  152. if (c.Channel == DEBUG_CHANNEL) c.Type = ChatTypeEnum.DebugChannel;
  153. switch (sourceType)
  154. {
  155. case ChatSourceType.Agent:
  156. ScenePresence avatar = scene.GetScenePresence(c.Sender.AgentId);
  157. fromPos = avatar.AbsolutePosition;
  158. fromName = avatar.Name;
  159. fromID = c.Sender.AgentId;
  160. ownerID = c.Sender.AgentId;
  161. break;
  162. case ChatSourceType.Object:
  163. fromID = c.SenderUUID;
  164. if (c.SenderObject != null && c.SenderObject is SceneObjectPart)
  165. ownerID = ((SceneObjectPart)c.SenderObject).OwnerID;
  166. break;
  167. }
  168. // TODO: iterate over message
  169. if (message.Length >= 1000) // libomv limit
  170. message = message.Substring(0, 1000);
  171. // m_log.DebugFormat(
  172. // "[CHAT]: DCTA: fromID {0} fromName {1}, region{2}, cType {3}, sType {4}, targetID {5}",
  173. // fromID, fromName, scene.RegionInfo.RegionName, c.Type, sourceType, targetID);
  174. HashSet<UUID> receiverIDs = new HashSet<UUID>();
  175. if (targetID == UUID.Zero)
  176. {
  177. // This should use ForEachClient, but clients don't have a position.
  178. // If camera is moved into client, then camera position can be used
  179. scene.ForEachScenePresence(
  180. delegate(ScenePresence presence)
  181. {
  182. if (TrySendChatMessage(
  183. presence, fromPos, regionPos, fromID, ownerID, fromName, c.Type, message, sourceType, false))
  184. receiverIDs.Add(presence.UUID);
  185. }
  186. );
  187. }
  188. else
  189. {
  190. // This is a send to a specific client eg from llRegionSayTo
  191. // no need to check distance etc, jand send is as say
  192. ScenePresence presence = scene.GetScenePresence(targetID);
  193. if (presence != null && !presence.IsChildAgent)
  194. {
  195. if (TrySendChatMessage(
  196. presence, fromPos, regionPos, fromID, ownerID, fromName, ChatTypeEnum.Say, message, sourceType, true))
  197. receiverIDs.Add(presence.UUID);
  198. }
  199. }
  200. scene.EventManager.TriggerOnChatToClients(
  201. fromID, receiverIDs, message, c.Type, fromPos, fromName, sourceType, ChatAudibleLevel.Fully);
  202. }
  203. static private Vector3 CenterOfRegion = new Vector3(128, 128, 30);
  204. public virtual void OnChatBroadcast(Object sender, OSChatMessage c)
  205. {
  206. if (c.Channel != 0 && c.Channel != DEBUG_CHANNEL) return;
  207. ChatTypeEnum cType = c.Type;
  208. if (c.Channel == DEBUG_CHANNEL)
  209. cType = ChatTypeEnum.DebugChannel;
  210. if (cType == ChatTypeEnum.Region)
  211. cType = ChatTypeEnum.Say;
  212. if (c.Message.Length > 1100)
  213. c.Message = c.Message.Substring(0, 1000);
  214. // broadcast chat works by redistributing every incoming chat
  215. // message to each avatar in the scene.
  216. string fromName = c.From;
  217. UUID fromID = UUID.Zero;
  218. ChatSourceType sourceType = ChatSourceType.Object;
  219. if (null != c.Sender)
  220. {
  221. ScenePresence avatar = (c.Scene as Scene).GetScenePresence(c.Sender.AgentId);
  222. fromID = c.Sender.AgentId;
  223. fromName = avatar.Name;
  224. sourceType = ChatSourceType.Agent;
  225. }
  226. else if (c.SenderUUID != UUID.Zero)
  227. {
  228. fromID = c.SenderUUID;
  229. }
  230. // m_log.DebugFormat("[CHAT] Broadcast: fromID {0} fromName {1}, cType {2}, sType {3}", fromID, fromName, cType, sourceType);
  231. HashSet<UUID> receiverIDs = new HashSet<UUID>();
  232. ((Scene)c.Scene).ForEachRootClient(
  233. delegate(IClientAPI client)
  234. {
  235. // don't forward SayOwner chat from objects to
  236. // non-owner agents
  237. if ((c.Type == ChatTypeEnum.Owner) &&
  238. (null != c.SenderObject) &&
  239. (((SceneObjectPart)c.SenderObject).OwnerID != client.AgentId))
  240. return;
  241. client.SendChatMessage(
  242. c.Message, (byte)cType, CenterOfRegion, fromName, fromID, fromID,
  243. (byte)sourceType, (byte)ChatAudibleLevel.Fully);
  244. receiverIDs.Add(client.AgentId);
  245. });
  246. (c.Scene as Scene).EventManager.TriggerOnChatToClients(
  247. fromID, receiverIDs, c.Message, cType, CenterOfRegion, fromName, sourceType, ChatAudibleLevel.Fully);
  248. }
  249. /// <summary>
  250. /// Try to send a message to the given presence
  251. /// </summary>
  252. /// <param name="presence">The receiver</param>
  253. /// <param name="fromPos"></param>
  254. /// <param name="regionPos">/param>
  255. /// <param name="fromAgentID"></param>
  256. /// <param name='ownerID'>
  257. /// Owner of the message. For at least some messages from objects, this has to be correctly filled with the owner's UUID.
  258. /// This is the case for script error messages in viewer 3 since LLViewer change EXT-7762
  259. /// </param>
  260. /// <param name="fromName"></param>
  261. /// <param name="type"></param>
  262. /// <param name="message"></param>
  263. /// <param name="src"></param>
  264. /// <returns>true if the message was sent to the receiver, false if it was not sent due to failing a
  265. /// precondition</returns>
  266. protected virtual bool TrySendChatMessage(
  267. ScenePresence presence, Vector3 fromPos, Vector3 regionPos,
  268. UUID fromAgentID, UUID ownerID, string fromName, ChatTypeEnum type,
  269. string message, ChatSourceType src, bool ignoreDistance)
  270. {
  271. Vector3 fromRegionPos = fromPos + regionPos;
  272. Vector3 toRegionPos = presence.AbsolutePosition +
  273. new Vector3(presence.Scene.RegionInfo.RegionLocX * Constants.RegionSize,
  274. presence.Scene.RegionInfo.RegionLocY * Constants.RegionSize, 0);
  275. int dis = (int)Util.GetDistanceTo(toRegionPos, fromRegionPos);
  276. if (!ignoreDistance)
  277. {
  278. if (type == ChatTypeEnum.Whisper && dis > m_whisperdistance ||
  279. type == ChatTypeEnum.Say && dis > m_saydistance ||
  280. type == ChatTypeEnum.Shout && dis > m_shoutdistance)
  281. {
  282. return false;
  283. }
  284. }
  285. // TODO: should change so the message is sent through the avatar rather than direct to the ClientView
  286. presence.ControllingClient.SendChatMessage(
  287. message, (byte) type, fromPos, fromName,
  288. fromAgentID, ownerID, (byte)src, (byte)ChatAudibleLevel.Fully);
  289. return true;
  290. }
  291. }
  292. }