InstantMessageServiceConnector.cs 5.5 KB

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