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