HGFriendsServiceConnector.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. try
  73. {
  74. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  75. m_ServerURI + "/hgfriends",
  76. reqString);
  77. if (reply != string.Empty)
  78. {
  79. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  80. if ((replyData != null) && replyData.ContainsKey("Value") && (replyData["Value"] != null))
  81. {
  82. uint perms = 0;
  83. uint.TryParse(replyData["Value"].ToString(), out perms);
  84. return perms;
  85. }
  86. else
  87. m_log.DebugFormat("[HGFRIENDS CONNECTOR]: GetFriendPerms {0} received null response",
  88. PrincipalID);
  89. }
  90. }
  91. catch (Exception e)
  92. {
  93. m_log.DebugFormat("[HGFRIENDS CONNECTOR]: Exception when contacting friends server: {0}", e.Message);
  94. }
  95. return 0;
  96. }
  97. public bool NewFriendship(UUID PrincipalID, string Friend)
  98. {
  99. FriendInfo finfo = new FriendInfo();
  100. finfo.PrincipalID = PrincipalID;
  101. finfo.Friend = Friend;
  102. Dictionary<string, object> sendData = finfo.ToKeyValuePairs();
  103. sendData["METHOD"] = "newfriendship";
  104. sendData["KEY"] = m_ServiceKey;
  105. sendData["SESSIONID"] = m_SessionID.ToString();
  106. string reply = string.Empty;
  107. try
  108. {
  109. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  110. m_ServerURI + "/hgfriends",
  111. ServerUtils.BuildQueryString(sendData));
  112. }
  113. catch (Exception e)
  114. {
  115. m_log.DebugFormat("[HGFRIENDS CONNECTOR]: Exception when contacting friends server: {0}", e.Message);
  116. return false;
  117. }
  118. if (reply != string.Empty)
  119. {
  120. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  121. if ((replyData != null) && replyData.ContainsKey("Result") && (replyData["Result"] != null))
  122. {
  123. bool success = false;
  124. Boolean.TryParse(replyData["Result"].ToString(), out success);
  125. return success;
  126. }
  127. else
  128. m_log.DebugFormat("[HGFRIENDS CONNECTOR]: StoreFriend {0} {1} received null response",
  129. PrincipalID, Friend);
  130. }
  131. else
  132. m_log.DebugFormat("[HGFRIENDS CONNECTOR]: StoreFriend received null reply");
  133. return false;
  134. }
  135. public bool DeleteFriendship(UUID PrincipalID, UUID Friend, string secret)
  136. {
  137. FriendInfo finfo = new FriendInfo();
  138. finfo.PrincipalID = PrincipalID;
  139. finfo.Friend = Friend.ToString();
  140. Dictionary<string, object> sendData = finfo.ToKeyValuePairs();
  141. sendData["METHOD"] = "deletefriendship";
  142. sendData["SECRET"] = secret;
  143. string reply = string.Empty;
  144. try
  145. {
  146. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  147. m_ServerURI + "/hgfriends",
  148. ServerUtils.BuildQueryString(sendData));
  149. }
  150. catch (Exception e)
  151. {
  152. m_log.DebugFormat("[HGFRIENDS CONNECTOR]: Exception when contacting friends server: {0}", e.Message);
  153. return false;
  154. }
  155. if (reply != string.Empty)
  156. {
  157. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  158. if ((replyData != null) && replyData.ContainsKey("Result") && (replyData["Result"] != null))
  159. {
  160. bool success = false;
  161. Boolean.TryParse(replyData["Result"].ToString(), out success);
  162. return success;
  163. }
  164. else
  165. m_log.DebugFormat("[HGFRIENDS CONNECTOR]: Delete {0} {1} received null response",
  166. PrincipalID, Friend);
  167. }
  168. else
  169. m_log.DebugFormat("[HGFRIENDS CONNECTOR]: DeleteFriend received null reply");
  170. return false;
  171. }
  172. #endregion
  173. }
  174. }