M
mem
Im coding an irc bot to alert channels of new torrent releases.
threading is needed to reply to server pings else we are disconnected.
while the threads are started, they dont print anything until input is
recieved on $sock (my raw connection). complete example code below. All
help is greatly appreciated, thanks.
use threads;
use threads::shared;
use IO::Socket;
$server = "au.austnet.org";
$login = $nick = "perlbot";
my $chan = "#test1";
@arr_a = ("q", "w", "e", "r", "t", "y", "a", "s", "d", "f");
@arr_b = ("1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
print "\n### Connecting to irc server: $server\n\n";
our $sock = new IO::Socket::INET
(
PeerAddr => $server,
PeerPort => 6667,
Proto => 'tcp'
) or die "!!! Error Couldnt connect to $server\n";
print "### Send login information\n";
print $main::sock "NICK $nick\r\n";
print $main::sock "USER $login 8 * :argghhh\r\n";
# wait for login to be confirmed
while ($input = <$main::sock>)
{
if ($input =~ /004/)
{
# 004 = login confirmed
last;
}
elsif ($input =~ /433/)
{
die "Nickname is already in use.";
}
elsif ($input =~ /^PING \.*)$/i)
{
# We must respond to PINGs to avoid being disconnected.
print $sock "PONG $1\r\n";
print "*** PONG :$1\r\n";
}
}
print "### Connected to $server\n\n";
print $sock "JOIN $chan\r\n";
print "### Joined $chan\n";
while ($input = <$sock>)
{
if ($input =~ /^PING \.*)$/i)
{
# We must respond to PINGs to avoid being disconnected.
print $sock "PONG $1\r\n";
print "*** PONG :$1\r\n";
}
elsif($input =~ /^\.*?)\!.*? PRIVMSG (\#.*?)\s+\:\!test1/)
{
$user = lc $1;
push @thr, threads->create(\&speak_arr, $user, @arr_a)->detach;
}
elsif($input =~ /^\.*?)\!.*? PRIVMSG (\#.*?)\s+\:\!test2/)
{
$user = lc $1;
push @thr, threads->create(\&speak_arr, $user, @arr_b)->detach;
}
}
exit;
sub speak_arr
{
my ($chan, @arr) = @_;
for my $line(@arr)
{
print $main::sock "PRIVMSG $chan :$line\r\n";
print ">>> $nick \@ $chan | $line\n";
sleep 2;
}
}
threading is needed to reply to server pings else we are disconnected.
while the threads are started, they dont print anything until input is
recieved on $sock (my raw connection). complete example code below. All
help is greatly appreciated, thanks.
use threads;
use threads::shared;
use IO::Socket;
$server = "au.austnet.org";
$login = $nick = "perlbot";
my $chan = "#test1";
@arr_a = ("q", "w", "e", "r", "t", "y", "a", "s", "d", "f");
@arr_b = ("1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
print "\n### Connecting to irc server: $server\n\n";
our $sock = new IO::Socket::INET
(
PeerAddr => $server,
PeerPort => 6667,
Proto => 'tcp'
) or die "!!! Error Couldnt connect to $server\n";
print "### Send login information\n";
print $main::sock "NICK $nick\r\n";
print $main::sock "USER $login 8 * :argghhh\r\n";
# wait for login to be confirmed
while ($input = <$main::sock>)
{
if ($input =~ /004/)
{
# 004 = login confirmed
last;
}
elsif ($input =~ /433/)
{
die "Nickname is already in use.";
}
elsif ($input =~ /^PING \.*)$/i)
{
# We must respond to PINGs to avoid being disconnected.
print $sock "PONG $1\r\n";
print "*** PONG :$1\r\n";
}
}
print "### Connected to $server\n\n";
print $sock "JOIN $chan\r\n";
print "### Joined $chan\n";
while ($input = <$sock>)
{
if ($input =~ /^PING \.*)$/i)
{
# We must respond to PINGs to avoid being disconnected.
print $sock "PONG $1\r\n";
print "*** PONG :$1\r\n";
}
elsif($input =~ /^\.*?)\!.*? PRIVMSG (\#.*?)\s+\:\!test1/)
{
$user = lc $1;
push @thr, threads->create(\&speak_arr, $user, @arr_a)->detach;
}
elsif($input =~ /^\.*?)\!.*? PRIVMSG (\#.*?)\s+\:\!test2/)
{
$user = lc $1;
push @thr, threads->create(\&speak_arr, $user, @arr_b)->detach;
}
}
exit;
sub speak_arr
{
my ($chan, @arr) = @_;
for my $line(@arr)
{
print $main::sock "PRIVMSG $chan :$line\r\n";
print ">>> $nick \@ $chan | $line\n";
sleep 2;
}
}