InstantMessageServerConnector.cs 10 KB

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