aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Environment/Modules/ChatModule.cs
diff options
context:
space:
mode:
authorSean Dague2007-10-22 15:37:54 +0000
committerSean Dague2007-10-22 15:37:54 +0000
commit6acaabefc32525aed7ded45fc15e6152895d5382 (patch)
tree1350ddbde54e72c8d96719580a416cc2f0d85d24 /OpenSim/Region/Environment/Modules/ChatModule.cs
parent* Major ass commit. (diff)
downloadopensim-SC_OLD-6acaabefc32525aed7ded45fc15e6152895d5382.zip
opensim-SC_OLD-6acaabefc32525aed7ded45fc15e6152895d5382.tar.gz
opensim-SC_OLD-6acaabefc32525aed7ded45fc15e6152895d5382.tar.bz2
opensim-SC_OLD-6acaabefc32525aed7ded45fc15e6152895d5382.tar.xz
revert r2162 as it completely clobbered all the work on
the ChatModule by MW and myself. Couldn't find Adam online after that rev went in.
Diffstat (limited to 'OpenSim/Region/Environment/Modules/ChatModule.cs')
-rw-r--r--OpenSim/Region/Environment/Modules/ChatModule.cs381
1 files changed, 214 insertions, 167 deletions
diff --git a/OpenSim/Region/Environment/Modules/ChatModule.cs b/OpenSim/Region/Environment/Modules/ChatModule.cs
index 1f488b0..58b9d7b 100644
--- a/OpenSim/Region/Environment/Modules/ChatModule.cs
+++ b/OpenSim/Region/Environment/Modules/ChatModule.cs
@@ -45,55 +45,26 @@ namespace OpenSim.Region.Environment.Modules
45 private List<Scene> m_scenes = new List<Scene>(); 45 private List<Scene> m_scenes = new List<Scene>();
46 private LogBase m_log; 46 private LogBase m_log;
47 47
48 private string m_server = null;
49 private int m_port = 6668;
50 private string m_user = "USER OpenSimBot 8 * :I'm a OpenSim to irc bot";
51 private string m_nick = null;
52 private string m_channel = null;
53
54 private int m_whisperdistance = 10; 48 private int m_whisperdistance = 10;
55 private int m_saydistance = 30; 49 private int m_saydistance = 30;
56 private int m_shoutdistance = 100; 50 private int m_shoutdistance = 100;
57 51
58 private NetworkStream m_stream; 52 private IRCChatModule m_irc = null;
59 private TcpClient m_irc;
60 private StreamWriter m_ircWriter;
61 private StreamReader m_ircReader;
62
63 private Thread pingSender;
64 private Thread listener;
65
66 private bool m_enable_irc = false;
67 private bool connected = false;
68 53
69 public ChatModule() 54 public ChatModule()
70 { 55 {
71 m_nick = "OSimBot" + Util.RandomClass.Next(1, 99);
72 m_irc = null;
73 m_ircWriter = null;
74 m_ircReader = null;
75
76 m_log = OpenSim.Framework.Console.MainLog.Instance; 56 m_log = OpenSim.Framework.Console.MainLog.Instance;
77 } 57 }
78 58
79 public void Initialise(Scene scene, Nini.Config.IConfigSource config) 59 public void Initialise(Scene scene, Nini.Config.IConfigSource config)
80 { 60 {
61 // wrap this in a try block so that defaults will work if
62 // the config file doesn't specify otherwise.
81 try { 63 try {
82 m_server = config.Configs["IRC"].GetString("server"); 64 m_whisperdistance = config.Configs["Chat"].GetInt("whisper_distance", m_whisperdistance);
83 m_nick = config.Configs["IRC"].GetString("nick"); 65 m_saydistance = config.Configs["Chat"].GetInt("say_distance", m_saydistance);
84 m_channel = config.Configs["IRC"].GetString("channel"); 66 m_shoutdistance = config.Configs["Chat"].GetInt("shout_distance", m_shoutdistance);
85 m_port = config.Configs["IRC"].GetInt("port", m_port); 67 } catch (Exception e) {}
86 m_user = config.Configs["IRC"].GetString("username", m_user);
87 if (m_server != null && m_nick != null && m_channel != null) {
88 m_enable_irc = true;
89 }
90 } catch (Exception e) {
91 OpenSim.Framework.Console.MainLog.Instance.Verbose("No IRC config information, skipping IRC bridge configuration");
92 }
93
94 m_whisperdistance = config.Configs["Chat"].GetInt("whisper_distance");
95 m_saydistance = config.Configs["Chat"].GetInt("say_distance");
96 m_shoutdistance = config.Configs["Chat"].GetInt("shout_distance");
97 68
98 if (!m_scenes.Contains(scene)) 69 if (!m_scenes.Contains(scene))
99 { 70 {
@@ -101,43 +72,21 @@ namespace OpenSim.Region.Environment.Modules
101 scene.EventManager.OnNewClient += NewClient; 72 scene.EventManager.OnNewClient += NewClient;
102 scene.RegisterModuleInterface<ISimChat>(this); 73 scene.RegisterModuleInterface<ISimChat>(this);
103 } 74 }
75
76 // setup IRC Relay
77 m_irc = new IRCChatModule(config);
104 } 78 }
105 79
106 public void PostInitialise() 80 public void PostInitialise()
107 { 81 {
108 if( m_enable_irc ) { 82
109 try 83 if (m_irc.Enabled) {
110 { 84 m_irc.Connect(m_scenes);
111 m_irc = new TcpClient(m_server, m_port);
112 m_stream = m_irc.GetStream();
113 m_ircReader = new StreamReader(m_stream);
114 m_ircWriter = new StreamWriter(m_stream);
115
116 pingSender = new Thread(new ThreadStart(this.PingRun));
117 pingSender.Start();
118
119 listener = new Thread(new ThreadStart(this.ListenerRun));
120 listener.Start();
121
122 m_ircWriter.WriteLine(m_user);
123 m_ircWriter.Flush();
124 m_ircWriter.WriteLine("NICK " + m_nick);
125 m_ircWriter.Flush();
126 m_ircWriter.WriteLine("JOIN " + m_channel);
127 m_ircWriter.Flush();
128 connected = true;
129 }
130 catch (Exception e)
131 {
132 Console.WriteLine(e.ToString());
133 }
134 } 85 }
135 } 86 }
136 87
137 public void Close() 88 public void Close()
138 { 89 {
139 m_ircWriter.Close();
140 m_ircReader.Close();
141 m_irc.Close(); 90 m_irc.Close();
142 } 91 }
143 92
@@ -156,43 +105,6 @@ namespace OpenSim.Region.Environment.Modules
156 client.OnChatFromViewer += SimChat; 105 client.OnChatFromViewer += SimChat;
157 } 106 }
158 107
159 public void PingRun()
160 {
161 while (true)
162 {
163 m_ircWriter.WriteLine("PING :" + m_server);
164 m_ircWriter.Flush();
165 Thread.Sleep(15000);
166 }
167 }
168
169 public void ListenerRun()
170 {
171 string inputLine;
172 LLVector3 pos = new LLVector3(128, 128, 20);
173 while (true)
174 {
175 while ((inputLine = m_ircReader.ReadLine()) != null)
176 {
177 Console.WriteLine(inputLine);
178 if (inputLine.Contains(m_channel))
179 {
180 string mess = inputLine.Substring(inputLine.IndexOf(m_channel));
181 foreach (Scene m_scene in m_scenes)
182 {
183 m_scene.Broadcast(delegate(IClientAPI client)
184 {
185 client.SendChatMessage(
186 Helpers.StringToField(mess), 255, pos, "IRC:",
187 LLUUID.Zero);
188 });
189 }
190 }
191 }
192 Thread.Sleep(50);
193 }
194 }
195
196 public void SimChat(Object sender, ChatFromViewerArgs e) 108 public void SimChat(Object sender, ChatFromViewerArgs e)
197 { 109 {
198 ScenePresence avatar = null; 110 ScenePresence avatar = null;
@@ -246,80 +158,215 @@ namespace OpenSim.Region.Environment.Modules
246 158
247 m_log.Verbose("CHAT", fromName + " (" + e.Channel + " @ " + scene.RegionInfo.RegionName + ") " + typeName + ": " + e.Message); 159 m_log.Verbose("CHAT", fromName + " (" + e.Channel + " @ " + scene.RegionInfo.RegionName + ") " + typeName + ": " + e.Message);
248 160
249 if (connected) 161 if (m_irc.Connected)
250 { 162 {
251 try 163 m_irc.PrivMsg(fromName, scene.RegionInfo.RegionName, e.Message);
252 { 164 }
253 m_ircWriter.WriteLine("PRIVMSG " + m_channel + " :" + "<" + fromName + " in " + scene.RegionInfo.RegionName + ">: " + 165
254 e.Message); 166 if (e.Channel == 0)
255 m_ircWriter.Flush(); 167 {
168 foreach (Scene m_scene in m_scenes)
169 {
170 m_scene.ForEachScenePresence(delegate(ScenePresence presence)
171 {
172 int dis = -100000;
173
174 LLVector3 avatarRegionPos = presence.AbsolutePosition + new LLVector3(scene.RegionInfo.RegionLocX * 256, scene.RegionInfo.RegionLocY * 256, 0);
175 dis = Math.Abs((int)avatarRegionPos.GetDistanceTo(fromRegionPos));
176
177 switch (e.Type)
178 {
179 case ChatTypeEnum.Whisper:
180 if (dis < m_whisperdistance)
181 {
182 //should change so the message is sent through the avatar rather than direct to the ClientView
183 presence.ControllingClient.SendChatMessage(message,
184 type,
185 fromPos,
186 fromName,
187 fromAgentID);
188 }
189 break;
190 case ChatTypeEnum.Say:
191 if (dis < m_saydistance)
192 {
193 //Console.WriteLine("sending chat");
194 presence.ControllingClient.SendChatMessage(message,
195 type,
196 fromPos,
197 fromName,
198 fromAgentID);
199 }
200 break;
201 case ChatTypeEnum.Shout:
202 if (dis < m_shoutdistance)
203 {
204 presence.ControllingClient.SendChatMessage(message,
205 type,
206 fromPos,
207 fromName,
208 fromAgentID);
209 }
210 break;
211
212 case ChatTypeEnum.Broadcast:
213 presence.ControllingClient.SendChatMessage(message, type,
214 fromPos,
215 fromName,
216 fromAgentID);
217 break;
218 default:
219 break;
220 }
221 });
256 } 222 }
257 catch (IOException) 223 }
258 { 224 }
259 m_log.Error("IRC","Disconnected from IRC server."); 225 }
260 listener.Abort(); 226
261 pingSender.Abort(); 227 class IRCChatModule {
262 connected = false; 228 private string m_server = null;
229 private int m_port = 6668;
230 private string m_user = "USER OpenSimBot 8 * :I'm a OpenSim to irc bot";
231 private string m_nick = null;
232 private string m_channel = null;
233
234 private NetworkStream m_stream;
235 private TcpClient m_tcp;
236 private StreamWriter m_writer;
237 private StreamReader m_reader;
238
239 private Thread pingSender;
240 private Thread listener;
241
242 private bool m_enabled = false;
243 private bool m_connected = false;
244
245 private List<Scene> m_scenes = null;
246 private LogBase m_log;
247
248 public IRCChatModule(Nini.Config.IConfigSource config) {
249 m_nick = "OSimBot" + Util.RandomClass.Next(1, 99);
250 m_tcp = null;
251 m_writer = null;
252 m_reader = null;
253
254 try {
255 m_server = config.Configs["IRC"].GetString("server");
256 m_nick = config.Configs["IRC"].GetString("nick");
257 m_channel = config.Configs["IRC"].GetString("channel");
258 m_port = config.Configs["IRC"].GetInt("port", m_port);
259 m_user = config.Configs["IRC"].GetString("username", m_user);
260 if (m_server != null && m_nick != null && m_channel != null) {
261 m_enabled = true;
263 } 262 }
263 } catch (Exception e) {
264 Console.WriteLine("No IRC config information, skipping IRC bridge configuration");
264 } 265 }
266 m_log = OpenSim.Framework.Console.MainLog.Instance;
267 }
265 268
266 if (e.Channel == 0) 269 public bool Connect(List<Scene> scenes) {
270 try {
271 m_scenes = scenes;
272
273 m_tcp = new TcpClient(m_server, m_port);
274 m_log.Verbose("IRC", "Connecting...");
275 m_stream = m_tcp.GetStream();
276 m_log.Verbose("IRC", "Connected to " + m_server);
277 m_reader = new StreamReader(m_stream);
278 m_writer = new StreamWriter(m_stream);
279
280 pingSender = new Thread(new ThreadStart(this.PingRun));
281 pingSender.Start();
282
283 listener = new Thread(new ThreadStart(this.ListenerRun));
284 listener.Start();
285
286 m_writer.WriteLine(m_user);
287 m_writer.Flush();
288 m_writer.WriteLine("NICK " + m_nick);
289 m_writer.Flush();
290 m_writer.WriteLine("JOIN " + m_channel);
291 m_writer.Flush();
292 m_log.Verbose("IRC", "Connection fully established");
293 m_connected = true;
294 } catch (Exception e) {
295 Console.WriteLine(e.ToString());
296 }
297 return m_connected;
298 }
299
300 public bool Enabled
301 {
302 get { return m_enabled; }
303 }
304
305 public bool Connected
306 {
307 get { return m_connected; }
308 }
309
310 public void PrivMsg(string from, string region, string msg) {
311 try {
312 m_writer.WriteLine("PRIVMSG {0} :<{1} in {2}>: {3}", m_channel, from, region, msg);
313 m_writer.Flush();
314 } catch (IOException) {
315 m_log.Error("IRC","Disconnected from IRC server.");
316 listener.Abort();
317 pingSender.Abort();
318 m_connected = false;
319 }
320 }
321
322
323 public void PingRun()
324 {
325 while (true)
326 {
327 m_writer.WriteLine("PING :" + m_server);
328 m_writer.Flush();
329 Thread.Sleep(15000);
330 }
331 }
332
333 public void ListenerRun()
334 {
335 string inputLine;
336 LLVector3 pos = new LLVector3(128, 128, 20);
337 while (true)
267 { 338 {
268 foreach (Scene m_scene in m_scenes) 339 while ((inputLine = m_reader.ReadLine()) != null)
269 { 340 {
270 m_scene.ForEachScenePresence(delegate(ScenePresence presence) 341 Console.WriteLine(inputLine);
271 { 342 if (inputLine.Contains(m_channel))
272 int dis = -100000; 343 {
273 344 string mess = inputLine.Substring(inputLine.IndexOf(m_channel));
274 LLVector3 avatarRegionPos = presence.AbsolutePosition + new LLVector3(scene.RegionInfo.RegionLocX * 256, scene.RegionInfo.RegionLocY * 256, 0); 345 foreach (Scene m_scene in m_scenes)
275 dis = Math.Abs((int)avatarRegionPos.GetDistanceTo(fromRegionPos)); 346 {
276 347 m_scene.ForEachScenePresence(delegate(ScenePresence avatar)
277 switch (e.Type) 348 {
278 { 349 if (!avatar.IsChildAgent)
279 case ChatTypeEnum.Whisper: 350 {
280 if (dis < m_whisperdistance) 351 avatar.ControllingClient.SendChatMessage(
281 { 352 Helpers.StringToField(mess), 255, pos, "IRC:",
282 //should change so the message is sent through the avatar rather than direct to the ClientView 353 LLUUID.Zero);
283 presence.ControllingClient.SendChatMessage(message,
284 type,
285 fromPos,
286 fromName,
287 fromAgentID);
288 }
289 break;
290 default:
291 case ChatTypeEnum.Say:
292 if (dis < m_saydistance)
293 {
294 //Console.WriteLine("sending chat");
295 presence.ControllingClient.SendChatMessage(message,
296 type,
297 fromPos,
298 fromName,
299 fromAgentID);
300 }
301 break;
302 case ChatTypeEnum.Shout:
303 if (dis < m_shoutdistance)
304 {
305 presence.ControllingClient.SendChatMessage(message,
306 type,
307 fromPos,
308 fromName,
309 fromAgentID);
310 } 354 }
311 break; 355 });
312 356 }
313 case ChatTypeEnum.Broadcast: 357 }
314 presence.ControllingClient.SendChatMessage(message, type,
315 fromPos,
316 fromName,
317 fromAgentID);
318 break;
319 }
320 });
321 } 358 }
359 Thread.Sleep(50);
322 } 360 }
323 } 361 }
362
363
364 public void Close() {
365 listener.Abort();
366 pingSender.Abort();
367 m_writer.Close();
368 m_reader.Close();
369 m_tcp.Close();
370 }
324 } 371 }
325} 372}