HGFriendsServiceConnector.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 log4net;
  28. using System;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Reflection;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Services.Interfaces;
  35. using OpenSim.Services.Connectors.Friends;
  36. using FriendInfo = OpenSim.Services.Interfaces.FriendInfo;
  37. using OpenSim.Server.Base;
  38. using OpenMetaverse;
  39. namespace OpenSim.Services.Connectors.Hypergrid
  40. {
  41. public class HGFriendsServicesConnector
  42. {
  43. private static readonly ILog m_log =
  44. LogManager.GetLogger(
  45. MethodBase.GetCurrentMethod().DeclaringType);
  46. private string m_ServerURI = String.Empty;
  47. private string m_ServiceKey = String.Empty;
  48. private UUID m_SessionID;
  49. public HGFriendsServicesConnector()
  50. {
  51. }
  52. public HGFriendsServicesConnector(string serverURI)
  53. {
  54. m_ServerURI = serverURI.TrimEnd('/');
  55. }
  56. public HGFriendsServicesConnector(string serverURI, UUID sessionID, string serviceKey)
  57. {
  58. m_ServerURI = serverURI.TrimEnd('/');
  59. m_ServiceKey = serviceKey;
  60. m_SessionID = sessionID;
  61. }
  62. #region IFriendsService
  63. public uint GetFriendPerms(UUID PrincipalID, UUID friendID)
  64. {
  65. Dictionary<string, object> sendData = new Dictionary<string, object>();
  66. sendData["PRINCIPALID"] = PrincipalID.ToString();
  67. sendData["FRIENDID"] = friendID.ToString();
  68. sendData["METHOD"] = "getfriendperms";
  69. sendData["KEY"] = m_ServiceKey;
  70. sendData["SESSIONID"] = m_SessionID.ToString();
  71. string reqString = ServerUtils.BuildQueryString(sendData);
  72. string uri = m_ServerURI + "/hgfriends";
  73. try
  74. {
  75. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  76. uri,
  77. reqString);
  78. if (reply != string.Empty)
  79. {
  80. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  81. if ((replyData != null) && replyData.ContainsKey("Value") && (replyData["Value"] != null))
  82. {
  83. uint perms = 0;
  84. uint.TryParse(replyData["Value"].ToString(), out perms);
  85. return perms;
  86. }
  87. else
  88. m_log.DebugFormat("[HGFRIENDS CONNECTOR]: GetFriendPerms {0} received null response",
  89. PrincipalID);
  90. }
  91. }
  92. catch (Exception e)
  93. {
  94. m_log.DebugFormat("[HGFRIENDS CONNECTOR]: Exception when contacting friends server at {0}: {1}", uri, e.Message);
  95. }
  96. return 0;
  97. }
  98. public bool NewFriendship(UUID PrincipalID, string Friend)
  99. {
  100. FriendInfo finfo = new FriendInfo();
  101. finfo.PrincipalID = PrincipalID;
  102. finfo.Friend = Friend;
  103. Dictionary<string, object> sendData = finfo.ToKeyValuePairs();
  104. sendData["METHOD"] = "newfriendship";
  105. sendData["KEY"] = m_ServiceKey;
  106. sendData["SESSIONID"] = m_SessionID.ToString();
  107. string reply = string.Empty;
  108. string uri = m_ServerURI + "/hgfriends";
  109. try
  110. {
  111. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  112. uri,
  113. ServerUtils.BuildQueryString(sendData));
  114. }
  115. catch (Exception e)
  116. {
  117. m_log.DebugFormat("[HGFRIENDS CONNECTOR]: Exception when contacting friends server at {0}: {1}", uri, e.Message);
  118. return false;
  119. }
  120. if (reply != string.Empty)
  121. {
  122. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  123. if ((replyData != null) && replyData.ContainsKey("Result") && (replyData["Result"] != null))
  124. {
  125. bool success = false;
  126. Boolean.TryParse(replyData["Result"].ToString(), out success);
  127. return success;
  128. }
  129. else
  130. m_log.DebugFormat("[HGFRIENDS CONNECTOR]: StoreFriend {0} {1} received null response",
  131. PrincipalID, Friend);
  132. }
  133. else
  134. m_log.DebugFormat("[HGFRIENDS CONNECTOR]: StoreFriend received null reply");
  135. return false;
  136. }
  137. public bool DeleteFriendship(UUID PrincipalID, UUID Friend, string secret)
  138. {
  139. FriendInfo finfo = new FriendInfo();
  140. finfo.PrincipalID = PrincipalID;
  141. finfo.Friend = Friend.ToString();
  142. Dictionary<string, object> sendData = finfo.ToKeyValuePairs();
  143. sendData["METHOD"] = "deletefriendship";
  144. sendData["SECRET"] = secret;
  145. string reply = string.Empty;
  146. string uri = m_ServerURI + "/hgfriends";
  147. try
  148. {
  149. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  150. uri,
  151. ServerUtils.BuildQueryString(sendData));
  152. }
  153. catch (Exception e)
  154. {
  155. m_log.DebugFormat("[HGFRIENDS CONNECTOR]: Exception when contacting friends server at {0}: {1}", uri, e.Message);
  156. return false;
  157. }
  158. if (reply != string.Empty)
  159. {
  160. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  161. if ((replyData != null) && replyData.ContainsKey("Result") && (replyData["Result"] != null))
  162. {
  163. bool success = false;
  164. Boolean.TryParse(replyData["Result"].ToString(), out success);
  165. return success;
  166. }
  167. else
  168. m_log.DebugFormat("[HGFRIENDS CONNECTOR]: Delete {0} {1} received null response",
  169. PrincipalID, Friend);
  170. }
  171. else
  172. m_log.DebugFormat("[HGFRIENDS CONNECTOR]: DeleteFriend received null reply");
  173. return false;
  174. }
  175. #endregion
  176. }
  177. }