ChatModule.cs 20 KB

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