help with threads please, example included

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;

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top