WorldCommModule.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 OpenSim 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. */
  28. using System;
  29. using System.Collections.Generic;
  30. using libsecondlife;
  31. using Nini.Config;
  32. using OpenSim.Framework;
  33. using OpenSim.Region.Environment.Interfaces;
  34. using OpenSim.Region.Environment.Scenes;
  35. /*****************************************************
  36. *
  37. * WorldCommModule
  38. *
  39. *
  40. * Holding place for world comms - basically llListen
  41. * function implementation.
  42. *
  43. * lLListen(integer channel, string name, key id, string msg)
  44. * The name, id, and msg arguments specify the filtering
  45. * criteria. You can pass the empty string
  46. * (or NULL_KEY for id) for these to set a completely
  47. * open filter; this causes the listen() event handler to be
  48. * invoked for all chat on the channel. To listen only
  49. * for chat spoken by a specific object or avatar,
  50. * specify the name and/or id arguments. To listen
  51. * only for a specific command, specify the
  52. * (case-sensitive) msg argument. If msg is not empty,
  53. * listener will only hear strings which are exactly equal
  54. * to msg. You can also use all the arguments to establish
  55. * the most restrictive filtering criteria.
  56. *
  57. * It might be useful for each listener to maintain a message
  58. * digest, with a list of recent messages by UUID. This can
  59. * be used to prevent in-world repeater loops. However, the
  60. * linden functions do not have this capability, so for now
  61. * thats the way it works.
  62. *
  63. * **************************************************/
  64. namespace OpenSim.Region.Environment.Modules
  65. {
  66. public class WorldCommModule : IRegionModule, IWorldComm
  67. {
  68. private Scene m_scene;
  69. private object CommListLock = new object();
  70. private object ListLock = new object();
  71. private string m_name = "WorldCommModule";
  72. private ListenerManager m_listenerManager;
  73. private Queue<ListenerInfo> m_pending;
  74. public WorldCommModule()
  75. {
  76. }
  77. public void Initialise(Scene scene, IConfigSource config)
  78. {
  79. m_scene = scene;
  80. m_scene.RegisterModuleInterface<IWorldComm>(this);
  81. m_listenerManager = new ListenerManager();
  82. m_pending = new Queue<ListenerInfo>();
  83. m_scene.EventManager.OnNewClient += NewClient;
  84. }
  85. public void PostInitialise()
  86. {
  87. }
  88. public void Close()
  89. {
  90. }
  91. public string Name
  92. {
  93. get { return m_name; }
  94. }
  95. public bool IsSharedModule
  96. {
  97. get { return false; }
  98. }
  99. public void NewClient(IClientAPI client)
  100. {
  101. client.OnChatFromViewer += DeliverClientMessage;
  102. }
  103. private void DeliverClientMessage(Object sender, ChatFromViewerArgs e)
  104. {
  105. DeliverMessage(e.Sender.AgentId.ToString(),
  106. e.Type, e.Channel,
  107. e.Sender.FirstName + " " + e.Sender.LastName,
  108. e.Message);
  109. }
  110. public int Listen(uint localID, LLUUID itemID, LLUUID hostID, int channel, string name, string id, string msg)
  111. {
  112. return m_listenerManager.AddListener(localID, itemID, hostID, channel, name, id, msg);
  113. }
  114. public void ListenControl(int handle, int active)
  115. {
  116. if (active == 1)
  117. m_listenerManager.Activate(handle);
  118. else if (active == 0)
  119. m_listenerManager.Dectivate(handle);
  120. }
  121. public void ListenRemove(int handle)
  122. {
  123. m_listenerManager.Remove(handle);
  124. }
  125. public void DeleteListener(LLUUID itemID)
  126. {
  127. lock (ListLock)
  128. {
  129. m_listenerManager.DeleteListener(itemID);
  130. }
  131. }
  132. // This method scans nearby objects and determines if they are listeners,
  133. // and if so if this message fits the filter. If it does, then
  134. // enqueue the message for delivery to the objects listen event handler.
  135. // Objects that do an llSay have their messages delivered here, and for
  136. // nearby avatars, the SimChat function is used.
  137. public void DeliverMessage(string sourceItemID, ChatTypeEnum type, int channel, string name, string msg)
  138. {
  139. SceneObjectPart source = null;
  140. ScenePresence avatar = null;
  141. source = m_scene.GetSceneObjectPart(new LLUUID(sourceItemID));
  142. if (source == null)
  143. {
  144. avatar = m_scene.GetScenePresence(new LLUUID(sourceItemID));
  145. }
  146. if ((avatar != null) || (source != null))
  147. {
  148. // Loop through the objects in the scene
  149. // If they are in proximity, then if they are
  150. // listeners, if so add them to the pending queue
  151. foreach (LLUUID eb in m_scene.Entities.Keys)
  152. {
  153. EntityBase sPart;
  154. m_scene.Entities.TryGetValue(eb, out sPart);
  155. // Dont process if this message is from itself!
  156. if (eb.ToString().Equals(sourceItemID) ||
  157. sPart.UUID.ToString().Equals(sourceItemID))
  158. continue;
  159. double dis = 0;
  160. if (source != null)
  161. dis = Util.GetDistanceTo(sPart.AbsolutePosition, source.AbsolutePosition);
  162. else
  163. dis = Util.GetDistanceTo(sPart.AbsolutePosition, avatar.AbsolutePosition);
  164. switch (type)
  165. {
  166. case ChatTypeEnum.Whisper:
  167. if ((dis < 10) && (dis > -10))
  168. {
  169. ListenerInfo isListener = m_listenerManager.IsListenerMatch(
  170. sourceItemID, sPart.UUID, channel, name, msg
  171. );
  172. if (isListener != null)
  173. {
  174. m_pending.Enqueue(isListener);
  175. }
  176. }
  177. break;
  178. case ChatTypeEnum.Say:
  179. if ((dis < 30) && (dis > -30))
  180. {
  181. ListenerInfo isListener = m_listenerManager.IsListenerMatch(
  182. sourceItemID, sPart.UUID, channel, name, msg
  183. );
  184. if (isListener != null)
  185. {
  186. m_pending.Enqueue(isListener);
  187. }
  188. }
  189. break;
  190. case ChatTypeEnum.Shout:
  191. if ((dis < 100) && (dis > -100))
  192. {
  193. ListenerInfo isListener = m_listenerManager.IsListenerMatch(
  194. sourceItemID, sPart.UUID, channel, name, msg
  195. );
  196. if (isListener != null)
  197. {
  198. m_pending.Enqueue(isListener);
  199. }
  200. }
  201. break;
  202. case ChatTypeEnum.Broadcast:
  203. ListenerInfo isListen =
  204. m_listenerManager.IsListenerMatch(sourceItemID, eb, channel, name, msg);
  205. if (isListen != null)
  206. {
  207. ListenerInfo isListener = m_listenerManager.IsListenerMatch(
  208. sourceItemID, sPart.UUID, channel, name, msg
  209. );
  210. if (isListener != null)
  211. {
  212. m_pending.Enqueue(isListener);
  213. }
  214. }
  215. break;
  216. }
  217. }
  218. ;
  219. }
  220. }
  221. public bool HasMessages()
  222. {
  223. return (m_pending.Count > 0);
  224. }
  225. public ListenerInfo GetNextMessage()
  226. {
  227. ListenerInfo li = null;
  228. lock (CommListLock)
  229. {
  230. li = m_pending.Dequeue();
  231. }
  232. return li;
  233. }
  234. }
  235. // hostID: the ID of the ScenePart
  236. // itemID: the ID of the script host engine
  237. // localID: local ID of host engine
  238. public class ListenerManager
  239. {
  240. private Dictionary<int, ListenerInfo> m_listeners;
  241. private object ListenersLock = new object();
  242. private int m_MaxListeners = 100;
  243. public ListenerManager()
  244. {
  245. m_listeners = new Dictionary<int, ListenerInfo>();
  246. }
  247. public int AddListener(uint localID, LLUUID itemID, LLUUID hostID, int channel, string name, string id,
  248. string msg)
  249. {
  250. if (m_listeners.Count < m_MaxListeners)
  251. {
  252. ListenerInfo isListener = IsListenerMatch(LLUUID.Zero.ToString(), itemID, channel, name, msg);
  253. if (isListener == null)
  254. {
  255. int newHandle = GetNewHandle();
  256. if (newHandle > -1)
  257. {
  258. ListenerInfo li = new ListenerInfo(localID, newHandle, itemID, hostID, channel, name, id, msg);
  259. lock (ListenersLock)
  260. {
  261. m_listeners.Add(newHandle, li);
  262. }
  263. return newHandle;
  264. }
  265. }
  266. }
  267. return -1;
  268. }
  269. public void Remove(int handle)
  270. {
  271. m_listeners.Remove(handle);
  272. }
  273. public void DeleteListener(LLUUID itemID)
  274. {
  275. foreach (ListenerInfo li in m_listeners.Values)
  276. {
  277. if (li.GetItemID().Equals(itemID))
  278. {
  279. Remove(li.GetHandle());
  280. return;
  281. }
  282. }
  283. }
  284. private int GetNewHandle()
  285. {
  286. for (int i = 0; i < int.MaxValue - 1; i++)
  287. {
  288. if (!m_listeners.ContainsKey(i))
  289. return i;
  290. }
  291. return -1;
  292. }
  293. public bool IsListener(LLUUID hostID)
  294. {
  295. foreach (ListenerInfo li in m_listeners.Values)
  296. {
  297. if (li.GetHostID().Equals(hostID))
  298. return true;
  299. }
  300. return false;
  301. }
  302. public void Activate(int handle)
  303. {
  304. ListenerInfo li;
  305. if (m_listeners.TryGetValue(handle, out li))
  306. {
  307. li.Activate();
  308. }
  309. }
  310. public void Dectivate(int handle)
  311. {
  312. ListenerInfo li;
  313. if (m_listeners.TryGetValue(handle, out li))
  314. {
  315. li.Deactivate();
  316. }
  317. }
  318. // Theres probably a more clever and efficient way to
  319. // do this, maybe with regex.
  320. public ListenerInfo IsListenerMatch(string sourceItemID, LLUUID listenerKey, int channel, string name,
  321. string msg)
  322. {
  323. bool isMatch = true;
  324. foreach (ListenerInfo li in m_listeners.Values)
  325. {
  326. if (li.GetHostID().Equals(listenerKey))
  327. {
  328. if (li.IsActive())
  329. {
  330. if (channel == li.GetChannel())
  331. {
  332. if ((li.GetID().ToString().Length > 0) &&
  333. (!li.GetID().Equals(LLUUID.Zero)))
  334. {
  335. if (!li.GetID().ToString().Equals(sourceItemID))
  336. {
  337. isMatch = false;
  338. }
  339. }
  340. if (isMatch && (li.GetName().Length > 0))
  341. {
  342. if (li.GetName().Equals(name))
  343. {
  344. isMatch = false;
  345. }
  346. }
  347. if (isMatch)
  348. {
  349. return new ListenerInfo(
  350. li.GetLocalID(), li.GetHandle(), li.GetItemID(), li.GetHostID(),
  351. li.GetChannel(), name, li.GetID(), msg, new LLUUID(sourceItemID)
  352. );
  353. }
  354. }
  355. }
  356. }
  357. }
  358. return null;
  359. }
  360. }
  361. public class ListenerInfo
  362. {
  363. private LLUUID m_itemID; // ID of the host script engine
  364. private LLUUID m_hostID; // ID of the host/scene part
  365. private LLUUID m_sourceItemID; // ID of the scenePart or avatar source of the message
  366. private int m_channel; // Channel
  367. private int m_handle; // Assigned handle of this listener
  368. private uint m_localID; // Local ID from script engine
  369. private string m_name; // Object name to filter messages from
  370. private LLUUID m_id; // ID to filter messages from
  371. private string m_message; // The message
  372. private bool m_active; // Listener is active or not
  373. public ListenerInfo(uint localID, int handle, LLUUID ItemID, LLUUID hostID, int channel, string name, LLUUID id,
  374. string message)
  375. {
  376. Initialise(localID, handle, ItemID, hostID, channel, name, id, message);
  377. }
  378. public ListenerInfo(uint localID, int handle, LLUUID ItemID, LLUUID hostID, int channel, string name, LLUUID id,
  379. string message, LLUUID sourceItemID)
  380. {
  381. Initialise(localID, handle, ItemID, hostID, channel, name, id, message);
  382. m_sourceItemID = sourceItemID;
  383. }
  384. private void Initialise(uint localID, int handle, LLUUID ItemID, LLUUID hostID, int channel, string name,
  385. LLUUID id, string message)
  386. {
  387. m_handle = handle;
  388. m_channel = channel;
  389. m_itemID = ItemID;
  390. m_hostID = hostID;
  391. m_name = name;
  392. m_id = id;
  393. m_message = message;
  394. m_active = true;
  395. m_localID = localID;
  396. }
  397. public LLUUID GetItemID()
  398. {
  399. return m_itemID;
  400. }
  401. public LLUUID GetHostID()
  402. {
  403. return m_hostID;
  404. }
  405. public LLUUID GetSourceItemID()
  406. {
  407. return m_sourceItemID;
  408. }
  409. public int GetChannel()
  410. {
  411. return m_channel;
  412. }
  413. public uint GetLocalID()
  414. {
  415. return m_localID;
  416. }
  417. public int GetHandle()
  418. {
  419. return m_handle;
  420. }
  421. public string GetMessage()
  422. {
  423. return m_message;
  424. }
  425. public string GetName()
  426. {
  427. return m_name;
  428. }
  429. public bool IsActive()
  430. {
  431. return m_active;
  432. }
  433. public void Deactivate()
  434. {
  435. m_active = false;
  436. }
  437. public void Activate()
  438. {
  439. m_active = true;
  440. }
  441. public LLUUID GetID()
  442. {
  443. return m_id;
  444. }
  445. }
  446. }