1
0

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