ChatModule.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. {
  102. featuresModule.AddOpenSimExtraFeature("say-range", new OSDInteger(m_saydistance));
  103. featuresModule.AddOpenSimExtraFeature("whisper-range", new OSDInteger(m_whisperdistance));
  104. featuresModule.AddOpenSimExtraFeature("shout-range", new OSDInteger(m_shoutdistance));
  105. }
  106. }
  107. public virtual void RemoveRegion(Scene scene)
  108. {
  109. if (!m_enabled) return;
  110. lock (m_syncy)
  111. {
  112. if (m_scenes.Contains(scene))
  113. {
  114. scene.EventManager.OnNewClient -= OnNewClient;
  115. scene.EventManager.OnChatFromWorld -= OnChatFromWorld;
  116. scene.EventManager.OnChatBroadcast -= OnChatBroadcast;
  117. m_scenes.Remove(scene);
  118. }
  119. }
  120. }
  121. public virtual void Close()
  122. {
  123. }
  124. public virtual void PostInitialise()
  125. {
  126. }
  127. public virtual Type ReplaceableInterface
  128. {
  129. get { return null; }
  130. }
  131. public virtual string Name
  132. {
  133. get { return "ChatModule"; }
  134. }
  135. #endregion
  136. public virtual void OnNewClient(IClientAPI client)
  137. {
  138. client.OnChatFromClient += OnChatFromClient;
  139. }
  140. public virtual void OnChatFromClient(Object sender, OSChatMessage c)
  141. {
  142. // redistribute to interested subscribers
  143. Scene scene = (Scene)c.Scene;
  144. scene.EventManager.TriggerOnChatFromClient(sender, c);
  145. // early return if not on public or debug channel
  146. if (c.Channel != 0 && c.Channel != DEBUG_CHANNEL) return;
  147. // sanity check:
  148. if (c.Sender == null)
  149. {
  150. m_log.ErrorFormat("[CHAT]: OnChatFromClient from {0} has empty Sender field!", sender);
  151. return;
  152. }
  153. if (FreezeCache.Contains(c.Sender.AgentId.ToString()))
  154. {
  155. if (c.Type != ChatTypeEnum.StartTyping || c.Type != ChatTypeEnum.StopTyping)
  156. c.Sender.SendAgentAlertMessage("You may not talk as you are frozen.", false);
  157. }
  158. else
  159. {
  160. DeliverChatToAvatars(ChatSourceType.Agent, c);
  161. }
  162. }
  163. public virtual void OnChatFromWorld(Object sender, OSChatMessage c)
  164. {
  165. // early return if not on public or debug channel
  166. if (c.Channel != 0 && c.Channel != DEBUG_CHANNEL) return;
  167. DeliverChatToAvatars(ChatSourceType.Object, c);
  168. }
  169. protected virtual void DeliverChatToAvatars(ChatSourceType sourceType, OSChatMessage c)
  170. {
  171. string fromName = c.From;
  172. string fromNamePrefix = "";
  173. UUID fromID = UUID.Zero;
  174. UUID ownerID = UUID.Zero;
  175. string message = c.Message;
  176. Scene scene = c.Scene as Scene;
  177. UUID destination = c.Destination;
  178. Vector3 fromPos = c.Position;
  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.GetScenePresence(c.Sender.AgentId);
  193. if(avatar == null)
  194. return;
  195. fromPos = avatar.AbsolutePosition;
  196. fromName = avatar.Name;
  197. fromID = c.Sender.AgentId;
  198. if (avatar.IsViewerUIGod)
  199. { // let gods speak to outside or things may get confusing
  200. fromNamePrefix = m_adminPrefix;
  201. checkParcelHide = false;
  202. }
  203. else
  204. {
  205. checkParcelHide = true;
  206. }
  207. destination = UUID.Zero; // Avatars cant "SayTo"
  208. ownerID = c.Sender.AgentId;
  209. hidePos = fromPos;
  210. break;
  211. case ChatSourceType.Object:
  212. fromID = c.SenderUUID;
  213. if (c.SenderObject != null && c.SenderObject is SceneObjectPart)
  214. {
  215. ownerID = ((SceneObjectPart)c.SenderObject).OwnerID;
  216. if (((SceneObjectPart)c.SenderObject).ParentGroup.IsAttachment)
  217. {
  218. checkParcelHide = true;
  219. hidePos = ((SceneObjectPart)c.SenderObject).ParentGroup.AbsolutePosition;
  220. }
  221. }
  222. break;
  223. }
  224. if (message.Length > 1100)
  225. message = message.Substring(0, 1000);
  226. //m_log.DebugFormat(
  227. // "[CHAT]: DCTA: fromID {0} fromName {1}, region{2}, cType {3}, sType {4}",
  228. // fromID, fromName, scene.RegionInfo.RegionName, c.Type, sourceType);
  229. if (checkParcelHide)
  230. {
  231. checkParcelHide = false;
  232. if (c.Type < ChatTypeEnum.DebugChannel && destination.IsZero())
  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. Vector3 regionPos = new Vector3(scene.RegionInfo.WorldLocX, scene.RegionInfo.WorldLocY, 0);
  243. scene.ForEachScenePresence(
  244. delegate(ScenePresence presence)
  245. {
  246. if (destination.IsNotZero() && presence.UUID.NotEqual(destination))
  247. return;
  248. if(presence.IsChildAgent)
  249. {
  250. if(!checkParcelHide)
  251. {
  252. TrySendChatMessage(presence, fromPos, regionPos, fromID,
  253. ownerID, fromNamePrefix + fromName, c.Type,
  254. message, sourceType, destination.IsNotZero());
  255. }
  256. return;
  257. }
  258. ILandObject Presencecheck = scene.LandChannel.GetLandObject(presence.AbsolutePosition.X, presence.AbsolutePosition.Y);
  259. if (Presencecheck != null)
  260. {
  261. if (checkParcelHide)
  262. {
  263. if (sourceParcelID.NotEqual(Presencecheck.LandData.GlobalID) && !presence.IsViewerUIGod)
  264. return;
  265. }
  266. if (c.Sender == null || !Presencecheck.IsEitherBannedOrRestricted(c.Sender.AgentId))
  267. {
  268. TrySendChatMessage(presence, fromPos, regionPos, fromID,
  269. ownerID, fromNamePrefix + fromName, c.Type,
  270. message, sourceType, destination.IsNotZero());
  271. }
  272. }
  273. });
  274. }
  275. static protected Vector3 CenterOfRegion = new Vector3(128, 128, 30);
  276. public virtual void OnChatBroadcast(Object sender, OSChatMessage c)
  277. {
  278. if (c.Channel != 0 && c.Channel != DEBUG_CHANNEL) return;
  279. ChatTypeEnum cType;
  280. if (c.Channel == DEBUG_CHANNEL)
  281. cType = ChatTypeEnum.DebugChannel;
  282. else if (c.Type == ChatTypeEnum.Region)
  283. cType = ChatTypeEnum.Say;
  284. else
  285. cType = c.Type;
  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;
  292. UUID ownerID;
  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 = UUID.Zero;
  300. sourceType = ChatSourceType.Agent;
  301. }
  302. else if (c.SenderUUID.IsNotZero())
  303. {
  304. if(c.SenderObject == null)
  305. return;
  306. fromID = c.SenderUUID;
  307. ownerID = ((SceneObjectPart)c.SenderObject).OwnerID;
  308. sourceType = ChatSourceType.Object;
  309. }
  310. else
  311. {
  312. sourceType = ChatSourceType.Object;
  313. fromID = UUID.Zero;
  314. ownerID = UUID.Zero;
  315. }
  316. // m_log.DebugFormat("[CHAT] Broadcast: fromID {0} fromName {1}, cType {2}, sType {3}", fromID, fromName, cType, sourceType);
  317. Scene scene = c.Scene as Scene;
  318. if (scene != null)
  319. {
  320. scene.ForEachRootClient
  321. (
  322. delegate(IClientAPI client)
  323. {
  324. // don't forward SayOwner chat from objects to
  325. // non-owner agents
  326. if ((c.Type == ChatTypeEnum.Owner) &&
  327. (null != c.SenderObject) &&
  328. (((SceneObjectPart)c.SenderObject).OwnerID.NotEqual(client.AgentId)))
  329. return;
  330. client.SendChatMessage(c.Message, (byte)cType, CenterOfRegion, fromName, fromID, ownerID,
  331. (byte)sourceType, (byte)ChatAudibleLevel.Fully);
  332. }
  333. );
  334. }
  335. }
  336. /// <summary>
  337. /// Try to send a message to the given presence
  338. /// </summary>
  339. /// <param name="presence">The receiver</param>
  340. /// <param name="fromPos"></param>
  341. /// <param name="regionPos">/param>
  342. /// <param name="fromAgentID"></param>
  343. /// <param name='ownerID'>
  344. /// Owner of the message. For at least some messages from objects, this has to be correctly filled with the owner's UUID.
  345. /// This is the case for script error messages in viewer 3 since LLViewer change EXT-7762
  346. /// </param>
  347. /// <param name="fromName"></param>
  348. /// <param name="type"></param>
  349. /// <param name="message"></param>
  350. /// <param name="src"></param>
  351. /// <returns>true if the message was sent to the receiver, false if it was not sent due to failing a
  352. /// precondition</returns>
  353. protected virtual bool TrySendChatMessage(
  354. ScenePresence presence, Vector3 fromPos, Vector3 regionPos,
  355. UUID fromAgentID, UUID ownerID, string fromName, ChatTypeEnum type,
  356. string message, ChatSourceType src, bool ignoreDistance)
  357. {
  358. if (presence.IsDeleted || presence.IsInTransit || !presence.ControllingClient.IsActive)
  359. return false;
  360. if (!ignoreDistance)
  361. {
  362. float maxDistSQ;
  363. switch(type)
  364. {
  365. case ChatTypeEnum.Whisper:
  366. maxDistSQ = m_whisperdistanceSQ;
  367. break;
  368. case ChatTypeEnum.Say:
  369. maxDistSQ = m_saydistanceSQ;
  370. break;
  371. case ChatTypeEnum.Shout:
  372. maxDistSQ = m_shoutdistanceSQ;
  373. break;
  374. default:
  375. maxDistSQ = -1f;
  376. break;
  377. }
  378. if(maxDistSQ > 0)
  379. {
  380. Vector3 fromRegionPos = fromPos + regionPos;
  381. Vector3 toRegionPos = presence.AbsolutePosition +
  382. new Vector3(presence.Scene.RegionInfo.WorldLocX, presence.Scene.RegionInfo.WorldLocY, 0);
  383. if(maxDistSQ < Vector3.DistanceSquared(toRegionPos, fromRegionPos))
  384. return false;
  385. }
  386. }
  387. presence.ControllingClient.SendChatMessage(
  388. message, (byte) type, fromPos, fromName,
  389. fromAgentID, ownerID, (byte)src, (byte)ChatAudibleLevel.Fully);
  390. return true;
  391. }
  392. Dictionary<UUID, System.Threading.Timer> Timers = new Dictionary<UUID, System.Threading.Timer>();
  393. public virtual void ParcelFreezeUser(IClientAPI client, UUID parcelowner, uint flags, UUID target)
  394. {
  395. System.Threading.Timer Timer;
  396. if (flags == 0)
  397. {
  398. FreezeCache.Add(target.ToString());
  399. System.Threading.TimerCallback timeCB = new System.Threading.TimerCallback(OnEndParcelFrozen);
  400. Timer = new System.Threading.Timer(timeCB, target, 30000, 0);
  401. Timers.Add(target, Timer);
  402. }
  403. else
  404. {
  405. FreezeCache.Remove(target.ToString());
  406. Timers.TryGetValue(target, out Timer);
  407. Timers.Remove(target);
  408. Timer.Dispose();
  409. }
  410. }
  411. protected virtual void OnEndParcelFrozen(object avatar)
  412. {
  413. UUID target = (UUID)avatar;
  414. FreezeCache.Remove(target.ToString());
  415. System.Threading.Timer Timer;
  416. Timers.TryGetValue(target, out Timer);
  417. Timers.Remove(target);
  418. Timer.Dispose();
  419. }
  420. }
  421. }