ChatModule.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. protected const int DEBUG_CHANNEL = 2147483647;
  46. protected bool m_enabled = true;
  47. protected int m_saydistance = 20;
  48. protected int m_shoutdistance = 100;
  49. protected int m_whisperdistance = 10;
  50. protected List<Scene> m_scenes = new List<Scene>();
  51. protected List<string> FreezeCache = new List<string>();
  52. protected string m_adminPrefix = "";
  53. protected object m_syncy = new object();
  54. protected IConfig m_config;
  55. #region ISharedRegionModule Members
  56. public virtual void Initialise(IConfigSource config)
  57. {
  58. m_config = config.Configs["Chat"];
  59. if (m_config != null)
  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 = m_config.GetInt("whisper_distance", m_whisperdistance);
  68. m_saydistance = m_config.GetInt("say_distance", m_saydistance);
  69. m_shoutdistance = m_config.GetInt("shout_distance", m_shoutdistance);
  70. m_adminPrefix = m_config.GetString("admin_prefix", "");
  71. }
  72. }
  73. public virtual void AddRegion(Scene scene)
  74. {
  75. if (!m_enabled) return;
  76. lock (m_syncy)
  77. {
  78. if (!m_scenes.Contains(scene))
  79. {
  80. m_scenes.Add(scene);
  81. scene.EventManager.OnNewClient += OnNewClient;
  82. scene.EventManager.OnChatFromWorld += OnChatFromWorld;
  83. scene.EventManager.OnChatBroadcast += OnChatBroadcast;
  84. }
  85. }
  86. m_log.InfoFormat("[CHAT]: Initialized for {0} w:{1} s:{2} S:{3}", scene.RegionInfo.RegionName,
  87. m_whisperdistance, m_saydistance, m_shoutdistance);
  88. }
  89. public virtual void RegionLoaded(Scene scene)
  90. {
  91. if (!m_enabled)
  92. return;
  93. ISimulatorFeaturesModule featuresModule = scene.RequestModuleInterface<ISimulatorFeaturesModule>();
  94. if (featuresModule != null)
  95. featuresModule.OnSimulatorFeaturesRequest += OnSimulatorFeaturesRequest;
  96. }
  97. public virtual void RemoveRegion(Scene scene)
  98. {
  99. if (!m_enabled) return;
  100. lock (m_syncy)
  101. {
  102. if (m_scenes.Contains(scene))
  103. {
  104. scene.EventManager.OnNewClient -= OnNewClient;
  105. scene.EventManager.OnChatFromWorld -= OnChatFromWorld;
  106. scene.EventManager.OnChatBroadcast -= OnChatBroadcast;
  107. m_scenes.Remove(scene);
  108. }
  109. }
  110. }
  111. public virtual void Close()
  112. {
  113. }
  114. public virtual void PostInitialise()
  115. {
  116. }
  117. public virtual Type ReplaceableInterface
  118. {
  119. get { return null; }
  120. }
  121. public virtual string Name
  122. {
  123. get { return "ChatModule"; }
  124. }
  125. #endregion
  126. public virtual void OnNewClient(IClientAPI client)
  127. {
  128. client.OnChatFromClient += OnChatFromClient;
  129. }
  130. protected virtual OSChatMessage FixPositionOfChatMessage(OSChatMessage c)
  131. {
  132. ScenePresence avatar;
  133. Scene scene = (Scene)c.Scene;
  134. if ((avatar = scene.GetScenePresence(c.Sender.AgentId)) != null)
  135. c.Position = avatar.AbsolutePosition;
  136. return c;
  137. }
  138. public virtual void OnChatFromClient(Object sender, OSChatMessage c)
  139. {
  140. c = FixPositionOfChatMessage(c);
  141. // redistribute to interested subscribers
  142. Scene scene = (Scene)c.Scene;
  143. scene.EventManager.TriggerOnChatFromClient(sender, c);
  144. // early return if not on public or debug channel
  145. if (c.Channel != 0 && c.Channel != DEBUG_CHANNEL) return;
  146. // sanity check:
  147. if (c.Sender == null)
  148. {
  149. m_log.ErrorFormat("[CHAT]: OnChatFromClient from {0} has empty Sender field!", sender);
  150. return;
  151. }
  152. if (FreezeCache.Contains(c.Sender.AgentId.ToString()))
  153. {
  154. if (c.Type != ChatTypeEnum.StartTyping || c.Type != ChatTypeEnum.StopTyping)
  155. c.Sender.SendAgentAlertMessage("You may not talk as you are frozen.", false);
  156. }
  157. else
  158. {
  159. DeliverChatToAvatars(ChatSourceType.Agent, c);
  160. }
  161. }
  162. public virtual void OnChatFromWorld(Object sender, OSChatMessage c)
  163. {
  164. // early return if not on public or debug channel
  165. if (c.Channel != 0 && c.Channel != DEBUG_CHANNEL) return;
  166. DeliverChatToAvatars(ChatSourceType.Object, c);
  167. }
  168. protected virtual void DeliverChatToAvatars(ChatSourceType sourceType, OSChatMessage c)
  169. {
  170. string fromName = c.From;
  171. string fromNamePrefix = "";
  172. UUID fromID = UUID.Zero;
  173. UUID ownerID = UUID.Zero;
  174. string message = c.Message;
  175. Scene scene = c.Scene as Scene;
  176. UUID destination = c.Destination;
  177. Vector3 fromPos = c.Position;
  178. Vector3 regionPos = new Vector3(scene.RegionInfo.WorldLocX, scene.RegionInfo.WorldLocY, 0);
  179. bool checkParcelHide = false;
  180. UUID sourceParcelID = UUID.Zero;
  181. Vector3 hidePos = fromPos;
  182. if (c.Channel == DEBUG_CHANNEL) c.Type = ChatTypeEnum.DebugChannel;
  183. if(!m_scenes.Contains(scene))
  184. {
  185. m_log.WarnFormat("[CHAT]: message from unkown scene {0} ignored",
  186. scene.RegionInfo.RegionName);
  187. return;
  188. }
  189. switch (sourceType)
  190. {
  191. case ChatSourceType.Agent:
  192. ScenePresence avatar = (scene as Scene).GetScenePresence(c.Sender.AgentId);
  193. fromPos = avatar.AbsolutePosition;
  194. fromName = avatar.Name;
  195. fromID = c.Sender.AgentId;
  196. if (avatar.IsViewerUIGod)
  197. { // let gods speak to outside or things may get confusing
  198. fromNamePrefix = m_adminPrefix;
  199. checkParcelHide = false;
  200. }
  201. else
  202. {
  203. checkParcelHide = true;
  204. }
  205. destination = UUID.Zero; // Avatars cant "SayTo"
  206. ownerID = c.Sender.AgentId;
  207. hidePos = fromPos;
  208. break;
  209. case ChatSourceType.Object:
  210. fromID = c.SenderUUID;
  211. if (c.SenderObject != null && c.SenderObject is SceneObjectPart)
  212. {
  213. ownerID = ((SceneObjectPart)c.SenderObject).OwnerID;
  214. if (((SceneObjectPart)c.SenderObject).ParentGroup.IsAttachment)
  215. {
  216. checkParcelHide = true;
  217. hidePos = ((SceneObjectPart)c.SenderObject).ParentGroup.AbsolutePosition;
  218. }
  219. }
  220. break;
  221. }
  222. // TODO: iterate over message
  223. if (message.Length >= 1000) // libomv limit
  224. message = message.Substring(0, 1000);
  225. // m_log.DebugFormat(
  226. // "[CHAT]: DCTA: fromID {0} fromName {1}, region{2}, cType {3}, sType {4}",
  227. // fromID, fromName, scene.RegionInfo.RegionName, c.Type, sourceType);
  228. HashSet<UUID> receiverIDs = new HashSet<UUID>();
  229. if (checkParcelHide)
  230. {
  231. checkParcelHide = false;
  232. if (c.Type < ChatTypeEnum.DebugChannel && destination == UUID.Zero)
  233. {
  234. ILandObject srcland = scene.LandChannel.GetLandObject(hidePos.X, hidePos.Y);
  235. if (srcland != null && !srcland.LandData.SeeAVs)
  236. {
  237. sourceParcelID = srcland.LandData.GlobalID;
  238. checkParcelHide = true;
  239. }
  240. }
  241. }
  242. scene.ForEachScenePresence(
  243. delegate(ScenePresence presence)
  244. {
  245. if (destination != UUID.Zero && presence.UUID != destination)
  246. return;
  247. if(presence.IsChildAgent)
  248. {
  249. if(checkParcelHide)
  250. return;
  251. if (TrySendChatMessage(presence, fromPos, regionPos, fromID,
  252. ownerID, fromNamePrefix + fromName, c.Type,
  253. message, sourceType, (destination != UUID.Zero)))
  254. receiverIDs.Add(presence.UUID);
  255. return;
  256. }
  257. ILandObject Presencecheck = scene.LandChannel.GetLandObject(presence.AbsolutePosition.X, presence.AbsolutePosition.Y);
  258. if (Presencecheck != null)
  259. {
  260. if (checkParcelHide)
  261. {
  262. if (sourceParcelID != Presencecheck.LandData.GlobalID && !presence.IsViewerUIGod)
  263. return;
  264. }
  265. if (c.Sender == null || Presencecheck.IsEitherBannedOrRestricted(c.Sender.AgentId) != true)
  266. {
  267. if (TrySendChatMessage(presence, fromPos, regionPos, fromID,
  268. ownerID, fromNamePrefix + fromName, c.Type,
  269. message, sourceType, (destination != UUID.Zero)))
  270. receiverIDs.Add(presence.UUID);
  271. }
  272. }
  273. });
  274. scene.EventManager.TriggerOnChatToClients(
  275. fromID, receiverIDs, message, c.Type, fromPos, fromName, sourceType, ChatAudibleLevel.Fully);
  276. }
  277. static protected Vector3 CenterOfRegion = new Vector3(128, 128, 30);
  278. public virtual void OnChatBroadcast(Object sender, OSChatMessage c)
  279. {
  280. if (c.Channel != 0 && c.Channel != DEBUG_CHANNEL) return;
  281. ChatTypeEnum cType = c.Type;
  282. if (c.Channel == DEBUG_CHANNEL)
  283. cType = ChatTypeEnum.DebugChannel;
  284. if (cType == ChatTypeEnum.Region)
  285. cType = ChatTypeEnum.Say;
  286. if (c.Message.Length > 1100)
  287. c.Message = c.Message.Substring(0, 1000);
  288. // broadcast chat works by redistributing every incoming chat
  289. // message to each avatar in the scene.
  290. string fromName = c.From;
  291. UUID fromID = UUID.Zero;
  292. UUID ownerID = UUID.Zero;
  293. ChatSourceType sourceType = ChatSourceType.Object;
  294. if (null != c.Sender)
  295. {
  296. ScenePresence avatar = (c.Scene as Scene).GetScenePresence(c.Sender.AgentId);
  297. fromID = c.Sender.AgentId;
  298. fromName = avatar.Name;
  299. ownerID = c.Sender.AgentId;
  300. sourceType = ChatSourceType.Agent;
  301. }
  302. else if (c.SenderUUID != UUID.Zero)
  303. {
  304. fromID = c.SenderUUID;
  305. ownerID = ((SceneObjectPart)c.SenderObject).OwnerID;
  306. }
  307. // m_log.DebugFormat("[CHAT] Broadcast: fromID {0} fromName {1}, cType {2}, sType {3}", fromID, fromName, cType, sourceType);
  308. HashSet<UUID> receiverIDs = new HashSet<UUID>();
  309. if (c.Scene != null)
  310. {
  311. ((Scene)c.Scene).ForEachRootClient
  312. (
  313. delegate(IClientAPI client)
  314. {
  315. // don't forward SayOwner chat from objects to
  316. // non-owner agents
  317. if ((c.Type == ChatTypeEnum.Owner) &&
  318. (null != c.SenderObject) &&
  319. (((SceneObjectPart)c.SenderObject).OwnerID != client.AgentId))
  320. return;
  321. client.SendChatMessage(c.Message, (byte)cType, CenterOfRegion, fromName, fromID, fromID,
  322. (byte)sourceType, (byte)ChatAudibleLevel.Fully);
  323. receiverIDs.Add(client.AgentId);
  324. }
  325. );
  326. (c.Scene as Scene).EventManager.TriggerOnChatToClients(
  327. fromID, receiverIDs, c.Message, cType, CenterOfRegion, fromName, sourceType, ChatAudibleLevel.Fully);
  328. }
  329. }
  330. /// <summary>
  331. /// Try to send a message to the given presence
  332. /// </summary>
  333. /// <param name="presence">The receiver</param>
  334. /// <param name="fromPos"></param>
  335. /// <param name="regionPos">/param>
  336. /// <param name="fromAgentID"></param>
  337. /// <param name='ownerID'>
  338. /// Owner of the message. For at least some messages from objects, this has to be correctly filled with the owner's UUID.
  339. /// This is the case for script error messages in viewer 3 since LLViewer change EXT-7762
  340. /// </param>
  341. /// <param name="fromName"></param>
  342. /// <param name="type"></param>
  343. /// <param name="message"></param>
  344. /// <param name="src"></param>
  345. /// <returns>true if the message was sent to the receiver, false if it was not sent due to failing a
  346. /// precondition</returns>
  347. protected virtual bool TrySendChatMessage(
  348. ScenePresence presence, Vector3 fromPos, Vector3 regionPos,
  349. UUID fromAgentID, UUID ownerID, string fromName, ChatTypeEnum type,
  350. string message, ChatSourceType src, bool ignoreDistance)
  351. {
  352. if (presence.LifecycleState != ScenePresenceState.Running)
  353. return false;
  354. if (!ignoreDistance)
  355. {
  356. Vector3 fromRegionPos = fromPos + regionPos;
  357. Vector3 toRegionPos = presence.AbsolutePosition +
  358. new Vector3(presence.Scene.RegionInfo.WorldLocX, presence.Scene.RegionInfo.WorldLocY, 0);
  359. int dis = (int)Util.GetDistanceTo(toRegionPos, fromRegionPos);
  360. if (type == ChatTypeEnum.Whisper && dis > m_whisperdistance ||
  361. type == ChatTypeEnum.Say && dis > m_saydistance ||
  362. type == ChatTypeEnum.Shout && dis > m_shoutdistance)
  363. {
  364. return false;
  365. }
  366. }
  367. // TODO: should change so the message is sent through the avatar rather than direct to the ClientView
  368. presence.ControllingClient.SendChatMessage(
  369. message, (byte) type, fromPos, fromName,
  370. fromAgentID, ownerID, (byte)src, (byte)ChatAudibleLevel.Fully);
  371. return true;
  372. }
  373. Dictionary<UUID, System.Threading.Timer> Timers = new Dictionary<UUID, System.Threading.Timer>();
  374. public virtual void ParcelFreezeUser(IClientAPI client, UUID parcelowner, uint flags, UUID target)
  375. {
  376. System.Threading.Timer Timer;
  377. if (flags == 0)
  378. {
  379. FreezeCache.Add(target.ToString());
  380. System.Threading.TimerCallback timeCB = new System.Threading.TimerCallback(OnEndParcelFrozen);
  381. Timer = new System.Threading.Timer(timeCB, target, 30000, 0);
  382. Timers.Add(target, Timer);
  383. }
  384. else
  385. {
  386. FreezeCache.Remove(target.ToString());
  387. Timers.TryGetValue(target, out Timer);
  388. Timers.Remove(target);
  389. Timer.Dispose();
  390. }
  391. }
  392. protected virtual void OnEndParcelFrozen(object avatar)
  393. {
  394. UUID target = (UUID)avatar;
  395. FreezeCache.Remove(target.ToString());
  396. System.Threading.Timer Timer;
  397. Timers.TryGetValue(target, out Timer);
  398. Timers.Remove(target);
  399. Timer.Dispose();
  400. }
  401. #region SimulatorFeaturesRequest
  402. protected static OSDInteger m_SayRange, m_WhisperRange, m_ShoutRange;
  403. protected virtual void OnSimulatorFeaturesRequest(UUID agentID, ref OSDMap features)
  404. {
  405. OSD extras = new OSDMap();
  406. if (features.ContainsKey("OpenSimExtras"))
  407. extras = features["OpenSimExtras"];
  408. else
  409. features["OpenSimExtras"] = extras;
  410. if (m_SayRange == null)
  411. {
  412. // Do this only once
  413. m_SayRange = new OSDInteger(m_saydistance);
  414. m_WhisperRange = new OSDInteger(m_whisperdistance);
  415. m_ShoutRange = new OSDInteger(m_shoutdistance);
  416. }
  417. ((OSDMap)extras)["say-range"] = m_SayRange;
  418. ((OSDMap)extras)["whisper-range"] = m_WhisperRange;
  419. ((OSDMap)extras)["shout-range"] = m_ShoutRange;
  420. }
  421. #endregion
  422. }
  423. }