31 using System.Reflection;
35 using System.Text.RegularExpressions;
37 using System.Xml.Serialization;
38 using System.Collections.Generic;
39 using OpenSim.Server.Base;
40 using OpenSim.Services.Interfaces;
43 using OpenSim.Framework;
44 using OpenSim.Framework.Servers.HttpServer;
47 namespace OpenSim.Server.Handlers.Hypergrid
51 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
58 base(
"POST",
"/hgfriends")
60 m_TheService = service;
61 m_UserAgentService = uas;
62 m_FriendsLocalSimConnector = friendsConn;
64 m_log.DebugFormat(
"[HGFRIENDS HANDLER]: HGFriendsServerPostHandler is On ({0})",
65 (m_FriendsLocalSimConnector == null ?
"robust" :
"standalone"));
67 if (m_TheService == null)
68 m_log.ErrorFormat(
"[HGFRIENDS HANDLER]: TheService is null!");
74 StreamReader sr =
new StreamReader(requestData);
75 string body = sr.ReadToEnd();
83 Dictionary<string, object> request =
84 ServerUtils.ParseQueryString(body);
86 if (!request.ContainsKey(
"METHOD"))
87 return FailureResult();
89 string method = request[
"METHOD"].ToString();
93 case "getfriendperms":
94 return GetFriendPerms(request);
97 return NewFriendship(request);
99 case "deletefriendship":
100 return DeleteFriendship(request);
103 case "friendship_offered":
104 return FriendshipOffered(request);
106 case "validate_friendship_offered":
107 return ValidateFriendshipOffered(request);
109 case "statusnotification":
110 return StatusNotification(request);
126 m_log.DebugFormat(
"[HGFRIENDS HANDLER]: unknown method {0}", method);
130 m_log.DebugFormat(
"[HGFRIENDS HANDLER]: Exception {0}", e);
133 return FailureResult();
136 #region Method-specific handlers
138 byte[] GetFriendPerms(Dictionary<string, object> request)
140 if (!VerifyServiceKey(request))
141 return FailureResult();
143 UUID principalID = UUID.Zero;
144 if (request.ContainsKey(
"PRINCIPALID"))
145 UUID.TryParse(request[
"PRINCIPALID"].ToString(), out principalID);
148 m_log.WarnFormat(
"[HGFRIENDS HANDLER]: no principalID in request to get friend perms");
149 return FailureResult();
152 UUID friendID = UUID.Zero;
153 if (request.ContainsKey(
"FRIENDID"))
154 UUID.TryParse(request[
"FRIENDID"].ToString(), out friendID);
157 m_log.WarnFormat(
"[HGFRIENDS HANDLER]: no friendID in request to get friend perms");
158 return FailureResult();
161 int perms = m_TheService.GetFriendPerms(principalID, friendID);
163 return FailureResult(
"Friend not found");
165 return SuccessResult(perms.ToString());
168 byte[] NewFriendship(Dictionary<string, object> request)
170 bool verified = VerifyServiceKey(request);
174 bool success = m_TheService.NewFriendship(
friend, verified);
177 return SuccessResult();
179 return FailureResult();
182 byte[] DeleteFriendship(Dictionary<string, object> request)
185 string secret = string.Empty;
186 if (request.ContainsKey(
"SECRET"))
187 secret = request[
"SECRET"].ToString();
189 if (secret ==
string.Empty)
190 return BoolResult(
false);
192 bool success = m_TheService.DeleteFriendship(
friend, secret);
194 return BoolResult(success);
197 byte[] FriendshipOffered(Dictionary<string, object> request)
199 UUID fromID = UUID.Zero;
200 UUID toID = UUID.Zero;
201 string message = string.Empty;
202 string name = string.Empty;
204 if (!request.ContainsKey(
"FromID") || !request.ContainsKey(
"ToID"))
205 return BoolResult(
false);
207 if (!
UUID.TryParse(request[
"ToID"].ToString(), out toID))
208 return BoolResult(
false);
210 message = request[
"Message"].ToString();
212 if (!
UUID.TryParse(request[
"FromID"].ToString(), out fromID))
213 return BoolResult(
false);
215 if (request.ContainsKey(
"FromName"))
216 name = request[
"FromName"].ToString();
218 bool success = m_TheService.FriendshipOffered(fromID, name, toID, message);
220 return BoolResult(success);
223 byte[] ValidateFriendshipOffered(Dictionary<string, object> request)
226 UUID friendID = UUID.Zero;
227 if (!
UUID.TryParse(
friend.Friend, out friendID))
228 return BoolResult(
false);
230 bool success = m_TheService.ValidateFriendshipOffered(friend.PrincipalID, friendID);
232 return BoolResult(success);
235 byte[] StatusNotification(Dictionary<string, object> request)
237 UUID principalID = UUID.Zero;
238 if (request.ContainsKey(
"userID"))
239 UUID.TryParse(request[
"userID"].ToString(), out principalID);
242 m_log.WarnFormat(
"[HGFRIENDS HANDLER]: no userID in request to notify");
243 return FailureResult();
247 if (request.ContainsKey(
"online"))
248 Boolean.TryParse(request[
"online"].ToString(), out online);
251 m_log.WarnFormat(
"[HGFRIENDS HANDLER]: no online in request to notify");
252 return FailureResult();
255 List<string> friends =
new List<string>();
257 foreach (KeyValuePair<string, object> kvp
in request)
259 if (kvp.Key.Equals(
"friend_" + i.ToString()))
261 friends.Add(kvp.Value.ToString());
266 List<UUID> onlineFriends = m_TheService.StatusNotification(friends, principalID, online);
268 Dictionary<string, object> result =
new Dictionary<string, object>();
269 if ((onlineFriends == null) || ((onlineFriends != null) && (onlineFriends.Count == 0)))
270 result[
"RESULT"] =
"NULL";
274 foreach (UUID f
in onlineFriends)
276 result[
"friend_" + i] = f.ToString();
281 string xmlString = ServerUtils.BuildXmlResponse(result);
284 return Util.UTF8NoBomEncoding.GetBytes(xmlString);
291 private bool VerifyServiceKey(Dictionary<string, object> request)
293 if (!request.ContainsKey(
"KEY") || !request.ContainsKey(
"SESSIONID"))
295 m_log.WarnFormat(
"[HGFRIENDS HANDLER]: ignoring request without Key or SessionID");
299 if (request[
"KEY"] == null || request[
"SESSIONID"] == null)
302 string serviceKey = request[
"KEY"].ToString();
303 string sessionStr = request[
"SESSIONID"].ToString();
306 if (!
UUID.TryParse(sessionStr, out sessionID) || serviceKey == string.Empty)
309 if (!m_UserAgentService.VerifyAgent(sessionID, serviceKey))
311 m_log.WarnFormat(
"[HGFRIENDS HANDLER]: Key {0} for session {1} did not match existing key. Ignoring request", serviceKey, sessionID);
315 m_log.DebugFormat(
"[HGFRIENDS HANDLER]: Verification ok");
319 private byte[] SuccessResult()
321 XmlDocument doc =
new XmlDocument();
323 XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
326 doc.AppendChild(xmlnode);
328 XmlElement rootElement = doc.CreateElement(
"",
"ServerResponse",
331 doc.AppendChild(rootElement);
333 XmlElement result = doc.CreateElement(
"",
"Result",
"");
334 result.AppendChild(doc.CreateTextNode(
"Success"));
336 rootElement.AppendChild(result);
338 return Util.DocToBytes(doc);
341 private byte[] SuccessResult(
string value)
343 XmlDocument doc =
new XmlDocument();
345 XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
348 doc.AppendChild(xmlnode);
350 XmlElement rootElement = doc.CreateElement(
"",
"ServerResponse",
353 doc.AppendChild(rootElement);
355 XmlElement result = doc.CreateElement(
"",
"RESULT",
"");
356 result.AppendChild(doc.CreateTextNode(
"Success"));
358 rootElement.AppendChild(result);
360 XmlElement message = doc.CreateElement(
"",
"Value",
"");
361 message.AppendChild(doc.CreateTextNode(value));
363 rootElement.AppendChild(message);
365 return Util.DocToBytes(doc);
369 private byte[] FailureResult()
371 return FailureResult(
String.Empty);
374 private byte[] FailureResult(
string msg)
376 XmlDocument doc =
new XmlDocument();
378 XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
381 doc.AppendChild(xmlnode);
383 XmlElement rootElement = doc.CreateElement(
"",
"ServerResponse",
386 doc.AppendChild(rootElement);
388 XmlElement result = doc.CreateElement(
"",
"RESULT",
"");
389 result.AppendChild(doc.CreateTextNode(
"Failure"));
391 rootElement.AppendChild(result);
393 XmlElement message = doc.CreateElement(
"",
"Message",
"");
394 message.AppendChild(doc.CreateTextNode(msg));
396 rootElement.AppendChild(message);
398 return Util.DocToBytes(doc);
401 private byte[] BoolResult(
bool value)
403 XmlDocument doc =
new XmlDocument();
405 XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
408 doc.AppendChild(xmlnode);
410 XmlElement rootElement = doc.CreateElement(
"",
"ServerResponse",
413 doc.AppendChild(rootElement);
415 XmlElement result = doc.CreateElement(
"",
"RESULT",
"");
416 result.AppendChild(doc.CreateTextNode(value.ToString()));
418 rootElement.AppendChild(result);
420 return Util.DocToBytes(doc);
override byte[] ProcessRequest(string path, Stream requestData, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
Base streamed request handler.
HGFriendsServerPostHandler(IHGFriendsService service, IUserAgentService uas, IFriendsSimConnector friendsConn)
OpenSim.Services.Interfaces.FriendInfo FriendInfo
OpenSim.Services.Interfaces.GridRegion GridRegion