AvatarPickerSearchModule.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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.Collections.Specialized;
  30. using System.Net;
  31. using log4net;
  32. using Nini.Config;
  33. using Mono.Addins;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Servers.HttpServer;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Region.Framework.Scenes;
  39. using OpenSim.Framework.Capabilities;
  40. using Caps = OpenSim.Framework.Capabilities.Caps;
  41. namespace OpenSim.Region.ClientStack.Linden
  42. {
  43. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "AvatarPickerSearchModule")]
  44. public class AvatarPickerSearchModule : ISharedRegionModule
  45. {
  46. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. private int m_nscenes;
  48. private IPeople m_People = null;
  49. private bool m_Enabled = false;
  50. private string m_URL;
  51. #region ISharedRegionModule Members
  52. public void Initialise(IConfigSource source)
  53. {
  54. IConfig config = source.Configs["ClientStack.LindenCaps"];
  55. if (config == null)
  56. return;
  57. m_URL = config.GetString("Cap_AvatarPickerSearch", string.Empty);
  58. // Cap doesn't exist
  59. if (m_URL != string.Empty)
  60. m_Enabled = true;
  61. }
  62. public void AddRegion(Scene s)
  63. {
  64. if (!m_Enabled)
  65. return;
  66. }
  67. public void RemoveRegion(Scene s)
  68. {
  69. if (!m_Enabled)
  70. return;
  71. s.EventManager.OnRegisterCaps -= RegisterCaps;
  72. --m_nscenes;
  73. if(m_nscenes >= 0)
  74. m_People = null;
  75. }
  76. public void RegionLoaded(Scene s)
  77. {
  78. if (!m_Enabled)
  79. return;
  80. if(m_People == null)
  81. m_People = s.RequestModuleInterface<IPeople>();
  82. s.EventManager.OnRegisterCaps += RegisterCaps;
  83. ++m_nscenes;
  84. }
  85. public void PostInitialise()
  86. {
  87. }
  88. public void Close() { }
  89. public string Name { get { return "AvatarPickerSearchModule"; } }
  90. public Type ReplaceableInterface
  91. {
  92. get { return null; }
  93. }
  94. #endregion
  95. public void RegisterCaps(UUID agentID, Caps caps)
  96. {
  97. UUID capID = UUID.Random();
  98. if (m_URL == "localhost")
  99. {
  100. // m_log.DebugFormat("[AVATAR PICKER SEARCH]: /CAPS/{0} in region {1}", capID, m_scene.RegionInfo.RegionName);
  101. if(m_People != null)
  102. caps.RegisterSimpleHandler("AvatarPickerSearch",
  103. new SimpleStreamHandler("/" + UUID.Random(), ProcessRequest));
  104. }
  105. else
  106. {
  107. // m_log.DebugFormat("[AVATAR PICKER SEARCH]: {0} in region {1}", m_URL, m_scene.RegionInfo.RegionName);
  108. caps.RegisterHandler("AvatarPickerSearch", m_URL);
  109. }
  110. }
  111. protected void ProcessRequest(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  112. {
  113. if(httpRequest.HttpMethod != "GET")
  114. {
  115. httpResponse.StatusCode = (int)HttpStatusCode.NotFound;
  116. return;
  117. }
  118. NameValueCollection query = httpRequest.QueryString;
  119. string names = query.GetOne("names");
  120. string psize = query.GetOne("page_size");
  121. string pnumber = query.GetOne("page");
  122. if (string.IsNullOrEmpty(names) || names.Length < 3)
  123. {
  124. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  125. return;
  126. }
  127. int page_size;
  128. int page_number;
  129. try
  130. {
  131. page_size = (string.IsNullOrEmpty(psize) ? 500 : Int32.Parse(psize));
  132. page_number = (string.IsNullOrEmpty(pnumber) ? 1 : Int32.Parse(pnumber));
  133. }
  134. catch
  135. {
  136. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  137. return;
  138. }
  139. // Full content request
  140. List<UserData> users = m_People.GetUserData(names, page_size, page_number);
  141. LLSDAvatarPicker osdReply = new LLSDAvatarPicker();
  142. osdReply.next_page_url = httpRequest.RawUrl;
  143. foreach (UserData u in users)
  144. osdReply.agents.Array.Add(ConvertUserData(u));
  145. string reply = LLSDHelpers.SerialiseLLSDReply(osdReply);
  146. httpResponse.RawBuffer = Util.UTF8.GetBytes(reply);
  147. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  148. httpResponse.ContentType = "application/llsd+xml";
  149. }
  150. private LLSDPerson ConvertUserData(UserData user)
  151. {
  152. LLSDPerson p = new LLSDPerson();
  153. p.legacy_first_name = user.FirstName;
  154. p.legacy_last_name = user.LastName;
  155. p.display_name = user.FirstName + " " + user.LastName;
  156. if (user.LastName.StartsWith("@"))
  157. p.username = user.FirstName.ToLower() + user.LastName.ToLower();
  158. else
  159. p.username = user.FirstName.ToLower() + "." + user.LastName.ToLower();
  160. p.id = user.Id;
  161. p.is_display_name_default = false;
  162. return p;
  163. }
  164. }
  165. }