OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
InstantMessageModule.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 using System;
28 using System.Collections.Generic;
29 using System.Reflection;
30 using System.Timers;
31 using log4net;
32 using Mono.Addins;
33 using Nini.Config;
34 using OpenMetaverse;
35 using OpenSim.Framework;
36 using OpenSim.Framework.Client;
37 using OpenSim.Region.Framework.Interfaces;
38 using OpenSim.Region.Framework.Scenes;
39 
40 namespace OpenSim.Region.CoreModules.Avatar.InstantMessage
41 {
42  [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "InstantMessageModule")]
44  {
45  private static readonly ILog m_log = LogManager.GetLogger(
46  MethodBase.GetCurrentMethod().DeclaringType);
47 
48  private Timer m_logTimer = new Timer(10000);
49  private List<GridInstantMessage> m_logData = new List<GridInstantMessage>();
50  private string m_restUrl;
51 
55  private bool m_enabled = false;
56 
57  private readonly List<Scene> m_scenes = new List<Scene>();
58 
59  #region Region Module interface
60 
61  private IMessageTransferModule m_TransferModule = null;
62 
63  public void Initialise(IConfigSource config)
64  {
65  if (config.Configs["Messaging"] != null)
66  {
67  if (config.Configs["Messaging"].GetString(
68  "InstantMessageModule", "InstantMessageModule") !=
69  "InstantMessageModule")
70  return;
71  m_restUrl = config.Configs["Messaging"].GetString("LogURL", String.Empty);
72  }
73 
74  m_enabled = true;
75  m_logTimer.AutoReset = false;
76  m_logTimer.Elapsed += LogTimerElapsed;
77  }
78 
79  public void AddRegion(Scene scene)
80  {
81  if (!m_enabled)
82  return;
83 
84  lock (m_scenes)
85  {
86  if (!m_scenes.Contains(scene))
87  {
88  m_scenes.Add(scene);
89  scene.EventManager.OnClientConnect += OnClientConnect;
90  scene.EventManager.OnIncomingInstantMessage += OnGridInstantMessage;
91  }
92  }
93  }
94 
95  public void RegionLoaded(Scene scene)
96  {
97  if (!m_enabled)
98  return;
99 
100  if (m_TransferModule == null)
101  {
102  m_TransferModule =
103  scene.RequestModuleInterface<IMessageTransferModule>();
104 
105  if (m_TransferModule == null)
106  {
107  m_log.Error("[INSTANT MESSAGE]: No message transfer module, IM will not work!");
108  scene.EventManager.OnClientConnect -= OnClientConnect;
109  scene.EventManager.OnIncomingInstantMessage -= OnGridInstantMessage;
110 
111  m_scenes.Clear();
112  m_enabled = false;
113  }
114  }
115  }
116 
117  public void RemoveRegion(Scene scene)
118  {
119  if (!m_enabled)
120  return;
121 
122  lock (m_scenes)
123  {
124  m_scenes.Remove(scene);
125  }
126  }
127 
128  void OnClientConnect(IClientCore client)
129  {
130  IClientIM clientIM;
131  if (client.TryGet(out clientIM))
132  {
133  clientIM.OnInstantMessage += OnInstantMessage;
134  }
135  }
136 
137  public void PostInitialise()
138  {
139  }
140 
141  public void Close()
142  {
143  }
144 
145  public string Name
146  {
147  get { return "InstantMessageModule"; }
148  }
149 
150  public Type ReplaceableInterface
151  {
152  get { return null; }
153  }
154 
155  #endregion
156 
158  {
159  byte dialog = im.dialog;
160 
161  if (client != null && dialog == (byte)InstantMessageDialog.MessageFromAgent)
162  LogInstantMesssage(im);
163 
164  if (dialog != (byte)InstantMessageDialog.MessageFromAgent
165  && dialog != (byte)InstantMessageDialog.StartTyping
166  && dialog != (byte)InstantMessageDialog.StopTyping
167  && dialog != (byte)InstantMessageDialog.BusyAutoResponse
168  && dialog != (byte)InstantMessageDialog.MessageFromObject)
169  {
170  return;
171  }
172 
173  //DateTime dt = DateTime.UtcNow;
174 
175  // Ticks from UtcNow, but make it look like local. Evil, huh?
176  //dt = DateTime.SpecifyKind(dt, DateTimeKind.Local);
177 
178  //try
179  //{
180  // // Convert that to the PST timezone
181  // TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("America/Los_Angeles");
182  // dt = TimeZoneInfo.ConvertTime(dt, timeZoneInfo);
183  //}
184  //catch
185  //{
186  // //m_log.Info("[OFFLINE MESSAGING]: No PST timezone found on this machine. Saving with local timestamp.");
187  //}
188 
190  //dt = DateTime.SpecifyKind(dt, DateTimeKind.Utc);
191 
192  // If client is null, this message comes from storage and IS offline
193  if (client != null)
194  im.offline = 0;
195 
196  if (im.offline == 0)
197  im.timestamp = (uint)Util.UnixTimeSinceEpoch();
198 
199  if (m_TransferModule != null)
200  {
201  if (client != null)
202  im.fromAgentName = client.FirstName + " " + client.LastName;
203  m_TransferModule.SendInstantMessage(im,
204  delegate(bool success)
205  {
206  if (dialog == (uint)InstantMessageDialog.StartTyping ||
207  dialog == (uint)InstantMessageDialog.StopTyping ||
208  dialog == (uint)InstantMessageDialog.MessageFromObject)
209  {
210  return;
211  }
212 
213  if ((client != null) && !success)
214  {
215  client.SendInstantMessage(
216  new GridInstantMessage(
217  null, new UUID(im.fromAgentID), "System",
218  new UUID(im.toAgentID),
219  (byte)InstantMessageDialog.BusyAutoResponse,
220  "Unable to send instant message. "+
221  "User is not logged in.", false,
222  new Vector3()));
223  }
224  }
225  );
226  }
227  }
228 
233  private void OnGridInstantMessage(GridInstantMessage msg)
234  {
235  // Just call the Text IM handler above
236  // This event won't be raised unless we have that agent,
237  // so we can depend on the above not trying to send
238  // via grid again
239  //
240  OnInstantMessage(null, msg);
241  }
242 
243  private void LogInstantMesssage(GridInstantMessage im)
244  {
245  if (m_logData.Count < 20)
246  {
247  // Restart the log write timer
248  m_logTimer.Stop();
249  }
250  if (!m_logTimer.Enabled)
251  m_logTimer.Start();
252 
253  lock (m_logData)
254  {
255  m_logData.Add(im);
256  }
257  }
258 
259  private void LogTimerElapsed(object source, ElapsedEventArgs e)
260  {
261  lock (m_logData)
262  {
263  if (m_restUrl != String.Empty && m_logData.Count > 0)
264  {
265  bool success = SynchronousRestObjectRequester.MakeRequest<List<GridInstantMessage>, bool>("POST", m_restUrl + "/LogMessages/", m_logData);
266  if (!success)
267  m_log.ErrorFormat("[INSTANT MESSAGE]: Failed to save log data");
268  }
269  m_logData.Clear();
270  }
271  }
272  }
273 }
void Close()
This is the inverse to Initialise. After a Close(), this instance won't be usable anymore...
System.Timers.Timer Timer
void AddRegion(Scene scene)
This is called whenever a Scene is added. For shared modules, this can happen several times...
void PostInitialise()
This is called exactly once after all the shared region-modules have been instanciated and IRegionMod...
void RemoveRegion(Scene scene)
This is called whenever a Scene is removed. For shared modules, this can happen several times...
void Initialise(IConfigSource config)
This is called to initialize the region module. For shared modules, this is called exactly once...
void RegionLoaded(Scene scene)
This will be called once for every scene loaded. In a shared module this will be multiple times in on...