BasicSearchModule.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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.IO;
  30. using System.Reflection;
  31. using System.Threading;
  32. using OpenSim.Framework;
  33. using OpenSim.Framework.Console;
  34. using OpenSim.Framework.Monitoring;
  35. using OpenSim.Region.ClientStack.LindenUDP;
  36. using OpenSim.Region.Framework;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Region.Framework.Scenes;
  39. using OpenSim.Services.Interfaces;
  40. using OpenSim.Services.Connectors.Hypergrid;
  41. using OpenMetaverse;
  42. using OpenMetaverse.Packets;
  43. using log4net;
  44. using Nini.Config;
  45. using Mono.Addins;
  46. using DirFindFlags = OpenMetaverse.DirectoryManager.DirFindFlags;
  47. namespace OpenSim.Region.CoreModules.Framework.Search
  48. {
  49. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "BasicSearchModule")]
  50. public class BasicSearchModule : ISharedRegionModule
  51. {
  52. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  53. protected bool m_Enabled;
  54. protected List<Scene> m_Scenes = new List<Scene>();
  55. private IGroupsModule m_GroupsService = null;
  56. #region ISharedRegionModule
  57. public void Initialise(IConfigSource config)
  58. {
  59. string umanmod = config.Configs["Modules"].GetString("SearchModule", Name);
  60. if (umanmod == Name)
  61. {
  62. m_Enabled = true;
  63. m_log.DebugFormat("[BASIC SEARCH MODULE]: {0} is enabled", Name);
  64. }
  65. }
  66. public bool IsSharedModule
  67. {
  68. get { return true; }
  69. }
  70. public virtual string Name
  71. {
  72. get { return "BasicSearchModule"; }
  73. }
  74. public Type ReplaceableInterface
  75. {
  76. get { return null; }
  77. }
  78. public void AddRegion(Scene scene)
  79. {
  80. if (m_Enabled)
  81. {
  82. m_Scenes.Add(scene);
  83. scene.EventManager.OnMakeRootAgent += new Action<ScenePresence>(EventManager_OnMakeRootAgent);
  84. scene.EventManager.OnMakeChildAgent += new EventManager.OnMakeChildAgentDelegate(EventManager_OnMakeChildAgent);
  85. }
  86. }
  87. public void RemoveRegion(Scene scene)
  88. {
  89. if (m_Enabled)
  90. {
  91. m_Scenes.Remove(scene);
  92. scene.EventManager.OnMakeRootAgent -= new Action<ScenePresence>(EventManager_OnMakeRootAgent);
  93. scene.EventManager.OnMakeChildAgent -= new EventManager.OnMakeChildAgentDelegate(EventManager_OnMakeChildAgent);
  94. }
  95. }
  96. public void RegionLoaded(Scene s)
  97. {
  98. if (!m_Enabled)
  99. return;
  100. if (m_GroupsService == null)
  101. {
  102. m_GroupsService = s.RequestModuleInterface<IGroupsModule>();
  103. // No Groups Service Connector, then group search won't work...
  104. if (m_GroupsService == null)
  105. m_log.Warn("[BASIC SEARCH MODULE]: Could not get IGroupsModule");
  106. }
  107. }
  108. public void PostInitialise()
  109. {
  110. }
  111. public void Close()
  112. {
  113. m_Scenes.Clear();
  114. }
  115. #endregion ISharedRegionModule
  116. #region Event Handlers
  117. void EventManager_OnMakeRootAgent(ScenePresence sp)
  118. {
  119. sp.ControllingClient.OnDirFindQuery += OnDirFindQuery;
  120. }
  121. void EventManager_OnMakeChildAgent(ScenePresence sp)
  122. {
  123. sp.ControllingClient.OnDirFindQuery -= OnDirFindQuery;
  124. }
  125. void OnDirFindQuery(IClientAPI remoteClient, UUID queryID, string queryText, uint queryFlags, int queryStart)
  126. {
  127. if (((DirFindFlags)queryFlags & DirFindFlags.People) == DirFindFlags.People)
  128. {
  129. if (string.IsNullOrEmpty(queryText))
  130. remoteClient.SendDirPeopleReply(queryID, new DirPeopleReplyData[0]);
  131. List<UserAccount> accounts = m_Scenes[0].UserAccountService.GetUserAccounts(m_Scenes[0].RegionInfo.ScopeID, queryText);
  132. DirPeopleReplyData[] hits = new DirPeopleReplyData[accounts.Count];
  133. int i = 0;
  134. foreach (UserAccount acc in accounts)
  135. {
  136. DirPeopleReplyData d = new DirPeopleReplyData();
  137. d.agentID = acc.PrincipalID;
  138. d.firstName = acc.FirstName;
  139. d.lastName = acc.LastName;
  140. d.online = false;
  141. hits[i++] = d;
  142. }
  143. // TODO: This currently ignores pretty much all the query flags including Mature and sort order
  144. remoteClient.SendDirPeopleReply(queryID, hits);
  145. }
  146. else if (((DirFindFlags)queryFlags & DirFindFlags.Groups) == DirFindFlags.Groups)
  147. {
  148. if (m_GroupsService == null)
  149. {
  150. m_log.Warn("[BASIC SEARCH MODULE]: Groups service is not available. Unable to search groups.");
  151. remoteClient.SendAlertMessage("Groups search is not enabled");
  152. return;
  153. }
  154. if (string.IsNullOrEmpty(queryText))
  155. remoteClient.SendDirGroupsReply(queryID, new DirGroupsReplyData[0]);
  156. // TODO: This currently ignores pretty much all the query flags including Mature and sort order
  157. remoteClient.SendDirGroupsReply(queryID, m_GroupsService.FindGroups(remoteClient, queryText).ToArray());
  158. }
  159. }
  160. #endregion Event Handlers
  161. }
  162. }