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. Vector3 regionPos = new Vector3(scene.RegionInfo.WorldLocX, scene.RegionInfo.WorldLocY, 0);
  185. bool checkParcelHide = false;
  186. UUID sourceParcelID = UUID.Zero;
  187. Vector3 hidePos = fromPos;
  188. if (c.Channel == DEBUG_CHANNEL) c.Type = ChatTypeEnum.DebugChannel;
  189. if(!m_scenes.Contains(scene))
  190. {
  191. m_log.WarnFormat("[CHAT]: message from unkown scene {0} ignored",
  192. scene.RegionInfo.RegionName);
  193. return;
  194. }
  195. switch (sourceType)
  196. {
  197. case ChatSourceType.Agent:
  198. ScenePresence avatar = (scene as Scene).GetScenePresence(c.Sender.AgentId);
  199. fromPos = avatar.AbsolutePosition;
  200. fromName = avatar.Name;
  201. fromID = c.Sender.AgentId;
  202. if (avatar.IsViewerUIGod)
  203. { // let gods speak to outside or things may get confusing
  204. fromNamePrefix = m_adminPrefix;
  205. checkParcelHide = false;
  206. }
  207. else
  208. {
  209. checkParcelHide = true;
  210. }
  211. destination = UUID.Zero; // Avatars cant "SayTo"
  212. ownerID = c.Sender.AgentId;
  213. hidePos = fromPos;
  214. break;
  215. case ChatSourceType.Object:
  216. fromID = c.SenderUUID;
  217. if (c.SenderObject != null && c.SenderObject is SceneObjectPart)
  218. {
  219. ownerID = ((SceneObjectPart)c.SenderObject).OwnerID;
  220. if (((SceneObjectPart)c.SenderObject).ParentGroup.IsAttachment)
  221. {
  222. checkParcelHide = true;
  223. hidePos = ((SceneObjectPart)c.SenderObject).ParentGroup.AbsolutePosition;
  224. }
  225. }
  226. break;
  227. }
  228. // TODO: iterate over message
  229. if (message.Length >= 1000) // libomv limit
  230. message = message.Substring(0, 1000);
  231. // m_log.DebugFormat(
  232. // "[CHAT]: DCTA: fromID {0} fromName {1}, region{2}, cType {3}, sType {4}",
  233. // fromID, fromName, scene.RegionInfo.RegionName, c.Type, sourceType);
  234. HashSet<UUID> receiverIDs = new HashSet<UUID>();
  235. if (checkParcelHide)
  236. {
  237. checkParcelHide = false;
  238. if (c.Type < ChatTypeEnum.DebugChannel && destination == UUID.Zero)
  239. {
  240. ILandObject srcland = scene.LandChannel.GetLandObject(hidePos.X, hidePos.Y);
  241. if (srcland != null && !srcland.LandData.SeeAVs)
  242. {
  243. sourceParcelID = srcland.LandData.GlobalID;
  244. checkParcelHide = true;
  245. }
  246. }
  247. }
  248. scene.ForEachScenePresence(
  249. delegate(ScenePresence presence)
  250. {
  251. if (destination != UUID.Zero && presence.UUID != destination)
  252. return;
  253. if(presence.IsChildAgent)
  254. {
  255. if(checkParcelHide)
  256. return;
  257. if (TrySendChatMessage(presence, fromPos, regionPos, fromID,
  258. ownerID, fromNamePrefix + fromName, c.Type,
  259. message, sourceType, (destination != UUID.Zero)))
  260. receiverIDs.Add(presence.UUID);
  261. return;
  262. }
  263. ILandObject Presencecheck = scene.LandChannel.GetLandObject(presence.AbsolutePosition.X, presence.AbsolutePosition.Y);
  264. if (Presencecheck != null)
  265. {
  266. if (checkParcelHide)
  267. {
  268. if (sourceParcelID != Presencecheck.LandData.GlobalID && !presence.IsViewerUIGod)
  269. return;
  270. }
  271. if (c.Sender == null || Presencecheck.IsEitherBannedOrRestricted(c.Sender.AgentId) != true)
  272. {
  273. if (TrySendChatMessage(presence, fromPos, regionPos, fromID,
  274. ownerID, fromNamePrefix + fromName, c.Type,
  275. message, sourceType, (destination != UUID.Zero)))
  276. receiverIDs.Add(presence.UUID);
  277. }
  278. }
  279. });
  280. scene.EventManager.TriggerOnChatToClients(
  281. fromID, receiverIDs, message, c.Type, fromPos, fromName, sourceType, ChatAudibleLevel.Fully);
  282. }
  283. static protected Vector3 CenterOfRegion = new Vector3(128, 128, 30);
  284. public virtual void OnChatBroadcast(Object sender, OSChatMessage c)
  285. {
  286. if (c.Channel != 0 && c.Channel != DEBUG_CHANNEL) return;
  287. ChatTypeEnum cType = c.Type;
  288. if (c.Channel == DEBUG_CHANNEL)
  289. cType = ChatTypeEnum.DebugChannel;
  290. if (cType == ChatTypeEnum.Region)
  291. cType = ChatTypeEnum.Say;
  292. if (c.Message.Length > 1100)
  293. c.Message = c.Message.Substring(0, 1000);
  294. // broadcast chat works by redistributing every incoming chat
  295. // message to each avatar in the scene.
  296. string fromName = c.From;
  297. UUID fromID = UUID.Zero;
  298. UUID ownerID = UUID.Zero;
  299. ChatSourceType sourceType = ChatSourceType.Object;
  300. if (null != c.Sender)
  301. {
  302. ScenePresence avatar = (c.Scene as Scene).GetScenePresence(c.Sender.AgentId);
  303. fromID = c.Sender.AgentId;
  304. fromName = avatar.Name;
  305. ownerID = c.Sender.AgentId;
  306. sourceType = ChatSourceType.Agent;
  307. }
  308. else if (c.SenderUUID != UUID.Zero)
  309. {
  310. if(c.SenderObject == null)
  311. return;
  312. fromID = c.SenderUUID;
  313. ownerID = ((SceneObjectPart)c.SenderObject).OwnerID;
  314. }
  315. // m_log.DebugFormat("[CHAT] Broadcast: fromID {0} fromName {1}, cType {2}, sType {3}", fromID, fromName, cType, sourceType);
  316. HashSet<UUID> receiverIDs = new HashSet<UUID>();
  317. if (c.Scene != null)
  318. {
  319. ((Scene)c.Scene).ForEachRootClient
  320. (
  321. delegate(IClientAPI client)
  322. {
  323. // don't forward SayOwner chat from objects to
  324. // non-owner agents
  325. if ((c.Type == ChatTypeEnum.Owner) &&
  326. (null != c.SenderObject) &&
  327. (((SceneObjectPart)c.SenderObject).OwnerID != client.AgentId))
  328. return;
  329. client.SendChatMessage(c.Message, (byte)cType, CenterOfRegion, fromName, fromID, fromID,
  330. (byte)sourceType, (byte)ChatAudibleLevel.Fully);
  331. receiverIDs.Add(client.AgentId);
  332. }
  333. );
  334. (c.Scene as Scene).EventManager.TriggerOnChatToClients(
  335. fromID, receiverIDs, c.Message, cType, CenterOfRegion, fromName, sourceType, ChatAudibleLevel.Fully);
  336. }
  337. }
  338. /// <summary>
  339. /// Try to send a message to the given presence
  340. /// </summary>
  341. /// <param name="presence">The receiver</param>
  342. /// <param name="fromPos"></param>
  343. /// <param name="regionPos">/param>
  344. /// <param name="fromAgentID"></param>
  345. /// <param name='ownerID'>
  346. /// Owner of the message. For at least some messages from objects, this has to be correctly filled with the owner's UUID.
  347. /// This is the case for script error messages in viewer 3 since LLViewer change EXT-7762
  348. /// </param>
  349. /// <param name="fromName"></param>
  350. /// <param name="type"></param>
  351. /// <param name="message"></param>
  352. /// <param name="src"></param>
  353. /// <returns>true if the message was sent to the receiver, false if it was not sent due to failing a
  354. /// precondition</returns>
  355. protected virtual bool TrySendChatMessage(
  356. ScenePresence presence, Vector3 fromPos, Vector3 regionPos,
  357. UUID fromAgentID, UUID ownerID, string fromName, ChatTypeEnum type,
  358. string message, ChatSourceType src, bool ignoreDistance)
  359. {
  360. if (presence.IsDeleted || presence.IsInTransit || !presence.ControllingClient.IsActive)
  361. return false;
  362. if (!ignoreDistance)
  363. {
  364. float maxDistSQ;
  365. switch(type)
  366. {
  367. case ChatTypeEnum.Whisper:
  368. maxDistSQ = m_whisperdistanceSQ;
  369. break;
  370. case ChatTypeEnum.Say:
  371. maxDistSQ = m_saydistanceSQ;
  372. break;
  373. case ChatTypeEnum.Shout:
  374. maxDistSQ = m_shoutdistanceSQ;
  375. break;
  376. default:
  377. maxDistSQ = -1f;
  378. break;
  379. }
  380. if(maxDistSQ > 0)
  381. {
  382. Vector3 fromRegionPos = fromPos + regionPos;
  383. Vector3 toRegionPos = presence.AbsolutePosition +
  384. new Vector3(presence.Scene.RegionInfo.WorldLocX, presence.Scene.RegionInfo.WorldLocY, 0);
  385. if(maxDistSQ < Vector3.DistanceSquared(toRegionPos, fromRegionPos))
  386. return false;
  387. }
  388. }
  389. // TODO: should change so the message is sent through the avatar rather than direct to the ClientView
  390. presence.ControllingClient.SendChatMessage(
  391. message, (byte) type, fromPos, fromName,
  392. fromAgentID, ownerID, (byte)src, (byte)ChatAudibleLevel.Fully);
  393. return true;
  394. }
  395. Dictionary<UUID, System.Threading.Timer> Timers = new Dictionary<UUID, System.Threading.Timer>();
  396. public virtual void ParcelFreezeUser(IClientAPI client, UUID parcelowner, uint flags, UUID target)
  397. {
  398. System.Threading.Timer Timer;
  399. if (flags == 0)
  400. {
  401. FreezeCache.Add(target.ToString());
  402. System.Threading.TimerCallback timeCB = new System.Threading.TimerCallback(OnEndParcelFrozen);
  403. Timer = new System.Threading.Timer(timeCB, target, 30000, 0);
  404. Timers.Add(target, Timer);
  405. }
  406. else
  407. {
  408. FreezeCache.Remove(target.ToString());
  409. Timers.TryGetValue(target, out Timer);
  410. Timers.Remove(target);
  411. Timer.Dispose();
  412. }
  413. }
  414. protected virtual void OnEndParcelFrozen(object avatar)
  415. {
  416. UUID target = (UUID)avatar;
  417. FreezeCache.Remove(target.ToString());
  418. System.Threading.Timer Timer;
  419. Timers.TryGetValue(target, out Timer);
  420. Timers.Remove(target);
  421. Timer.Dispose();
  422. }
  423. #region SimulatorFeaturesRequest
  424. protected static OSDInteger m_SayRange, m_WhisperRange, m_ShoutRange;
  425. protected virtual void OnSimulatorFeaturesRequest(UUID agentID, ref OSDMap features)
  426. {
  427. OSD extras = new OSDMap();
  428. if (features.ContainsKey("OpenSimExtras"))
  429. extras = features["OpenSimExtras"];
  430. else
  431. features["OpenSimExtras"] = extras;
  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. }