SimianFriendsServiceConnector.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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.Reflection;
  31. using log4net;
  32. using Nini.Config;
  33. using OpenMetaverse;
  34. using OpenMetaverse.StructuredData;
  35. using OpenSim.Framework;
  36. using OpenSim.Services.Interfaces;
  37. using FriendInfo = OpenSim.Services.Interfaces.FriendInfo;
  38. namespace OpenSim.Services.Connectors.SimianGrid
  39. {
  40. /// <summary>
  41. /// Stores and retrieves friend lists from the SimianGrid backend
  42. /// </summary>
  43. public class SimianFriendsServiceConnector : IFriendsService
  44. {
  45. private static readonly ILog m_log =
  46. LogManager.GetLogger(
  47. MethodBase.GetCurrentMethod().DeclaringType);
  48. private string m_serverUrl = String.Empty;
  49. public SimianFriendsServiceConnector(IConfigSource source)
  50. {
  51. Initialise(source);
  52. }
  53. public void Initialise(IConfigSource source)
  54. {
  55. IConfig gridConfig = source.Configs["FriendsService"];
  56. if (gridConfig != null)
  57. {
  58. string serviceUrl = gridConfig.GetString("FriendsServerURI");
  59. if (!String.IsNullOrEmpty(serviceUrl))
  60. {
  61. if (!serviceUrl.EndsWith("/") && !serviceUrl.EndsWith("="))
  62. serviceUrl = serviceUrl + '/';
  63. m_serverUrl = serviceUrl;
  64. }
  65. }
  66. if (String.IsNullOrEmpty(m_serverUrl))
  67. m_log.Info("[SIMIAN FRIENDS CONNECTOR]: No FriendsServerURI specified, disabling connector");
  68. }
  69. #region IFriendsService
  70. public FriendInfo[] GetFriends(UUID principalID)
  71. {
  72. return GetFriends(principalID.ToString());
  73. }
  74. public FriendInfo[] GetFriends(string principalID)
  75. {
  76. if (String.IsNullOrEmpty(m_serverUrl))
  77. return new FriendInfo[0];
  78. Dictionary<UUID, FriendInfo> friends = new Dictionary<UUID, FriendInfo>();
  79. OSDArray friendsArray = GetFriended(principalID);
  80. OSDArray friendedMeArray = GetFriendedBy(principalID);
  81. // Load the list of friends and their granted permissions
  82. for (int i = 0; i < friendsArray.Count; i++)
  83. {
  84. OSDMap friendEntry = friendsArray[i] as OSDMap;
  85. if (friendEntry != null)
  86. {
  87. UUID friendID = friendEntry["Key"].AsUUID();
  88. FriendInfo friend = new FriendInfo();
  89. if (!UUID.TryParse(principalID, out friend.PrincipalID))
  90. {
  91. string tmp = string.Empty;
  92. if (!Util.ParseUniversalUserIdentifier(principalID, out friend.PrincipalID, out tmp, out tmp, out tmp, out tmp))
  93. // bad record. ignore this entry
  94. continue;
  95. }
  96. friend.Friend = friendID.ToString();
  97. friend.MyFlags = friendEntry["Value"].AsInteger();
  98. friend.TheirFlags = -1;
  99. friends[friendID] = friend;
  100. }
  101. }
  102. // Load the permissions those friends have granted to this user
  103. for (int i = 0; i < friendedMeArray.Count; i++)
  104. {
  105. OSDMap friendedMeEntry = friendedMeArray[i] as OSDMap;
  106. if (friendedMeEntry != null)
  107. {
  108. UUID friendID = friendedMeEntry["OwnerID"].AsUUID();
  109. FriendInfo friend;
  110. if (friends.TryGetValue(friendID, out friend))
  111. friend.TheirFlags = friendedMeEntry["Value"].AsInteger();
  112. }
  113. }
  114. // Convert the dictionary of friends to an array and return it
  115. FriendInfo[] array = new FriendInfo[friends.Count];
  116. int j = 0;
  117. foreach (FriendInfo friend in friends.Values)
  118. array[j++] = friend;
  119. return array;
  120. }
  121. public bool StoreFriend(string principalID, string friend, int flags)
  122. {
  123. if (String.IsNullOrEmpty(m_serverUrl))
  124. return true;
  125. NameValueCollection requestArgs = new NameValueCollection
  126. {
  127. { "RequestMethod", "AddGeneric" },
  128. { "OwnerID", principalID.ToString() },
  129. { "Type", "Friend" },
  130. { "Key", friend },
  131. { "Value", flags.ToString() }
  132. };
  133. OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
  134. bool success = response["Success"].AsBoolean();
  135. if (!success)
  136. m_log.Error("[SIMIAN FRIENDS CONNECTOR]: Failed to store friend " + friend + " for user " + principalID + ": " + response["Message"].AsString());
  137. return success;
  138. }
  139. public bool Delete(UUID principalID, string friend)
  140. {
  141. return Delete(principalID.ToString(), friend);
  142. }
  143. public bool Delete(string principalID, string friend)
  144. {
  145. if (String.IsNullOrEmpty(m_serverUrl))
  146. return true;
  147. NameValueCollection requestArgs = new NameValueCollection
  148. {
  149. { "RequestMethod", "RemoveGeneric" },
  150. { "OwnerID", principalID.ToString() },
  151. { "Type", "Friend" },
  152. { "Key", friend }
  153. };
  154. OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
  155. bool success = response["Success"].AsBoolean();
  156. if (!success)
  157. m_log.Error("[SIMIAN FRIENDS CONNECTOR]: Failed to remove friend " + friend + " for user " + principalID + ": " + response["Message"].AsString());
  158. return success;
  159. }
  160. #endregion IFriendsService
  161. private OSDArray GetFriended(string ownerID)
  162. {
  163. NameValueCollection requestArgs = new NameValueCollection
  164. {
  165. { "RequestMethod", "GetGenerics" },
  166. { "OwnerID", ownerID.ToString() },
  167. { "Type", "Friend" }
  168. };
  169. OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
  170. if (response["Success"].AsBoolean() && response["Entries"] is OSDArray)
  171. {
  172. return (OSDArray)response["Entries"];
  173. }
  174. else
  175. {
  176. m_log.Warn("[SIMIAN FRIENDS CONNECTOR]: Failed to retrieve friends for user " + ownerID + ": " + response["Message"].AsString());
  177. return new OSDArray(0);
  178. }
  179. }
  180. private OSDArray GetFriendedBy(string ownerID)
  181. {
  182. NameValueCollection requestArgs = new NameValueCollection
  183. {
  184. { "RequestMethod", "GetGenerics" },
  185. { "Key", ownerID.ToString() },
  186. { "Type", "Friend" }
  187. };
  188. OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
  189. if (response["Success"].AsBoolean() && response["Entries"] is OSDArray)
  190. {
  191. return (OSDArray)response["Entries"];
  192. }
  193. else
  194. {
  195. m_log.Warn("[SIMIAN FRIENDS CONNECTOR]: Failed to retrieve reverse friends for user " + ownerID + ": " + response["Message"].AsString());
  196. return new OSDArray(0);
  197. }
  198. }
  199. }
  200. }