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