WorldCommModule.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. using System;
  28. using System.Collections;
  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 m_pendingQ;
  74. private Queue m_pending;
  75. public WorldCommModule()
  76. {
  77. }
  78. public void Initialise(Scene scene, IConfigSource config)
  79. {
  80. m_scene = scene;
  81. m_scene.RegisterModuleInterface<IWorldComm>(this);
  82. m_listenerManager = new ListenerManager();
  83. m_scene.EventManager.OnNewClient += NewClient;
  84. m_pendingQ = new Queue();
  85. m_pending = Queue.Synchronized(m_pendingQ);
  86. }
  87. public void PostInitialise()
  88. {
  89. }
  90. public void Close()
  91. {
  92. }
  93. public string Name
  94. {
  95. get { return m_name; }
  96. }
  97. public bool IsSharedModule
  98. {
  99. get { return false; }
  100. }
  101. public void NewClient(IClientAPI client)
  102. {
  103. client.OnChatFromViewer += DeliverClientMessage;
  104. }
  105. /********************************************************************
  106. *
  107. * Listener Stuff
  108. *
  109. * *****************************************************************/
  110. private void DeliverClientMessage(Object sender, ChatFromViewerArgs e)
  111. {
  112. DeliverMessage(e.Sender.AgentId.ToString(),
  113. e.Type, e.Channel,
  114. e.Sender.FirstName + " " + e.Sender.LastName,
  115. e.Message);
  116. }
  117. public int Listen(uint localID, LLUUID itemID, LLUUID hostID, int channel, string name, string id, string msg)
  118. {
  119. return m_listenerManager.AddListener(localID, itemID, hostID, channel, name, id, msg);
  120. }
  121. public void ListenControl(int handle, int active)
  122. {
  123. if (m_listenerManager != null)
  124. {
  125. if (active == 1)
  126. m_listenerManager.Activate(handle);
  127. else if (active == 0)
  128. m_listenerManager.Dectivate(handle);
  129. }
  130. }
  131. public void ListenRemove(int handle)
  132. {
  133. if (m_listenerManager != null)
  134. {
  135. m_listenerManager.Remove(handle);
  136. }
  137. }
  138. public void DeleteListener(LLUUID itemID)
  139. {
  140. if (m_listenerManager != null)
  141. {
  142. m_listenerManager.DeleteListener(itemID);
  143. }
  144. }
  145. // This method scans nearby objects and determines if they are listeners,
  146. // and if so if this message fits the filter. If it does, then
  147. // enqueue the message for delivery to the objects listen event handler.
  148. // Objects that do an llSay have their messages delivered here, and for
  149. // nearby avatars, the SimChat function is used.
  150. public void DeliverMessage(string sourceItemID, ChatTypeEnum type, int channel, string name, string msg)
  151. {
  152. SceneObjectPart source = null;
  153. ScenePresence avatar = null;
  154. source = m_scene.GetSceneObjectPart(new LLUUID(sourceItemID));
  155. if (source == null)
  156. {
  157. avatar = m_scene.GetScenePresence(new LLUUID(sourceItemID));
  158. }
  159. if ((avatar != null) || (source != null))
  160. {
  161. // Loop through the objects in the scene
  162. // If they are in proximity, then if they are
  163. // listeners, if so add them to the pending queue
  164. foreach (ListenerInfo li in m_listenerManager.GetListeners())
  165. {
  166. EntityBase sPart;
  167. m_scene.Entities.TryGetValue(li.GetHostID(), out sPart);
  168. if (sPart != null)
  169. {
  170. // Dont process if this message is from itself!
  171. if (li.GetHostID().ToString().Equals(sourceItemID) ||
  172. sPart.UUID.ToString().Equals(sourceItemID))
  173. continue;
  174. double dis = 0;
  175. if (source != null)
  176. dis = Util.GetDistanceTo(sPart.AbsolutePosition, source.AbsolutePosition);
  177. else
  178. dis = Util.GetDistanceTo(sPart.AbsolutePosition, avatar.AbsolutePosition);
  179. switch (type)
  180. {
  181. case ChatTypeEnum.Whisper:
  182. if ((dis < 10) && (dis > -10))
  183. {
  184. if (li.GetChannel() == channel)
  185. {
  186. ListenerInfo isListener = m_listenerManager.IsListenerMatch(
  187. sourceItemID, sPart.UUID, channel, name, msg
  188. );
  189. if (isListener != null)
  190. {
  191. lock (m_pending.SyncRoot)
  192. {
  193. m_pending.Enqueue(isListener);
  194. }
  195. }
  196. }
  197. }
  198. break;
  199. case ChatTypeEnum.Say:
  200. if ((dis < 30) && (dis > -30))
  201. {
  202. if (li.GetChannel() == channel)
  203. {
  204. ListenerInfo isListener = m_listenerManager.IsListenerMatch(
  205. sourceItemID, sPart.UUID, channel, name, msg
  206. );
  207. if (isListener != null)
  208. {
  209. lock (m_pending.SyncRoot)
  210. {
  211. m_pending.Enqueue(isListener);
  212. }
  213. }
  214. }
  215. }
  216. break;
  217. case ChatTypeEnum.Shout:
  218. if ((dis < 100) && (dis > -100))
  219. {
  220. if (li.GetChannel() == channel)
  221. {
  222. ListenerInfo isListener = m_listenerManager.IsListenerMatch(
  223. sourceItemID, sPart.UUID, channel, name, msg
  224. );
  225. if (isListener != null)
  226. {
  227. lock (m_pending.SyncRoot)
  228. {
  229. m_pending.Enqueue(isListener);
  230. }
  231. }
  232. }
  233. }
  234. break;
  235. case ChatTypeEnum.Broadcast:
  236. ListenerInfo isListen =
  237. m_listenerManager.IsListenerMatch(sourceItemID, li.GetItemID(), channel, name, msg);
  238. if (isListen != null)
  239. {
  240. ListenerInfo isListener = m_listenerManager.IsListenerMatch(
  241. sourceItemID, sPart.UUID, channel, name, msg
  242. );
  243. if (isListener != null)
  244. {
  245. lock (m_pending.SyncRoot)
  246. {
  247. m_pending.Enqueue(isListener);
  248. }
  249. }
  250. }
  251. break;
  252. }
  253. }
  254. }
  255. }
  256. }
  257. public bool HasMessages()
  258. {
  259. if (m_pending != null)
  260. return (m_pending.Count > 0);
  261. else
  262. return false;
  263. }
  264. public ListenerInfo GetNextMessage()
  265. {
  266. ListenerInfo li = null;
  267. lock (m_pending.SyncRoot)
  268. {
  269. li = (ListenerInfo)m_pending.Dequeue();
  270. }
  271. return li;
  272. }
  273. public uint PeekNextMessageLocalID()
  274. {
  275. return ((ListenerInfo)m_pending.Peek()).GetLocalID();
  276. }
  277. public LLUUID PeekNextMessageItemID()
  278. {
  279. return ((ListenerInfo)m_pending.Peek()).GetItemID();
  280. }
  281. }
  282. /**********************************************************
  283. *
  284. * Even more listener stuff
  285. *
  286. * ********************************************************/
  287. public class ListenerManager
  288. {
  289. //private Dictionary<int, ListenerInfo> m_listeners;
  290. private Hashtable m_listeners = Hashtable.Synchronized(new Hashtable());
  291. private object ListenersLock = new object();
  292. private int m_MaxListeners = 100;
  293. public int AddListener(uint localID, LLUUID itemID, LLUUID hostID, int channel, string name, string id, string msg)
  294. {
  295. if (m_listeners.Count < m_MaxListeners)
  296. {
  297. ListenerInfo isListener = IsListenerMatch(LLUUID.Zero.ToString(), itemID, channel, name, msg);
  298. if (isListener == null)
  299. {
  300. int newHandle = GetNewHandle();
  301. if (newHandle > -1)
  302. {
  303. ListenerInfo li = new ListenerInfo(localID, newHandle, itemID, hostID, channel, name, id, msg);
  304. lock (m_listeners.SyncRoot)
  305. {
  306. m_listeners.Add(newHandle, li);
  307. }
  308. return newHandle;
  309. }
  310. }
  311. }
  312. return -1;
  313. }
  314. public void Remove(int handle)
  315. {
  316. lock (m_listeners.SyncRoot)
  317. {
  318. m_listeners.Remove(handle);
  319. }
  320. }
  321. public void DeleteListener(LLUUID itemID)
  322. {
  323. ArrayList removedListeners = new ArrayList();
  324. lock (m_listeners.SyncRoot)
  325. {
  326. IDictionaryEnumerator en = m_listeners.GetEnumerator();
  327. while (en.MoveNext())
  328. {
  329. ListenerInfo li = (ListenerInfo)en.Value;
  330. if (li.GetItemID().Equals(itemID))
  331. {
  332. removedListeners.Add(li.GetHandle());
  333. }
  334. }
  335. foreach (int handle in removedListeners)
  336. {
  337. m_listeners.Remove(handle);
  338. }
  339. }
  340. }
  341. private int GetNewHandle()
  342. {
  343. for (int i = 0; i < int.MaxValue - 1; i++)
  344. {
  345. if (!m_listeners.ContainsKey(i))
  346. return i;
  347. }
  348. return -1;
  349. }
  350. public bool IsListener(LLUUID hostID)
  351. {
  352. foreach (ListenerInfo li in m_listeners.Values)
  353. {
  354. if (li.GetHostID().Equals(hostID))
  355. return true;
  356. }
  357. return false;
  358. }
  359. public void Activate(int handle)
  360. {
  361. if (m_listeners.ContainsKey(handle))
  362. {
  363. lock (m_listeners.SyncRoot)
  364. {
  365. ListenerInfo li = (ListenerInfo)m_listeners[handle];
  366. li.Activate();
  367. }
  368. }
  369. }
  370. public void Dectivate(int handle)
  371. {
  372. if (m_listeners.ContainsKey(handle))
  373. {
  374. ListenerInfo li = (ListenerInfo)m_listeners[handle];
  375. li.Deactivate();
  376. }
  377. }
  378. // Theres probably a more clever and efficient way to
  379. // do this, maybe with regex.
  380. public ListenerInfo IsListenerMatch(string sourceItemID, LLUUID listenerKey, int channel, string name,
  381. string msg)
  382. {
  383. bool isMatch = true;
  384. lock (m_listeners.SyncRoot)
  385. {
  386. IDictionaryEnumerator en = m_listeners.GetEnumerator();
  387. while (en.MoveNext())
  388. {
  389. ListenerInfo li = (ListenerInfo)en.Value;
  390. if (li.IsActive())
  391. {
  392. if (li.GetHostID().Equals(listenerKey))
  393. {
  394. if (channel == li.GetChannel())
  395. {
  396. if ((li.GetID().ToString().Length > 0) &&
  397. (!li.GetID().Equals(LLUUID.Zero)))
  398. {
  399. if (!li.GetID().ToString().Equals(sourceItemID))
  400. {
  401. isMatch = false;
  402. }
  403. }
  404. if (isMatch && (li.GetName().Length > 0))
  405. {
  406. if (li.GetName().Equals(name))
  407. {
  408. isMatch = false;
  409. }
  410. }
  411. if (isMatch)
  412. {
  413. return new ListenerInfo(
  414. li.GetLocalID(), li.GetHandle(), li.GetItemID(), li.GetHostID(),
  415. li.GetChannel(), name, li.GetID(), msg, new LLUUID(sourceItemID)
  416. );
  417. }
  418. }
  419. }
  420. }
  421. }
  422. }
  423. return null;
  424. }
  425. public ICollection GetListeners()
  426. {
  427. return m_listeners.Values;
  428. }
  429. }
  430. public class ListenerInfo
  431. {
  432. private LLUUID m_itemID; // ID of the host script engine
  433. private LLUUID m_hostID; // ID of the host/scene part
  434. private LLUUID m_sourceItemID; // ID of the scenePart or avatar source of the message
  435. private int m_channel; // Channel
  436. private int m_handle; // Assigned handle of this listener
  437. private uint m_localID; // Local ID from script engine
  438. private string m_name; // Object name to filter messages from
  439. private LLUUID m_id; // ID to filter messages from
  440. private string m_message; // The message
  441. private bool m_active; // Listener is active or not
  442. public ListenerInfo(uint localID, int handle, LLUUID ItemID, LLUUID hostID, int channel, string name, LLUUID id, string message)
  443. {
  444. Initialise(localID, handle, ItemID, hostID, channel, name, id, message);
  445. }
  446. public ListenerInfo(uint localID, int handle, LLUUID ItemID, LLUUID hostID, int channel, string name, LLUUID id,
  447. string message, LLUUID sourceItemID)
  448. {
  449. Initialise(localID, handle, ItemID, hostID, channel, name, id, message);
  450. m_sourceItemID = sourceItemID;
  451. }
  452. private void Initialise(uint localID, int handle, LLUUID ItemID, LLUUID hostID, int channel, string name,
  453. LLUUID id, string message)
  454. {
  455. m_handle = handle;
  456. m_channel = channel;
  457. m_itemID = ItemID;
  458. m_hostID = hostID;
  459. m_name = name;
  460. m_id = id;
  461. m_message = message;
  462. m_active = true;
  463. m_localID = localID;
  464. }
  465. public LLUUID GetItemID()
  466. {
  467. return m_itemID;
  468. }
  469. public LLUUID GetHostID()
  470. {
  471. return m_hostID;
  472. }
  473. public LLUUID GetSourceItemID()
  474. {
  475. return m_sourceItemID;
  476. }
  477. public int GetChannel()
  478. {
  479. return m_channel;
  480. }
  481. public uint GetLocalID()
  482. {
  483. return m_localID;
  484. }
  485. public int GetHandle()
  486. {
  487. return m_handle;
  488. }
  489. public string GetMessage()
  490. {
  491. return m_message;
  492. }
  493. public string GetName()
  494. {
  495. return m_name;
  496. }
  497. public bool IsActive()
  498. {
  499. return m_active;
  500. }
  501. public void Deactivate()
  502. {
  503. m_active = false;
  504. }
  505. public void Activate()
  506. {
  507. m_active = true;
  508. }
  509. public LLUUID GetID()
  510. {
  511. return m_id;
  512. }
  513. }
  514. }