FriendsRequestHandler.cs 12 KB

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