BasicSearchModule.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. private ExpiringCache<string, List<UserAccount>> queryPeopleCache = new ExpiringCache<string, List<UserAccount>>();
  57. private ExpiringCache<string, List<DirGroupsReplyData>> queryGroupCache = new ExpiringCache<string, List<DirGroupsReplyData>>();
  58. #region ISharedRegionModule
  59. public void Initialise(IConfigSource config)
  60. {
  61. string umanmod = config.Configs["Modules"].GetString("SearchModule", Name);
  62. if (umanmod == Name)
  63. {
  64. m_Enabled = true;
  65. m_log.DebugFormat("[BASIC SEARCH MODULE]: {0} is enabled", Name);
  66. }
  67. }
  68. public bool IsSharedModule
  69. {
  70. get { return true; }
  71. }
  72. public virtual string Name
  73. {
  74. get { return "BasicSearchModule"; }
  75. }
  76. public Type ReplaceableInterface
  77. {
  78. get { return null; }
  79. }
  80. public void AddRegion(Scene scene)
  81. {
  82. if (m_Enabled)
  83. {
  84. m_Scenes.Add(scene);
  85. scene.EventManager.OnMakeRootAgent += new Action<ScenePresence>(EventManager_OnMakeRootAgent);
  86. scene.EventManager.OnMakeChildAgent += new EventManager.OnMakeChildAgentDelegate(EventManager_OnMakeChildAgent);
  87. }
  88. }
  89. public void RemoveRegion(Scene scene)
  90. {
  91. if (m_Enabled)
  92. {
  93. m_Scenes.Remove(scene);
  94. scene.EventManager.OnMakeRootAgent -= new Action<ScenePresence>(EventManager_OnMakeRootAgent);
  95. scene.EventManager.OnMakeChildAgent -= new EventManager.OnMakeChildAgentDelegate(EventManager_OnMakeChildAgent);
  96. }
  97. }
  98. public void RegionLoaded(Scene s)
  99. {
  100. if (!m_Enabled)
  101. return;
  102. if (m_GroupsService == null)
  103. {
  104. m_GroupsService = s.RequestModuleInterface<IGroupsModule>();
  105. // No Groups Service Connector, then group search won't work...
  106. if (m_GroupsService == null)
  107. m_log.Warn("[BASIC SEARCH MODULE]: Could not get IGroupsModule");
  108. }
  109. }
  110. public void PostInitialise()
  111. {
  112. }
  113. public void Close()
  114. {
  115. m_Scenes.Clear();
  116. }
  117. #endregion ISharedRegionModule
  118. #region Event Handlers
  119. void EventManager_OnMakeRootAgent(ScenePresence sp)
  120. {
  121. sp.ControllingClient.OnDirFindQuery += OnDirFindQuery;
  122. }
  123. void EventManager_OnMakeChildAgent(ScenePresence sp)
  124. {
  125. sp.ControllingClient.OnDirFindQuery -= OnDirFindQuery;
  126. }
  127. void OnDirFindQuery(IClientAPI remoteClient, UUID queryID, string queryText, uint queryFlags, int queryStart)
  128. {
  129. if (!string.IsNullOrEmpty(queryText))
  130. {
  131. queryText = queryText.Trim();
  132. queryText = queryText.ToLowerInvariant();
  133. }
  134. if (((DirFindFlags)queryFlags & DirFindFlags.People) == DirFindFlags.People)
  135. {
  136. if (string.IsNullOrEmpty(queryText))
  137. remoteClient.SendDirPeopleReply(queryID, new DirPeopleReplyData[0]);
  138. List<UserAccount> accounts;
  139. if (!queryPeopleCache.TryGetValue(queryText, out accounts))
  140. accounts = m_Scenes[0].UserAccountService.GetUserAccounts(m_Scenes[0].RegionInfo.ScopeID, queryText);
  141. queryPeopleCache.AddOrUpdate(queryText, accounts, 30.0);
  142. if (accounts.Count == 0)
  143. {
  144. remoteClient.SendDirPeopleReply(queryID, new DirPeopleReplyData[0]);
  145. return;
  146. }
  147. DirPeopleReplyData[] hits = new DirPeopleReplyData[accounts.Count];
  148. int count = 0;
  149. foreach (UserAccount acc in accounts)
  150. {
  151. DirPeopleReplyData d = new DirPeopleReplyData();
  152. d.agentID = acc.PrincipalID;
  153. d.firstName = acc.FirstName;
  154. d.lastName = acc.LastName;
  155. d.online = false;
  156. hits[count++] = d;
  157. }
  158. // viewers don't sent sorting, so results they show are a nice mess
  159. if ((queryStart > 0) && (queryStart < count))
  160. {
  161. int len = count - queryStart;
  162. if (len > 101) // a viewer page is 100
  163. len = 101;
  164. DirPeopleReplyData[] tmp = new DirPeopleReplyData[len];
  165. Array.Copy(hits, queryStart, tmp, 0, len);
  166. hits = tmp;
  167. }
  168. else if (count > 101)
  169. {
  170. DirPeopleReplyData[] tmp = new DirPeopleReplyData[101];
  171. Array.Copy(hits, 0, tmp, 0, 101);
  172. hits = tmp;
  173. }
  174. // TODO: This currently ignores pretty much all the query flags including Mature and sort order
  175. remoteClient.SendDirPeopleReply(queryID, hits);
  176. }
  177. else if (((DirFindFlags)queryFlags & DirFindFlags.Groups) == DirFindFlags.Groups)
  178. {
  179. if (m_GroupsService == null)
  180. {
  181. m_log.Warn("[BASIC SEARCH MODULE]: Groups service is not available. Unable to search groups.");
  182. remoteClient.SendAlertMessage("Groups search is not enabled");
  183. return;
  184. }
  185. if (string.IsNullOrEmpty(queryText))
  186. remoteClient.SendDirGroupsReply(queryID, new DirGroupsReplyData[0]);
  187. List<DirGroupsReplyData> answer;
  188. if (!queryGroupCache.TryGetValue(queryText, out answer))
  189. answer = m_GroupsService.FindGroups(remoteClient, queryText);
  190. queryGroupCache.AddOrUpdate(queryText, answer, 30.0);
  191. if(answer.Count == 0)
  192. {
  193. remoteClient.SendDirGroupsReply(queryID, new DirGroupsReplyData[0]);
  194. return;
  195. }
  196. // filter out groups
  197. DirGroupsReplyData[] result = new DirGroupsReplyData[answer.Count];
  198. int count = 0;
  199. foreach(DirGroupsReplyData dgrd in answer)
  200. {
  201. if(dgrd.members > 0)
  202. result[count++] = dgrd;
  203. }
  204. answer = null;
  205. // viewers don't sent sorting, so results they show are a nice mess
  206. if ((queryStart > 0) && (queryStart < count))
  207. {
  208. int len = count - queryStart;
  209. if (len > 101) // a viewer page is 100
  210. len = 101;
  211. DirGroupsReplyData[] tmp = new DirGroupsReplyData[len];
  212. Array.Copy(result, queryStart, tmp, 0, len);
  213. result = tmp;
  214. }
  215. else if (count > 101)
  216. {
  217. DirGroupsReplyData[] tmp = new DirGroupsReplyData[101];
  218. Array.Copy(result, 0, tmp, 0, 101);
  219. result = tmp;
  220. }
  221. // TODO: This currently ignores pretty much all the query flags including Mature and sort order
  222. remoteClient.SendDirGroupsReply(queryID, result);
  223. }
  224. }
  225. #endregion Event Handlers
  226. }
  227. }