2 // Radegast Metaverse Client
3 // Copyright (c) 2009-2011, Radegast Development Team
4 // All rights reserved.
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 // * Neither the name of the application "Radegast", nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 using System.Collections.Generic;
35 using Radegast.Netcom;
37 using OpenMetaverse.StructuredData;
41 public class ChatTextManager : IDisposable
43 private RadegastInstance instance;
44 private RadegastNetcom netcom { get { return instance.Netcom; } }
45 private GridClient client { get { return instance.Client; } }
46 private ITextPrinter textPrinter;
48 private List<ChatBufferItem> textBuffer;
50 private bool showTimestamps;
52 public ChatTextManager(RadegastInstance instance, ITextPrinter textPrinter)
54 this.textPrinter = textPrinter;
55 this.textBuffer = new List<ChatBufferItem>();
57 this.instance = instance;
61 netcom.ChatReceived += new EventHandler<ChatEventArgs>(netcom_ChatReceived);
62 netcom.ChatSent += new EventHandler<ChatSentEventArgs>(netcom_ChatSent);
63 netcom.AlertMessageReceived += new EventHandler<AlertMessageEventArgs>(netcom_AlertMessageReceived);
68 netcom.ChatReceived -= new EventHandler<ChatEventArgs>(netcom_ChatReceived);
69 netcom.ChatSent -= new EventHandler<ChatSentEventArgs>(netcom_ChatSent);
70 netcom.AlertMessageReceived -= new EventHandler<AlertMessageEventArgs>(netcom_AlertMessageReceived);
73 private void InitializeConfig()
75 Settings s = instance.GlobalSettings;
77 if (s["chat_timestamps"].Type == OSDType.Unknown)
78 s["chat_timestamps"] = OSD.FromBoolean(true);
80 showTimestamps = s["chat_timestamps"].AsBoolean();
82 s.OnSettingChanged += new Settings.SettingChangedCallback(s_OnSettingChanged);
85 void s_OnSettingChanged(object sender, SettingsEventArgs e)
87 if (e.Key == "chat_timestamps" && e.Value != null)
89 showTimestamps = e.Value.AsBoolean();
94 private void netcom_ChatSent(object sender, ChatSentEventArgs e)
96 if (e.Channel == 0) return;
98 ProcessOutgoingChat(e);
101 private void netcom_AlertMessageReceived(object sender, AlertMessageEventArgs e)
103 if (e.Message.ToLower().Contains("autopilot canceled")) return; //workaround the stupid autopilot alerts
105 ChatBufferItem item = new ChatBufferItem(
106 DateTime.Now, "Alert message: " + e.Message, ChatBufferTextStyle.Alert);
108 ProcessBufferItem(item, true);
111 private void netcom_ChatReceived(object sender, ChatEventArgs e)
113 ProcessIncomingChat(e);
116 public void PrintStartupMessage()
118 ChatBufferItem title = new ChatBufferItem(
119 DateTime.Now, Properties.Resources.RadegastTitle + "." + RadegastBuild.CurrentRev, ChatBufferTextStyle.StartupTitle);
121 ChatBufferItem ready = new ChatBufferItem(
122 DateTime.Now, "Ready.", ChatBufferTextStyle.StatusBlue);
124 ProcessBufferItem(title, true);
125 ProcessBufferItem(ready, true);
128 private Object SyncChat = new Object();
130 public void ProcessBufferItem(ChatBufferItem item, bool addToBuffer)
134 instance.LogClientMessage("chat.txt", item.Text);
135 if (addToBuffer) textBuffer.Add(item);
139 textPrinter.ForeColor = SystemColors.GrayText;
140 textPrinter.PrintText(item.Timestamp.ToString("[HH:mm] "));
145 case ChatBufferTextStyle.Normal:
146 textPrinter.ForeColor = SystemColors.WindowText;
149 case ChatBufferTextStyle.StatusBlue:
150 textPrinter.ForeColor = Color.Blue;
153 case ChatBufferTextStyle.StatusDarkBlue:
154 textPrinter.ForeColor = Color.DarkBlue;
157 case ChatBufferTextStyle.LindenChat:
158 textPrinter.ForeColor = Color.DarkGreen;
161 case ChatBufferTextStyle.ObjectChat:
162 textPrinter.ForeColor = Color.DarkCyan;
165 case ChatBufferTextStyle.StartupTitle:
166 textPrinter.ForeColor = SystemColors.WindowText;
167 textPrinter.Font = new Font(textPrinter.Font, FontStyle.Bold);
170 case ChatBufferTextStyle.Alert:
171 textPrinter.ForeColor = Color.DarkRed;
174 case ChatBufferTextStyle.Error:
175 textPrinter.ForeColor = Color.Red;
178 case ChatBufferTextStyle.OwnerSay:
179 textPrinter.ForeColor = Color.FromArgb(255, 180, 150, 0);
183 textPrinter.PrintTextLine(item.Text);
187 //Used only for non-public chat
188 private void ProcessOutgoingChat(ChatSentEventArgs e)
190 StringBuilder sb = new StringBuilder();
192 sb.AppendFormat("(channel {0}) {1}", e.Channel, client.Self.Name);
196 case ChatType.Normal:
200 case ChatType.Whisper:
201 sb.Append(" whisper: ");
205 sb.Append(" shout: ");
209 sb.Append(e.Message);
211 ChatBufferItem item = new ChatBufferItem(
212 DateTime.Now, sb.ToString(), ChatBufferTextStyle.StatusDarkBlue);
214 ProcessBufferItem(item, true);
219 private void ProcessIncomingChat(ChatEventArgs e)
221 if (string.IsNullOrEmpty(e.Message)) return;
223 if (instance.RLV.Enabled && e.Message.StartsWith("@"))
225 instance.RLV.TryProcessCMD(e);
231 StringBuilder sb = new StringBuilder();
232 // if (e.SourceType == ChatSourceType.Object) {
233 // sb.Append(e.Position + " ");
235 if (e.SourceType == ChatSourceType.Agent)
237 sb.Append(instance.Names.Get(e.SourceID, e.FromName));
241 sb.Append(e.FromName);
244 bool isEmote = e.Message.StartsWith("/me ");
251 case ChatType.Whisper:
252 sb.Append(" whispers");
256 sb.Append(" shouts");
263 if (e.SourceType == ChatSourceType.Agent && instance.RLV.RestictionActive("recvemote", e.SourceID.ToString()))
266 sb.Append(e.Message.Substring(3));
271 if (e.SourceType == ChatSourceType.Agent && !e.Message.StartsWith("/") && instance.RLV.RestictionActive("recvchat", e.SourceID.ToString()))
274 sb.Append(e.Message);
277 ChatBufferItem item = new ChatBufferItem();
278 item.Timestamp = DateTime.Now;
279 item.Text = sb.ToString();
281 switch (e.SourceType)
283 case ChatSourceType.Agent:
285 (e.FromName.EndsWith("Linden") ?
286 ChatBufferTextStyle.LindenChat : ChatBufferTextStyle.Normal);
289 case ChatSourceType.Object:
290 if (e.Type == ChatType.OwnerSay)
292 item.Style = ChatBufferTextStyle.OwnerSay;
296 item.Style = ChatBufferTextStyle.ObjectChat;
301 ProcessBufferItem(item, true);
305 public void ReprintAllText()
307 textPrinter.ClearText();
309 foreach (ChatBufferItem item in textBuffer)
311 ProcessBufferItem(item, false);
315 public void ClearInternalBuffer()
320 public ITextPrinter TextPrinter
322 get { return textPrinter; }
323 set { textPrinter = value; }