clockwerk-opensim – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 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.Generic;
30 using System.Reflection;
31 using log4net;
32 using Nini.Config;
33 using Mono.Addins;
34 using OpenMetaverse;
35 using OpenMetaverse.StructuredData;
36 using OpenSim.Framework;
37 using OpenSim.Region.Framework.Interfaces;
38 using OpenSim.Region.Framework.Scenes;
39  
40 namespace OpenSim.Region.CoreModules.Avatar.Chat
41 {
42 [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "ChatModule")]
43 public class ChatModule : ISharedRegionModule
44 {
45 private static readonly ILog m_log =
46 LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
47  
48 private const int DEBUG_CHANNEL = 2147483647;
49  
50 private bool m_enabled = true;
51 private int m_saydistance = 20;
52 private int m_shoutdistance = 100;
53 private int m_whisperdistance = 10;
54  
55 internal object m_syncy = new object();
56  
57 internal IConfig m_config;
58  
59 #region ISharedRegionModule Members
60 public virtual void Initialise(IConfigSource config)
61 {
62 m_config = config.Configs["Chat"];
63  
64 if (null == m_config)
65 {
66 m_log.Info("[CHAT]: no config found, plugin disabled");
67 m_enabled = false;
68 return;
69 }
70  
71 if (!m_config.GetBoolean("enabled", true))
72 {
73 m_log.Info("[CHAT]: plugin disabled by configuration");
74 m_enabled = false;
75 return;
76 }
77  
78 m_whisperdistance = config.Configs["Chat"].GetInt("whisper_distance", m_whisperdistance);
79 m_saydistance = config.Configs["Chat"].GetInt("say_distance", m_saydistance);
80 m_shoutdistance = config.Configs["Chat"].GetInt("shout_distance", m_shoutdistance);
81 }
82  
83 public virtual void AddRegion(Scene scene)
84 {
85 if (!m_enabled)
86 return;
87  
88 scene.EventManager.OnNewClient += OnNewClient;
89 scene.EventManager.OnChatFromWorld += OnChatFromWorld;
90 scene.EventManager.OnChatBroadcast += OnChatBroadcast;
91  
92 m_log.InfoFormat("[CHAT]: Initialized for {0} w:{1} s:{2} S:{3}", scene.RegionInfo.RegionName,
93 m_whisperdistance, m_saydistance, m_shoutdistance);
94 }
95  
96 public virtual void RegionLoaded(Scene scene)
97 {
98 if (!m_enabled)
99 return;
100  
101 ISimulatorFeaturesModule featuresModule = scene.RequestModuleInterface<ISimulatorFeaturesModule>();
102  
103 if (featuresModule != null)
104 featuresModule.OnSimulatorFeaturesRequest += OnSimulatorFeaturesRequest;
105  
106 }
107  
108 public virtual void RemoveRegion(Scene scene)
109 {
110 if (!m_enabled)
111 return;
112  
113 scene.EventManager.OnNewClient -= OnNewClient;
114 scene.EventManager.OnChatFromWorld -= OnChatFromWorld;
115 scene.EventManager.OnChatBroadcast -= OnChatBroadcast;
116 }
117  
118 public virtual void Close()
119 {
120 }
121  
122 public virtual void PostInitialise()
123 {
124 }
125  
126 public Type ReplaceableInterface
127 {
128 get { return null; }
129 }
130  
131 public virtual string Name
132 {
133 get { return "ChatModule"; }
134 }
135  
136 #endregion
137  
138  
139 public virtual void OnNewClient(IClientAPI client)
140 {
141 client.OnChatFromClient += OnChatFromClient;
142 }
143  
144 protected OSChatMessage FixPositionOfChatMessage(OSChatMessage c)
145 {
146 ScenePresence avatar;
147 Scene scene = (Scene)c.Scene;
148 if ((avatar = scene.GetScenePresence(c.Sender.AgentId)) != null)
149 c.Position = avatar.AbsolutePosition;
150  
151 return c;
152 }
153  
154 public virtual void OnChatFromClient(Object sender, OSChatMessage c)
155 {
156 c = FixPositionOfChatMessage(c);
157  
158 // redistribute to interested subscribers
159 Scene scene = (Scene)c.Scene;
160 scene.EventManager.TriggerOnChatFromClient(sender, c);
161  
162 // early return if not on public or debug channel
163 if (c.Channel != 0 && c.Channel != DEBUG_CHANNEL) return;
164  
165 // sanity check:
166 if (c.Sender == null)
167 {
168 m_log.ErrorFormat("[CHAT]: OnChatFromClient from {0} has empty Sender field!", sender);
169 return;
170 }
171  
172 DeliverChatToAvatars(ChatSourceType.Agent, c);
173 }
174  
175 public virtual void OnChatFromWorld(Object sender, OSChatMessage c)
176 {
177 // early return if not on public or debug channel
178 if (c.Channel != 0 && c.Channel != DEBUG_CHANNEL) return;
179  
180 DeliverChatToAvatars(ChatSourceType.Object, c);
181 }
182  
183 protected virtual void DeliverChatToAvatars(ChatSourceType sourceType, OSChatMessage c)
184 {
185 string fromName = c.From;
186 UUID fromID = UUID.Zero;
187 UUID ownerID = UUID.Zero;
188 UUID targetID = c.TargetUUID;
189 string message = c.Message;
190 Scene scene = (Scene)c.Scene;
191 Vector3 fromPos = c.Position;
192 Vector3 regionPos = new Vector3(scene.RegionInfo.WorldLocX, scene.RegionInfo.WorldLocY, 0);
193  
194 if (c.Channel == DEBUG_CHANNEL) c.Type = ChatTypeEnum.DebugChannel;
195  
196 switch (sourceType)
197 {
198 case ChatSourceType.Agent:
199 ScenePresence avatar = scene.GetScenePresence(c.Sender.AgentId);
200 fromPos = avatar.AbsolutePosition;
201 fromName = avatar.Name;
202 fromID = c.Sender.AgentId;
203 ownerID = c.Sender.AgentId;
204  
205 break;
206  
207 case ChatSourceType.Object:
208 fromID = c.SenderUUID;
209  
210 if (c.SenderObject != null && c.SenderObject is SceneObjectPart)
211 ownerID = ((SceneObjectPart)c.SenderObject).OwnerID;
212  
213 break;
214 }
215  
216 // TODO: iterate over message
217 if (message.Length >= 1000) // libomv limit
218 message = message.Substring(0, 1000);
219  
220 // m_log.DebugFormat(
221 // "[CHAT]: DCTA: fromID {0} fromName {1}, region{2}, cType {3}, sType {4}, targetID {5}",
222 // fromID, fromName, scene.RegionInfo.RegionName, c.Type, sourceType, targetID);
223  
224 HashSet<UUID> receiverIDs = new HashSet<UUID>();
225  
226 if (targetID == UUID.Zero)
227 {
228 // This should use ForEachClient, but clients don't have a position.
229 // If camera is moved into client, then camera position can be used
230 scene.ForEachScenePresence(
231 delegate(ScenePresence presence)
232 {
233 if (TrySendChatMessage(
234 presence, fromPos, regionPos, fromID, ownerID, fromName, c.Type, message, sourceType, false))
235 receiverIDs.Add(presence.UUID);
236 }
237 );
238 }
239 else
240 {
241 // This is a send to a specific client eg from llRegionSayTo
242 // no need to check distance etc, jand send is as say
243 ScenePresence presence = scene.GetScenePresence(targetID);
244 if (presence != null && !presence.IsChildAgent)
245 {
246 if (TrySendChatMessage(
247 presence, fromPos, regionPos, fromID, ownerID, fromName, ChatTypeEnum.Say, message, sourceType, true))
248 receiverIDs.Add(presence.UUID);
249 }
250 }
251  
252 scene.EventManager.TriggerOnChatToClients(
253 fromID, receiverIDs, message, c.Type, fromPos, fromName, sourceType, ChatAudibleLevel.Fully);
254 }
255  
256 static private Vector3 CenterOfRegion = new Vector3(128, 128, 30);
257  
258 public virtual void OnChatBroadcast(Object sender, OSChatMessage c)
259 {
260 if (c.Channel != 0 && c.Channel != DEBUG_CHANNEL) return;
261  
262 ChatTypeEnum cType = c.Type;
263 if (c.Channel == DEBUG_CHANNEL)
264 cType = ChatTypeEnum.DebugChannel;
265  
266 if (cType == ChatTypeEnum.Region)
267 cType = ChatTypeEnum.Say;
268  
269 if (c.Message.Length > 1100)
270 c.Message = c.Message.Substring(0, 1000);
271  
272 // broadcast chat works by redistributing every incoming chat
273 // message to each avatar in the scene.
274 string fromName = c.From;
275  
276 UUID fromID = UUID.Zero;
277 ChatSourceType sourceType = ChatSourceType.Object;
278 if (null != c.Sender)
279 {
280 ScenePresence avatar = (c.Scene as Scene).GetScenePresence(c.Sender.AgentId);
281 fromID = c.Sender.AgentId;
282 fromName = avatar.Name;
283 sourceType = ChatSourceType.Agent;
284 }
285 else if (c.SenderUUID != UUID.Zero)
286 {
287 fromID = c.SenderUUID;
288 }
289  
290 // m_log.DebugFormat("[CHAT] Broadcast: fromID {0} fromName {1}, cType {2}, sType {3}", fromID, fromName, cType, sourceType);
291  
292 HashSet<UUID> receiverIDs = new HashSet<UUID>();
293  
294 ((Scene)c.Scene).ForEachRootClient(
295 delegate(IClientAPI client)
296 {
297 // don't forward SayOwner chat from objects to
298 // non-owner agents
299 if ((c.Type == ChatTypeEnum.Owner) &&
300 (null != c.SenderObject) &&
301 (((SceneObjectPart)c.SenderObject).OwnerID != client.AgentId))
302 return;
303  
304 client.SendChatMessage(
305 c.Message, (byte)cType, CenterOfRegion, fromName, fromID, fromID,
306 (byte)sourceType, (byte)ChatAudibleLevel.Fully);
307  
308 receiverIDs.Add(client.AgentId);
309 });
310  
311 (c.Scene as Scene).EventManager.TriggerOnChatToClients(
312 fromID, receiverIDs, c.Message, cType, CenterOfRegion, fromName, sourceType, ChatAudibleLevel.Fully);
313 }
314  
315 /// <summary>
316 /// Try to send a message to the given presence
317 /// </summary>
318 /// <param name="presence">The receiver</param>
319 /// <param name="fromPos"></param>
320 /// <param name="regionPos">/param>
321 /// <param name="fromAgentID"></param>
322 /// <param name='ownerID'>
323 /// Owner of the message. For at least some messages from objects, this has to be correctly filled with the owner's UUID.
324 /// This is the case for script error messages in viewer 3 since LLViewer change EXT-7762
325 /// </param>
326 /// <param name="fromName"></param>
327 /// <param name="type"></param>
328 /// <param name="message"></param>
329 /// <param name="src"></param>
330 /// <returns>true if the message was sent to the receiver, false if it was not sent due to failing a
331 /// precondition</returns>
332 protected virtual bool TrySendChatMessage(
333 ScenePresence presence, Vector3 fromPos, Vector3 regionPos,
334 UUID fromAgentID, UUID ownerID, string fromName, ChatTypeEnum type,
335 string message, ChatSourceType src, bool ignoreDistance)
336 {
337 if (presence.LifecycleState != ScenePresenceState.Running)
338 return false;
339  
340 if (!ignoreDistance)
341 {
342 Vector3 fromRegionPos = fromPos + regionPos;
343 Vector3 toRegionPos = presence.AbsolutePosition +
344 new Vector3(presence.Scene.RegionInfo.WorldLocX, presence.Scene.RegionInfo.WorldLocY, 0);
345  
346 int dis = (int)Util.GetDistanceTo(toRegionPos, fromRegionPos);
347  
348 if (type == ChatTypeEnum.Whisper && dis > m_whisperdistance ||
349 type == ChatTypeEnum.Say && dis > m_saydistance ||
350 type == ChatTypeEnum.Shout && dis > m_shoutdistance)
351 {
352 return false;
353 }
354 }
355  
356 // TODO: should change so the message is sent through the avatar rather than direct to the ClientView
357 presence.ControllingClient.SendChatMessage(
358 message, (byte) type, fromPos, fromName,
359 fromAgentID, ownerID, (byte)src, (byte)ChatAudibleLevel.Fully);
360  
361 return true;
362 }
363  
364 #region SimulatorFeaturesRequest
365  
366 static OSDInteger m_SayRange, m_WhisperRange, m_ShoutRange;
367  
368 private void OnSimulatorFeaturesRequest(UUID agentID, ref OSDMap features)
369 {
370 OSD extras = new OSDMap();
371 if (features.ContainsKey("OpenSimExtras"))
372 extras = features["OpenSimExtras"];
373 else
374 features["OpenSimExtras"] = extras;
375  
376 if (m_SayRange == null)
377 {
378 // Do this only once
379 m_SayRange = new OSDInteger(m_saydistance);
380 m_WhisperRange = new OSDInteger(m_whisperdistance);
381 m_ShoutRange = new OSDInteger(m_shoutdistance);
382 }
383  
384 ((OSDMap)extras)["say-range"] = m_SayRange;
385 ((OSDMap)extras)["whisper-range"] = m_WhisperRange;
386 ((OSDMap)extras)["shout-range"] = m_ShoutRange;
387  
388 }
389  
390 #endregion
391 }
392 }