Thanks kinoc for your improved IRC Gateway patch as referenced in mantis issue 390.

ThreadPoolClientBranch
Teravus Ovares 2008-01-29 04:35:01 +00:00
parent 47761a4a5e
commit da0fa4253b
2 changed files with 81 additions and 23 deletions

View File

@ -32,6 +32,7 @@ Patches
* BigFootAg * BigFootAg
* CharlieO * CharlieO
* jhurliman (LLSD Login) * jhurliman (LLSD Login)
* kinoc
LSL Devs LSL Devs

View File

@ -163,6 +163,12 @@ namespace OpenSim.Region.Environment.Modules
fromAgentID = e.Sender.AgentId; fromAgentID = e.Sender.AgentId;
} }
// Try to reconnect to server if not connected
if ((m_irc.Enabled)&&(!m_irc.Connected))
{
m_irc.Connect(m_scenes);
}
if (e.Message.Length > 0) if (e.Message.Length > 0)
{ {
if (m_irc.Connected && (avatar != null)) // this is to keep objects from talking to IRC if (m_irc.Connected && (avatar != null)) // this is to keep objects from talking to IRC
@ -205,6 +211,7 @@ namespace OpenSim.Region.Environment.Modules
private bool m_connected = false; private bool m_connected = false;
private List<Scene> m_scenes = null; private List<Scene> m_scenes = null;
private List<Scene> m_last_scenes = null;
private LogBase m_log; private LogBase m_log;
public IRCChatModule(IConfigSource config) public IRCChatModule(IConfigSource config)
@ -214,6 +221,19 @@ namespace OpenSim.Region.Environment.Modules
m_writer = null; m_writer = null;
m_reader = null; m_reader = null;
// configuration in OpenSim.ini
// [IRC]
// server = chat.freenode.net
// nick = OSimBot_mysim
// ;username = OSimBot_mysim
// channel = #opensim-regions
// port = 6667
//
// Traps I/O disconnects so it does not crash the sim
// Trys to reconnect if disconnected and someone says something
// Tells IRC server "QUIT" when doing a close (just to be nice)
// Default port back to 6667
try try
{ {
m_server = config.Configs["IRC"].GetString("server"); m_server = config.Configs["IRC"].GetString("server");
@ -238,7 +258,7 @@ namespace OpenSim.Region.Environment.Modules
try try
{ {
m_scenes = scenes; m_scenes = scenes;
m_last_scenes = scenes;
m_tcp = new TcpClient(m_server, (int) m_port); m_tcp = new TcpClient(m_server, (int) m_port);
m_log.Verbose("IRC", "Connecting..."); m_log.Verbose("IRC", "Connecting...");
m_stream = m_tcp.GetStream(); m_stream = m_tcp.GetStream();
@ -290,14 +310,20 @@ namespace OpenSim.Region.Environment.Modules
m_log.Error("IRC", "Disconnected from IRC server."); m_log.Error("IRC", "Disconnected from IRC server.");
listener.Abort(); listener.Abort();
pingSender.Abort(); pingSender.Abort();
m_writer.Close();
m_reader.Close();
m_tcp.Close();
m_connected = false; m_connected = false;
if (m_enabled) { Connect(m_last_scenes); }
} }
} }
private Dictionary<string, string> ExtractMsg(string input) private Dictionary<string, string> ExtractMsg(string input)
{ {
m_log.Verbose("IRC", "ExtractMsg: " + input);
Dictionary<string, string> result = null; Dictionary<string, string> result = null;
string regex = @":(?<nick>\w*)!~(?<user>\S*) PRIVMSG (?<channel>\S+) :(?<msg>.*)"; //string regex = @":(?<nick>\w*)!~(?<user>\S*) PRIVMSG (?<channel>\S+) :(?<msg>.*)";
string regex = @":(?<nick>\w*)!(?<user>\S*) PRIVMSG (?<channel>\S+) :(?<msg>.*)";
Regex RE = new Regex(regex, RegexOptions.Multiline); Regex RE = new Regex(regex, RegexOptions.Multiline);
MatchCollection matches = RE.Matches(input); MatchCollection matches = RE.Matches(input);
// Get some direct matches $1 $4 is a // Get some direct matches $1 $4 is a
@ -324,10 +350,24 @@ namespace OpenSim.Region.Environment.Modules
{ {
while (true) while (true)
{ {
m_writer.WriteLine("PING :" + m_server); try
m_writer.Flush(); {
Thread.Sleep(15000); m_writer.WriteLine("PING :" + m_server);
} m_writer.Flush();
Thread.Sleep(15000);
}
catch (IOException)
{
m_log.Error("IRC", "Disconnected from IRC server.");
listener.Abort();
pingSender.Abort();
m_writer.Close();
m_reader.Close();
m_tcp.Close();
m_connected = false;
if (m_enabled) { Connect(m_last_scenes); }
}
}
} }
public void ListenerRun() public void ListenerRun()
@ -336,36 +376,53 @@ namespace OpenSim.Region.Environment.Modules
LLVector3 pos = new LLVector3(128, 128, 20); LLVector3 pos = new LLVector3(128, 128, 20);
while (true) while (true)
{ {
while ((inputLine = m_reader.ReadLine()) != null) try
{ {
// Console.WriteLine(inputLine); while ((inputLine = m_reader.ReadLine()) != null)
if (inputLine.Contains(m_channel))
{ {
Dictionary<string, string> data = ExtractMsg(inputLine); // Console.WriteLine(inputLine);
if (data != null) if (inputLine.Contains(m_channel))
{ {
foreach (Scene m_scene in m_scenes) Dictionary<string, string> data = ExtractMsg(inputLine);
if (data != null)
{ {
m_scene.ForEachScenePresence(delegate(ScenePresence avatar) foreach (Scene m_scene in m_scenes)
{ {
if (!avatar.IsChildAgent) m_scene.ForEachScenePresence(delegate(ScenePresence avatar)
{ {
avatar.ControllingClient.SendChatMessage( if (!avatar.IsChildAgent)
Helpers.StringToField(data["msg"]), 255, {
pos, data["nick"], avatar.ControllingClient.SendChatMessage(
LLUUID.Zero); Helpers.StringToField(data["msg"]), 255,
} pos, data["nick"],
}); LLUUID.Zero);
}
});
}
} }
} }
} }
Thread.Sleep(50);
} }
Thread.Sleep(50); catch (IOException)
{
m_log.Error("IRC", "Disconnected from IRC server.");
listener.Abort();
pingSender.Abort();
m_writer.Close();
m_reader.Close();
m_tcp.Close();
m_connected = false;
if (m_enabled) { Connect(m_last_scenes); }
}
} }
} }
public void Close() public void Close()
{ {
m_writer.WriteLine("QUIT :" + m_nick+" to "+m_channel+" wormhole with "+m_server+" closing");
m_writer.Flush();
listener.Abort(); listener.Abort();
pingSender.Abort(); pingSender.Abort();
m_writer.Close(); m_writer.Close();