ChatModule.cs 14 KB

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