SimianFriendsServiceConnector.cs 9.3 KB

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