InstantMessageServiceConnector.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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;
  29. using System.Reflection;
  30. using OpenMetaverse;
  31. using Nwc.XmlRpc;
  32. using log4net;
  33. using OpenSim.Framework;
  34. using System.Net.Http;
  35. namespace OpenSim.Services.Connectors.InstantMessage
  36. {
  37. public class InstantMessageServiceConnector
  38. {
  39. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  40. /// <summary>
  41. /// This actually does the XMLRPC Request
  42. /// </summary>
  43. /// <param name="url">URL we pull the data out of to send the request to</param>
  44. /// <param name="im">The Instant Message </param>
  45. /// <returns>Bool if the message was successfully delivered at the other side.</returns>
  46. public static bool SendInstantMessage(string url, GridInstantMessage im, string messageKey)
  47. {
  48. Hashtable xmlrpcdata = ConvertGridInstantMessageToXMLRPC(im, messageKey);
  49. xmlrpcdata["region_handle"] = 0;
  50. XmlRpcRequest GridReq = new("grid_instant_message", new ArrayList { xmlrpcdata });
  51. try
  52. {
  53. using HttpClient hclient = WebUtil.GetNewGlobalHttpClient(10000);
  54. XmlRpcResponse GridResp = GridReq.Send(url, hclient);
  55. Hashtable responseData = (Hashtable)GridResp.Value;
  56. if (responseData.ContainsKey("success"))
  57. {
  58. return ((string)responseData["success"] == "TRUE");
  59. }
  60. else
  61. {
  62. m_log.DebugFormat("[GRID INSTANT MESSAGE]: No response from {0}", url);
  63. return false;
  64. }
  65. }
  66. catch (Exception e)
  67. {
  68. m_log.ErrorFormat("[GRID INSTANT MESSAGE]: Error sending message to {0} : {1}", url, e.Message);
  69. }
  70. return false;
  71. }
  72. /// <summary>
  73. /// Takes a GridInstantMessage and converts it into a Hashtable for XMLRPC
  74. /// </summary>
  75. /// <param name="msg">The GridInstantMessage object</param>
  76. /// <returns>Hashtable containing the XMLRPC request</returns>
  77. protected static Hashtable ConvertGridInstantMessageToXMLRPC(GridInstantMessage msg, string messageKey)
  78. {
  79. Hashtable gim = new()
  80. {
  81. ["from_agent_id"] = msg.fromAgentID.ToString(),
  82. // Kept for compatibility
  83. ["from_agent_session"] = UUID.Zero.ToString(),
  84. ["to_agent_id"] = msg.toAgentID.ToString(),
  85. ["im_session_id"] = msg.imSessionID.ToString(),
  86. ["timestamp"] = msg.timestamp.ToString(),
  87. ["from_agent_name"] = msg.fromAgentName,
  88. ["message"] = msg.message,
  89. ["from_group"] = msg.fromGroup ? "TRUE" : "FALSE",
  90. ["parent_estate_id"] = msg.ParentEstateID.ToString(),
  91. ["position_x"] = msg.Position.X.ToString(),
  92. ["position_y"] = msg.Position.Y.ToString(),
  93. ["position_z"] = msg.Position.Z.ToString(),
  94. ["region_id"] = msg.RegionID.ToString(),
  95. ["binary_bucket"] = Convert.ToBase64String(msg.binaryBucket, Base64FormattingOptions.None),
  96. ["region_id"] = new UUID(msg.RegionID).ToString(),
  97. ["dialog"] = Convert.ToBase64String(new byte[] { msg.dialog }, Base64FormattingOptions.None),
  98. ["offline"] = Convert.ToBase64String(new byte[] { msg.offline }, Base64FormattingOptions.None)
  99. };
  100. if (!string.IsNullOrEmpty(messageKey))
  101. gim["message_key"] = messageKey;
  102. return gim;
  103. }
  104. }
  105. }