OpenProfileClient.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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;
  29. using System.Collections.Generic;
  30. using System.Linq;
  31. using System.Net;
  32. using System.Net.Sockets;
  33. using System.Reflection;
  34. using System.Text;
  35. using System.Xml;
  36. using log4net;
  37. using Nwc.XmlRpc;
  38. using OpenMetaverse;
  39. using OpenSim.Framework;
  40. namespace OpenSim.Region.CoreModules.Avatar.UserProfiles
  41. {
  42. /// <summary>
  43. /// A client for accessing a profile server using the OpenProfile protocol.
  44. /// </summary>
  45. /// <remarks>
  46. /// This class was adapted from the full OpenProfile class. Since it's only a client, and not a server,
  47. /// it's much simpler.
  48. /// </remarks>
  49. public class OpenProfileClient
  50. {
  51. static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  52. private string m_serverURI;
  53. /// <summary>
  54. /// Creates a client for accessing a foreign grid's profile server using the OpenProfile protocol.
  55. /// </summary>
  56. /// <param name="serverURI">The grid's profile server URL</param>
  57. public OpenProfileClient(string serverURI)
  58. {
  59. m_serverURI = serverURI;
  60. }
  61. /// <summary>
  62. /// Gets an avatar's profile using the OpenProfile protocol.
  63. /// </summary>
  64. /// <param name="props">On success, this will contain the avatar's profile</param>
  65. /// <returns>Success/failure</returns>
  66. /// <remarks>
  67. /// There are two profile modules currently in use in OpenSim: the older one is OpenProfile, and the newer
  68. /// one is UserProfileModule (this file). This method attempts to read an avatar's profile from a foreign
  69. /// grid using the OpenProfile protocol.
  70. /// </remarks>
  71. public bool RequestAvatarPropertiesUsingOpenProfile(ref UserProfileProperties props)
  72. {
  73. Hashtable ReqHash = new Hashtable();
  74. ReqHash["avatar_id"] = props.UserId.ToString();
  75. Hashtable profileData = GenericXMLRPCRequest(ReqHash, "avatar_properties_request", m_serverURI);
  76. ArrayList dataArray = (ArrayList)profileData["data"];
  77. if (dataArray == null || dataArray[0] == null)
  78. return false;
  79. profileData = (Hashtable)dataArray[0];
  80. props.WebUrl = string.Empty;
  81. props.AboutText = String.Empty;
  82. props.FirstLifeText = String.Empty;
  83. props.ImageId = UUID.Zero;
  84. props.FirstLifeImageId = UUID.Zero;
  85. props.PartnerId = UUID.Zero;
  86. if (profileData["ProfileUrl"] != null)
  87. props.WebUrl = profileData["ProfileUrl"].ToString();
  88. if (profileData["AboutText"] != null)
  89. props.AboutText = profileData["AboutText"].ToString();
  90. if (profileData["FirstLifeAboutText"] != null)
  91. props.FirstLifeText = profileData["FirstLifeAboutText"].ToString();
  92. if (profileData["Image"] != null)
  93. props.ImageId = new UUID(profileData["Image"].ToString());
  94. if (profileData["FirstLifeImage"] != null)
  95. props.FirstLifeImageId = new UUID(profileData["FirstLifeImage"].ToString());
  96. if (profileData["Partner"] != null)
  97. props.PartnerId = new UUID(profileData["Partner"].ToString());
  98. props.WantToMask = 0;
  99. props.WantToText = String.Empty;
  100. props.SkillsMask = 0;
  101. props.SkillsText = String.Empty;
  102. props.Language = String.Empty;
  103. if (profileData["wantmask"] != null)
  104. props.WantToMask = Convert.ToInt32(profileData["wantmask"].ToString());
  105. if (profileData["wanttext"] != null)
  106. props.WantToText = profileData["wanttext"].ToString();
  107. if (profileData["skillsmask"] != null)
  108. props.SkillsMask = Convert.ToInt32(profileData["skillsmask"].ToString());
  109. if (profileData["skillstext"] != null)
  110. props.SkillsText = profileData["skillstext"].ToString();
  111. if (profileData["languages"] != null)
  112. props.Language = profileData["languages"].ToString();
  113. return true;
  114. }
  115. private Hashtable GenericXMLRPCRequest(Hashtable ReqParams, string method, string server)
  116. {
  117. ArrayList SendParams = new ArrayList();
  118. SendParams.Add(ReqParams);
  119. XmlRpcResponse Resp;
  120. try
  121. {
  122. XmlRpcRequest Req = new XmlRpcRequest(method, SendParams);
  123. Resp = Req.Send(server, 30000);
  124. }
  125. catch (WebException ex)
  126. {
  127. m_log.ErrorFormat("[PROFILE]: Unable to connect to Profile " +
  128. "Server {0}. Exception {1}", server, ex);
  129. Hashtable ErrorHash = new Hashtable();
  130. ErrorHash["success"] = false;
  131. ErrorHash["errorMessage"] = "Unable to fetch profile data at this time. ";
  132. ErrorHash["errorURI"] = "";
  133. return ErrorHash;
  134. }
  135. catch (SocketException ex)
  136. {
  137. m_log.ErrorFormat(
  138. "[PROFILE]: Unable to connect to Profile Server {0}. Method {1}, params {2}. " +
  139. "Exception {3}", server, method, ReqParams, ex);
  140. Hashtable ErrorHash = new Hashtable();
  141. ErrorHash["success"] = false;
  142. ErrorHash["errorMessage"] = "Unable to fetch profile data at this time. ";
  143. ErrorHash["errorURI"] = "";
  144. return ErrorHash;
  145. }
  146. catch (XmlException ex)
  147. {
  148. m_log.ErrorFormat(
  149. "[PROFILE]: Unable to connect to Profile Server {0}. Method {1}, params {2}. " +
  150. "Exception {3}", server, method, ReqParams.ToString(), ex);
  151. Hashtable ErrorHash = new Hashtable();
  152. ErrorHash["success"] = false;
  153. ErrorHash["errorMessage"] = "Unable to fetch profile data at this time. ";
  154. ErrorHash["errorURI"] = "";
  155. return ErrorHash;
  156. }
  157. if (Resp.IsFault)
  158. {
  159. Hashtable ErrorHash = new Hashtable();
  160. ErrorHash["success"] = false;
  161. ErrorHash["errorMessage"] = "Unable to fetch profile data at this time. ";
  162. ErrorHash["errorURI"] = "";
  163. return ErrorHash;
  164. }
  165. Hashtable RespData = (Hashtable)Resp.Value;
  166. return RespData;
  167. }
  168. }
  169. }