1
0

ConciergeModule.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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 System.IO;
  31. using System.Net.Sockets;
  32. using System.Reflection;
  33. using System.Text.RegularExpressions;
  34. using System.Threading;
  35. using log4net;
  36. using Nini.Config;
  37. using Nwc.XmlRpc;
  38. using OpenMetaverse;
  39. using OpenSim.Framework;
  40. using OpenSim.Framework.Servers;
  41. using OpenSim.Region.Environment.Interfaces;
  42. using OpenSim.Region.Environment.Scenes;
  43. using OpenSim.Region.Environment.Modules.Avatar.Chat;
  44. namespace OpenSim.Region.Environment.Modules.Avatar.Concierge
  45. {
  46. public class ConciergeModule : ChatModule, IRegionModule
  47. {
  48. private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. private const int DEBUG_CHANNEL = 2147483647;
  50. private List<IScene> _scenes = new List<IScene>();
  51. private List<IScene> _conciergedScenes = new List<IScene>();
  52. private Dictionary<IScene, List<UUID>> _sceneAttendees = new Dictionary<IScene, List<UUID>>();
  53. private bool _replacingChatModule = false;
  54. private IConfig _config;
  55. private string _whoami = "conferencier";
  56. private Regex _regions = null;
  57. private string _welcomes = null;
  58. private int _conciergeChannel = 42;
  59. private string _announceEntering = "{0} enters {1} (now {2} visitors in this region)";
  60. private string _announceLeaving = "{0} leaves {1} (back to {2} visitors in this region)";
  61. private string _xmlRpcPassword = String.Empty;
  62. internal object _syncy = new object();
  63. #region IRegionModule Members
  64. public override void Initialise(Scene scene, IConfigSource config)
  65. {
  66. try
  67. {
  68. if ((_config = config.Configs["Concierge"]) == null)
  69. {
  70. _log.InfoFormat("[Concierge]: no configuration section [Concierge] in OpenSim.ini: module not configured");
  71. return;
  72. }
  73. if (!_config.GetBoolean("enabled", false))
  74. {
  75. _log.InfoFormat("[Concierge]: module disabled by OpenSim.ini configuration");
  76. return;
  77. }
  78. }
  79. catch (Exception)
  80. {
  81. _log.Info("[Concierge]: module not configured");
  82. return;
  83. }
  84. // check whether ChatModule has been disabled: if yes,
  85. // then we'll "stand in"
  86. try
  87. {
  88. if (config.Configs["Chat"] == null)
  89. {
  90. _replacingChatModule = false;
  91. }
  92. else
  93. {
  94. _replacingChatModule = !config.Configs["Chat"].GetBoolean("enabled", true);
  95. }
  96. }
  97. catch (Exception)
  98. {
  99. _replacingChatModule = false;
  100. }
  101. _log.InfoFormat("[Concierge] {0} ChatModule", _replacingChatModule ? "replacing" : "not replacing");
  102. // take note of concierge channel and of identity
  103. _conciergeChannel = config.Configs["Concierge"].GetInt("concierge_channel", _conciergeChannel);
  104. _whoami = _config.GetString("whoami", "conferencier");
  105. _welcomes = _config.GetString("welcomes", _welcomes);
  106. _announceEntering = _config.GetString("announce_entering", _announceEntering);
  107. _announceLeaving = _config.GetString("announce_leaving", _announceLeaving);
  108. _xmlRpcPassword = _config.GetString("password", _xmlRpcPassword);
  109. _log.InfoFormat("[Concierge] reporting as \"{0}\" to our users", _whoami);
  110. // calculate regions Regex
  111. if (_regions == null)
  112. {
  113. string regions = _config.GetString("regions", String.Empty);
  114. if (!String.IsNullOrEmpty(regions))
  115. {
  116. _regions = new Regex(@regions, RegexOptions.Compiled | RegexOptions.IgnoreCase);
  117. }
  118. }
  119. scene.CommsManager.HttpServer.AddXmlRPCHandler("concierge_update_welcome", XmlRpcUpdateWelcomeMethod, false);
  120. lock (_syncy)
  121. {
  122. if (!_scenes.Contains(scene))
  123. {
  124. _scenes.Add(scene);
  125. if (_regions == null || _regions.IsMatch(scene.RegionInfo.RegionName))
  126. _conciergedScenes.Add(scene);
  127. // subscribe to NewClient events
  128. scene.EventManager.OnNewClient += OnNewClient;
  129. // subscribe to *Chat events
  130. scene.EventManager.OnChatFromWorld += OnChatFromWorld;
  131. if (!_replacingChatModule)
  132. scene.EventManager.OnChatFromClient += OnChatFromClient;
  133. scene.EventManager.OnChatBroadcast += OnChatBroadcast;
  134. // subscribe to agent change events
  135. scene.EventManager.OnMakeRootAgent += OnMakeRootAgent;
  136. scene.EventManager.OnMakeChildAgent += OnMakeChildAgent;
  137. }
  138. }
  139. _log.InfoFormat("[Concierge] initialized for {0}", scene.RegionInfo.RegionName);
  140. }
  141. public override void PostInitialise()
  142. {
  143. }
  144. public override void Close()
  145. {
  146. }
  147. public override string Name
  148. {
  149. get { return "ConciergeModule"; }
  150. }
  151. public override bool IsSharedModule
  152. {
  153. get { return true; }
  154. }
  155. #endregion
  156. #region ISimChat Members
  157. public override void OnChatBroadcast(Object sender, OSChatMessage c)
  158. {
  159. if (_replacingChatModule)
  160. {
  161. // distribute chat message to each and every avatar in
  162. // the region
  163. base.OnChatBroadcast(sender, c);
  164. }
  165. // TODO: capture logic
  166. return;
  167. }
  168. public override void OnChatFromClient(Object sender, OSChatMessage c)
  169. {
  170. if (_replacingChatModule)
  171. {
  172. // replacing ChatModule: need to redistribute
  173. // ChatFromClient to interested subscribers
  174. c = FixPositionOfChatMessage(c);
  175. Scene scene = (Scene)c.Scene;
  176. scene.EventManager.TriggerOnChatFromClient(sender, c);
  177. if (_conciergedScenes.Contains(c.Scene))
  178. {
  179. // when we are replacing ChatModule, we treat
  180. // OnChatFromClient like OnChatBroadcast for
  181. // concierged regions, effectively extending the
  182. // range of chat to cover the whole
  183. // region. however, we don't do this for whisper
  184. // (got to have some privacy)
  185. if (c.Type != ChatTypeEnum.Whisper)
  186. {
  187. base.OnChatBroadcast(sender, c);
  188. return;
  189. }
  190. }
  191. // redistribution will be done by base class
  192. base.OnChatFromClient(sender, c);
  193. }
  194. // TODO: capture chat
  195. return;
  196. }
  197. public override void OnChatFromWorld(Object sender, OSChatMessage c)
  198. {
  199. if (_replacingChatModule)
  200. {
  201. if (_conciergedScenes.Contains(c.Scene))
  202. {
  203. // when we are replacing ChatModule, we treat
  204. // OnChatFromClient like OnChatBroadcast for
  205. // concierged regions, effectively extending the
  206. // range of chat to cover the whole
  207. // region. however, we don't do this for whisper
  208. // (got to have some privacy)
  209. if (c.Type != ChatTypeEnum.Whisper)
  210. {
  211. base.OnChatBroadcast(sender, c);
  212. return;
  213. }
  214. }
  215. base.OnChatFromWorld(sender, c);
  216. }
  217. return;
  218. }
  219. #endregion
  220. public override void OnNewClient(IClientAPI client)
  221. {
  222. client.OnLogout += OnClientLoggedOut;
  223. client.OnConnectionClosed += OnClientLoggedOut;
  224. if (_replacingChatModule)
  225. client.OnChatFromClient += OnChatFromClient;
  226. }
  227. public void OnClientLoggedOut(IClientAPI client)
  228. {
  229. client.OnLogout -= OnClientLoggedOut;
  230. client.OnConnectionClosed -= OnClientLoggedOut;
  231. if (_conciergedScenes.Contains(client.Scene))
  232. {
  233. _log.DebugFormat("[Concierge]: {0} logs off from {1}", client.Name, client.Scene.RegionInfo.RegionName);
  234. RemoveFromAttendeeList(client.AgentId, client.Name, client.Scene);
  235. AnnounceToAgentsRegion(client.Scene, String.Format(_announceLeaving, client.Name, client.Scene.RegionInfo.RegionName,
  236. _sceneAttendees[client.Scene].Count));
  237. }
  238. }
  239. public void OnMakeRootAgent(ScenePresence agent)
  240. {
  241. if (_conciergedScenes.Contains(agent.Scene))
  242. {
  243. _log.DebugFormat("[Concierge]: {0} enters {1}", agent.Name, agent.Scene.RegionInfo.RegionName);
  244. AddToAttendeeList(agent.UUID, agent.Scene);
  245. WelcomeAvatar(agent, agent.Scene);
  246. AnnounceToAgentsRegion(agent.Scene, String.Format(_announceEntering, agent.Name, agent.Scene.RegionInfo.RegionName,
  247. _sceneAttendees[agent.Scene].Count));
  248. }
  249. }
  250. public void OnMakeChildAgent(ScenePresence agent)
  251. {
  252. if (_conciergedScenes.Contains(agent.Scene))
  253. {
  254. _log.DebugFormat("[Concierge]: {0} leaves {1}", agent.Name, agent.Scene.RegionInfo.RegionName);
  255. RemoveFromAttendeeList(agent.UUID, agent.Name, agent.Scene);
  256. AnnounceToAgentsRegion(agent.Scene, String.Format(_announceLeaving, agent.Name, agent.Scene.RegionInfo.RegionName,
  257. _sceneAttendees[agent.Scene].Count));
  258. }
  259. }
  260. protected void AddToAttendeeList(UUID agentID, Scene scene)
  261. {
  262. lock (_sceneAttendees)
  263. {
  264. if (!_sceneAttendees.ContainsKey(scene))
  265. _sceneAttendees[scene] = new List<UUID>();
  266. List<UUID> attendees = _sceneAttendees[scene];
  267. if (!attendees.Contains(agentID))
  268. attendees.Add(agentID);
  269. }
  270. }
  271. protected void RemoveFromAttendeeList(UUID agentID, String name, IScene scene)
  272. {
  273. lock (_sceneAttendees)
  274. {
  275. if (!_sceneAttendees.ContainsKey(scene))
  276. {
  277. _log.WarnFormat("[Concierge]: attendee list missing for region {0}", scene.RegionInfo.RegionName);
  278. return;
  279. }
  280. List<UUID> attendees = _sceneAttendees[scene];
  281. if (!attendees.Contains(agentID))
  282. {
  283. _log.WarnFormat("[Concierge]: avatar {0} sneaked in (not on attendee list of region {1})",
  284. name, scene.RegionInfo.RegionName);
  285. return;
  286. }
  287. attendees.Remove(agentID);
  288. }
  289. }
  290. protected void WelcomeAvatar(ScenePresence agent, Scene scene)
  291. {
  292. // welcome mechanics: check whether we have a welcomes
  293. // directory set and wether there is a region specific
  294. // welcome file there: if yes, send it to the agent
  295. if (!String.IsNullOrEmpty(_welcomes))
  296. {
  297. string[] welcomes = new string[] {
  298. Path.Combine(_welcomes, agent.Scene.RegionInfo.RegionName),
  299. Path.Combine(_welcomes, "DEFAULT")};
  300. foreach (string welcome in welcomes)
  301. {
  302. if (File.Exists(welcome))
  303. {
  304. try
  305. {
  306. string[] welcomeLines = File.ReadAllLines(welcome);
  307. foreach (string l in welcomeLines)
  308. {
  309. AnnounceToAgent(agent, String.Format(l, agent.Name, scene.RegionInfo.RegionName, _whoami));
  310. }
  311. }
  312. catch (IOException ioe)
  313. {
  314. _log.ErrorFormat("[Concierge]: run into trouble reading welcome file {0} for region {1} for avatar {2}: {3}",
  315. welcome, scene.RegionInfo.RegionName, agent.Name, ioe);
  316. }
  317. catch (FormatException fe)
  318. {
  319. _log.ErrorFormat("[Concierge]: welcome file {0} is malformed: {1}", welcome, fe);
  320. }
  321. }
  322. return;
  323. }
  324. _log.DebugFormat("[Concierge]: no welcome message for region {0}", scene.RegionInfo.RegionName);
  325. }
  326. }
  327. static private Vector3 PosOfGod = new Vector3(128, 128, 9999);
  328. // protected void AnnounceToAgentsRegion(Scene scene, string msg)
  329. // {
  330. // ScenePresence agent = null;
  331. // if ((client.Scene is Scene) && (client.Scene as Scene).TryGetAvatar(client.AgentId, out agent))
  332. // AnnounceToAgentsRegion(agent, msg);
  333. // else
  334. // _log.DebugFormat("[Concierge]: could not find an agent for client {0}", client.Name);
  335. // }
  336. protected void AnnounceToAgentsRegion(IScene scene, string msg)
  337. {
  338. OSChatMessage c = new OSChatMessage();
  339. c.Message = msg;
  340. c.Type = ChatTypeEnum.Say;
  341. c.Channel = 0;
  342. c.Position = PosOfGod;
  343. c.From = _whoami;
  344. c.Sender = null;
  345. c.SenderUUID = UUID.Zero;
  346. c.Scene = scene;
  347. if (scene is Scene)
  348. (scene as Scene).EventManager.TriggerOnChatBroadcast(this, c);
  349. }
  350. protected void AnnounceToAgent(ScenePresence agent, string msg)
  351. {
  352. OSChatMessage c = new OSChatMessage();
  353. c.Message = msg;
  354. c.Type = ChatTypeEnum.Say;
  355. c.Channel = 0;
  356. c.Position = PosOfGod;
  357. c.From = _whoami;
  358. c.Sender = null;
  359. c.SenderUUID = UUID.Zero;
  360. c.Scene = agent.Scene;
  361. agent.ControllingClient.SendChatMessage(msg, (byte) ChatTypeEnum.Say, PosOfGod, _whoami, UUID.Zero,
  362. (byte)ChatSourceType.Object, (byte)ChatAudibleLevel.Fully);
  363. }
  364. private static void checkStringParameters(XmlRpcRequest request, string[] param)
  365. {
  366. Hashtable requestData = (Hashtable) request.Params[0];
  367. foreach (string p in param)
  368. {
  369. if (!requestData.Contains(p))
  370. throw new Exception(String.Format("missing string parameter {0}", p));
  371. if (String.IsNullOrEmpty((string)requestData[p]))
  372. throw new Exception(String.Format("parameter {0} is empty", p));
  373. }
  374. }
  375. public XmlRpcResponse XmlRpcUpdateWelcomeMethod(XmlRpcRequest request)
  376. {
  377. _log.Info("[Concierge]: processing UpdateWelcome request");
  378. XmlRpcResponse response = new XmlRpcResponse();
  379. Hashtable responseData = new Hashtable();
  380. try
  381. {
  382. Hashtable requestData = (Hashtable)request.Params[0];
  383. checkStringParameters(request, new string[] { "password", "region", "welcome" });
  384. // check password
  385. if (!String.IsNullOrEmpty(_xmlRpcPassword) &&
  386. (string)requestData["password"] != _xmlRpcPassword) throw new Exception("wrong password");
  387. if (String.IsNullOrEmpty(_welcomes))
  388. throw new Exception("welcome templates are not enabled, ask your OpenSim operator to set the \"welcomes\" option in the [Concierge] section of OpenSim.ini");
  389. string msg = (string)requestData["welcome"];
  390. if (String.IsNullOrEmpty(msg))
  391. throw new Exception("empty parameter \"welcome\"");
  392. string regionName = (string)requestData["region"];
  393. IScene scene = _scenes.Find(delegate(IScene s) { return s.RegionInfo.RegionName == regionName; });
  394. if (scene == null)
  395. throw new Exception(String.Format("unknown region \"{0}\"", regionName));
  396. if (!_conciergedScenes.Contains(scene))
  397. throw new Exception(String.Format("region \"{0}\" is not a concierged region.", regionName));
  398. string welcome = Path.Combine(_welcomes, regionName);
  399. if (File.Exists(welcome))
  400. {
  401. _log.InfoFormat("[Concierge]: UpdateWelcome: updating existing template \"{0}\"", welcome);
  402. string welcomeBackup = String.Format("{0}~", welcome);
  403. if (File.Exists(welcomeBackup))
  404. File.Delete(welcomeBackup);
  405. File.Move(welcome, welcomeBackup);
  406. }
  407. File.WriteAllText(welcome, msg);
  408. responseData["success"] = "true";
  409. response.Value = responseData;
  410. }
  411. catch (Exception e)
  412. {
  413. _log.InfoFormat("[Concierge]: UpdateWelcome failed: {0}", e.Message);
  414. responseData["success"] = "false";
  415. responseData["error"] = e.Message;
  416. response.Value = responseData;
  417. }
  418. _log.Debug("[Concierge]: done processing UpdateWelcome request");
  419. return response;
  420. }
  421. }
  422. }