HGFriendsServicesConnector.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 : FriendsSimConnector
  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. protected override string ServicePath()
  63. {
  64. return "hgfriends";
  65. }
  66. #region IFriendsService
  67. public uint GetFriendPerms(UUID PrincipalID, UUID friendID)
  68. {
  69. Dictionary<string, object> sendData = new Dictionary<string, object>();
  70. sendData["PRINCIPALID"] = PrincipalID.ToString();
  71. sendData["FRIENDID"] = friendID.ToString();
  72. sendData["METHOD"] = "getfriendperms";
  73. sendData["KEY"] = m_ServiceKey;
  74. sendData["SESSIONID"] = m_SessionID.ToString();
  75. string reqString = ServerUtils.BuildQueryString(sendData);
  76. string uri = m_ServerURI + "/hgfriends";
  77. try
  78. {
  79. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  80. uri,
  81. reqString);
  82. if (reply != string.Empty)
  83. {
  84. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  85. if ((replyData != null) && replyData.ContainsKey("Value") && (replyData["Value"] != null))
  86. {
  87. uint perms = 0;
  88. uint.TryParse(replyData["Value"].ToString(), out perms);
  89. return perms;
  90. }
  91. else
  92. m_log.DebugFormat("[HGFRIENDS CONNECTOR]: GetFriendPerms {0} received null response",
  93. PrincipalID);
  94. }
  95. }
  96. catch (Exception e)
  97. {
  98. m_log.DebugFormat("[HGFRIENDS CONNECTOR]: Exception when contacting friends server at {0}: {1}", uri, e.Message);
  99. }
  100. return 0;
  101. }
  102. public bool NewFriendship(UUID PrincipalID, string Friend)
  103. {
  104. FriendInfo finfo = new FriendInfo();
  105. finfo.PrincipalID = PrincipalID;
  106. finfo.Friend = Friend;
  107. Dictionary<string, object> sendData = finfo.ToKeyValuePairs();
  108. sendData["METHOD"] = "newfriendship";
  109. sendData["KEY"] = m_ServiceKey;
  110. sendData["SESSIONID"] = m_SessionID.ToString();
  111. string reply = string.Empty;
  112. string uri = m_ServerURI + "/hgfriends";
  113. try
  114. {
  115. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  116. uri,
  117. ServerUtils.BuildQueryString(sendData));
  118. }
  119. catch (Exception e)
  120. {
  121. m_log.DebugFormat("[HGFRIENDS CONNECTOR]: Exception when contacting friends server at {0}: {1}", uri, e.Message);
  122. return false;
  123. }
  124. if (reply != string.Empty)
  125. {
  126. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  127. if ((replyData != null) && replyData.ContainsKey("Result") && (replyData["Result"] != null))
  128. {
  129. bool success = false;
  130. Boolean.TryParse(replyData["Result"].ToString(), out success);
  131. return success;
  132. }
  133. else
  134. m_log.DebugFormat("[HGFRIENDS CONNECTOR]: StoreFriend {0} {1} received null response",
  135. PrincipalID, Friend);
  136. }
  137. else
  138. m_log.DebugFormat("[HGFRIENDS CONNECTOR]: StoreFriend received null reply");
  139. return false;
  140. }
  141. public bool DeleteFriendship(UUID PrincipalID, UUID Friend, string secret)
  142. {
  143. FriendInfo finfo = new FriendInfo();
  144. finfo.PrincipalID = PrincipalID;
  145. finfo.Friend = Friend.ToString();
  146. Dictionary<string, object> sendData = finfo.ToKeyValuePairs();
  147. sendData["METHOD"] = "deletefriendship";
  148. sendData["SECRET"] = secret;
  149. string reply = string.Empty;
  150. string uri = m_ServerURI + "/hgfriends";
  151. try
  152. {
  153. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  154. uri,
  155. ServerUtils.BuildQueryString(sendData));
  156. }
  157. catch (Exception e)
  158. {
  159. m_log.DebugFormat("[HGFRIENDS CONNECTOR]: Exception when contacting friends server at {0}: {1}", uri, e.Message);
  160. return false;
  161. }
  162. if (reply != string.Empty)
  163. {
  164. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  165. if (replyData.ContainsKey("RESULT"))
  166. {
  167. if (replyData["RESULT"].ToString().ToLower() == "true")
  168. return true;
  169. else
  170. return false;
  171. }
  172. else
  173. m_log.DebugFormat("[HGFRIENDS CONNECTOR]: reply data does not contain result field");
  174. }
  175. else
  176. m_log.DebugFormat("[HGFRIENDS CONNECTOR]: received empty reply");
  177. return false;
  178. }
  179. public bool ValidateFriendshipOffered(UUID fromID, UUID toID)
  180. {
  181. FriendInfo finfo = new FriendInfo();
  182. finfo.PrincipalID = fromID;
  183. finfo.Friend = toID.ToString();
  184. Dictionary<string, object> sendData = finfo.ToKeyValuePairs();
  185. sendData["METHOD"] = "validate_friendship_offered";
  186. string reply = string.Empty;
  187. string uri = m_ServerURI + "/hgfriends";
  188. try
  189. {
  190. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  191. uri,
  192. ServerUtils.BuildQueryString(sendData));
  193. }
  194. catch (Exception e)
  195. {
  196. m_log.DebugFormat("[HGFRIENDS CONNECTOR]: Exception when contacting friends server at {0}: {1}", uri, e.Message);
  197. return false;
  198. }
  199. if (reply != string.Empty)
  200. {
  201. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  202. if (replyData.ContainsKey("RESULT"))
  203. {
  204. if (replyData["RESULT"].ToString().ToLower() == "true")
  205. return true;
  206. else
  207. return false;
  208. }
  209. else
  210. m_log.DebugFormat("[HGFRIENDS CONNECTOR]: reply data does not contain result field");
  211. }
  212. else
  213. m_log.DebugFormat("[HGFRIENDS CONNECTOR]: received empty reply");
  214. return false;
  215. }
  216. public List<UUID> StatusNotification(List<string> friends, UUID userID, bool online)
  217. {
  218. Dictionary<string, object> sendData = new Dictionary<string, object>();
  219. List<UUID> friendsOnline = new List<UUID>();
  220. sendData["METHOD"] = "statusnotification";
  221. sendData["userID"] = userID.ToString();
  222. sendData["online"] = online.ToString();
  223. int i = 0;
  224. foreach (string s in friends)
  225. {
  226. sendData["friend_" + i.ToString()] = s;
  227. i++;
  228. }
  229. string reply = string.Empty;
  230. string uri = m_ServerURI + "/hgfriends";
  231. try
  232. {
  233. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  234. uri,
  235. ServerUtils.BuildQueryString(sendData), 15);
  236. }
  237. catch (Exception e)
  238. {
  239. m_log.DebugFormat("[HGFRIENDS CONNECTOR]: Exception when contacting friends server at {0}: {1}", uri, e.Message);
  240. return friendsOnline;
  241. }
  242. if (reply != string.Empty)
  243. {
  244. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  245. // Here is the actual response
  246. foreach (string key in replyData.Keys)
  247. {
  248. if (key.StartsWith("friend_") && replyData[key] != null)
  249. {
  250. UUID uuid;
  251. if (UUID.TryParse(replyData[key].ToString(), out uuid))
  252. friendsOnline.Add(uuid);
  253. }
  254. }
  255. }
  256. else
  257. m_log.DebugFormat("[HGFRIENDS CONNECTOR]: Received empty reply from remote StatusNotify");
  258. return friendsOnline;
  259. }
  260. #endregion
  261. }
  262. }