FriendsCommandsModule.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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.Linq;
  30. using System.Reflection;
  31. using System.Text;
  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.Statistics;
  40. using OpenSim.Region.ClientStack.LindenUDP;
  41. using OpenSim.Region.CoreModules.Avatar.Friends;
  42. using OpenSim.Region.Framework.Interfaces;
  43. using OpenSim.Region.Framework.Scenes;
  44. using OpenSim.Services.Interfaces;
  45. using FriendInfo = OpenSim.Services.Interfaces.FriendInfo;
  46. namespace OpenSim.Region.OptionalModules.Avatar.Friends
  47. {
  48. /// <summary>
  49. /// A module that just holds commands for inspecting avatar appearance.
  50. /// </summary>
  51. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "FriendsCommandModule")]
  52. public class FriendsCommandsModule : ISharedRegionModule
  53. {
  54. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  55. private Scene m_scene;
  56. private IFriendsModule m_friendsModule;
  57. private IUserManagement m_userManagementModule;
  58. private IPresenceService m_presenceService;
  59. // private IAvatarFactoryModule m_avatarFactory;
  60. public string Name { get { return "Appearance Information Module"; } }
  61. public Type ReplaceableInterface { get { return null; } }
  62. public void Initialise(IConfigSource source)
  63. {
  64. // m_log.DebugFormat("[FRIENDS COMMAND MODULE]: INITIALIZED MODULE");
  65. }
  66. public void PostInitialise()
  67. {
  68. // m_log.DebugFormat("[FRIENDS COMMAND MODULE]: POST INITIALIZED MODULE");
  69. }
  70. public void Close()
  71. {
  72. // m_log.DebugFormat("[FRIENDS COMMAND MODULE]: CLOSED MODULE");
  73. }
  74. public void AddRegion(Scene scene)
  75. {
  76. // m_log.DebugFormat("[FRIENDS COMMANDO MODULE]: REGION {0} ADDED", scene.RegionInfo.RegionName);
  77. }
  78. public void RemoveRegion(Scene scene)
  79. {
  80. // m_log.DebugFormat("[FRIENDS COMMAND MODULE]: REGION {0} REMOVED", scene.RegionInfo.RegionName);
  81. }
  82. public void RegionLoaded(Scene scene)
  83. {
  84. // m_log.DebugFormat("[APPEARANCE INFO MODULE]: REGION {0} LOADED", scene.RegionInfo.RegionName);
  85. if (m_scene == null)
  86. m_scene = scene;
  87. m_friendsModule = m_scene.RequestModuleInterface<IFriendsModule>();
  88. m_userManagementModule = m_scene.RequestModuleInterface<IUserManagement>();
  89. m_presenceService = m_scene.RequestModuleInterface<IPresenceService>();
  90. if (m_friendsModule != null && m_userManagementModule != null && m_presenceService != null)
  91. {
  92. m_scene.AddCommand(
  93. "Friends", this, "friends show",
  94. "friends show [--cache] <first-name> <last-name>",
  95. "Show the friends for the given user if they exist.\n",
  96. "The --cache option will show locally cached information for that user.",
  97. HandleFriendsShowCommand);
  98. }
  99. }
  100. protected void HandleFriendsShowCommand(string module, string[] cmd)
  101. {
  102. Dictionary<string, object> options = new Dictionary<string, object>();
  103. OptionSet optionSet = new OptionSet().Add("c|cache", delegate (string v) { options["cache"] = v != null; });
  104. List<string> mainParams = optionSet.Parse(cmd);
  105. if (mainParams.Count != 4)
  106. {
  107. MainConsole.Instance.OutputFormat("Usage: friends show [--cache] <first-name> <last-name>");
  108. return;
  109. }
  110. string firstName = mainParams[2];
  111. string lastName = mainParams[3];
  112. UUID userId = m_userManagementModule.GetUserIdByName(firstName, lastName);
  113. // UserAccount ua
  114. // = m_Scenes[0].UserAccountService.GetUserAccount(m_Scenes[0].RegionInfo.ScopeID, firstName, lastName);
  115. if (userId == UUID.Zero)
  116. {
  117. MainConsole.Instance.OutputFormat("No such user as {0} {1}", firstName, lastName);
  118. return;
  119. }
  120. FriendInfo[] friends;
  121. if (options.ContainsKey("cache"))
  122. {
  123. if (!m_friendsModule.AreFriendsCached(userId))
  124. {
  125. MainConsole.Instance.OutputFormat("No friends cached on this simulator for {0} {1}", firstName, lastName);
  126. return;
  127. }
  128. else
  129. {
  130. friends = m_friendsModule.GetFriendsFromCache(userId);
  131. }
  132. }
  133. else
  134. {
  135. // FIXME: We're forced to do this right now because IFriendsService has no region connectors. We can't
  136. // just expose FriendsModule.GetFriendsFromService() because it forces an IClientAPI requirement that
  137. // can't currently be changed because of HGFriendsModule code that takes the scene from the client.
  138. friends = ((FriendsModule)m_friendsModule).FriendsService.GetFriends(userId);
  139. }
  140. MainConsole.Instance.OutputFormat("Friends for {0} {1} {2}:", firstName, lastName, userId);
  141. MainConsole.Instance.OutputFormat(
  142. "{0,-36} {1,-36} {2,-7} {3,7} {4,10}", "UUID", "Name", "Status", "MyFlags", "TheirFlags");
  143. foreach (FriendInfo friend in friends)
  144. {
  145. // MainConsole.Instance.OutputFormat(friend.PrincipalID.ToString());
  146. // string friendFirstName, friendLastName;
  147. //
  148. // UserAccount friendUa
  149. // = m_Scenes[0].UserAccountService.GetUserAccount(m_Scenes[0].RegionInfo.ScopeID, friend.PrincipalID);
  150. UUID friendId;
  151. string friendName;
  152. string onlineText;
  153. if (UUID.TryParse(friend.Friend, out friendId))
  154. friendName = m_userManagementModule.GetUserName(friendId);
  155. else
  156. friendName = friend.Friend;
  157. OpenSim.Services.Interfaces.PresenceInfo[] pi = m_presenceService.GetAgents(new string[] { friend.Friend });
  158. if (pi.Length > 0)
  159. onlineText = "online";
  160. else
  161. onlineText = "offline";
  162. MainConsole.Instance.OutputFormat(
  163. "{0,-36} {1,-36} {2,-7} {3,-7} {4,-10}",
  164. friend.Friend, friendName, onlineText, friend.MyFlags, friend.TheirFlags);
  165. }
  166. }
  167. }
  168. }