1
0

InstantMessageServerConnector.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Server.Base;
  35. using OpenSim.Services.Interfaces;
  36. using OpenSim.Framework.Servers.HttpServer;
  37. using OpenSim.Server.Handlers.Base;
  38. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  39. using log4net;
  40. using Nwc.XmlRpc;
  41. using OpenMetaverse;
  42. namespace OpenSim.Server.Handlers.Hypergrid
  43. {
  44. public class InstantMessageServerConnector : ServiceConnector
  45. {
  46. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. private IInstantMessage m_IMService;
  48. public InstantMessageServerConnector(IConfigSource config, IHttpServer server) :
  49. this(config, server, (IInstantMessageSimConnector)null)
  50. {
  51. }
  52. public InstantMessageServerConnector(IConfigSource config, IHttpServer server, string configName) :
  53. this(config, server)
  54. {
  55. }
  56. public InstantMessageServerConnector(IConfigSource config, IHttpServer server, IInstantMessageSimConnector simConnector) :
  57. base(config, server, String.Empty)
  58. {
  59. IConfig gridConfig = config.Configs["HGInstantMessageService"];
  60. if (gridConfig != null)
  61. {
  62. string serviceDll = gridConfig.GetString("LocalServiceModule", string.Empty);
  63. Object[] args = new Object[] { config, simConnector };
  64. m_IMService = ServerUtils.LoadPlugin<IInstantMessage>(serviceDll, args);
  65. }
  66. if (m_IMService == null)
  67. throw new Exception("InstantMessage server connector cannot proceed because of missing service");
  68. server.AddXmlRPCHandler("grid_instant_message", ProcessInstantMessage, false);
  69. }
  70. public IInstantMessage GetService()
  71. {
  72. return m_IMService;
  73. }
  74. protected virtual XmlRpcResponse ProcessInstantMessage(XmlRpcRequest request, IPEndPoint remoteClient)
  75. {
  76. bool successful = false;
  77. try
  78. {
  79. // various rational defaults
  80. UUID fromAgentID = UUID.Zero;
  81. UUID toAgentID = UUID.Zero;
  82. UUID imSessionID = UUID.Zero;
  83. uint timestamp = 0;
  84. string fromAgentName = "";
  85. string message = "";
  86. byte dialog = (byte)0;
  87. bool fromGroup = false;
  88. byte offline = (byte)0;
  89. uint ParentEstateID = 0;
  90. Vector3 Position = Vector3.Zero;
  91. UUID RegionID = UUID.Zero;
  92. byte[] binaryBucket = Array.Empty<byte>();
  93. float pos_x = 0;
  94. float pos_y = 0;
  95. float pos_z = 0;
  96. //m_log.Info("Processing IM");
  97. Hashtable requestData = (Hashtable)request.Params[0];
  98. // Check if it's got all the data
  99. if (requestData.ContainsKey("from_agent_id")
  100. && requestData.ContainsKey("to_agent_id") && requestData.ContainsKey("im_session_id")
  101. && requestData.ContainsKey("timestamp") && requestData.ContainsKey("from_agent_name")
  102. && requestData.ContainsKey("message") && requestData.ContainsKey("dialog")
  103. && requestData.ContainsKey("from_group")
  104. && requestData.ContainsKey("offline") && requestData.ContainsKey("parent_estate_id")
  105. && requestData.ContainsKey("position_x") && requestData.ContainsKey("position_y")
  106. && requestData.ContainsKey("position_z") && requestData.ContainsKey("region_id")
  107. && requestData.ContainsKey("binary_bucket"))
  108. {
  109. // Do the easy way of validating the UUIDs
  110. UUID.TryParse((string)requestData["from_agent_id"], out fromAgentID);
  111. UUID.TryParse((string)requestData["to_agent_id"], out toAgentID);
  112. UUID.TryParse((string)requestData["im_session_id"], out imSessionID);
  113. UUID.TryParse((string)requestData["region_id"], out RegionID);
  114. try
  115. {
  116. timestamp = (uint)Convert.ToInt32((string)requestData["timestamp"]);
  117. }
  118. catch (ArgumentException)
  119. {
  120. }
  121. catch (FormatException)
  122. {
  123. }
  124. catch (OverflowException)
  125. {
  126. }
  127. fromAgentName = (string)requestData["from_agent_name"];
  128. message = (string)requestData["message"];
  129. if (message == null)
  130. message = string.Empty;
  131. // Bytes don't transfer well over XMLRPC, so, we Base64 Encode them.
  132. string requestData1 = (string)requestData["dialog"];
  133. if (string.IsNullOrEmpty(requestData1))
  134. {
  135. dialog = 0;
  136. }
  137. else
  138. {
  139. byte[] dialogdata = Convert.FromBase64String(requestData1);
  140. dialog = dialogdata[0];
  141. }
  142. if ((string)requestData["from_group"] == "TRUE")
  143. fromGroup = true;
  144. string requestData2 = (string)requestData["offline"];
  145. if (String.IsNullOrEmpty(requestData2))
  146. {
  147. offline = 0;
  148. }
  149. else
  150. {
  151. byte[] offlinedata = Convert.FromBase64String(requestData2);
  152. offline = offlinedata[0];
  153. }
  154. try
  155. {
  156. ParentEstateID = (uint)Convert.ToInt32((string)requestData["parent_estate_id"]);
  157. }
  158. catch (ArgumentException)
  159. {
  160. }
  161. catch (FormatException)
  162. {
  163. }
  164. catch (OverflowException)
  165. {
  166. }
  167. float.TryParse((string)requestData["position_x"], out pos_x);
  168. float.TryParse((string)requestData["position_y"], out pos_y);
  169. float.TryParse((string)requestData["position_z"], out pos_z);
  170. Position = new Vector3(pos_x, pos_y, pos_z);
  171. string requestData3 = (string)requestData["binary_bucket"];
  172. if (string.IsNullOrEmpty(requestData3))
  173. {
  174. binaryBucket = Array.Empty<byte>();
  175. }
  176. else
  177. {
  178. binaryBucket = Convert.FromBase64String(requestData3);
  179. }
  180. // Create a New GridInstantMessageObject the the data
  181. GridInstantMessage gim = new GridInstantMessage();
  182. gim.fromAgentID = fromAgentID.Guid;
  183. gim.fromAgentName = fromAgentName;
  184. gim.fromGroup = fromGroup;
  185. gim.imSessionID = imSessionID.Guid;
  186. gim.RegionID = RegionID.Guid;
  187. gim.timestamp = timestamp;
  188. gim.toAgentID = toAgentID.Guid;
  189. gim.message = message;
  190. gim.dialog = dialog;
  191. gim.offline = offline;
  192. gim.ParentEstateID = ParentEstateID;
  193. gim.Position = Position;
  194. gim.binaryBucket = binaryBucket;
  195. successful = m_IMService.IncomingInstantMessage(gim);
  196. }
  197. }
  198. catch (Exception e)
  199. {
  200. m_log.Error("[INSTANT MESSAGE]: Caught unexpected exception:", e);
  201. successful = false;
  202. }
  203. //Send response back to region calling if it was successful
  204. // calling region uses this to know when to look up a user's location again.
  205. XmlRpcResponse resp = new XmlRpcResponse();
  206. Hashtable respdata = new Hashtable();
  207. respdata["success"] = successful ? "TRUE" : "FALSE";
  208. resp.Value = respdata;
  209. return resp;
  210. }
  211. }
  212. }