OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
FriendsRequestHandler.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.IO;
31 using System.Reflection;
32 using System.Xml;
33 
34 using OpenSim.Framework;
35 using OpenSim.Server.Base;
36 using OpenSim.Framework.Servers.HttpServer;
38 using OpenSim.Services.Interfaces;
39 
40 using OpenMetaverse;
41 using log4net;
42 
43 namespace OpenSim.Region.CoreModules.Avatar.Friends
44 {
46  {
47  private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
48 
49  private FriendsModule m_FriendsModule;
50 
52  : base("POST", "/friends", new BasicDosProtectorOptions()
53  {
54  AllowXForwardedFor = true,
55  ForgetTimeSpan = TimeSpan.FromMinutes(2),
56  MaxRequestsInTimeframe = 20,
57  ReportingName = "FRIENDSDOSPROTECTOR",
58  RequestTimeSpan = TimeSpan.FromSeconds(5),
59  ThrottledAction = BasicDOSProtector.ThrottleAction.DoThrottledMethod
60  })
61  {
62  m_FriendsModule = fmodule;
63  }
64 
65  protected override byte[] ProcessRequest(
66  string path, Stream requestData, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
67  {
68  StreamReader sr = new StreamReader(requestData);
69  string body = sr.ReadToEnd();
70  sr.Close();
71  body = body.Trim();
72 
73  //m_log.DebugFormat("[XXX]: query String: {0}", body);
74 
75  try
76  {
77  Dictionary<string, object> request =
78  ServerUtils.ParseQueryString(body);
79 
80  if (!request.ContainsKey("METHOD"))
81  return FailureResult();
82 
83  string method = request["METHOD"].ToString();
84  request.Remove("METHOD");
85 
86  switch (method)
87  {
88  case "friendship_offered":
89  return FriendshipOffered(request);
90  case "friendship_approved":
91  return FriendshipApproved(request);
92  case "friendship_denied":
93  return FriendshipDenied(request);
94  case "friendship_terminated":
95  return FriendshipTerminated(request);
96  case "grant_rights":
97  return GrantRights(request);
98  case "status":
99  return StatusNotification(request);
100  }
101  }
102  catch (Exception e)
103  {
104  m_log.Debug("[FRIENDS]: Exception {0}" + e.ToString());
105  }
106 
107  return FailureResult();
108  }
109 
110  byte[] FriendshipOffered(Dictionary<string, object> request)
111  {
112  UUID fromID = UUID.Zero;
113  UUID toID = UUID.Zero;
114  string message = string.Empty;
115 
116  if (!request.ContainsKey("FromID") || !request.ContainsKey("ToID"))
117  return FailureResult();
118 
119  message = request["Message"].ToString();
120 
121  if (!UUID.TryParse(request["FromID"].ToString(), out fromID))
122  return FailureResult();
123 
124  if (!UUID.TryParse(request["ToID"].ToString(), out toID))
125  return FailureResult();
126 
127  UserAccount account = m_FriendsModule.UserAccountService.GetUserAccount(UUID.Zero, fromID);
128  string name = (account == null) ? "Unknown" : account.FirstName + " " + account.LastName;
129 
130  GridInstantMessage im = new GridInstantMessage(m_FriendsModule.Scene, fromID, name, toID,
131  (byte)InstantMessageDialog.FriendshipOffered, message, false, Vector3.Zero);
132 
133  // !! HACK
134  im.imSessionID = im.fromAgentID;
135 
136  if (m_FriendsModule.LocalFriendshipOffered(toID, im))
137  return SuccessResult();
138 
139  return FailureResult();
140  }
141 
142  byte[] FriendshipApproved(Dictionary<string, object> request)
143  {
144  UUID fromID = UUID.Zero;
145  UUID toID = UUID.Zero;
146  string fromName = string.Empty;
147 
148  if (!request.ContainsKey("FromID") || !request.ContainsKey("ToID"))
149  return FailureResult();
150 
151  if (!UUID.TryParse(request["FromID"].ToString(), out fromID))
152  return FailureResult();
153 
154  if (!UUID.TryParse(request["ToID"].ToString(), out toID))
155  return FailureResult();
156 
157  if (request.ContainsKey("FromName"))
158  fromName = request["FromName"].ToString();
159 
160  if (m_FriendsModule.LocalFriendshipApproved(fromID, fromName, toID))
161  return SuccessResult();
162 
163  return FailureResult();
164  }
165 
166  byte[] FriendshipDenied(Dictionary<string, object> request)
167  {
168  UUID fromID = UUID.Zero;
169  UUID toID = UUID.Zero;
170  string fromName = string.Empty;
171 
172  if (!request.ContainsKey("FromID") || !request.ContainsKey("ToID"))
173  return FailureResult();
174 
175  if (!UUID.TryParse(request["FromID"].ToString(), out fromID))
176  return FailureResult();
177 
178  if (!UUID.TryParse(request["ToID"].ToString(), out toID))
179  return FailureResult();
180 
181  if (request.ContainsKey("FromName"))
182  fromName = request["FromName"].ToString();
183 
184  if (m_FriendsModule.LocalFriendshipDenied(fromID, fromName, toID))
185  return SuccessResult();
186 
187  return FailureResult();
188  }
189 
190  byte[] FriendshipTerminated(Dictionary<string, object> request)
191  {
192  UUID fromID = UUID.Zero;
193  UUID toID = UUID.Zero;
194 
195  if (!request.ContainsKey("FromID") || !request.ContainsKey("ToID"))
196  return FailureResult();
197 
198  if (!UUID.TryParse(request["FromID"].ToString(), out fromID))
199  return FailureResult();
200 
201  if (!UUID.TryParse(request["ToID"].ToString(), out toID))
202  return FailureResult();
203 
204  if (m_FriendsModule.LocalFriendshipTerminated(fromID, toID))
205  return SuccessResult();
206 
207  return FailureResult();
208  }
209 
210  byte[] GrantRights(Dictionary<string, object> request)
211  {
212  UUID fromID = UUID.Zero;
213  UUID toID = UUID.Zero;
214  int oldRights = 0, newRights = 0;
215 
216  if (!request.ContainsKey("FromID") || !request.ContainsKey("ToID"))
217  return FailureResult();
218 
219  if (!UUID.TryParse(request["FromID"].ToString(), out fromID))
220  return FailureResult();
221 
222  if (!UUID.TryParse(request["ToID"].ToString(), out toID))
223  return FailureResult();
224 
225  if (!Int32.TryParse(request["UserFlags"].ToString(), out oldRights))
226  return FailureResult();
227 
228  if (!Int32.TryParse(request["Rights"].ToString(), out newRights))
229  return FailureResult();
230 
231  if (m_FriendsModule.LocalGrantRights(fromID, toID, oldRights, newRights))
232  return SuccessResult();
233 
234  return FailureResult();
235  }
236 
237  byte[] StatusNotification(Dictionary<string, object> request)
238  {
239  UUID fromID = UUID.Zero;
240  UUID toID = UUID.Zero;
241  bool online = false;
242 
243  if (!request.ContainsKey("FromID") || !request.ContainsKey("ToID") || !request.ContainsKey("Online"))
244  return FailureResult();
245 
246  if (!UUID.TryParse(request["FromID"].ToString(), out fromID))
247  return FailureResult();
248 
249  if (!UUID.TryParse(request["ToID"].ToString(), out toID))
250  return FailureResult();
251 
252  if (!Boolean.TryParse(request["Online"].ToString(), out online))
253  return FailureResult();
254 
255  if (m_FriendsModule.LocalStatusNotification(fromID, toID, online))
256  return SuccessResult();
257 
258  return FailureResult();
259  }
260 
261  #region Misc
262 
263  private byte[] FailureResult()
264  {
265  return BoolResult(false);
266  }
267 
268  private byte[] SuccessResult()
269  {
270  return BoolResult(true);
271  }
272 
273  private byte[] BoolResult(bool value)
274  {
275  XmlDocument doc = new XmlDocument();
276 
277  XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
278  "", "");
279 
280  doc.AppendChild(xmlnode);
281 
282  XmlElement rootElement = doc.CreateElement("", "ServerResponse",
283  "");
284 
285  doc.AppendChild(rootElement);
286 
287  XmlElement result = doc.CreateElement("", "RESULT", "");
288  result.AppendChild(doc.CreateTextNode(value.ToString()));
289 
290  rootElement.AppendChild(result);
291 
292  return Util.DocToBytes(doc);
293  }
294 
295  #endregion
296  }
297 }
override byte[] ProcessRequest(string path, Stream requestData, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
OpenSim.Services.Interfaces.FriendInfo FriendInfo
BaseStreamHandlerBasicDOSProtector Base streamed request handler.