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