FriendsRequestHandler.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 : BaseStreamHandler
  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")
  47. {
  48. m_FriendsModule = fmodule;
  49. }
  50. public override byte[] Handle(string path, Stream requestData,
  51. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  52. {
  53. StreamReader sr = new StreamReader(requestData);
  54. string body = sr.ReadToEnd();
  55. sr.Close();
  56. body = body.Trim();
  57. //m_log.DebugFormat("[XXX]: query String: {0}", body);
  58. try
  59. {
  60. Dictionary<string, object> request =
  61. ServerUtils.ParseQueryString(body);
  62. if (!request.ContainsKey("METHOD"))
  63. return FailureResult();
  64. string method = request["METHOD"].ToString();
  65. request.Remove("METHOD");
  66. switch (method)
  67. {
  68. case "friendship_offered":
  69. return FriendshipOffered(request);
  70. case "friendship_approved":
  71. return FriendshipApproved(request);
  72. case "friendship_denied":
  73. return FriendshipDenied(request);
  74. case "friendship_terminated":
  75. return FriendshipTerminated(request);
  76. case "grant_rights":
  77. return GrantRights(request);
  78. case "status":
  79. return StatusNotification(request);
  80. }
  81. }
  82. catch (Exception e)
  83. {
  84. m_log.Debug("[FRIENDS]: Exception {0}" + e.ToString());
  85. }
  86. return FailureResult();
  87. }
  88. byte[] FriendshipOffered(Dictionary<string, object> request)
  89. {
  90. UUID fromID = UUID.Zero;
  91. UUID toID = UUID.Zero;
  92. string message = string.Empty;
  93. if (!request.ContainsKey("FromID") || !request.ContainsKey("ToID"))
  94. return FailureResult();
  95. message = request["Message"].ToString();
  96. if (!UUID.TryParse(request["FromID"].ToString(), out fromID))
  97. return FailureResult();
  98. if (!UUID.TryParse(request["ToID"].ToString(), out toID))
  99. return FailureResult();
  100. UserAccount account = m_FriendsModule.UserAccountService.GetUserAccount(UUID.Zero, fromID);
  101. string name = (account == null) ? "Unknown" : account.FirstName + " " + account.LastName;
  102. GridInstantMessage im = new GridInstantMessage(m_FriendsModule.Scene, fromID, name, toID,
  103. (byte)InstantMessageDialog.FriendshipOffered, message, false, Vector3.Zero);
  104. // !! HACK
  105. im.imSessionID = im.fromAgentID;
  106. if (m_FriendsModule.LocalFriendshipOffered(toID, im))
  107. return SuccessResult();
  108. return FailureResult();
  109. }
  110. byte[] FriendshipApproved(Dictionary<string, object> request)
  111. {
  112. UUID fromID = UUID.Zero;
  113. UUID toID = UUID.Zero;
  114. string fromName = string.Empty;
  115. if (!request.ContainsKey("FromID") || !request.ContainsKey("ToID"))
  116. return FailureResult();
  117. if (!UUID.TryParse(request["FromID"].ToString(), out fromID))
  118. return FailureResult();
  119. if (!UUID.TryParse(request["ToID"].ToString(), out toID))
  120. return FailureResult();
  121. if (request.ContainsKey("FromName"))
  122. fromName = request["FromName"].ToString();
  123. if (m_FriendsModule.LocalFriendshipApproved(fromID, fromName, toID))
  124. return SuccessResult();
  125. return FailureResult();
  126. }
  127. byte[] FriendshipDenied(Dictionary<string, object> request)
  128. {
  129. UUID fromID = UUID.Zero;
  130. UUID toID = UUID.Zero;
  131. string fromName = string.Empty;
  132. if (!request.ContainsKey("FromID") || !request.ContainsKey("ToID"))
  133. return FailureResult();
  134. if (!UUID.TryParse(request["FromID"].ToString(), out fromID))
  135. return FailureResult();
  136. if (!UUID.TryParse(request["ToID"].ToString(), out toID))
  137. return FailureResult();
  138. if (request.ContainsKey("FromName"))
  139. fromName = request["FromName"].ToString();
  140. if (m_FriendsModule.LocalFriendshipDenied(fromID, fromName, toID))
  141. return SuccessResult();
  142. return FailureResult();
  143. }
  144. byte[] FriendshipTerminated(Dictionary<string, object> request)
  145. {
  146. UUID fromID = UUID.Zero;
  147. UUID toID = UUID.Zero;
  148. if (!request.ContainsKey("FromID") || !request.ContainsKey("ToID"))
  149. return FailureResult();
  150. if (!UUID.TryParse(request["FromID"].ToString(), out fromID))
  151. return FailureResult();
  152. if (!UUID.TryParse(request["ToID"].ToString(), out toID))
  153. return FailureResult();
  154. if (m_FriendsModule.LocalFriendshipTerminated(toID))
  155. return SuccessResult();
  156. return FailureResult();
  157. }
  158. byte[] GrantRights(Dictionary<string, object> request)
  159. {
  160. UUID fromID = UUID.Zero;
  161. UUID toID = UUID.Zero;
  162. int rights = 0, userFlags = 0;
  163. if (!request.ContainsKey("FromID") || !request.ContainsKey("ToID"))
  164. return FailureResult();
  165. if (!UUID.TryParse(request["FromID"].ToString(), out fromID))
  166. return FailureResult();
  167. if (!UUID.TryParse(request["ToID"].ToString(), out toID))
  168. return FailureResult();
  169. if (!Int32.TryParse(request["UserFlags"].ToString(), out userFlags))
  170. return FailureResult();
  171. if (!Int32.TryParse(request["Rights"].ToString(), out rights))
  172. return FailureResult();
  173. if (m_FriendsModule.LocalGrantRights(UUID.Zero, UUID.Zero, userFlags, rights))
  174. return SuccessResult();
  175. return FailureResult();
  176. }
  177. byte[] StatusNotification(Dictionary<string, object> request)
  178. {
  179. UUID fromID = UUID.Zero;
  180. UUID toID = UUID.Zero;
  181. bool online = false;
  182. if (!request.ContainsKey("FromID") || !request.ContainsKey("ToID") || !request.ContainsKey("Online"))
  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 (!Boolean.TryParse(request["Online"].ToString(), out online))
  189. return FailureResult();
  190. if (m_FriendsModule.LocalStatusNotification(fromID, toID, online))
  191. return SuccessResult();
  192. return FailureResult();
  193. }
  194. #region Misc
  195. private byte[] FailureResult()
  196. {
  197. return BoolResult(false);
  198. }
  199. private byte[] SuccessResult()
  200. {
  201. return BoolResult(true);
  202. }
  203. private byte[] BoolResult(bool value)
  204. {
  205. XmlDocument doc = new XmlDocument();
  206. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  207. "", "");
  208. doc.AppendChild(xmlnode);
  209. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  210. "");
  211. doc.AppendChild(rootElement);
  212. XmlElement result = doc.CreateElement("", "RESULT", "");
  213. result.AppendChild(doc.CreateTextNode(value.ToString()));
  214. rootElement.AppendChild(result);
  215. return DocToBytes(doc);
  216. }
  217. private byte[] DocToBytes(XmlDocument doc)
  218. {
  219. MemoryStream ms = new MemoryStream();
  220. XmlTextWriter xw = new XmlTextWriter(ms, null);
  221. xw.Formatting = Formatting.Indented;
  222. doc.WriteTo(xw);
  223. xw.Flush();
  224. return ms.ToArray();
  225. }
  226. #endregion
  227. }
  228. }