FriendsRequestHandler.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 is null || m_FriendsModule.Scene is 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. object tmpo;
  125. if (!request.TryGetValue("FromID", out tmpo) || !UUID.TryParse(tmpo.ToString(), out UUID fromID))
  126. return FailureResult();
  127. if (!request.TryGetValue("ToID", out tmpo) || !UUID.TryParse(tmpo.ToString(), out UUID toID))
  128. return FailureResult();
  129. string message = string.Empty;
  130. if (request.TryGetValue("Message", out tmpo))
  131. message = tmpo.ToString();
  132. UserAccount account = m_FriendsModule.UserAccountService.GetUserAccount(UUID.Zero, fromID);
  133. string name = (account == null) ? "Unknown" : account.FirstName + " " + account.LastName;
  134. GridInstantMessage im = new GridInstantMessage(m_FriendsModule.Scene, fromID, name, toID,
  135. (byte)InstantMessageDialog.FriendshipOffered, message, false, Vector3.Zero);
  136. // !! HACK
  137. im.imSessionID = im.fromAgentID;
  138. if (m_FriendsModule.LocalFriendshipOffered(toID, im))
  139. return SuccessResult();
  140. return FailureResult();
  141. }
  142. byte[] FriendshipApproved(Dictionary<string, object> request)
  143. {
  144. object tmpo;
  145. if (!request.TryGetValue("FromID", out tmpo) || !UUID.TryParse(tmpo.ToString(), out UUID fromID))
  146. return FailureResult();
  147. if (!request.TryGetValue("ToID", out tmpo) || !UUID.TryParse(tmpo.ToString(), out UUID toID))
  148. return FailureResult();
  149. string fromName = string.Empty;
  150. if (request.TryGetValue("FromName", out tmpo))
  151. fromName = tmpo.ToString();
  152. if (m_FriendsModule.LocalFriendshipApproved(fromID, fromName, toID))
  153. return SuccessResult();
  154. return FailureResult();
  155. }
  156. byte[] FriendshipDenied(Dictionary<string, object> request)
  157. {
  158. object tmpo;
  159. string fromName = string.Empty;
  160. if (!request.TryGetValue("FromID", out tmpo) || !UUID.TryParse(tmpo.ToString(), out UUID fromID))
  161. return FailureResult();
  162. if (!request.TryGetValue("ToID", out tmpo) || !UUID.TryParse(tmpo.ToString(), out UUID toID))
  163. return FailureResult();
  164. if (request.TryGetValue("FromName", out tmpo))
  165. fromName = tmpo.ToString();
  166. if (m_FriendsModule.LocalFriendshipDenied(fromID, fromName, toID))
  167. return SuccessResult();
  168. return FailureResult();
  169. }
  170. byte[] FriendshipTerminated(Dictionary<string, object> request)
  171. {
  172. object tmpo;
  173. if (!request.TryGetValue("FromID", out tmpo) || !UUID.TryParse(tmpo.ToString(), out UUID fromID))
  174. return FailureResult();
  175. if (!request.TryGetValue("ToID", out tmpo) || !UUID.TryParse(tmpo.ToString(), out UUID toID))
  176. return FailureResult();
  177. if (m_FriendsModule.LocalFriendshipTerminated(fromID, toID))
  178. return SuccessResult();
  179. return FailureResult();
  180. }
  181. byte[] GrantRights(Dictionary<string, object> request)
  182. {
  183. object tmpo;
  184. if (!request.TryGetValue("FromID", out tmpo) || !UUID.TryParse(tmpo.ToString(), out UUID fromID))
  185. return FailureResult();
  186. if (!request.TryGetValue("ToID", out tmpo) || !UUID.TryParse(tmpo.ToString(), out UUID toID))
  187. return FailureResult();
  188. if (!request.TryGetValue("UserFlags", out tmpo) || !Int32.TryParse(tmpo.ToString(), out int oldRights))
  189. return FailureResult();
  190. if (!request.TryGetValue("Rights", out tmpo) || !Int32.TryParse(tmpo.ToString(), out int newRights))
  191. return FailureResult();
  192. if (m_FriendsModule.LocalGrantRights(fromID, toID, oldRights, newRights))
  193. return SuccessResult();
  194. return FailureResult();
  195. }
  196. byte[] StatusNotification(Dictionary<string, object> request)
  197. {
  198. object tmpo;
  199. if (!request.TryGetValue("FromID", out tmpo) || !UUID.TryParse(tmpo.ToString(), out UUID fromID))
  200. return FailureResult();
  201. if (!request.TryGetValue("ToID", out tmpo) || !UUID.TryParse(tmpo.ToString(), out UUID toID))
  202. return FailureResult();
  203. if (!request.TryGetValue("Online", out tmpo) || !Boolean.TryParse(tmpo.ToString(), out bool online))
  204. return FailureResult();
  205. if (m_FriendsModule.LocalStatusNotification(fromID, toID, online))
  206. return SuccessResult();
  207. return FailureResult();
  208. }
  209. #region Misc
  210. private byte[] FailureResult()
  211. {
  212. return BoolResult(false);
  213. }
  214. private byte[] SuccessResult()
  215. {
  216. return BoolResult(true);
  217. }
  218. private byte[] BoolResult(bool value)
  219. {
  220. XmlDocument doc = new XmlDocument();
  221. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  222. "", "");
  223. doc.AppendChild(xmlnode);
  224. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  225. "");
  226. doc.AppendChild(rootElement);
  227. XmlElement result = doc.CreateElement("", "RESULT", "");
  228. result.AppendChild(doc.CreateTextNode(value.ToString()));
  229. rootElement.AppendChild(result);
  230. return Util.DocToBytes(doc);
  231. }
  232. #endregion
  233. }
  234. }