ChatModule.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 OpenMetaverse.StructuredData;
  35. using OpenSim.Framework;
  36. using OpenSim.Region.Framework.Interfaces;
  37. using OpenSim.Region.Framework.Scenes;
  38. namespace OpenSim.Region.CoreModules.Avatar.Chat
  39. {
  40. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "ChatModule")]
  41. public class ChatModule : ISharedRegionModule
  42. {
  43. private static readonly ILog m_log =
  44. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. private const int DEBUG_CHANNEL = 2147483647;
  46. private bool m_enabled = true;
  47. private int m_saydistance = 20;
  48. private int m_shoutdistance = 100;
  49. private int m_whisperdistance = 10;
  50. internal object m_syncy = new object();
  51. internal IConfig m_config;
  52. #region ISharedRegionModule Members
  53. public virtual void Initialise(IConfigSource config)
  54. {
  55. m_config = config.Configs["Chat"];
  56. if (null == m_config)
  57. {
  58. m_log.Info("[CHAT]: no config found, plugin disabled");
  59. m_enabled = false;
  60. return;
  61. }
  62. if (!m_config.GetBoolean("enabled", true))
  63. {
  64. m_log.Info("[CHAT]: plugin disabled by configuration");
  65. m_enabled = false;
  66. return;
  67. }
  68. m_whisperdistance = config.Configs["Chat"].GetInt("whisper_distance", m_whisperdistance);
  69. m_saydistance = config.Configs["Chat"].GetInt("say_distance", m_saydistance);
  70. m_shoutdistance = config.Configs["Chat"].GetInt("shout_distance", m_shoutdistance);
  71. }
  72. public virtual void AddRegion(Scene scene)
  73. {
  74. if (!m_enabled)
  75. return;
  76. scene.EventManager.OnNewClient += OnNewClient;
  77. scene.EventManager.OnChatFromWorld += OnChatFromWorld;
  78. scene.EventManager.OnChatBroadcast += OnChatBroadcast;
  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 RegionLoaded(Scene scene)
  83. {
  84. if (!m_enabled)
  85. return;
  86. ISimulatorFeaturesModule featuresModule = scene.RequestModuleInterface<ISimulatorFeaturesModule>();
  87. if (featuresModule != null)
  88. featuresModule.OnSimulatorFeaturesRequest += OnSimulatorFeaturesRequest;
  89. }
  90. public virtual void RemoveRegion(Scene scene)
  91. {
  92. if (!m_enabled)
  93. return;
  94. scene.EventManager.OnNewClient -= OnNewClient;
  95. scene.EventManager.OnChatFromWorld -= OnChatFromWorld;
  96. scene.EventManager.OnChatBroadcast -= OnChatBroadcast;
  97. }
  98. public virtual void Close()
  99. {
  100. }
  101. public virtual void PostInitialise()
  102. {
  103. }
  104. public Type ReplaceableInterface
  105. {
  106. get { return null; }
  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. UUID ownerID = UUID.Zero;
  152. UUID targetID = c.TargetUUID;
  153. string message = c.Message;
  154. Scene scene = (Scene)c.Scene;
  155. Vector3 fromPos = c.Position;
  156. Vector3 regionPos = new Vector3(scene.RegionInfo.WorldLocX, scene.RegionInfo.WorldLocY, 0);
  157. if (c.Channel == DEBUG_CHANNEL) c.Type = ChatTypeEnum.DebugChannel;
  158. switch (sourceType)
  159. {
  160. case ChatSourceType.Agent:
  161. ScenePresence avatar = scene.GetScenePresence(c.Sender.AgentId);
  162. fromPos = avatar.AbsolutePosition;
  163. fromName = avatar.Name;
  164. fromID = c.Sender.AgentId;
  165. ownerID = c.Sender.AgentId;
  166. break;
  167. case ChatSourceType.Object:
  168. fromID = c.SenderUUID;
  169. if (c.SenderObject != null && c.SenderObject is SceneObjectPart)
  170. ownerID = ((SceneObjectPart)c.SenderObject).OwnerID;
  171. break;
  172. }
  173. // TODO: iterate over message
  174. if (message.Length >= 1000) // libomv limit
  175. message = message.Substring(0, 1000);
  176. // m_log.DebugFormat(
  177. // "[CHAT]: DCTA: fromID {0} fromName {1}, region{2}, cType {3}, sType {4}, targetID {5}",
  178. // fromID, fromName, scene.RegionInfo.RegionName, c.Type, sourceType, targetID);
  179. HashSet<UUID> receiverIDs = new HashSet<UUID>();
  180. if (targetID == UUID.Zero)
  181. {
  182. // This should use ForEachClient, but clients don't have a position.
  183. // If camera is moved into client, then camera position can be used
  184. scene.ForEachScenePresence(
  185. delegate(ScenePresence presence)
  186. {
  187. if (TrySendChatMessage(
  188. presence, fromPos, regionPos, fromID, ownerID, fromName, c.Type, message, sourceType, false))
  189. receiverIDs.Add(presence.UUID);
  190. }
  191. );
  192. }
  193. else
  194. {
  195. // This is a send to a specific client eg from llRegionSayTo
  196. // no need to check distance etc, jand send is as say
  197. ScenePresence presence = scene.GetScenePresence(targetID);
  198. if (presence != null && !presence.IsChildAgent)
  199. {
  200. if (TrySendChatMessage(
  201. presence, fromPos, regionPos, fromID, ownerID, fromName, ChatTypeEnum.Say, message, sourceType, true))
  202. receiverIDs.Add(presence.UUID);
  203. }
  204. }
  205. scene.EventManager.TriggerOnChatToClients(
  206. fromID, receiverIDs, message, c.Type, fromPos, fromName, sourceType, ChatAudibleLevel.Fully);
  207. }
  208. static private Vector3 CenterOfRegion = new Vector3(128, 128, 30);
  209. public virtual void OnChatBroadcast(Object sender, OSChatMessage c)
  210. {
  211. if (c.Channel != 0 && c.Channel != DEBUG_CHANNEL) return;
  212. ChatTypeEnum cType = c.Type;
  213. if (c.Channel == DEBUG_CHANNEL)
  214. cType = ChatTypeEnum.DebugChannel;
  215. if (cType == ChatTypeEnum.Region)
  216. cType = ChatTypeEnum.Say;
  217. if (c.Message.Length > 1100)
  218. c.Message = c.Message.Substring(0, 1000);
  219. // broadcast chat works by redistributing every incoming chat
  220. // message to each avatar in the scene.
  221. string fromName = c.From;
  222. UUID fromID = UUID.Zero;
  223. ChatSourceType sourceType = ChatSourceType.Object;
  224. if (null != c.Sender)
  225. {
  226. ScenePresence avatar = (c.Scene as Scene).GetScenePresence(c.Sender.AgentId);
  227. fromID = c.Sender.AgentId;
  228. fromName = avatar.Name;
  229. sourceType = ChatSourceType.Agent;
  230. }
  231. else if (c.SenderUUID != UUID.Zero)
  232. {
  233. fromID = c.SenderUUID;
  234. }
  235. // m_log.DebugFormat("[CHAT] Broadcast: fromID {0} fromName {1}, cType {2}, sType {3}", fromID, fromName, cType, sourceType);
  236. HashSet<UUID> receiverIDs = new HashSet<UUID>();
  237. ((Scene)c.Scene).ForEachRootClient(
  238. delegate(IClientAPI client)
  239. {
  240. // don't forward SayOwner chat from objects to
  241. // non-owner agents
  242. if ((c.Type == ChatTypeEnum.Owner) &&
  243. (null != c.SenderObject) &&
  244. (((SceneObjectPart)c.SenderObject).OwnerID != client.AgentId))
  245. return;
  246. client.SendChatMessage(
  247. c.Message, (byte)cType, CenterOfRegion, fromName, fromID, fromID,
  248. (byte)sourceType, (byte)ChatAudibleLevel.Fully);
  249. receiverIDs.Add(client.AgentId);
  250. });
  251. (c.Scene as Scene).EventManager.TriggerOnChatToClients(
  252. fromID, receiverIDs, c.Message, cType, CenterOfRegion, fromName, sourceType, ChatAudibleLevel.Fully);
  253. }
  254. /// <summary>
  255. /// Try to send a message to the given presence
  256. /// </summary>
  257. /// <param name="presence">The receiver</param>
  258. /// <param name="fromPos"></param>
  259. /// <param name="regionPos">/param>
  260. /// <param name="fromAgentID"></param>
  261. /// <param name='ownerID'>
  262. /// Owner of the message. For at least some messages from objects, this has to be correctly filled with the owner's UUID.
  263. /// This is the case for script error messages in viewer 3 since LLViewer change EXT-7762
  264. /// </param>
  265. /// <param name="fromName"></param>
  266. /// <param name="type"></param>
  267. /// <param name="message"></param>
  268. /// <param name="src"></param>
  269. /// <returns>true if the message was sent to the receiver, false if it was not sent due to failing a
  270. /// precondition</returns>
  271. protected virtual bool TrySendChatMessage(
  272. ScenePresence presence, Vector3 fromPos, Vector3 regionPos,
  273. UUID fromAgentID, UUID ownerID, string fromName, ChatTypeEnum type,
  274. string message, ChatSourceType src, bool ignoreDistance)
  275. {
  276. if (presence.LifecycleState != ScenePresenceState.Running)
  277. return false;
  278. if (!ignoreDistance)
  279. {
  280. Vector3 fromRegionPos = fromPos + regionPos;
  281. Vector3 toRegionPos = presence.AbsolutePosition +
  282. new Vector3(presence.Scene.RegionInfo.WorldLocX, presence.Scene.RegionInfo.WorldLocY, 0);
  283. int dis = (int)Util.GetDistanceTo(toRegionPos, fromRegionPos);
  284. if (type == ChatTypeEnum.Whisper && dis > m_whisperdistance ||
  285. type == ChatTypeEnum.Say && dis > m_saydistance ||
  286. type == ChatTypeEnum.Shout && dis > m_shoutdistance)
  287. {
  288. return false;
  289. }
  290. }
  291. // TODO: should change so the message is sent through the avatar rather than direct to the ClientView
  292. presence.ControllingClient.SendChatMessage(
  293. message, (byte) type, fromPos, fromName,
  294. fromAgentID, ownerID, (byte)src, (byte)ChatAudibleLevel.Fully);
  295. return true;
  296. }
  297. #region SimulatorFeaturesRequest
  298. static OSDInteger m_SayRange, m_WhisperRange, m_ShoutRange;
  299. private void OnSimulatorFeaturesRequest(UUID agentID, ref OSDMap features)
  300. {
  301. OSD extras = new OSDMap();
  302. if (features.ContainsKey("OpenSimExtras"))
  303. extras = features["OpenSimExtras"];
  304. else
  305. features["OpenSimExtras"] = extras;
  306. if (m_SayRange == null)
  307. {
  308. // Do this only once
  309. m_SayRange = new OSDInteger(m_saydistance);
  310. m_WhisperRange = new OSDInteger(m_whisperdistance);
  311. m_ShoutRange = new OSDInteger(m_shoutdistance);
  312. }
  313. ((OSDMap)extras)["say-range"] = m_SayRange;
  314. ((OSDMap)extras)["whisper-range"] = m_WhisperRange;
  315. ((OSDMap)extras)["shout-range"] = m_ShoutRange;
  316. }
  317. #endregion
  318. }
  319. }