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