diff options
author | Diva Canto | 2011-05-19 16:54:46 -0700 |
---|---|---|
committer | Diva Canto | 2011-05-19 16:54:46 -0700 |
commit | d21e9c755f004d8fe03b11bc57b810dbd401435a (patch) | |
tree | 1efd9e48308192d21ca73d8ff12d6a48c186077c /OpenSim/Server/Handlers/Hypergrid/HGFriendsServerPostHandler.cs | |
parent | Accidentally committed too early (diff) | |
download | opensim-SC-d21e9c755f004d8fe03b11bc57b810dbd401435a.zip opensim-SC-d21e9c755f004d8fe03b11bc57b810dbd401435a.tar.gz opensim-SC-d21e9c755f004d8fe03b11bc57b810dbd401435a.tar.bz2 opensim-SC-d21e9c755f004d8fe03b11bc57b810dbd401435a.tar.xz |
HG Friends working to some extent: friendships offered and accepted correctly handled. Friends list showing correct foreign names. TODO: GrantRights.
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Server/Handlers/Hypergrid/HGFriendsServerPostHandler.cs | 242 |
1 files changed, 242 insertions, 0 deletions
diff --git a/OpenSim/Server/Handlers/Hypergrid/HGFriendsServerPostHandler.cs b/OpenSim/Server/Handlers/Hypergrid/HGFriendsServerPostHandler.cs new file mode 100644 index 0000000..13d1502 --- /dev/null +++ b/OpenSim/Server/Handlers/Hypergrid/HGFriendsServerPostHandler.cs | |||
@@ -0,0 +1,242 @@ | |||
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 Nini.Config; | ||
29 | using log4net; | ||
30 | using System; | ||
31 | using System.Reflection; | ||
32 | using System.IO; | ||
33 | using System.Net; | ||
34 | using System.Text; | ||
35 | using System.Text.RegularExpressions; | ||
36 | using System.Xml; | ||
37 | using System.Xml.Serialization; | ||
38 | using System.Collections.Generic; | ||
39 | using OpenSim.Server.Base; | ||
40 | using OpenSim.Services.Interfaces; | ||
41 | using FriendInfo = OpenSim.Services.Interfaces.FriendInfo; | ||
42 | using OpenSim.Framework; | ||
43 | using OpenSim.Framework.Servers.HttpServer; | ||
44 | using OpenMetaverse; | ||
45 | |||
46 | namespace OpenSim.Server.Handlers.Hypergrid | ||
47 | { | ||
48 | public class HGFriendsServerPostHandler : BaseStreamHandler | ||
49 | { | ||
50 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
51 | |||
52 | private IFriendsService m_FriendsService; | ||
53 | private IUserAgentService m_UserAgentService; | ||
54 | |||
55 | public HGFriendsServerPostHandler(IFriendsService service, IUserAgentService uservice) : | ||
56 | base("POST", "/hgfriends") | ||
57 | { | ||
58 | m_FriendsService = service; | ||
59 | m_UserAgentService = uservice; | ||
60 | m_log.DebugFormat("[HGFRIENDS HANDLER]: HGFriendsServerPostHandler is On"); | ||
61 | } | ||
62 | |||
63 | public override byte[] Handle(string path, Stream requestData, | ||
64 | OSHttpRequest httpRequest, OSHttpResponse httpResponse) | ||
65 | { | ||
66 | StreamReader sr = new StreamReader(requestData); | ||
67 | string body = sr.ReadToEnd(); | ||
68 | sr.Close(); | ||
69 | body = body.Trim(); | ||
70 | |||
71 | //m_log.DebugFormat("[XXX]: query String: {0}", body); | ||
72 | |||
73 | try | ||
74 | { | ||
75 | Dictionary<string, object> request = | ||
76 | ServerUtils.ParseQueryString(body); | ||
77 | |||
78 | if (!request.ContainsKey("METHOD")) | ||
79 | return FailureResult(); | ||
80 | |||
81 | string method = request["METHOD"].ToString(); | ||
82 | |||
83 | switch (method) | ||
84 | { | ||
85 | case "getfriends": | ||
86 | return GetFriends(request); | ||
87 | |||
88 | case "newfriendship": | ||
89 | return NewFriendship(request); | ||
90 | |||
91 | } | ||
92 | m_log.DebugFormat("[HGFRIENDS HANDLER]: unknown method {0} request {1}", method.Length, method); | ||
93 | } | ||
94 | catch (Exception e) | ||
95 | { | ||
96 | m_log.DebugFormat("[HGFRIENDS HANDLER]: Exception {0}", e); | ||
97 | } | ||
98 | |||
99 | return FailureResult(); | ||
100 | |||
101 | } | ||
102 | |||
103 | #region Method-specific handlers | ||
104 | |||
105 | byte[] GetFriends(Dictionary<string, object> request) | ||
106 | { | ||
107 | UUID principalID = UUID.Zero; | ||
108 | if (request.ContainsKey("PRINCIPALID")) | ||
109 | UUID.TryParse(request["PRINCIPALID"].ToString(), out principalID); | ||
110 | else | ||
111 | m_log.WarnFormat("[HGFRIENDS HANDLER]: no principalID in request to get friends"); | ||
112 | |||
113 | FriendInfo[] finfos = m_FriendsService.GetFriends(principalID); | ||
114 | //m_log.DebugFormat("[FRIENDS HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count); | ||
115 | |||
116 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
117 | if ((finfos == null) || ((finfos != null) && (finfos.Length == 0))) | ||
118 | result["result"] = "null"; | ||
119 | else | ||
120 | { | ||
121 | int i = 0; | ||
122 | foreach (FriendInfo finfo in finfos) | ||
123 | { | ||
124 | Dictionary<string, object> rinfoDict = finfo.ToKeyValuePairs(); | ||
125 | result["friend" + i] = rinfoDict; | ||
126 | i++; | ||
127 | } | ||
128 | } | ||
129 | |||
130 | string xmlString = ServerUtils.BuildXmlResponse(result); | ||
131 | //m_log.DebugFormat("[FRIENDS HANDLER]: resp string: {0}", xmlString); | ||
132 | UTF8Encoding encoding = new UTF8Encoding(); | ||
133 | return encoding.GetBytes(xmlString); | ||
134 | |||
135 | } | ||
136 | |||
137 | byte[] NewFriendship(Dictionary<string, object> request) | ||
138 | { | ||
139 | if (!request.ContainsKey("KEY") || !request.ContainsKey("SESSIONID")) | ||
140 | { | ||
141 | m_log.WarnFormat("[HGFRIENDS HANDLER]: ignoring request without Key or SessionID"); | ||
142 | return FailureResult(); | ||
143 | } | ||
144 | |||
145 | string serviceKey = request["KEY"].ToString(); | ||
146 | string sessionStr = request["SESSIONID"].ToString(); | ||
147 | UUID sessionID; | ||
148 | UUID.TryParse(sessionStr, out sessionID); | ||
149 | |||
150 | if (!m_UserAgentService.VerifyAgent(sessionID, serviceKey)) | ||
151 | { | ||
152 | m_log.WarnFormat("[HGFRIENDS HANDLER]: Key {0} for session {1} did not match existing key. Ignoring request", serviceKey, sessionID); | ||
153 | return FailureResult(); | ||
154 | } | ||
155 | |||
156 | m_log.DebugFormat("[XXX] Verification ok"); | ||
157 | // OK, can proceed | ||
158 | FriendInfo friend = new FriendInfo(request); | ||
159 | |||
160 | // the user needs to confirm when he gets home | ||
161 | bool success = m_FriendsService.StoreFriend(friend.PrincipalID.ToString(), friend.Friend, 0); | ||
162 | //if (success) | ||
163 | // m_FriendsService.StoreFriend(friend.Friend, friend.PrincipalID.ToString(), 1); | ||
164 | |||
165 | if (success) | ||
166 | return SuccessResult(); | ||
167 | else | ||
168 | return FailureResult(); | ||
169 | } | ||
170 | |||
171 | #endregion | ||
172 | |||
173 | #region Misc | ||
174 | |||
175 | private byte[] SuccessResult() | ||
176 | { | ||
177 | XmlDocument doc = new XmlDocument(); | ||
178 | |||
179 | XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, | ||
180 | "", ""); | ||
181 | |||
182 | doc.AppendChild(xmlnode); | ||
183 | |||
184 | XmlElement rootElement = doc.CreateElement("", "ServerResponse", | ||
185 | ""); | ||
186 | |||
187 | doc.AppendChild(rootElement); | ||
188 | |||
189 | XmlElement result = doc.CreateElement("", "Result", ""); | ||
190 | result.AppendChild(doc.CreateTextNode("Success")); | ||
191 | |||
192 | rootElement.AppendChild(result); | ||
193 | |||
194 | return DocToBytes(doc); | ||
195 | } | ||
196 | |||
197 | private byte[] FailureResult() | ||
198 | { | ||
199 | return FailureResult(String.Empty); | ||
200 | } | ||
201 | |||
202 | private byte[] FailureResult(string msg) | ||
203 | { | ||
204 | XmlDocument doc = new XmlDocument(); | ||
205 | |||
206 | XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, | ||
207 | "", ""); | ||
208 | |||
209 | doc.AppendChild(xmlnode); | ||
210 | |||
211 | XmlElement rootElement = doc.CreateElement("", "ServerResponse", | ||
212 | ""); | ||
213 | |||
214 | doc.AppendChild(rootElement); | ||
215 | |||
216 | XmlElement result = doc.CreateElement("", "Result", ""); | ||
217 | result.AppendChild(doc.CreateTextNode("Failure")); | ||
218 | |||
219 | rootElement.AppendChild(result); | ||
220 | |||
221 | XmlElement message = doc.CreateElement("", "Message", ""); | ||
222 | message.AppendChild(doc.CreateTextNode(msg)); | ||
223 | |||
224 | rootElement.AppendChild(message); | ||
225 | |||
226 | return DocToBytes(doc); | ||
227 | } | ||
228 | |||
229 | private byte[] DocToBytes(XmlDocument doc) | ||
230 | { | ||
231 | MemoryStream ms = new MemoryStream(); | ||
232 | XmlTextWriter xw = new XmlTextWriter(ms, null); | ||
233 | xw.Formatting = Formatting.Indented; | ||
234 | doc.WriteTo(xw); | ||
235 | xw.Flush(); | ||
236 | |||
237 | return ms.ToArray(); | ||
238 | } | ||
239 | |||
240 | #endregion | ||
241 | } | ||
242 | } | ||