clockwerk-opensim-stable – Blame information for rev 1

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