UserCommandsModule.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 System.Text;
  31. using System.Text.RegularExpressions;
  32. using log4net;
  33. using Mono.Addins;
  34. using NDesk.Options;
  35. using Nini.Config;
  36. using OpenMetaverse;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.Console;
  39. using OpenSim.Framework.Monitoring;
  40. using OpenSim.Region.Framework.Interfaces;
  41. using OpenSim.Region.Framework.Scenes;
  42. namespace OpenSim.Region.CoreModules.Avatars.Commands
  43. {
  44. /// <summary>
  45. /// A module that holds commands for manipulating objects in the scene.
  46. /// </summary>
  47. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "UserCommandsModule")]
  48. public class UserCommandsModule : ISharedRegionModule
  49. {
  50. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  51. public const string TeleportUserCommandSyntax = "teleport user <first-name> <last-name> <destination>";
  52. public static Regex InterRegionDestinationRegex
  53. = new Regex(@"^(?<regionName>.+)/(?<x>\d+)/(?<y>\d+)/(?<z>\d+)$", RegexOptions.Compiled);
  54. public static Regex WithinRegionDestinationRegex
  55. = new Regex(@"^(?<x>\d+)/(?<y>\d+)/(?<z>\d+)$", RegexOptions.Compiled);
  56. private Dictionary<UUID, Scene> m_scenes = new Dictionary<UUID, Scene>();
  57. public string Name { get { return "User Commands Module"; } }
  58. public Type ReplaceableInterface { get { return null; } }
  59. public void Initialise(IConfigSource source)
  60. {
  61. // m_log.DebugFormat("[USER COMMANDS MODULE]: INITIALIZED MODULE");
  62. }
  63. public void PostInitialise()
  64. {
  65. // m_log.DebugFormat("[USER COMMANDS MODULE]: POST INITIALIZED MODULE");
  66. }
  67. public void Close()
  68. {
  69. // m_log.DebugFormat("[USER COMMANDS MODULE]: CLOSED MODULE");
  70. }
  71. public void AddRegion(Scene scene)
  72. {
  73. // m_log.DebugFormat("[USER COMMANDS MODULE]: REGION {0} ADDED", scene.RegionInfo.RegionName);
  74. lock (m_scenes)
  75. m_scenes[scene.RegionInfo.RegionID] = scene;
  76. scene.AddCommand(
  77. "Users",
  78. this,
  79. "teleport user",
  80. TeleportUserCommandSyntax,
  81. "Teleport a user in this simulator to the given destination",
  82. "<destination> is in format [<region-name>]/<x>/<y>/<z>, e.g. regionone/20/30/40 or just 20/30/40 to teleport within same region."
  83. + "\nIf the region contains a space then the whole destination must be in quotes, e.g. \"region one/20/30/40\"",
  84. HandleTeleportUser);
  85. }
  86. public void RemoveRegion(Scene scene)
  87. {
  88. // m_log.DebugFormat("[USER COMMANDS MODULE]: REGION {0} REMOVED", scene.RegionInfo.RegionName);
  89. lock (m_scenes)
  90. m_scenes.Remove(scene.RegionInfo.RegionID);
  91. }
  92. public void RegionLoaded(Scene scene)
  93. {
  94. // m_log.DebugFormat("[USER COMMANDS MODULE]: REGION {0} LOADED", scene.RegionInfo.RegionName);
  95. }
  96. private ScenePresence GetUser(string firstName, string lastName)
  97. {
  98. ScenePresence userFound = null;
  99. lock (m_scenes)
  100. {
  101. foreach (Scene scene in m_scenes.Values)
  102. {
  103. ScenePresence user = scene.GetScenePresence(firstName, lastName);
  104. if (user != null && !user.IsChildAgent)
  105. {
  106. userFound = user;
  107. break;
  108. }
  109. }
  110. }
  111. return userFound;
  112. }
  113. private void HandleTeleportUser(string module, string[] cmd)
  114. {
  115. if (cmd.Length < 5)
  116. {
  117. MainConsole.Instance.Output("Usage: " + TeleportUserCommandSyntax);
  118. return;
  119. }
  120. string firstName = cmd[2];
  121. string lastName = cmd[3];
  122. string rawDestination = cmd[4];
  123. ScenePresence user = GetUser(firstName, lastName);
  124. if (user == null)
  125. {
  126. MainConsole.Instance.Output("No user found with name {0} {1}", firstName, lastName);
  127. return;
  128. }
  129. // MainConsole.Instance.OutputFormat("rawDestination [{0}]", rawDestination);
  130. Match m = WithinRegionDestinationRegex.Match(rawDestination);
  131. if (!m.Success)
  132. {
  133. m = InterRegionDestinationRegex.Match(rawDestination);
  134. if (!m.Success)
  135. {
  136. MainConsole.Instance.Output("Invalid destination {0}", rawDestination);
  137. return;
  138. }
  139. }
  140. string regionName
  141. = m.Groups["regionName"].Success ? m.Groups["regionName"].Value : user.Scene.RegionInfo.RegionName;
  142. MainConsole.Instance.Output(
  143. "Teleporting {0} to {1},{2},{3} in {4}",
  144. user.Name,
  145. m.Groups["x"], m.Groups["y"], m.Groups["z"],
  146. regionName);
  147. user.Scene.RequestTeleportLocation(
  148. user.ControllingClient,
  149. regionName,
  150. new Vector3(
  151. float.Parse(m.Groups["x"].Value),
  152. float.Parse(m.Groups["y"].Value),
  153. float.Parse(m.Groups["z"].Value)),
  154. user.Lookat,
  155. (uint)TeleportFlags.ViaLocation);
  156. }
  157. }
  158. }